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

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

trunc et nint vire ds piacmd.cc / ca ne compile pas sous linux - Reza 08/01/2003

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