1 | #include "piacmd.h"
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <ctype.h>
|
---|
5 | #include <math.h>
|
---|
6 |
|
---|
7 | #include "basexecut.h"
|
---|
8 |
|
---|
9 | #include "pdlmgr.h"
|
---|
10 | #include "ctimer.h"
|
---|
11 | // #include "dlftypes.h"
|
---|
12 |
|
---|
13 | #include "pistdimgapp.h"
|
---|
14 | #include "nobjmgr.h"
|
---|
15 | #include "piafitting.h"
|
---|
16 | #include "pawexecut.h"
|
---|
17 | #include "cxxexecutor.h"
|
---|
18 | #include "cxxexecwin.h"
|
---|
19 |
|
---|
20 | #include PISTDWDG_H
|
---|
21 | #include PILIST_H
|
---|
22 |
|
---|
23 | // ------------------------------------------------------------
|
---|
24 | // Gestion d'une fenetre d'aide interactive
|
---|
25 | // Classe PIAHelpWind
|
---|
26 | // ------------------------------------------------------------
|
---|
27 |
|
---|
28 | class PIAHelpWind : public PIWindow {
|
---|
29 | public :
|
---|
30 | PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
|
---|
31 | virtual ~PIAHelpWind();
|
---|
32 | virtual void Show();
|
---|
33 | virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
|
---|
34 | inline void AddHelpGroup(const char * hgrp, int gid)
|
---|
35 | { hgrpom->AppendItem(hgrp, 20000+gid); }
|
---|
36 | inline void ClearHelpList()
|
---|
37 | { mNitem=0; hitemlist->DeleteAllItems(); }
|
---|
38 | inline void AddHelpItem(const char * hitem)
|
---|
39 | { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
|
---|
40 | protected :
|
---|
41 | PIStdImgApp* dap;
|
---|
42 | PIACmd* piac;
|
---|
43 | int mNitem;
|
---|
44 | PIList* hitemlist;
|
---|
45 | PIOptMenu* hgrpom;
|
---|
46 | PIButton * mBut;
|
---|
47 | PILabel * mLab;
|
---|
48 | PIText* mTxt;
|
---|
49 | };
|
---|
50 |
|
---|
51 | /* --Methode-- */
|
---|
52 | PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
|
---|
53 | : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
|
---|
54 | {
|
---|
55 | dap = par;
|
---|
56 | piac = piacmd;
|
---|
57 | mNitem = 0;
|
---|
58 | SetMsg(77);
|
---|
59 |
|
---|
60 | int bsx, bsy;
|
---|
61 | int tsx, tsy;
|
---|
62 | int spx, spy;
|
---|
63 | PIApplicationPrefCompSize(bsx, bsy);
|
---|
64 | spx = bsx/6; spy = bsy/6;
|
---|
65 | tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
|
---|
66 | SetSize(tsx,tsy);
|
---|
67 | hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
|
---|
68 | hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
69 | hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
|
---|
70 | hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
71 | // hitemlist->SetBorderWidth(2);
|
---|
72 | mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
|
---|
73 | // mTxt->SetMutiLineMode(true);
|
---|
74 | mTxt->SetTextEditable(false);
|
---|
75 | mTxt->SetText("");
|
---|
76 | mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
77 | mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
|
---|
78 | mLab->SetBorderWidth(1);
|
---|
79 | mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
80 | mLab->SetLabel("");
|
---|
81 | mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
|
---|
82 | mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* --Methode-- */
|
---|
86 | PIAHelpWind::~PIAHelpWind()
|
---|
87 | {
|
---|
88 | delete hgrpom;
|
---|
89 | delete hitemlist;
|
---|
90 | delete mTxt;
|
---|
91 | delete mLab;
|
---|
92 | delete mBut;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* --Methode-- */
|
---|
96 | void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
|
---|
97 | {
|
---|
98 | PIMessage um = UserMsg(msg);
|
---|
99 | if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
|
---|
100 | Hide();
|
---|
101 | return;
|
---|
102 | }
|
---|
103 | else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
|
---|
104 | mTxt->SetText("");
|
---|
105 | mLab->SetLabel("");
|
---|
106 | piac->UpdateHelpList(this, um-20000);
|
---|
107 | }
|
---|
108 | else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
|
---|
109 | string s = hitemlist->GetSelectionStr();
|
---|
110 | mTxt->SetText(piac->GetUsage(s));
|
---|
111 | mLab->SetLabel(s);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* --Methode-- */
|
---|
116 | void PIAHelpWind::Show()
|
---|
117 | {
|
---|
118 | hgrpom->SetValue(20000); // Groupe All
|
---|
119 | mTxt->SetText("");
|
---|
120 | mLab->SetLabel("");
|
---|
121 | piac->UpdateHelpList(this, 0);
|
---|
122 | PIWindow::Show();
|
---|
123 | }
|
---|
124 |
|
---|
125 | // ------------------------------------------------------------
|
---|
126 | // Bloc de commandes (Foreach, ...)
|
---|
127 | // Classe PIACmdBloc
|
---|
128 | // ------------------------------------------------------------
|
---|
129 |
|
---|
130 | class PIACmdBloc {
|
---|
131 | public:
|
---|
132 | enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
|
---|
133 |
|
---|
134 | PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
|
---|
135 | ~PIACmdBloc();
|
---|
136 | inline PIACmdBloc* Parent() { return(parent); }
|
---|
137 | inline bool CheckOK() { return blkok; }
|
---|
138 | inline void AddLine(string& line)
|
---|
139 | { lines.push_back(line); bloclineid.push_back(lines.size()); }
|
---|
140 | inline void AddBloc(PIACmdBloc* blk)
|
---|
141 | { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
|
---|
142 | PIACmdBloc* Execute();
|
---|
143 | protected:
|
---|
144 | PIACmd* piacmd;
|
---|
145 | PIACmdBloc* parent;
|
---|
146 | bool blkok; // true -> block OK
|
---|
147 | BType typ; // foreach , integer loop, float loop, test
|
---|
148 | string varname;
|
---|
149 | vector<string> strlist;
|
---|
150 | vector<string> lines;
|
---|
151 | vector<PIACmdBloc *> blocs;
|
---|
152 | vector<int> bloclineid;
|
---|
153 | int i1,i2,di;
|
---|
154 | float f1,f2,df;
|
---|
155 | };
|
---|
156 |
|
---|
157 | /* --Methode-- */
|
---|
158 | PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
|
---|
159 | {
|
---|
160 | piacmd = piac;
|
---|
161 | parent = par;
|
---|
162 | blkok = false;
|
---|
163 | typ = BT_None;
|
---|
164 | i1 = 0; i2 = -1; di = 1;
|
---|
165 | f1 = 0.; f2 = -1.; df = 1.;
|
---|
166 | if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
|
---|
167 | if ((kw != "foreach") && (kw != "for")) return;
|
---|
168 | varname = args[0]; // $CHECK$ Variable name should be checked
|
---|
169 | //if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
|
---|
170 | if (kw == "foreach" ) { // This is a foreach bloc with string list
|
---|
171 | for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
|
---|
172 | typ = BT_ForeachList;
|
---|
173 | blkok = true;
|
---|
174 | }
|
---|
175 | else { // This is an integer or float loop
|
---|
176 | size_t l = args[1].length();
|
---|
177 | size_t p = args[1].find(':');
|
---|
178 | size_t pp = args[1].find('.');
|
---|
179 | bool fl = (pp < l) ? true : false; // Float loop or integer loop
|
---|
180 | if (p >= l) return; // Syntaxe error
|
---|
181 | string a1 = args[1].substr(0, p);
|
---|
182 | string aa = args[1].substr(p+1);
|
---|
183 | p = aa.find(':');
|
---|
184 | string a2, a3;
|
---|
185 | bool hasa3 = false;
|
---|
186 | if (p < aa.length() ) {
|
---|
187 | a2 = aa.substr(0,p);
|
---|
188 | a3 = aa.substr(p+1);
|
---|
189 | hasa3 = true;
|
---|
190 | }
|
---|
191 | else a2 = aa;
|
---|
192 | if (fl) {
|
---|
193 | typ = BT_ForeachFloat;
|
---|
194 | blkok = true;
|
---|
195 | f1 = atof(a1.c_str());
|
---|
196 | f2 = atof(a2.c_str());
|
---|
197 | if (hasa3) df = atof(a3.c_str());
|
---|
198 | else df = 1.;
|
---|
199 | }
|
---|
200 | else {
|
---|
201 | typ = BT_ForeachInt;
|
---|
202 | blkok = true;
|
---|
203 | i1 = atoi(a1.c_str());
|
---|
204 | i2 = atoi(a2.c_str());
|
---|
205 | if (hasa3) di = atoi(a3.c_str());
|
---|
206 | else di = 1;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* --Methode-- */
|
---|
212 | PIACmdBloc::~PIACmdBloc()
|
---|
213 | {
|
---|
214 | for(int k=0; k<blocs.size(); k++) delete blocs[k];
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* --Methode-- */
|
---|
218 | PIACmdBloc* PIACmdBloc::Execute()
|
---|
219 | {
|
---|
220 | // cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
|
---|
221 | // " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
|
---|
222 | string cmd;
|
---|
223 | int k=0;
|
---|
224 | int kj=0;
|
---|
225 | int kk=0;
|
---|
226 | char buff[32];
|
---|
227 | if (typ == BT_ForeachList) // foreach string loop
|
---|
228 | for(k=0; k<strlist.size(); k++) {
|
---|
229 | cmd = "set " + varname + " '" + strlist[k] + "'";
|
---|
230 | piacmd->Interpret(cmd);
|
---|
231 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
232 | kk = bloclineid[kj];
|
---|
233 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
234 | else blocs[-kk-1]->Execute();
|
---|
235 | }
|
---|
236 | }
|
---|
237 | else if (typ == BT_ForeachInt) // Integer loop
|
---|
238 | for(int i=i1; i<i2; i+=di) {
|
---|
239 | k++;
|
---|
240 | if (++k > 9999) {
|
---|
241 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | sprintf(buff, " %d", i);
|
---|
245 | cmd = "set " + varname + buff;
|
---|
246 | piacmd->Interpret(cmd);
|
---|
247 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
248 | kk = bloclineid[kj];
|
---|
249 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
250 | else blocs[-kk-1]->Execute();
|
---|
251 | }
|
---|
252 | }
|
---|
253 | else if (typ == BT_ForeachFloat) // float loop
|
---|
254 | for(float f=f1; f<f2; f+=df) {
|
---|
255 | k++;
|
---|
256 | if (++k > 9999) {
|
---|
257 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
---|
258 | break;
|
---|
259 | }
|
---|
260 | sprintf(buff, " %g", f);
|
---|
261 | cmd = "set " + varname + buff;
|
---|
262 | piacmd->Interpret(cmd);
|
---|
263 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
264 | kk = bloclineid[kj];
|
---|
265 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
266 | else blocs[-kk-1]->Execute();
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | return(parent);
|
---|
271 | }
|
---|
272 |
|
---|
273 | // ------------------------------------------------------------
|
---|
274 | // Classe PIACmd
|
---|
275 | // ------------------------------------------------------------
|
---|
276 |
|
---|
277 | static PIACmd* curpiacmd = NULL;
|
---|
278 | /* --Methode-- */
|
---|
279 | PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
|
---|
280 | {
|
---|
281 | mObjMgr = omg;
|
---|
282 | mImgApp = app;
|
---|
283 | system("cp history.pic hisold.pic");
|
---|
284 | hist.open("history.pic");
|
---|
285 | histon = true;
|
---|
286 | trace = false; timing = false;
|
---|
287 | gltimer = NULL;
|
---|
288 | curblk = NULL;
|
---|
289 | felevel = 0;
|
---|
290 | mulinecmd = "";
|
---|
291 | mulinefg = false;
|
---|
292 |
|
---|
293 | cmdhgrp["All"] = 0;
|
---|
294 | cmdgrpid = 1;
|
---|
295 | cmdhgrp["Commands"] = 1;
|
---|
296 | helpwin = new PIAHelpWind(app, this);
|
---|
297 | helpwin->AddHelpGroup("All", 0);
|
---|
298 | helpwin->AddHelpGroup("Commands", 1);
|
---|
299 |
|
---|
300 | string kw = "piacmd";
|
---|
301 | string usage;
|
---|
302 | usage = ">>> (piacmd) Interpreter's keywords : \n";
|
---|
303 | usage += " > set varname string # To set a variable, $varname \n";
|
---|
304 | usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
|
---|
305 | usage += " > setol varname patt # Fills varname with object list \n";
|
---|
306 | usage += " > unset varname # clear variable definition \n";
|
---|
307 | usage += " > echo string # output string \n";
|
---|
308 | usage += " > alias name string # define a command alias \n";
|
---|
309 | usage += " > readstdin varname # reads a line from stdin into $varname \n";
|
---|
310 | usage += " > foreach varname string-list # Loop \n";
|
---|
311 | usage += " > for varname i1:i2[:di] # Integer loop \n";
|
---|
312 | usage += " > for varname f1:f2[:df] # Float loop \n";
|
---|
313 | usage += " > end # end loops \n";
|
---|
314 | usage += " > if test then # Conditional test : a == != < > <= >= b \n";
|
---|
315 | usage += " > else # Conditional \n";
|
---|
316 | usage += " > endif # End of conditional if bloc \n";
|
---|
317 | usage += " > break # Delete (clears) all test and loop blocs \n";
|
---|
318 | usage += " > return # Stops command execution from a file \n";
|
---|
319 | usage += " > listvars # List of variable names and values \n";
|
---|
320 | usage += " > listalias # List of alias names and values \n";
|
---|
321 | usage += " > listcommands # List of all known commands \n";
|
---|
322 | usage += " > exec filename # Execute commands from file \n";
|
---|
323 | usage += " > shell comand_string # Execute shell command \n";
|
---|
324 | usage += " > help <command_name> # <command_name> usage info \n";
|
---|
325 | usage += " > helpwindow # Displays help window \n";
|
---|
326 | usage += " > timingon timingoff traceon traceoff \n";
|
---|
327 | string grp = "Commands";
|
---|
328 | RegisterHelp(kw, usage, grp);
|
---|
329 |
|
---|
330 | basexec = new PIABaseExecutor(this, omg, app);
|
---|
331 | fitexec = new PIAFitter(this, app);
|
---|
332 | pawexec = new PAWExecutor(this, app);
|
---|
333 | CxxExecutor * cxxe = new CxxExecutor(this, app);
|
---|
334 | cxxexec = cxxe;
|
---|
335 | cxxoptwin = new CxxOptionWind(app, cxxe);
|
---|
336 | cxxexwin = new CxxExecWind(app, cxxe);
|
---|
337 |
|
---|
338 | AddInterpreter(this);
|
---|
339 | curcmdi = this;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* --Methode-- */
|
---|
343 | PIACmd::~PIACmd()
|
---|
344 | {
|
---|
345 | hist.close();
|
---|
346 | if (gltimer) { delete gltimer; gltimer = NULL; }
|
---|
347 | Modmap::iterator it;
|
---|
348 | for(it = modmap.begin(); it != modmap.end(); it++) {
|
---|
349 | string name = (*it).first + "_end";
|
---|
350 | DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
|
---|
351 | if (fend) fend();
|
---|
352 | delete (*it).second;
|
---|
353 | }
|
---|
354 | delete helpwin;
|
---|
355 | delete cxxexwin;
|
---|
356 | delete cxxoptwin;
|
---|
357 | if (curpiacmd == this) curpiacmd = NULL;
|
---|
358 | delete basexec;
|
---|
359 | delete fitexec;
|
---|
360 | delete pawexec;
|
---|
361 | delete cxxexec;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /* --Methode-- */
|
---|
365 | PIACmd* PIACmd::GetInterpreter()
|
---|
366 | {
|
---|
367 | return(curpiacmd);
|
---|
368 | }
|
---|
369 |
|
---|
370 | /* --Methode-- */
|
---|
371 | string PIACmd::Name()
|
---|
372 | {
|
---|
373 | return("piacmd");
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* --Methode-- */
|
---|
377 | void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
|
---|
378 | {
|
---|
379 | if (!ce) {
|
---|
380 | RegisterHelp(keyw, usage, grp);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 | int gid = CheckHelpGrp(grp);
|
---|
384 | cmdex cme;
|
---|
385 | cme.group = gid;
|
---|
386 | cme.us = usage;
|
---|
387 | cme.cex = ce;
|
---|
388 | cmdexmap[keyw] = cme;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /* --Methode-- */
|
---|
392 | void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
|
---|
393 | {
|
---|
394 | int gid = CheckHelpGrp(grp);
|
---|
395 | cmdex cme;
|
---|
396 | cme.group = gid;
|
---|
397 | cme.us = usage;
|
---|
398 | cme.cex = NULL;
|
---|
399 | helpexmap[keyw] = cme;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* --Methode-- */
|
---|
403 | int PIACmd::CheckHelpGrp(string& grp)
|
---|
404 | {
|
---|
405 | int gid=0;
|
---|
406 | CmdHGroup::iterator it = cmdhgrp.find(grp);
|
---|
407 | if (it == cmdhgrp.end()) {
|
---|
408 | cmdgrpid++; gid = cmdgrpid;
|
---|
409 | cmdhgrp[grp] = gid;
|
---|
410 | helpwin->AddHelpGroup(grp.c_str(), gid);
|
---|
411 | }
|
---|
412 | else gid = (*it).second;
|
---|
413 | return(gid);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* --Methode-- */
|
---|
417 | void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
|
---|
418 | {
|
---|
419 | helpwin->ClearHelpList();
|
---|
420 | CmdExmap::iterator it;
|
---|
421 | for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
|
---|
422 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
---|
423 | helpwin->AddHelpItem((*it).first.c_str());
|
---|
424 | }
|
---|
425 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
---|
426 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
---|
427 | helpwin->AddHelpItem((*it).first.c_str());
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | /* --Methode-- */
|
---|
432 | void PIACmd::LoadModule(string& fnameso, string& name)
|
---|
433 | {
|
---|
434 | PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
|
---|
435 | if (dynlink == NULL) {
|
---|
436 | cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
|
---|
437 | return;
|
---|
438 | }
|
---|
439 | string fname = name + "_init";
|
---|
440 | DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
|
---|
441 | if (!finit) {
|
---|
442 | cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
|
---|
443 | return;
|
---|
444 | }
|
---|
445 | cout << "PIACmd/LoadModule_Info: Initialisation module" << name
|
---|
446 | << " " << fname << "() ..." << endl;
|
---|
447 | finit();
|
---|
448 | modmap[name] = dynlink;
|
---|
449 | return;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /* --Methode-- */
|
---|
453 | void PIACmd::AddInterpreter(CmdInterpreter * cl)
|
---|
454 | {
|
---|
455 | if (!cl) return;
|
---|
456 | interpmap[cl->Name()] = cl;}
|
---|
457 |
|
---|
458 | /* --Methode-- */
|
---|
459 | void PIACmd::SelInterpreter(string& name)
|
---|
460 | {
|
---|
461 | InterpMap::iterator it = interpmap.find(name);
|
---|
462 | if (it == interpmap.end()) return;
|
---|
463 | curcmdi = (*it).second;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 |
|
---|
468 | /* Fonction */
|
---|
469 | static string GetStringFrStdin(PIACmd* piac)
|
---|
470 | {
|
---|
471 | PIStdImgApp* piapp = piac->GetImgApp();
|
---|
472 | if (piapp) {
|
---|
473 | PIBaseWdg* wdg = piapp->CurrentBaseWdg();
|
---|
474 | if (wdg) wdg->Refresh();
|
---|
475 | #ifndef __mac__
|
---|
476 | /* On vide le buffer X-Window */
|
---|
477 | XSync(PIXDisplay(),False);
|
---|
478 | #endif
|
---|
479 | }
|
---|
480 | char buff[128];
|
---|
481 | fgets(buff, 128, stdin);
|
---|
482 | buff[127] = '\0';
|
---|
483 | return((string)buff);
|
---|
484 | }
|
---|
485 |
|
---|
486 | /* --Methode-- */
|
---|
487 | int PIACmd::Interpret(string& s)
|
---|
488 | {
|
---|
489 | int rc = 0;
|
---|
490 | NamedObjMgr omg;
|
---|
491 |
|
---|
492 | // On saute de commandes vides
|
---|
493 | size_t l;
|
---|
494 | l = s.length();
|
---|
495 | if (l < 1) return(0);
|
---|
496 |
|
---|
497 | // On enregistre les commandes
|
---|
498 | if (histon) hist << s << endl;
|
---|
499 |
|
---|
500 | if (s[0] == '#') return(0); // si c'est un commentaire
|
---|
501 |
|
---|
502 | // Logique de gestion des lignes suite
|
---|
503 | // un \ en derniere position indique la presence d'une ligne suite
|
---|
504 | size_t lnb = s.find_last_not_of(' ');
|
---|
505 | if (s[lnb] == '\\' ) { // Lignes suite ...
|
---|
506 | mulinefg = true;
|
---|
507 | mulinecmd += s.substr(0,lnb);
|
---|
508 | mImgApp->GetConsole()->SetPrompt("...? ");
|
---|
509 | return(0);
|
---|
510 | }
|
---|
511 |
|
---|
512 | if (mulinefg) { // Il y avait des lignes suite
|
---|
513 | s = mulinecmd + s;
|
---|
514 | mulinecmd = "";
|
---|
515 | mulinefg = false;
|
---|
516 | const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
|
---|
517 | mImgApp->GetConsole()->SetPrompt(rprompt);
|
---|
518 | }
|
---|
519 |
|
---|
520 | // Removing leading blanks
|
---|
521 | size_t p,q;
|
---|
522 |
|
---|
523 | p=s.find_first_not_of(" \t");
|
---|
524 | if (p < l) s = s.substr(p);
|
---|
525 |
|
---|
526 | // >>>> Substitution d'alias (1er mot)
|
---|
527 | CmdVarList::iterator it;
|
---|
528 | p = 0;
|
---|
529 | q = s.find_first_of(" \t");
|
---|
530 | l = s.length();
|
---|
531 | string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
|
---|
532 | it = mAliases.find(w1);
|
---|
533 | if (it != mAliases.end()) {
|
---|
534 | s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
|
---|
535 | l = s.length();
|
---|
536 | p=s.find_first_not_of(" \t");
|
---|
537 | if (p < l) s = s.substr(p);
|
---|
538 | p = 0;
|
---|
539 | q = s.find_first_of(" ");
|
---|
540 | }
|
---|
541 |
|
---|
542 | // >>>> Separating keyword
|
---|
543 | string toks,kw;
|
---|
544 | if (q < l)
|
---|
545 | { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
|
---|
546 | else { kw = s.substr(p,l-p); toks = ""; }
|
---|
547 |
|
---|
548 | // les mot-cle end else endif doivent etre le seul mot de la ligne
|
---|
549 | if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
|
---|
550 | size_t ltk = toks.length();
|
---|
551 | if (toks.find_first_not_of(" \t") < ltk) {
|
---|
552 | cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
|
---|
553 | << "line: " << s << endl;
|
---|
554 | return(1);
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | if (kw == "break") {
|
---|
559 | PIACmdBloc* curb = curblk;
|
---|
560 | while (curb) {
|
---|
561 | PIACmdBloc* dblk = curb;
|
---|
562 | curb = curb->Parent();
|
---|
563 | delete dblk;
|
---|
564 | }
|
---|
565 | testresult.clear();
|
---|
566 | return(0);
|
---|
567 | }
|
---|
568 | else if (kw == "return") return(77777);
|
---|
569 |
|
---|
570 | // On verifie si nous sommes dans un bloc (for , foreach)
|
---|
571 | if (curblk != NULL) { // On est dans un bloc
|
---|
572 | if ( (kw == "for") || (kw == "foreach")) felevel++;
|
---|
573 | else if (kw == "end") felevel--;
|
---|
574 | if (felevel == 0) { // Il faut executer le bloc
|
---|
575 | PIACmdBloc* curb = curblk;
|
---|
576 | curblk = curb->Parent();
|
---|
577 | mImgApp->GetConsole()->SetPrompt("Cmd> ");
|
---|
578 | // cout << " *DBG* Executing bloc " << endl;
|
---|
579 | bool ohv = histon;
|
---|
580 | histon = false;
|
---|
581 | curb->Execute();
|
---|
582 | histon = ohv;
|
---|
583 | }
|
---|
584 | else curblk->AddLine(s);
|
---|
585 | return(0);
|
---|
586 | }
|
---|
587 | else if (kw == "end") {
|
---|
588 | cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
|
---|
589 | << "line: " << s << endl;
|
---|
590 | return(1);
|
---|
591 | }
|
---|
592 |
|
---|
593 | // Sommes-nous dans un bloc de test if then else
|
---|
594 | if (testresult.size() > 0) { // Nous sommes ds un bloc if
|
---|
595 | if (kw == "else") {
|
---|
596 | if ((*tresit) & 2) {
|
---|
597 | cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
|
---|
598 | << "line: " << s << endl;
|
---|
599 | return(1);
|
---|
600 | }
|
---|
601 | else {
|
---|
602 | char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
|
---|
603 | mImgApp->GetConsole()->SetPrompt(npr);
|
---|
604 | (*tresit) |= 2;
|
---|
605 | return(0);
|
---|
606 | }
|
---|
607 | }
|
---|
608 | else if (kw == "endif") {
|
---|
609 | list<char>::iterator dbit = tresit;
|
---|
610 | tresit--;
|
---|
611 | testresult.erase(dbit);
|
---|
612 | char * npr = "Cmd> ";
|
---|
613 | if (testresult.size() > 1) {
|
---|
614 | if (!((*tresit)&2))
|
---|
615 | npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
|
---|
616 | else
|
---|
617 | npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
|
---|
618 | }
|
---|
619 | mImgApp->GetConsole()->SetPrompt(npr);
|
---|
620 | return(0);
|
---|
621 | }
|
---|
622 | }
|
---|
623 | else if ((kw == "else") || (kw == "endif")) {
|
---|
624 | cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
|
---|
625 | << "line: " << s << endl;
|
---|
626 | return(1);
|
---|
627 | }
|
---|
628 |
|
---|
629 | bool fgcont = true;
|
---|
630 | if (testresult.size() > 0) { // Resultat de if ou else
|
---|
631 | list<char>::iterator it;
|
---|
632 | for(it=testresult.begin(); it!=testresult.end(); it++) {
|
---|
633 | // Si on n'est pas ds le else et le if est faux
|
---|
634 | if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
|
---|
635 | // Si on est ds else et le if etait vrai !
|
---|
636 | if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
|
---|
637 | if (!fgcont) break;
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | if ((!fgcont) && (kw != "if")) return(0);
|
---|
642 |
|
---|
643 |
|
---|
644 | // Nous ne sommes donc pas dans un bloc .... Substitution de variables
|
---|
645 | string s2;
|
---|
646 | int rcs ;
|
---|
647 | // Execution de code C++
|
---|
648 |
|
---|
649 | if (s[0] == '@') {
|
---|
650 | CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
|
---|
651 | if (cxxe == NULL) {
|
---|
652 | cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
|
---|
653 | return(99);
|
---|
654 | }
|
---|
655 | // Sans substitution des variables $
|
---|
656 | if (s[1] == '@') return(cxxe->ExecuteCXX(s.substr(2)));
|
---|
657 | else { // AVEC substitution des variables $
|
---|
658 | rcs = SubstituteVars(s, s2);
|
---|
659 | if (rcs) return(rcs);
|
---|
660 | return(cxxe->ExecuteCXX(s2.substr(1)));
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | rcs = SubstituteVars(s, s2);
|
---|
666 | if (rcs) return(rcs);
|
---|
667 |
|
---|
668 | // >>>> Separating keyword and tokens
|
---|
669 | vector<string> tokens;
|
---|
670 |
|
---|
671 | q = s2.find(' ');
|
---|
672 | l = s2.length();
|
---|
673 | if (q < l)
|
---|
674 | { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
|
---|
675 | else { kw = s2; toks = ""; }
|
---|
676 |
|
---|
677 | q = 0;
|
---|
678 | while (q < l) {
|
---|
679 | p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
|
---|
680 | if (p>=l) break;
|
---|
681 | if ( (toks[p] == '\'') || (toks[p] == '"') ) {
|
---|
682 | q = toks.find(toks[p],p+1);
|
---|
683 | if (q>=l) {
|
---|
684 | cerr << "PIACmd::Interpret()/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
|
---|
685 | return(2);
|
---|
686 | }
|
---|
687 | p++;
|
---|
688 | }
|
---|
689 | else {
|
---|
690 | q = toks.find_first_of(" \t",p); // la fin du token;
|
---|
691 | }
|
---|
692 | string token = toks.substr(p,q-p);
|
---|
693 | tokens.push_back(token);
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | // Si c'est un for/foreach, on cree un nouveau bloc
|
---|
698 | if ((kw == "foreach") || (kw == "for")) {
|
---|
699 | // cout << " *DBG* We got a foreach... " << endl;
|
---|
700 | PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
|
---|
701 | if (!bloc->CheckOK()) {
|
---|
702 | cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
|
---|
703 | delete bloc;
|
---|
704 | return(1);
|
---|
705 | }
|
---|
706 | felevel++;
|
---|
707 | if (curblk) curblk->AddBloc(bloc);
|
---|
708 | else mImgApp->GetConsole()->SetPrompt("for...> ");
|
---|
709 | curblk = bloc;
|
---|
710 | // cout << " *DBG* New Bloc created ... " << endl;
|
---|
711 | return(0);
|
---|
712 | }
|
---|
713 | else if (kw == "if") { // Un test if
|
---|
714 | bool restst = true;
|
---|
715 | int rct = EvaluateTest(tokens, s, restst);
|
---|
716 | if (rct) {
|
---|
717 | cerr << "PIACmd::Interpret() if syntax Error ! " << endl;
|
---|
718 | return(1);
|
---|
719 | }
|
---|
720 | char res_tst = (restst) ? 1 : 0;
|
---|
721 | testresult.push_back(res_tst);
|
---|
722 | if (testresult.size() == 1) tresit = testresult.begin();
|
---|
723 | else tresit++;
|
---|
724 | char * npr = (restst) ? "if-T> " : "if-F> ";
|
---|
725 | mImgApp->GetConsole()->SetPrompt(npr);
|
---|
726 | }
|
---|
727 | // Execution de commandes
|
---|
728 | else rc = ExecuteCommandLine(kw, tokens, toks);
|
---|
729 | return(rc);
|
---|
730 |
|
---|
731 | // cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
|
---|
732 | // for(int ii=0; ii<tokens.size(); ii++)
|
---|
733 | // cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
|
---|
734 |
|
---|
735 | }
|
---|
736 |
|
---|
737 | /* --Methode-- */
|
---|
738 | int PIACmd::SubstituteVars(string & s, string & s2)
|
---|
739 | // Variable substitution
|
---|
740 | {
|
---|
741 | NamedObjMgr omg;
|
---|
742 |
|
---|
743 | size_t p,q,q2,l;
|
---|
744 |
|
---|
745 | s2="";
|
---|
746 | p = 0;
|
---|
747 | l = s.length();
|
---|
748 | string vn;
|
---|
749 | while (p < l) {
|
---|
750 | q = s.find('$',p);
|
---|
751 | if (q > l) break;
|
---|
752 | q2 = s.find('\'',p);
|
---|
753 | if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
|
---|
754 | q2 = s.find('\'',q2+1);
|
---|
755 | if (q2 >= l) {
|
---|
756 | cerr << " Syntax error - Unbalenced quotes !!! " << endl;
|
---|
757 | return(1);
|
---|
758 | }
|
---|
759 | s2 += s.substr(p, q2-p+1);
|
---|
760 | p = q2+1; continue;
|
---|
761 | }
|
---|
762 | // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
|
---|
763 | if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
|
---|
764 | s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
|
---|
765 | continue;
|
---|
766 | }
|
---|
767 | if (q >= l-1) {
|
---|
768 | cerr << " Syntax error - line ending with $ !!! " << endl;
|
---|
769 | return(2);
|
---|
770 | }
|
---|
771 | vn = "";
|
---|
772 | if ( s[q+1] == '{' ) { // Variable in the form ${name}
|
---|
773 | q2 = s.find('}',q+1);
|
---|
774 | if (q2 >= l) {
|
---|
775 | cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
|
---|
776 | return(3);
|
---|
777 | }
|
---|
778 | vn = s.substr(q+2,q2-q-2);
|
---|
779 | q2++;
|
---|
780 | }
|
---|
781 | else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
|
---|
782 | q2 = s.find(']',q+1);
|
---|
783 | if (q2 >= l) {
|
---|
784 | cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
|
---|
785 | return(4);
|
---|
786 | }
|
---|
787 | vn = s.substr(q+2,q2-q-2);
|
---|
788 | if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
|
---|
789 | cerr << " Error: Undefined variable " << vn << " ! " << endl;
|
---|
790 | return(5);
|
---|
791 | }
|
---|
792 | vn = omg.GetVar(vn);
|
---|
793 | q2++;
|
---|
794 | }
|
---|
795 | else {
|
---|
796 | q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
|
---|
797 | if (q2 > l) q2 = l;
|
---|
798 | vn = s.substr(q+1, q2-q-1);
|
---|
799 | }
|
---|
800 | if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
|
---|
801 | cerr << " Error: Undefined variable " << vn << " ! " << endl;
|
---|
802 | return(5);
|
---|
803 | }
|
---|
804 | s2 += (s.substr(p, q-p) + omg.GetVar(vn));
|
---|
805 | p = q2;
|
---|
806 | }
|
---|
807 | if (p < l) s2 += s.substr(p);
|
---|
808 |
|
---|
809 | p = s2.find_first_not_of(" \t");
|
---|
810 | if (p < l) s2 = s2.substr(p);
|
---|
811 |
|
---|
812 | return(0);
|
---|
813 | }
|
---|
814 |
|
---|
815 | /* --Methode-- */
|
---|
816 | int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
|
---|
817 | {
|
---|
818 | res = true;
|
---|
819 | if ((args.size() < 4) || (args[3] != "then")) return(1);
|
---|
820 | if (args[1] == "==") res = (args[0] == args[2]);
|
---|
821 | else if (args[1] == "!=") res = (args[0] != args[2]);
|
---|
822 | else if (args[1] == "<")
|
---|
823 | res = (atof(args[0].c_str()) < atof(args[2].c_str()));
|
---|
824 | else if (args[1] == ">")
|
---|
825 | res = (atof(args[0].c_str()) > atof(args[2].c_str()));
|
---|
826 | else if (args[1] == "<=")
|
---|
827 | res = (atof(args[0].c_str()) <= atof(args[2].c_str()));
|
---|
828 | else if (args[1] == ">=")
|
---|
829 | res = (atof(args[0].c_str()) >= atof(args[2].c_str()));
|
---|
830 | else return(2);
|
---|
831 | return(0);
|
---|
832 | }
|
---|
833 |
|
---|
834 | /* --Methode-- */
|
---|
835 | int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
|
---|
836 | {
|
---|
837 | int rc = 0;
|
---|
838 | NamedObjMgr omg;
|
---|
839 |
|
---|
840 | // >>>>>>>>>>> Commande d'interpreteur
|
---|
841 | if (kw == "helpwindow") ShowHelpWindow();
|
---|
842 | else if (kw == "help") {
|
---|
843 | if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
|
---|
844 | else {
|
---|
845 | string kwh = "piacmd";
|
---|
846 | cout << GetUsage(kwh) << endl;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | else if (kw == "set") {
|
---|
851 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
|
---|
852 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
853 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
854 | return(0);
|
---|
855 | }
|
---|
856 | // string xx = tokens[1];
|
---|
857 | // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
|
---|
858 | omg.SetVar(tokens[0], tokens[1]);
|
---|
859 | }
|
---|
860 |
|
---|
861 | else if (kw == "getvar") {
|
---|
862 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
|
---|
863 | if (!omg.HasVar(tokens[1])) {
|
---|
864 | cerr << "Error - No " << tokens[1] << " Variable " << endl;
|
---|
865 | return(0);
|
---|
866 | }
|
---|
867 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
868 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
869 | return(0);
|
---|
870 | }
|
---|
871 | omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
|
---|
872 | }
|
---|
873 |
|
---|
874 | else if (kw == "alias") {
|
---|
875 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
|
---|
876 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
877 | cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
|
---|
878 | return(0);
|
---|
879 | }
|
---|
880 | string xx = tokens[1];
|
---|
881 | for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
|
---|
882 | mAliases[tokens[0]] = xx;
|
---|
883 | }
|
---|
884 |
|
---|
885 | else if (kw == "setol") {
|
---|
886 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
|
---|
887 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
888 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
889 | return(0);
|
---|
890 | }
|
---|
891 | vector<string> ol;
|
---|
892 | mObjMgr->GetObjList(tokens[1], ol);
|
---|
893 | string vol;
|
---|
894 | if (ol.size() < 1) vol = "";
|
---|
895 | else {
|
---|
896 | vol = ol[0];
|
---|
897 | for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
|
---|
898 | }
|
---|
899 | omg.SetVar(tokens[0], vol);
|
---|
900 | }
|
---|
901 | else if (kw == "unset") {
|
---|
902 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
|
---|
903 | if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
|
---|
904 | else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
|
---|
905 | }
|
---|
906 | else if (kw == "echo") {
|
---|
907 | for (int ii=0; ii<tokens.size(); ii++)
|
---|
908 | cout << tokens[ii] << " " ;
|
---|
909 | cout << endl;
|
---|
910 | }
|
---|
911 | else if (kw == "readstdin") {
|
---|
912 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
|
---|
913 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
914 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
915 | return(0);
|
---|
916 | }
|
---|
917 | mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
|
---|
918 | cout << tokens[0] << " ? " << endl;
|
---|
919 | omg.SetVar(tokens[0], GetStringFrStdin(this) );
|
---|
920 | }
|
---|
921 |
|
---|
922 | else if (kw == "listvars") {
|
---|
923 | cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
|
---|
924 | DVList& varlist = omg.GetVarList();
|
---|
925 | DVList::ValList::const_iterator it;
|
---|
926 | string value;
|
---|
927 | for(it = varlist.Begin(); it != varlist.End(); it++) {
|
---|
928 | #ifdef SANS_EVOLPLANCK
|
---|
929 | MuTyV mtv = (*it).second;
|
---|
930 | value = (string)(mtv);
|
---|
931 | #else
|
---|
932 | value = (string)((*it).second.elval);
|
---|
933 | #endif
|
---|
934 | cout << (*it).first << " = " << value << "\n";
|
---|
935 | }
|
---|
936 | cout << endl;
|
---|
937 | }
|
---|
938 | else if (kw == "listalias") {
|
---|
939 | cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
|
---|
940 | CmdVarList::iterator it;
|
---|
941 | for(it = mAliases.begin(); it != mAliases.end(); it++)
|
---|
942 | cout << (*it).first << " = " << (*it).second << "\n";
|
---|
943 | cout << endl;
|
---|
944 | }
|
---|
945 | else if (kw == "listcommands") {
|
---|
946 | cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
|
---|
947 | CmdExmap::iterator it;
|
---|
948 | int kc = 0;
|
---|
949 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
---|
950 | cout << (*it).first << " ";
|
---|
951 | kc++;
|
---|
952 | if (kc >= 5) { cout << "\n"; kc = 0; }
|
---|
953 | }
|
---|
954 | cout << endl;
|
---|
955 | }
|
---|
956 | else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
|
---|
957 | else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
|
---|
958 | else if (kw == "timingon") {
|
---|
959 | cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
|
---|
960 | if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
|
---|
961 | }
|
---|
962 | else if (kw == "timingoff") {
|
---|
963 | cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
|
---|
964 | if (gltimer) delete gltimer; gltimer = NULL; timing = false;
|
---|
965 | }
|
---|
966 | else if (kw == "exec") {
|
---|
967 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
|
---|
968 | ExecFile(tokens[0], tokens);
|
---|
969 | }
|
---|
970 | else if (kw == "shell") {
|
---|
971 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
|
---|
972 | string cmd;
|
---|
973 | for (int ii=0; ii<tokens.size(); ii++)
|
---|
974 | cmd += (tokens[ii] + ' ');
|
---|
975 | system(cmd.c_str());
|
---|
976 | }
|
---|
977 |
|
---|
978 | // Execution d'une commande enregistree
|
---|
979 | else rc = ExecuteCommand(kw, tokens, toks);
|
---|
980 |
|
---|
981 | if (timing) gltimer->Split();
|
---|
982 | return(rc);
|
---|
983 | }
|
---|
984 |
|
---|
985 | /* --Methode-- */
|
---|
986 | int PIACmd::ParseLineExecute(string& line)
|
---|
987 | {
|
---|
988 | vector<string> tokens;
|
---|
989 |
|
---|
990 | if (line.length() < 1) return(0);
|
---|
991 |
|
---|
992 | string toks,kw;
|
---|
993 | size_t p = line.find_first_not_of(" ");
|
---|
994 | line = line.substr(p);
|
---|
995 | p = 0;
|
---|
996 | size_t q = line.find_first_of(" ");
|
---|
997 | size_t l = line.length();
|
---|
998 |
|
---|
999 | if (q < l)
|
---|
1000 | { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
|
---|
1001 | else { kw = line.substr(p,l-p); toks = ""; }
|
---|
1002 |
|
---|
1003 | q = 0;
|
---|
1004 | while (q < l) {
|
---|
1005 | p = toks.find_first_not_of(" ",q+1); // au debut d'un token
|
---|
1006 | if (p>=l) break;
|
---|
1007 | q = toks.find_first_of(" ",p); // la fin du token;
|
---|
1008 | string token = toks.substr(p,q-p);
|
---|
1009 | tokens.push_back(token);
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | return(ExecuteCommand(kw, tokens, toks));
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /* --Methode-- */
|
---|
1016 | int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
|
---|
1017 | {
|
---|
1018 | int rc = -1;
|
---|
1019 | CmdExmap::iterator it = cmdexmap.find(keyw);
|
---|
1020 | if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
|
---|
1021 | else {
|
---|
1022 | if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
|
---|
1023 | else cout << "Dont know how to execute " << keyw << " ? " << endl;
|
---|
1024 | }
|
---|
1025 | return(rc);
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | /* --Methode-- */
|
---|
1029 | int PIACmd::ExecFile(string& file, vector<string>& args)
|
---|
1030 | {
|
---|
1031 | char line_buff[512];
|
---|
1032 | FILE *fip;
|
---|
1033 |
|
---|
1034 | if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
|
---|
1035 | if (file.find('.') >= file.length()) {
|
---|
1036 | cout << "PIACmd::Exec(): Error opening file " << file << endl;
|
---|
1037 | file += ".pic";
|
---|
1038 | cout << " Trying file " << file << endl;
|
---|
1039 | fip = fopen(file.c_str(),"r");
|
---|
1040 | }
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | if(fip == NULL) {
|
---|
1044 | cerr << "PIACmd::Exec() Error opening file " << file << endl;
|
---|
1045 | hist << "##! PIACmd::Exec() Error opening file " << file << endl;
|
---|
1046 | return(0);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | // hist << "### Executing commands from " << file << endl;
|
---|
1050 |
|
---|
1051 | // Setting $0 ... $99 variables
|
---|
1052 | int k;
|
---|
1053 | char buff[32];
|
---|
1054 | // First, we clear all previous values
|
---|
1055 | NamedObjMgr omg;
|
---|
1056 | string vn="#";
|
---|
1057 | omg.DeleteVar(vn);
|
---|
1058 | for(k=0; k<99; k++) {
|
---|
1059 | sprintf(buff,"%d",k);
|
---|
1060 | vn = buff;
|
---|
1061 | omg.DeleteVar(vn);
|
---|
1062 | }
|
---|
1063 | // We then set them
|
---|
1064 | string vval;
|
---|
1065 | vn="#";
|
---|
1066 | sprintf(buff,"%d",(int)args.size());
|
---|
1067 | omg.SetVar(vn, buff);
|
---|
1068 | for(k=0; k<args.size(); k++) {
|
---|
1069 | sprintf(buff,"%d",k);
|
---|
1070 | vn = buff;
|
---|
1071 | omg.SetVar(vn, args[k]);
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | if (trace) {
|
---|
1075 | mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
|
---|
1076 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
---|
1077 | mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | bool ohv = histon;
|
---|
1081 | histon = false;
|
---|
1082 | while (fgets(line_buff,511,fip) != NULL)
|
---|
1083 | {
|
---|
1084 | if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
|
---|
1085 | line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
|
---|
1086 | string line(line_buff);
|
---|
1087 | if (Interpret(line) == 77777) break;
|
---|
1088 | }
|
---|
1089 | histon = ohv;
|
---|
1090 |
|
---|
1091 | // hist << "### End of Exec( " << file << " ) " << endl;
|
---|
1092 | if (trace) {
|
---|
1093 | mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
|
---|
1094 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
---|
1095 | mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | return(0);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 |
|
---|
1102 | static string* videstr = NULL;
|
---|
1103 | /* --Methode-- */
|
---|
1104 | string& PIACmd::GetUsage(const string& kw)
|
---|
1105 | {
|
---|
1106 | bool fndok = false;
|
---|
1107 | CmdExmap::iterator it = cmdexmap.find(kw);
|
---|
1108 | if (it == cmdexmap.end()) {
|
---|
1109 | it = helpexmap.find(kw);
|
---|
1110 | if (it != helpexmap.end()) fndok = true;
|
---|
1111 | }
|
---|
1112 | else fndok = true;
|
---|
1113 | if (fndok) return( (*it).second.us );
|
---|
1114 | // Keyword pas trouve
|
---|
1115 | if (videstr == NULL) videstr = new string("");
|
---|
1116 | *videstr = "Nothing known about " + kw + " ?? ";
|
---|
1117 | return(*videstr);
|
---|
1118 |
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /* --Methode-- */
|
---|
1122 | void PIACmd::ShowHelpWindow()
|
---|
1123 | {
|
---|
1124 | helpwin->Show();
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /* --Methode-- */
|
---|
1128 | void PIACmd::ShowCxxOptionWindow()
|
---|
1129 | {
|
---|
1130 | cxxoptwin->Show();
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /* --Methode-- */
|
---|
1134 | void PIACmd::ShowCxxExecWindow()
|
---|
1135 | {
|
---|
1136 | cxxexwin->Show();
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /* --Methode-- */
|
---|
1140 | void PIACmd::HelptoLaTex(string const & fname)
|
---|
1141 | {
|
---|
1142 | FILE *fip;
|
---|
1143 | if ((fip = fopen(fname.c_str(), "w")) == NULL) {
|
---|
1144 | cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
|
---|
1145 | return;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | fputs("% ----- Liste des groupes de Help ----- \n",fip);
|
---|
1149 | fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
|
---|
1150 | fputs("\\begin{itemize} \n",fip);
|
---|
1151 | CmdHGroup::iterator it;
|
---|
1152 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
|
---|
1153 | fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
|
---|
1154 | (*it).first.c_str(), (*it).first.c_str());
|
---|
1155 |
|
---|
1156 | fputs("\\end{itemize} \n",fip);
|
---|
1157 |
|
---|
1158 | fputs("\\newpage \n",fip);
|
---|
1159 |
|
---|
1160 | CmdExmap::iterator ite;
|
---|
1161 | fputs("% ----- Liste de toutes les commandes ----- \n",fip);
|
---|
1162 | fputs("\\begin{center} \n ", fip);
|
---|
1163 | fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
|
---|
1164 | fputs("\n \n \\vspace{5mm} \n",fip);
|
---|
1165 | fputs("\\begin{tabular}{llllll} \n", fip);
|
---|
1166 | int kt = 0;
|
---|
1167 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
1168 | fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
|
---|
1169 | kt++;
|
---|
1170 | if (kt < 3) fputs(" & ", fip);
|
---|
1171 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
---|
1172 | }
|
---|
1173 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
---|
1174 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
---|
1175 | fputs("\\end{tabular} \n", fip);
|
---|
1176 | fputs("\\end{center} \n", fip);
|
---|
1177 | fputs("\n \n \\vspace{1cm} \n",fip);
|
---|
1178 |
|
---|
1179 | fputs("\\begin{center} \n ", fip);
|
---|
1180 | fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
|
---|
1181 | fputs("\n \n \\vspace{5mm} \n",fip);
|
---|
1182 | fputs("\\begin{tabular}{llllll} \n", fip);
|
---|
1183 | kt = 0;
|
---|
1184 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
---|
1185 | fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
|
---|
1186 | kt++;
|
---|
1187 | if (kt < 3) fputs(" & ", fip);
|
---|
1188 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
---|
1189 | }
|
---|
1190 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
---|
1191 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
---|
1192 | fputs("\\end{tabular} \n", fip);
|
---|
1193 | fputs("\\end{center} \n", fip);
|
---|
1194 | // fputs("\\newline \n",fip);
|
---|
1195 |
|
---|
1196 | fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
|
---|
1197 | fputs("\\newpage \n",fip);
|
---|
1198 | int gid;
|
---|
1199 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
|
---|
1200 | gid = (*it).second;
|
---|
1201 | if (gid == 0) continue;
|
---|
1202 | fprintf(fip,"\\subsection{%s} \\label{%s} \n",
|
---|
1203 | (*it).first.c_str(), (*it).first.c_str());
|
---|
1204 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
1205 | if ((*ite).second.group != gid) continue;
|
---|
1206 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
|
---|
1207 | (*ite).first.c_str(), (*ite).first.c_str());
|
---|
1208 | fputs("\\begin{verbatim} \n",fip);
|
---|
1209 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
---|
1210 | fputs("\\end{verbatim} \n",fip);
|
---|
1211 | }
|
---|
1212 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
---|
1213 | if ((*ite).second.group != gid) continue;
|
---|
1214 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
|
---|
1215 | (*ite).first.c_str(), (*ite).first.c_str());
|
---|
1216 | fputs("\\begin{verbatim} \n",fip);
|
---|
1217 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
---|
1218 | fputs("\\end{verbatim} \n",fip);
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | fclose(fip);
|
---|
1223 | return;
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 |
|
---|
1227 |
|
---|