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

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

Amelioration de l'interpreteur de base de piapp (PIAcmd), en particulier :

  • Gestion de stack pour les arguments des .pic
  • Gestion de stack pour les if then else endif
  • Gestion de stack pour les blocs for / foreach
  • Amelioration decodage des variables $varname et introduction de la possibilite d'acces aux mots d'une variable sous forme de tableau $varname[i] : le mot numero i de la chaine $varname

Reza - 20/10/2002

File size: 39.6 KB
Line 
1#include "piacmd.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <ctype.h>
5#include <math.h>
6
7#include "basexecut.h"
8
9#include "pdlmgr.h"
10#include "ctimer.h"
11#include "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 int rcdia = ctoi(sia.c_str(), &iarr);
892 if (rcdia < 0) {
893 cerr << " Syntax error - in $varname[iarr] : $"
894 << vn << "[" << sia <<"]" << endl;
895 return(4);
896 }
897 }
898 }
899 if (!GetVar(vn, vv)) return(5);
900 if (iarr < 0) {
901 s2 += (s.substr(p, q-p) + vv);
902 p = q2;
903 }
904 else {
905 vector<string> vs;
906 FillVStringFrString(vv, vs);
907 if (iarr >= vs.size()) {
908 cerr << " Substitution error - word index out of range in "
909 << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
910 return(4);
911 }
912 else s2 += (s.substr(p, q-p) + vs[iarr]);
913 p = q3+1;
914 }
915}
916if (p < l) s2 += s.substr(p);
917
918p = s2.find_first_not_of(" \t");
919if (p < l) s2 = s2.substr(p);
920
921return(0);
922}
923
924/* --Methode-- */
925bool PIACmd::GetVar(string & vn, string & vv)
926{
927NamedObjMgr& omg = *mObjMgr;
928if (vn.length() < 1) {
929 cerr << " PIACmd::SubstituteVar/Error: length(varname=" << vn << ")<1 !" << endl;
930 vv = ""; return(false);
931}
932// Variable de type $# $0 $1 ... (argument de .pic ou de script)
933int ka = 0;
934if (vn == "#") {
935 if (ArgsStack.empty()) {
936 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
937 << " ($" << vn << ")" << endl;
938 vv = ""; return(false);
939 }
940 char buff[32];
941 long an = ArgsStack.top().size();
942 sprintf(buff,"%ld", an);
943 vv = buff;
944}
945else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
946 if (ArgsStack.empty()) {
947 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
948 << " ($" << vn << ")" << endl;
949 vv = ""; return(false);
950 }
951 if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
952 cerr << " PIACmd::SubstituteVar/Error: Undefined variable ! "
953 << " ($" << vn << ")" << endl;
954 vv = ""; return(false);
955 }
956 vv = ArgsStack.top()[ka];
957}
958else { // variable ordinaire geree par NamedObjMgr
959 if ( (!omg.HasVar(vn)) ) {
960 cerr << " PIACmd::SubstituteVarError: Undefined variable "
961 << vn << " ! " << endl;
962 vv = ""; return(false);
963 }
964 vv = omg.GetVar(vn);
965}
966
967return(true);
968}
969
970/* --Methode-- */
971int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
972{
973 res = true;
974 if ((args.size() != 6) || (args[5] != "then") ||
975 (args[0] != "(") || (args[4] != ")") ) return(1);
976 if (args[2] == "==") res = (args[1] == args[3]);
977 else if (args[2] == "!=") res = (args[1] != args[3]);
978 else if (args[2] == "<")
979 res = (atof(args[1].c_str()) < atof(args[3].c_str()));
980 else if (args[2] == ">")
981 res = (atof(args[1].c_str()) > atof(args[3].c_str()));
982 else if (args[2] == "<=")
983 res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
984 else if (args[2] == ">=")
985 res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
986 else return(2);
987 return(0);
988}
989
990/* --Methode-- */
991void PIACmd::PushStack(vector<string>& args)
992{
993 // We push the argument list (args) on the stack
994 ArgsStack.push(args);
995 // We push also PIACmdBloc and testresult on the stack
996 CmdBlks.push(NULL);
997 list<char> xtx;
998 TestsStack.push(xtx);
999
1000}
1001
1002/* --Methode-- */
1003void PIACmd::PopStack(bool psta)
1004{
1005 // We remove the argument list (args) from the stack
1006 if (psta) ArgsStack.pop();
1007 // And PIACmdBloc and TestResult from the corresponding stacks
1008 PIACmdBloc* curb = CmdBlks.top();
1009 while (curb != NULL) {
1010 PIACmdBloc* parb = curb->Parent();
1011 delete curb; curb = parb;
1012 }
1013 CmdBlks.pop();
1014 TestsStack.pop();
1015}
1016
1017/* --Methode-- */
1018int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
1019{
1020int rc = 0;
1021NamedObjMgr omg;
1022
1023// >>>>>>>>>>> Commande d'interpreteur
1024if (kw == "helpwindow") ShowHelpWindow();
1025else if (kw == "help") {
1026 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
1027 else {
1028 string kwh = "piacmd";
1029 cout << GetUsage(kwh) << endl;
1030 }
1031 }
1032
1033else if (kw == "set") {
1034 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
1035 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1036 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1037 return(0);
1038 }
1039 // string xx = tokens[1];
1040 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
1041 omg.SetVar(tokens[0], tokens[1]);
1042 }
1043
1044else if (kw == "getvar") {
1045 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
1046 if (!omg.HasVar(tokens[1])) {
1047 cerr << "Error - No " << tokens[1] << " Variable " << endl;
1048 return(0);
1049 }
1050 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1051 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1052 return(0);
1053 }
1054 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
1055 }
1056
1057else if (kw == "alias") {
1058 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
1059 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1060 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
1061 return(0);
1062 }
1063 string xx = tokens[1];
1064 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
1065 mAliases[tokens[0]] = xx;
1066 }
1067
1068else if (kw == "setol") {
1069 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
1070 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1071 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1072 return(0);
1073 }
1074 vector<string> ol;
1075 mObjMgr->GetObjList(tokens[1], ol);
1076 string vol;
1077 if (ol.size() < 1) vol = "";
1078 else {
1079 vol = ol[0];
1080 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
1081 }
1082 omg.SetVar(tokens[0], vol);
1083 }
1084else if (kw == "unset") {
1085 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
1086 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
1087 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
1088 }
1089else if (kw == "echo") {
1090 for (int ii=0; ii<tokens.size(); ii++)
1091 cout << tokens[ii] << " " ;
1092 cout << endl;
1093 }
1094else if (kw == "readstdin") {
1095 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
1096 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1097 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1098 return(0);
1099 }
1100 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
1101 cout << tokens[0] << " ? " << endl;
1102 omg.SetVar(tokens[0], GetStringFrStdin(this) );
1103 }
1104
1105else if (kw == "listvars") {
1106 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
1107 DVList& varlist = omg.GetVarList();
1108 DVList::ValList::const_iterator it;
1109 string value;
1110 for(it = varlist.Begin(); it != varlist.End(); it++) {
1111#ifdef SANS_EVOLPLANCK
1112 MuTyV mtv = (*it).second;
1113 value = (string)(mtv);
1114#else
1115 value = (string)((*it).second.elval);
1116#endif
1117 cout << (*it).first << " = " << value << "\n";
1118 }
1119 cout << endl;
1120 }
1121else if (kw == "listalias") {
1122 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
1123 CmdStrList::iterator it;
1124 for(it = mAliases.begin(); it != mAliases.end(); it++)
1125 cout << (*it).first << " = " << (*it).second << "\n";
1126 cout << endl;
1127 }
1128else if (kw == "listcommands") {
1129 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
1130 CmdExmap::iterator it;
1131 int kc = 0;
1132 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
1133 cout << (*it).first << " ";
1134 kc++;
1135 if (kc >= 5) { cout << "\n"; kc = 0; }
1136 }
1137 cout << endl;
1138 }
1139else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
1140else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
1141else if (kw == "timingon") {
1142 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
1143 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
1144 }
1145else if (kw == "timingoff") {
1146 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
1147 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
1148 }
1149else if (kw == "exec") {
1150 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
1151 ExecFile(tokens[0], tokens);
1152 }
1153else if (kw == "shell") {
1154 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
1155 string cmd;
1156 for (int ii=0; ii<tokens.size(); ii++)
1157 cmd += (tokens[ii] + ' ');
1158 system(cmd.c_str());
1159 }
1160else if (kw == "cshell") {
1161 if(tokens.size()<1) {cout<<"PIACmd::Interpret() Usage: cshell cmdline"<<endl; return(0);}
1162 string cmd="";
1163 for(int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
1164 CShellExecute(cmd);
1165 }
1166
1167// Execution d'une commande enregistree
1168else rc = ExecuteCommand(kw, tokens, toks);
1169
1170if (timing) gltimer->Split();
1171return(rc);
1172}
1173
1174/* --Methode-- */
1175int PIACmd::ParseLineExecute(string& line)
1176{
1177vector<string> tokens;
1178
1179if (line.length() < 1) return(0);
1180
1181string toks,kw;
1182size_t p = line.find_first_not_of(" ");
1183line = line.substr(p);
1184p = 0;
1185size_t q = line.find_first_of(" ");
1186size_t l = line.length();
1187
1188if (q < l)
1189 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
1190else { kw = line.substr(p,l-p); toks = ""; }
1191
1192q = 0;
1193while (q < l) {
1194 p = toks.find_first_not_of(" ",q+1); // au debut d'un token
1195 if (p>=l) break;
1196 q = toks.find_first_of(" ",p); // la fin du token;
1197 string token = toks.substr(p,q-p);
1198 tokens.push_back(token);
1199 }
1200
1201return(ExecuteCommand(kw, tokens, toks));
1202}
1203
1204/* --Methode-- */
1205int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
1206{
1207 int rc = -1;
1208 CmdExmap::iterator it = cmdexmap.find(keyw);
1209 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
1210 else {
1211 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
1212 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1213 }
1214 return(rc);
1215}
1216
1217/* --Methode-- */
1218int PIACmd::ExecFile(string& file, vector<string>& args)
1219{
1220char line_buff[512];
1221FILE *fip;
1222
1223if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
1224 if (file.find('.') >= file.length()) {
1225 cout << "PIACmd::Exec(): Error opening file " << file << endl;
1226 file += ".pic";
1227 cout << " Trying file " << file << endl;
1228 fip = fopen(file.c_str(),"r");
1229 }
1230 }
1231
1232if(fip == NULL) {
1233 cerr << "PIACmd::Exec() Error opening file " << file << endl;
1234 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
1235 return(0);
1236 }
1237
1238// hist << "### Executing commands from " << file << endl;
1239
1240PushStack(args);
1241if (trace) {
1242 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
1243 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1244 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
1245 }
1246
1247bool ohv = histon;
1248histon = false;
1249while (fgets(line_buff,511,fip) != NULL)
1250 {
1251 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
1252 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1253 string line(line_buff);
1254 if (Interpret(line) == 77777) break;
1255 }
1256histon = ohv;
1257
1258// hist << "### End of Exec( " << file << " ) " << endl;
1259if (trace) {
1260 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
1261 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1262 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
1263 }
1264
1265PopStack(true);
1266
1267return(0);
1268}
1269
1270/* --Methode-- */
1271int PIACmd::CShellExecute(string cmd)
1272{
1273 if(cmd.size()<=0) return -1;
1274
1275 NamedObjMgr omg;
1276 string fname = omg.GetTmpDir(); fname += "cshell_exec_pia.csh";
1277
1278 string cmdrm = "rm -f " + fname;
1279 system(cmdrm.c_str());
1280
1281 FILE *fip = fopen(fname.c_str(),"w");
1282 if(fip==NULL) {
1283 cout << "PIACmd/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
1284 return -2;
1285 }
1286 fprintf(fip,"#!/bin/csh\n\n");
1287 fprintf(fip,"%s\n",cmd.c_str());
1288 fprintf(fip,"\nexit 0\n");
1289 fclose(fip);
1290
1291 cmd = "csh "; cmd += fname;
1292 system(cmd.c_str());
1293
1294 system(cmdrm.c_str());
1295
1296 return 0;
1297}
1298
1299static string* videstr = NULL;
1300/* --Methode-- */
1301string& PIACmd::GetUsage(const string& kw)
1302{
1303bool fndok = false;
1304CmdExmap::iterator it = cmdexmap.find(kw);
1305if (it == cmdexmap.end()) {
1306 it = helpexmap.find(kw);
1307 if (it != helpexmap.end()) fndok = true;
1308 }
1309 else fndok = true;
1310if (fndok) return( (*it).second.us );
1311// Keyword pas trouve
1312if (videstr == NULL) videstr = new string("");
1313*videstr = "Nothing known about " + kw + " ?? ";
1314return(*videstr);
1315
1316}
1317
1318/* --Methode-- */
1319void PIACmd::ShowHelpWindow()
1320{
1321helpwin->Show();
1322}
1323
1324/* --Methode-- */
1325void PIACmd::ShowCxxOptionWindow()
1326{
1327cxxoptwin->Show();
1328}
1329
1330/* --Methode-- */
1331void PIACmd::ShowCxxExecWindow()
1332{
1333cxxexwin->Show();
1334}
1335
1336/* --Methode-- */
1337void PIACmd::HelptoLaTex(string const & fname)
1338{
1339FILE *fip;
1340if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1341 cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
1342 return;
1343 }
1344
1345fputs("% ----- Liste des groupes de Help ----- \n",fip);
1346fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1347fputs("\\begin{itemize} \n",fip);
1348CmdHGroup::iterator it;
1349for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
1350 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1351 (*it).first.c_str(), (*it).first.c_str());
1352
1353fputs("\\end{itemize} \n",fip);
1354
1355fputs("\\newpage \n",fip);
1356
1357CmdExmap::iterator ite;
1358fputs("% ----- Liste de toutes les commandes ----- \n",fip);
1359fputs("\\begin{center} \n ", fip);
1360fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
1361fputs("\n \n \\vspace{5mm} \n",fip);
1362fputs("\\begin{tabular}{llllll} \n", fip);
1363int kt = 0;
1364for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1365 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1366 kt++;
1367 if (kt < 3) fputs(" & ", fip);
1368 else { fputs(" \\\\ \n", fip); kt = 0; }
1369 }
1370if (kt == 1) fputs(" & & & \\\\ \n", fip);
1371else if (kt == 2) fputs(" & \\\\ \n", fip);
1372fputs("\\end{tabular} \n", fip);
1373fputs("\\end{center} \n", fip);
1374fputs("\n \n \\vspace{1cm} \n",fip);
1375
1376fputs("\\begin{center} \n ", fip);
1377fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
1378fputs("\n \n \\vspace{5mm} \n",fip);
1379fputs("\\begin{tabular}{llllll} \n", fip);
1380kt = 0;
1381for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1382 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1383 kt++;
1384 if (kt < 3) fputs(" & ", fip);
1385 else { fputs(" \\\\ \n", fip); kt = 0; }
1386 }
1387if (kt == 1) fputs(" & & & \\\\ \n", fip);
1388else if (kt == 2) fputs(" & \\\\ \n", fip);
1389fputs("\\end{tabular} \n", fip);
1390fputs("\\end{center} \n", fip);
1391// fputs("\\newline \n",fip);
1392
1393fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
1394fputs("\\newpage \n",fip);
1395int gid;
1396for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1397 gid = (*it).second;
1398 if (gid == 0) continue;
1399 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1400 (*it).first.c_str(), (*it).first.c_str());
1401 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1402 if ((*ite).second.group != gid) continue;
1403 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1404 (*ite).first.c_str(), (*ite).first.c_str());
1405 fputs("\\begin{verbatim} \n",fip);
1406 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1407 fputs("\\end{verbatim} \n",fip);
1408 }
1409 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1410 if ((*ite).second.group != gid) continue;
1411 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1412 (*ite).first.c_str(), (*ite).first.c_str());
1413 fputs("\\begin{verbatim} \n",fip);
1414 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1415 fputs("\\end{verbatim} \n",fip);
1416 }
1417}
1418
1419fclose(fip);
1420return;
1421}
1422
1423
1424
Note: See TracBrowser for help on using the repository browser.