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

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

Petit bugs ! Reza 12/7/99

File size: 13.8 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();
[330]26 virtual void Show();
[293]27 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]28 inline void AddHelpGroup(const char * hgrp, int gid)
29 { hgrpom->AppendItem(hgrp, 20000+gid); }
30 inline void ClearHelpList()
31 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]32 inline void AddHelpItem(const char * hitem)
[330]33 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[293]34protected :
35 PIStdImgApp* dap;
36 PIACmd* piac;
37 int mNitem;
38 PIList* hitemlist;
[330]39 PIOptMenu* hgrpom;
[293]40 PIButton * mBut;
41 PILabel * mLab;
42 PIText* mTxt;
43};
[165]44
[293]45/* --Methode-- */
46PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
47 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
48{
49dap = par;
50piac = piacmd;
51mNitem = 0;
[330]52SetMsg(77);
[165]53
[293]54int bsx, bsy;
55int tsx, tsy;
56int spx, spy;
57PIApplicationPrefCompSize(bsx, bsy);
58spx = bsx/6; spy = bsy/6;
59tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
60SetSize(tsx,tsy);
[330]61hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
62hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
63hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
[293]64hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
65// hitemlist->SetBorderWidth(2);
[324]66mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
67// mTxt->SetMutiLineMode(true);
[293]68mTxt->SetTextEditable(false);
69mTxt->SetText("");
70mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
71mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
72mLab->SetBorderWidth(1);
73mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
74mLab->SetLabel("");
[330]75mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]76mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
77}
[165]78
79/* --Methode-- */
[293]80PIAHelpWind::~PIAHelpWind()
81{
[330]82delete hgrpom;
[293]83delete hitemlist;
84delete mTxt;
85delete mLab;
86delete mBut;
87}
88
89/* --Methode-- */
90void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
91{
92PIMessage um = UserMsg(msg);
[330]93if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]94 Hide();
95 return;
96 }
[330]97else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
98 mTxt->SetText("");
99 mLab->SetLabel("");
100 piac->UpdateHelpList(this, um-20000);
101}
102else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]103 string s = hitemlist->GetSelectionStr();
104 mTxt->SetText(piac->GetUsage(s));
105 mLab->SetLabel(s);
106 }
107}
108
[330]109/* --Methode-- */
110void PIAHelpWind::Show()
111{
112hgrpom->SetValue(20000); // Groupe All
113mTxt->SetText("");
114mLab->SetLabel("");
115piac->UpdateHelpList(this, 0);
116PIWindow::Show();
117}
[293]118
119static PIACmd* curpiacmd = NULL;
120/* --Methode-- */
[165]121PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
122{
[293]123mObjMgr = omg;
[165]124mImgApp = app;
125system("cp history.pic hisold.pic");
126hist.open("history.pic");
127trace = false; timing = false;
128gltimer = NULL;
[293]129
[330]130cmdhgrp["All"] = 0;
131cmdgrpid = 1;
132cmdhgrp["Commands"] = 1;
[293]133helpwin = new PIAHelpWind(app, this);
[330]134helpwin->AddHelpGroup("All", 0);
135helpwin->AddHelpGroup("Commands", 1);
[293]136
137string kw = "piacmd";
138string usage;
139usage = ">>> (piacmd) Interpreter's keywords : \n";
140usage += " timingon timingoff traceon traceoff \n";
141usage += " set unset listvar listcommands exec shell \n";
142usage += " > set varname 'string' # To set a variable, $varname \n";
[333]143usage += " > setol varname patt # Fills varname with object list \n";
[293]144usage += " > unset varname # clear variable definition \n";
[333]145usage += " > echo string # output string \n";
[293]146usage += " > listvars # List of variable names and values \n";
147usage += " > listcommands # List of all known commands \n";
148usage += " > exec filename # Execute commands from file \n";
149usage += " > shell comand_string # Execute shell command \n";
150usage += " > help <command_name> # <command_name> usage info \n";
151usage += " > helpwindow # Displays help window \n";
[330]152string grp = "Commands";
153RegisterHelp(kw, usage, grp);
[293]154
155basexec = new PIABaseExecutor(this, omg, app);
156AddInterpreter(this);
157curcmdi = this;
[165]158}
159
160/* --Methode-- */
161PIACmd::~PIACmd()
162{
163hist.close();
164if (gltimer) { delete gltimer; gltimer = NULL; }
[293]165Modmap::iterator it;
166for(it = modmap.begin(); it != modmap.end(); it++) {
167 string name = (*it).first + "_end";
168 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
169 if (fend) fend();
170 delete (*it).second;
171 }
172delete helpwin;
173if (curpiacmd == this) curpiacmd = NULL;
[165]174}
175
176/* --Methode-- */
[293]177PIACmd* PIACmd::GetInterpreter()
[165]178{
[293]179return(curpiacmd);
180}
[165]181
[293]182/* --Methode-- */
183string PIACmd::Name()
184{
185return("piacmd");
186}
[165]187
[293]188/* --Methode-- */
[330]189void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]190{
[330]191if (!ce) {
192 RegisterHelp(keyw, usage, grp);
193 return;
194 }
195int gid = CheckHelpGrp(grp);
[293]196cmdex cme;
[330]197cme.group = gid;
[293]198cme.us = usage;
199cme.cex = ce;
200cmdexmap[keyw] = cme;
201}
202
203/* --Methode-- */
[330]204void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
205{
206int gid = CheckHelpGrp(grp);
207cmdex cme;
208cme.group = gid;
209cme.us = usage;
210cme.cex = NULL;
211helpexmap[keyw] = cme;
212}
213
214/* --Methode-- */
215int PIACmd::CheckHelpGrp(string& grp)
216{
217int gid=0;
218CmdHGroup::iterator it = cmdhgrp.find(grp);
219if (it == cmdhgrp.end()) {
220 cmdgrpid++; gid = cmdgrpid;
221 cmdhgrp[grp] = gid;
222 helpwin->AddHelpGroup(grp.c_str(), gid);
223 }
224else gid = (*it).second;
225return(gid);
226}
227
228/* --Methode-- */
229void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
230{
231helpwin->ClearHelpList();
232CmdExmap::iterator it;
233for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
234 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
235 helpwin->AddHelpItem((*it).first.c_str());
236 }
237for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
238 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
239 helpwin->AddHelpItem((*it).first.c_str());
240 }
241}
242
243/* --Methode-- */
[293]244void PIACmd::LoadModule(string& fnameso, string& name)
245{
246PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
247if (dynlink == NULL) {
248 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
249 return;
[165]250 }
[293]251string fname = name + "_init";
252DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
253if (!finit) {
254 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
255 return;
[165]256 }
[293]257cout << "PIACmd/LoadModule_Info: Initialisation module" << name
258 << " " << fname << "() ..." << endl;
259finit();
260modmap[name] = dynlink;
261return;
262}
[165]263
[293]264/* --Methode-- */
265void PIACmd::AddInterpreter(CmdInterpreter * cl)
266{
267if (!cl) return;
268interpmap[cl->Name()] = cl;
[165]269}
270
271/* --Methode-- */
[293]272void PIACmd::SelInterpreter(string& name)
[165]273{
[293]274InterpMap::iterator it = interpmap.find(name);
275if (it == interpmap.end()) return;
276curcmdi = (*it).second;
277}
[165]278
[293]279
280// Pour le decoupage des commandes en lignes
281typedef vector<string> cmdtok;
282
283/* --Methode-- */
284int PIACmd::Interpret(string& s)
285{
[333]286int rc = 0;
[165]287cmdtok tokens;
288if (s.length() < 1) return(0);
289
290hist << s << endl; // On enregistre les commandes
291
292if (s[0] == '#') {
[293]293 cout << "PIACmd::Interpret() Comment-Line:" << s << endl;
[165]294 return(0);
295 }
296string toks,kw;
297size_t p = s.find_first_not_of(" ");
298s = s.substr(p);
299p = 0;
300size_t q = s.find_first_of(" ");
301size_t l = s.length();
302
303if (q < l)
304 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
305else { kw = s.substr(p,l-p); toks = ""; }
306
307q = 0;
308while (q < l) {
309 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
310 if (p>=l) break;
311 q = toks.find_first_of(" ",p); // la fin du token;
312 string token = toks.substr(p,q-p);
313 tokens.push_back(token);
314 }
315
316for(int k=0; k<tokens.size(); k++) { // On remplace les $varname par la valeur de la variable
[293]317 if ((tokens[k])[0] != '$') continue;
318 CmdVarList::iterator it = mVars.find(tokens[k].substr(1));
319 if (it != mVars.end()) tokens[k] = (*it).second;
320 }
[165]321
322// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
323// for(int ii=0; ii<tokens.size(); ii++)
324// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
325
326// >>>>>>>>>>> Commande d'interpreteur
[293]327if (kw == "helpwindow") ShowHelpWindow();
328else if (kw == "help") {
[330]329 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]330 else {
[330]331 string kwh = "piacmd";
332 cout << GetUsage(kwh) << endl;
[293]333 }
[165]334 }
335
336else if (kw == "set") {
[293]337 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[333]338 string xx = "";
[335]339 for (int kk=1; kk<tokens.size(); kk++) xx += (tokens[kk] + ' ');
[333]340 mVars[tokens[0]] = xx;
[165]341 }
[333]342else if (kw == "setol") {
343 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
344 vector<string> ol;
345 mObjMgr->GetObjList(tokens[1], ol);
346 string vol = "";
347 for (int kk=0; kk<ol.size(); kk++) vol += (ol[kk] + ' ');
348 mVars[tokens[0]] = vol;
349 }
[293]350else if (kw == "unset") {
351 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[165]352 CmdVarList::iterator it = mVars.find(tokens[0]);
353 if (it != mVars.end()) mVars.erase(it);
[293]354 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]355 }
[333]356else if (kw == "echo") {
357 for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
358 cout << endl;
359 }
[165]360else if (kw == "listvars") {
[293]361 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[165]362 CmdVarList::iterator it;
363 for(it = mVars.begin(); it != mVars.end(); it++)
364 cout << (*it).first << " = " << (*it).second << "\n";
365 cout << endl;
366 }
[293]367else if (kw == "listvars") {
[330]368 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
[293]369 CmdExmap::iterator it;
370 int kc = 0;
371 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
372 cout << (*it).first << " ";
373 kc++;
374 if (kc >= 5) { cout << "\n"; kc = 0; }
375 }
376 cout << endl;
377 }
378else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
379else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
[165]380else if (kw == "timingon") {
[293]381 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
382 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
383 }
[165]384else if (kw == "timingoff") {
[293]385 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
386 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
387 }
[165]388else if (kw == "exec") {
[293]389 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
390 ExecFile(tokens[0]);
[165]391 }
392else if (kw == "shell") {
[293]393 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[165]394 system(toks.c_str());
395 }
[293]396// Execution d'une commande enregistree
[333]397else rc = ExecuteCommand(kw, tokens);
[165]398
399if (timing) gltimer->Split();
[333]400return(rc);
[165]401}
402
[333]403/* --Methode-- */
404int PIACmd::ExecuteCommandLine(string& line)
405{
406cmdtok tokens;
407if (line.length() < 1) return(0);
[165]408
[333]409string toks,kw;
410size_t p = line.find_first_not_of(" ");
411line = line.substr(p);
412p = 0;
413size_t q = line.find_first_of(" ");
414size_t l = line.length();
415
416if (q < l)
417 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
418else { kw = line.substr(p,l-p); toks = ""; }
419
420q = 0;
421while (q < l) {
422 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
423 if (p>=l) break;
424 q = toks.find_first_of(" ",p); // la fin du token;
425 string token = toks.substr(p,q-p);
426 tokens.push_back(token);
427 }
428
429return(ExecuteCommand(kw, tokens));
430}
431
[165]432/* --Methode-- */
[333]433int PIACmd::ExecuteCommand(string& keyw, vector<string>& args)
434{
435 int rc = -1;
436 CmdExmap::iterator it = cmdexmap.find(keyw);
437 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
438 else {
439 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args);
440 else cout << "Dont know how to execute " << keyw << " ? " << endl;
441 }
442 return(rc);
443}
444
445/* --Methode-- */
[293]446int PIACmd::ExecFile(string& file)
[165]447{
[293]448char line_buff[512];
449FILE *fip;
[165]450
[293]451if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
452 cerr << "PIACmd::Exec() Error opening file " << file << endl;
453 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
454 return(0);
[165]455 }
[293]456
457hist << "### Executing commands from " << file << endl;
458if (trace) {
459 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
460 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
461 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
462 }
[165]463
[293]464while (fgets(line_buff,511,fip) != NULL)
465 {
466 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
467 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
468 string line(line_buff);
469 Interpret(line);
[165]470 }
[293]471hist << "### End of Exec( " << file << " ) " << endl;
472if (trace) {
473 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
474 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
475 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
[165]476 }
477
478return(0);
479}
480
[293]481
482static string* videstr = NULL;
483/* --Methode-- */
484string& PIACmd::GetUsage(const string& kw)
[165]485{
[330]486bool fndok = false;
[293]487CmdExmap::iterator it = cmdexmap.find(kw);
[330]488if (it == cmdexmap.end()) {
489 it = helpexmap.find(kw);
490 if (it != helpexmap.end()) fndok = true;
[165]491 }
[330]492 else fndok = true;
493if (fndok) return( (*it).second.us );
494// Keyword pas trouve
495if (videstr == NULL) videstr = new string("");
496*videstr = "Nothing known about " + kw + " ?? ";
497return(*videstr);
498
[165]499}
500
[293]501/* --Methode-- */
502void PIACmd::ShowHelpWindow()
[165]503{
[293]504helpwin->Show();
[165]505}
Note: See TracBrowser for help on using the repository browser.