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

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

Correction traitement lignes suites (avec backslash) ds PIACmd - Reza 18/6/2001

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