source: Sophya/trunk/SophyaLib/SysTools/commander.cc@ 2615

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 57.5 KB
RevLine 
[2615]1#include "sopnamsp.h"
[2446]2#include "commander.h"
3#include <stdio.h>
4#include <stdlib.h>
[2518]5#include <unistd.h>
[2446]6#include <ctype.h>
7#include <math.h>
8
9#include "strutil.h"
10#include "strutilxx.h"
[2512]11#include "cexpre.h"
12#include "rpneval.h"
[2446]13#include "srandgen.h"
14
15
[2483]16namespace SOPHYA {
[2518]17
18// Differents code de retour specifiques
19#define CMD_RETURN_RC 99900
20#define CMD_BREAK_RC 99990
21#define CMD_BREAKEXE_RC 99999
22
[2446]23// ------------------------------------------------------------
24// Bloc de commandes (Foreach, ...)
25// Classe CommanderBloc
26// ------------------------------------------------------------
27/*!
[2598]28 \internal
[2446]29 \class SOPHYA::CommanderBloc
30 \ingroup SysTools
[2598]31 Class for internal use by class Commander to handle loops
[2446]32*/
33class CommanderBloc {
34public:
[2483]35 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat,
36 BT_ForeachLineInFile };
[2446]37
38 CommanderBloc(Commander* piac, CommanderBloc* par, string& kw, vector<string>& args);
39 ~CommanderBloc();
40 inline CommanderBloc* Parent() { return(parent); }
41 inline bool CheckOK() { return blkok; }
42 inline void AddLine(string& line)
43 { lines.push_back(line); bloclineid.push_back(lines.size()); }
44 void AddLine(string& line, string& kw);
45 inline void AddBloc(CommanderBloc* blk)
46 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
[2518]47
48 // Execution complete du bloc (boucle)
49 int Execute();
50 // Execution pour un element de bloc
51 int ExecuteOnce(string& lvv);
52
[2446]53 inline int& TestLevel() { return testlevel; }
54 inline int& LoopLevel() { return looplevel; }
55 inline bool CheckBloc()
56 { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)); }
57
58protected:
59 Commander* _commander;
60 CommanderBloc* parent;
61 bool blkok; // true -> block OK
62 BType typ; // foreach , integer loop, float loop, test
63 string varname;
[2483]64 string filename; // forinfile bloc
[2446]65 vector<string> strlist;
66 vector<string> lines;
67 vector<CommanderBloc *> blocs;
68 vector<int> bloclineid;
69 int i1,i2,di;
70 float f1,f2,df;
71 int testlevel; // niveau d'imbrication des if
72 int looplevel; // niveau d'imbrication des for/foreach
73 bool scrdef; // true -> commande defscript ds for/foreach
74};
75
76/* --Methode-- */
77CommanderBloc::CommanderBloc(Commander* piac, CommanderBloc* par, string& kw, vector<string>& args)
78{
79_commander = piac;
80parent = par;
81blkok = false;
82typ = BT_None;
83i1 = 0; i2 = -1; di = 1;
84f1 = 0.; f2 = -1.; df = 1.;
85testlevel = looplevel = 0;
86scrdef = false;
87
88if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
[2483]89if ((kw != "foreach") && (kw != "for") && (kw != "forinfile")) return;
[2518]90if (!piac->CheckVarName(args[0])) return;
91varname = args[0];
92
[2446]93//if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
[2483]94if (kw == "forinfile") {
95 filename = args[1];
[2518]96 typ = BT_ForeachLineInFile;
[2483]97 blkok = true;
98}
99else if (kw == "foreach" ) { // This is a foreach bloc with string list
[2518]100 if ( (args[1] == "(") && (args[args.size()-1] == ")") ) {
101 // foreach varname ( w1 w2 w3 ... )
102 for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
103 }
104 else {
105 // foreach varname WordVectorName
106 if (!piac->GetVar(args[1], strlist)) return;
107 }
108 if (strlist.size() < 1) return;
[2446]109 typ = BT_ForeachList;
110 blkok = true;
[2483]111}
[2446]112else { // This is an integer or float loop
113 size_t l = args[1].length();
114 size_t p = args[1].find(':');
115 size_t pp = args[1].find('.');
116 bool fl = (pp < l) ? true : false; // Float loop or integer loop
117 if (p >= l) return; // Syntaxe error
118 string a1 = args[1].substr(0, p);
119 string aa = args[1].substr(p+1);
120 p = aa.find(':');
121 string a2, a3;
122 bool hasa3 = false;
123 if (p < aa.length() ) {
124 a2 = aa.substr(0,p);
125 a3 = aa.substr(p+1);
126 hasa3 = true;
127 }
128 else a2 = aa;
129 if (fl) {
130 typ = BT_ForeachFloat;
131 blkok = true;
132 f1 = atof(a1.c_str());
133 f2 = atof(a2.c_str());
134 if (hasa3) df = atof(a3.c_str());
135 else df = 1.;
136 }
137 else {
138 typ = BT_ForeachInt;
139 blkok = true;
140 i1 = atoi(a1.c_str());
141 i2 = atoi(a2.c_str());
142 if (hasa3) di = atoi(a3.c_str());
143 else di = 1;
144 }
145 }
146}
147
148/* --Methode-- */
149CommanderBloc::~CommanderBloc()
150{
151for(int k=0; k<blocs.size(); k++) delete blocs[k];
152}
153
154/* --Methode-- */
155void CommanderBloc::AddLine(string& line, string& kw)
156{
157 AddLine(line);
158 if (kw == "if") testlevel++;
159 else if (kw == "endif") testlevel--;
160 else if ((kw == "for") || (kw == "foreach")) looplevel++;
161 else if (kw == "end") looplevel--;
162 else if (kw == "defscript") scrdef = true;
163}
164
165/* --Methode-- */
[2518]166int CommanderBloc::Execute()
[2446]167{
168int k=0;
169char buff[32];
170int rcc = 0;
171
172int mxloop = _commander->GetMaxLoopLimit();
173
[2483]174if (typ == BT_ForeachLineInFile) { // foreach line in file loop
[2518]175 ifstream is(filename.c_str());
176 char buff[256];
177 string line;
178 while (!is.eof()) {
179 rcc = 0;
180 is.clear();
181 is.getline(buff, 256);
182 line += buff;
183 if (is.good()) {
184 rcc = ExecuteOnce(line);
185 line = "";
186 }
187 if (rcc == CMD_BREAKEXE_RC) return rcc;
188 else if (rcc == CMD_BREAK_RC) break;
189 }
[2483]190}
[2518]191else if (typ == BT_ForeachList) { // foreach string loop
[2446]192 for(k=0; k<strlist.size(); k++) {
[2518]193 rcc = ExecuteOnce(strlist[k]);
194 if (rcc == CMD_BREAKEXE_RC) return rcc;
195 else if (rcc == CMD_BREAK_RC) break;
196 }
197}
198else if (typ == BT_ForeachInt) { // Integer loop
[2446]199 for(int i=i1; i<i2; i+=di) {
200 k++;
201 if ((mxloop>0) && (k > mxloop)) {
202 cout << ">>> Maximum CommanderBloc loop limit ("<< mxloop << ") -> break " << endl;
203 break;
204 }
[2518]205 sprintf(buff, "%d", i);
206 string lvv = buff;
207 rcc = ExecuteOnce(lvv);
208 if (rcc == CMD_BREAKEXE_RC) return rcc;
209 else if (rcc == CMD_BREAK_RC) break;
[2446]210 }
[2518]211}
212else if (typ == BT_ForeachFloat) { // float loop
213 for(double f=f1; f<f2; f+=df) {
[2446]214 k++;
215 if ((mxloop>0) && (k > mxloop)) {
216 cout << ">>> Maximum CommanderBloc loop limit ("<< mxloop << ") -> break " << endl;
217 break;
218 }
[2518]219 sprintf(buff, "%g", f);
220 string lvv = buff;
221 rcc = ExecuteOnce(lvv);
222 if (rcc == CMD_BREAKEXE_RC) return rcc;
223 else if (rcc == CMD_BREAK_RC) break;
[2446]224 }
[2518]225}
226return(rcc);
227}
[2446]228
[2518]229/* --Methode-- */
230int CommanderBloc::ExecuteOnce(string& lvv)
231{
232 int kj=0;
233 int kk=0;
234 int rcc = 0;
235 _commander->SetVar(varname, lvv);
236 for(kj=0; kj<bloclineid.size(); kj++) {
237 rcc = 0;
238 kk = bloclineid[kj];
239 if (kk > 0)
240 rcc = _commander->Interpret(lines[kk-1]);
241 else
242 rcc = blocs[-kk-1]->Execute();
243 if (rcc == CMD_BREAKEXE_RC) return (rcc);
244 if (rcc == CMD_BREAK_RC) break;
245 }
246 return rcc;
[2446]247}
248
249// ---------------------------------------------------------------
250// Classe CommanderScript
251// Definition et execution d'un script de Commander
252// script : Une liste de commande Commander - Lors de l'execution,
253// les variables-argument $# $0 $1 sont definies.
254// ---------------------------------------------------------------
255
256/*!
[2598]257 \internal
[2446]258 \class SOPHYA::CommanderScript
259 \ingroup SysTools
[2598]260 Class for internal use by class Commander to handle functions
[2446]261 or scripts
262*/
263
264class CommanderScript {
265public:
266 CommanderScript(Commander* piac, string const& name, string const& comm);
267 virtual ~CommanderScript();
268
269 void AddLine(string& line, string& kw);
270 virtual int Execute(vector<string>& args);
271
272 inline string& Name() { return mName; }
273 inline string& Comment() { return mComm; }
274 inline int& TestLevel() { return testlevel; }
275 inline int& LoopLevel() { return looplevel; }
276 inline bool CheckScript()
277 { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)&&fgok); }
278
279protected:
280 Commander* _commander;
281 string mName;
282 string mComm;
283 vector<string> lines;
284 int testlevel; // niveau d'imbrication des if
285 int looplevel; // niveau d'imbrication des for/foreach
286 bool scrdef; // true -> commande defscript ds for/foreach
287 bool fgok; // Script name OK
288
289};
290
291/* --Methode-- */
292CommanderScript::CommanderScript(Commander* piac, string const& name,
293 string const& comm)
294{
295_commander = piac;
296testlevel = looplevel = 0;
297scrdef = false;
298mName = name;
299if (!isalpha(name[0])) fgok = false;
300else fgok = true;
301mComm = comm;
302}
303
304/* --Methode-- */
305CommanderScript::~CommanderScript()
306{
307}
308
309/* --Methode-- */
310void CommanderScript::AddLine(string& line, string& kw)
311{
312 if (kw == "if") testlevel++;
313 else if (kw == "endif") testlevel--;
314 else if ((kw == "for") || (kw == "foreach")) looplevel++;
315 else if (kw == "end") looplevel--;
316 else if (kw == "defscript") scrdef = true;
317 lines.push_back(line);
318}
319
320/* --Methode-- */
321int CommanderScript::Execute(vector<string>& args)
322{
[2518]323 int rcc;
[2446]324 if (!CheckScript()) return(-1);
325 cout << " CommanderScript::Execute() - Executing script " << Name() << endl;
326 for(int k=0; k<lines.size(); k++) {
[2518]327 rcc = _commander->Interpret(lines[k]);
328 if ( (rcc == CMD_BREAKEXE_RC) || (rcc == CMD_RETURN_RC) ) break;
[2446]329 }
[2518]330 return(rcc);
[2446]331}
332
333// ------------------------------------------------------------
334// Classe Commander
335// ------------------------------------------------------------
336typedef void (* DlModuleInitEndFunction) ();
337
338/*!
[2598]339 \class Commander
[2446]340 \ingroup SysTools
[2598]341 \brief Simple command interpreter
342
343 This Simple command interpreter with c-shell like syntax
344 can be used to add scripting capabilities
345 to applications.
346
347 Although the insterpreter has many limitations compared to
348 c-shell, or Tcl , it provides some interesting possibilities:
349
350 - Extended arithmetic operations (c-like and RPN)
351 - Simple and vector variables
352 - Script definition
353 - Dynamic Load
354
355 \sa CmdExecutor CExpressionEvaluator RPNExpressionEvaluator
356
357 Usage example:
358 \code
359 #include "commander.h"
360 ...
361 Commander cmd;
362 char* ss[3] = {"foreach f ( AA bbb CCCC ddddd )", "echo $f" , "end"};
363 for(int k=0; k<3; k++) {
364 string line = ss[k];
365 cmd.Interpret(line);
366 }
367 \endcode
[2446]368*/
369
370#define _MAGENTA_ 1
371
372static Commander* cur_commander = NULL;
373/* --Methode-- */
[2598]374//! Default constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
[2446]375Commander::Commander()
376{
377system("cp history.pic hisold.pic");
378hist.open("history.pic");
379histon = true;
380trace = false; timing = false;
381gltimer = NULL;
382felevel = 0;
383
384mulinecmd = "";
385mulinefg = false;
386spromptmul = "Cmd> ";
387SetCurrentPrompt(spromptmul);
[2483]388SetDefaultPrompt(spromptmul);
[2446]389curscript = NULL;
390
[2483]391_xstatus = 0;
392 _retstr = "";
393
[2518]394// Controle du flot d'execution
395 fgexebrk = false;
396
[2446]397CmdBlks.push(NULL);
398list<char> xtx;
399TestsStack.push(xtx);
400curtestresult = true;
401
[2473]402// Numero de help-groupe courant - Le premier groupe ajoute aura un gid = 1
403// gid = 0 n'existe pas : c'est le groupe de toutes les commandes
404cmdgrpid = 0;
[2446]405
[2466]406string grp = "Commander";
407string gdesc = "Basic (generic) interpreter (class SOPHYA::Commander) builtin commands";
408AddHelpGroup(grp, gdesc);
409
[2446]410string kw = "Commander";
411string usage;
412usage = ">>> (Commander) Interpreter's keywords : \n";
413usage += " > set varname string # To set a variable, $varname \n";
414usage += " > unset varname # clear variable definition \n";
[2518]415usage += " > rpneval varname RPNExpression # Reverse Polish Notation evaluation \n";
416usage += " > varname = ArithmeticExpression # C-like Expression evaluation \n";
417usage += " > varname = 'String' # Set variable vname \n";
418usage += " > var2words varname wordvarname [sep] # to break varname into words \n";
419usage += " > echo string # output string \n";
420usage += " > echo2file filename string # Append the string to the specified file \n";
421usage += " > alias name string # define a command alias \n";
422usage += " > foreach varname ( string-list ) # Loop \n";
423usage += " > for varname i1:i2[:di] # Integer loop \n";
[2446]424usage += " > for varname f1:f2[:df] # Float loop \n";
[2518]425usage += " > forinfile varname FileName # Loop over lines in file \n";
[2446]426usage += " > end # end loops \n";
427usage += " > if ( test ) then # Conditional test : a == != < > <= >= b \n";
428usage += " > else # Conditional \n";
429usage += " > endif # End of conditional if bloc \n";
430usage += " > break # Delete (clears) all test and loop blocs \n";
431usage += " > return # Stops command execution from a file \n";
432usage += " > defscript endscript # Command script definition \n";
433usage += " > listvars # List of variable names and values \n";
434usage += " > listalias # List of alias names and values \n";
435usage += " > listcommands # List of all known commands \n";
436usage += " > listscripts # List of all known scripts \n";
437usage += " > clearcript # Clear a script definition \n";
438usage += " > exec filename # Execute commands from file \n";
[2518]439usage += " > help <command_name> # <command_name> usage info \n";
440usage += " > sleep nsec # sleep nsec seconds \n";
441usage += " > readstdin varname # reads a line from stdin into $varname \n";
[2446]442usage += " > timingon timingoff traceon traceoff \n";
443RegisterHelp(kw, usage, grp);
444
445kw = "RPNEvaluator";
446usage = " Reverse Polish Notation (HP calculator like) expression evaluation \n";
447usage += " >> Stack: \n";
448usage += " ... (4) (3) z=(2) y=(1) x=(0)=Stack.Top() \n";
449usage += " >> Examples: \n";
450usage += " - sin(PI/6): pi 6 / sin \n";
451usage += " - 1*2*...*5: 1 2 3 4 5 product \n";
452usage += " - x=x+y: x = $x $y * \n";
453usage += " >>> Stack operations : \n";
454usage += " print x<>y pop push (duplicate x) \n";
455usage += " >>> Constants (Cst pushed to stack): \n";
456usage += " pi e \n";
457usage += " >>> Arithmetic operators (x,y) --> x@y \n";
458usage += " + - * / % ( (int)y % (int)x )\n";
459usage += " >>> F(X): x --> F(x) \n";
460usage += " chs sqrt sq log log10 exp \n";
461usage += " fabs floor ceil \n";
462usage += " cos sin tan acos asin atan deg2rad rad2deg \n";
463usage += " >>> F(X,Y): (x,y) --> F(x,y) \n";
464usage += " pow atan2 \n";
465usage += " >>> F(): random number generators \n";
466usage += " rand (flat 0..1) norand (normal/gaussian) \n";
467usage += " >>> Stack sum/product/mean/sigma/sigma^2 \n";
468usage += " sum product mean sigma sigma2 sigmean (y->sigma x->mean) \n";
469RegisterHelp(kw, usage, grp);
470
471kw = "autoiniranf";
472usage = "> Automatic random number generator initialisation\n";
473usage += " by Auto_Ini_Ranf(int lp) \n";
474usage += " Usage: autoiniranf";
475RegisterCommand(kw, usage, NULL, grp);
476
477kw = "shell execute";
478usage = "> shell command_string # Execute shell command\n";
479usage += "> cshell command_string # Execute cshell command\n";
480usage += "---Examples:\n";
481usage += " > shell ls\n";
482usage += " > cshell echo '$LD_LIBRARY_PATH'; map2cl -h; ls\n";
483usage += " > shell myfile.csh [arg1] [arg2] [...]\n";
484usage += " (where the first line of \"myfile.csh\" is \"#!/bin/csh\")\n";
485RegisterCommand(kw, usage, NULL, grp);
486
487
488AddInterpreter(this);
489curcmdi = this;
490}
491
492/* --Methode-- */
493Commander::~Commander()
494{
495hist.close();
496if (gltimer) { delete gltimer; gltimer = NULL; }
497Modmap::iterator it;
498for(it = modmap.begin(); it != modmap.end(); it++) {
499 string name = (*it).first + "_end";
500 DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
501 if (fend) fend();
502 delete (*it).second;
503 }
504
505for(ScriptList::iterator sit = mScripts.begin();
506 sit != mScripts.end(); sit++) delete (*sit).second;
507
508if (cur_commander == this) cur_commander = NULL;
509}
510
511/* --Methode-- */
512Commander* Commander::GetInterpreter()
513{
514return(cur_commander);
515}
516
517/* --Methode-- */
[2598]518//! Returns the string \c Commander as the interpreter's name.
[2446]519string Commander::Name()
520{
521return("Commander");
522}
523
524/* --Methode-- */
[2598]525//! Add the \b grp help group with description \b desc.
[2466]526void Commander::AddHelpGroup(string& grp, string& desc)
[2446]527{
[2466]528 int gid;
529 CheckHelpGrp(grp, gid, desc);
530}
531
532/* --Methode-- */
[2598]533/*!
534 \brief Register a command executor associated with a given keyword.
535 \param keyw : keyword identifying the command
536 \param usage : the command help and usage information
537 \param ce : CmdExecutor pointer for this a command. The same object can be registered
538 multiple time for different commands.
539 \param grp : The help group corresponding to this command.
540*/
[2466]541void Commander::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string& grp)
542{
[2446]543if (!ce) {
544 RegisterHelp(keyw, usage, grp);
545 return;
546 }
[2466]547int gid;
548CheckHelpGrp(grp,gid);
[2446]549cmdex cme;
550cme.group = gid;
551cme.us = usage;
552cme.cex = ce;
553cmdexmap[keyw] = cme;
554}
555
556/* --Methode-- */
[2598]557/*!
558 \brief Register a help text.
559 \param keyw : help keyword
560 \param usage : help text
561 \param grp : help group
562*/
[2446]563void Commander::RegisterHelp(string& keyw, string& usage, string& grp)
564{
[2466]565int gid;
566CheckHelpGrp(grp,gid);
[2446]567cmdex cme;
568cme.group = gid;
569cme.us = usage;
570cme.cex = NULL;
571helpexmap[keyw] = cme;
572}
573
574/* --Methode-- */
[2466]575bool Commander::CheckHelpGrp(string& grp, int& gid, string& desc)
[2446]576{
[2466]577gid = 0;
[2446]578CmdHGroup::iterator it = cmdhgrp.find(grp);
579if (it == cmdhgrp.end()) {
[2466]580 cmdgrpid++; gid = cmdgrpid;
581 hgrpst hgs; hgs.gid = gid; hgs.desc = desc;
582 cmdhgrp[grp] = hgs;
583 return true;
[2446]584 }
[2466]585else {
586 if (desc.length() > 0) (*it).second.desc = desc;
587 gid = (*it).second.gid;
588 return false;
[2446]589}
[2466]590}
[2446]591
592
593/* --Methode-- */
[2598]594/*!
595 \brief Dynamic loader for modules
596
597 A module is a shared library extending the application functionalities.
598 Typically, a module adds new commands to the interpreter. Once loaded,
599 the module is activated (initialized) by calling a function with the
600 name \b modulename_init . This function should be declared extern C
601 to avoid C++ name mangling. A cleanup function \b modulename_end
602 is called by the Commander destructor.
603
604 \param fnameso : Shared library name containing the module functions
605 and classes.
606 \param name : Module name. This string is used to form module
607 initializer and cleanup function name \c name_init \c name_end
608*/
[2446]609void Commander::LoadModule(string& fnameso, string& name)
610{
611PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
612if (dynlink == NULL) {
613 cerr << "Commander/LoadModule_Error: Pb opening SO " << fnameso << endl;
614 return;
615 }
616string fname = name + "_init";
617DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
618if (!finit) {
619 cerr << "Commander/LoadModule_Error: Pb linking " << fname << endl;
620 return;
621 }
622cout << "Commander/LoadModule_Info: Initialisation module" << name
623 << " " << fname << "() ..." << endl;
624finit();
625modmap[name] = dynlink;
626return;
627}
628
629/* --Methode-- */
[2598]630//! Declare a new interpreter
[2446]631void Commander::AddInterpreter(CmdInterpreter * cl)
632{
633if (!cl) return;
634interpmap[cl->Name()] = cl;}
635
636/* --Methode-- */
[2598]637//! Select an interpreter by its name. The corresponding Interpret method is then called
[2446]638void Commander::SelInterpreter(string& name)
639{
640InterpMap::iterator it = interpmap.find(name);
641if (it == interpmap.end()) return;
642curcmdi = (*it).second;
643}
644
645
646
647/* Fonction */
648static string GetStringFrStdin(Commander* piac)
649{
650char buff[128];
651fgets(buff, 128, stdin);
652buff[127] = '\0';
653return((string)buff);
654}
655
656/* --Methode-- */
[2598]657/*!
658 \brief Method which has to be invoked to interpret a giev command line or string.
659*/
[2446]660int Commander::Interpret(string& s)
661{
662int rc = 0;
663ScriptList::iterator sit;
664
665// On saute de commandes vides
666size_t l;
667l = s.length();
668if (!mulinefg && (l < 1)) return(0);
669
670// On enregistre les commandes
671if (histon) hist << s << endl;
672
673if (s[0] == '#') return(0); // si c'est un commentaire
674
675// Logique de gestion des lignes suite
676// un \ en derniere position indique la presence d'une ligne suite
677size_t lnb = s.find_last_not_of(' ');
678if (s[lnb] == '\\' ) { // Lignes suite ...
679 mulinecmd += s.substr(0,lnb);
680 if (!mulinefg) {
681 spromptmul = GetCurrentPrompt();
682 SetCurrentPrompt("...? ");
683 mulinefg = true;
684 }
685 return(0);
686}
687
688if (mulinefg) { // Il y avait des lignes suite
689 s = mulinecmd + s;
690 l = s.length();
691 mulinecmd = "";
692 mulinefg = false;
693 SetCurrentPrompt(spromptmul);
694}
695
696// Removing leading blanks
697size_t p,q;
698
699// On enleve le dernier caractere, si celui-ci est \n
700if (s[l-1] == '\n') s[l-1] = '\0';
701p=s.find_first_not_of(" \t");
702if (p < l) s = s.substr(p);
703// >>>> Substitution d'alias (1er mot)
704CmdStrList::iterator it;
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);
715 p = 0;
716 q = s.find_first_of(" ");
717 }
718
719// >>>> Separating keyword
720string toks,kw;
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
725// les mot-cle end else endif doivent etre le seul mot de la ligne
726if ( (kw == "end") || (kw == "else") || (kw == "endif") || (kw == "endscript") ) {
727 size_t ltk = toks.length();
728 if (toks.find_first_not_of(" \t") < ltk) {
729 cerr << "Commander::Interpret()/syntax error near end else endif endscript \n"
730 << "line: " << s << endl;
[2483]731 _xstatus = 91;
732 return(91);
[2446]733 }
734}
735
736// On verifie si on est en train de definir un script
737if (curscript) {
738 if (kw == "endscript") {
739 if (curscript->CheckScript()) {
740 sit = mScripts.find(curscript->Name());
741 if (sit != mScripts.end()) {
742 cout << "Commander::Interpret() replacing script "
743 << curscript->Name() << endl;
744 CommanderScript* scr = mScripts[curscript->Name()];
745 mScripts.erase(sit);
746 delete scr;
747 }
748 cout << "Commander::Interpret() Script " << curscript->Name()
749 << " defined successfully" << endl;
750 mScripts[curscript->Name()] = curscript;
751 SetCurrentPrompt("Cmd> ");
752 curscript = NULL;
[2483]753 _xstatus = 0;
[2446]754 return(0);
755 }
756 else {
757 cout << "Commander::Interpret() Error in Script " << curscript->Name()
758 << " definition " << endl;
759 SetCurrentPrompt("Cmd> ");
760 curscript = NULL;
[2483]761 _xstatus = 92;
762 return(92);
[2446]763 }
764 }
765 else curscript->AddLine(s, kw);
[2483]766 _xstatus = 0;
[2446]767 return(0);
768}
769// On verifie si nous sommes dans un bloc (for , foreach)
770if (CmdBlks.top() != NULL) { // On est dans un bloc
771 if ( (kw == "for") || (kw == "foreach")) felevel++;
772 else if (kw == "end") felevel--;
773 if (felevel == 0) { // Il faut executer le bloc
774 CommanderBloc* curb = CmdBlks.top();
775 CmdBlks.top() = curb->Parent();
776 SetCurrentPrompt("Cmd> ");
777 if (!curb->CheckBloc()) {
778 cerr << "Commander::Interpret()/syntax error - unbalenced if ... endif"
779 << " within for/foreach bloc ! " << endl;
780 delete curb;
[2483]781 _xstatus = 93;
782 return(93);
[2446]783 }
784 // cout << " *DBG* Executing bloc " << endl;
785 bool ohv = histon;
786 histon = false;
787 if (curtestresult) {
788 // We push also CommanderBloc and testresult on the stack
789 CmdBlks.push(NULL);
790 list<char> xtx;
791 TestsStack.push(xtx);
792 curb->Execute();
793 // And CommanderBloc and TestResult from the corresponding stacks
794 PopStack(false);
795 }
[2483]796 SetCurrentPrompt(defprompt);
[2446]797 delete curb;
798 histon = ohv;
799 }
800 else CmdBlks.top()->AddLine(s, kw);
[2483]801 _xstatus = 0;
[2446]802 return(0);
803}
804else if (kw == "end") {
805 cerr << "Commander::Interpret()/syntax error - end outside for/foreach bloc \n"
806 << "line: " << s << endl;
[2483]807 _xstatus = 94;
808 return(94);
[2446]809}
810
811// Sommes-nous dans un bloc de test if then else
812if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if
813 if (kw == "else") {
814 if ((*tresit) & 2) {
815 cerr << "Commander::Interpret()/syntax error - multiple else in if bloc \n"
816 << "line: " << s << endl;
[2483]817 _xstatus = 95;
818 return(95);
[2446]819 }
820 else {
821 const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
822 if ((*tresit)&1) curtestresult = false;
823 SetCurrentPrompt(npr);
824 (*tresit) |= 2;
[2483]825 _xstatus = 0;
[2446]826 return(0);
827 }
828 }
829 else if (kw == "endif") {
830 list<char>::iterator dbit = tresit;
831 tresit--;
832 TestsStack.top().erase(dbit);
833 const char * npr = "Cmd> ";
834 if (TestsStack.top().size() > 1) {
835 curtestresult = true;
836 list<char>::iterator it;
837 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
838 // Si on n'est pas ds le else et le if est faux
839 if ( !((*it)&2) && !((*it)&1) ) curtestresult = false;
840 // Si on est ds else et le if etait vrai !
841 if ( ((*it)&2) && ((*it)&1) ) curtestresult = false;
842 if (!curtestresult) break;
843 }
844
845 if (!((*tresit)&2))
846 npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
847 else
848 npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
849 }
850 else curtestresult = true;
851 SetCurrentPrompt(npr);
[2483]852 _xstatus = 0;
[2446]853 return(0);
854 }
855}
856else if ((kw == "else") || (kw == "endif")) {
857 cerr << "Commander::Interpret()/syntax error - else,endif outside if bloc \n"
858 << "line: " << s << endl;
[2483]859 _xstatus = 91;
860 return(91);
[2446]861}
862
863bool fgcont = true;
864if (TestsStack.top().size() > 0) { // Resultat de if ou else
865 list<char>::iterator it;
866 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
867 // Si on n'est pas ds le else et le if est faux
868 if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
869 // Si on est ds else et le if etait vrai !
870 if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
871 if (!fgcont) break;
872 }
873}
874
[2483]875if ((!fgcont) && (kw != "if")) {
876 _xstatus = 0;
877 return(0);
878}
[2446]879
880
881// Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
[2518]882if (kw == "break") return CMD_BREAK_RC;
[2483]883else if (kw == "return") {
884 _retstr = toks;
[2518]885 return CMD_RETURN_RC;
[2483]886}
[2446]887
888// Nous ne sommes donc pas dans un bloc .... Substitution de variables
889string s2;
890int rcs ;
891
892rcs = SubstituteVars(s, s2);
893if (rcs) {
894 cerr << "Commander::Interpret()/syntax error in SubstituteVars() \n"
895 << "line: " << s << endl;
[2483]896 _xstatus = 99;
897 return(99);
[2446]898}
899// >>>> Separating keyword and tokens
900vector<string> tokens;
[2518]901vector<bool> qottoks;
[2446]902/* decoupage en mots */
[2518]903LineToWords(s2, kw, tokens, qottoks, toks, true);
[2446]904
905// Si c'est un for/foreach, on cree un nouveau bloc
[2483]906if ((kw == "foreach") || (kw == "for") || (kw == "forinfile") ) {
[2446]907 // cout << " *DBG* We got a foreach... " << endl;
908 CommanderBloc* bloc = new CommanderBloc(this, CmdBlks.top(), kw, tokens);
909 if (!bloc->CheckOK()) {
910 cerr << "Commander::Interpret() for/foreach syntax Error ! " << endl;
911 delete bloc;
[2483]912 _xstatus = 91;
913 return(91);
[2446]914 }
915 felevel++;
916 if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
917 else SetCurrentPrompt("for...> ");
918 CmdBlks.top() = bloc;
919 // cout << " *DBG* New Bloc created ... " << endl;
920 return(0);
921 }
922else if (kw == "if") { // Un test if
923 bool restst = true;
924 int rct = EvaluateTest(tokens, s, restst);
925 if (rct) {
926 cerr << "Commander::Interpret() if syntax Error ! " << "line: " << s << endl;
[2483]927 _xstatus = 91;
928 return(91);
[2446]929 }
930 char res_tst = (restst) ? 1 : 0;
931 TestsStack.top().push_back(res_tst);
932 if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin();
933 else tresit++;
934 const char * npr = (restst) ? "if-T> " : "if-F> ";
935 SetCurrentPrompt(npr);
936}
937else if ((tokens.size() > 0) && (tokens[0] == "=")) {
[2512]938 // x = Expression
[2518]939 if (qottoks[1]) { // decodage sous forme de chaine
940 SetVariable(kw, tokens[1]);
[2512]941 }
[2518]942 else {
943 try {
944 double res = 0.;
945 if (tokens.size() > 2) {
946 string sex = tokens[1];
947 for(int js=2; js<tokens.size(); js++) sex += tokens[js];
948 CExpressionEvaluator cex(sex);
949 res = cex.Value();
950 }
951 else {
952 CExpressionEvaluator cex(tokens[1]);
953 res = cex.Value();
954 }
955 char cbuff[64];
956 sprintf(cbuff,"%g",res);
957 string vv = cbuff;
958 SetVariable(kw, vv);
959 }
960 catch (CExprException& cexerr) {
961 cerr << "Commander::Interpret() evaluation Error : \n " << "line: " << s
962 << " \n Msg=" << cexerr.Msg() << endl;
963 _xstatus = 98;
964 return(98);
965 }
[2446]966 }
967}
968else if (kw == "defscript") { // definition de script
969 if (tokens.size() > 0) {
970 if (tokens.size() < 2) tokens.push_back("");
971 curscript = new CommanderScript(this, tokens[0], tokens[1]);
972 SetCurrentPrompt("Script...> ");
973 return(0);
974 }
975 else {
976 cerr << "Commander::Interpret() No script name in defscript" << "line: " << s << endl;
[2483]977 _xstatus = 91;
978 return(91);
[2446]979 }
980}
981else {
982 // Si c'est le nom d'un script
983 sit = mScripts.find(kw);
984 if (sit != mScripts.end()) {
985 bool ohv = histon;
986 histon = false;
987 tokens.insert(tokens.begin(), kw);
988 PushStack(tokens);
989 (*sit).second->Execute(tokens);
990 PopStack(true);
991 histon = ohv;
992 }
993 // Execution de commandes
994 else rc = ExecuteCommandLine(kw, tokens, toks);
[2483]995 _xstatus = rc;
[2446]996 return(rc);
997}
998// cout << "Commander::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
999// for(int ii=0; ii<tokens.size(); ii++)
1000// cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
1001
1002return(0);
1003}
1004
1005
1006/* --Methode-- */
1007int Commander::LineToWords(string& line, string& kw, vector<string>& tokens,
[2518]1008 vector<bool>& qottoks, string& toks, bool uq)
[2446]1009{
1010if (line.length() < 1) return(0);
1011int nw = 1;
1012size_t p = line.find_first_not_of(" ");
1013line = line.substr(p);
1014p = 0;
1015size_t q = line.find_first_of(" ");
1016size_t l = line.length();
1017
1018if (q < l)
1019 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
1020else { kw = line.substr(p,l-p); toks = ""; }
1021
1022q = 0;
1023while (q < l) {
[2518]1024 bool swq = false; // true -> chaine delimite par ' ou "
[2446]1025 p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
1026 if (p>=l) break;
1027 if ( uq && ((toks[p] == '\'') || (toks[p] == '"')) ) {
1028 q = toks.find(toks[p],p+1);
1029 if (q>=l) {
1030 cerr << "Commander::LineToWords/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
1031 return(-1);
1032 }
[2518]1033 p++; swq = true;
[2446]1034 }
1035 else {
1036 q = toks.find_first_of(" \t",p); // la fin du token;
1037 }
1038 string token = toks.substr(p,q-p);
[2518]1039 tokens.push_back(token);
1040 qottoks.push_back(swq);
1041 nw++;
[2446]1042 }
1043
1044return(nw);
1045}
1046
1047/* --Methode-- */
1048int Commander::SubstituteVars(string & s, string & s2)
1049// Variable substitution
1050{
1051
1052int iarr = -1; // index d'element de tableau
1053size_t p,q,q2,q3,l;
1054
1055s2="";
1056p = 0;
1057l = s.length();
1058string vn, vv;
1059while (p < l) {
1060 iarr = -1;
1061 q = s.find('$',p);
1062 if (q > l) break;
1063 q2 = s.find('\'',p);
1064 if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
1065 q2 = s.find('\'',q2+1);
1066 if (q2 >= l) {
1067 cerr << " Syntax error - Unbalenced quotes !!! " << endl;
1068 return(1);
1069 }
1070 s2 += s.substr(p, q2-p+1);
1071 p = q2+1; continue;
1072 }
1073 // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
1074 if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
1075 s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
1076 continue;
1077 }
1078 if (q >= l-1) {
1079 cerr << " Syntax error - line ending with $ !!! " << endl;
1080 return(2);
1081 }
1082 vn = "";
1083 if ( s[q+1] == '{' ) { // Variable in the form ${name}
1084 q2 = s.find('}',q+1);
1085 if (q2 >= l) {
1086 cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
1087 return(3);
1088 }
1089 vn = s.substr(q+2,q2-q-2);
1090 q2++;
1091 }
1092 else if ( s[q+1] == '(' ) { // Variable in the form $(name)
1093 q2 = s.find(')',q+1);
1094 if (q2 >= l) {
1095 cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl;
1096 return(3);
1097 }
1098 vn = s.substr(q+2,q2-q-2);
1099 q2++;
1100 }
1101 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
1102 q2 = s.find(']',q+1);
1103 if (q2 >= l) {
1104 cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
1105 return(4);
1106 }
1107 vn = s.substr(q+2,q2-q-2);
[2483]1108 if (!Var2Str(vn, vv)) return(5);
[2446]1109 vn = vv;
1110 q2++;
1111 }
1112 else {
1113 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
1114 if (q2 > l) q2 = l;
1115 q3 = q2;
1116 vn = s.substr(q+1, q2-q-1);
1117 // Si variable de type $varname[index] : element de tableau
1118 if ((q2 < l) && (s[q2] == '[') ) {
1119 q3 = s.find_first_of("]",q2+1);
1120 string sia = s.substr(q2+1, q3-q2-1);
1121 if (sia.length() < 1) {
1122 cerr << " Syntax error - in $varname[index] : $"
1123 << vn << "[" << sia <<"]" << endl;
1124 return(4);
1125 }
1126 if (isalpha(sia[0])) {
1127 string sia2;
[2483]1128 if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
[2446]1129 cerr << " Syntax error - in $varname[index] : $"
1130 << vn << "[" << sia <<"]" << endl;
1131 return(4);
1132 }
1133 sia = sia2;
1134 }
1135 int rcdia = ctoi(sia.c_str(), &iarr);
1136 if (rcdia < 0) {
1137 cerr << " Syntax error - in $varname[iarr] : $"
1138 << vn << "[" << sia <<"]" << endl;
1139 return(4);
1140 }
1141 }
1142 }
1143 if (iarr < 0) {
[2483]1144 if (!Var2Str(vn, vv)) return(5);
[2446]1145 s2 += (s.substr(p, q-p) + vv);
1146 p = q2;
1147 }
1148 else {
[2483]1149 if (! Var2Str(vn, iarr, vv) ) {
[2446]1150 cerr << " Substitution error - word index out of range in "
1151 << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
1152 return(4);
1153 }
[2483]1154 else s2 += (s.substr(p, q-p) + vv);
[2446]1155 p = q3+1;
1156 }
1157}
1158if (p < l) s2 += s.substr(p);
1159
1160p = s2.find_first_not_of(" \t");
1161if (p < l) s2 = s2.substr(p);
1162
1163return(0);
1164}
1165
1166/* --Methode-- */
[2483]1167bool Commander::Var2Str(string const & vn, string & vv)
[2446]1168{
1169if (vn.length() < 1) {
[2518]1170 cerr << " Commander::Var2Str/Error: length(varname=" << vn << ")<1 !" << endl;
[2446]1171 vv = ""; return(false);
1172}
1173// Variable de type $# $0 $1 ... (argument de .pic ou de script)
1174int ka = 0;
[2483]1175char buff[32];
1176
[2446]1177if (vn == "#") {
1178 if (ArgsStack.empty()) {
[2518]1179 cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
[2446]1180 << " ($" << vn << ")" << endl;
1181 vv = ""; return(false);
1182 }
1183 char buff[32];
1184 long an = ArgsStack.top().size();
[2518]1185 if (an > 0) an--; // Pour se conformer a l'usage de csh : Nb args sans le $0
[2446]1186 sprintf(buff,"%ld", an);
1187 vv = buff; return(true);
1188}
[2518]1189else if (vn == "*") {
1190 if (ArgsStack.empty()) {
1191 cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
1192 << " ($" << vn << ")" << endl;
1193 vv = ""; return(false);
1194 }
1195 vv = ArgsStack.top()[0];
1196 for(int ssk=1; ssk<ArgsStack.top().size(); ssk++) vv += ArgsStack.top()[ssk];
1197 return(true);
1198}
[2446]1199else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
1200 if (ArgsStack.empty()) {
[2518]1201 cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
[2446]1202 << " ($" << vn << ")" << endl;
1203 vv = ""; return(false);
1204 }
1205 if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
[2518]1206 cerr << " Commander::Var2Str/Error: ArgsStack index <0 or >=args.size() ! "
[2446]1207 << " ($" << vn << ")" << endl;
1208 vv = ""; return(false);
1209 }
1210 vv = ArgsStack.top()[ka]; return(true);
1211}
1212else if (vn[0] == '#') { // Variable de type $#vname --> size(vname)
[2483]1213 CmdVarList::iterator it = variables.find(vn.substr(1));
1214 if (it == variables.end()) {
[2518]1215 cerr << " Commander::Var2Str/Error #vname Undefined variable "
[2446]1216 << vn << " ! " << endl;
1217 vv = ""; return(false);
1218 }
[2483]1219 sprintf(buff,"%d", (int)(*it).second.size());
[2446]1220 vv = buff; return(true);
[2483]1221}
1222else if (vn == "status") {
1223 sprintf(buff,"%d", _xstatus);
1224 vv = buff;
1225 return true;
1226}
[2518]1227else if ((vn == "retstr") || (vn == "retval")) {
[2483]1228 vv = _retstr;
1229 return true;
1230}
1231else { // Variable de l'interpreteur, ou de l'environnement application , env. global
1232 if (GetVar(vn, vv)) return true;
1233 else if (GetVarApp(vn, vv)) return true;
1234 else if (GetVarEnv(vn, vv)) return true;
1235 else {
[2518]1236 cerr << " Commander::Var2Str/Error Undefined variable "
[2446]1237 << vn << " ! " << endl;
[2483]1238 vv = ""; return false;
[2446]1239 }
1240}
1241
[2483]1242return false;
[2446]1243}
[2483]1244
[2446]1245/* --Methode-- */
[2483]1246bool Commander::SetVariable(string const & vn, string const & vv)
[2446]1247{
[2483]1248 // On verifie si le nom est de type vname[idx]
1249 size_t p,q,l;
1250 l = vn.length();
1251 p = vn.find('[');
1252 if (p < l) {
1253 q = vn.find(']');
1254 if (q != (l-1)) {
1255 cout << "Commander::Str2Var/SetVar() - Bad varname with []: "
1256 << vn << endl;
1257 return false;
1258 }
1259 string vna = vn.substr(0, p);
1260 string sia = vn.substr(p+1, q-(p+1));
1261 if (isalpha(sia[0])) {
1262 string sia2;
1263 if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
1264 cerr << "Commander::Str2Var/SetVar() Syntax error- varname[index]:"
1265 << vn << endl;
1266 return false;
1267 }
1268 sia = sia2;
1269 }
1270 int iarr;
1271 int rcdia = ctoi(sia.c_str(), &iarr);
1272 if (rcdia < 0) {
1273 cerr << "Commander::Str2Var/SetVar() Syntax error- varname[iarr]: "
1274 << vn << endl;
1275 return false;
1276 }
1277 return SetVar(vna, iarr, vv);
[2446]1278 }
[2483]1279 else {
1280 if (vn == "status") {
1281 _xstatus = atoi(vv.c_str());
1282 return true;
1283 }
1284 else if (vn == "retstr") {
1285 _retstr = vv;
1286 return true;
1287 }
1288 else return SetVar(vn, vv);
1289 }
[2446]1290}
1291
1292/* --Methode-- */
[2483]1293bool Commander::GetVar(string const & vn, string & vv)
[2446]1294{
[2483]1295 CmdVarList::iterator it = variables.find(vn);
1296 if (it == variables.end()) {
1297 vv = "";
1298 return false;
1299 }
1300 vv = (*it).second[0];
1301 if ((*it).second.size() > 1) {
1302 for(int k=1; k<(*it).second.size(); k++) {
1303 vv += ' '; vv += (*it).second[k];
1304 }
1305 }
1306 return true;
1307}
1308
1309/* --Methode-- */
1310bool Commander::GetVar(string const & vn, int idx, string & vv)
1311{
1312 vv = "";
1313 CmdVarList::iterator it = variables.find(vn);
1314 if (it == variables.end()) return false;
1315 if ((idx < 0) || (idx > (*it).second.size()-1))
1316 return false;
1317 vv = (*it).second[idx];
1318 return true;
1319}
1320
1321/* --Methode-- */
1322bool Commander::GetVar(string const & vn, vector<string> & vv)
1323{
1324 vv.clear();
1325 // vv.erase(vv.begin(),vv.end());
1326 CmdVarList::iterator it = variables.find(vn);
1327 if (it == variables.end()) return false;
1328 vv = (*it).second;
1329 return true;
1330}
1331
1332/* --Methode-- */
1333bool Commander::SetVar(string const & vn, string const & val)
1334{
1335 if ( !CheckVarName(vn) ) {
1336 cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
[2446]1337 return(false);
1338 }
[2483]1339 bool fg = false;
1340 vector<string> nouv;
1341 nouv.push_back(val);
1342 CmdVarList::iterator it = variables.find(vn);
1343 if (it == variables.end()) variables[vn] = nouv;
1344 else {
1345 (*it).second = nouv;
1346 fg = true;
1347 }
1348 return fg;
[2446]1349}
1350
1351/* --Methode-- */
[2483]1352bool Commander::SetVar(string const & vn, int idx, string const & val)
[2446]1353{
[2483]1354 if ( !CheckVarName(vn) ) {
1355 cerr << "Commander::SetVar( " << vn << " ,idx, ...) Bad VarName " << endl;
[2446]1356 return(false);
1357 }
[2483]1358 if ((vn == "status") || (vn == "retstr")) {
1359 cerr << "Commander::SetVar(vn,idx,val) ERROR - special var status/retstr "
1360 << endl;
1361 return(false);
1362 }
1363 if (idx < 0) {
1364 cout << "Commander::SetVar(vn," << idx << ",...) Error idx < 0" << endl;
1365 return(false);
1366 }
1367 bool fg = false;
1368 CmdVarList::iterator it = variables.find(vn);
1369 if (it == variables.end()) {
1370 vector<string> nouv;
1371 for(int j=0; j<idx; j++) nouv.push_back("");
1372 nouv.push_back(val);
1373 variables[vn] = nouv;
1374 }
1375 else {
1376 if (idx >= (*it).second.size())
[2512]1377 for(int j=(*it).second.size(); j<=idx; j++) (*it).second.push_back("");
1378 (*it).second[idx] = val;
[2483]1379 fg = true;
1380 }
[2446]1381 return fg;
1382}
1383
1384/* --Methode-- */
[2483]1385bool Commander::SetVar(string const & vn, vector<string> const & val)
[2446]1386{
[2483]1387 if ( !CheckVarName(vn) ) {
1388 cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
[2446]1389 return(false);
1390 }
[2483]1391 if ((vn == "status") || (vn == "retstr")) {
1392 cerr << "Commander::SetVar(vn, vector<string>) ERROR - special var status/retstr "
1393 << endl;
1394 return(false);
1395 }
1396 bool fg = false;
1397 CmdVarList::iterator it = variables.find(vn);
1398 if (it == variables.end()) variables[vn] = val;
1399 else {
1400 (*it).second = val;
1401 fg = true;
1402 }
1403 return fg;
[2446]1404}
1405
1406/* --Methode-- */
[2483]1407bool Commander::CheckVarName(string const & vn)
[2446]1408{
[2483]1409 size_t l,k;
1410 l = vn.length();
1411 if (l < 1) return false;
1412 if (!isalpha(vn[0])) return false;
1413 for(k=1; k<l; k++)
1414 if ((!isalnum(vn[k])) && (vn[k] != '_')) return false;
1415 return true;
1416}
1417
1418/* --Methode-- */
1419bool Commander::DeleteVar(string const & vn)
1420{
1421 CmdVarList::iterator it = variables.find(vn);
1422 if (it == variables.end()) {
1423 cerr << "Commander::DeleteVar() Var " << vn << " undefined!" << endl;
1424 return false;
1425 }
1426 variables.erase(it);
1427 return true;
1428}
1429
1430/* --Methode-- */
1431void Commander::ListVar()
1432{
1433 cout << " ---- Commander::ListVar() List of defined variables ---- "
[2446]1434 << endl;
[2483]1435 CmdVarList::iterator it;
1436 for(it = variables.begin(); it != variables.end(); it++) {
1437 string vn = (*it).first;
1438 int vs = (*it).second.size();
1439 cout << vn << " -> Size= " << vs << endl;
1440 }
[2446]1441 cout << "---------------------------------------------------------- "
1442 << endl;
1443}
1444
1445/* --Methode-- */
[2483]1446bool Commander::GetVarApp(string const & vn, string & vv)
1447{
1448 vv = "";
[2518]1449 // cout << " Commander::GetVarApp() Not available ! " << endl;
[2483]1450 return false;
1451}
1452
1453/* --Methode-- */
1454bool Commander::SetVarApp(string const & vn, string const & vv)
1455{
[2518]1456 // cout << " Commander::SetVarApp() Not available ! " << endl;
[2483]1457 return false;
1458}
1459
1460/* --Methode-- */
1461bool Commander::DeleteVarApp(string const & vn)
1462{
[2518]1463 // cout << " Commander::DeleteVarApp() Not available ! " << endl;
[2483]1464 return false;
1465}
1466
1467/* --Methode-- */
1468void Commander::ListVarApp()
1469{
[2518]1470 // cout << " Commander::ListVarApp() Not available ! " << endl;
[2483]1471 return;
1472}
1473
1474
1475/* --Methode-- */
1476bool Commander::GetVarEnv(string const & vn, string & vv)
1477{
[2518]1478 char* vev = getenv(vn.c_str());
1479 if (vev) {
1480 vv = vev;
1481 return true;
1482 }
1483 else {
1484 vv = "";
1485 return false;
1486 }
[2483]1487}
1488
1489/* --Methode-- */
1490bool Commander::SetVarEnv(string const & vn, string const & vv)
1491{
[2518]1492 string pev = vn;
1493 pev += '=';
1494 pev += vv;
[2532]1495#if defined(Linux)
1496// Reza - 28/04/2004
1497// putenv de Linux ne declare pas la variable char *string const
1498// On ne doit meme pas utiliser une variable automatique
1499// J'alloue donc un nouveau tableau - mais qui va le liberer ?
1500 char* bev = new char[pev.size()+1];
1501 strcpy(bev, pev.c_str());
1502 if (putenv(bev) == 0) return true;
1503#else
[2518]1504 if (putenv(pev.c_str()) == 0) return true;
[2532]1505#endif
[2518]1506 else return false;
[2483]1507}
1508
1509/* --Methode-- */
1510bool Commander::DeleteVarEnv(string const & vn)
1511{
[2518]1512 // cout << " Commander::DeleteVarEnv() Not available ! " << endl;
[2483]1513 return false;
1514}
1515
1516/* --Methode-- */
1517void Commander::ListVarEnv()
1518{
1519 cout << " Commander::ListVarEnv() Not available ! " << endl;
1520 return;
1521}
1522
1523
1524/* --Methode-- */
[2446]1525string Commander::GetTmpDir()
1526{
1527 return("/tmp");
1528}
1529
1530/* --Methode-- */
1531void Commander::SetCurrentPrompt(const char* pr)
1532{
1533 curprompt = pr;
1534}
1535
1536/* --Methode-- */
1537void Commander::ShowMessage(const char * msg, int att)
1538{
1539 cout << msg ;
1540}
1541
1542
1543
1544/* --Methode-- */
1545int Commander::EvaluateTest(vector<string> & args, string & line, bool & res)
1546{
1547 res = true;
1548 if ((args.size() != 6) || (args[5] != "then") ||
1549 (args[0] != "(") || (args[4] != ")") ) return(1);
1550 if (args[2] == "==") res = (args[1] == args[3]);
1551 else if (args[2] == "!=") res = (args[1] != args[3]);
1552 else if (args[2] == "<")
1553 res = (atof(args[1].c_str()) < atof(args[3].c_str()));
1554 else if (args[2] == ">")
1555 res = (atof(args[1].c_str()) > atof(args[3].c_str()));
1556 else if (args[2] == "<=")
1557 res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
1558 else if (args[2] == ">=")
1559 res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
1560 else return(2);
1561 return(0);
1562}
1563
1564
1565/* --Methode-- */
1566int Commander::EvalRPNExpr(vector<string> & args, string & line)
1567{
[2512]1568 // A virer - Reza 15/03/2004
[2446]1569 return(0);
1570}
1571
1572/* --Methode-- */
1573void Commander::PushStack(vector<string>& args)
1574{
1575 // We push the argument list (args) on the stack
1576 ArgsStack.push(args);
1577 // We push also CommanderBloc and testresult on the stack
1578 CmdBlks.push(NULL);
1579 list<char> xtx;
1580 TestsStack.push(xtx);
1581
1582}
1583
1584/* --Methode-- */
1585void Commander::PopStack(bool psta)
1586{
1587 // We remove the argument list (args) from the stack
1588 if (psta) ArgsStack.pop();
1589 // And CommanderBloc and TestResult from the corresponding stacks
1590 CommanderBloc* curb = CmdBlks.top();
1591 while (curb != NULL) {
1592 CommanderBloc* parb = curb->Parent();
1593 delete curb; curb = parb;
1594 }
1595 CmdBlks.pop();
1596 TestsStack.pop();
1597}
1598
1599/* --Methode-- */
1600int Commander::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
1601{
1602int rc = 0;
1603
1604// >>>>>>>>>>> Commande d'interpreteur
1605if (kw == "help") {
1606 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
1607 else {
1608 string kwh = "Commander";
1609 cout << GetUsage(kwh) << endl;
1610 }
1611 }
[2518]1612else if (kw == "sleep") {
1613 if (tokens.size() < 1) {
1614 cout << "Commander::Interpret() Usage: sleep nsec " << endl;
1615 return(1);
1616 }
1617 int nsec = atoi(tokens[0].c_str());
1618 cout << "Commander::Interpret() sleep " << nsec << " seconds" << endl;
1619 sleep(nsec);
1620}
[2446]1621
1622else if (kw == "set") {
[2518]1623 if (tokens.size() < 2) {
1624 cout << "Commander::Interpret() Usage: set varname value or set vecvar ( w1 w2 ... ) " << endl;
1625 return(1);
1626 }
1627
[2483]1628 if (tokens.size() == 2)
1629 SetVariable(tokens[0], tokens[1]);
1630 else {
[2518]1631 if ( (tokens[1] != "(") || (tokens[tokens.size()-1] != ")") ) {
1632 cout << "Commander::Interpret() Usage: set vecvar ( w1 w2 ... ) " << endl;
1633 return(1);
1634 }
1635 string vname = tokens[0];
1636 vector<string>::iterator vit;
1637 vit = tokens.begin(); tokens.erase(vit);
1638 vit = tokens.begin(); tokens.erase(vit);
1639 tokens.pop_back();
1640 SetVar(vname, tokens);
[2446]1641 }
[2483]1642 return 0;
1643}
[2518]1644else if (kw == "var2words") {
1645 if (tokens.size() < 2) {
1646 cout << "Commander::Interpret() Usage: var2words varname wordvarname [sep]" << endl;
1647 return(1);
[2446]1648 }
[2518]1649 char sep = ' ';
1650 if (tokens.size() > 2) sep = tokens[2][0];
1651 string vv;
1652 if (!GetVar(tokens[0], vv)) {
1653 cout << "Commander::Interpret() var2words/Error No variable with name " << tokens[0] << endl;
1654 return 2;
1655 }
1656 vector<string> vs;
1657 FillVStringFrString(vv, vs, sep);
1658 SetVar(tokens[1], vs);
1659}
[2446]1660else if (kw == "alias") {
1661 if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: alias aliasname string" << endl; return(0); }
1662 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1663 cerr << "Commander::Interpret()/Error alias name should start with alphabetic" << endl;
[2518]1664 return(1);
[2483]1665 }
[2446]1666 string xx = tokens[1];
1667 for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
1668 mAliases[tokens[0]] = xx;
[2483]1669}
[2446]1670
[2483]1671else if ( (kw == "unset") || (kw == "clearvar") ) {
1672 if (tokens.size() < 1) {
1673 cout << "Commander::Interpret() Usage: unset/clearvar varname" << endl;
[2518]1674 return(1);
[2446]1675 }
[2483]1676 else DeleteVar(tokens[0]);
1677}
1678// Evaluation d'expression en notation polonaise inverse
[2512]1679else if (kw == "rpneval") {
1680 try {
1681 RPNExpressionEvaluator rpn(tokens, 1);
1682 double res = rpn.Value();
1683 char cbuff[64];
1684 sprintf(cbuff,"%g",res);
1685 string vv = cbuff;
1686 SetVariable(tokens[0],vv);
1687 return 0;
1688 }
1689 catch (RPNExprException& rpnerr) {
1690 cerr << " rpneval: Syntax error - Msg=" << rpnerr.Msg()
1691 << " \n Line=" << toks << endl;
1692 return 98;
1693 }
[2446]1694}
1695else if (kw == "echo") {
1696 for (int ii=0; ii<tokens.size(); ii++)
1697 cout << tokens[ii] << " " ;
1698 cout << endl;
1699 }
1700else if (kw == "echo2file") {
1701 if (tokens.size() < 1) {
1702 cout << "Commander::Interpret() Usage: echo2file filename [string ] " << endl;
[2518]1703 return(1);
[2446]1704 }
1705 ofstream ofs(tokens[0].c_str(), ios::app);
1706 for (int ii=1; ii<tokens.size(); ii++)
1707 ofs << tokens[ii] << " " ;
1708 ofs << endl;
1709 }
1710else if (kw == "readstdin") {
1711 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: readstdin varname" << endl; return(0); }
1712 if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
1713 cerr << "Commander::Interpret()/Error Variable name should start with alphabetic" << endl;
1714 return(0);
1715 }
1716 ShowMessage(">>> Reading From StdIn \n", _MAGENTA_);
1717 cout << tokens[0] << " ? " << endl;
1718 SetVar(tokens[0], GetStringFrStdin(this) );
1719 }
1720
[2483]1721else if (kw == "listvar") ListVar();
[2446]1722else if (kw == "listalias") {
1723 cout << "Commander::Interpret() Alias List , AliasName = Value \n";
1724 CmdStrList::iterator it;
1725 for(it = mAliases.begin(); it != mAliases.end(); it++)
1726 cout << (*it).first << " = " << (*it).second << "\n";
1727 cout << endl;
1728 }
1729else if (kw == "listcommands") {
1730 cout << "---- Commander::Interpret() Command List ----- \n";
1731 CmdExmap::iterator it;
1732 int kc = 0;
1733 for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
1734 cout << (*it).first << " ";
1735 kc++;
1736 if (kc >= 5) { cout << "\n"; kc = 0; }
1737 }
1738 cout << endl;
1739 }
1740else if (kw == "listscripts") {
1741 cout << "---- Commander::Interpret() Script List ----- \n";
1742 for(ScriptList::iterator sit = mScripts.begin();
1743 sit != mScripts.end(); sit++)
1744 cout << " Script: " << (*sit).second->Name() << " - "
1745 << (*sit).second->Comment() << endl;
1746}
1747else if (kw == "clearscript") {
1748 if (tokens.size() < 1) {
1749 cout << "Commander::Interpret() Usage: clearscript scriptname" << endl;
1750 return(0);
1751 }
1752 ScriptList::iterator sit = mScripts.find(tokens[0]);
1753 if (sit == mScripts.end()) {
1754 cout << "Commander::Interpret() No script with name" << tokens[0] << endl;
1755 return(0);
1756 }
1757 else {
1758 delete (*sit).second;
1759 mScripts.erase(sit);
1760 cout << "Commander::Interpret() script " << tokens[0] << " cleared" << endl;
1761 return(0);
1762 }
1763}
1764else if (kw == "traceon") { cout << "Commander::Interpret() -> Trace ON mode " << endl; trace = true; }
1765else if (kw == "traceoff") { cout << "Commander::Interpret() -> Trace OFF mode " << endl; trace = false; }
1766else if (kw == "timingon") {
1767 cout << "Commander::Interpret() -> Timing ON mode " << endl;
1768 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
1769 }
1770else if (kw == "timingoff") {
1771 cout << "Commander::Interpret() -> Timing OFF mode " << endl;
1772 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
1773 }
1774else if (kw == "exec") {
1775 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: exec filename" << endl; return(0); }
1776 ExecFile(tokens[0], tokens);
1777 }
1778else if (kw == "autoiniranf") {
1779 Auto_Ini_Ranf(1);
1780 return(0);
1781}
1782else if (kw == "shell") {
1783 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: shell cmdline" << endl; return(0); }
1784 string cmd;
1785 for (int ii=0; ii<tokens.size(); ii++)
1786 cmd += (tokens[ii] + ' ');
1787 system(cmd.c_str());
1788 }
1789else if (kw == "cshell") {
1790 if(tokens.size()<1) {cout<<"Commander::Interpret() Usage: cshell cmdline"<<endl; return(0);}
1791 string cmd="";
1792 for(int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
1793 CShellExecute(cmd);
1794 }
1795
1796// Execution d'une commande enregistree
1797else rc = ExecuteCommand(kw, tokens, toks);
1798
1799if (timing) gltimer->Split();
1800return(rc);
1801}
1802
1803/* --Methode-- */
1804int Commander::ParseLineExecute(string& line, bool qw)
1805 // Si qw == true, on decoupe entre '' ou "" ou espaces
1806{
1807vector<string> tokens;
[2518]1808vector<bool> qottoks;
[2446]1809string kw, toks;
1810if (line.length() < 1) return(0);
[2518]1811LineToWords(line, kw, tokens, qottoks, toks, qw);
[2446]1812return(ExecuteCommand(kw, tokens, toks));
1813}
1814
1815/* --Methode-- */
1816int Commander::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
1817{
1818 int rc = -1;
1819 CmdExmap::iterator it = cmdexmap.find(keyw);
1820 if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
1821 else {
1822 if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
1823 else cout << "Dont know how to execute " << keyw << " ? " << endl;
1824 }
1825 return(rc);
1826}
1827
1828/* --Methode-- */
1829int Commander::ExecFile(string& file, vector<string>& args)
1830{
1831char line_buff[512];
1832FILE *fip;
[2518]1833int rcc = 0;
[2446]1834if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
1835 if (file.find('.') >= file.length()) {
1836 cout << "Commander::Exec(): Error opening file " << file << endl;
1837 file += ".pic";
1838 cout << " Trying file " << file << endl;
1839 fip = fopen(file.c_str(),"r");
1840 }
1841 }
1842
1843if(fip == NULL) {
1844 cerr << "Commander::Exec() Error opening file " << file << endl;
1845 hist << "##! Commander::Exec() Error opening file " << file << endl;
1846 return(0);
1847 }
1848
1849// hist << "### Executing commands from " << file << endl;
1850PushStack(args);
1851if (trace) {
1852 ShowMessage("### Executing commands from ", _MAGENTA_);
1853 ShowMessage(file.c_str(), _MAGENTA_);
1854 ShowMessage("\n", _MAGENTA_);
1855 }
1856
1857bool ohv = histon;
1858histon = false;
1859while (fgets(line_buff,511,fip) != NULL)
1860 {
1861 if (trace) ShowMessage(line_buff, _MAGENTA_);
1862 line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
1863 string line(line_buff);
[2518]1864 rcc = Interpret(line);
1865 if ((rcc == CMD_RETURN_RC) || (rcc == CMD_BREAKEXE_RC)) break;
[2446]1866 }
1867histon = ohv;
1868
1869// hist << "### End of Exec( " << file << " ) " << endl;
1870if (trace) {
1871 ShowMessage("### End of Exec( ", _MAGENTA_);
1872 ShowMessage(file.c_str(), _MAGENTA_);
1873 ShowMessage(" ) \n", _MAGENTA_);
1874 }
1875
1876PopStack(true);
1877
1878return(0);
1879}
1880
1881/* --Methode-- */
1882int Commander::CShellExecute(string cmd)
1883{
1884 if(cmd.size()<=0) return -1;
1885
1886 string fname = GetTmpDir(); fname += "cshell_exec_pia.csh";
1887
1888 string cmdrm = "rm -f " + fname;
1889 system(cmdrm.c_str());
1890
1891 FILE *fip = fopen(fname.c_str(),"w");
1892 if(fip==NULL) {
1893 cout << "Commander/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
1894 return -2;
1895 }
1896 fprintf(fip,"#!/bin/csh\n\n");
1897 fprintf(fip,"%s\n",cmd.c_str());
1898 fprintf(fip,"\nexit 0\n");
1899 fclose(fip);
1900
1901 cmd = "csh "; cmd += fname;
1902 system(cmd.c_str());
1903
1904 system(cmdrm.c_str());
1905
1906 return 0;
1907}
1908
1909static string* videstr = NULL;
1910/* --Methode-- */
1911string& Commander::GetUsage(const string& kw)
1912{
1913bool fndok = false;
1914CmdExmap::iterator it = cmdexmap.find(kw);
1915if (it == cmdexmap.end()) {
1916 it = helpexmap.find(kw);
1917 if (it != helpexmap.end()) fndok = true;
1918 }
1919 else fndok = true;
1920if (fndok) return( (*it).second.us );
1921// Keyword pas trouve
1922if (videstr == NULL) videstr = new string("");
1923*videstr = "Nothing known about " + kw + " ?? ";
1924return(*videstr);
1925
1926}
1927
1928
1929/* Les definitions suivantes doivent se trouver ds l'en-tete du fichier LaTeX
1930 \newcommand{\piacommand}[1]{
1931 \framebox{\bf \Large #1 } \index{#1} % (Command)
1932 }
1933
1934 \newcommand{\piahelpitem}[1]{
1935 \framebox{\bf \Large #1 } \index{#1} (Help item)
1936 }
1937
1938 \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) }
1939*/
1940
1941// Fonction qui remplace tout caractere non alphanumerique en Z
1942static void check_latex_reflabel(string & prl)
1943{
1944 for(int k=0; k<prl.length(); k++)
1945 if (! isalnum(prl[k]) ) prl[k] = 'Z';
1946}
[2466]1947
[2446]1948// Fonction qui remplace _ en \_
1949static string check_latex_underscore(string const & mot)
1950{
1951 string rs;
1952 for(int k=0; k<mot.length(); k++) {
[2466]1953 if (mot[k] == '_') rs += "\\_";
[2446]1954 else rs += mot[k];
1955 }
1956 return rs;
1957}
1958
1959/* --Methode-- */
[2598]1960//! Produces a LaTeX file containing the registered command helps
[2446]1961void Commander::HelptoLaTeX(string const & fname)
1962{
1963FILE *fip;
1964if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1965 cout << "Commander::HelptoLaTex_Error: fopen( " << fname << endl;
1966 return;
1967 }
1968
1969fputs("% ----- Liste des groupes de Help ----- \n",fip);
1970fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
1971fputs("\\begin{itemize} \n",fip);
1972string prl;
1973string mol;
1974CmdHGroup::iterator it;
1975for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
1976 if ((*it).first == "All") continue;
1977 prl = (*it).first; check_latex_reflabel(prl);
1978 mol = check_latex_underscore((*it).first);
1979 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
1980 mol.c_str(), prl.c_str());
1981}
1982
1983fputs("\\end{itemize} \n",fip);
1984
1985fputs("\\vspace*{10mm} \n",fip);
1986
1987CmdExmap::iterator ite;
1988fputs("% ----- Liste de toutes les commandes et help item ----- \n",fip);
1989fputs("\\vspace{5mm} \n",fip);
1990// fputs("\\begin{table}[h!] \n", fip);
1991fputs("\\begin{center} \n ", fip);
1992fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \\\\ \n", fip);
1993fputs("\\vspace{3mm} \n",fip);
1994fputs("\\begin{tabular}{llllll} \n", fip);
1995int kt = 0;
1996for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1997 prl = (*ite).first; check_latex_reflabel(prl);
1998 mol = check_latex_underscore((*ite).first);
1999 fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
2000 kt++;
2001 if (kt < 3) fputs(" & ", fip);
2002 else { fputs(" \\\\ \n", fip); kt = 0; }
2003 }
2004if (kt == 1) fputs(" & & & \\\\ \n", fip);
2005else if (kt == 2) fputs(" & \\\\ \n", fip);
2006fputs("\\end{tabular} \n", fip);
2007fputs("\\end{center} \n", fip);
2008//fputs("\\end{table} \n", fip);
2009fputs("\\newpage \n",fip);
2010
2011int gid;
2012for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
[2466]2013 gid = (*it).second.gid;
[2446]2014 if (gid == 0) continue;
2015 // fputs("\\begin{table}[h!] \n",fip);
2016 fputs("\\vspace{6mm} \n",fip);
2017 fputs("\\begin{center} \n ", fip);
[2466]2018 fprintf(fip, "\\rule{2cm}{0.5mm} \\makebox[60mm]{{ \\bf %s } help group} \\rule{2cm}{0.5mm} \\\\ \n",
[2446]2019 (*it).first.c_str());
2020 fputs("\\vspace{3mm} \n",fip);
2021 fputs("\\begin{tabular}{llllll} \n", fip);
2022 kt = 0;
2023 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
2024 if ((*ite).second.group != gid) continue;
2025 prl = (*ite).first; check_latex_reflabel(prl);
2026 mol = check_latex_underscore((*ite).first);
2027 fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
2028 kt++;
2029 if (kt < 3) fputs(" & ", fip);
2030 else { fputs(" \\\\ \n", fip); kt = 0; }
2031 }
2032 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
2033 if ((*ite).second.group != gid) continue;
2034 prl = (*ite).first; check_latex_reflabel(prl);
2035 mol = check_latex_underscore((*ite).first);
2036 fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
2037 kt++;
2038 if (kt < 3) fputs(" & ", fip);
2039 else { fputs(" \\\\ \n", fip); kt = 0; }
2040 }
2041 if (kt == 1) fputs(" & & & \\\\ \n", fip);
2042 else if (kt == 2) fputs(" & \\\\ \n", fip);
2043 fputs("\\end{tabular} \n", fip);
2044 fputs("\\end{center} \n", fip);
2045 // fputs("\\end{table} \n",fip);
2046 // fputs("\\vspace{5mm} \n",fip);
2047}
2048// fputs("\\newline \n",fip);
2049
2050fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
2051fputs("\\newpage \n",fip);
2052
2053for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
[2466]2054 gid = (*it).second.gid;
[2446]2055 if (gid == 0) continue;
2056 prl = (*it).first; check_latex_reflabel(prl);
2057 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
2058 (*it).first.c_str(), prl.c_str());
[2466]2059 if ((*it).second.desc.length() > 0)
2060 fprintf(fip,"%s \n \\\\[2mm] ", (*it).second.desc.c_str());
[2446]2061 fprintf(fip,"\\noindent \n");
2062 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
2063 if ((*ite).second.group != gid) continue;
2064 prl = (*ite).first; check_latex_reflabel(prl);
2065 mol = check_latex_underscore((*ite).first);
2066 fprintf(fip,"\\piahelpitem{%s} \\label{%s} \n",
2067 mol.c_str(), prl.c_str());
2068 fputs("\\begin{verbatim} \n",fip);
2069 fprintf(fip,"%s\n", (*ite).second.us.c_str());
2070 fputs("\\end{verbatim} \n",fip);
2071 }
2072 for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
2073 if ((*ite).second.group != gid) continue;
2074 prl = (*ite).first; check_latex_reflabel(prl);
2075 mol = check_latex_underscore((*ite).first);
2076 fprintf(fip,"\\piacommand{%s} \\label{%s} \n",
2077 mol.c_str(), prl.c_str());
2078 fputs("\\begin{verbatim} \n",fip);
2079 fprintf(fip,"%s\n", (*ite).second.us.c_str());
2080 fputs("\\end{verbatim} \n",fip);
2081 }
2082}
2083
2084fclose(fip);
[2466]2085cout << " Commander::HelptoLaTeX() - LaTeX format help written to file " << fname << endl;
2086
[2446]2087return;
2088}
2089
2090
[2483]2091} // End of namespace SOPHYA
[2446]2092
Note: See TracBrowser for help on using the repository browser.