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

Last change on this file since 2218 was 2218, checked in by ansari, 23 years ago

Suite amelioration interpreteur PIACmd , variables de type $#varname
et $varname[indexvar] - Reza 21/10/2002

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