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

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

Amelioration PIACmd (for/foreach , debut if ...) - Reza 4/7/2001

File size: 31.6 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;
290bloccxx = false;
291bloctest = false;
292mulinecmd = "";
293mulinefg = false;
294
295cmdhgrp["All"] = 0;
296cmdgrpid = 1;
297cmdhgrp["Commands"] = 1;
298helpwin = new PIAHelpWind(app, this);
299helpwin->AddHelpGroup("All", 0);
300helpwin->AddHelpGroup("Commands", 1);
301
302string kw = "piacmd";
303string usage;
304usage = ">>> (piacmd) Interpreter's keywords : \n";
305usage += " > set varname string # To set a variable, $varname \n";
306usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
307usage += " > setol varname patt # Fills varname with object list \n";
308usage += " > unset varname # clear variable definition \n";
309usage += " > echo string # output string \n";
310usage += " > alias name string # define a command alias \n";
311usage += " > readstdin varname # reads a line from stdin into $varname \n";
312usage += " > foreach varname string-list # Loop \n";
313usage += " > for varname i1:i2[:di] # Integer loop \n";
314usage += " > for varname f1:f2[:df] # Float loop \n";
315usage += " > end # end loops \n";
316usage += " > listvars # List of variable names and values \n";
317usage += " > listalias # List of alias names and values \n";
318usage += " > listcommands # List of all known commands \n";
319usage += " > exec filename # Execute commands from file \n";
320usage += " > shell comand_string # Execute shell command \n";
321usage += " > help <command_name> # <command_name> usage info \n";
322usage += " > helpwindow # Displays help window \n";
323usage += " > timingon timingoff traceon traceoff \n";
324string grp = "Commands";
325RegisterHelp(kw, usage, grp);
326
327basexec = new PIABaseExecutor(this, omg, app);
328fitexec = new PIAFitter(this, app);
329pawexec = new PAWExecutor(this, app);
330CxxExecutor * cxxe = new CxxExecutor(this, app);
331cxxexec = cxxe;
332cxxoptwin = new CxxOptionWind(app, cxxe);
333cxxexwin = new CxxExecWind(app, cxxe);
334
335AddInterpreter(this);
336curcmdi = this;
337}
338
339/* --Methode-- */
340PIACmd::~PIACmd()
341{
342hist.close();
343if (gltimer) { delete gltimer; gltimer = NULL; }
344Modmap::iterator it;
345for(it = modmap.begin(); it != modmap.end(); it++) {
346 string name = (*it).first + "_end";
347 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
348 if (fend) fend();
349 delete (*it).second;
350 }
351delete helpwin;
352delete cxxexwin;
353delete cxxoptwin;
354if (curpiacmd == this) curpiacmd = NULL;
355delete basexec;
356delete fitexec;
357delete pawexec;
358delete cxxexec;
359}
360
361/* --Methode-- */
362PIACmd* PIACmd::GetInterpreter()
363{
364return(curpiacmd);
365}
366
367/* --Methode-- */
368string PIACmd::Name()
369{
370return("piacmd");
371}
372
373/* --Methode-- */
374void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
375{
376if (!ce) {
377 RegisterHelp(keyw, usage, grp);
378 return;
379 }
380int gid = CheckHelpGrp(grp);
381cmdex cme;
382cme.group = gid;
383cme.us = usage;
384cme.cex = ce;
385cmdexmap[keyw] = cme;
386}
387
388/* --Methode-- */
389void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
390{
391int gid = CheckHelpGrp(grp);
392cmdex cme;
393cme.group = gid;
394cme.us = usage;
395cme.cex = NULL;
396helpexmap[keyw] = cme;
397}
398
399/* --Methode-- */
400int PIACmd::CheckHelpGrp(string& grp)
401{
402int gid=0;
403CmdHGroup::iterator it = cmdhgrp.find(grp);
404if (it == cmdhgrp.end()) {
405 cmdgrpid++; gid = cmdgrpid;
406 cmdhgrp[grp] = gid;
407 helpwin->AddHelpGroup(grp.c_str(), gid);
408 }
409else gid = (*it).second;
410return(gid);
411}
412
413/* --Methode-- */
414void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
415{
416helpwin->ClearHelpList();
417CmdExmap::iterator it;
418for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
419 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
420 helpwin->AddHelpItem((*it).first.c_str());
421 }
422for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
423 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
424 helpwin->AddHelpItem((*it).first.c_str());
425 }
426}
427
428/* --Methode-- */
429void PIACmd::LoadModule(string& fnameso, string& name)
430{
431PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
432if (dynlink == NULL) {
433 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
434 return;
435 }
436string fname = name + "_init";
437DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
438if (!finit) {
439 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
440 return;
441 }
442cout << "PIACmd/LoadModule_Info: Initialisation module" << name
443 << " " << fname << "() ..." << endl;
444finit();
445modmap[name] = dynlink;
446return;
447}
448
449/* --Methode-- */
450void PIACmd::AddInterpreter(CmdInterpreter * cl)
451{
452if (!cl) return;
453interpmap[cl->Name()] = cl;}
454
455/* --Methode-- */
456void PIACmd::SelInterpreter(string& name)
457{
458InterpMap::iterator it = interpmap.find(name);
459if (it == interpmap.end()) return;
460curcmdi = (*it).second;
461}
462
463
464
465/* Fonction */
466static string GetStringFrStdin(PIACmd* piac)
467{
468PIStdImgApp* piapp = piac->GetImgApp();
469if (piapp) {
470 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
471 if (wdg) wdg->Refresh();
472#ifndef __mac__
473 /* On vide le buffer X-Window */
474 XSync(PIXDisplay(),False);
475#endif
476}
477char buff[128];
478fgets(buff, 128, stdin);
479buff[127] = '\0';
480return((string)buff);
481}
482
483/* --Methode-- */
484int PIACmd::Interpret(string& s)
485{
486int rc = 0;
487NamedObjMgr omg;
488
489// On saute de commandes vides
490size_t l;
491l = s.length();
492if (l < 1) return(0);
493
494// On enregistre les commandes
495if (histon) hist << s << endl;
496
497if (s[0] == '#') return(0); // si c'est un commentaire
498
499// Logique de gestion des lignes suite
500// un \ en derniere position indique la presence d'une ligne suite
501size_t lnb = s.find_last_not_of(' ');
502if (s[lnb] == '\\' ) { // Lignes suite ...
503 mulinefg = true;
504 mulinecmd += s.substr(0,lnb);
505 mImgApp->GetConsole()->SetPrompt("...? ");
506 return(0);
507}
508
509if (mulinefg) { // Il y avait des lignes suite
510 s = mulinecmd + s;
511 mulinecmd = "";
512 mulinefg = false;
513 const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
514 mImgApp->GetConsole()->SetPrompt(rprompt);
515}
516
517// Removing leading blanks
518size_t p,q;
519
520p=s.find_first_not_of(" \t");
521if (p < l) s = s.substr(p);
522
523// >>>> Substitution d'alias (1er mot)
524CmdVarList::iterator it;
525p = 0;
526q = s.find_first_of(" \t");
527l = s.length();
528string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
529it = mAliases.find(w1);
530if (it != mAliases.end()) {
531 s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
532 l = s.length();
533 p=s.find_first_not_of(" \t");
534 if (p < l) s = s.substr(p);
535 p = 0;
536 q = s.find_first_of(" ");
537 }
538
539// >>>> Separating keyword
540string toks,kw;
541if (q < l)
542 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
543else { kw = s.substr(p,l-p); toks = ""; }
544
545// les mot-cle end else endif doivent etre le seul mot de la ligne
546if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
547 size_t ltk = toks.length();
548 if (toks.find_first_not_of(" \t") < ltk) {
549 cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
550 << "line: " << s << endl;
551 return(1);
552 }
553}
554
555// On verifie si nous sommes dans un bloc (for , foreach)
556if (curblk != NULL) { // On est dans un bloc
557 if ( (kw == "for") || (kw == "foreach")) felevel++;
558 else if (kw == "end") felevel--;
559 if (felevel == 0) { // Il faut executer le bloc
560 PIACmdBloc* curb = curblk;
561 curblk = curb->Parent();
562 mImgApp->GetConsole()->SetPrompt("Cmd> ");
563 // cout << " *DBG* Executing bloc " << endl;
564 bool ohv = histon;
565 histon = false;
566 curb->Execute();
567 histon = ohv;
568 }
569 else curblk->AddLine(s);
570 return(0);
571}
572
573// Sommes-nous dans un bloc de test if then else
574if (testresult.size() > 0) { // Nous sommes ds un bloc if
575 if (kw == "endif") {
576 list<bool>::iterator dbit = tresit;
577 tresit--;
578 testresult.erase(dbit);
579 return(0);
580 }
581}
582if ((testresult.size() > 0) && !(*tresit)) return(0);
583
584// Nous ne sommes donc pas dans un bloc .... Substitution de variables
585string s2;
586int rcs ;
587// Execution de code C++
588
589if (s[0] == '@') {
590 CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
591 if (cxxe == NULL) {
592 cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
593 return(99);
594 }
595 // Sans substitution des variables $
596 if (s[1] == '@') return(cxxe->ExecuteCXX(s.substr(2)));
597 else { // AVEC substitution des variables $
598 rcs = SubstituteVars(s, s2);
599 if (rcs) return(rcs);
600 return(cxxe->ExecuteCXX(s2.substr(1)));
601 }
602}
603
604
605rcs = SubstituteVars(s, s2);
606if (rcs) return(rcs);
607
608// >>>> Separating keyword and tokens
609vector<string> tokens;
610
611q = s2.find(' ');
612l = s2.length();
613if (q < l)
614 { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
615else { kw = s2; toks = ""; }
616
617q = 0;
618while (q < l) {
619 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
620 if (p>=l) break;
621 if ( (toks[p] == '\'') || (toks[p] == '"') ) {
622 q = toks.find(toks[p],p+1);
623 if (q>=l) {
624 cerr << "Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
625 return(2);
626 }
627 p++;
628 }
629 else {
630 q = toks.find_first_of(" \t",p); // la fin du token;
631 }
632 string token = toks.substr(p,q-p);
633 tokens.push_back(token);
634 }
635
636
637// Si c'est un for/foreach, on cree un nouveau bloc
638if ((kw == "foreach") || (kw == "for")) {
639 // cout << " *DBG* We got a foreach... " << endl;
640 PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
641 if (!bloc->CheckOK()) {
642 cerr << "for/foreach syntax Error ! " << endl;
643 delete bloc;
644 return(0);
645 }
646 felevel++;
647 if (curblk) curblk->AddBloc(bloc);
648 else mImgApp->GetConsole()->SetPrompt("for...> ");
649 curblk = bloc;
650 // cout << " *DBG* New Bloc created ... " << endl;
651 return(0);
652 }
653else if (kw == "if") { // Un test
654 bool res_tst = EvaluateTest(tokens);
655 testresult.push_back(res_tst);
656 if (testresult.size() == 1) tresit = testresult.begin();
657 else tresit++;
658}
659else rc = ExecuteCommandLine(kw, tokens, toks);
660return(rc);
661
662// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
663// for(int ii=0; ii<tokens.size(); ii++)
664// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
665
666}
667
668/* --Methode-- */
669int PIACmd::SubstituteVars(string & s, string & s2)
670// Variable substitution
671{
672NamedObjMgr omg;
673
674size_t p,q,q2,l;
675
676s2="";
677p = 0;
678l = s.length();
679string vn;
680while (p < l) {
681 q = s.find('$',p);
682 if (q > l) break;
683 q2 = s.find('\'',p);
684 if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
685 q2 = s.find('\'',q2+1);
686 if (q2 >= l) {
687 cerr << " Syntax error - Unbalenced quotes !!! " << endl;
688 return(1);
689 }
690 s2 += s.substr(p, q2-p+1);
691 p = q2+1; continue;
692 }
693 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
694 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
695 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
696 continue;
697 }
698 if (q >= l-1) {
699 cerr << " Syntax error - line ending with $ !!! " << endl;
700 return(2);
701 }
702 vn = "";
703 if ( s[q+1] == '{' ) { // Variable in the form ${name}
704 q2 = s.find('}',q+1);
705 if (q2 >= l) {
706 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
707 return(3);
708 }
709 vn = s.substr(q+2,q2-q-2);
710 q2++;
711 }
712 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
713 q2 = s.find(']',q+1);
714 if (q2 >= l) {
715 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
716 return(4);
717 }
718 vn = s.substr(q+2,q2-q-2);
719 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
720 cerr << " Error: Undefined variable " << vn << " ! " << endl;
721 return(5);
722 }
723 vn = omg.GetVar(vn);
724 q2++;
725 }
726 else {
727 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
728 if (q2 > l) q2 = l;
729 vn = s.substr(q+1, q2-q-1);
730 }
731 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
732 cerr << " Error: Undefined variable " << vn << " ! " << endl;
733 return(5);
734 }
735 s2 += (s.substr(p, q-p) + omg.GetVar(vn));
736 p = q2;
737 }
738if (p < l) s2 += s.substr(p);
739
740p = s2.find_first_not_of(" \t");
741if (p < l) s2 = s2.substr(p);
742
743return(0);
744}
745
746/* --Methode-- */
747bool PIACmd::EvaluateTest(vector<string> & args)
748{
749 if (args.size() < 2) {
750 cerr << " PIACmd::EvaluateTest()/Warning - args.size() < 2 " << endl;
751 return true;
752 }
753 else return(args[0] == args[1]);
754}
755
756/* --Methode-- */
757int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
758{
759int rc = 0;
760NamedObjMgr omg;
761
762// >>>>>>>>>>> Commande d'interpreteur
763if (kw == "helpwindow") ShowHelpWindow();
764else if (kw == "help") {
765 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
766 else {
767 string kwh = "piacmd";
768 cout << GetUsage(kwh) << endl;
769 }
770 }
771
772else if (kw == "set") {
773 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
774 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
775 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
776 return(0);
777 }
778 // string xx = tokens[1];
779 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
780 omg.SetVar(tokens[0], tokens[1]);
781 }
782
783else if (kw == "getvar") {
784 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
785 if (!omg.HasVar(tokens[1])) {
786 cerr << "Error - No " << tokens[1] << " Variable " << endl;
787 return(0);
788 }
789 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
790 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
791 return(0);
792 }
793 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
794 }
795
796else if (kw == "alias") {
797 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
798 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
799 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
800 return(0);
801 }
802 string xx = tokens[1];
803 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
804 mAliases[tokens[0]] = xx;
805 }
806
807else if (kw == "setol") {
808 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
809 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
810 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
811 return(0);
812 }
813 vector<string> ol;
814 mObjMgr->GetObjList(tokens[1], ol);
815 string vol;
816 if (ol.size() < 1) vol = "";
817 else {
818 vol = ol[0];
819 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
820 }
821 omg.SetVar(tokens[0], vol);
822 }
823else if (kw == "unset") {
824 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
825 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
826 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
827 }
828else if (kw == "echo") {
829 for (int ii=0; ii<tokens.size(); ii++)
830 cout << tokens[ii] << " " ;
831 cout << endl;
832 }
833else if (kw == "readstdin") {
834 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
835 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
836 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
837 return(0);
838 }
839 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
840 cout << tokens[0] << " ? " << endl;
841 omg.SetVar(tokens[0], GetStringFrStdin(this) );
842 }
843
844else if (kw == "listvars") {
845 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
846 DVList& varlist = omg.GetVarList();
847 DVList::ValList::const_iterator it;
848 string value;
849 for(it = varlist.Begin(); it != varlist.End(); it++) {
850#ifdef SANS_EVOLPLANCK
851 MuTyV mtv = (*it).second;
852 value = (string)(mtv);
853#else
854 value = (string)((*it).second.elval);
855#endif
856 cout << (*it).first << " = " << value << "\n";
857 }
858 cout << endl;
859 }
860else if (kw == "listalias") {
861 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
862 CmdVarList::iterator it;
863 for(it = mAliases.begin(); it != mAliases.end(); it++)
864 cout << (*it).first << " = " << (*it).second << "\n";
865 cout << endl;
866 }
867else if (kw == "listcommands") {
868 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
869 CmdExmap::iterator it;
870 int kc = 0;
871 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
872 cout << (*it).first << " ";
873 kc++;
874 if (kc >= 5) { cout << "\n"; kc = 0; }
875 }
876 cout << endl;
877 }
878else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
879else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
880else if (kw == "timingon") {
881 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
882 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
883 }
884else if (kw == "timingoff") {
885 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
886 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
887 }
888else if (kw == "exec") {
889 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
890 ExecFile(tokens[0], tokens);
891 }
892else if (kw == "shell") {
893 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
894 string cmd;
895 for (int ii=0; ii<tokens.size(); ii++)
896 cmd += (tokens[ii] + ' ');
897 system(cmd.c_str());
898 }
899
900// Execution d'une commande enregistree
901else rc = ExecuteCommand(kw, tokens, toks);
902
903if (timing) gltimer->Split();
904return(rc);
905}
906
907/* --Methode-- */
908int PIACmd::ParseLineExecute(string& line)
909{
910vector<string> tokens;
911
912if (line.length() < 1) return(0);
913
914string toks,kw;
915size_t p = line.find_first_not_of(" ");
916line = line.substr(p);
917p = 0;
918size_t q = line.find_first_of(" ");
919size_t l = line.length();
920
921if (q < l)
922 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
923else { kw = line.substr(p,l-p); toks = ""; }
924
925q = 0;
926while (q < l) {
927 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
928 if (p>=l) break;
929 q = toks.find_first_of(" ",p); // la fin du token;
930 string token = toks.substr(p,q-p);
931 tokens.push_back(token);
932 }
933
934return(ExecuteCommand(kw, tokens, toks));
935}
936
937/* --Methode-- */
938int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
939{
940 int rc = -1;
941 CmdExmap::iterator it = cmdexmap.find(keyw);
942 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
943 else {
944 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
945 else cout << "Dont know how to execute " << keyw << " ? " << endl;
946 }
947 return(rc);
948}
949
950/* --Methode-- */
951int PIACmd::ExecFile(string& file, vector<string>& args)
952{
953char line_buff[512];
954FILE *fip;
955
956if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
957 if (file.find('.') >= file.length()) {
958 cout << "PIACmd::Exec(): Error opening file " << file << endl;
959 file += ".pic";
960 cout << " Trying file " << file << endl;
961 fip = fopen(file.c_str(),"r");
962 }
963 }
964
965if(fip == NULL) {
966 cerr << "PIACmd::Exec() Error opening file " << file << endl;
967 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
968 return(0);
969 }
970
971// hist << "### Executing commands from " << file << endl;
972
973// Setting $0 ... $99 variables
974int k;
975char buff[32];
976// First, we clear all previous values
977NamedObjMgr omg;
978string vn="#";
979omg.DeleteVar(vn);
980for(k=0; k<99; k++) {
981 sprintf(buff,"%d",k);
982 vn = buff;
983 omg.DeleteVar(vn);
984 }
985// We then set them
986string vval;
987vn="#";
988sprintf(buff,"%d",(int)args.size());
989omg.SetVar(vn, buff);
990for(k=0; k<args.size(); k++) {
991 sprintf(buff,"%d",k);
992 vn = buff;
993 omg.SetVar(vn, args[k]);
994 }
995
996if (trace) {
997 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
998 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
999 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
1000 }
1001
1002bool ohv = histon;
1003histon = false;
1004while (fgets(line_buff,511,fip) != NULL)
1005 {
1006 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
1007 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1008 string line(line_buff);
1009 Interpret(line);
1010 }
1011histon = ohv;
1012
1013// hist << "### End of Exec( " << file << " ) " << endl;
1014if (trace) {
1015 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
1016 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1017 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
1018 }
1019
1020return(0);
1021}
1022
1023
1024static string* videstr = NULL;
1025/* --Methode-- */
1026string& PIACmd::GetUsage(const string& kw)
1027{
1028bool fndok = false;
1029CmdExmap::iterator it = cmdexmap.find(kw);
1030if (it == cmdexmap.end()) {
1031 it = helpexmap.find(kw);
1032 if (it != helpexmap.end()) fndok = true;
1033 }
1034 else fndok = true;
1035if (fndok) return( (*it).second.us );
1036// Keyword pas trouve
1037if (videstr == NULL) videstr = new string("");
1038*videstr = "Nothing known about " + kw + " ?? ";
1039return(*videstr);
1040
1041}
1042
1043/* --Methode-- */
1044void PIACmd::ShowHelpWindow()
1045{
1046helpwin->Show();
1047}
1048
1049/* --Methode-- */
1050void PIACmd::ShowCxxOptionWindow()
1051{
1052cxxoptwin->Show();
1053}
1054
1055/* --Methode-- */
1056void PIACmd::ShowCxxExecWindow()
1057{
1058cxxexwin->Show();
1059}
1060
1061/* --Methode-- */
1062void PIACmd::HelptoLaTex(string const & fname)
1063{
1064FILE *fip;
1065if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1066 cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
1067 return;
1068 }
1069
1070fputs("% ----- Liste des groupes de Help ----- \n",fip);
1071fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1072fputs("\\begin{itemize} \n",fip);
1073CmdHGroup::iterator it;
1074for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
1075 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1076 (*it).first.c_str(), (*it).first.c_str());
1077
1078fputs("\\end{itemize} \n",fip);
1079
1080fputs("\\newpage \n",fip);
1081
1082CmdExmap::iterator ite;
1083fputs("% ----- Liste de toutes les commandes ----- \n",fip);
1084fputs("\\begin{center} \n ", fip);
1085fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
1086fputs("\n \n \\vspace{5mm} \n",fip);
1087fputs("\\begin{tabular}{llllll} \n", fip);
1088int kt = 0;
1089for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1090 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1091 kt++;
1092 if (kt < 3) fputs(" & ", fip);
1093 else { fputs(" \\\\ \n", fip); kt = 0; }
1094 }
1095if (kt == 1) fputs(" & & & \\\\ \n", fip);
1096else if (kt == 2) fputs(" & \\\\ \n", fip);
1097fputs("\\end{tabular} \n", fip);
1098fputs("\\end{center} \n", fip);
1099fputs("\n \n \\vspace{1cm} \n",fip);
1100
1101fputs("\\begin{center} \n ", fip);
1102fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
1103fputs("\n \n \\vspace{5mm} \n",fip);
1104fputs("\\begin{tabular}{llllll} \n", fip);
1105kt = 0;
1106for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1107 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1108 kt++;
1109 if (kt < 3) fputs(" & ", fip);
1110 else { fputs(" \\\\ \n", fip); kt = 0; }
1111 }
1112if (kt == 1) fputs(" & & & \\\\ \n", fip);
1113else if (kt == 2) fputs(" & \\\\ \n", fip);
1114fputs("\\end{tabular} \n", fip);
1115fputs("\\end{center} \n", fip);
1116// fputs("\\newline \n",fip);
1117
1118fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
1119fputs("\\newpage \n",fip);
1120int gid;
1121for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1122 gid = (*it).second;
1123 if (gid == 0) continue;
1124 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1125 (*it).first.c_str(), (*it).first.c_str());
1126 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1127 if ((*ite).second.group != gid) continue;
1128 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1129 (*ite).first.c_str(), (*ite).first.c_str());
1130 fputs("\\begin{verbatim} \n",fip);
1131 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1132 fputs("\\end{verbatim} \n",fip);
1133 }
1134 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1135 if ((*ite).second.group != gid) continue;
1136 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1137 (*ite).first.c_str(), (*ite).first.c_str());
1138 fputs("\\begin{verbatim} \n",fip);
1139 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1140 fputs("\\end{verbatim} \n",fip);
1141 }
1142}
1143
1144fclose(fip);
1145return;
1146}
1147
1148
1149
Note: See TracBrowser for help on using the repository browser.