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

Last change on this file since 1565 was 1565, checked in by ansari, 24 years ago

suite implementation test if ... then ... else ... endif et debug for/foreach/if ds PIACmd - Reza 4/7/2001

File size: 34.1 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#include "piafitting.h"
16#include "pawexecut.h"
17#include "cxxexecutor.h"
18#include "cxxexecwin.h"
19
20#include PISTDWDG_H
21#include PILIST_H
22
23// ------------------------------------------------------------
24// Gestion d'une fenetre d'aide interactive
25// Classe PIAHelpWind
26// ------------------------------------------------------------
27
28class PIAHelpWind : public PIWindow {
29public :
30 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
31 virtual ~PIAHelpWind();
32 virtual void Show();
33 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
34 inline void AddHelpGroup(const char * hgrp, int gid)
35 { hgrpom->AppendItem(hgrp, 20000+gid); }
36 inline void ClearHelpList()
37 { mNitem=0; hitemlist->DeleteAllItems(); }
38 inline void AddHelpItem(const char * hitem)
39 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
40protected :
41 PIStdImgApp* dap;
42 PIACmd* piac;
43 int mNitem;
44 PIList* hitemlist;
45 PIOptMenu* hgrpom;
46 PIButton * mBut;
47 PILabel * mLab;
48 PIText* mTxt;
49};
50
51/* --Methode-- */
52PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
53 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
54{
55dap = par;
56piac = piacmd;
57mNitem = 0;
58SetMsg(77);
59
60int bsx, bsy;
61int tsx, tsy;
62int spx, spy;
63PIApplicationPrefCompSize(bsx, bsy);
64spx = bsx/6; spy = bsy/6;
65tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
66SetSize(tsx,tsy);
67hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
68hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
69hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
70hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
71// hitemlist->SetBorderWidth(2);
72mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
73// mTxt->SetMutiLineMode(true);
74mTxt->SetTextEditable(false);
75mTxt->SetText("");
76mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
77mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
78mLab->SetBorderWidth(1);
79mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
80mLab->SetLabel("");
81mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
82mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
83}
84
85/* --Methode-- */
86PIAHelpWind::~PIAHelpWind()
87{
88delete hgrpom;
89delete hitemlist;
90delete mTxt;
91delete mLab;
92delete mBut;
93}
94
95/* --Methode-- */
96void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
97{
98PIMessage um = UserMsg(msg);
99if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
100 Hide();
101 return;
102 }
103else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
104 mTxt->SetText("");
105 mLab->SetLabel("");
106 piac->UpdateHelpList(this, um-20000);
107}
108else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
109 string s = hitemlist->GetSelectionStr();
110 mTxt->SetText(piac->GetUsage(s));
111 mLab->SetLabel(s);
112 }
113}
114
115/* --Methode-- */
116void PIAHelpWind::Show()
117{
118hgrpom->SetValue(20000); // Groupe All
119mTxt->SetText("");
120mLab->SetLabel("");
121piac->UpdateHelpList(this, 0);
122PIWindow::Show();
123}
124
125// ------------------------------------------------------------
126// Bloc de commandes (Foreach, ...)
127// Classe PIACmdBloc
128// ------------------------------------------------------------
129
130class PIACmdBloc {
131public:
132 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
133
134 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
135 ~PIACmdBloc();
136 inline PIACmdBloc* Parent() { return(parent); }
137 inline bool CheckOK() { return blkok; }
138 inline void AddLine(string& line)
139 { lines.push_back(line); bloclineid.push_back(lines.size()); }
140 inline void AddBloc(PIACmdBloc* blk)
141 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
142 PIACmdBloc* Execute();
143protected:
144 PIACmd* piacmd;
145 PIACmdBloc* parent;
146 bool blkok; // true -> block OK
147 BType typ; // foreach , integer loop, float loop, test
148 string varname;
149 vector<string> strlist;
150 vector<string> lines;
151 vector<PIACmdBloc *> blocs;
152 vector<int> bloclineid;
153 int i1,i2,di;
154 float f1,f2,df;
155};
156
157/* --Methode-- */
158PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
159{
160piacmd = piac;
161parent = par;
162blkok = false;
163typ = BT_None;
164i1 = 0; i2 = -1; di = 1;
165f1 = 0.; f2 = -1.; df = 1.;
166if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
167if ((kw != "foreach") && (kw != "for")) return;
168varname = args[0]; // $CHECK$ Variable name should be checked
169//if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
170if (kw == "foreach" ) { // This is a foreach bloc with string list
171 for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
172 typ = BT_ForeachList;
173 blkok = true;
174 }
175else { // This is an integer or float loop
176 size_t l = args[1].length();
177 size_t p = args[1].find(':');
178 size_t pp = args[1].find('.');
179 bool fl = (pp < l) ? true : false; // Float loop or integer loop
180 if (p >= l) return; // Syntaxe error
181 string a1 = args[1].substr(0, p);
182 string aa = args[1].substr(p+1);
183 p = aa.find(':');
184 string a2, a3;
185 bool hasa3 = false;
186 if (p < aa.length() ) {
187 a2 = aa.substr(0,p);
188 a3 = aa.substr(p+1);
189 hasa3 = true;
190 }
191 else a2 = aa;
192 if (fl) {
193 typ = BT_ForeachFloat;
194 blkok = true;
195 f1 = atof(a1.c_str());
196 f2 = atof(a2.c_str());
197 if (hasa3) df = atof(a3.c_str());
198 else df = 1.;
199 }
200 else {
201 typ = BT_ForeachInt;
202 blkok = true;
203 i1 = atoi(a1.c_str());
204 i2 = atoi(a2.c_str());
205 if (hasa3) di = atoi(a3.c_str());
206 else di = 1;
207 }
208 }
209}
210
211/* --Methode-- */
212PIACmdBloc::~PIACmdBloc()
213{
214for(int k=0; k<blocs.size(); k++) delete blocs[k];
215}
216
217/* --Methode-- */
218PIACmdBloc* PIACmdBloc::Execute()
219{
220// cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
221// " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
222string cmd;
223int k=0;
224int kj=0;
225int kk=0;
226char buff[32];
227if (typ == BT_ForeachList) // foreach string loop
228 for(k=0; k<strlist.size(); k++) {
229 cmd = "set " + varname + " '" + strlist[k] + "'";
230 piacmd->Interpret(cmd);
231 for(kj=0; kj<bloclineid.size(); kj++) {
232 kk = bloclineid[kj];
233 if (kk > 0) piacmd->Interpret(lines[kk-1]);
234 else blocs[-kk-1]->Execute();
235 }
236 }
237else if (typ == BT_ForeachInt) // Integer loop
238 for(int i=i1; i<i2; i+=di) {
239 k++;
240 if (++k > 9999) {
241 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
242 break;
243 }
244 sprintf(buff, " %d", i);
245 cmd = "set " + varname + buff;
246 piacmd->Interpret(cmd);
247 for(kj=0; kj<bloclineid.size(); kj++) {
248 kk = bloclineid[kj];
249 if (kk > 0) piacmd->Interpret(lines[kk-1]);
250 else blocs[-kk-1]->Execute();
251 }
252 }
253else if (typ == BT_ForeachFloat) // float loop
254 for(float f=f1; f<f2; f+=df) {
255 k++;
256 if (++k > 9999) {
257 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
258 break;
259 }
260 sprintf(buff, " %g", f);
261 cmd = "set " + varname + buff;
262 piacmd->Interpret(cmd);
263 for(kj=0; kj<bloclineid.size(); kj++) {
264 kk = bloclineid[kj];
265 if (kk > 0) piacmd->Interpret(lines[kk-1]);
266 else blocs[-kk-1]->Execute();
267 }
268 }
269
270return(parent);
271}
272
273// ------------------------------------------------------------
274// Classe PIACmd
275// ------------------------------------------------------------
276
277static PIACmd* curpiacmd = NULL;
278/* --Methode-- */
279PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
280{
281mObjMgr = omg;
282mImgApp = app;
283system("cp history.pic hisold.pic");
284hist.open("history.pic");
285histon = true;
286trace = false; timing = false;
287gltimer = NULL;
288curblk = NULL;
289felevel = 0;
290mulinecmd = "";
291mulinefg = false;
292
293cmdhgrp["All"] = 0;
294cmdgrpid = 1;
295cmdhgrp["Commands"] = 1;
296helpwin = new PIAHelpWind(app, this);
297helpwin->AddHelpGroup("All", 0);
298helpwin->AddHelpGroup("Commands", 1);
299
300string kw = "piacmd";
301string usage;
302usage = ">>> (piacmd) Interpreter's keywords : \n";
303usage += " > set varname string # To set a variable, $varname \n";
304usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
305usage += " > setol varname patt # Fills varname with object list \n";
306usage += " > unset varname # clear variable definition \n";
307usage += " > echo string # output string \n";
308usage += " > alias name string # define a command alias \n";
309usage += " > readstdin varname # reads a line from stdin into $varname \n";
310usage += " > foreach varname string-list # Loop \n";
311usage += " > for varname i1:i2[:di] # Integer loop \n";
312usage += " > for varname f1:f2[:df] # Float loop \n";
313usage += " > end # end loops \n";
314usage += " > if test then # Conditional test : a == != < > <= >= b \n";
315usage += " > else # Conditional \n";
316usage += " > endif # End of conditional if bloc \n";
317usage += " > break # Delete (clears) all test and loop blocs \n";
318usage += " > return # Stops command execution from a file \n";
319usage += " > listvars # List of variable names and values \n";
320usage += " > listalias # List of alias names and values \n";
321usage += " > listcommands # List of all known commands \n";
322usage += " > exec filename # Execute commands from file \n";
323usage += " > shell comand_string # Execute shell command \n";
324usage += " > help <command_name> # <command_name> usage info \n";
325usage += " > helpwindow # Displays help window \n";
326usage += " > timingon timingoff traceon traceoff \n";
327string grp = "Commands";
328RegisterHelp(kw, usage, grp);
329
330basexec = new PIABaseExecutor(this, omg, app);
331fitexec = new PIAFitter(this, app);
332pawexec = new PAWExecutor(this, app);
333CxxExecutor * cxxe = new CxxExecutor(this, app);
334cxxexec = cxxe;
335cxxoptwin = new CxxOptionWind(app, cxxe);
336cxxexwin = new CxxExecWind(app, cxxe);
337
338AddInterpreter(this);
339curcmdi = this;
340}
341
342/* --Methode-- */
343PIACmd::~PIACmd()
344{
345hist.close();
346if (gltimer) { delete gltimer; gltimer = NULL; }
347Modmap::iterator it;
348for(it = modmap.begin(); it != modmap.end(); it++) {
349 string name = (*it).first + "_end";
350 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
351 if (fend) fend();
352 delete (*it).second;
353 }
354delete helpwin;
355delete cxxexwin;
356delete cxxoptwin;
357if (curpiacmd == this) curpiacmd = NULL;
358delete basexec;
359delete fitexec;
360delete pawexec;
361delete cxxexec;
362}
363
364/* --Methode-- */
365PIACmd* PIACmd::GetInterpreter()
366{
367return(curpiacmd);
368}
369
370/* --Methode-- */
371string PIACmd::Name()
372{
373return("piacmd");
374}
375
376/* --Methode-- */
377void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
378{
379if (!ce) {
380 RegisterHelp(keyw, usage, grp);
381 return;
382 }
383int gid = CheckHelpGrp(grp);
384cmdex cme;
385cme.group = gid;
386cme.us = usage;
387cme.cex = ce;
388cmdexmap[keyw] = cme;
389}
390
391/* --Methode-- */
392void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
393{
394int gid = CheckHelpGrp(grp);
395cmdex cme;
396cme.group = gid;
397cme.us = usage;
398cme.cex = NULL;
399helpexmap[keyw] = cme;
400}
401
402/* --Methode-- */
403int PIACmd::CheckHelpGrp(string& grp)
404{
405int gid=0;
406CmdHGroup::iterator it = cmdhgrp.find(grp);
407if (it == cmdhgrp.end()) {
408 cmdgrpid++; gid = cmdgrpid;
409 cmdhgrp[grp] = gid;
410 helpwin->AddHelpGroup(grp.c_str(), gid);
411 }
412else gid = (*it).second;
413return(gid);
414}
415
416/* --Methode-- */
417void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
418{
419helpwin->ClearHelpList();
420CmdExmap::iterator it;
421for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
422 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
423 helpwin->AddHelpItem((*it).first.c_str());
424 }
425for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
426 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
427 helpwin->AddHelpItem((*it).first.c_str());
428 }
429}
430
431/* --Methode-- */
432void PIACmd::LoadModule(string& fnameso, string& name)
433{
434PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
435if (dynlink == NULL) {
436 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
437 return;
438 }
439string fname = name + "_init";
440DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
441if (!finit) {
442 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
443 return;
444 }
445cout << "PIACmd/LoadModule_Info: Initialisation module" << name
446 << " " << fname << "() ..." << endl;
447finit();
448modmap[name] = dynlink;
449return;
450}
451
452/* --Methode-- */
453void PIACmd::AddInterpreter(CmdInterpreter * cl)
454{
455if (!cl) return;
456interpmap[cl->Name()] = cl;}
457
458/* --Methode-- */
459void PIACmd::SelInterpreter(string& name)
460{
461InterpMap::iterator it = interpmap.find(name);
462if (it == interpmap.end()) return;
463curcmdi = (*it).second;
464}
465
466
467
468/* Fonction */
469static string GetStringFrStdin(PIACmd* piac)
470{
471PIStdImgApp* piapp = piac->GetImgApp();
472if (piapp) {
473 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
474 if (wdg) wdg->Refresh();
475#ifndef __mac__
476 /* On vide le buffer X-Window */
477 XSync(PIXDisplay(),False);
478#endif
479}
480char buff[128];
481fgets(buff, 128, stdin);
482buff[127] = '\0';
483return((string)buff);
484}
485
486/* --Methode-- */
487int PIACmd::Interpret(string& s)
488{
489int rc = 0;
490NamedObjMgr omg;
491
492// On saute de commandes vides
493size_t l;
494l = s.length();
495if (l < 1) return(0);
496
497// On enregistre les commandes
498if (histon) hist << s << endl;
499
500if (s[0] == '#') return(0); // si c'est un commentaire
501
502// Logique de gestion des lignes suite
503// un \ en derniere position indique la presence d'une ligne suite
504size_t lnb = s.find_last_not_of(' ');
505if (s[lnb] == '\\' ) { // Lignes suite ...
506 mulinefg = true;
507 mulinecmd += s.substr(0,lnb);
508 mImgApp->GetConsole()->SetPrompt("...? ");
509 return(0);
510}
511
512if (mulinefg) { // Il y avait des lignes suite
513 s = mulinecmd + s;
514 mulinecmd = "";
515 mulinefg = false;
516 const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
517 mImgApp->GetConsole()->SetPrompt(rprompt);
518}
519
520// Removing leading blanks
521size_t p,q;
522
523p=s.find_first_not_of(" \t");
524if (p < l) s = s.substr(p);
525
526// >>>> Substitution d'alias (1er mot)
527CmdVarList::iterator it;
528p = 0;
529q = s.find_first_of(" \t");
530l = s.length();
531string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
532it = mAliases.find(w1);
533if (it != mAliases.end()) {
534 s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
535 l = s.length();
536 p=s.find_first_not_of(" \t");
537 if (p < l) s = s.substr(p);
538 p = 0;
539 q = s.find_first_of(" ");
540 }
541
542// >>>> Separating keyword
543string toks,kw;
544if (q < l)
545 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
546else { kw = s.substr(p,l-p); toks = ""; }
547
548// les mot-cle end else endif doivent etre le seul mot de la ligne
549if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
550 size_t ltk = toks.length();
551 if (toks.find_first_not_of(" \t") < ltk) {
552 cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
553 << "line: " << s << endl;
554 return(1);
555 }
556}
557
558if (kw == "break") {
559 PIACmdBloc* curb = curblk;
560 while (curb) {
561 PIACmdBloc* dblk = curb;
562 curb = curb->Parent();
563 delete dblk;
564 }
565 testresult.clear();
566 return(0);
567}
568else if (kw == "return") return(77777);
569
570// On verifie si nous sommes dans un bloc (for , foreach)
571if (curblk != NULL) { // On est dans un bloc
572 if ( (kw == "for") || (kw == "foreach")) felevel++;
573 else if (kw == "end") felevel--;
574 if (felevel == 0) { // Il faut executer le bloc
575 PIACmdBloc* curb = curblk;
576 curblk = curb->Parent();
577 mImgApp->GetConsole()->SetPrompt("Cmd> ");
578 // cout << " *DBG* Executing bloc " << endl;
579 bool ohv = histon;
580 histon = false;
581 curb->Execute();
582 histon = ohv;
583 }
584 else curblk->AddLine(s);
585 return(0);
586}
587else if (kw == "end") {
588 cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
589 << "line: " << s << endl;
590 return(1);
591}
592
593// Sommes-nous dans un bloc de test if then else
594if (testresult.size() > 0) { // Nous sommes ds un bloc if
595 if (kw == "else") {
596 if ((*tresit) & 2) {
597 cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
598 << "line: " << s << endl;
599 return(1);
600 }
601 else {
602 char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
603 mImgApp->GetConsole()->SetPrompt(npr);
604 (*tresit) |= 2;
605 return(0);
606 }
607 }
608 else if (kw == "endif") {
609 list<char>::iterator dbit = tresit;
610 tresit--;
611 testresult.erase(dbit);
612 char * npr = "Cmd> ";
613 if (testresult.size() > 1) {
614 if (!((*tresit)&2))
615 npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
616 else
617 npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
618 }
619 mImgApp->GetConsole()->SetPrompt(npr);
620 return(0);
621 }
622}
623else if ((kw == "else") || (kw == "endif")) {
624 cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
625 << "line: " << s << endl;
626 return(1);
627}
628
629bool fgcont = true;
630if (testresult.size() > 0) { // Resultat de if ou else
631 list<char>::iterator it;
632 for(it=testresult.begin(); it!=testresult.end(); it++) {
633 // Si on n'est pas ds le else et le if est faux
634 if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
635 // Si on est ds else et le if etait vrai !
636 if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
637 if (!fgcont) break;
638 }
639}
640
641if ((!fgcont) && (kw != "if")) return(0);
642
643
644// Nous ne sommes donc pas dans un bloc .... Substitution de variables
645string s2;
646int rcs ;
647// Execution de code C++
648
649if (s[0] == '@') {
650 CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
651 if (cxxe == NULL) {
652 cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
653 return(99);
654 }
655 // Sans substitution des variables $
656 if (s[1] == '@') return(cxxe->ExecuteCXX(s.substr(2)));
657 else { // AVEC substitution des variables $
658 rcs = SubstituteVars(s, s2);
659 if (rcs) return(rcs);
660 return(cxxe->ExecuteCXX(s2.substr(1)));
661 }
662}
663
664
665rcs = SubstituteVars(s, s2);
666if (rcs) return(rcs);
667
668// >>>> Separating keyword and tokens
669vector<string> tokens;
670
671q = s2.find(' ');
672l = s2.length();
673if (q < l)
674 { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
675else { kw = s2; toks = ""; }
676
677q = 0;
678while (q < l) {
679 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
680 if (p>=l) break;
681 if ( (toks[p] == '\'') || (toks[p] == '"') ) {
682 q = toks.find(toks[p],p+1);
683 if (q>=l) {
684 cerr << "PIACmd::Interpret()/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
685 return(2);
686 }
687 p++;
688 }
689 else {
690 q = toks.find_first_of(" \t",p); // la fin du token;
691 }
692 string token = toks.substr(p,q-p);
693 tokens.push_back(token);
694 }
695
696
697// Si c'est un for/foreach, on cree un nouveau bloc
698if ((kw == "foreach") || (kw == "for")) {
699 // cout << " *DBG* We got a foreach... " << endl;
700 PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
701 if (!bloc->CheckOK()) {
702 cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
703 delete bloc;
704 return(1);
705 }
706 felevel++;
707 if (curblk) curblk->AddBloc(bloc);
708 else mImgApp->GetConsole()->SetPrompt("for...> ");
709 curblk = bloc;
710 // cout << " *DBG* New Bloc created ... " << endl;
711 return(0);
712 }
713else if (kw == "if") { // Un test if
714 bool restst = true;
715 int rct = EvaluateTest(tokens, s, restst);
716 if (rct) {
717 cerr << "PIACmd::Interpret() if syntax Error ! " << endl;
718 return(1);
719 }
720 char res_tst = (restst) ? 1 : 0;
721 testresult.push_back(res_tst);
722 if (testresult.size() == 1) tresit = testresult.begin();
723 else tresit++;
724 char * npr = (restst) ? "if-T> " : "if-F> ";
725 mImgApp->GetConsole()->SetPrompt(npr);
726}
727// Execution de commandes
728else rc = ExecuteCommandLine(kw, tokens, toks);
729return(rc);
730
731// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
732// for(int ii=0; ii<tokens.size(); ii++)
733// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
734
735}
736
737/* --Methode-- */
738int PIACmd::SubstituteVars(string & s, string & s2)
739// Variable substitution
740{
741NamedObjMgr omg;
742
743size_t p,q,q2,l;
744
745s2="";
746p = 0;
747l = s.length();
748string vn;
749while (p < l) {
750 q = s.find('$',p);
751 if (q > l) break;
752 q2 = s.find('\'',p);
753 if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
754 q2 = s.find('\'',q2+1);
755 if (q2 >= l) {
756 cerr << " Syntax error - Unbalenced quotes !!! " << endl;
757 return(1);
758 }
759 s2 += s.substr(p, q2-p+1);
760 p = q2+1; continue;
761 }
762 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
763 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
764 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
765 continue;
766 }
767 if (q >= l-1) {
768 cerr << " Syntax error - line ending with $ !!! " << endl;
769 return(2);
770 }
771 vn = "";
772 if ( s[q+1] == '{' ) { // Variable in the form ${name}
773 q2 = s.find('}',q+1);
774 if (q2 >= l) {
775 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
776 return(3);
777 }
778 vn = s.substr(q+2,q2-q-2);
779 q2++;
780 }
781 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
782 q2 = s.find(']',q+1);
783 if (q2 >= l) {
784 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
785 return(4);
786 }
787 vn = s.substr(q+2,q2-q-2);
788 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
789 cerr << " Error: Undefined variable " << vn << " ! " << endl;
790 return(5);
791 }
792 vn = omg.GetVar(vn);
793 q2++;
794 }
795 else {
796 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
797 if (q2 > l) q2 = l;
798 vn = s.substr(q+1, q2-q-1);
799 }
800 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
801 cerr << " Error: Undefined variable " << vn << " ! " << endl;
802 return(5);
803 }
804 s2 += (s.substr(p, q-p) + omg.GetVar(vn));
805 p = q2;
806 }
807if (p < l) s2 += s.substr(p);
808
809p = s2.find_first_not_of(" \t");
810if (p < l) s2 = s2.substr(p);
811
812return(0);
813}
814
815/* --Methode-- */
816int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
817{
818 res = true;
819 if ((args.size() < 4) || (args[3] != "then")) return(1);
820 if (args[1] == "==") res = (args[0] == args[2]);
821 else if (args[1] == "!=") res = (args[0] != args[2]);
822 else if (args[1] == "<")
823 res = (atof(args[0].c_str()) < atof(args[2].c_str()));
824 else if (args[1] == ">")
825 res = (atof(args[0].c_str()) > atof(args[2].c_str()));
826 else if (args[1] == "<=")
827 res = (atof(args[0].c_str()) <= atof(args[2].c_str()));
828 else if (args[1] == ">=")
829 res = (atof(args[0].c_str()) >= atof(args[2].c_str()));
830 else return(2);
831 return(0);
832}
833
834/* --Methode-- */
835int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
836{
837int rc = 0;
838NamedObjMgr omg;
839
840// >>>>>>>>>>> Commande d'interpreteur
841if (kw == "helpwindow") ShowHelpWindow();
842else if (kw == "help") {
843 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
844 else {
845 string kwh = "piacmd";
846 cout << GetUsage(kwh) << endl;
847 }
848 }
849
850else if (kw == "set") {
851 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
852 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
853 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
854 return(0);
855 }
856 // string xx = tokens[1];
857 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
858 omg.SetVar(tokens[0], tokens[1]);
859 }
860
861else if (kw == "getvar") {
862 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
863 if (!omg.HasVar(tokens[1])) {
864 cerr << "Error - No " << tokens[1] << " Variable " << endl;
865 return(0);
866 }
867 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
868 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
869 return(0);
870 }
871 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
872 }
873
874else if (kw == "alias") {
875 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
876 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
877 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
878 return(0);
879 }
880 string xx = tokens[1];
881 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
882 mAliases[tokens[0]] = xx;
883 }
884
885else if (kw == "setol") {
886 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
887 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
888 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
889 return(0);
890 }
891 vector<string> ol;
892 mObjMgr->GetObjList(tokens[1], ol);
893 string vol;
894 if (ol.size() < 1) vol = "";
895 else {
896 vol = ol[0];
897 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
898 }
899 omg.SetVar(tokens[0], vol);
900 }
901else if (kw == "unset") {
902 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
903 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
904 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
905 }
906else if (kw == "echo") {
907 for (int ii=0; ii<tokens.size(); ii++)
908 cout << tokens[ii] << " " ;
909 cout << endl;
910 }
911else if (kw == "readstdin") {
912 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
913 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
914 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
915 return(0);
916 }
917 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
918 cout << tokens[0] << " ? " << endl;
919 omg.SetVar(tokens[0], GetStringFrStdin(this) );
920 }
921
922else if (kw == "listvars") {
923 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
924 DVList& varlist = omg.GetVarList();
925 DVList::ValList::const_iterator it;
926 string value;
927 for(it = varlist.Begin(); it != varlist.End(); it++) {
928#ifdef SANS_EVOLPLANCK
929 MuTyV mtv = (*it).second;
930 value = (string)(mtv);
931#else
932 value = (string)((*it).second.elval);
933#endif
934 cout << (*it).first << " = " << value << "\n";
935 }
936 cout << endl;
937 }
938else if (kw == "listalias") {
939 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
940 CmdVarList::iterator it;
941 for(it = mAliases.begin(); it != mAliases.end(); it++)
942 cout << (*it).first << " = " << (*it).second << "\n";
943 cout << endl;
944 }
945else if (kw == "listcommands") {
946 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
947 CmdExmap::iterator it;
948 int kc = 0;
949 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
950 cout << (*it).first << " ";
951 kc++;
952 if (kc >= 5) { cout << "\n"; kc = 0; }
953 }
954 cout << endl;
955 }
956else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
957else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
958else if (kw == "timingon") {
959 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
960 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
961 }
962else if (kw == "timingoff") {
963 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
964 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
965 }
966else if (kw == "exec") {
967 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
968 ExecFile(tokens[0], tokens);
969 }
970else if (kw == "shell") {
971 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
972 string cmd;
973 for (int ii=0; ii<tokens.size(); ii++)
974 cmd += (tokens[ii] + ' ');
975 system(cmd.c_str());
976 }
977
978// Execution d'une commande enregistree
979else rc = ExecuteCommand(kw, tokens, toks);
980
981if (timing) gltimer->Split();
982return(rc);
983}
984
985/* --Methode-- */
986int PIACmd::ParseLineExecute(string& line)
987{
988vector<string> tokens;
989
990if (line.length() < 1) return(0);
991
992string toks,kw;
993size_t p = line.find_first_not_of(" ");
994line = line.substr(p);
995p = 0;
996size_t q = line.find_first_of(" ");
997size_t l = line.length();
998
999if (q < l)
1000 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
1001else { kw = line.substr(p,l-p); toks = ""; }
1002
1003q = 0;
1004while (q < l) {
1005 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
1006 if (p>=l) break;
1007 q = toks.find_first_of(" ",p); // la fin du token;
1008 string token = toks.substr(p,q-p);
1009 tokens.push_back(token);
1010 }
1011
1012return(ExecuteCommand(kw, tokens, toks));
1013}
1014
1015/* --Methode-- */
1016int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
1017{
1018 int rc = -1;
1019 CmdExmap::iterator it = cmdexmap.find(keyw);
1020 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
1021 else {
1022 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
1023 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1024 }
1025 return(rc);
1026}
1027
1028/* --Methode-- */
1029int PIACmd::ExecFile(string& file, vector<string>& args)
1030{
1031char line_buff[512];
1032FILE *fip;
1033
1034if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
1035 if (file.find('.') >= file.length()) {
1036 cout << "PIACmd::Exec(): Error opening file " << file << endl;
1037 file += ".pic";
1038 cout << " Trying file " << file << endl;
1039 fip = fopen(file.c_str(),"r");
1040 }
1041 }
1042
1043if(fip == NULL) {
1044 cerr << "PIACmd::Exec() Error opening file " << file << endl;
1045 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
1046 return(0);
1047 }
1048
1049// hist << "### Executing commands from " << file << endl;
1050
1051// Setting $0 ... $99 variables
1052int k;
1053char buff[32];
1054// First, we clear all previous values
1055NamedObjMgr omg;
1056string vn="#";
1057omg.DeleteVar(vn);
1058for(k=0; k<99; k++) {
1059 sprintf(buff,"%d",k);
1060 vn = buff;
1061 omg.DeleteVar(vn);
1062 }
1063// We then set them
1064string vval;
1065vn="#";
1066sprintf(buff,"%d",(int)args.size());
1067omg.SetVar(vn, buff);
1068for(k=0; k<args.size(); k++) {
1069 sprintf(buff,"%d",k);
1070 vn = buff;
1071 omg.SetVar(vn, args[k]);
1072 }
1073
1074if (trace) {
1075 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
1076 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1077 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
1078 }
1079
1080bool ohv = histon;
1081histon = false;
1082while (fgets(line_buff,511,fip) != NULL)
1083 {
1084 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
1085 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1086 string line(line_buff);
1087 if (Interpret(line) == 77777) break;
1088 }
1089histon = ohv;
1090
1091// hist << "### End of Exec( " << file << " ) " << endl;
1092if (trace) {
1093 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
1094 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1095 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
1096 }
1097
1098return(0);
1099}
1100
1101
1102static string* videstr = NULL;
1103/* --Methode-- */
1104string& PIACmd::GetUsage(const string& kw)
1105{
1106bool fndok = false;
1107CmdExmap::iterator it = cmdexmap.find(kw);
1108if (it == cmdexmap.end()) {
1109 it = helpexmap.find(kw);
1110 if (it != helpexmap.end()) fndok = true;
1111 }
1112 else fndok = true;
1113if (fndok) return( (*it).second.us );
1114// Keyword pas trouve
1115if (videstr == NULL) videstr = new string("");
1116*videstr = "Nothing known about " + kw + " ?? ";
1117return(*videstr);
1118
1119}
1120
1121/* --Methode-- */
1122void PIACmd::ShowHelpWindow()
1123{
1124helpwin->Show();
1125}
1126
1127/* --Methode-- */
1128void PIACmd::ShowCxxOptionWindow()
1129{
1130cxxoptwin->Show();
1131}
1132
1133/* --Methode-- */
1134void PIACmd::ShowCxxExecWindow()
1135{
1136cxxexwin->Show();
1137}
1138
1139/* --Methode-- */
1140void PIACmd::HelptoLaTex(string const & fname)
1141{
1142FILE *fip;
1143if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1144 cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
1145 return;
1146 }
1147
1148fputs("% ----- Liste des groupes de Help ----- \n",fip);
1149fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1150fputs("\\begin{itemize} \n",fip);
1151CmdHGroup::iterator it;
1152for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
1153 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1154 (*it).first.c_str(), (*it).first.c_str());
1155
1156fputs("\\end{itemize} \n",fip);
1157
1158fputs("\\newpage \n",fip);
1159
1160CmdExmap::iterator ite;
1161fputs("% ----- Liste de toutes les commandes ----- \n",fip);
1162fputs("\\begin{center} \n ", fip);
1163fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
1164fputs("\n \n \\vspace{5mm} \n",fip);
1165fputs("\\begin{tabular}{llllll} \n", fip);
1166int kt = 0;
1167for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1168 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1169 kt++;
1170 if (kt < 3) fputs(" & ", fip);
1171 else { fputs(" \\\\ \n", fip); kt = 0; }
1172 }
1173if (kt == 1) fputs(" & & & \\\\ \n", fip);
1174else if (kt == 2) fputs(" & \\\\ \n", fip);
1175fputs("\\end{tabular} \n", fip);
1176fputs("\\end{center} \n", fip);
1177fputs("\n \n \\vspace{1cm} \n",fip);
1178
1179fputs("\\begin{center} \n ", fip);
1180fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
1181fputs("\n \n \\vspace{5mm} \n",fip);
1182fputs("\\begin{tabular}{llllll} \n", fip);
1183kt = 0;
1184for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1185 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1186 kt++;
1187 if (kt < 3) fputs(" & ", fip);
1188 else { fputs(" \\\\ \n", fip); kt = 0; }
1189 }
1190if (kt == 1) fputs(" & & & \\\\ \n", fip);
1191else if (kt == 2) fputs(" & \\\\ \n", fip);
1192fputs("\\end{tabular} \n", fip);
1193fputs("\\end{center} \n", fip);
1194// fputs("\\newline \n",fip);
1195
1196fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
1197fputs("\\newpage \n",fip);
1198int gid;
1199for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1200 gid = (*it).second;
1201 if (gid == 0) continue;
1202 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1203 (*it).first.c_str(), (*it).first.c_str());
1204 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1205 if ((*ite).second.group != gid) continue;
1206 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1207 (*ite).first.c_str(), (*ite).first.c_str());
1208 fputs("\\begin{verbatim} \n",fip);
1209 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1210 fputs("\\end{verbatim} \n",fip);
1211 }
1212 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1213 if ((*ite).second.group != gid) continue;
1214 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1215 (*ite).first.c_str(), (*ite).first.c_str());
1216 fputs("\\begin{verbatim} \n",fip);
1217 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1218 fputs("\\end{verbatim} \n",fip);
1219 }
1220}
1221
1222fclose(fip);
1223return;
1224}
1225
1226
1227
Note: See TracBrowser for help on using the repository browser.