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

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

Correction arguments script ; -tcsh devient le nom du script comme pour les .pic - Reza 11/12/02

File size: 52.8 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;
419cmdhgrp["Commands"] = 1;
[293]420helpwin = new PIAHelpWind(app, this);
[330]421helpwin->AddHelpGroup("All", 0);
422helpwin->AddHelpGroup("Commands", 1);
[293]423
424string kw = "piacmd";
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";
[333]432usage += " > echo string # output string \n";
[2287]433usage += " > echo2file filename string # Append the string to the specified file \n";
[353]434usage += " > alias name string # define a command alias \n";
435usage += " > readstdin varname # reads a line from stdin into $varname \n";
[2241]436usage += " > foreach varname ( string-list ) # Loop \n";
[1562]437usage += " > for varname i1:i2[:di] # Integer loop \n";
438usage += " > for varname f1:f2[:df] # Float loop \n";
[349]439usage += " > end # end loops \n";
[2241]440usage += " > if ( test ) then # Conditional test : a == != < > <= >= b \n";
[1565]441usage += " > else # Conditional \n";
442usage += " > endif # End of conditional if bloc \n";
443usage += " > break # Delete (clears) all test and loop blocs \n";
444usage += " > return # Stops command execution from a file \n";
[2236]445usage += " > defscript endscript # Command script definition \n";
[353]446usage += " > listvars # List of variable names and values \n";
447usage += " > listalias # List of alias names and values \n";
448usage += " > listcommands # List of all known commands \n";
[2236]449usage += " > listscripts # List of all known scripts \n";
450usage += " > clearcript # Clear a script definition \n";
[353]451usage += " > exec filename # Execute commands from file \n";
[293]452usage += " > help <command_name> # <command_name> usage info \n";
453usage += " > helpwindow # Displays help window \n";
[349]454usage += " > timingon timingoff traceon traceoff \n";
[330]455string grp = "Commands";
456RegisterHelp(kw, usage, grp);
[293]457
[2203]458kw = "shell execute";
459usage = "> shell command_string # Execute shell command\n";
460usage += "> cshell command_string # Execute cshell command\n";
461usage += "---Exemples:\n";
462usage += " > shell ls\n";
463usage += " > cshell echo '$LD_LIBRARY_PATH'; map2cl -h; ls\n";
464usage += " > shell myfile.csh [arg1] [arg2] [...]\n";
465usage += " (where the first line of \"myfile.csh\" is \"#!/bin/csh\")\n";
466RegisterHelp(kw, usage, grp);
467
[293]468basexec = new PIABaseExecutor(this, omg, app);
[361]469fitexec = new PIAFitter(this, app);
[463]470pawexec = new PAWExecutor(this, app);
[1251]471CxxExecutor * cxxe = new CxxExecutor(this, app);
472cxxexec = cxxe;
[1828]473
474ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
475cntexec = cntxx; //_OP_
[1920]476FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
477flwexec = flwxx; //_OP_
[1828]478
[1251]479cxxoptwin = new CxxOptionWind(app, cxxe);
480cxxexwin = new CxxExecWind(app, cxxe);
481
[293]482AddInterpreter(this);
483curcmdi = this;
[165]484}
485
486/* --Methode-- */
487PIACmd::~PIACmd()
488{
489hist.close();
490if (gltimer) { delete gltimer; gltimer = NULL; }
[293]491Modmap::iterator it;
492for(it = modmap.begin(); it != modmap.end(); it++) {
493 string name = (*it).first + "_end";
494 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
495 if (fend) fend();
496 delete (*it).second;
497 }
[2236]498
499for(ScriptList::iterator sit = mScripts.begin();
500 sit != mScripts.end(); sit++) delete (*sit).second;
501
[293]502delete helpwin;
[1251]503delete cxxexwin;
504delete cxxoptwin;
[293]505if (curpiacmd == this) curpiacmd = NULL;
[361]506delete basexec;
507delete fitexec;
[463]508delete pawexec;
[1224]509delete cxxexec;
[165]510}
511
512/* --Methode-- */
[293]513PIACmd* PIACmd::GetInterpreter()
[165]514{
[293]515return(curpiacmd);
516}
[165]517
[293]518/* --Methode-- */
519string PIACmd::Name()
520{
521return("piacmd");
522}
[165]523
[293]524/* --Methode-- */
[330]525void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]526{
[330]527if (!ce) {
528 RegisterHelp(keyw, usage, grp);
529 return;
530 }
531int gid = CheckHelpGrp(grp);
[293]532cmdex cme;
[330]533cme.group = gid;
[293]534cme.us = usage;
535cme.cex = ce;
536cmdexmap[keyw] = cme;
537}
538
539/* --Methode-- */
[330]540void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
541{
542int gid = CheckHelpGrp(grp);
543cmdex cme;
544cme.group = gid;
545cme.us = usage;
546cme.cex = NULL;
547helpexmap[keyw] = cme;
548}
549
550/* --Methode-- */
551int PIACmd::CheckHelpGrp(string& grp)
552{
553int gid=0;
554CmdHGroup::iterator it = cmdhgrp.find(grp);
555if (it == cmdhgrp.end()) {
556 cmdgrpid++; gid = cmdgrpid;
557 cmdhgrp[grp] = gid;
558 helpwin->AddHelpGroup(grp.c_str(), gid);
559 }
560else gid = (*it).second;
561return(gid);
562}
563
564/* --Methode-- */
565void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
566{
567helpwin->ClearHelpList();
568CmdExmap::iterator it;
569for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
570 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
571 helpwin->AddHelpItem((*it).first.c_str());
572 }
573for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
574 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
575 helpwin->AddHelpItem((*it).first.c_str());
576 }
577}
578
579/* --Methode-- */
[293]580void PIACmd::LoadModule(string& fnameso, string& name)
581{
582PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
583if (dynlink == NULL) {
584 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
585 return;
[165]586 }
[293]587string fname = name + "_init";
588DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
589if (!finit) {
590 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
591 return;
[165]592 }
[293]593cout << "PIACmd/LoadModule_Info: Initialisation module" << name
594 << " " << fname << "() ..." << endl;
595finit();
596modmap[name] = dynlink;
597return;
598}
[165]599
[293]600/* --Methode-- */
601void PIACmd::AddInterpreter(CmdInterpreter * cl)
602{
603if (!cl) return;
[349]604interpmap[cl->Name()] = cl;}
[165]605
606/* --Methode-- */
[293]607void PIACmd::SelInterpreter(string& name)
[165]608{
[293]609InterpMap::iterator it = interpmap.find(name);
610if (it == interpmap.end()) return;
611curcmdi = (*it).second;
612}
[165]613
[293]614
615
[357]616/* Fonction */
[368]617static string GetStringFrStdin(PIACmd* piac)
[357]618{
[368]619PIStdImgApp* piapp = piac->GetImgApp();
620if (piapp) {
621 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
622 if (wdg) wdg->Refresh();
[357]623#ifndef __mac__
[368]624 /* On vide le buffer X-Window */
[374]625 XSync(PIXDisplay(),False);
[357]626#endif
[368]627}
[357]628char buff[128];
629fgets(buff, 128, stdin);
630buff[127] = '\0';
631return((string)buff);
632}
633
[293]634/* --Methode-- */
635int PIACmd::Interpret(string& s)
636{
[333]637int rc = 0;
[1262]638NamedObjMgr omg;
[2236]639ScriptList::iterator sit;
[165]640
[1262]641// On saute de commandes vides
642size_t l;
[349]643l = s.length();
[2285]644if (!mulinefg && (l < 1)) return(0);
[165]645
[349]646// On enregistre les commandes
647if (histon) hist << s << endl;
648
[1262]649if (s[0] == '#') return(0); // si c'est un commentaire
[349]650
[1276]651// Logique de gestion des lignes suite
652// un \ en derniere position indique la presence d'une ligne suite
[1539]653size_t lnb = s.find_last_not_of(' ');
654if (s[lnb] == '\\' ) { // Lignes suite ...
655 mulinecmd += s.substr(0,lnb);
[2285]656 if (!mulinefg) {
657 spromptmul = mImgApp->GetConsole()->GetPrompt();
658 mImgApp->GetConsole()->SetPrompt("...? ");
659 mulinefg = true;
660 }
[1276]661 return(0);
662}
[165]663
[1276]664if (mulinefg) { // Il y avait des lignes suite
665 s = mulinecmd + s;
666 mulinecmd = "";
667 mulinefg = false;
[2274]668 mImgApp->GetConsole()->SetPrompt(spromptmul);
[1276]669}
670
[1262]671// Removing leading blanks
[1276]672size_t p,q;
[1262]673
674p=s.find_first_not_of(" \t");
675if (p < l) s = s.substr(p);
676
[349]677// >>>> Substitution d'alias (1er mot)
[2214]678CmdStrList::iterator it;
[449]679p = 0;
680q = s.find_first_of(" \t");
681l = s.length();
682string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
683it = mAliases.find(w1);
684if (it != mAliases.end()) {
685 s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
686 l = s.length();
687 p=s.find_first_not_of(" \t");
688 if (p < l) s = s.substr(p);
[349]689 p = 0;
690 q = s.find_first_of(" ");
691 }
692
693// >>>> Separating keyword
[1262]694string toks,kw;
[165]695if (q < l)
696 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
697else { kw = s.substr(p,l-p); toks = ""; }
698
[1562]699// les mot-cle end else endif doivent etre le seul mot de la ligne
[2236]700if ( (kw == "end") || (kw == "else") || (kw == "endif") || (kw == "endscript") ) {
[1562]701 size_t ltk = toks.length();
702 if (toks.find_first_not_of(" \t") < ltk) {
[2236]703 cerr << "PIACmd::Interpret()/syntax error near end else endif endscript \n"
[1562]704 << "line: " << s << endl;
705 return(1);
706 }
707}
708
[2236]709// On verifie si on est en train de definir un script
710if (curscript) {
711 if (kw == "endscript") {
712 if (curscript->CheckScript()) {
713 sit = mScripts.find(curscript->Name());
714 if (sit != mScripts.end()) {
715 cout << "PIACmd::Interpret() replacing script "
716 << curscript->Name() << endl;
717 PIACmdScript* scr = mScripts[curscript->Name()];
718 mScripts.erase(sit);
719 delete scr;
720 }
721 cout << "PIACmd::Interpret() Script " << curscript->Name()
722 << " defined successfully" << endl;
723 mScripts[curscript->Name()] = curscript;
724 mImgApp->GetConsole()->SetPrompt("Cmd> ");
725 curscript = NULL;
726 return(0);
727 }
728 else {
729 cout << "PIACmd::Interpret() Error in Script " << curscript->Name()
730 << " definition " << endl;
731 mImgApp->GetConsole()->SetPrompt("Cmd> ");
732 curscript = NULL;
733 return(2);
734 }
735 }
736 else curscript->AddLine(s, kw);
737 return(0);
738}
[1562]739// On verifie si nous sommes dans un bloc (for , foreach)
[2215]740if (CmdBlks.top() != NULL) { // On est dans un bloc
[1562]741 if ( (kw == "for") || (kw == "foreach")) felevel++;
742 else if (kw == "end") felevel--;
743 if (felevel == 0) { // Il faut executer le bloc
[2215]744 PIACmdBloc* curb = CmdBlks.top();
745 CmdBlks.top() = curb->Parent();
[1562]746 mImgApp->GetConsole()->SetPrompt("Cmd> ");
[2236]747 if (!curb->CheckBloc()) {
[2215]748 cerr << "PIACmd::Interpret()/syntax error - unbalenced if ... endif"
749 << " within for/foreach bloc ! " << endl;
750 delete curb;
751 return(2);
752 }
[349]753 // cout << " *DBG* Executing bloc " << endl;
[1562]754 bool ohv = histon;
755 histon = false;
[2215]756 if (curtestresult) {
757 // We push also PIACmdBloc and testresult on the stack
758 CmdBlks.push(NULL);
759 list<char> xtx;
760 TestsStack.push(xtx);
761 curb->Execute();
762 // And PIACmdBloc and TestResult from the corresponding stacks
763 PopStack(false);
764 }
765 delete curb;
[1562]766 histon = ohv;
767 }
[2215]768 else CmdBlks.top()->AddLine(s, kw);
[1562]769 return(0);
770}
[1565]771else if (kw == "end") {
772 cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
773 << "line: " << s << endl;
774 return(1);
775}
[1562]776
777// Sommes-nous dans un bloc de test if then else
[2215]778if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if
[1565]779 if (kw == "else") {
780 if ((*tresit) & 2) {
781 cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
782 << "line: " << s << endl;
783 return(1);
784 }
785 else {
[1570]786 const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
[2215]787 if ((*tresit)&1) curtestresult = false;
[1565]788 mImgApp->GetConsole()->SetPrompt(npr);
789 (*tresit) |= 2;
790 return(0);
791 }
792 }
793 else if (kw == "endif") {
794 list<char>::iterator dbit = tresit;
[1562]795 tresit--;
[2215]796 TestsStack.top().erase(dbit);
[1570]797 const char * npr = "Cmd> ";
[2215]798 if (TestsStack.top().size() > 1) {
799 curtestresult = true;
800 list<char>::iterator it;
801 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
802 // Si on n'est pas ds le else et le if est faux
803 if ( !((*it)&2) && !((*it)&1) ) curtestresult = false;
804 // Si on est ds else et le if etait vrai !
805 if ( ((*it)&2) && ((*it)&1) ) curtestresult = false;
806 if (!curtestresult) break;
807 }
808
[1565]809 if (!((*tresit)&2))
810 npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
811 else
812 npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
813 }
[2215]814 else curtestresult = true;
[1565]815 mImgApp->GetConsole()->SetPrompt(npr);
[349]816 return(0);
817 }
[1565]818}
819else if ((kw == "else") || (kw == "endif")) {
820 cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
821 << "line: " << s << endl;
822 return(1);
[1562]823}
[349]824
[1565]825bool fgcont = true;
[2215]826if (TestsStack.top().size() > 0) { // Resultat de if ou else
[1565]827 list<char>::iterator it;
[2215]828 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
[1565]829 // Si on n'est pas ds le else et le if est faux
830 if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
831 // Si on est ds else et le if etait vrai !
832 if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
833 if (!fgcont) break;
834 }
835}
836
837if ((!fgcont) && (kw != "if")) return(0);
838
839
[2215]840// Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
841if (kw == "break") return(77766);
842else if (kw == "return") return(77777);
843
[1262]844// Nous ne sommes donc pas dans un bloc .... Substitution de variables
[1276]845string s2;
846int rcs ;
[1262]847// Execution de code C++
848
[2236]849if (kw == "@@") {
[1262]850 CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
851 if (cxxe == NULL) {
852 cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
853 return(99);
854 }
[2236]855 return(cxxe->ExecuteCXX(s.substr(2)));
[1262]856}
857
858
[1276]859rcs = SubstituteVars(s, s2);
860if (rcs) return(rcs);
[1262]861
862// >>>> Separating keyword and tokens
863vector<string> tokens;
[2236]864/* decoupage en mots */
865LineToWords(s2, kw, tokens, toks, true);
[2298]866
[1562]867// Si c'est un for/foreach, on cree un nouveau bloc
868if ((kw == "foreach") || (kw == "for")) {
[1262]869 // cout << " *DBG* We got a foreach... " << endl;
[2215]870 PIACmdBloc* bloc = new PIACmdBloc(this, CmdBlks.top(), kw, tokens);
[1262]871 if (!bloc->CheckOK()) {
[1565]872 cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
[1262]873 delete bloc;
[1565]874 return(1);
[1262]875 }
876 felevel++;
[2215]877 if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
[1562]878 else mImgApp->GetConsole()->SetPrompt("for...> ");
[2215]879 CmdBlks.top() = bloc;
[1262]880 // cout << " *DBG* New Bloc created ... " << endl;
881 return(0);
882 }
[1565]883else if (kw == "if") { // Un test if
884 bool restst = true;
885 int rct = EvaluateTest(tokens, s, restst);
886 if (rct) {
887 cerr << "PIACmd::Interpret() if syntax Error ! " << endl;
888 return(1);
889 }
890 char res_tst = (restst) ? 1 : 0;
[2215]891 TestsStack.top().push_back(res_tst);
892 if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin();
[1562]893 else tresit++;
[1570]894 const char * npr = (restst) ? "if-T> " : "if-F> ";
[1565]895 mImgApp->GetConsole()->SetPrompt(npr);
[1562]896}
[2241]897else if ((tokens.size() > 0) && (tokens[0] == "=")) {
898 // x = RPNExpression (Evaluation d'expression RPN)
899 tokens[0] = kw;
900 int rcev = EvalRPNExpr(tokens, s);
[2236]901 if (rcev) {
[2241]902 cerr << "PIACmd::Interpret() evaluation (RPN) syntax Error ! " << endl;
[2236]903 return(1);
904 }
905 return(0);
906}
907else if (kw == "defscript") { // definition de script
[2275]908 if (tokens.size() > 0) {
909 if (tokens.size() < 2) tokens.push_back("");
910 curscript = new PIACmdScript(this, tokens[0], tokens[1]);
911 mImgApp->GetConsole()->SetPrompt("Script...> ");
912 return(0);
913 }
914 else {
915 cerr << "PIACmd::Interpret() No script name in defscript" << endl;
916 return(1);
917 }
[2236]918}
919else {
920 // Si c'est le nom d'un script
921 sit = mScripts.find(kw);
922 if (sit != mScripts.end()) {
923 bool ohv = histon;
924 histon = false;
[2298]925 tokens.insert(tokens.begin(), kw);
[2236]926 PushStack(tokens);
927 (*sit).second->Execute(tokens);
928 PopStack(true);
929 histon = ohv;
930 }
931 // Execution de commandes
932 else rc = ExecuteCommandLine(kw, tokens, toks);
933 return(rc);
934}
[1262]935// cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
936// for(int ii=0; ii<tokens.size(); ii++)
937// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
938
[2241]939return(0);
[1262]940}
941
[2236]942
[1262]943/* --Methode-- */
[2236]944int PIACmd::LineToWords(string& line, string& kw, vector<string>& tokens,
945 string& toks, bool uq)
946{
947if (line.length() < 1) return(0);
948int nw = 1;
949size_t p = line.find_first_not_of(" ");
950line = line.substr(p);
951p = 0;
952size_t q = line.find_first_of(" ");
953size_t l = line.length();
954
955if (q < l)
956 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
957else { kw = line.substr(p,l-p); toks = ""; }
958
959q = 0;
960while (q < l) {
961 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
962 if (p>=l) break;
963 if ( uq && ((toks[p] == '\'') || (toks[p] == '"')) ) {
964 q = toks.find(toks[p],p+1);
965 if (q>=l) {
966 cerr << "PIACmd::LineToWords/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
967 return(-1);
968 }
969 p++;
970 }
971 else {
972 q = toks.find_first_of(" \t",p); // la fin du token;
973 }
974 string token = toks.substr(p,q-p);
975 tokens.push_back(token); nw++;
976 }
977
978return(nw);
979}
980
981/* --Methode-- */
[1262]982int PIACmd::SubstituteVars(string & s, string & s2)
983// Variable substitution
984{
[2214]985NamedObjMgr& omg = *mObjMgr;
[1262]986
[2215]987int iarr = -1; // index d'element de tableau
988size_t p,q,q2,q3,l;
[1262]989
990s2="";
[349]991p = 0;
992l = s.length();
[2214]993string vn, vv;
[349]994while (p < l) {
995 q = s.find('$',p);
[1276]996 if (q > l) break;
997 q2 = s.find('\'',p);
998 if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
999 q2 = s.find('\'',q2+1);
1000 if (q2 >= l) {
1001 cerr << " Syntax error - Unbalenced quotes !!! " << endl;
1002 return(1);
1003 }
1004 s2 += s.substr(p, q2-p+1);
1005 p = q2+1; continue;
1006 }
[349]1007 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
1008 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
1009 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
1010 continue;
1011 }
1012 if (q >= l-1) {
[1276]1013 cerr << " Syntax error - line ending with $ !!! " << endl;
1014 return(2);
[349]1015 }
1016 vn = "";
1017 if ( s[q+1] == '{' ) { // Variable in the form ${name}
1018 q2 = s.find('}',q+1);
1019 if (q2 >= l) {
[1276]1020 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
1021 return(3);
[349]1022 }
1023 vn = s.substr(q+2,q2-q-2);
1024 q2++;
[2215]1025 }
1026 else if ( s[q+1] == '(' ) { // Variable in the form $(name)
1027 q2 = s.find(')',q+1);
1028 if (q2 >= l) {
1029 cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl;
1030 return(3);
1031 }
1032 vn = s.substr(q+2,q2-q-2);
1033 q2++;
1034 }
[349]1035 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
1036 q2 = s.find(']',q+1);
1037 if (q2 >= l) {
[1276]1038 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
1039 return(4);
[349]1040 }
1041 vn = s.substr(q+2,q2-q-2);
[2214]1042 if (!GetVar(vn, vv)) return(5);
1043 vn = vv;
[349]1044 q2++;
1045 }
1046 else {
[1562]1047 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
[349]1048 if (q2 > l) q2 = l;
[2215]1049 q3 = q2;
[349]1050 vn = s.substr(q+1, q2-q-1);
[2215]1051 // Si variable de type $varname[index] : element de tableau
1052 if ((q2 < l) && (s[q2] == '[') ) {
1053 q3 = s.find_first_of("]",q2+1);
1054 string sia = s.substr(q2+1, q3-q2-1);
[2218]1055 if (sia.length() < 1) {
1056 cerr << " Syntax error - in $varname[index] : $"
1057 << vn << "[" << sia <<"]" << endl;
1058 return(4);
1059 }
1060 if (isalpha(sia[0])) {
1061 string sia2;
1062 if (!GetVar(sia, sia2) || (sia2.length() < 1)) {
1063 cerr << " Syntax error - in $varname[index] : $"
1064 << vn << "[" << sia <<"]" << endl;
1065 return(4);
1066 }
1067 sia = sia2;
1068 }
[2215]1069 int rcdia = ctoi(sia.c_str(), &iarr);
1070 if (rcdia < 0) {
1071 cerr << " Syntax error - in $varname[iarr] : $"
1072 << vn << "[" << sia <<"]" << endl;
1073 return(4);
1074 }
[349]1075 }
[2215]1076 }
[2214]1077 if (!GetVar(vn, vv)) return(5);
[2215]1078 if (iarr < 0) {
1079 s2 += (s.substr(p, q-p) + vv);
1080 p = q2;
1081 }
1082 else {
1083 vector<string> vs;
1084 FillVStringFrString(vv, vs);
1085 if (iarr >= vs.size()) {
1086 cerr << " Substitution error - word index out of range in "
1087 << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
1088 return(4);
1089 }
1090 else s2 += (s.substr(p, q-p) + vs[iarr]);
1091 p = q3+1;
1092 }
1093}
[349]1094if (p < l) s2 += s.substr(p);
1095
1096p = s2.find_first_not_of(" \t");
1097if (p < l) s2 = s2.substr(p);
1098
[1262]1099return(0);
1100}
[349]1101
[1262]1102/* --Methode-- */
[2214]1103bool PIACmd::GetVar(string & vn, string & vv)
1104{
1105NamedObjMgr& omg = *mObjMgr;
1106if (vn.length() < 1) {
1107 cerr << " PIACmd::SubstituteVar/Error: length(varname=" << vn << ")<1 !" << endl;
1108 vv = ""; return(false);
1109}
1110// Variable de type $# $0 $1 ... (argument de .pic ou de script)
1111int ka = 0;
1112if (vn == "#") {
1113 if (ArgsStack.empty()) {
1114 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
1115 << " ($" << vn << ")" << endl;
[2218]1116 vv = ""; return(false);
[2214]1117 }
1118 char buff[32];
1119 long an = ArgsStack.top().size();
1120 sprintf(buff,"%ld", an);
[2218]1121 vv = buff; return(true);
[2214]1122}
1123else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
1124 if (ArgsStack.empty()) {
1125 cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
1126 << " ($" << vn << ")" << endl;
1127 vv = ""; return(false);
1128 }
1129 if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
1130 cerr << " PIACmd::SubstituteVar/Error: Undefined variable ! "
1131 << " ($" << vn << ")" << endl;
1132 vv = ""; return(false);
1133 }
[2218]1134 vv = ArgsStack.top()[ka]; return(true);
[2214]1135}
[2218]1136else if (vn[0] == '#') { // Variable de type $#vname --> size(vname)
1137 vn = vn.substr(1);
1138 if (!omg.HasVar(vn) ) {
1139 cerr << " PIACmd::SubstituteVarError: Undefined variable "
1140 << vn << " ! " << endl;
1141 vv = ""; return(false);
1142 }
1143 vn = omg.GetVar(vn);
1144 vector<string> vs;
1145 FillVStringFrString(vn, vs);
1146 char buff[32];
1147 sprintf(buff,"%d", (int)vs.size());
1148 vv = buff; return(true);
1149 }
[2214]1150else { // variable ordinaire geree par NamedObjMgr
1151 if ( (!omg.HasVar(vn)) ) {
1152 cerr << " PIACmd::SubstituteVarError: Undefined variable "
1153 << vn << " ! " << endl;
[2218]1154 vv = ""; return(false);
[2214]1155 }
[2218]1156 vv = omg.GetVar(vn); return(true);
[2214]1157}
1158
[2218]1159return(false);
[2214]1160}
1161
1162/* --Methode-- */
[1565]1163int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
[1562]1164{
[1565]1165 res = true;
[2215]1166 if ((args.size() != 6) || (args[5] != "then") ||
1167 (args[0] != "(") || (args[4] != ")") ) return(1);
1168 if (args[2] == "==") res = (args[1] == args[3]);
1169 else if (args[2] == "!=") res = (args[1] != args[3]);
1170 else if (args[2] == "<")
1171 res = (atof(args[1].c_str()) < atof(args[3].c_str()));
1172 else if (args[2] == ">")
1173 res = (atof(args[1].c_str()) > atof(args[3].c_str()));
1174 else if (args[2] == "<=")
1175 res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
1176 else if (args[2] == ">=")
1177 res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
[1565]1178 else return(2);
1179 return(0);
[1562]1180}
1181
[2241]1182/* Operations sur le stack RPN */
1183inline bool Check_myRPNStack_(stack<double>& s, double& x, string& line)
1184{
1185 if (s.empty()) {
1186 cerr << "PIACmd::EvalRPNExpr: syntax error / empty RPN stack " << line << endl;
1187 return true;
1188 }
1189 else x = s.top();
1190 return false;
1191}
1192inline bool Check_myRPNStack_(stack<double>& s, double& x, double& y, string& line)
1193{
1194 if (s.size() < 2) {
1195 cerr << "PIACmd::EvalRPNExpr: syntax error / RPN stack size < 2 " << line << endl;
1196 return true;
1197 }
1198 else {
1199 x = s.top(); s.pop(); y = s.top();
1200 }
1201 return false;
1202}
1203
1204inline void Print_myRPNStack_(stack<double> s)
1205{
1206 if (s.empty())
1207 cout << "PIACmd::EvalRPNExpr/PrintStack: Empty stack " << endl;
1208 else {
1209 int k = 0;
1210 cout << "PIACmd::EvalRPNExpr/PrintStack: Size()= " << s.size() << endl;
1211 while( !s.empty() ) {
1212 cout << " " << k << ": " << s.top() << " ";
1213 if (k == 0) cout << " (x) " << endl;
1214 else if (k == 1) cout << " (y) " << endl;
1215 else if (k == 2) cout << " (z) " << endl;
1216 else cout << endl;
1217 s.pop(); k++;
1218 }
1219 }
[2236]1220
[2241]1221}
1222
[2265]1223int Sum_RPNStack_(stack<double>& s, double& sx, double& sx2, string& line)
1224{
1225 sx = sx2 = 0.;
1226 int nn = 0;
1227 double x = 0.;
1228 while( !s.empty() ) {
1229 x = s.top(); s.pop();
1230 sx += x; sx2 += x*x;
1231 nn++;
1232 }
1233 return(nn);
1234}
1235
1236int Product_RPNStack_(stack<double>& s, double& px, string& line)
1237{
1238 px = 1.;
1239 int nn = 0;
1240 double x = 0.;
1241 while( !s.empty() ) {
1242 x = s.top(); s.pop();
1243 px *= x; nn++;
1244 }
1245 return(nn);
1246}
[2241]1247
[1562]1248/* --Methode-- */
[2241]1249int PIACmd::EvalRPNExpr(vector<string> & args, string & line)
[2236]1250{
[2241]1251 NamedObjMgr& omg = *mObjMgr;
1252
1253 if (args.size() < 2) {
1254 cerr << "PIACmd::EvalRPNExpr: syntax error / missing arguments " << line << endl;
[2236]1255 return(1);
1256 }
[2241]1257 else if (args.size() == 2) {
1258 omg.SetVar(args[0], args[1]);
1259 return(0);
1260 }
[2236]1261
[2241]1262 double x,y;
1263 x = y = 0.;
1264 stack<double> rpnstack; // Stack des operations en RPN
1265 for(int k=1; k<args.size(); k++) {
1266 // Les 4 operations de base + - * /
[2236]1267 if (args[k] == "+") {
[2241]1268 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1269 rpnstack.top() = y+x;
[2236]1270 }
1271 else if (args[k] == "-") {
[2241]1272 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1273 rpnstack.top() = y-x;
[2236]1274 }
1275 else if (args[k] == "*") {
[2241]1276 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1277 rpnstack.top() = y*x;
[2236]1278 }
1279 else if (args[k] == "/") {
[2241]1280 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1281 rpnstack.top() = y/x;
[2236]1282 }
[2241]1283 // Les constantes : e , pi
1284 else if (args[k] == "e") {
1285 rpnstack.push(M_E);
1286 }
1287 else if (args[k] == "pi") {
1288 rpnstack.push(M_PI);
1289 }
1290 // Les fonctions usuelles a 1 argument f(x)
1291 else if (args[k] == "cos") {
1292 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1293 rpnstack.top() = cos(x);
1294 }
1295 else if (args[k] == "sin") {
1296 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1297 rpnstack.top() = sin(x);
1298 }
1299 else if (args[k] == "tan") {
1300 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1301 rpnstack.top() = tan(x);
1302 }
1303 else if (args[k] == "acos") {
1304 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1305 rpnstack.top() = acos(x);
1306 }
1307 else if (args[k] == "asin") {
1308 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1309 rpnstack.top() = asin(x);
1310 }
1311 else if (args[k] == "atan") {
1312 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1313 rpnstack.top() = atan(x);
1314 }
1315 else if (args[k] == "log") {
1316 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1317 rpnstack.top() = log(x);
1318 }
1319 else if (args[k] == "log10") {
1320 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1321 rpnstack.top() = log10(x);
1322 }
1323 else if (args[k] == "exp") {
1324 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1325 rpnstack.top() = exp(x);
1326 }
[2294]1327 else if (args[k] == "fabs") {
1328 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1329 rpnstack.top() = fabs(x);
1330 }
[2297]1331 else if (args[k] == "chs") {
1332 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1333 rpnstack.top() = -x;
1334 }
[2241]1335 else if (args[k] == "deg2rad") {
1336 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1337 rpnstack.top() = x*M_PI/180.;
1338 }
1339 else if (args[k] == "rad2deg") {
1340 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
1341 rpnstack.top() = x*180./M_PI;
1342 }
1343 // Les fonctions usuelles a 2 argument f(x,y)
1344 else if (args[k] == "pow") {
1345 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1346 rpnstack.top() = pow(y,x);
1347 }
1348 else if (args[k] == "atan2") {
1349 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1350 rpnstack.top() = atan2(x,y);
1351 }
[2265]1352 // Fonction a N arguments - Somme, produit, etc ...
1353 else if ((args[k] == "sum") || (args[k] == "mean") ||
1354 (args[k] == "sigma") || (args[k] == "sigma2") ) {
1355 double sx, sx2;
1356 int nn = Sum_RPNStack_(rpnstack, sx, sx2, line);
1357 if (args[k] == "sum") rpnstack.push(sx);
1358 else {
1359 if (nn == 0) {
1360 cerr << "PIACmd::EvalRPNExpr: mean/sigma error- RPN stack empty: "
1361 << line << endl;
1362 return(1);
1363 }
1364 double fnn = nn;
1365 if (args[k] == "mean") rpnstack.push(sx/fnn);
1366 else if (args[k] == "sigma2") rpnstack.push(sx2/fnn-(x*x/(fnn*fnn)));
1367 else rpnstack.push(sqrt(sx2/fnn-(x*x/(fnn*fnn))));
1368 }
1369 }
1370 else if (args[k] == "product") {
1371 double px;
1372 int nn = Product_RPNStack_(rpnstack, px, line);
1373 if (nn == 0) {
1374 cerr << "PIACmd::EvalRPNExpr: product error- RPN stack empty: "
1375 << line << endl;
1376 return(1);
1377 }
1378 rpnstack.push(px);
1379 }
[2241]1380 // Fonctions de manipulation de stack
1381 else if (args[k] == "print") {
1382 Print_myRPNStack_(rpnstack);
1383 }
1384 else if (args[k] == "x<>y") {
1385 if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
1386 rpnstack.top() = x; rpnstack.push(y);
1387 }
1388 // On met un nombre sur le stack
1389 else {
[2294]1390 char * esptr;
1391 x = strtod(args[k].c_str(), &esptr);
1392 // if (ctof(args[k].c_str(),&x) < 0) {
1393 if (esptr == args[k].c_str()) {
[2241]1394 cerr << "PIACmd::EvalRPNExpr: syntax error near " << args[k]
1395 << " in expression: \n" << line << endl;
1396 return(2);
1397 }
1398 rpnstack.push(x);
1399 }
1400
[2236]1401 }
1402
[2241]1403 if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
[2236]1404 char buff[64];
1405 sprintf(buff, "%g", x);
[2241]1406 string res = buff;
1407 omg.SetVar(args[0], res);
[2236]1408 return(0);
1409}
1410
1411/* --Methode-- */
[2215]1412void PIACmd::PushStack(vector<string>& args)
1413{
1414 // We push the argument list (args) on the stack
1415 ArgsStack.push(args);
1416 // We push also PIACmdBloc and testresult on the stack
1417 CmdBlks.push(NULL);
1418 list<char> xtx;
1419 TestsStack.push(xtx);
1420
1421}
1422
1423/* --Methode-- */
1424void PIACmd::PopStack(bool psta)
1425{
1426 // We remove the argument list (args) from the stack
1427 if (psta) ArgsStack.pop();
1428 // And PIACmdBloc and TestResult from the corresponding stacks
1429 PIACmdBloc* curb = CmdBlks.top();
1430 while (curb != NULL) {
1431 PIACmdBloc* parb = curb->Parent();
1432 delete curb; curb = parb;
1433 }
1434 CmdBlks.pop();
1435 TestsStack.pop();
1436}
1437
1438/* --Methode-- */
[1262]1439int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
1440{
1441int rc = 0;
1442NamedObjMgr omg;
[349]1443
[165]1444// >>>>>>>>>>> Commande d'interpreteur
[1262]1445if (kw == "helpwindow") ShowHelpWindow();
[293]1446else if (kw == "help") {
[330]1447 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]1448 else {
[330]1449 string kwh = "piacmd";
1450 cout << GetUsage(kwh) << endl;
[293]1451 }
[165]1452 }
1453
1454else if (kw == "set") {
[293]1455 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[349]1456 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1457 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1458 return(0);
1459 }
[1276]1460 // string xx = tokens[1];
1461 // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
1462 omg.SetVar(tokens[0], tokens[1]);
[165]1463 }
[349]1464
1465else if (kw == "getvar") {
1466 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
[1262]1467 if (!omg.HasVar(tokens[1])) {
[349]1468 cerr << "Error - No " << tokens[1] << " Variable " << endl;
1469 return(0);
1470 }
1471 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1472 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1473 return(0);
1474 }
[1262]1475 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
[349]1476 }
1477
1478else if (kw == "alias") {
1479 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
1480 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1481 cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
1482 return(0);
1483 }
1484 string xx = tokens[1];
1485 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
1486 mAliases[tokens[0]] = xx;
1487 }
1488
[333]1489else if (kw == "setol") {
1490 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
[349]1491 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1492 cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
1493 return(0);
1494 }
[333]1495 vector<string> ol;
[349]1496 mObjMgr->GetObjList(tokens[1], ol);
1497 string vol;
1498 if (ol.size() < 1) vol = "";
1499 else {
1500 vol = ol[0];
1501 for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
1502 }
[1262]1503 omg.SetVar(tokens[0], vol);
[333]1504 }
[293]1505else if (kw == "unset") {
1506 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[1262]1507 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
[293]1508 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]1509 }
[2241]1510 else if (kw == "rpneval") { // Evaluation d'expression en notation polonaise inverse
1511 return(EvalRPNExpr(tokens, toks));
1512}
[333]1513else if (kw == "echo") {
[1276]1514 for (int ii=0; ii<tokens.size(); ii++)
1515 cout << tokens[ii] << " " ;
[333]1516 cout << endl;
[1276]1517 }
[2287]1518else if (kw == "echo2file") {
1519 if (tokens.size() < 1) {
1520 cout << "PIACmd::Interpret() Usage: echo2file filename [string ] " << endl;
1521 return(0);
1522 }
1523 ofstream ofs(tokens[0].c_str(), ios::app);
1524 for (int ii=1; ii<tokens.size(); ii++)
1525 ofs << tokens[ii] << " " ;
1526 ofs << endl;
1527 }
[353]1528else if (kw == "readstdin") {
1529 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
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 }
[357]1534 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
1535 cout << tokens[0] << " ? " << endl;
[1262]1536 omg.SetVar(tokens[0], GetStringFrStdin(this) );
[353]1537 }
1538
[165]1539else if (kw == "listvars") {
[293]1540 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[1262]1541 DVList& varlist = omg.GetVarList();
1542 DVList::ValList::const_iterator it;
1543 string value;
1544 for(it = varlist.Begin(); it != varlist.End(); it++) {
[1291]1545#ifdef SANS_EVOLPLANCK
1546 MuTyV mtv = (*it).second;
1547 value = (string)(mtv);
1548#else
[1262]1549 value = (string)((*it).second.elval);
[1291]1550#endif
[1262]1551 cout << (*it).first << " = " << value << "\n";
1552 }
[165]1553 cout << endl;
1554 }
[353]1555else if (kw == "listalias") {
1556 cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
[2214]1557 CmdStrList::iterator it;
[353]1558 for(it = mAliases.begin(); it != mAliases.end(); it++)
1559 cout << (*it).first << " = " << (*it).second << "\n";
1560 cout << endl;
1561 }
1562else if (kw == "listcommands") {
[2236]1563 cout << "---- PIACmd::Interpret() Command List ----- \n";
[293]1564 CmdExmap::iterator it;
1565 int kc = 0;
1566 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
1567 cout << (*it).first << " ";
1568 kc++;
1569 if (kc >= 5) { cout << "\n"; kc = 0; }
1570 }
1571 cout << endl;
1572 }
[2236]1573else if (kw == "listscripts") {
1574 cout << "---- PIACmd::Interpret() Script List ----- \n";
1575 for(ScriptList::iterator sit = mScripts.begin();
1576 sit != mScripts.end(); sit++)
1577 cout << " Script: " << (*sit).second->Name() << " - "
1578 << (*sit).second->Comment() << endl;
1579}
1580else if (kw == "clearscript") {
1581 if (tokens.size() < 1) {
1582 cout << "PIACmd::Interpret() Usage: clearscript scriptname" << endl;
1583 return(0);
1584 }
1585 ScriptList::iterator sit = mScripts.find(tokens[0]);
1586 if (sit == mScripts.end()) {
1587 cout << "PIACmd::Interpret() No script with name" << tokens[0] << endl;
1588 return(0);
1589 }
1590 else {
1591 delete (*sit).second;
1592 mScripts.erase(sit);
1593 cout << "PIACmd::Interpret() script " << tokens[0] << " cleared" << endl;
1594 return(0);
1595 }
1596}
[293]1597else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
1598else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
[165]1599else if (kw == "timingon") {
[293]1600 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
1601 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
1602 }
[165]1603else if (kw == "timingoff") {
[293]1604 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
1605 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
1606 }
[165]1607else if (kw == "exec") {
[293]1608 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
[349]1609 ExecFile(tokens[0], tokens);
[165]1610 }
1611else if (kw == "shell") {
[293]1612 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[1276]1613 string cmd;
1614 for (int ii=0; ii<tokens.size(); ii++)
1615 cmd += (tokens[ii] + ' ');
1616 system(cmd.c_str());
[165]1617 }
[2203]1618else if (kw == "cshell") {
1619 if(tokens.size()<1) {cout<<"PIACmd::Interpret() Usage: cshell cmdline"<<endl; return(0);}
1620 string cmd="";
1621 for(int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
1622 CShellExecute(cmd);
1623 }
[1276]1624
[293]1625// Execution d'une commande enregistree
[1268]1626else rc = ExecuteCommand(kw, tokens, toks);
[165]1627
1628if (timing) gltimer->Split();
[333]1629return(rc);
[165]1630}
1631
[333]1632/* --Methode-- */
[2236]1633int PIACmd::ParseLineExecute(string& line, bool qw)
1634 // Si qw == true, on decoupe entre '' ou "" ou espaces
[333]1635{
[1262]1636vector<string> tokens;
[2236]1637string kw, toks;
[333]1638if (line.length() < 1) return(0);
[2236]1639LineToWords(line, kw, tokens, toks, qw);
[1268]1640return(ExecuteCommand(kw, tokens, toks));
[333]1641}
1642
[165]1643/* --Methode-- */
[1268]1644int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
[333]1645{
1646 int rc = -1;
1647 CmdExmap::iterator it = cmdexmap.find(keyw);
1648 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
1649 else {
[1268]1650 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
[333]1651 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1652 }
1653 return(rc);
1654}
1655
1656/* --Methode-- */
[349]1657int PIACmd::ExecFile(string& file, vector<string>& args)
[165]1658{
[293]1659char line_buff[512];
1660FILE *fip;
[165]1661
[293]1662if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
[384]1663 if (file.find('.') >= file.length()) {
1664 cout << "PIACmd::Exec(): Error opening file " << file << endl;
1665 file += ".pic";
1666 cout << " Trying file " << file << endl;
1667 fip = fopen(file.c_str(),"r");
1668 }
1669 }
1670
1671if(fip == NULL) {
[293]1672 cerr << "PIACmd::Exec() Error opening file " << file << endl;
1673 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
1674 return(0);
[165]1675 }
[349]1676
1677// hist << "### Executing commands from " << file << endl;
1678
[2215]1679PushStack(args);
[293]1680if (trace) {
1681 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
1682 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1683 mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
1684 }
[165]1685
[1562]1686bool ohv = histon;
[349]1687histon = false;
[293]1688while (fgets(line_buff,511,fip) != NULL)
1689 {
1690 if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
1691 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1692 string line(line_buff);
[1565]1693 if (Interpret(line) == 77777) break;
[165]1694 }
[1562]1695histon = ohv;
[349]1696
1697// hist << "### End of Exec( " << file << " ) " << endl;
[293]1698if (trace) {
1699 mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
1700 mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
1701 mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
[165]1702 }
1703
[2215]1704PopStack(true);
[2214]1705
[165]1706return(0);
1707}
1708
[2203]1709/* --Methode-- */
1710int PIACmd::CShellExecute(string cmd)
1711{
1712 if(cmd.size()<=0) return -1;
[293]1713
[2203]1714 NamedObjMgr omg;
1715 string fname = omg.GetTmpDir(); fname += "cshell_exec_pia.csh";
1716
1717 string cmdrm = "rm -f " + fname;
1718 system(cmdrm.c_str());
1719
1720 FILE *fip = fopen(fname.c_str(),"w");
1721 if(fip==NULL) {
1722 cout << "PIACmd/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
1723 return -2;
1724 }
1725 fprintf(fip,"#!/bin/csh\n\n");
1726 fprintf(fip,"%s\n",cmd.c_str());
1727 fprintf(fip,"\nexit 0\n");
1728 fclose(fip);
1729
1730 cmd = "csh "; cmd += fname;
1731 system(cmd.c_str());
1732
1733 system(cmdrm.c_str());
1734
1735 return 0;
1736}
1737
[293]1738static string* videstr = NULL;
1739/* --Methode-- */
1740string& PIACmd::GetUsage(const string& kw)
[165]1741{
[330]1742bool fndok = false;
[293]1743CmdExmap::iterator it = cmdexmap.find(kw);
[330]1744if (it == cmdexmap.end()) {
1745 it = helpexmap.find(kw);
1746 if (it != helpexmap.end()) fndok = true;
[165]1747 }
[330]1748 else fndok = true;
1749if (fndok) return( (*it).second.us );
1750// Keyword pas trouve
1751if (videstr == NULL) videstr = new string("");
1752*videstr = "Nothing known about " + kw + " ?? ";
1753return(*videstr);
1754
[165]1755}
1756
[293]1757/* --Methode-- */
1758void PIACmd::ShowHelpWindow()
[165]1759{
[293]1760helpwin->Show();
[165]1761}
[357]1762
[463]1763/* --Methode-- */
[1251]1764void PIACmd::ShowCxxOptionWindow()
1765{
1766cxxoptwin->Show();
1767}
1768
1769/* --Methode-- */
1770void PIACmd::ShowCxxExecWindow()
1771{
1772cxxexwin->Show();
1773}
1774
[2263]1775/* Les definitions suivantes doivent se trouver ds l'en-tete du fichier LaTeX
1776 \newcommand{\piacommand}[1]{
1777 \framebox{\bf \Large #1 } \index{#1} % (Command)
1778 }
1779
1780 \newcommand{\piahelpitem}[1]{
1781 \framebox{\bf \Large #1 } \index{#1} (Help item)
1782 }
1783
1784 \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) }
1785*/
1786
[1251]1787/* --Methode-- */
[2263]1788void PIACmd::HelptoLaTeX(string const & fname)
[463]1789{
1790FILE *fip;
1791if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1792 cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
1793 return;
1794 }
[357]1795
[463]1796fputs("% ----- Liste des groupes de Help ----- \n",fip);
1797fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1798fputs("\\begin{itemize} \n",fip);
1799CmdHGroup::iterator it;
[2263]1800for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1801 if ((*it).first == "All") continue;
[484]1802 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1803 (*it).first.c_str(), (*it).first.c_str());
[2263]1804}
[463]1805
1806fputs("\\end{itemize} \n",fip);
1807
[2263]1808fputs("\\vspace*{10mm} \n",fip);
[484]1809
1810CmdExmap::iterator ite;
1811fputs("% ----- Liste de toutes les commandes ----- \n",fip);
1812fputs("\\begin{center} \n ", fip);
1813fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
1814fputs("\n \n \\vspace{5mm} \n",fip);
1815fputs("\\begin{tabular}{llllll} \n", fip);
1816int kt = 0;
1817for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1818 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1819 kt++;
1820 if (kt < 3) fputs(" & ", fip);
1821 else { fputs(" \\\\ \n", fip); kt = 0; }
1822 }
1823if (kt == 1) fputs(" & & & \\\\ \n", fip);
1824else if (kt == 2) fputs(" & \\\\ \n", fip);
1825fputs("\\end{tabular} \n", fip);
1826fputs("\\end{center} \n", fip);
[2263]1827fputs("\n \n \\newpage \\vspace{1cm} \n",fip);
[484]1828
1829fputs("\\begin{center} \n ", fip);
1830fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
1831fputs("\n \n \\vspace{5mm} \n",fip);
1832fputs("\\begin{tabular}{llllll} \n", fip);
1833kt = 0;
1834for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1835 fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
1836 kt++;
1837 if (kt < 3) fputs(" & ", fip);
1838 else { fputs(" \\\\ \n", fip); kt = 0; }
1839 }
1840if (kt == 1) fputs(" & & & \\\\ \n", fip);
1841else if (kt == 2) fputs(" & \\\\ \n", fip);
1842fputs("\\end{tabular} \n", fip);
1843fputs("\\end{center} \n", fip);
1844// fputs("\\newline \n",fip);
1845
[463]1846fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
1847fputs("\\newpage \n",fip);
1848int gid;
1849for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1850 gid = (*it).second;
1851 if (gid == 0) continue;
[484]1852 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1853 (*it).first.c_str(), (*it).first.c_str());
[2263]1854 fprintf(fip,"\\noindent \n");
[463]1855 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1856 if ((*ite).second.group != gid) continue;
[2263]1857 fprintf(fip,"\\piahelpitem{%s} \\label{%s} \n",
[484]1858 (*ite).first.c_str(), (*ite).first.c_str());
[463]1859 fputs("\\begin{verbatim} \n",fip);
1860 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1861 fputs("\\end{verbatim} \n",fip);
1862 }
1863 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
1864 if ((*ite).second.group != gid) continue;
[2263]1865 fprintf(fip,"\\piacommand{%s} \\label{%s} \n",
[484]1866 (*ite).first.c_str(), (*ite).first.c_str());
[463]1867 fputs("\\begin{verbatim} \n",fip);
1868 fprintf(fip,"%s\n", (*ite).second.us.c_str());
1869 fputs("\\end{verbatim} \n",fip);
1870 }
1871}
1872
1873fclose(fip);
1874return;
1875}
1876
1877
1878
Note: See TracBrowser for help on using the repository browser.