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

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

Petites amelioration + readstdin Reza 6/8/99

File size: 23.7 KB
RevLine 
[293]1#include "piacmd.h"
[165]2#include <stdio.h>
3#include <stdlib.h>
[349]4#include <ctype.h>
[165]5#include <math.h>
6
[293]7#include "basexecut.h"
[165]8
[293]9#include "pdlmgr.h"
[165]10#include "ctimer.h"
[293]11// #include "dlftypes.h"
[165]12
[293]13#include "pistdimgapp.h"
[165]14#include "nobjmgr.h"
15
[293]16#include PISTDWDG_H
17#include PILIST_H
[165]18
[293]19// ------------------------------------------------------------
[349]20// Gestion d'une fenetre d'aide interactive
21// Classe PIAHelpWind
[293]22// ------------------------------------------------------------
[165]23
[293]24class PIAHelpWind : public PIWindow {
25public :
26 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
27 virtual ~PIAHelpWind();
[330]28 virtual void Show();
[293]29 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]30 inline void AddHelpGroup(const char * hgrp, int gid)
31 { hgrpom->AppendItem(hgrp, 20000+gid); }
32 inline void ClearHelpList()
33 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]34 inline void AddHelpItem(const char * hitem)
[330]35 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[293]36protected :
37 PIStdImgApp* dap;
38 PIACmd* piac;
39 int mNitem;
40 PIList* hitemlist;
[330]41 PIOptMenu* hgrpom;
[293]42 PIButton * mBut;
43 PILabel * mLab;
44 PIText* mTxt;
45};
[165]46
[293]47/* --Methode-- */
48PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
49 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
50{
51dap = par;
52piac = piacmd;
53mNitem = 0;
[330]54SetMsg(77);
[165]55
[293]56int bsx, bsy;
57int tsx, tsy;
58int spx, spy;
59PIApplicationPrefCompSize(bsx, bsy);
60spx = bsx/6; spy = bsy/6;
61tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
62SetSize(tsx,tsy);
[330]63hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
64hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
65hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
[293]66hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
67// hitemlist->SetBorderWidth(2);
[324]68mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
69// mTxt->SetMutiLineMode(true);
[293]70mTxt->SetTextEditable(false);
71mTxt->SetText("");
72mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
73mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
74mLab->SetBorderWidth(1);
75mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
76mLab->SetLabel("");
[330]77mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]78mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
79}
[165]80
81/* --Methode-- */
[293]82PIAHelpWind::~PIAHelpWind()
83{
[330]84delete hgrpom;
[293]85delete hitemlist;
86delete mTxt;
87delete mLab;
88delete mBut;
89}
90
91/* --Methode-- */
92void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
93{
94PIMessage um = UserMsg(msg);
[330]95if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]96 Hide();
97 return;
98 }
[330]99else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
100 mTxt->SetText("");
101 mLab->SetLabel("");
102 piac->UpdateHelpList(this, um-20000);
103}
104else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]105 string s = hitemlist->GetSelectionStr();
106 mTxt->SetText(piac->GetUsage(s));
107 mLab->SetLabel(s);
108 }
109}
110
[330]111/* --Methode-- */
112void PIAHelpWind::Show()
113{
114hgrpom->SetValue(20000); // Groupe All
115mTxt->SetText("");
116mLab->SetLabel("");
117piac->UpdateHelpList(this, 0);
118PIWindow::Show();
119}
[293]120
[349]121// ------------------------------------------------------------
122// Bloc de commandes (Foreach, ...)
123// Classe PIACmdBloc
124// ------------------------------------------------------------
125
126class PIACmdBloc {
127public:
128 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
129 ~PIACmdBloc();
130 inline PIACmdBloc* Parent() { return(parent); }
131 inline bool CheckOK() { return((typ >= 0) ? true : false); }
132 inline void AddLine(string& line)
133 { lines.push_back(line); bloclineid.push_back(lines.size()); }
134 inline void AddBloc(PIACmdBloc* blk)
135 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
136 PIACmdBloc* Execute();
137protected:
138 PIACmd* piacmd;
139 PIACmdBloc* parent;
140 int typ; // 0 foreach , 1 integer loop, 2 float loop
141 string varname;
142 vector<string> strlist;
143 vector<string> lines;
144 vector<PIACmdBloc *> blocs;
145 vector<int> bloclineid;
146 int i1,i2,di;
147 float f1,f2,df;
148};
149
150/* --Methode-- */
151PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args)
152{
153piacmd = piac;
154parent = par;
155typ = -1;
156i1 = 0; i2 = -1; di = 1;
157f1 = 0.; f2 = -1.; df = 1.;
158if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
159varname = args[0];
160if (isalpha((int)args[1][0]) ) { // This is a foreach-integer bloc
161 for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
162 typ = 0;
163 }
164else { // This is an integer or float loop
165 size_t l = args[1].length();
166 size_t p = args[1].find(':');
167 size_t pp = args[1].find('.');
168 bool fl = (pp < l) ? true : false; // Float loop or integer loop
169 if (p >= l) return; // Syntaxe error
170 string a1 = args[1].substr(0, p);
171 string aa = args[1].substr(p+1);
172 p = aa.find(':');
173 string a2, a3;
174 bool hasa3 = false;
175 if (p < aa.length() ) {
176 a2 = aa.substr(0,p);
177 a3 = aa.substr(p+1);
178 hasa3 = true;
179 }
180 else a2 = aa;
181 if (fl) {
182 typ = 2;
183 f1 = atof(a1.c_str());
184 f2 = atof(a2.c_str());
185 if (hasa3) df = atof(a3.c_str());
186 else df = 1.;
187 }
188 else {
189 typ = 1;
190 i1 = atoi(a1.c_str());
191 i2 = atoi(a2.c_str());
192 if (hasa3) di = atoi(a3.c_str());
193 else di = 1;
194 }
195 }
196}
197
198/* --Methode-- */
199PIACmdBloc::~PIACmdBloc()
200{
201for(int k=0; k<blocs.size(); k++) delete blocs[k];
202}
203
204/* --Methode-- */
205PIACmdBloc* PIACmdBloc::Execute()
206{
207// cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
208// " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
209string cmd;
210int k=0;
211int kj=0;
212int kk=0;
213char buff[32];
214if (typ == 0) // foreach string loop
215 for(k=0; k<strlist.size(); k++) {
216 cmd = "set " + varname + " " + strlist[k];
217 piacmd->Interpret(cmd);
218 for(kj=0; kj<bloclineid.size(); kj++) {
219 kk = bloclineid[kj];
220 if (kk > 0) piacmd->Interpret(lines[kk-1]);
221 else blocs[-kk-1]->Execute();
222 }
223 }
224else if (typ == 1) // Integer loop
225 for(int i=i1; i<i2; i+=di) {
226 k++;
227 if (++k > 9999) {
228 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
229 break;
230 }
231 sprintf(buff, " %d", i);
232 cmd = "set " + varname + buff;
233 piacmd->Interpret(cmd);
234 for(kj=0; kj<bloclineid.size(); kj++) {
235 kk = bloclineid[kj];
236 if (kk > 0) piacmd->Interpret(lines[kk-1]);
237 else blocs[-kk-1]->Execute();
238 }
239 }
240else if (typ == 2) // float loop
241 for(float f=f1; f<f2; f+=df) {
242 k++;
243 if (++k > 9999) {
244 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
245 break;
246 }
247 sprintf(buff, " %g", f);
248 cmd = "set " + varname + buff;
249 piacmd->Interpret(cmd);
250 for(kj=0; kj<bloclineid.size(); kj++) {
251 kk = bloclineid[kj];
252 if (kk > 0) piacmd->Interpret(lines[kk-1]);
253 else blocs[-kk-1]->Execute();
254 }
255 }
256
257return(parent);
258}
259
260// ------------------------------------------------------------
261// Classe PIACmd
262// ------------------------------------------------------------
263
[293]264static PIACmd* curpiacmd = NULL;
265/* --Methode-- */
[165]266PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
267{
[293]268mObjMgr = omg;
[165]269mImgApp = app;
270system("cp history.pic hisold.pic");
271hist.open("history.pic");
[349]272histon = true;
[165]273trace = false; timing = false;
274gltimer = NULL;
[349]275curblk = NULL;
276felevel = 0;
[293]277
[330]278cmdhgrp["All"] = 0;
279cmdgrpid = 1;
280cmdhgrp["Commands"] = 1;
[293]281helpwin = new PIAHelpWind(app, this);
[330]282helpwin->AddHelpGroup("All", 0);
283helpwin->AddHelpGroup("Commands", 1);
[293]284
285string kw = "piacmd";
286string usage;
287usage = ">>> (piacmd) Interpreter's keywords : \n";
[349]288usage += " > set varname string # To set a variable, $varname \n";
289usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
[333]290usage += " > setol varname patt # Fills varname with object list \n";
[293]291usage += " > unset varname # clear variable definition \n";
[333]292usage += " > echo string # output string \n";
[353]293usage += " > alias name string # define a command alias \n";
294usage += " > readstdin varname # reads a line from stdin into $varname \n";
[349]295usage += " > foreach varname string-list # Loop \n";
296usage += " > foreach varname i1:i2[:di] # Integer loop \n";
297usage += " > foreach varname f1:f2[:df] # Float loop \n";
298usage += " > end # end loops \n";
[353]299usage += " > listvars # List of variable names and values \n";
300usage += " > listalias # List of alias names and values \n";
301usage += " > listcommands # List of all known commands \n";
302usage += " > exec filename # Execute commands from file \n";
[293]303usage += " > shell comand_string # Execute shell command \n";
304usage += " > help <command_name> # <command_name> usage info \n";
305usage += " > helpwindow # Displays help window \n";
[349]306usage += " > timingon timingoff traceon traceoff \n";
[330]307string grp = "Commands";
308RegisterHelp(kw, usage, grp);
[293]309
310basexec = new PIABaseExecutor(this, omg, app);
311AddInterpreter(this);
312curcmdi = this;
[165]313}
314
315/* --Methode-- */
316PIACmd::~PIACmd()
317{
318hist.close();
319if (gltimer) { delete gltimer; gltimer = NULL; }
[293]320Modmap::iterator it;
321for(it = modmap.begin(); it != modmap.end(); it++) {
322 string name = (*it).first + "_end";
323 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
324 if (fend) fend();
325 delete (*it).second;
326 }
327delete helpwin;
328if (curpiacmd == this) curpiacmd = NULL;
[165]329}
330
331/* --Methode-- */
[293]332PIACmd* PIACmd::GetInterpreter()
[165]333{
[293]334return(curpiacmd);
335}
[165]336
[293]337/* --Methode-- */
338string PIACmd::Name()
339{
340return("piacmd");
341}
[165]342
[293]343/* --Methode-- */
[330]344void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]345{
[330]346if (!ce) {
347 RegisterHelp(keyw, usage, grp);
348 return;
349 }
350int gid = CheckHelpGrp(grp);
[293]351cmdex cme;
[330]352cme.group = gid;
[293]353cme.us = usage;
354cme.cex = ce;
355cmdexmap[keyw] = cme;
356}
357
358/* --Methode-- */
[330]359void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
360{
361int gid = CheckHelpGrp(grp);
362cmdex cme;
363cme.group = gid;
364cme.us = usage;
365cme.cex = NULL;
366helpexmap[keyw] = cme;
367}
368
369/* --Methode-- */
370int PIACmd::CheckHelpGrp(string& grp)
371{
372int gid=0;
373CmdHGroup::iterator it = cmdhgrp.find(grp);
374if (it == cmdhgrp.end()) {
375 cmdgrpid++; gid = cmdgrpid;
376 cmdhgrp[grp] = gid;
377 helpwin->AddHelpGroup(grp.c_str(), gid);
378 }
379else gid = (*it).second;
380return(gid);
381}
382
383/* --Methode-- */
384void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
385{
386helpwin->ClearHelpList();
387CmdExmap::iterator it;
388for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
389 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
390 helpwin->AddHelpItem((*it).first.c_str());
391 }
392for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
393 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
394 helpwin->AddHelpItem((*it).first.c_str());
395 }
396}
397
398/* --Methode-- */
[293]399void PIACmd::LoadModule(string& fnameso, string& name)
400{
401PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
402if (dynlink == NULL) {
403 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
404 return;
[165]405 }
[293]406string fname = name + "_init";
407DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
408if (!finit) {
409 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
410 return;
[165]411 }
[293]412cout << "PIACmd/LoadModule_Info: Initialisation module" << name
413 << " " << fname << "() ..." << endl;
414finit();
415modmap[name] = dynlink;
416return;
417}
[165]418
[293]419/* --Methode-- */
420void PIACmd::AddInterpreter(CmdInterpreter * cl)
421{
422if (!cl) return;
[349]423interpmap[cl->Name()] = cl;}
[165]424
425/* --Methode-- */
[293]426void PIACmd::SelInterpreter(string& name)
[165]427{
[293]428InterpMap::iterator it = interpmap.find(name);
429if (it == interpmap.end()) return;
430curcmdi = (*it).second;
431}
[165]432
[293]433
434// Pour le decoupage des commandes en lignes
435typedef vector<string> cmdtok;
436
437/* --Methode-- */
438int PIACmd::Interpret(string& s)
439{
[333]440int rc = 0;
[165]441cmdtok tokens;
[349]442CmdVarList::iterator it;
[165]443
[349]444// Removing leading blanks
445size_t p,q,q2,l;
446l = s.length();
447if (l < 1) return(0);
448if (s[0] == '#') return(0); // si c'est un commentaire
449p=s.find_first_not_of(" \t");
450if (p < l) s = s.substr(p);
451// else return(0);
[165]452
[349]453// On enregistre les commandes
454if (histon) hist << s << endl;
455
456
[165]457string toks,kw;
458
[349]459// >>>> Substitution d'alias (1er mot)
460int als = 0;
461while (als < 2) {
462 p = s.find_first_not_of(" ");
463 s = s.substr(p);
464 p = 0;
465 q = s.find_first_of(" ");
466 l = s.length();
467 string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
468 it = mAliases.find(w1);
469 if (it != mAliases.end()) s = (*it).second + s.substr(q);
470 else als++;
471 als++;
472 }
473
474// >>>> Separating keyword
[165]475if (q < l)
476 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
477else { kw = s.substr(p,l-p); toks = ""; }
478
[349]479// On verifie si nous sommes dans un bloc
480if ( (curblk != NULL) && (kw != "foreach") ) { // On est dans un bloc
481 if (kw != "end") { curblk->AddLine(s); return(0); }
482 else {
483 PIACmdBloc* curb = curblk;
484 curblk = curb->Parent();
485 felevel--;
486 if (curblk == NULL) {
487 mImgApp->GetConsole()->SetPrompt("Cmd> ");
488 // cout << " *DBG* Executing bloc " << endl;
489 curb->Execute();
490 }
491 else {
492 char prompt[64];
[353]493 sprintf(prompt, "foreach-%d? ", felevel);
[349]494 mImgApp->GetConsole()->SetPrompt(prompt);
495 }
496 return(0);
497 }
498 }
499
500// Nous ne sommes donc pas dans un bloc ....
501
502// >>>> Variable substitution
503string s2="";
504p = 0;
505l = s.length();
506string vn;
507while (p < l) {
508 q = s.find('$',p);
509 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
510 if (q > l) break;
511 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
512 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
513 continue;
514 }
515 if (q >= l-1) {
516 cerr << " Syntax error !!! " << endl;
517 return(0);
518 }
519 vn = "";
520 if ( s[q+1] == '{' ) { // Variable in the form ${name}
521 q2 = s.find('}',q+1);
522 if (q2 >= l) {
523 cerr << " Syntax error !!! " << endl;
524 return(0);
525 }
526 vn = s.substr(q+2,q2-q-2);
527 q2++;
528 }
529 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
530 q2 = s.find(']',q+1);
531 if (q2 >= l) {
532 cerr << " Syntax error !!! " << endl;
533 return(0);
534 }
535 vn = s.substr(q+2,q2-q-2);
536 it = mVars.find(vn);
537 if ( (vn.length() < 1) || (it == mVars.end()) ) {
538 cerr << " Error: Undefined variable " << vn << " ! " << endl;
539 return(0);
540 }
541 vn = mVars[vn];
542 q2++;
543 }
544 else {
545 q2 = s.find_first_of(" .:/,]()$",q+1);
546 if (q2 > l) q2 = l;
547 vn = s.substr(q+1, q2-q-1);
548 }
549 it = mVars.find(vn);
550 if ( (vn.length() < 1) || (it == mVars.end()) ) {
551 cerr << " Error: Undefined variable " << vn << " ! " << endl;
552 return(0);
553 }
554 s2 += (s.substr(p, q-p) + (*it).second);
555 p = q2;
556 }
557if (p < l) s2 += s.substr(p);
558
559p = s2.find_first_not_of(" \t");
560if (p < l) s2 = s2.substr(p);
561
562
563// >>>> Separating keyword and tokens
564q = s2.find(' ');
565l = s2.length();
566if (q < l)
567 { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
568else { kw = s2; toks = ""; }
569
[165]570q = 0;
571while (q < l) {
[349]572 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
[165]573 if (p>=l) break;
[349]574 q = toks.find_first_of(" \t",p); // la fin du token;
[165]575 string token = toks.substr(p,q-p);
576 tokens.push_back(token);
577 }
578
[349]579
580// Si c'est un foreach, on cree un nouveau bloc
581if (kw == "foreach") {
582 // cout << " *DBG* We got a foreach... " << endl;
583 PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
584 if (!bloc->CheckOK()) {
585 cerr << "foreach syntax Error ! " << endl;
586 delete bloc;
587 return(0);
588 }
589 felevel++;
590 if (curblk) curblk->AddBloc(bloc);
591 else {
592 char prompt[64];
593 sprintf(prompt, "foreach-%d> ", felevel);
594 mImgApp->GetConsole()->SetPrompt(prompt);
595 }
596 curblk = bloc;
597 // cout << " *DBG* New Bloc created ... " << endl;
598 return(0);
[293]599 }
[165]600
[349]601
[165]602// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
603// for(int ii=0; ii<tokens.size(); ii++)
604// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
605
606// >>>>>>>>>>> Commande d'interpreteur
[349]607else if (kw == "helpwindow") ShowHelpWindow();
[293]608else if (kw == "help") {
[330]609 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]610 else {
[330]611 string kwh = "piacmd";
612 cout << GetUsage(kwh) << endl;
[293]613 }
[165]614 }
615
616else if (kw == "set") {
[293]617 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[349]618 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
619 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
620 return(0);
621 }
622 string xx = tokens[1];
623 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
[333]624 mVars[tokens[0]] = xx;
[165]625 }
[349]626
627else if (kw == "getvar") {
628 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
629 it = mVars.find(tokens[1]);
630 if (it == mVars.end()) {
631 cerr << "Error - No " << tokens[1] << " Variable " << endl;
632 return(0);
633 }
634 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
635 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
636 return(0);
637 }
638 mVars[tokens[0]] = (*it).second;
639 }
640
641else if (kw == "alias") {
642 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
643 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
644 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
645 return(0);
646 }
647 string xx = tokens[1];
648 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
649 mAliases[tokens[0]] = xx;
650 }
651
[333]652else if (kw == "setol") {
653 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
[349]654 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
655 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
656 return(0);
657 }
[333]658 vector<string> ol;
[349]659 mObjMgr->GetObjList(tokens[1], ol);
660 string vol;
661 if (ol.size() < 1) vol = "";
662 else {
663 vol = ol[0];
664 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
665 }
[333]666 mVars[tokens[0]] = vol;
667 }
[293]668else if (kw == "unset") {
669 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[165]670 CmdVarList::iterator it = mVars.find(tokens[0]);
671 if (it != mVars.end()) mVars.erase(it);
[293]672 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]673 }
[333]674else if (kw == "echo") {
675 for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
676 cout << endl;
677 }
[353]678
679else if (kw == "readstdin") {
680 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
681 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
682 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
683 return(0);
684 }
685 char buff[128];
686 fgets(buff, 128, stdin);
687 buff[127] = '\0';
688 mVars[tokens[0]] = buff;
689 }
690
[165]691else if (kw == "listvars") {
[293]692 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[165]693 CmdVarList::iterator it;
694 for(it = mVars.begin(); it != mVars.end(); it++)
695 cout << (*it).first << " = " << (*it).second << "\n";
696 cout << endl;
697 }
[353]698else if (kw == "listalias") {
699 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
700 CmdVarList::iterator it;
701 for(it = mAliases.begin(); it != mAliases.end(); it++)
702 cout << (*it).first << " = " << (*it).second << "\n";
703 cout << endl;
704 }
705else if (kw == "listcommands") {
[330]706 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
[293]707 CmdExmap::iterator it;
708 int kc = 0;
709 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
710 cout << (*it).first << " ";
711 kc++;
712 if (kc >= 5) { cout << "\n"; kc = 0; }
713 }
714 cout << endl;
715 }
716else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
717else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
[165]718else if (kw == "timingon") {
[293]719 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
720 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
721 }
[165]722else if (kw == "timingoff") {
[293]723 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
724 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
725 }
[165]726else if (kw == "exec") {
[293]727 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
[349]728 ExecFile(tokens[0], tokens);
[165]729 }
730else if (kw == "shell") {
[293]731 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[165]732 system(toks.c_str());
733 }
[293]734// Execution d'une commande enregistree
[333]735else rc = ExecuteCommand(kw, tokens);
[165]736
737if (timing) gltimer->Split();
[333]738return(rc);
[165]739}
740
[333]741/* --Methode-- */
742int PIACmd::ExecuteCommandLine(string& line)
743{
744cmdtok tokens;
745if (line.length() < 1) return(0);
[165]746
[333]747string toks,kw;
748size_t p = line.find_first_not_of(" ");
749line = line.substr(p);
750p = 0;
751size_t q = line.find_first_of(" ");
752size_t l = line.length();
753
754if (q < l)
755 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
756else { kw = line.substr(p,l-p); toks = ""; }
757
758q = 0;
759while (q < l) {
760 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
761 if (p>=l) break;
762 q = toks.find_first_of(" ",p); // la fin du token;
763 string token = toks.substr(p,q-p);
764 tokens.push_back(token);
765 }
766
767return(ExecuteCommand(kw, tokens));
768}
769
[165]770/* --Methode-- */
[333]771int PIACmd::ExecuteCommand(string& keyw, vector<string>& args)
772{
773 int rc = -1;
774 CmdExmap::iterator it = cmdexmap.find(keyw);
775 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
776 else {
777 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args);
778 else cout << "Dont know how to execute " << keyw << " ? " << endl;
779 }
780 return(rc);
781}
782
783/* --Methode-- */
[349]784int PIACmd::ExecFile(string& file, vector<string>& args)
[165]785{
[293]786char line_buff[512];
787FILE *fip;
[165]788
[293]789if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
790 cerr << "PIACmd::Exec() Error opening file " << file << endl;
791 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
792 return(0);
[165]793 }
[349]794
795// hist << "### Executing commands from " << file << endl;
796
797// Setting $0 ... $99 variables
798int k;
799CmdVarList::iterator it;
800char buff[32];
801// First, we clear all previous values
802string vn="#";
803it = mVars.find(vn);
804if (it != mVars.end()) mVars.erase(it);
805for(k=0; k<99; k++) {
806 sprintf(buff,"%d",k);
807 vn = buff;
808 it = mVars.find(vn);
809 if (it != mVars.end()) mVars.erase(it);
810 }
811// We then set them
812vn="#";
813sprintf(buff,"%d",(int)args.size());
814mVars[vn] = buff;
815for(k=0; k<args.size(); k++) {
816 sprintf(buff,"%d",k);
817 vn = buff;
818 mVars[vn] = args[k];
819 }
820
[293]821if (trace) {
822 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
823 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
824 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
825 }
[165]826
[349]827histon = false;
[293]828while (fgets(line_buff,511,fip) != NULL)
829 {
830 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
831 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
832 string line(line_buff);
833 Interpret(line);
[165]834 }
[349]835histon = false;
836
837// hist << "### End of Exec( " << file << " ) " << endl;
[293]838if (trace) {
839 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
840 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
841 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
[165]842 }
843
844return(0);
845}
846
[293]847
848static string* videstr = NULL;
849/* --Methode-- */
850string& PIACmd::GetUsage(const string& kw)
[165]851{
[330]852bool fndok = false;
[293]853CmdExmap::iterator it = cmdexmap.find(kw);
[330]854if (it == cmdexmap.end()) {
855 it = helpexmap.find(kw);
856 if (it != helpexmap.end()) fndok = true;
[165]857 }
[330]858 else fndok = true;
859if (fndok) return( (*it).second.us );
860// Keyword pas trouve
861if (videstr == NULL) videstr = new string("");
862*videstr = "Nothing known about " + kw + " ?? ";
863return(*videstr);
864
[165]865}
866
[293]867/* --Methode-- */
868void PIACmd::ShowHelpWindow()
[165]869{
[293]870helpwin->Show();
[165]871}
Note: See TracBrowser for help on using the repository browser.