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
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 "strutil.h"
12#include "strutilxx.h"
13// #include "dlftypes.h"
14
15#include "pistdimgapp.h"
16#include "nobjmgr.h"
17#include "piafitting.h"
18#include "pawexecut.h"
19#include "cxxexecutor.h"
20#include "cxxexecwin.h"
21#include "contmodex.h"
22#include "flowmodex.h"
23
24#include PISTDWDG_H
25#include PILIST_H
26
27// ------------------------------------------------------------
28// Gestion d'une fenetre d'aide interactive
29// Classe PIAHelpWind
30// ------------------------------------------------------------
31
32class PIAHelpWind : public PIWindow {
33public :
34 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
35 virtual ~PIAHelpWind();
36 virtual void Show();
37 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
38 inline void AddHelpGroup(const char * hgrp, int gid)
39 { hgrpom->AppendItem(hgrp, 20000+gid); }
40 inline void ClearHelpList()
41 { mNitem=0; hitemlist->DeleteAllItems(); }
42 inline void AddHelpItem(const char * hitem)
43 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
44protected :
45 PIStdImgApp* dap;
46 PIACmd* piac;
47 int mNitem;
48 PIList* hitemlist;
49 PIOptMenu* hgrpom;
50 PIButton * mBut;
51 PILabel * mLab;
52 PIText* mTxt;
53};
54
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;
62SetMsg(77);
63
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);
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);
74hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
75// hitemlist->SetBorderWidth(2);
76mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
77// mTxt->SetMutiLineMode(true);
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("");
85mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
86mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
87}
88
89/* --Methode-- */
90PIAHelpWind::~PIAHelpWind()
91{
92delete hgrpom;
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);
103if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
104 Hide();
105 return;
106 }
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) ) {
113 string s = hitemlist->GetSelectionStr();
114 mTxt->SetText(piac->GetUsage(s));
115 mLab->SetLabel(s);
116 }
117}
118
119/* --Methode-- */
120void PIAHelpWind::Show()
121{
122hgrpom->SetValue(20000); // Groupe All
123mTxt->SetText("");
124mLab->SetLabel("");
125piac->UpdateHelpList(this, 0);
126PIWindow::Show();
127}
128
129// ------------------------------------------------------------
130// Bloc de commandes (Foreach, ...)
131// Classe PIACmdBloc
132// ------------------------------------------------------------
133
134class PIACmdBloc {
135public:
136 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
137
138 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
139 ~PIACmdBloc();
140 inline PIACmdBloc* Parent() { return(parent); }
141 inline bool CheckOK() { return blkok; }
142 inline void AddLine(string& line)
143 { lines.push_back(line); bloclineid.push_back(lines.size()); }
144 void AddLine(string& line, string& kw);
145 inline void AddBloc(PIACmdBloc* blk)
146 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
147 PIACmdBloc* Execute();
148 inline int& TestLevel() { return testlevel; }
149protected:
150 PIACmd* piacmd;
151 PIACmdBloc* parent;
152 bool blkok; // true -> block OK
153 BType typ; // foreach , integer loop, float loop, test
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;
161 int testlevel; // niveau d'imbrication des if
162};
163
164/* --Methode-- */
165PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
166{
167piacmd = piac;
168parent = par;
169blkok = false;
170typ = BT_None;
171i1 = 0; i2 = -1; di = 1;
172f1 = 0.; f2 = -1.; df = 1.;
173testlevel = 0;
174if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
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
179 if ((args[1] != "(") || (args[args.size()-1] != ")") ) return;
180 for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
181 typ = BT_ForeachList;
182 blkok = true;
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) {
202 typ = BT_ForeachFloat;
203 blkok = true;
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 {
210 typ = BT_ForeachInt;
211 blkok = true;
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-- */
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-- */
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];
244int rcc = 0;
245
246if (typ == BT_ForeachList) // foreach string loop
247 for(k=0; k<strlist.size(); k++) {
248 cmd = "set " + varname + " '" + strlist[k] + "'";
249 piacmd->Interpret(cmd);
250 for(kj=0; kj<bloclineid.size(); kj++) {
251 kk = bloclineid[kj];
252 if (kk > 0) {
253 rcc = piacmd->Interpret(lines[kk-1]);
254 if (rcc == 77766) break;
255 }
256 else blocs[-kk-1]->Execute();
257 }
258 if (rcc == 77766) break;
259 }
260else if (typ == BT_ForeachInt) // Integer loop
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];
272 if (kk > 0) {
273 rcc = piacmd->Interpret(lines[kk-1]);
274 if (rcc == 77766) break;
275 }
276 else blocs[-kk-1]->Execute();
277 }
278 if (rcc == 77766) break;
279 }
280else if (typ == BT_ForeachFloat) // float loop
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];
292 if (kk > 0) {
293 rcc = piacmd->Interpret(lines[kk-1]);
294 if (rcc == 77766) break;
295 }
296 else blocs[-kk-1]->Execute();
297 }
298 if (rcc == 77766) break;
299 }
300
301return(parent);
302}
303
304// ------------------
305// Classe PIACmdScript
306// ------------------
307class PIACmdScript {
308public:
309 PIACmdScript();
310 virtual ~PIACmdScript();
311};
312
313// ------------------------------------------------------------
314// Classe PIACmd
315// ------------------------------------------------------------
316
317static PIACmd* curpiacmd = NULL;
318/* --Methode-- */
319PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
320{
321mObjMgr = omg;
322mImgApp = app;
323system("cp history.pic hisold.pic");
324hist.open("history.pic");
325histon = true;
326trace = false; timing = false;
327gltimer = NULL;
328felevel = 0;
329
330mulinecmd = "";
331mulinefg = false;
332
333CmdBlks.push(NULL);
334list<char> xtx;
335TestsStack.push(xtx);
336curtestresult = true;
337
338cmdhgrp["All"] = 0;
339cmdgrpid = 1;
340cmdhgrp["Commands"] = 1;
341helpwin = new PIAHelpWind(app, this);
342helpwin->AddHelpGroup("All", 0);
343helpwin->AddHelpGroup("Commands", 1);
344
345string kw = "piacmd";
346string usage;
347usage = ">>> (piacmd) Interpreter's keywords : \n";
348usage += " > set varname string # To set a variable, $varname \n";
349usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
350usage += " > setol varname patt # Fills varname with object list \n";
351usage += " > unset varname # clear variable definition \n";
352usage += " > echo string # output string \n";
353usage += " > alias name string # define a command alias \n";
354usage += " > readstdin varname # reads a line from stdin into $varname \n";
355usage += " > foreach varname string-list # Loop \n";
356usage += " > for varname i1:i2[:di] # Integer loop \n";
357usage += " > for varname f1:f2[:df] # Float loop \n";
358usage += " > end # end loops \n";
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";
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";
368usage += " > help <command_name> # <command_name> usage info \n";
369usage += " > helpwindow # Displays help window \n";
370usage += " > timingon timingoff traceon traceoff \n";
371string grp = "Commands";
372RegisterHelp(kw, usage, grp);
373
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
384basexec = new PIABaseExecutor(this, omg, app);
385fitexec = new PIAFitter(this, app);
386pawexec = new PAWExecutor(this, app);
387CxxExecutor * cxxe = new CxxExecutor(this, app);
388cxxexec = cxxe;
389
390ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
391cntexec = cntxx; //_OP_
392FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
393flwexec = flwxx; //_OP_
394
395cxxoptwin = new CxxOptionWind(app, cxxe);
396cxxexwin = new CxxExecWind(app, cxxe);
397
398AddInterpreter(this);
399curcmdi = this;
400}
401
402/* --Methode-- */
403PIACmd::~PIACmd()
404{
405hist.close();
406if (gltimer) { delete gltimer; gltimer = NULL; }
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;
415delete cxxexwin;
416delete cxxoptwin;
417if (curpiacmd == this) curpiacmd = NULL;
418delete basexec;
419delete fitexec;
420delete pawexec;
421delete cxxexec;
422}
423
424/* --Methode-- */
425PIACmd* PIACmd::GetInterpreter()
426{
427return(curpiacmd);
428}
429
430/* --Methode-- */
431string PIACmd::Name()
432{
433return("piacmd");
434}
435
436/* --Methode-- */
437void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
438{
439if (!ce) {
440 RegisterHelp(keyw, usage, grp);
441 return;
442 }
443int gid = CheckHelpGrp(grp);
444cmdex cme;
445cme.group = gid;
446cme.us = usage;
447cme.cex = ce;
448cmdexmap[keyw] = cme;
449}
450
451/* --Methode-- */
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-- */
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;
498 }
499string fname = name + "_init";
500DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
501if (!finit) {
502 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
503 return;
504 }
505cout << "PIACmd/LoadModule_Info: Initialisation module" << name
506 << " " << fname << "() ..." << endl;
507finit();
508modmap[name] = dynlink;
509return;
510}
511
512/* --Methode-- */
513void PIACmd::AddInterpreter(CmdInterpreter * cl)
514{
515if (!cl) return;
516interpmap[cl->Name()] = cl;}
517
518/* --Methode-- */
519void PIACmd::SelInterpreter(string& name)
520{
521InterpMap::iterator it = interpmap.find(name);
522if (it == interpmap.end()) return;
523curcmdi = (*it).second;
524}
525
526
527
528/* Fonction */
529static string GetStringFrStdin(PIACmd* piac)
530{
531PIStdImgApp* piapp = piac->GetImgApp();
532if (piapp) {
533 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
534 if (wdg) wdg->Refresh();
535#ifndef __mac__
536 /* On vide le buffer X-Window */
537 XSync(PIXDisplay(),False);
538#endif
539}
540char buff[128];
541fgets(buff, 128, stdin);
542buff[127] = '\0';
543return((string)buff);
544}
545
546/* --Methode-- */
547int PIACmd::Interpret(string& s)
548{
549int rc = 0;
550NamedObjMgr omg;
551
552
553// On saute de commandes vides
554size_t l;
555l = s.length();
556if (l < 1) return(0);
557
558// On enregistre les commandes
559if (histon) hist << s << endl;
560
561if (s[0] == '#') return(0); // si c'est un commentaire
562
563// Logique de gestion des lignes suite
564// un \ en derniere position indique la presence d'une ligne suite
565size_t lnb = s.find_last_not_of(' ');
566if (s[lnb] == '\\' ) { // Lignes suite ...
567 mulinefg = true;
568 mulinecmd += s.substr(0,lnb);
569 mImgApp->GetConsole()->SetPrompt("...? ");
570 return(0);
571}
572
573if (mulinefg) { // Il y avait des lignes suite
574 s = mulinecmd + s;
575 mulinecmd = "";
576 mulinefg = false;
577 const char * rprompt = (CmdBlks.empty()) ? "Cmd> " : "for...? ";
578 mImgApp->GetConsole()->SetPrompt(rprompt);
579}
580
581// Removing leading blanks
582size_t p,q;
583
584p=s.find_first_not_of(" \t");
585if (p < l) s = s.substr(p);
586
587// >>>> Substitution d'alias (1er mot)
588CmdStrList::iterator it;
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);
599 p = 0;
600 q = s.find_first_of(" ");
601 }
602
603// >>>> Separating keyword
604string toks,kw;
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
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)
620if (CmdBlks.top() != NULL) { // On est dans un bloc
621 if ( (kw == "for") || (kw == "foreach")) felevel++;
622 else if (kw == "end") felevel--;
623 if (felevel == 0) { // Il faut executer le bloc
624 PIACmdBloc* curb = CmdBlks.top();
625 CmdBlks.top() = curb->Parent();
626 mImgApp->GetConsole()->SetPrompt("Cmd> ");
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 }
633 // cout << " *DBG* Executing bloc " << endl;
634 bool ohv = histon;
635 histon = false;
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;
646 histon = ohv;
647 }
648 else CmdBlks.top()->AddLine(s, kw);
649 return(0);
650}
651else if (kw == "end") {
652 cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
653 << "line: " << s << endl;
654 return(1);
655}
656
657// Sommes-nous dans un bloc de test if then else
658if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if
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 {
666 const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
667 if ((*tresit)&1) curtestresult = false;
668 mImgApp->GetConsole()->SetPrompt(npr);
669 (*tresit) |= 2;
670 return(0);
671 }
672 }
673 else if (kw == "endif") {
674 list<char>::iterator dbit = tresit;
675 tresit--;
676 TestsStack.top().erase(dbit);
677 const char * npr = "Cmd> ";
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
689 if (!((*tresit)&2))
690 npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
691 else
692 npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
693 }
694 else curtestresult = true;
695 mImgApp->GetConsole()->SetPrompt(npr);
696 return(0);
697 }
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);
703}
704
705bool fgcont = true;
706if (TestsStack.top().size() > 0) { // Resultat de if ou else
707 list<char>::iterator it;
708 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
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
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
724// Nous ne sommes donc pas dans un bloc .... Substitution de variables
725string s2;
726int rcs ;
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 }
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);
740 return(cxxe->ExecuteCXX(s2.substr(1)));
741 }
742}
743
744
745rcs = SubstituteVars(s, s2);
746if (rcs) return(rcs);
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;
761 if ( (toks[p] == '\'') || (toks[p] == '"') ) {
762 q = toks.find(toks[p],p+1);
763 if (q>=l) {
764 cerr << "PIACmd::Interpret()/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
765 return(2);
766 }
767 p++;
768 }
769 else {
770 q = toks.find_first_of(" \t",p); // la fin du token;
771 }
772 string token = toks.substr(p,q-p);
773 tokens.push_back(token);
774 }
775
776
777// Si c'est un for/foreach, on cree un nouveau bloc
778if ((kw == "foreach") || (kw == "for")) {
779 // cout << " *DBG* We got a foreach... " << endl;
780 PIACmdBloc* bloc = new PIACmdBloc(this, CmdBlks.top(), kw, tokens);
781 if (!bloc->CheckOK()) {
782 cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
783 delete bloc;
784 return(1);
785 }
786 felevel++;
787 if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
788 else mImgApp->GetConsole()->SetPrompt("for...> ");
789 CmdBlks.top() = bloc;
790 // cout << " *DBG* New Bloc created ... " << endl;
791 return(0);
792 }
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;
801 TestsStack.top().push_back(res_tst);
802 if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin();
803 else tresit++;
804 const char * npr = (restst) ? "if-T> " : "if-F> ";
805 mImgApp->GetConsole()->SetPrompt(npr);
806}
807// Execution de commandes
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{
821NamedObjMgr& omg = *mObjMgr;
822
823int iarr = -1; // index d'element de tableau
824size_t p,q,q2,q3,l;
825
826s2="";
827p = 0;
828l = s.length();
829string vn, vv;
830while (p < l) {
831 q = s.find('$',p);
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 }
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) {
849 cerr << " Syntax error - line ending with $ !!! " << endl;
850 return(2);
851 }
852 vn = "";
853 if ( s[q+1] == '{' ) { // Variable in the form ${name}
854 q2 = s.find('}',q+1);
855 if (q2 >= l) {
856 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
857 return(3);
858 }
859 vn = s.substr(q+2,q2-q-2);
860 q2++;
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 }
871 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
872 q2 = s.find(']',q+1);
873 if (q2 >= l) {
874 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
875 return(4);
876 }
877 vn = s.substr(q+2,q2-q-2);
878 if (!GetVar(vn, vv)) return(5);
879 vn = vv;
880 q2++;
881 }
882 else {
883 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
884 if (q2 > l) q2 = l;
885 q3 = q2;
886 vn = s.substr(q+1, q2-q-1);
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);
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 }
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 }
911 }
912 }
913 if (!GetVar(vn, vv)) return(5);
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}
930if (p < l) s2 += s.substr(p);
931
932p = s2.find_first_not_of(" \t");
933if (p < l) s2 = s2.substr(p);
934
935return(0);
936}
937
938/* --Methode-- */
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;
952 vv = ""; return(false);
953 }
954 char buff[32];
955 long an = ArgsStack.top().size();
956 sprintf(buff,"%ld", an);
957 vv = buff; return(true);
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 }
970 vv = ArgsStack.top()[ka]; return(true);
971}
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 }
986else { // variable ordinaire geree par NamedObjMgr
987 if ( (!omg.HasVar(vn)) ) {
988 cerr << " PIACmd::SubstituteVarError: Undefined variable "
989 << vn << " ! " << endl;
990 vv = ""; return(false);
991 }
992 vv = omg.GetVar(vn); return(true);
993}
994
995return(false);
996}
997
998/* --Methode-- */
999int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
1000{
1001 res = true;
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()));
1014 else return(2);
1015 return(0);
1016}
1017
1018/* --Methode-- */
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-- */
1046int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
1047{
1048int rc = 0;
1049NamedObjMgr omg;
1050
1051// >>>>>>>>>>> Commande d'interpreteur
1052if (kw == "helpwindow") ShowHelpWindow();
1053else if (kw == "help") {
1054 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
1055 else {
1056 string kwh = "piacmd";
1057 cout << GetUsage(kwh) << endl;
1058 }
1059 }
1060
1061else if (kw == "set") {
1062 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
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 }
1067 // string xx = tokens[1];
1068 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
1069 omg.SetVar(tokens[0], tokens[1]);
1070 }
1071
1072else if (kw == "getvar") {
1073 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
1074 if (!omg.HasVar(tokens[1])) {
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 }
1082 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
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
1096else if (kw == "setol") {
1097 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
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 }
1102 vector<string> ol;
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 }
1110 omg.SetVar(tokens[0], vol);
1111 }
1112else if (kw == "unset") {
1113 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
1114 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
1115 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
1116 }
1117else if (kw == "echo") {
1118 for (int ii=0; ii<tokens.size(); ii++)
1119 cout << tokens[ii] << " " ;
1120 cout << endl;
1121 }
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 }
1128 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
1129 cout << tokens[0] << " ? " << endl;
1130 omg.SetVar(tokens[0], GetStringFrStdin(this) );
1131 }
1132
1133else if (kw == "listvars") {
1134 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
1135 DVList& varlist = omg.GetVarList();
1136 DVList::ValList::const_iterator it;
1137 string value;
1138 for(it = varlist.Begin(); it != varlist.End(); it++) {
1139#ifdef SANS_EVOLPLANCK
1140 MuTyV mtv = (*it).second;
1141 value = (string)(mtv);
1142#else
1143 value = (string)((*it).second.elval);
1144#endif
1145 cout << (*it).first << " = " << value << "\n";
1146 }
1147 cout << endl;
1148 }
1149else if (kw == "listalias") {
1150 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
1151 CmdStrList::iterator it;
1152 for(it = mAliases.begin(); it != mAliases.end(); it++)
1153 cout << (*it).first << " = " << (*it).second << "\n";
1154 cout << endl;
1155 }
1156else if (kw == "listcommands") {
1157 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
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; }
1169else if (kw == "timingon") {
1170 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
1171 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
1172 }
1173else if (kw == "timingoff") {
1174 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
1175 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
1176 }
1177else if (kw == "exec") {
1178 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
1179 ExecFile(tokens[0], tokens);
1180 }
1181else if (kw == "shell") {
1182 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
1183 string cmd;
1184 for (int ii=0; ii<tokens.size(); ii++)
1185 cmd += (tokens[ii] + ' ');
1186 system(cmd.c_str());
1187 }
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 }
1194
1195// Execution d'une commande enregistree
1196else rc = ExecuteCommand(kw, tokens, toks);
1197
1198if (timing) gltimer->Split();
1199return(rc);
1200}
1201
1202/* --Methode-- */
1203int PIACmd::ParseLineExecute(string& line)
1204{
1205vector<string> tokens;
1206
1207if (line.length() < 1) return(0);
1208
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
1229return(ExecuteCommand(kw, tokens, toks));
1230}
1231
1232/* --Methode-- */
1233int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
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 {
1239 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
1240 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1241 }
1242 return(rc);
1243}
1244
1245/* --Methode-- */
1246int PIACmd::ExecFile(string& file, vector<string>& args)
1247{
1248char line_buff[512];
1249FILE *fip;
1250
1251if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
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) {
1261 cerr << "PIACmd::Exec() Error opening file " << file << endl;
1262 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
1263 return(0);
1264 }
1265
1266// hist << "### Executing commands from " << file << endl;
1267
1268PushStack(args);
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 }
1274
1275bool ohv = histon;
1276histon = false;
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);
1282 if (Interpret(line) == 77777) break;
1283 }
1284histon = ohv;
1285
1286// hist << "### End of Exec( " << file << " ) " << endl;
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);
1291 }
1292
1293PopStack(true);
1294
1295return(0);
1296}
1297
1298/* --Methode-- */
1299int PIACmd::CShellExecute(string cmd)
1300{
1301 if(cmd.size()<=0) return -1;
1302
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
1327static string* videstr = NULL;
1328/* --Methode-- */
1329string& PIACmd::GetUsage(const string& kw)
1330{
1331bool fndok = false;
1332CmdExmap::iterator it = cmdexmap.find(kw);
1333if (it == cmdexmap.end()) {
1334 it = helpexmap.find(kw);
1335 if (it != helpexmap.end()) fndok = true;
1336 }
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
1344}
1345
1346/* --Methode-- */
1347void PIACmd::ShowHelpWindow()
1348{
1349helpwin->Show();
1350}
1351
1352/* --Methode-- */
1353void PIACmd::ShowCxxOptionWindow()
1354{
1355cxxoptwin->Show();
1356}
1357
1358/* --Methode-- */
1359void PIACmd::ShowCxxExecWindow()
1360{
1361cxxexwin->Show();
1362}
1363
1364/* --Methode-- */
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 }
1372
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++)
1378 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1379 (*it).first.c_str(), (*it).first.c_str());
1380
1381fputs("\\end{itemize} \n",fip);
1382
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
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;
1427 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1428 (*it).first.c_str(), (*it).first.c_str());
1429 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1430 if ((*ite).second.group != gid) continue;
1431 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1432 (*ite).first.c_str(), (*ite).first.c_str());
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;
1439 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1440 (*ite).first.c_str(), (*ite).first.c_str());
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.