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

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

A/ ajout des blocs foreach et ameliorations gestion des variables ($x)
pour l'interpreteur piacmd.
B/ Ajout PIStdImgApp::AddText et corrections diverses

Reza 05/08/99

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