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

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

compil PEIDA (nbrand.h -> nbrandom.h) / Reza 9/1/2003

File size: 56.1 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"
[2307]14#ifdef SANS_EVOLPLANCK
[2308]15#include "nbrandom.h"
[2307]16#else
17#include "srandgen.h"
18#endif
[165]19
[293]20#include "pistdimgapp.h"
[165]21#include "nobjmgr.h"
[361]22#include "piafitting.h"
[463]23#include "pawexecut.h"
[1224]24#include "cxxexecutor.h"
[1251]25#include "cxxexecwin.h"
[1828]26#include "contmodex.h"
[1920]27#include "flowmodex.h"
[165]28
[293]29#include PISTDWDG_H
30#include PILIST_H
[165]31
[293]32// ------------------------------------------------------------
[349]33// Gestion d'une fenetre d'aide interactive
34// Classe PIAHelpWind
[293]35// ------------------------------------------------------------
[165]36
[293]37class PIAHelpWind : public PIWindow {
38public :
39 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
40 virtual ~PIAHelpWind();
[330]41 virtual void Show();
[293]42 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]43 inline void AddHelpGroup(const char * hgrp, int gid)
44 { hgrpom->AppendItem(hgrp, 20000+gid); }
45 inline void ClearHelpList()
46 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]47 inline void AddHelpItem(const char * hitem)
[330]48 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[293]49protected :
50 PIStdImgApp* dap;
51 PIACmd* piac;
52 int mNitem;
53 PIList* hitemlist;
[330]54 PIOptMenu* hgrpom;
[293]55 PIButton * mBut;
56 PILabel * mLab;
57 PIText* mTxt;
58};
[165]59
[293]60/* --Methode-- */
61PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
62 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
63{
64dap = par;
65piac = piacmd;
66mNitem = 0;
[330]67SetMsg(77);
[165]68
[293]69int bsx, bsy;
70int tsx, tsy;
71int spx, spy;
72PIApplicationPrefCompSize(bsx, bsy);
73spx = bsx/6; spy = bsy/6;
74tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
75SetSize(tsx,tsy);
[330]76hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
77hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
78hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
[293]79hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
80// hitemlist->SetBorderWidth(2);
[324]81mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
82// mTxt->SetMutiLineMode(true);
[293]83mTxt->SetTextEditable(false);
84mTxt->SetText("");
85mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
86mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
87mLab->SetBorderWidth(1);
88mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
89mLab->SetLabel("");
[330]90mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]91mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
92}
[165]93
94/* --Methode-- */
[293]95PIAHelpWind::~PIAHelpWind()
96{
[330]97delete hgrpom;
[293]98delete hitemlist;
99delete mTxt;
100delete mLab;
101delete mBut;
102}
103
104/* --Methode-- */
105void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
106{
107PIMessage um = UserMsg(msg);
[330]108if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]109 Hide();
110 return;
111 }
[330]112else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
113 mTxt->SetText("");
114 mLab->SetLabel("");
115 piac->UpdateHelpList(this, um-20000);
116}
117else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]118 string s = hitemlist->GetSelectionStr();
119 mTxt->SetText(piac->GetUsage(s));
120 mLab->SetLabel(s);
121 }
122}
123
[330]124/* --Methode-- */
125void PIAHelpWind::Show()
126{
127hgrpom->SetValue(20000); // Groupe All
128mTxt->SetText("");
129mLab->SetLabel("");
130piac->UpdateHelpList(this, 0);
131PIWindow::Show();
132}
[293]133
[349]134// ------------------------------------------------------------
135// Bloc de commandes (Foreach, ...)
136// Classe PIACmdBloc
137// ------------------------------------------------------------
138
139class PIACmdBloc {
140public:
[1262]141 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
142
[1562]143 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
[1262]144 ~PIACmdBloc();
[349]145 inline PIACmdBloc* Parent() { return(parent); }
[1262]146 inline bool CheckOK() { return blkok; }
[349]147 inline void AddLine(string& line)
148 { lines.push_back(line); bloclineid.push_back(lines.size()); }
[2215]149 void AddLine(string& line, string& kw);
[349]150 inline void AddBloc(PIACmdBloc* blk)
151 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
152 PIACmdBloc* Execute();
[2215]153 inline int& TestLevel() { return testlevel; }
[2236]154 inline int& LoopLevel() { return looplevel; }
155 inline bool CheckBloc()
156 { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)); }
157
[349]158protected:
159 PIACmd* piacmd;
160 PIACmdBloc* parent;
[1262]161 bool blkok; // true -> block OK
162 BType typ; // foreach , integer loop, float loop, test
[349]163 string varname;
164 vector<string> strlist;
165 vector<string> lines;
166 vector<PIACmdBloc *> blocs;
167 vector<int> bloclineid;
168 int i1,i2,di;
169 float f1,f2,df;
[2215]170 int testlevel; // niveau d'imbrication des if
[2236]171 int looplevel; // niveau d'imbrication des for/foreach
172 bool scrdef; // true -> commande defscript ds for/foreach
[349]173};
174
175/* --Methode-- */
[1562]176PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
[349]177{
178piacmd = piac;
179parent = par;
[1262]180blkok = false;
181typ = BT_None;
[349]182i1 = 0; i2 = -1; di = 1;
183f1 = 0.; f2 = -1.; df = 1.;
[2236]184testlevel = looplevel = 0;
185scrdef = false;
186
[349]187if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
[1562]188if ((kw != "foreach") && (kw != "for")) return;
189varname = args[0]; // $CHECK$ Variable name should be checked
190//if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
191if (kw == "foreach" ) { // This is a foreach bloc with string list
[2215]192 if ((args[1] != "(") || (args[args.size()-1] != ")") ) return;
193 for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
[1262]194 typ = BT_ForeachList;
[1276]195 blkok = true;
[349]196 }
197else { // This is an integer or float loop
198 size_t l = args[1].length();
199 size_t p = args[1].find(':');
200 size_t pp = args[1].find('.');
201 bool fl = (pp < l) ? true : false; // Float loop or integer loop
202 if (p >= l) return; // Syntaxe error
203 string a1 = args[1].substr(0, p);
204 string aa = args[1].substr(p+1);
205 p = aa.find(':');
206 string a2, a3;
207 bool hasa3 = false;
208 if (p < aa.length() ) {
209 a2 = aa.substr(0,p);
210 a3 = aa.substr(p+1);
211 hasa3 = true;
212 }
213 else a2 = aa;
214 if (fl) {
[1262]215 typ = BT_ForeachFloat;
[1276]216 blkok = true;
[349]217 f1 = atof(a1.c_str());
218 f2 = atof(a2.c_str());
219 if (hasa3) df = atof(a3.c_str());
220 else df = 1.;
221 }
222 else {
[1262]223 typ = BT_ForeachInt;
[1276]224 blkok = true;
[349]225 i1 = atoi(a1.c_str());
226 i2 = atoi(a2.c_str());
227 if (hasa3) di = atoi(a3.c_str());
228 else di = 1;
229 }
230 }
231}
232
233/* --Methode-- */
234PIACmdBloc::~PIACmdBloc()
235{
236for(int k=0; k<blocs.size(); k++) delete blocs[k];
237}
238
239/* --Methode-- */
[2215]240void PIACmdBloc::AddLine(string& line, string& kw)
241{
242 AddLine(line);
243 if (kw == "if") testlevel++;
244 else if (kw == "endif") testlevel--;
[2236]245 else if ((kw == "for") || (kw == "foreach")) looplevel++;
246 else if (kw == "end") looplevel--;
247 else if (kw == "defscript") scrdef = true;
[2215]248}
249
250/* --Methode-- */
[349]251PIACmdBloc* PIACmdBloc::Execute()
252{
253// cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
254// " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
255string cmd;
256int k=0;
257int kj=0;
258int kk=0;
259char buff[32];
[2215]260int rcc = 0;
261
[1262]262if (typ == BT_ForeachList) // foreach string loop
[349]263 for(k=0; k<strlist.size(); k++) {
[1565]264 cmd = "set " + varname + " '" + strlist[k] + "'";
[349]265 piacmd->Interpret(cmd);
266 for(kj=0; kj<bloclineid.size(); kj++) {
267 kk = bloclineid[kj];
[2215]268 if (kk > 0) {
269 rcc = piacmd->Interpret(lines[kk-1]);
270 if (rcc == 77766) break;
271 }
[349]272 else blocs[-kk-1]->Execute();
[2215]273 }
274 if (rcc == 77766) break;
[349]275 }
[1262]276else if (typ == BT_ForeachInt) // Integer loop
[349]277 for(int i=i1; i<i2; i+=di) {
278 k++;
279 if (++k > 9999) {
280 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
281 break;
282 }
283 sprintf(buff, " %d", i);
284 cmd = "set " + varname + buff;
285 piacmd->Interpret(cmd);
286 for(kj=0; kj<bloclineid.size(); kj++) {
287 kk = bloclineid[kj];
[2215]288 if (kk > 0) {
289 rcc = piacmd->Interpret(lines[kk-1]);
290 if (rcc == 77766) break;
291 }
[349]292 else blocs[-kk-1]->Execute();
[2215]293 }
294 if (rcc == 77766) break;
[349]295 }
[1262]296else if (typ == BT_ForeachFloat) // float loop
[349]297 for(float f=f1; f<f2; f+=df) {
298 k++;
299 if (++k > 9999) {
300 cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
301 break;
302 }
303 sprintf(buff, " %g", f);
304 cmd = "set " + varname + buff;
305 piacmd->Interpret(cmd);
306 for(kj=0; kj<bloclineid.size(); kj++) {
307 kk = bloclineid[kj];
[2215]308 if (kk > 0) {
309 rcc = piacmd->Interpret(lines[kk-1]);
310 if (rcc == 77766) break;
311 }
[349]312 else blocs[-kk-1]->Execute();
[2215]313 }
314 if (rcc == 77766) break;
[349]315 }
316
317return(parent);
318}
319
[2236]320// ---------------------------------------------------------------
321// Classe PIACmdScript
322// Definition et execution d'un script de PIACmd
323// script : Une liste de commande PIACmd - Lors de l'execution,
324// les variables-argument $# $0 $1 sont definies.
325// ---------------------------------------------------------------
326
[2214]327class PIACmdScript {
328public:
[2236]329 PIACmdScript(PIACmd* piac, string const& name, string const& comm);
[2214]330 virtual ~PIACmdScript();
[2236]331
332 void AddLine(string& line, string& kw);
333 virtual int Execute(vector<string>& args);
334
335 inline string& Name() { return mName; }
336 inline string& Comment() { return mComm; }
337 inline int& TestLevel() { return testlevel; }
338 inline int& LoopLevel() { return looplevel; }
339 inline bool CheckScript()
340 { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)&&fgok); }
341
342protected:
343 PIACmd* piacmd;
344 string mName;
345 string mComm;
346 vector<string> lines;
347 int testlevel; // niveau d'imbrication des if
348 int looplevel; // niveau d'imbrication des for/foreach
349 bool scrdef; // true -> commande defscript ds for/foreach
350 bool fgok; // Script name OK
351
[2214]352};
353
[2236]354/* --Methode-- */
355PIACmdScript::PIACmdScript(PIACmd* piac, string const& name,
356 string const& comm)
357{
358piacmd = piac;
359testlevel = looplevel = 0;
360scrdef = false;
361mName = name;
362if (!isalpha(name[0])) fgok = false;
363else fgok = true;
364mComm = comm;
365}
366
367/* --Methode-- */
368PIACmdScript::~PIACmdScript()
369{
370}
371
372/* --Methode-- */
373void PIACmdScript::AddLine(string& line, string& kw)
374{
375 if (kw == "if") testlevel++;
376 else if (kw == "endif") testlevel--;
377 else if ((kw == "for") || (kw == "foreach")) looplevel++;
378 else if (kw == "end") looplevel--;
379 else if (kw == "defscript") scrdef = true;
380 lines.push_back(line);
381}
382
383/* --Methode-- */
384int PIACmdScript::Execute(vector<string>& args)
385{
386 if (!CheckScript()) return(-1);
387 cout << " PIACmdScript::Execute() - Executing script " << Name() << endl;
388 for(int k=0; k<lines.size(); k++) {
389 if (piacmd->Interpret(lines[k]) == 77777) break;
390 }
391 return(0);
392}
393
[349]394// ------------------------------------------------------------
395// Classe PIACmd
396// ------------------------------------------------------------
397
[293]398static PIACmd* curpiacmd = NULL;
399/* --Methode-- */
[165]400PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
401{
[293]402mObjMgr = omg;
[165]403mImgApp = app;
404system("cp history.pic hisold.pic");
405hist.open("history.pic");
[349]406histon = true;
[165]407trace = false; timing = false;
408gltimer = NULL;
[349]409felevel = 0;
[2215]410
[1276]411mulinecmd = "";
412mulinefg = false;
[2274]413spromptmul = "Cmd> ";
414if (mImgApp) mImgApp->GetConsole()->SetPrompt(spromptmul);
[2236]415curscript = NULL;
[293]416
[2215]417CmdBlks.push(NULL);
418list<char> xtx;
419TestsStack.push(xtx);
420curtestresult = true;
421
[330]422cmdhgrp["All"] = 0;
423cmdgrpid = 1;
[2305]424cmdhgrp["PIACmd"] = 1;
[293]425helpwin = new PIAHelpWind(app, this);
[330]426helpwin->AddHelpGroup("All", 0);
[2305]427helpwin->AddHelpGroup("PIACmd", 1);
[293]428
[2305]429string kw = "Interpreter";
[293]430string usage;
431usage = ">>> (piacmd) Interpreter's keywords : \n";
[349]432usage += " > set varname string # To set a variable, $varname \n";
433usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
[333]434usage += " > setol varname patt # Fills varname with object list \n";
[293]435usage += " > unset varname # clear variable definition \n";
[2241]436usage += " > rpneval varname RPNExpression # Reverse Polish Notation evaluation \n";
[2305]437usage += " > varname = 'string string ...' # To set a variable, $varname \n";
438usage += " > varname = RPNExpression # RPN evaluation / result -> varname \n";
[333]439usage += " > echo string # output string \n";
[2287]440usage += " > echo2file filename string # Append the string to the specified file \n";
[353]441usage += " > alias name string # define a command alias \n";
442usage += " > readstdin varname # reads a line from stdin into $varname \n";
[2241]443usage += " > foreach varname ( string-list ) # Loop \n";
[1562]444usage += " > for varname i1:i2[:di] # Integer loop \n";
445usage += " > for varname f1:f2[:df] # Float loop \n";
[349]446usage += " > end # end loops \n";
[2241]447usage += " > if ( test ) then # Conditional test : a == != < > <= >= b \n";
[1565]448usage += " > else # Conditional \n";
449usage += " > endif # End of conditional if bloc \n";
450usage += " > break # Delete (clears) all test and loop blocs \n";
451usage += " > return # Stops command execution from a file \n";
[2236]452usage += " > defscript endscript # Command script definition \n";
[353]453usage += " > listvars # List of variable names and values \n";
454usage += " > listalias # List of alias names and values \n";
455usage += " > listcommands # List of all known commands \n";
[2236]456usage += " > listscripts # List of all known scripts \n";
457usage += " > clearcript # Clear a script definition \n";
[353]458usage += " > exec filename # Execute commands from file \n";
[293]459usage += " > help <command_name> # <command_name> usage info \n";
460usage += " > helpwindow # Displays help window \n";
[349]461usage += " > timingon timingoff traceon traceoff \n";
[2305]462string grp = "PIACmd";
[330]463RegisterHelp(kw, usage, grp);
[293]464
[2305]465kw = "RPNEvaluator";
466usage = " Reverse Polish Notation (HP calculator like) expression evaluation \n";
467usage += " >> Stack: \n";
468usage += " ... (4) (3) z=(2) y=(1) x=(0)=Stack.Top() \n";
469usage += " >> Examples: \n";
470usage += " - sin(PI/6): pi 6 / sin \n";
471usage += " - 1*2*...*5: 1 2 3 4 5 product \n";
[2307]472usage += " - x=x+y: x = $x $y * \n";
[2305]473usage += " >>> Stack operations : \n";
474usage += " print x<>y pop push (duplicate x) \n";
475usage += " >>> Constants (Cst pushed to stack): \n";
476usage += " pi e \n";
477usage += " >>> Arithmetic operators (x,y) --> x@y \n";
478usage += " + - * / % ( (int)y % (int)x )\n";
479usage += " >>> F(X): x --> F(x) \n";
480usage += " chs sqrt sq log log10 exp \n";
[2306]481usage += " fabs floor ceil \n";
[2305]482usage += " cos sin tan acos asin atan deg2rad rad2deg \n";
483usage += " >>> F(X,Y): (x,y) --> F(x,y) \n";
484usage += " pow atan2 \n";
[2307]485usage += " >>> F(): random number generators \n";
486usage += " rand (flat 0..1) norand (normal/gaussian) \n";
[2305]487usage += " >>> Stack sum/product/mean/sigma/sigma^2 \n";
488usage += " sum product mean sigma sigma2 sigmean (y->sigma x->mean) \n";
489RegisterHelp(kw, usage, grp);
490
[2307]491kw = "autoiniranf";
492usage = "> Automatic random number generator initialisation\n";
493usage += " by Auto_Ini_Ranf(int lp) \n";
494usage += " Usage: autoiniranf";
495RegisterHelp(kw, usage, grp);
496
[2203]497kw = "shell execute";
498usage = "> shell command_string # Execute shell command\n";
499usage += "> cshell command_string # Execute cshell command\n";
[2305]500usage += "---Examples:\n";
[2203]501usage += " > shell ls\n";
502usage += " > cshell echo '$LD_LIBRARY_PATH'; map2cl -h; ls\n";
503usage += " > shell myfile.csh [arg1] [arg2] [...]\n";
504usage += " (where the first line of \"myfile.csh\" is \"#!/bin/csh\")\n";
505RegisterHelp(kw, usage, grp);
506
[2307]507kw = "exitpiapp";
508usage = "To end the piapp session (Interpreter's command)";
509RegisterHelp(kw, usage, grp);
510
[293]511basexec = new PIABaseExecutor(this, omg, app);
[361]512fitexec = new PIAFitter(this, app);
[463]513pawexec = new PAWExecutor(this, app);
[1251]514CxxExecutor * cxxe = new CxxExecutor(this, app);
515cxxexec = cxxe;
[1828]516
517ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
518cntexec = cntxx; //_OP_
[1920]519FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
520flwexec = flwxx; //_OP_
[1828]521
[1251]522cxxoptwin = new CxxOptionWind(app, cxxe);
523cxxexwin = new CxxExecWind(app, cxxe);
524
[293]525AddInterpreter(this);
526curcmdi = this;
[165]527}
528
529/* --Methode-- */
530PIACmd::~PIACmd()
531{
532hist.close();
533if (gltimer) { delete gltimer; gltimer = NULL; }
[293]534Modmap::iterator it;
535for(it = modmap.begin(); it != modmap.end(); it++) {
536 string name = (*it).first + "_end";
537 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
538 if (fend) fend();
539 delete (*it).second;
540 }
[2236]541
542for(ScriptList::iterator sit = mScripts.begin();
543 sit != mScripts.end(); sit++) delete (*sit).second;
544
[293]545delete helpwin;
[1251]546delete cxxexwin;
547delete cxxoptwin;
[293]548if (curpiacmd == this) curpiacmd = NULL;
[361]549delete basexec;
550delete fitexec;
[463]551delete pawexec;
[1224]552delete cxxexec;
[165]553}
554
555/* --Methode-- */
[293]556PIACmd* PIACmd::GetInterpreter()
[165]557{
[293]558return(curpiacmd);
559}
[165]560
[293]561/* --Methode-- */
562string PIACmd::Name()
563{
564return("piacmd");
565}
[165]566
[293]567/* --Methode-- */
[330]568void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]569{
[330]570if (!ce) {
571 RegisterHelp(keyw, usage, grp);
572 return;
573 }
574int gid = CheckHelpGrp(grp);
[293]575cmdex cme;
[330]576cme.group = gid;
[293]577cme.us = usage;
578cme.cex = ce;
579cmdexmap[keyw] = cme;
580}
581
582/* --Methode-- */
[330]583void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
584{
585int gid = CheckHelpGrp(grp);
586cmdex cme;
587cme.group = gid;
588cme.us = usage;
589cme.cex = NULL;
590helpexmap[keyw] = cme;
591}
592
593/* --Methode-- */
594int PIACmd::CheckHelpGrp(string& grp)
595{
596int gid=0;
597CmdHGroup::iterator it = cmdhgrp.find(grp);
598if (it == cmdhgrp.end()) {
599 cmdgrpid++; gid = cmdgrpid;
600 cmdhgrp[grp] = gid;
601 helpwin->AddHelpGroup(grp.c_str(), gid);
602 }
603else gid = (*it).second;
604return(gid);
605}
606
607/* --Methode-- */
608void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
609{
610helpwin->ClearHelpList();
611CmdExmap::iterator it;
612for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
613 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
614 helpwin->AddHelpItem((*it).first.c_str());
615 }
616for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
617 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
618 helpwin->AddHelpItem((*it).first.c_str());
619 }
620}
621
622/* --Methode-- */
[293]623void PIACmd::LoadModule(string& fnameso, string& name)
624{
625PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
626if (dynlink == NULL) {
627 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
628 return;
[165]629 }
[293]630string fname = name + "_init";
631DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
632if (!finit) {
633 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
634 return;
[165]635 }
[293]636cout << "PIACmd/LoadModule_Info: Initialisation module" << name
637 << " " << fname << "() ..." << endl;
638finit();
639modmap[name] = dynlink;
640return;
641}
[165]642
[293]643/* --Methode-- */
644void PIACmd::AddInterpreter(CmdInterpreter * cl)
645{
646if (!cl) return;
[349]647interpmap[cl->Name()] = cl;}
[165]648
649/* --Methode-- */
[293]650void PIACmd::SelInterpreter(string& name)
[165]651{
[293]652InterpMap::iterator it = interpmap.find(name);
653if (it == interpmap.end()) return;
654curcmdi = (*it).second;
655}
[165]656
[293]657
658
[357]659/* Fonction */
[368]660static string GetStringFrStdin(PIACmd* piac)
[357]661{
[368]662PIStdImgApp* piapp = piac->GetImgApp();
663if (piapp) {
664 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
665 if (wdg) wdg->Refresh();
[357]666#ifndef __mac__
[368]667 /* On vide le buffer X-Window */
[374]668 XSync(PIXDisplay(),False);
[357]669#endif
[368]670}
[357]671char buff[128];
672fgets(buff, 128, stdin);
673buff[127] = '\0';
674return((string)buff);
675}
676
[293]677/* --Methode-- */
678int PIACmd::Interpret(string& s)
679{
[333]680int rc = 0;
[1262]681NamedObjMgr omg;
[2236]682ScriptList::iterator sit;
[165]683
[1262]684// On saute de commandes vides
685size_t l;
[349]686l = s.length();
[2285]687if (!mulinefg && (l < 1)) return(0);
[165]688
[349]689// On enregistre les commandes
690if (histon) hist << s << endl;
691
[1262]692if (s[0] == '#') return(0); // si c'est un commentaire
[349]693
[1276]694// Logique de gestion des lignes suite
695// un \ en derniere position indique la presence d'une ligne suite
[1539]696size_t lnb = s.find_last_not_of(' ');
697if (s[lnb] == '\\' ) { // Lignes suite ...
698 mulinecmd += s.substr(0,lnb);
[2285]699 if (!mulinefg) {
700 spromptmul = mImgApp->GetConsole()->GetPrompt();
701 mImgApp->GetConsole()->SetPrompt("...? ");
702 mulinefg = true;
703 }
[1276]704 return(0);
705}
[165]706
[1276]707if (mulinefg) { // Il y avait des lignes suite
708 s = mulinecmd + s;
709 mulinecmd = "";
710 mulinefg = false;
[2274]711 mImgApp->GetConsole()->SetPrompt(spromptmul);
[1276]712}
713
[1262]714// Removing leading blanks
[1276]715size_t p,q;
[1262]716
717p=s.find_first_not_of(" \t");
718if (p < l) s = s.substr(p);
719
[349]720// >>>> Substitution d'alias (1er mot)
[2214]721CmdStrList::iterator it;
[449]722p = 0;
723q = s.find_first_of(" \t");
724l = s.length();
725string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
726it = mAliases.find(w1);
727if (it != mAliases.end()) {
728 s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
729 l = s.length();
730 p=s.find_first_not_of(" \t");
731 if (p < l) s = s.substr(p);
[349]732 p = 0;
733 q = s.find_first_of(" ");
734 }
735
736// >>>> Separating keyword
[1262]737string toks,kw;
[165]738if (q < l)
739 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
740else { kw = s.substr(p,l-p); toks = ""; }
741
[1562]742// les mot-cle end else endif doivent etre le seul mot de la ligne
[2236]743if ( (kw == "end") || (kw == "else") || (kw == "endif") || (kw == "endscript") ) {
[1562]744 size_t ltk = toks.length();
745 if (toks.find_first_not_of(" \t") < ltk) {
[2236]746 cerr << "PIACmd::Interpret()/syntax error near end else endif endscript \n"
[1562]747 << "line: " << s << endl;
748 return(1);
749 }
750}
751
[2236]752// On verifie si on est en train de definir un script
753if (curscript) {
754 if (kw == "endscript") {
755 if (curscript->CheckScript()) {
756 sit = mScripts.find(curscript->Name());
757 if (sit != mScripts.end()) {
758 cout << "PIACmd::Interpret() replacing script "
759 << curscript->Name() << endl;
760 PIACmdScript* scr = mScripts[curscript->Name()];
761 mScripts.erase(sit);
762 delete scr;
763 }
764 cout << "PIACmd::Interpret() Script " << curscript->Name()
765 << " defined successfully" << endl;
766 mScripts[curscript->Name()] = curscript;
767 mImgApp->GetConsole()->SetPrompt("Cmd> ");
768 curscript = NULL;
769 return(0);
770 }
771 else {
772 cout << "PIACmd::Interpret() Error in Script " << curscript->Name()
773 << " definition " << endl;
774 mImgApp->GetConsole()->SetPrompt("Cmd> ");
775 curscript = NULL;
776 return(2);
777 }
778 }
779 else curscript->AddLine(s, kw);
780 return(0);
781}
[1562]782// On verifie si nous sommes dans un bloc (for , foreach)
[2215]783if (CmdBlks.top() != NULL) { // On est dans un bloc
[1562]784 if ( (kw == "for") || (kw == "foreach")) felevel++;
785 else if (kw == "end") felevel--;
786 if (felevel == 0) { // Il faut executer le bloc
[2215]787 PIACmdBloc* curb = CmdBlks.top();
788 CmdBlks.top() = curb->Parent();
[1562]789 mImgApp->GetConsole()->SetPrompt("Cmd> ");
[2236]790 if (!curb->CheckBloc()) {
[2215]791 cerr << "PIACmd::Interpret()/syntax error - unbalenced if ... endif"
792 << " within for/foreach bloc ! " << endl;
793 delete curb;
794 return(2);
795 }
[349]796 // cout << " *DBG* Executing bloc " << endl;
[1562]797 bool ohv = histon;
798 histon = false;
[2215]799 if (curtestresult) {
800 // We push also PIACmdBloc and testresult on the stack
801 CmdBlks.push(NULL);
802 list<char> xtx;
803 TestsStack.push(xtx);
804 curb->Execute();
805 // And PIACmdBloc and TestResult from the corresponding stacks
806 PopStack(false);
807 }
808 delete curb;
[1562]809 histon = ohv;
810 }
[2215]811 else CmdBlks.top()->AddLine(s, kw);
[1562]812 return(0);
813}
[1565]814else if (kw == "end") {
815 cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
816 << "line: " << s << endl;
817 return(1);
818}
[1562]819
820// Sommes-nous dans un bloc de test if then else
[2215]821if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if
[1565]822 if (kw == "else") {
823 if ((*tresit) & 2) {
824 cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
825 << "line: " << s << endl;
826 return(1);
827 }
828 else {
[1570]829 const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
[2215]830 if ((*tresit)&1) curtestresult = false;
[1565]831 mImgApp->GetConsole()->SetPrompt(npr);
832 (*tresit) |= 2;
833 return(0);
834 }
835 }
836 else if (kw == "endif") {
837 list<char>::iterator dbit = tresit;
[1562]838 tresit--;
[2215]839 TestsStack.top().erase(dbit);
[1570]840 const char * npr = "Cmd> ";
[2215]841 if (TestsStack.top().size() > 1) {
842 curtestresult = true;
843 list<char>::iterator it;
844 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
845 // Si on n'est pas ds le else et le if est faux
846 if ( !((*it)&2) && !((*it)&1) ) curtestresult = false;
847 // Si on est ds else et le if etait vrai !
848 if ( ((*it)&2) && ((*it)&1) ) curtestresult = false;
849 if (!curtestresult) break;
850 }
851
[1565]852 if (!((*tresit)&2))
853 npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
854 else
855 npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
856 }
[2215]857 else curtestresult = true;
[1565]858 mImgApp->GetConsole()->SetPrompt(npr);
[349]859 return(0);
860 }
[1565]861}
862else if ((kw == "else") || (kw == "endif")) {
863 cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
864 << "line: " << s << endl;
865 return(1);
[1562]866}
[349]867
[1565]868bool fgcont = true;
[2215]869if (TestsStack.top().size() > 0) { // Resultat de if ou else
[1565]870 list<char>::iterator it;
[2215]871 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
[1565]872 // Si on n'est pas ds le else et le if est faux
873 if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
874 // Si on est ds else et le if etait vrai !
875 if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
876 if (!fgcont) break;
877 }
878}
879
880if ((!fgcont) && (kw != "if")) return(0);
881
882
[2215]883// Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
884if (kw == "break") return(77766);
885else if (kw == "return") return(77777);
886
[1262]887// Nous ne sommes donc pas dans un bloc .... Substitution de variables
[1276]888string s2;
889int rcs ;
[1262]890// Execution de code C++
891
[2236]892if (kw == "@@") {
[1262]893 CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
894 if (cxxe == NULL) {
895 cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
896 return(99);
897 }
[2236]898 return(cxxe->ExecuteCXX(s.substr(2)));
[1262]899}
900
901
[1276]902rcs = SubstituteVars(s, s2);
[2305]903if (rcs) {
904 cerr << "PIACmd::Interpret()/syntax error in SubstituteVars() \n"
905 << "line: " << s << endl;
906 return(rcs);
907}
[1262]908// >>>> Separating keyword and tokens
909vector<string> tokens;
[2236]910/* decoupage en mots */
911LineToWords(s2, kw, tokens, toks, true);
[2298]912
[1562]913// Si c'est un for/foreach, on cree un nouveau bloc
914if ((kw == "foreach") || (kw == "for")) {
[1262]915 // cout << " *DBG* We got a foreach... " << endl;
[2215]916 PIACmdBloc* bloc = new PIACmdBloc(this, CmdBlks.top(), kw, tokens);
[1262]917 if (!bloc->CheckOK()) {
[1565]918 cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
[1262]919 delete bloc;
[1565]920 return(1);
[1262]921 }
922 felevel++;
[2215]923 if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
[1562]924 else mImgApp->GetConsole()->SetPrompt("for...> ");
[2215]925 CmdBlks.top() = bloc;
[1262]926 // cout << " *DBG* New Bloc created ... " << endl;
927 return(0);
928 }
[1565]929else if (kw == "if") { // Un test if
930 bool restst = true;
931 int rct = EvaluateTest(tokens, s, restst);
932 if (rct) {
[2305]933 cerr << "PIACmd::Interpret() if syntax Error ! " << "line: " << s << endl;
[1565]934 return(1);
935 }
936 char res_tst = (restst) ? 1 : 0;
[2215]937 TestsStack.top().push_back(res_tst);
938 if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin();
[1562]939 else tresit++;
[1570]940 const char * npr = (restst) ? "if-T> " : "if-F> ";
[1565]941 mImgApp->GetConsole()->SetPrompt(npr);
[1562]942}
[2241]943else if ((tokens.size() > 0) && (tokens[0] == "=")) {
944 // x = RPNExpression (Evaluation d'expression RPN)
945 tokens[0] = kw;
946 int rcev = EvalRPNExpr(tokens, s);
[2236]947 if (rcev) {
[2305]948 cerr << "PIACmd::Interpret() evaluation (RPN) syntax Error ! " << "line: " << s << endl;
[2236]949 return(1);
950 }
951 return(0);
952}
953else if (kw == "defscript") { // definition de script
[2275]954 if (tokens.size() > 0) {
955 if (tokens.size() < 2) tokens.push_back("");
956 curscript = new PIACmdScript(this, tokens[0], tokens[1]);
957 mImgApp->GetConsole()->SetPrompt("Script...> ");
958 return(0);
959 }
960 else {
[2305]961 cerr << "PIACmd::Interpret() No script name in defscript" << "line: " << s << endl;
[2275]962 return(1);
963 }
[2236]964}
965else {
966 // Si c'est le nom d'un script
967 sit = mScripts.find(kw);
968 if (sit != mScripts.end()) {
969 bool ohv = histon;
970 histon = false;
[2298]971 tokens.insert(tokens.begin(), kw);
[2236]972 PushStack(tokens);
973 (*sit).second->Execute(tokens);
974 PopStack(true);
975 histon = ohv;
976 }
977 // Execution de commandes
978 else rc = ExecuteCommandLine(kw, tokens, toks);
979 return(rc);
980}
[1262]981// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
982// for(int ii=0; ii<tokens.size(); ii++)
983// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
984
[2241]985return(0);
[1262]986}
987
[2236]988
[1262]989/* --Methode-- */
[2236]990int PIACmd::LineToWords(string& line, string& kw, vector<string>& tokens,
991 string& toks, bool uq)
992{
993if (line.length() < 1) return(0);
994int nw = 1;
995size_t p = line.find_first_not_of(" ");
996line = line.substr(p);
997p = 0;
998size_t q = line.find_first_of(" ");
999size_t l = line.length();
1000
1001if (q < l)
1002 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
1003else { kw = line.substr(p,l-p); toks = ""; }
1004
1005q = 0;
1006while (q < l) {
1007 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
1008 if (p>=l) break;
1009 if ( uq && ((toks[p] == '\'') || (toks[p] == '"')) ) {
1010 q = toks.find(toks[p],p+1);
1011 if (q>=l) {
1012 cerr << "PIACmd::LineToWords/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
1013 return(-1);
1014 }
1015 p++;
1016 }
1017 else {
1018 q = toks.find_first_of(" \t",p); // la fin du token;
1019 }
1020 string token = toks.substr(p,q-p);
1021 tokens.push_back(token); nw++;
1022 }
1023
1024return(nw);
1025}
1026
1027/* --Methode-- */
[1262]1028int PIACmd::SubstituteVars(string & s, string & s2)
1029// Variable substitution
1030{
[2214]1031NamedObjMgr& omg = *mObjMgr;
[1262]1032
[2215]1033int iarr = -1; // index d'element de tableau
1034size_t p,q,q2,q3,l;
[1262]1035
1036s2="";
[349]1037p = 0;
1038l = s.length();
[2214]1039string vn, vv;
[349]1040while (p < l) {
[2305]1041 iarr = -1;
[349]1042 q = s.find('$',p);
[1276]1043 if (q > l) break;
1044 q2 = s.find('\'',p);
1045 if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
1046 q2 = s.find('\'',q2+1);
1047 if (q2 >= l) {
1048 cerr << " Syntax error - Unbalenced quotes !!! " << endl;
1049 return(1);
1050 }
1051 s2 += s.substr(p, q2-p+1);
1052 p = q2+1; continue;
1053 }
[349]1054 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
1055 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
1056 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
1057 continue;
1058 }
1059 if (q >= l-1) {
[1276]1060 cerr << " Syntax error - line ending with $ !!! " << endl;
1061 return(2);
[349]1062 }
1063 vn = "";
1064 if ( s[q+1] == '{' ) { // Variable in the form ${name}
1065 q2 = s.find('}',q+1);
1066 if (q2 >= l) {
[1276]1067 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
1068 return(3);
[349]1069 }
1070 vn = s.substr(q+2,q2-q-2);
1071 q2++;
[2215]1072 }
1073 else if ( s[q+1] == '(' ) { // Variable in the form $(name)
1074 q2 = s.find(')',q+1);
1075 if (q2 >= l) {
1076 cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl;
1077 return(3);
1078 }
1079 vn = s.substr(q+2,q2-q-2);
1080 q2++;
1081 }
[349]1082 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
1083 q2 = s.find(']',q+1);
1084 if (q2 >= l) {
[1276]1085 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
1086 return(4);
[349]1087 }
1088 vn = s.substr(q+2,q2-q-2);
[2214]1089 if (!GetVar(vn, vv)) return(5);
1090 vn = vv;
[349]1091 q2++;
1092 }
1093 else {
[1562]1094 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
[349]1095 if (q2 > l) q2 = l;
[2215]1096 q3 = q2;
[349]1097 vn = s.substr(q+1, q2-q-1);
[2215]1098 // Si variable de type $varname[index] : element de tableau
1099 if ((q2 < l) && (s[q2] == '[') ) {
1100 q3 = s.find_first_of("]",q2+1);
1101 string sia = s.substr(q2+1, q3-q2-1);
[2218]1102 if (sia.length() < 1) {
1103 cerr << " Syntax error - in $varname[index] : $"
1104 << vn << "[" << sia <<"]" << endl;
1105 return(4);
1106 }
1107 if (isalpha(sia[0])) {
1108 string sia2;
1109 if (!GetVar(sia, sia2) || (sia2.length() < 1)) {
1110 cerr << " Syntax error - in $varname[index] : $"
1111 << vn << "[" << sia <<"]" << endl;
1112 return(4);
1113 }
1114 sia = sia2;
1115 }
[2215]1116 int rcdia = ctoi(sia.c_str(), &iarr);
1117 if (rcdia < 0) {
1118 cerr << " Syntax error - in $varname[iarr] : $"
1119 << vn << "[" << sia <<"]" << endl;
1120 return(4);
1121 }
[349]1122 }
[2215]1123 }
[2214]1124 if (!GetVar(vn, vv)) return(5);
[2215]1125 if (iarr < 0) {
1126 s2 += (s.substr(p, q-p) + vv);
1127 p = q2;
1128 }
1129 else {
1130 vector<string> vs;
1131 FillVStringFrString(vv, vs);
1132 if (iarr >= vs.size()) {
1133 cerr << " Substitution error - word index out of range in "
1134 << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
1135 return(4);
1136 }
1137 else s2 += (s.substr(p, q-p) + vs[iarr]);
1138 p = q3+1;
1139 }
1140}
[349]1141if (p < l) s2 += s.substr(p);
1142
1143p = s2.find_first_not_of(" \t");
1144if (p < l) s2 = s2.substr(p);
1145
[1262]1146return(0);
1147}
[349]1148
[1262]1149/* --Methode-- */
[2214]1150bool PIACmd::GetVar(string & vn, string & vv)
1151{
1152NamedObjMgr& omg = *mObjMgr;
1153if (vn.length() < 1) {
1154 cerr << " PIACmd::SubstituteVar/Error: length(varname=" << vn << ")<1 !" << endl;
1155 vv = ""; return(false);
1156}
1157// Variable de type $# $0 $1 ... (argument de .pic ou de script)
1158int ka = 0;
1159if (vn == "#") {
1160 if (ArgsStack.empty()) {
1161 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
1162 << " ($" << vn << ")" << endl;
[2218]1163 vv = ""; return(false);
[2214]1164 }
1165 char buff[32];
1166 long an = ArgsStack.top().size();
1167 sprintf(buff,"%ld", an);
[2218]1168 vv = buff; return(true);
[2214]1169}
1170else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
1171 if (ArgsStack.empty()) {
1172 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
1173 << " ($" << vn << ")" << endl;
1174 vv = ""; return(false);
1175 }
1176 if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
1177 cerr << " PIACmd::SubstituteVar/Error: Undefined variable ! "
1178 << " ($" << vn << ")" << endl;
1179 vv = ""; return(false);
1180 }
[2218]1181 vv = ArgsStack.top()[ka]; return(true);
[2214]1182}
[2218]1183else if (vn[0] == '#') { // Variable de type $#vname --> size(vname)
1184 vn = vn.substr(1);
1185 if (!omg.HasVar(vn) ) {
1186 cerr << " PIACmd::SubstituteVarError: Undefined variable "
1187 << vn << " ! " << endl;
1188 vv = ""; return(false);
1189 }
1190 vn = omg.GetVar(vn);
1191 vector<string> vs;
1192 FillVStringFrString(vn, vs);
1193 char buff[32];
1194 sprintf(buff,"%d", (int)vs.size());
1195 vv = buff; return(true);
1196 }
[2214]1197else { // variable ordinaire geree par NamedObjMgr
1198 if ( (!omg.HasVar(vn)) ) {
1199 cerr << " PIACmd::SubstituteVarError: Undefined variable "
1200 << vn << " ! " << endl;
[2218]1201 vv = ""; return(false);
[2214]1202 }
[2218]1203 vv = omg.GetVar(vn); return(true);
[2214]1204}
1205
[2218]1206return(false);
[2214]1207}
1208
1209/* --Methode-- */
[1565]1210int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
[1562]1211{
[1565]1212 res = true;
[2215]1213 if ((args.size() != 6) || (args[5] != "then") ||
1214 (args[0] != "(") || (args[4] != ")") ) return(1);
1215 if (args[2] == "==") res = (args[1] == args[3]);
1216 else if (args[2] == "!=") res = (args[1] != args[3]);
1217 else if (args[2] == "<")
1218 res = (atof(args[1].c_str()) < atof(args[3].c_str()));
1219 else if (args[2] == ">")
1220 res = (atof(args[1].c_str()) > atof(args[3].c_str()));
1221 else if (args[2] == "<=")
1222 res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
1223 else if (args[2] == ">=")
1224 res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
[1565]1225 else return(2);
1226 return(0);
[1562]1227}
1228
[2241]1229/* Operations sur le stack RPN */
1230inline bool Check_myRPNStack_(stack<double>& s, double& x, string& line)
1231{
1232 if (s.empty()) {
1233 cerr << "PIACmd::EvalRPNExpr: syntax error / empty RPN stack " << line << endl;
1234 return true;
1235 }
1236 else x = s.top();
1237 return false;
1238}
1239inline bool Check_myRPNStack_(stack<double>& s, double& x, double& y, string& line)
1240{
1241 if (s.size() < 2) {
1242 cerr << "PIACmd::EvalRPNExpr: syntax error / RPN stack size < 2 " << line << endl;
1243 return true;
1244 }
1245 else {
1246 x = s.top(); s.pop(); y = s.top();
1247 }
1248 return false;
1249}
1250
1251inline void Print_myRPNStack_(stack<double> s)
1252{
1253 if (s.empty())
1254 cout << "PIACmd::EvalRPNExpr/PrintStack: Empty stack " << endl;
1255 else {
1256 int k = 0;
1257 cout << "PIACmd::EvalRPNExpr/PrintStack: Size()= " << s.size() << endl;
1258 while( !s.empty() ) {
1259 cout << " " << k << ": " << s.top() << " ";
1260 if (k == 0) cout << " (x) " << endl;
1261 else if (k == 1) cout << " (y) " << endl;
1262 else if (k == 2) cout << " (z) " << endl;
1263 else cout << endl;
1264 s.pop(); k++;
1265 }
1266 }
[2236]1267
[2241]1268}
1269
[2265]1270int Sum_RPNStack_(stack<double>& s, double& sx, double& sx2, string& line)
1271{
1272 sx = sx2 = 0.;
1273 int nn = 0;
1274 double x = 0.;
1275 while( !s.empty() ) {
1276 x = s.top(); s.pop();
1277 sx += x; sx2 += x*x;
1278 nn++;
1279 }
1280 return(nn);
1281}
1282
1283int Product_RPNStack_(stack<double>& s, double& px, string& line)
1284{
1285 px = 1.;
1286 int nn = 0;
1287 double x = 0.;
1288 while( !s.empty() ) {
1289 x = s.top(); s.pop();
1290 px *= x; nn++;
1291 }
1292 return(nn);
1293}
[2241]1294
[1562]1295/* --Methode-- */
[2241]1296int PIACmd::EvalRPNExpr(vector<string> & args, string & line)
[2236]1297{
[2241]1298 NamedObjMgr& omg = *mObjMgr;
1299
1300 if (args.size() < 2) {
1301 cerr << "PIACmd::EvalRPNExpr: syntax error / missing arguments " << line << endl;
[2236]1302 return(1);
1303 }
[2241]1304 else if (args.size() == 2) {
1305 omg.SetVar(args[0], args[1]);
1306 return(0);
1307 }
[2236]1308
[2241]1309 double x,y;
1310 x = y = 0.;
1311 stack<double> rpnstack; // Stack des operations en RPN
1312 for(int k=1; k<args.size(); k++) {
1313 // Les 4 operations de base + - * /
[2236]1314 if (args[k] == "+") {
[2241]1315 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1316 rpnstack.top() = y+x;
[2236]1317 }
1318 else if (args[k] == "-") {
[2241]1319 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1320 rpnstack.top() = y-x;
[2236]1321 }
1322 else if (args[k] == "*") {
[2241]1323 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1324 rpnstack.top() = y*x;
[2236]1325 }
1326 else if (args[k] == "/") {
[2241]1327 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1328 rpnstack.top() = y/x;
[2236]1329 }
[2305]1330 else if (args[k] == "%") {
1331 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1332 rpnstack.top() = (int)y % (int)x;
1333 }
[2241]1334 // Les constantes : e , pi
1335 else if (args[k] == "e") {
1336 rpnstack.push(M_E);
1337 }
1338 else if (args[k] == "pi") {
1339 rpnstack.push(M_PI);
1340 }
1341 // Les fonctions usuelles a 1 argument f(x)
1342 else if (args[k] == "cos") {
1343 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1344 rpnstack.top() = cos(x);
1345 }
1346 else if (args[k] == "sin") {
1347 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1348 rpnstack.top() = sin(x);
1349 }
1350 else if (args[k] == "tan") {
1351 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1352 rpnstack.top() = tan(x);
1353 }
1354 else if (args[k] == "acos") {
1355 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1356 rpnstack.top() = acos(x);
1357 }
1358 else if (args[k] == "asin") {
1359 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1360 rpnstack.top() = asin(x);
1361 }
1362 else if (args[k] == "atan") {
1363 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1364 rpnstack.top() = atan(x);
1365 }
[2305]1366 else if (args[k] == "chs") {
1367 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1368 rpnstack.top() = -x;
1369 }
1370 else if (args[k] == "sqrt") {
1371 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1372 rpnstack.top() = sqrt(x);
1373 }
1374 else if (args[k] == "sq") { // x^2
1375 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1376 rpnstack.top() = x*x;
1377 }
[2241]1378 else if (args[k] == "log") {
1379 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1380 rpnstack.top() = log(x);
1381 }
1382 else if (args[k] == "log10") {
1383 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1384 rpnstack.top() = log10(x);
1385 }
1386 else if (args[k] == "exp") {
1387 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1388 rpnstack.top() = exp(x);
1389 }
[2294]1390 else if (args[k] == "fabs") {
1391 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1392 rpnstack.top() = fabs(x);
1393 }
[2305]1394 else if (args[k] == "floor") {
[2297]1395 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
[2305]1396 rpnstack.top() = floor(x);
[2297]1397 }
[2305]1398 else if (args[k] == "ceil") {
1399 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1400 rpnstack.top() = ceil(x);
1401 }
[2306]1402 // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003
[2241]1403 else if (args[k] == "deg2rad") {
1404 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1405 rpnstack.top() = x*M_PI/180.;
1406 }
1407 else if (args[k] == "rad2deg") {
1408 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1409 rpnstack.top() = x*180./M_PI;
1410 }
1411 // Les fonctions usuelles a 2 argument f(x,y)
1412 else if (args[k] == "pow") {
1413 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1414 rpnstack.top() = pow(y,x);
1415 }
1416 else if (args[k] == "atan2") {
1417 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1418 rpnstack.top() = atan2(x,y);
1419 }
[2307]1420 // generateur aleatoire
1421 else if (args[k] == "rand") {
1422 double rnd = drand01();
1423 rpnstack.push(rnd);
1424 }
1425 else if (args[k] == "norand") {
1426 double rnd = GauRnd(0., 1.);
1427 rpnstack.push(rnd);
1428 }
[2265]1429 // Fonction a N arguments - Somme, produit, etc ...
[2305]1430 else if ((args[k] == "sum") || (args[k] == "mean") || (args[k] == "sigmean") ||
[2265]1431 (args[k] == "sigma") || (args[k] == "sigma2") ) {
1432 double sx, sx2;
1433 int nn = Sum_RPNStack_(rpnstack, sx, sx2, line);
1434 if (args[k] == "sum") rpnstack.push(sx);
1435 else {
1436 if (nn == 0) {
1437 cerr << "PIACmd::EvalRPNExpr: mean/sigma error- RPN stack empty: "
1438 << line << endl;
1439 return(1);
1440 }
1441 double fnn = nn;
[2305]1442 if ((args[k] == "sigma") || (args[k] == "sigmean"))
1443 rpnstack.push(sqrt(sx2/fnn-(x*x/(fnn*fnn))));
1444 else if ((args[k] == "mean") || (args[k] == "sigmean")) rpnstack.push(sx/fnn);
1445 else rpnstack.push(sx2/fnn-(x*x/(fnn*fnn)));
[2265]1446 }
1447 }
1448 else if (args[k] == "product") {
1449 double px;
1450 int nn = Product_RPNStack_(rpnstack, px, line);
1451 if (nn == 0) {
1452 cerr << "PIACmd::EvalRPNExpr: product error- RPN stack empty: "
1453 << line << endl;
1454 return(1);
1455 }
1456 rpnstack.push(px);
1457 }
[2241]1458 // Fonctions de manipulation de stack
1459 else if (args[k] == "print") {
1460 Print_myRPNStack_(rpnstack);
1461 }
1462 else if (args[k] == "x<>y") {
1463 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1464 rpnstack.top() = x; rpnstack.push(y);
1465 }
[2305]1466 else if (args[k] == "pop") {
1467 rpnstack.pop();
1468 }
1469 else if (args[k] == "push") {
1470 if (rpnstack.empty()) rpnstack.push(0.);
1471 else rpnstack.push(rpnstack.top());
1472 }
[2241]1473 // On met un nombre sur le stack
1474 else {
[2294]1475 char * esptr;
1476 x = strtod(args[k].c_str(), &esptr);
1477 // if (ctof(args[k].c_str(),&x) < 0) {
1478 if (esptr == args[k].c_str()) {
[2241]1479 cerr << "PIACmd::EvalRPNExpr: syntax error near " << args[k]
1480 << " in expression: \n" << line << endl;
1481 return(2);
1482 }
1483 rpnstack.push(x);
1484 }
1485
[2236]1486 }
1487
[2241]1488 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
[2236]1489 char buff[64];
1490 sprintf(buff, "%g", x);
[2241]1491 string res = buff;
1492 omg.SetVar(args[0], res);
[2236]1493 return(0);
1494}
1495
1496/* --Methode-- */
[2215]1497void PIACmd::PushStack(vector<string>& args)
1498{
1499 // We push the argument list (args) on the stack
1500 ArgsStack.push(args);
1501 // We push also PIACmdBloc and testresult on the stack
1502 CmdBlks.push(NULL);
1503 list<char> xtx;
1504 TestsStack.push(xtx);
1505
1506}
1507
1508/* --Methode-- */
1509void PIACmd::PopStack(bool psta)
1510{
1511 // We remove the argument list (args) from the stack
1512 if (psta) ArgsStack.pop();
1513 // And PIACmdBloc and TestResult from the corresponding stacks
1514 PIACmdBloc* curb = CmdBlks.top();
1515 while (curb != NULL) {
1516 PIACmdBloc* parb = curb->Parent();
1517 delete curb; curb = parb;
1518 }
1519 CmdBlks.pop();
1520 TestsStack.pop();
1521}
1522
1523/* --Methode-- */
[1262]1524int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
1525{
1526int rc = 0;
1527NamedObjMgr omg;
[349]1528
[165]1529// >>>>>>>>>>> Commande d'interpreteur
[1262]1530if (kw == "helpwindow") ShowHelpWindow();
[293]1531else if (kw == "help") {
[330]1532 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]1533 else {
[330]1534 string kwh = "piacmd";
1535 cout << GetUsage(kwh) << endl;
[293]1536 }
[165]1537 }
1538
1539else if (kw == "set") {
[293]1540 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[349]1541 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1542 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1543 return(0);
1544 }
[1276]1545 // string xx = tokens[1];
1546 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
1547 omg.SetVar(tokens[0], tokens[1]);
[165]1548 }
[349]1549
1550else if (kw == "getvar") {
1551 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
[1262]1552 if (!omg.HasVar(tokens[1])) {
[349]1553 cerr << "Error - No " << tokens[1] << " Variable " << endl;
1554 return(0);
1555 }
1556 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1557 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1558 return(0);
1559 }
[1262]1560 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
[349]1561 }
1562
1563else if (kw == "alias") {
1564 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
1565 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1566 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
1567 return(0);
1568 }
1569 string xx = tokens[1];
1570 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
1571 mAliases[tokens[0]] = xx;
1572 }
1573
[333]1574else if (kw == "setol") {
1575 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
[349]1576 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1577 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1578 return(0);
1579 }
[333]1580 vector<string> ol;
[349]1581 mObjMgr->GetObjList(tokens[1], ol);
1582 string vol;
1583 if (ol.size() < 1) vol = "";
1584 else {
1585 vol = ol[0];
1586 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
1587 }
[1262]1588 omg.SetVar(tokens[0], vol);
[333]1589 }
[293]1590else if (kw == "unset") {
1591 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[1262]1592 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
[293]1593 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]1594 }
[2241]1595 else if (kw == "rpneval") { // Evaluation d'expression en notation polonaise inverse
1596 return(EvalRPNExpr(tokens, toks));
1597}
[333]1598else if (kw == "echo") {
[1276]1599 for (int ii=0; ii<tokens.size(); ii++)
1600 cout << tokens[ii] << " " ;
[333]1601 cout << endl;
[1276]1602 }
[2287]1603else if (kw == "echo2file") {
1604 if (tokens.size() < 1) {
1605 cout << "PIACmd::Interpret() Usage: echo2file filename [string ] " << endl;
1606 return(0);
1607 }
1608 ofstream ofs(tokens[0].c_str(), ios::app);
1609 for (int ii=1; ii<tokens.size(); ii++)
1610 ofs << tokens[ii] << " " ;
1611 ofs << endl;
1612 }
[353]1613else if (kw == "readstdin") {
1614 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
1615 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1616 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1617 return(0);
1618 }
[357]1619 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
1620 cout << tokens[0] << " ? " << endl;
[1262]1621 omg.SetVar(tokens[0], GetStringFrStdin(this) );
[353]1622 }
1623
[165]1624else if (kw == "listvars") {
[293]1625 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[1262]1626 DVList& varlist = omg.GetVarList();
1627 DVList::ValList::const_iterator it;
1628 string value;
1629 for(it = varlist.Begin(); it != varlist.End(); it++) {
[1291]1630#ifdef SANS_EVOLPLANCK
1631 MuTyV mtv = (*it).second;
1632 value = (string)(mtv);
1633#else
[1262]1634 value = (string)((*it).second.elval);
[1291]1635#endif
[1262]1636 cout << (*it).first << " = " << value << "\n";
1637 }
[165]1638 cout << endl;
1639 }
[353]1640else if (kw == "listalias") {
1641 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
[2214]1642 CmdStrList::iterator it;
[353]1643 for(it = mAliases.begin(); it != mAliases.end(); it++)
1644 cout << (*it).first << " = " << (*it).second << "\n";
1645 cout << endl;
1646 }
1647else if (kw == "listcommands") {
[2236]1648 cout << "---- PIACmd::Interpret() Command List ----- \n";
[293]1649 CmdExmap::iterator it;
1650 int kc = 0;
1651 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
1652 cout << (*it).first << " ";
1653 kc++;
1654 if (kc >= 5) { cout << "\n"; kc = 0; }
1655 }
1656 cout << endl;
1657 }
[2236]1658else if (kw == "listscripts") {
1659 cout << "---- PIACmd::Interpret() Script List ----- \n";
1660 for(ScriptList::iterator sit = mScripts.begin();
1661 sit != mScripts.end(); sit++)
1662 cout << " Script: " << (*sit).second->Name() << " - "
1663 << (*sit).second->Comment() << endl;
1664}
1665else if (kw == "clearscript") {
1666 if (tokens.size() < 1) {
1667 cout << "PIACmd::Interpret() Usage: clearscript scriptname" << endl;
1668 return(0);
1669 }
1670 ScriptList::iterator sit = mScripts.find(tokens[0]);
1671 if (sit == mScripts.end()) {
1672 cout << "PIACmd::Interpret() No script with name" << tokens[0] << endl;
1673 return(0);
1674 }
1675 else {
1676 delete (*sit).second;
1677 mScripts.erase(sit);
1678 cout << "PIACmd::Interpret() script " << tokens[0] << " cleared" << endl;
1679 return(0);
1680 }
1681}
[293]1682else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
1683else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
[165]1684else if (kw == "timingon") {
[293]1685 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
1686 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
1687 }
[165]1688else if (kw == "timingoff") {
[293]1689 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
1690 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
1691 }
[165]1692else if (kw == "exec") {
[293]1693 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
[349]1694 ExecFile(tokens[0], tokens);
[165]1695 }
[2307]1696else if (kw == "autoiniranf") {
1697 Auto_Ini_Ranf(1);
1698 return(0);
1699}
1700// ----> Sortie d'application
1701else if (kw == "exitpiapp") {
1702 mImgApp->Stop();
1703 return(0);
1704}
[165]1705else if (kw == "shell") {
[293]1706 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[1276]1707 string cmd;
1708 for (int ii=0; ii<tokens.size(); ii++)
1709 cmd += (tokens[ii] + ' ');
1710 system(cmd.c_str());
[165]1711 }
[2203]1712else if (kw == "cshell") {
1713 if(tokens.size()<1) {cout<<"PIACmd::Interpret() Usage: cshell cmdline"<<endl; return(0);}
1714 string cmd="";
1715 for(int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
1716 CShellExecute(cmd);
1717 }
[1276]1718
[293]1719// Execution d'une commande enregistree
[1268]1720else rc = ExecuteCommand(kw, tokens, toks);
[165]1721
1722if (timing) gltimer->Split();
[333]1723return(rc);
[165]1724}
1725
[333]1726/* --Methode-- */
[2236]1727int PIACmd::ParseLineExecute(string& line, bool qw)
1728 // Si qw == true, on decoupe entre '' ou "" ou espaces
[333]1729{
[1262]1730vector<string> tokens;
[2236]1731string kw, toks;
[333]1732if (line.length() < 1) return(0);
[2236]1733LineToWords(line, kw, tokens, toks, qw);
[1268]1734return(ExecuteCommand(kw, tokens, toks));
[333]1735}
1736
[165]1737/* --Methode-- */
[1268]1738int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
[333]1739{
1740 int rc = -1;
1741 CmdExmap::iterator it = cmdexmap.find(keyw);
1742 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
1743 else {
[1268]1744 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
[333]1745 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1746 }
1747 return(rc);
1748}
1749
1750/* --Methode-- */
[349]1751int PIACmd::ExecFile(string& file, vector<string>& args)
[165]1752{
[293]1753char line_buff[512];
1754FILE *fip;
[165]1755
[293]1756if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
[384]1757 if (file.find('.') >= file.length()) {
1758 cout << "PIACmd::Exec(): Error opening file " << file << endl;
1759 file += ".pic";
1760 cout << " Trying file " << file << endl;
1761 fip = fopen(file.c_str(),"r");
1762 }
1763 }
1764
1765if(fip == NULL) {
[293]1766 cerr << "PIACmd::Exec() Error opening file " << file << endl;
1767 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
1768 return(0);
[165]1769 }
[349]1770
1771// hist << "### Executing commands from " << file << endl;
1772
[2215]1773PushStack(args);
[293]1774if (trace) {
1775 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
1776 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1777 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
1778 }
[165]1779
[1562]1780bool ohv = histon;
[349]1781histon = false;
[293]1782while (fgets(line_buff,511,fip) != NULL)
1783 {
1784 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
1785 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1786 string line(line_buff);
[1565]1787 if (Interpret(line) == 77777) break;
[165]1788 }
[1562]1789histon = ohv;
[349]1790
1791// hist << "### End of Exec( " << file << " ) " << endl;
[293]1792if (trace) {
1793 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
1794 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1795 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
[165]1796 }
1797
[2215]1798PopStack(true);
[2214]1799
[165]1800return(0);
1801}
1802
[2203]1803/* --Methode-- */
1804int PIACmd::CShellExecute(string cmd)
1805{
1806 if(cmd.size()<=0) return -1;
[293]1807
[2203]1808 NamedObjMgr omg;
1809 string fname = omg.GetTmpDir(); fname += "cshell_exec_pia.csh";
1810
1811 string cmdrm = "rm -f " + fname;
1812 system(cmdrm.c_str());
1813
1814 FILE *fip = fopen(fname.c_str(),"w");
1815 if(fip==NULL) {
1816 cout << "PIACmd/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
1817 return -2;
1818 }
1819 fprintf(fip,"#!/bin/csh\n\n");
1820 fprintf(fip,"%s\n",cmd.c_str());
1821 fprintf(fip,"\nexit 0\n");
1822 fclose(fip);
1823
1824 cmd = "csh "; cmd += fname;
1825 system(cmd.c_str());
1826
1827 system(cmdrm.c_str());
1828
1829 return 0;
1830}
1831
[293]1832static string* videstr = NULL;
1833/* --Methode-- */
1834string& PIACmd::GetUsage(const string& kw)
[165]1835{
[330]1836bool fndok = false;
[293]1837CmdExmap::iterator it = cmdexmap.find(kw);
[330]1838if (it == cmdexmap.end()) {
1839 it = helpexmap.find(kw);
1840 if (it != helpexmap.end()) fndok = true;
[165]1841 }
[330]1842 else fndok = true;
1843if (fndok) return( (*it).second.us );
1844// Keyword pas trouve
1845if (videstr == NULL) videstr = new string("");
1846*videstr = "Nothing known about " + kw + " ?? ";
1847return(*videstr);
1848
[165]1849}
1850
[293]1851/* --Methode-- */
1852void PIACmd::ShowHelpWindow()
[165]1853{
[293]1854helpwin->Show();
[165]1855}
[357]1856
[463]1857/* --Methode-- */
[1251]1858void PIACmd::ShowCxxOptionWindow()
1859{
1860cxxoptwin->Show();
1861}
1862
1863/* --Methode-- */
1864void PIACmd::ShowCxxExecWindow()
1865{
1866cxxexwin->Show();
1867}
1868
[2263]1869/* Les definitions suivantes doivent se trouver ds l'en-tete du fichier LaTeX
1870 \newcommand{\piacommand}[1]{
1871 \framebox{\bf \Large #1 } \index{#1} % (Command)
1872 }
1873
1874 \newcommand{\piahelpitem}[1]{
1875 \framebox{\bf \Large #1 } \index{#1} (Help item)
1876 }
1877
1878 \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) }
1879*/
1880
[1251]1881/* --Methode-- */
[2263]1882void PIACmd::HelptoLaTeX(string const & fname)
[463]1883{
1884FILE *fip;
1885if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1886 cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
1887 return;
1888 }
[357]1889
[463]1890fputs("% ----- Liste des groupes de Help ----- \n",fip);
1891fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1892fputs("\\begin{itemize} \n",fip);
1893CmdHGroup::iterator it;
[2263]1894for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1895 if ((*it).first == "All") continue;
[484]1896 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1897 (*it).first.c_str(), (*it).first.c_str());
[2263]1898}
[463]1899
1900fputs("\\end{itemize} \n",fip);
1901
[2263]1902fputs("\\vspace*{10mm} \n",fip);
[484]1903
1904CmdExmap::iterator ite;
1905fputs("% ----- Liste de toutes les commandes ----- \n",fip);
1906fputs("\\begin{center} \n ", fip);
1907fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
1908fputs("\n \n \\vspace{5mm} \n",fip);
1909fputs("\\begin{tabular}{llllll} \n", fip);
1910int kt = 0;
1911for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1912 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1913 kt++;
1914 if (kt < 3) fputs(" & ", fip);
1915 else { fputs(" \\\\ \n", fip); kt = 0; }
1916 }
1917if (kt == 1) fputs(" & & & \\\\ \n", fip);
1918else if (kt == 2) fputs(" & \\\\ \n", fip);
1919fputs("\\end{tabular} \n", fip);
1920fputs("\\end{center} \n", fip);
[2263]1921fputs("\n \n \\newpage \\vspace{1cm} \n",fip);
[484]1922
1923fputs("\\begin{center} \n ", fip);
1924fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
1925fputs("\n \n \\vspace{5mm} \n",fip);
1926fputs("\\begin{tabular}{llllll} \n", fip);
1927kt = 0;
1928for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1929 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1930 kt++;
1931 if (kt < 3) fputs(" & ", fip);
1932 else { fputs(" \\\\ \n", fip); kt = 0; }
1933 }
1934if (kt == 1) fputs(" & & & \\\\ \n", fip);
1935else if (kt == 2) fputs(" & \\\\ \n", fip);
1936fputs("\\end{tabular} \n", fip);
1937fputs("\\end{center} \n", fip);
1938// fputs("\\newline \n",fip);
1939
[463]1940fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
1941fputs("\\newpage \n",fip);
1942int gid;
1943for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1944 gid = (*it).second;
1945 if (gid == 0) continue;
[484]1946 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1947 (*it).first.c_str(), (*it).first.c_str());
[2263]1948 fprintf(fip,"\\noindent \n");
[463]1949 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1950 if ((*ite).second.group != gid) continue;
[2263]1951 fprintf(fip,"\\piahelpitem{%s} \\label{%s} \n",
[484]1952 (*ite).first.c_str(), (*ite).first.c_str());
[463]1953 fputs("\\begin{verbatim} \n",fip);
1954 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1955 fputs("\\end{verbatim} \n",fip);
1956 }
1957 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1958 if ((*ite).second.group != gid) continue;
[2263]1959 fprintf(fip,"\\piacommand{%s} \\label{%s} \n",
[484]1960 (*ite).first.c_str(), (*ite).first.c_str());
[463]1961 fputs("\\begin{verbatim} \n",fip);
1962 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1963 fputs("\\end{verbatim} \n",fip);
1964 }
1965}
1966
1967fclose(fip);
1968return;
1969}
1970
1971
1972
Note: See TracBrowser for help on using the repository browser.