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
RevLine 
[293]1#include "piacmd.h"
[165]2#include <stdio.h>
3#include <stdlib.h>
[349]4#include <ctype.h>
[165]5#include <math.h>
6
[293]7#include "basexecut.h"
[165]8
[293]9#include "pdlmgr.h"
[165]10#include "ctimer.h"
[293]11// #include "dlftypes.h"
[165]12
[293]13#include "pistdimgapp.h"
[165]14#include "nobjmgr.h"
[361]15#include "piafitting.h"
[463]16#include "pawexecut.h"
[1224]17#include "cxxexecutor.h"
[1251]18#include "cxxexecwin.h"
[165]19
[293]20#include PISTDWDG_H
21#include PILIST_H
[165]22
[293]23// ------------------------------------------------------------
[349]24// Gestion d'une fenetre d'aide interactive
25// Classe PIAHelpWind
[293]26// ------------------------------------------------------------
[165]27
[293]28class PIAHelpWind : public PIWindow {
29public :
30 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
31 virtual ~PIAHelpWind();
[330]32 virtual void Show();
[293]33 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]34 inline void AddHelpGroup(const char * hgrp, int gid)
35 { hgrpom->AppendItem(hgrp, 20000+gid); }
36 inline void ClearHelpList()
37 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]38 inline void AddHelpItem(const char * hitem)
[330]39 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[293]40protected :
41 PIStdImgApp* dap;
42 PIACmd* piac;
43 int mNitem;
44 PIList* hitemlist;
[330]45 PIOptMenu* hgrpom;
[293]46 PIButton * mBut;
47 PILabel * mLab;
48 PIText* mTxt;
49};
[165]50
[293]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;
[330]58SetMsg(77);
[165]59
[293]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);
[330]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);
[293]70hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
71// hitemlist->SetBorderWidth(2);
[324]72mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
73// mTxt->SetMutiLineMode(true);
[293]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("");
[330]81mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]82mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
83}
[165]84
85/* --Methode-- */
[293]86PIAHelpWind::~PIAHelpWind()
87{
[330]88delete hgrpom;
[293]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);
[330]99if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]100 Hide();
101 return;
102 }
[330]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) ) {
[293]109 string s = hitemlist->GetSelectionStr();
110 mTxt->SetText(piac->GetUsage(s));
111 mLab->SetLabel(s);
112 }
113}
114
[330]115/* --Methode-- */
116void PIAHelpWind::Show()
117{
118hgrpom->SetValue(20000); // Groupe All
119mTxt->SetText("");
120mLab->SetLabel("");
121piac->UpdateHelpList(this, 0);
122PIWindow::Show();
123}
[293]124
[349]125// ------------------------------------------------------------
126// Bloc de commandes (Foreach, ...)
127// Classe PIACmdBloc
128// ------------------------------------------------------------
129
130class PIACmdBloc {
131public:
[1262]132 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
133
[1562]134 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
[1262]135 ~PIACmdBloc();
[349]136 inline PIACmdBloc* Parent() { return(parent); }
[1262]137 inline bool CheckOK() { return blkok; }
[349]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;
[1262]146 bool blkok; // true -> block OK
147 BType typ; // foreach , integer loop, float loop, test
[349]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-- */
[1562]158PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
[349]159{
160piacmd = piac;
161parent = par;
[1262]162blkok = false;
163typ = BT_None;
[349]164i1 = 0; i2 = -1; di = 1;
165f1 = 0.; f2 = -1.; df = 1.;
166if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
[1562]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
[349]171 for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
[1262]172 typ = BT_ForeachList;
[1276]173 blkok = true;
[349]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) {
[1262]193 typ = BT_ForeachFloat;
[1276]194 blkok = true;
[349]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 {
[1262]201 typ = BT_ForeachInt;
[1276]202 blkok = true;
[349]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];
[1262]227if (typ == BT_ForeachList) // foreach string loop
[349]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 }
[1262]237else if (typ == BT_ForeachInt) // Integer loop
[349]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 }
[1262]253else if (typ == BT_ForeachFloat) // float loop
[349]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
[293]277static PIACmd* curpiacmd = NULL;
278/* --Methode-- */
[165]279PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
280{
[293]281mObjMgr = omg;
[165]282mImgApp = app;
283system("cp history.pic hisold.pic");
284hist.open("history.pic");
[349]285histon = true;
[165]286trace = false; timing = false;
287gltimer = NULL;
[349]288curblk = NULL;
289felevel = 0;
[1262]290bloccxx = false;
291bloctest = false;
[1276]292mulinecmd = "";
293mulinefg = false;
[293]294
[330]295cmdhgrp["All"] = 0;
296cmdgrpid = 1;
297cmdhgrp["Commands"] = 1;
[293]298helpwin = new PIAHelpWind(app, this);
[330]299helpwin->AddHelpGroup("All", 0);
300helpwin->AddHelpGroup("Commands", 1);
[293]301
302string kw = "piacmd";
303string usage;
304usage = ">>> (piacmd) Interpreter's keywords : \n";
[349]305usage += " > set varname string # To set a variable, $varname \n";
306usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
[333]307usage += " > setol varname patt # Fills varname with object list \n";
[293]308usage += " > unset varname # clear variable definition \n";
[333]309usage += " > echo string # output string \n";
[353]310usage += " > alias name string # define a command alias \n";
311usage += " > readstdin varname # reads a line from stdin into $varname \n";
[349]312usage += " > foreach varname string-list # Loop \n";
[1562]313usage += " > for varname i1:i2[:di] # Integer loop \n";
314usage += " > for varname f1:f2[:df] # Float loop \n";
[349]315usage += " > end # end loops \n";
[353]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";
[293]320usage += " > shell comand_string # Execute shell command \n";
321usage += " > help <command_name> # <command_name> usage info \n";
322usage += " > helpwindow # Displays help window \n";
[349]323usage += " > timingon timingoff traceon traceoff \n";
[330]324string grp = "Commands";
325RegisterHelp(kw, usage, grp);
[293]326
327basexec = new PIABaseExecutor(this, omg, app);
[361]328fitexec = new PIAFitter(this, app);
[463]329pawexec = new PAWExecutor(this, app);
[1251]330CxxExecutor * cxxe = new CxxExecutor(this, app);
331cxxexec = cxxe;
332cxxoptwin = new CxxOptionWind(app, cxxe);
333cxxexwin = new CxxExecWind(app, cxxe);
334
[293]335AddInterpreter(this);
336curcmdi = this;
[165]337}
338
339/* --Methode-- */
340PIACmd::~PIACmd()
341{
342hist.close();
343if (gltimer) { delete gltimer; gltimer = NULL; }
[293]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;
[1251]352delete cxxexwin;
353delete cxxoptwin;
[293]354if (curpiacmd == this) curpiacmd = NULL;
[361]355delete basexec;
356delete fitexec;
[463]357delete pawexec;
[1224]358delete cxxexec;
[165]359}
360
361/* --Methode-- */
[293]362PIACmd* PIACmd::GetInterpreter()
[165]363{
[293]364return(curpiacmd);
365}
[165]366
[293]367/* --Methode-- */
368string PIACmd::Name()
369{
370return("piacmd");
371}
[165]372
[293]373/* --Methode-- */
[330]374void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]375{
[330]376if (!ce) {
377 RegisterHelp(keyw, usage, grp);
378 return;
379 }
380int gid = CheckHelpGrp(grp);
[293]381cmdex cme;
[330]382cme.group = gid;
[293]383cme.us = usage;
384cme.cex = ce;
385cmdexmap[keyw] = cme;
386}
387
388/* --Methode-- */
[330]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-- */
[293]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;
[165]435 }
[293]436string fname = name + "_init";
437DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
438if (!finit) {
439 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
440 return;
[165]441 }
[293]442cout << "PIACmd/LoadModule_Info: Initialisation module" << name
443 << " " << fname << "() ..." << endl;
444finit();
445modmap[name] = dynlink;
446return;
447}
[165]448
[293]449/* --Methode-- */
450void PIACmd::AddInterpreter(CmdInterpreter * cl)
451{
452if (!cl) return;
[349]453interpmap[cl->Name()] = cl;}
[165]454
455/* --Methode-- */
[293]456void PIACmd::SelInterpreter(string& name)
[165]457{
[293]458InterpMap::iterator it = interpmap.find(name);
459if (it == interpmap.end()) return;
460curcmdi = (*it).second;
461}
[165]462
[293]463
464
[357]465/* Fonction */
[368]466static string GetStringFrStdin(PIACmd* piac)
[357]467{
[368]468PIStdImgApp* piapp = piac->GetImgApp();
469if (piapp) {
470 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
471 if (wdg) wdg->Refresh();
[357]472#ifndef __mac__
[368]473 /* On vide le buffer X-Window */
[374]474 XSync(PIXDisplay(),False);
[357]475#endif
[368]476}
[357]477char buff[128];
478fgets(buff, 128, stdin);
479buff[127] = '\0';
480return((string)buff);
481}
482
[293]483/* --Methode-- */
484int PIACmd::Interpret(string& s)
485{
[333]486int rc = 0;
[1262]487NamedObjMgr omg;
[165]488
[1262]489// On saute de commandes vides
490size_t l;
[349]491l = s.length();
492if (l < 1) return(0);
[165]493
[349]494// On enregistre les commandes
495if (histon) hist << s << endl;
496
[1262]497if (s[0] == '#') return(0); // si c'est un commentaire
[349]498
[1276]499// Logique de gestion des lignes suite
500// un \ en derniere position indique la presence d'une ligne suite
[1539]501size_t lnb = s.find_last_not_of(' ');
502if (s[lnb] == '\\' ) { // Lignes suite ...
[1276]503 mulinefg = true;
[1539]504 mulinecmd += s.substr(0,lnb);
[1276]505 mImgApp->GetConsole()->SetPrompt("...? ");
506 return(0);
507}
[165]508
[1276]509if (mulinefg) { // Il y avait des lignes suite
510 s = mulinecmd + s;
511 mulinecmd = "";
512 mulinefg = false;
[1562]513 const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
[1276]514 mImgApp->GetConsole()->SetPrompt(rprompt);
515}
516
[1262]517// Removing leading blanks
[1276]518size_t p,q;
[1262]519
520p=s.find_first_not_of(" \t");
521if (p < l) s = s.substr(p);
522
[349]523// >>>> Substitution d'alias (1er mot)
[1262]524CmdVarList::iterator it;
[449]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);
[349]535 p = 0;
536 q = s.find_first_of(" ");
537 }
538
539// >>>> Separating keyword
[1262]540string toks,kw;
[165]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
[1562]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
[349]560 PIACmdBloc* curb = curblk;
561 curblk = curb->Parent();
[1562]562 mImgApp->GetConsole()->SetPrompt("Cmd> ");
[349]563 // cout << " *DBG* Executing bloc " << endl;
[1562]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);
[349]579 return(0);
580 }
[1562]581}
582if ((testresult.size() > 0) && !(*tresit)) return(0);
[349]583
[1262]584// Nous ne sommes donc pas dans un bloc .... Substitution de variables
[1276]585string s2;
586int rcs ;
[1262]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 }
[1276]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);
[1269]600 return(cxxe->ExecuteCXX(s2.substr(1)));
[1262]601 }
602}
603
604
[1276]605rcs = SubstituteVars(s, s2);
606if (rcs) return(rcs);
[1262]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;
[1276]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 }
[1262]632 string token = toks.substr(p,q-p);
633 tokens.push_back(token);
634 }
635
636
[1562]637// Si c'est un for/foreach, on cree un nouveau bloc
638if ((kw == "foreach") || (kw == "for")) {
[1262]639 // cout << " *DBG* We got a foreach... " << endl;
[1562]640 PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
[1262]641 if (!bloc->CheckOK()) {
[1562]642 cerr << "for/foreach syntax Error ! " << endl;
[1262]643 delete bloc;
644 return(0);
645 }
646 felevel++;
647 if (curblk) curblk->AddBloc(bloc);
[1562]648 else mImgApp->GetConsole()->SetPrompt("for...> ");
[1262]649 curblk = bloc;
650 // cout << " *DBG* New Bloc created ... " << endl;
651 return(0);
652 }
[1562]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}
[1262]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="";
[349]677p = 0;
678l = s.length();
679string vn;
680while (p < l) {
681 q = s.find('$',p);
[1276]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 }
[349]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) {
[1276]699 cerr << " Syntax error - line ending with $ !!! " << endl;
700 return(2);
[349]701 }
702 vn = "";
703 if ( s[q+1] == '{' ) { // Variable in the form ${name}
704 q2 = s.find('}',q+1);
705 if (q2 >= l) {
[1276]706 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
707 return(3);
[349]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) {
[1276]715 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
716 return(4);
[349]717 }
718 vn = s.substr(q+2,q2-q-2);
[1262]719 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
[349]720 cerr << " Error: Undefined variable " << vn << " ! " << endl;
[1276]721 return(5);
[349]722 }
[1262]723 vn = omg.GetVar(vn);
[349]724 q2++;
725 }
726 else {
[1562]727 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
[349]728 if (q2 > l) q2 = l;
729 vn = s.substr(q+1, q2-q-1);
730 }
[1262]731 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
[349]732 cerr << " Error: Undefined variable " << vn << " ! " << endl;
[1276]733 return(5);
[349]734 }
[1262]735 s2 += (s.substr(p, q-p) + omg.GetVar(vn));
[349]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
[1262]743return(0);
744}
[349]745
[1262]746/* --Methode-- */
[1562]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-- */
[1262]757int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
758{
759int rc = 0;
760NamedObjMgr omg;
[349]761
[165]762// >>>>>>>>>>> Commande d'interpreteur
[1262]763if (kw == "helpwindow") ShowHelpWindow();
[293]764else if (kw == "help") {
[330]765 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]766 else {
[330]767 string kwh = "piacmd";
768 cout << GetUsage(kwh) << endl;
[293]769 }
[165]770 }
771
772else if (kw == "set") {
[293]773 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[349]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 }
[1276]778 // string xx = tokens[1];
779 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
780 omg.SetVar(tokens[0], tokens[1]);
[165]781 }
[349]782
783else if (kw == "getvar") {
784 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
[1262]785 if (!omg.HasVar(tokens[1])) {
[349]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 }
[1262]793 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
[349]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
[333]807else if (kw == "setol") {
808 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
[349]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 }
[333]813 vector<string> ol;
[349]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 }
[1262]821 omg.SetVar(tokens[0], vol);
[333]822 }
[293]823else if (kw == "unset") {
824 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[1262]825 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
[293]826 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]827 }
[333]828else if (kw == "echo") {
[1276]829 for (int ii=0; ii<tokens.size(); ii++)
830 cout << tokens[ii] << " " ;
[333]831 cout << endl;
[1276]832 }
[353]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 }
[357]839 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
840 cout << tokens[0] << " ? " << endl;
[1262]841 omg.SetVar(tokens[0], GetStringFrStdin(this) );
[353]842 }
843
[165]844else if (kw == "listvars") {
[293]845 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[1262]846 DVList& varlist = omg.GetVarList();
847 DVList::ValList::const_iterator it;
848 string value;
849 for(it = varlist.Begin(); it != varlist.End(); it++) {
[1291]850#ifdef SANS_EVOLPLANCK
851 MuTyV mtv = (*it).second;
852 value = (string)(mtv);
853#else
[1262]854 value = (string)((*it).second.elval);
[1291]855#endif
[1262]856 cout << (*it).first << " = " << value << "\n";
857 }
[165]858 cout << endl;
859 }
[353]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") {
[330]868 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
[293]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; }
[165]880else if (kw == "timingon") {
[293]881 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
882 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
883 }
[165]884else if (kw == "timingoff") {
[293]885 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
886 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
887 }
[165]888else if (kw == "exec") {
[293]889 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
[349]890 ExecFile(tokens[0], tokens);
[165]891 }
892else if (kw == "shell") {
[293]893 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[1276]894 string cmd;
895 for (int ii=0; ii<tokens.size(); ii++)
896 cmd += (tokens[ii] + ' ');
897 system(cmd.c_str());
[165]898 }
[1276]899
[293]900// Execution d'une commande enregistree
[1268]901else rc = ExecuteCommand(kw, tokens, toks);
[165]902
903if (timing) gltimer->Split();
[333]904return(rc);
[165]905}
906
[333]907/* --Methode-- */
[1262]908int PIACmd::ParseLineExecute(string& line)
[333]909{
[1262]910vector<string> tokens;
911
[333]912if (line.length() < 1) return(0);
[165]913
[333]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
[1268]934return(ExecuteCommand(kw, tokens, toks));
[333]935}
936
[165]937/* --Methode-- */
[1268]938int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
[333]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 {
[1268]944 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
[333]945 else cout << "Dont know how to execute " << keyw << " ? " << endl;
946 }
947 return(rc);
948}
949
950/* --Methode-- */
[349]951int PIACmd::ExecFile(string& file, vector<string>& args)
[165]952{
[293]953char line_buff[512];
954FILE *fip;
[165]955
[293]956if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
[384]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) {
[293]966 cerr << "PIACmd::Exec() Error opening file " << file << endl;
967 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
968 return(0);
[165]969 }
[349]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
[1262]977NamedObjMgr omg;
[349]978string vn="#";
[1262]979omg.DeleteVar(vn);
[349]980for(k=0; k<99; k++) {
981 sprintf(buff,"%d",k);
982 vn = buff;
[1262]983 omg.DeleteVar(vn);
[349]984 }
985// We then set them
[1262]986string vval;
[349]987vn="#";
988sprintf(buff,"%d",(int)args.size());
[1262]989omg.SetVar(vn, buff);
[349]990for(k=0; k<args.size(); k++) {
991 sprintf(buff,"%d",k);
992 vn = buff;
[1262]993 omg.SetVar(vn, args[k]);
[349]994 }
995
[293]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 }
[165]1001
[1562]1002bool ohv = histon;
[349]1003histon = false;
[293]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);
[165]1010 }
[1562]1011histon = ohv;
[349]1012
1013// hist << "### End of Exec( " << file << " ) " << endl;
[293]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);
[165]1018 }
1019
1020return(0);
1021}
1022
[293]1023
1024static string* videstr = NULL;
1025/* --Methode-- */
1026string& PIACmd::GetUsage(const string& kw)
[165]1027{
[330]1028bool fndok = false;
[293]1029CmdExmap::iterator it = cmdexmap.find(kw);
[330]1030if (it == cmdexmap.end()) {
1031 it = helpexmap.find(kw);
1032 if (it != helpexmap.end()) fndok = true;
[165]1033 }
[330]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
[165]1041}
1042
[293]1043/* --Methode-- */
1044void PIACmd::ShowHelpWindow()
[165]1045{
[293]1046helpwin->Show();
[165]1047}
[357]1048
[463]1049/* --Methode-- */
[1251]1050void PIACmd::ShowCxxOptionWindow()
1051{
1052cxxoptwin->Show();
1053}
1054
1055/* --Methode-- */
1056void PIACmd::ShowCxxExecWindow()
1057{
1058cxxexwin->Show();
1059}
1060
1061/* --Methode-- */
[463]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 }
[357]1069
[463]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++)
[484]1075 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1076 (*it).first.c_str(), (*it).first.c_str());
[463]1077
1078fputs("\\end{itemize} \n",fip);
1079
[484]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
[463]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;
[484]1124 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1125 (*it).first.c_str(), (*it).first.c_str());
[463]1126 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1127 if ((*ite).second.group != gid) continue;
[484]1128 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1129 (*ite).first.c_str(), (*ite).first.c_str());
[463]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;
[484]1136 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1137 (*ite).first.c_str(), (*ite).first.c_str());
[463]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.