source: Sophya/trunk/SophyaPI/PIext/piacmd.cc@ 311

Last change on this file since 311 was 293, checked in by ercodmgr, 26 years ago

Adaptation aux "normes DPC", introduction des classes CmdExecutor, ...
Fenetre d'aide en lignes, gestion de modules, ... Reza 11/05/99

File size: 11.1 KB
RevLine 
[293]1#include "piacmd.h"
[165]2#include <stdio.h>
3#include <stdlib.h>
4#include <math.h>
5
[293]6#include "basexecut.h"
[165]7
[293]8#include "pdlmgr.h"
[165]9#include "ctimer.h"
[293]10// #include "dlftypes.h"
[165]11
[293]12#include "pistdimgapp.h"
[165]13#include "nobjmgr.h"
14
[293]15#include PISTDWDG_H
16#include PILIST_H
[165]17
[293]18// ------------------------------------------------------------
19// Gestion d'une fenetre d'aide interactive
20// ------------------------------------------------------------
[165]21
[293]22class PIAHelpWind : public PIWindow {
23public :
24 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
25 virtual ~PIAHelpWind();
26 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
27 inline void AddHelpItem(const char * hitem)
28 { mNitem++; hitemlist->AppendItem(hitem, 1000+mNitem); }
29protected :
30 PIStdImgApp* dap;
31 PIACmd* piac;
32 int mNitem;
33 PIList* hitemlist;
34 PIButton * mBut;
35 PILabel * mLab;
36 PIText* mTxt;
37};
[165]38
[293]39/* --Methode-- */
40PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
41 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
42{
43dap = par;
44piac = piacmd;
45mNitem = 0;
46SetMsg(777);
[165]47
[293]48int bsx, bsy;
49int tsx, tsy;
50int spx, spy;
51PIApplicationPrefCompSize(bsx, bsy);
52spx = bsx/6; spy = bsy/6;
53tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
54SetSize(tsx,tsy);
55hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-2*spy, spx/2, spy);
56hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
57// hitemlist->SetBorderWidth(2);
58mTxt = new PIText(this, "helptext", bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
59mTxt->SetMutiLineMode(true);
60mTxt->SetTextEditable(false);
61mTxt->SetText("");
62mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
63mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
64mLab->SetBorderWidth(1);
65mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
66mLab->SetLabel("");
67mBut = new PIButton(this, "Close", 700, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
68mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
69}
[165]70
71/* --Methode-- */
[293]72PIAHelpWind::~PIAHelpWind()
73{
74delete hitemlist;
75delete mTxt;
76delete mLab;
77delete mBut;
78}
79
80/* --Methode-- */
81void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
82{
83PIMessage um = UserMsg(msg);
84if (((um == 777) && (ModMsg(msg) == PIMsg_Close)) || (um == 700) ) {
85 Hide();
86 return;
87 }
88else if ( (um > 1000) && (sender == hitemlist)) {
89 string s = hitemlist->GetSelectionStr();
90 mTxt->SetText(piac->GetUsage(s));
91 mLab->SetLabel(s);
92 }
93}
94
95
96static PIACmd* curpiacmd = NULL;
97/* --Methode-- */
[165]98PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
99{
[293]100mObjMgr = omg;
[165]101mImgApp = app;
102system("cp history.pic hisold.pic");
103hist.open("history.pic");
104trace = false; timing = false;
105gltimer = NULL;
[293]106
107helpwin = new PIAHelpWind(app, this);
108
109string kw = "piacmd";
110string usage;
111usage = ">>> (piacmd) Interpreter's keywords : \n";
112usage += " timingon timingoff traceon traceoff \n";
113usage += " set unset listvar listcommands exec shell \n";
114usage += " > set varname 'string' # To set a variable, $varname \n";
115usage += " > unset varname # clear variable definition \n";
116usage += " > listvars # List of variable names and values \n";
117usage += " > listcommands # List of all known commands \n";
118usage += " > exec filename # Execute commands from file \n";
119usage += " > shell comand_string # Execute shell command \n";
120usage += " > help <command_name> # <command_name> usage info \n";
121usage += " > helpwindow # Displays help window \n";
122RegisterCommand(kw, usage, NULL);
123
124basexec = new PIABaseExecutor(this, omg, app);
125AddInterpreter(this);
126curcmdi = this;
[165]127}
128
129/* --Methode-- */
130PIACmd::~PIACmd()
131{
132hist.close();
133if (gltimer) { delete gltimer; gltimer = NULL; }
[293]134Modmap::iterator it;
135for(it = modmap.begin(); it != modmap.end(); it++) {
136 string name = (*it).first + "_end";
137 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
138 if (fend) fend();
139 delete (*it).second;
140 }
141delete helpwin;
142if (curpiacmd == this) curpiacmd = NULL;
[165]143}
144
145/* --Methode-- */
[293]146PIACmd* PIACmd::GetInterpreter()
[165]147{
[293]148return(curpiacmd);
149}
[165]150
[293]151/* --Methode-- */
152string PIACmd::Name()
153{
154return("piacmd");
155}
[165]156
[293]157/* --Methode-- */
158void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce)
159{
160 // if (!ce) return;
161cmdex cme;
162cme.us = usage;
163cme.cex = ce;
164cmdexmap[keyw] = cme;
165if (helpwin) helpwin->AddHelpItem(keyw.c_str());
166}
167
168/* --Methode-- */
169void PIACmd::LoadModule(string& fnameso, string& name)
170{
171PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
172if (dynlink == NULL) {
173 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
174 return;
[165]175 }
[293]176string fname = name + "_init";
177DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
178if (!finit) {
179 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
180 return;
[165]181 }
[293]182cout << "PIACmd/LoadModule_Info: Initialisation module" << name
183 << " " << fname << "() ..." << endl;
184finit();
185modmap[name] = dynlink;
186return;
187}
[165]188
[293]189/* --Methode-- */
190void PIACmd::AddInterpreter(CmdInterpreter * cl)
191{
192if (!cl) return;
193interpmap[cl->Name()] = cl;
[165]194}
195
196/* --Methode-- */
[293]197void PIACmd::SelInterpreter(string& name)
[165]198{
[293]199InterpMap::iterator it = interpmap.find(name);
200if (it == interpmap.end()) return;
201curcmdi = (*it).second;
202}
[165]203
[293]204
205// Pour le decoupage des commandes en lignes
206typedef vector<string> cmdtok;
207
208/* --Methode-- */
209int PIACmd::Interpret(string& s)
210{
211
[165]212cmdtok tokens;
213if (s.length() < 1) return(0);
214
215hist << s << endl; // On enregistre les commandes
216
217if (s[0] == '#') {
[293]218 cout << "PIACmd::Interpret() Comment-Line:" << s << endl;
[165]219 return(0);
220 }
221string toks,kw;
222size_t p = s.find_first_not_of(" ");
223s = s.substr(p);
224p = 0;
225size_t q = s.find_first_of(" ");
226size_t l = s.length();
227
228if (q < l)
229 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
230else { kw = s.substr(p,l-p); toks = ""; }
231
232q = 0;
233while (q < l) {
234 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
235 if (p>=l) break;
236 q = toks.find_first_of(" ",p); // la fin du token;
237 string token = toks.substr(p,q-p);
238 tokens.push_back(token);
239 }
240
241for(int k=0; k<tokens.size(); k++) { // On remplace les $varname par la valeur de la variable
[293]242 if ((tokens[k])[0] != '$') continue;
243 CmdVarList::iterator it = mVars.find(tokens[k].substr(1));
244 if (it != mVars.end()) tokens[k] = (*it).second;
245 }
[165]246
247// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
248// for(int ii=0; ii<tokens.size(); ii++)
249// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
250
251// >>>>>>>>>>> Commande d'interpreteur
[293]252if (kw == "helpwindow") ShowHelpWindow();
253else if (kw == "help") {
254 if (tokens.size() > 0) {
255 CmdExmap::iterator it = cmdexmap.find(tokens[0]);
256 if (it == cmdexmap.end()) cout << "Nothing known about " << tokens[0] << " ?? " << endl;
257 else cout << (*it).second.us << endl;
258 }
259 else {
260 cout << "\n -------- PIACmd::Interpret() ::::: Help ------------ \n";
261 cout << ">>> Interpreter's keywords : \n";
262 cout << " timingon timingoff traceon traceoff \n";
263 cout << " set unset listvar listcommands exec shell \n";
264 cout << " > set varname 'string' # To set a variable, $varname \n";
265 cout << " > unset varname # clear variable definition \n";
266 cout << " > listvars # List of variable names and values \n";
267 cout << " > listcommands # List of all known commands \n";
268 cout << " > exec filename # Execute commands from file \n";
269 cout << " > shell comand_string # Execute shell command \n";
270 cout << " > help <command_name> # <command_name> usage info \n";
271 cout << " > helpwindow # Displays help window \n" << endl;
272 }
[165]273 }
274
275
276else if (kw == "set") {
[293]277 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[165]278 mVars[tokens[0]] = tokens[1];
279 }
[293]280else if (kw == "unset") {
281 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[165]282 CmdVarList::iterator it = mVars.find(tokens[0]);
283 if (it != mVars.end()) mVars.erase(it);
[293]284 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]285 }
286else if (kw == "listvars") {
[293]287 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[165]288 CmdVarList::iterator it;
289 for(it = mVars.begin(); it != mVars.end(); it++)
290 cout << (*it).first << " = " << (*it).second << "\n";
291 cout << endl;
292 }
[293]293else if (kw == "listvars") {
294 cout << "---- PIACmd::Interpret() Command keyword List ----- \n";
295 CmdExmap::iterator it;
296 int kc = 0;
297 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
298 cout << (*it).first << " ";
299 kc++;
300 if (kc >= 5) { cout << "\n"; kc = 0; }
301 }
302 cout << endl;
303 }
304else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
305else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
[165]306else if (kw == "timingon") {
[293]307 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
308 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
309 }
[165]310else if (kw == "timingoff") {
[293]311 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
312 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
313 }
[165]314else if (kw == "exec") {
[293]315 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
316 ExecFile(tokens[0]);
[165]317 }
318else if (kw == "shell") {
[293]319 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[165]320 system(toks.c_str());
321 }
[293]322// Execution d'une commande enregistree
323else {
324 CmdExmap::iterator it = cmdexmap.find(kw);
325 if (it == cmdexmap.end()) cout << "No such command : " << kw << " ! " << endl;
326 else {
327 if ((*it).second.cex) (*it).second.cex->Execute(kw, tokens);
328 else cout << "Dont know how to execute " << kw << " ? " << endl;
[165]329 }
330 }
331
332if (timing) gltimer->Split();
333return(0);
334}
335
336
337/* --Methode-- */
[293]338int PIACmd::ExecFile(string& file)
[165]339{
[293]340char line_buff[512];
341FILE *fip;
[165]342
[293]343if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
344 cerr << "PIACmd::Exec() Error opening file " << file << endl;
345 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
346 return(0);
[165]347 }
[293]348
349hist << "### Executing commands from " << file << endl;
350if (trace) {
351 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
352 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
353 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
354 }
[165]355
[293]356while (fgets(line_buff,511,fip) != NULL)
357 {
358 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
359 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
360 string line(line_buff);
361 Interpret(line);
[165]362 }
[293]363hist << "### End of Exec( " << file << " ) " << endl;
364if (trace) {
365 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
366 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
367 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
[165]368 }
369
370return(0);
371}
372
[293]373
374static string* videstr = NULL;
375/* --Methode-- */
376string& PIACmd::GetUsage(const string& kw)
[165]377{
[293]378CmdExmap::iterator it = cmdexmap.find(kw);
379if (it == cmdexmap.end()) {
380 if (videstr == NULL) videstr = new string("");
381 return(*videstr);
[165]382 }
[293]383else return( (*it).second.us );
[165]384}
385
[293]386/* --Methode-- */
387void PIACmd::ShowHelpWindow()
[165]388{
[293]389helpwin->Show();
[165]390}
Note: See TracBrowser for help on using the repository browser.