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 |
|
---|
16 | #include PISTDWDG_H
|
---|
17 | #include PILIST_H
|
---|
18 |
|
---|
19 | // ------------------------------------------------------------
|
---|
20 | // Gestion d'une fenetre d'aide interactive
|
---|
21 | // Classe PIAHelpWind
|
---|
22 | // ------------------------------------------------------------
|
---|
23 |
|
---|
24 | class PIAHelpWind : public PIWindow {
|
---|
25 | public :
|
---|
26 | PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
|
---|
27 | virtual ~PIAHelpWind();
|
---|
28 | virtual void Show();
|
---|
29 | virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
|
---|
30 | inline void AddHelpGroup(const char * hgrp, int gid)
|
---|
31 | { hgrpom->AppendItem(hgrp, 20000+gid); }
|
---|
32 | inline void ClearHelpList()
|
---|
33 | { mNitem=0; hitemlist->DeleteAllItems(); }
|
---|
34 | inline void AddHelpItem(const char * hitem)
|
---|
35 | { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
|
---|
36 | protected :
|
---|
37 | PIStdImgApp* dap;
|
---|
38 | PIACmd* piac;
|
---|
39 | int mNitem;
|
---|
40 | PIList* hitemlist;
|
---|
41 | PIOptMenu* hgrpom;
|
---|
42 | PIButton * mBut;
|
---|
43 | PILabel * mLab;
|
---|
44 | PIText* mTxt;
|
---|
45 | };
|
---|
46 |
|
---|
47 | /* --Methode-- */
|
---|
48 | PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
|
---|
49 | : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
|
---|
50 | {
|
---|
51 | dap = par;
|
---|
52 | piac = piacmd;
|
---|
53 | mNitem = 0;
|
---|
54 | SetMsg(77);
|
---|
55 |
|
---|
56 | int bsx, bsy;
|
---|
57 | int tsx, tsy;
|
---|
58 | int spx, spy;
|
---|
59 | PIApplicationPrefCompSize(bsx, bsy);
|
---|
60 | spx = bsx/6; spy = bsy/6;
|
---|
61 | tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
|
---|
62 | SetSize(tsx,tsy);
|
---|
63 | hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
|
---|
64 | hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
65 | hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
|
---|
66 | hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
67 | // hitemlist->SetBorderWidth(2);
|
---|
68 | mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
|
---|
69 | // mTxt->SetMutiLineMode(true);
|
---|
70 | mTxt->SetTextEditable(false);
|
---|
71 | mTxt->SetText("");
|
---|
72 | mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
73 | mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
|
---|
74 | mLab->SetBorderWidth(1);
|
---|
75 | mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
76 | mLab->SetLabel("");
|
---|
77 | mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
|
---|
78 | mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* --Methode-- */
|
---|
82 | PIAHelpWind::~PIAHelpWind()
|
---|
83 | {
|
---|
84 | delete hgrpom;
|
---|
85 | delete hitemlist;
|
---|
86 | delete mTxt;
|
---|
87 | delete mLab;
|
---|
88 | delete mBut;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* --Methode-- */
|
---|
92 | void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
|
---|
93 | {
|
---|
94 | PIMessage um = UserMsg(msg);
|
---|
95 | if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
|
---|
96 | Hide();
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
|
---|
100 | mTxt->SetText("");
|
---|
101 | mLab->SetLabel("");
|
---|
102 | piac->UpdateHelpList(this, um-20000);
|
---|
103 | }
|
---|
104 | else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
|
---|
105 | string s = hitemlist->GetSelectionStr();
|
---|
106 | mTxt->SetText(piac->GetUsage(s));
|
---|
107 | mLab->SetLabel(s);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* --Methode-- */
|
---|
112 | void PIAHelpWind::Show()
|
---|
113 | {
|
---|
114 | hgrpom->SetValue(20000); // Groupe All
|
---|
115 | mTxt->SetText("");
|
---|
116 | mLab->SetLabel("");
|
---|
117 | piac->UpdateHelpList(this, 0);
|
---|
118 | PIWindow::Show();
|
---|
119 | }
|
---|
120 |
|
---|
121 | // ------------------------------------------------------------
|
---|
122 | // Bloc de commandes (Foreach, ...)
|
---|
123 | // Classe PIACmdBloc
|
---|
124 | // ------------------------------------------------------------
|
---|
125 |
|
---|
126 | class PIACmdBloc {
|
---|
127 | public:
|
---|
128 | PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
|
---|
129 | ~PIACmdBloc();
|
---|
130 | inline PIACmdBloc* Parent() { return(parent); }
|
---|
131 | inline bool CheckOK() { return((typ >= 0) ? true : false); }
|
---|
132 | inline void AddLine(string& line)
|
---|
133 | { lines.push_back(line); bloclineid.push_back(lines.size()); }
|
---|
134 | inline void AddBloc(PIACmdBloc* blk)
|
---|
135 | { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
|
---|
136 | PIACmdBloc* Execute();
|
---|
137 | protected:
|
---|
138 | PIACmd* piacmd;
|
---|
139 | PIACmdBloc* parent;
|
---|
140 | int typ; // 0 foreach , 1 integer loop, 2 float loop
|
---|
141 | string varname;
|
---|
142 | vector<string> strlist;
|
---|
143 | vector<string> lines;
|
---|
144 | vector<PIACmdBloc *> blocs;
|
---|
145 | vector<int> bloclineid;
|
---|
146 | int i1,i2,di;
|
---|
147 | float f1,f2,df;
|
---|
148 | };
|
---|
149 |
|
---|
150 | /* --Methode-- */
|
---|
151 | PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args)
|
---|
152 | {
|
---|
153 | piacmd = piac;
|
---|
154 | parent = par;
|
---|
155 | typ = -1;
|
---|
156 | i1 = 0; i2 = -1; di = 1;
|
---|
157 | f1 = 0.; f2 = -1.; df = 1.;
|
---|
158 | if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
|
---|
159 | varname = args[0];
|
---|
160 | if (isalpha((int)args[1][0]) ) { // This is a foreach-integer bloc
|
---|
161 | for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
|
---|
162 | typ = 0;
|
---|
163 | }
|
---|
164 | else { // This is an integer or float loop
|
---|
165 | size_t l = args[1].length();
|
---|
166 | size_t p = args[1].find(':');
|
---|
167 | size_t pp = args[1].find('.');
|
---|
168 | bool fl = (pp < l) ? true : false; // Float loop or integer loop
|
---|
169 | if (p >= l) return; // Syntaxe error
|
---|
170 | string a1 = args[1].substr(0, p);
|
---|
171 | string aa = args[1].substr(p+1);
|
---|
172 | p = aa.find(':');
|
---|
173 | string a2, a3;
|
---|
174 | bool hasa3 = false;
|
---|
175 | if (p < aa.length() ) {
|
---|
176 | a2 = aa.substr(0,p);
|
---|
177 | a3 = aa.substr(p+1);
|
---|
178 | hasa3 = true;
|
---|
179 | }
|
---|
180 | else a2 = aa;
|
---|
181 | if (fl) {
|
---|
182 | typ = 2;
|
---|
183 | f1 = atof(a1.c_str());
|
---|
184 | f2 = atof(a2.c_str());
|
---|
185 | if (hasa3) df = atof(a3.c_str());
|
---|
186 | else df = 1.;
|
---|
187 | }
|
---|
188 | else {
|
---|
189 | typ = 1;
|
---|
190 | i1 = atoi(a1.c_str());
|
---|
191 | i2 = atoi(a2.c_str());
|
---|
192 | if (hasa3) di = atoi(a3.c_str());
|
---|
193 | else di = 1;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* --Methode-- */
|
---|
199 | PIACmdBloc::~PIACmdBloc()
|
---|
200 | {
|
---|
201 | for(int k=0; k<blocs.size(); k++) delete blocs[k];
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* --Methode-- */
|
---|
205 | PIACmdBloc* PIACmdBloc::Execute()
|
---|
206 | {
|
---|
207 | // cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
|
---|
208 | // " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
|
---|
209 | string cmd;
|
---|
210 | int k=0;
|
---|
211 | int kj=0;
|
---|
212 | int kk=0;
|
---|
213 | char buff[32];
|
---|
214 | if (typ == 0) // foreach string loop
|
---|
215 | for(k=0; k<strlist.size(); k++) {
|
---|
216 | cmd = "set " + varname + " " + strlist[k];
|
---|
217 | piacmd->Interpret(cmd);
|
---|
218 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
219 | kk = bloclineid[kj];
|
---|
220 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
221 | else blocs[-kk-1]->Execute();
|
---|
222 | }
|
---|
223 | }
|
---|
224 | else if (typ == 1) // Integer loop
|
---|
225 | for(int i=i1; i<i2; i+=di) {
|
---|
226 | k++;
|
---|
227 | if (++k > 9999) {
|
---|
228 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | sprintf(buff, " %d", i);
|
---|
232 | cmd = "set " + varname + buff;
|
---|
233 | piacmd->Interpret(cmd);
|
---|
234 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
235 | kk = bloclineid[kj];
|
---|
236 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
237 | else blocs[-kk-1]->Execute();
|
---|
238 | }
|
---|
239 | }
|
---|
240 | else if (typ == 2) // float loop
|
---|
241 | for(float f=f1; f<f2; f+=df) {
|
---|
242 | k++;
|
---|
243 | if (++k > 9999) {
|
---|
244 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
---|
245 | break;
|
---|
246 | }
|
---|
247 | sprintf(buff, " %g", f);
|
---|
248 | cmd = "set " + varname + buff;
|
---|
249 | piacmd->Interpret(cmd);
|
---|
250 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
251 | kk = bloclineid[kj];
|
---|
252 | if (kk > 0) piacmd->Interpret(lines[kk-1]);
|
---|
253 | else blocs[-kk-1]->Execute();
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | return(parent);
|
---|
258 | }
|
---|
259 |
|
---|
260 | // ------------------------------------------------------------
|
---|
261 | // Classe PIACmd
|
---|
262 | // ------------------------------------------------------------
|
---|
263 |
|
---|
264 | static PIACmd* curpiacmd = NULL;
|
---|
265 | /* --Methode-- */
|
---|
266 | PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
|
---|
267 | {
|
---|
268 | mObjMgr = omg;
|
---|
269 | mImgApp = app;
|
---|
270 | system("cp history.pic hisold.pic");
|
---|
271 | hist.open("history.pic");
|
---|
272 | histon = true;
|
---|
273 | trace = false; timing = false;
|
---|
274 | gltimer = NULL;
|
---|
275 | curblk = NULL;
|
---|
276 | felevel = 0;
|
---|
277 |
|
---|
278 | cmdhgrp["All"] = 0;
|
---|
279 | cmdgrpid = 1;
|
---|
280 | cmdhgrp["Commands"] = 1;
|
---|
281 | helpwin = new PIAHelpWind(app, this);
|
---|
282 | helpwin->AddHelpGroup("All", 0);
|
---|
283 | helpwin->AddHelpGroup("Commands", 1);
|
---|
284 |
|
---|
285 | string kw = "piacmd";
|
---|
286 | string usage;
|
---|
287 | usage = ">>> (piacmd) Interpreter's keywords : \n";
|
---|
288 | usage += " > set varname string # To set a variable, $varname \n";
|
---|
289 | usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
|
---|
290 | usage += " > setol varname patt # Fills varname with object list \n";
|
---|
291 | usage += " > unset varname # clear variable definition \n";
|
---|
292 | usage += " > echo string # output string \n";
|
---|
293 | usage += " > foreach varname string-list # Loop \n";
|
---|
294 | usage += " > foreach varname i1:i2[:di] # Integer loop \n";
|
---|
295 | usage += " > foreach varname f1:f2[:df] # Float loop \n";
|
---|
296 | usage += " > end # end loops \n";
|
---|
297 | usage += " > listvars # List of variable names and values \n";
|
---|
298 | usage += " > listcommands # List of all known commands \n";
|
---|
299 | usage += " > exec filename # Execute commands from file \n";
|
---|
300 | usage += " > shell comand_string # Execute shell command \n";
|
---|
301 | usage += " > help <command_name> # <command_name> usage info \n";
|
---|
302 | usage += " > helpwindow # Displays help window \n";
|
---|
303 | usage += " > timingon timingoff traceon traceoff \n";
|
---|
304 | string grp = "Commands";
|
---|
305 | RegisterHelp(kw, usage, grp);
|
---|
306 |
|
---|
307 | basexec = new PIABaseExecutor(this, omg, app);
|
---|
308 | AddInterpreter(this);
|
---|
309 | curcmdi = this;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* --Methode-- */
|
---|
313 | PIACmd::~PIACmd()
|
---|
314 | {
|
---|
315 | hist.close();
|
---|
316 | if (gltimer) { delete gltimer; gltimer = NULL; }
|
---|
317 | Modmap::iterator it;
|
---|
318 | for(it = modmap.begin(); it != modmap.end(); it++) {
|
---|
319 | string name = (*it).first + "_end";
|
---|
320 | DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
|
---|
321 | if (fend) fend();
|
---|
322 | delete (*it).second;
|
---|
323 | }
|
---|
324 | delete helpwin;
|
---|
325 | if (curpiacmd == this) curpiacmd = NULL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* --Methode-- */
|
---|
329 | PIACmd* PIACmd::GetInterpreter()
|
---|
330 | {
|
---|
331 | return(curpiacmd);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /* --Methode-- */
|
---|
335 | string PIACmd::Name()
|
---|
336 | {
|
---|
337 | return("piacmd");
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* --Methode-- */
|
---|
341 | void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
|
---|
342 | {
|
---|
343 | if (!ce) {
|
---|
344 | RegisterHelp(keyw, usage, grp);
|
---|
345 | return;
|
---|
346 | }
|
---|
347 | int gid = CheckHelpGrp(grp);
|
---|
348 | cmdex cme;
|
---|
349 | cme.group = gid;
|
---|
350 | cme.us = usage;
|
---|
351 | cme.cex = ce;
|
---|
352 | cmdexmap[keyw] = cme;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /* --Methode-- */
|
---|
356 | void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
|
---|
357 | {
|
---|
358 | int gid = CheckHelpGrp(grp);
|
---|
359 | cmdex cme;
|
---|
360 | cme.group = gid;
|
---|
361 | cme.us = usage;
|
---|
362 | cme.cex = NULL;
|
---|
363 | helpexmap[keyw] = cme;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /* --Methode-- */
|
---|
367 | int PIACmd::CheckHelpGrp(string& grp)
|
---|
368 | {
|
---|
369 | int gid=0;
|
---|
370 | CmdHGroup::iterator it = cmdhgrp.find(grp);
|
---|
371 | if (it == cmdhgrp.end()) {
|
---|
372 | cmdgrpid++; gid = cmdgrpid;
|
---|
373 | cmdhgrp[grp] = gid;
|
---|
374 | helpwin->AddHelpGroup(grp.c_str(), gid);
|
---|
375 | }
|
---|
376 | else gid = (*it).second;
|
---|
377 | return(gid);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* --Methode-- */
|
---|
381 | void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
|
---|
382 | {
|
---|
383 | helpwin->ClearHelpList();
|
---|
384 | CmdExmap::iterator it;
|
---|
385 | for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
|
---|
386 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
---|
387 | helpwin->AddHelpItem((*it).first.c_str());
|
---|
388 | }
|
---|
389 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
---|
390 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
---|
391 | helpwin->AddHelpItem((*it).first.c_str());
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | /* --Methode-- */
|
---|
396 | void PIACmd::LoadModule(string& fnameso, string& name)
|
---|
397 | {
|
---|
398 | PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
|
---|
399 | if (dynlink == NULL) {
|
---|
400 | cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
|
---|
401 | return;
|
---|
402 | }
|
---|
403 | string fname = name + "_init";
|
---|
404 | DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
|
---|
405 | if (!finit) {
|
---|
406 | cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | cout << "PIACmd/LoadModule_Info: Initialisation module" << name
|
---|
410 | << " " << fname << "() ..." << endl;
|
---|
411 | finit();
|
---|
412 | modmap[name] = dynlink;
|
---|
413 | return;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* --Methode-- */
|
---|
417 | void PIACmd::AddInterpreter(CmdInterpreter * cl)
|
---|
418 | {
|
---|
419 | if (!cl) return;
|
---|
420 | interpmap[cl->Name()] = cl;}
|
---|
421 |
|
---|
422 | /* --Methode-- */
|
---|
423 | void PIACmd::SelInterpreter(string& name)
|
---|
424 | {
|
---|
425 | InterpMap::iterator it = interpmap.find(name);
|
---|
426 | if (it == interpmap.end()) return;
|
---|
427 | curcmdi = (*it).second;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | // Pour le decoupage des commandes en lignes
|
---|
432 | typedef vector<string> cmdtok;
|
---|
433 |
|
---|
434 | /* --Methode-- */
|
---|
435 | int PIACmd::Interpret(string& s)
|
---|
436 | {
|
---|
437 | int rc = 0;
|
---|
438 | cmdtok tokens;
|
---|
439 | CmdVarList::iterator it;
|
---|
440 |
|
---|
441 | // Removing leading blanks
|
---|
442 | size_t p,q,q2,l;
|
---|
443 | l = s.length();
|
---|
444 | if (l < 1) return(0);
|
---|
445 | if (s[0] == '#') return(0); // si c'est un commentaire
|
---|
446 | p=s.find_first_not_of(" \t");
|
---|
447 | if (p < l) s = s.substr(p);
|
---|
448 | // else return(0);
|
---|
449 |
|
---|
450 | // On enregistre les commandes
|
---|
451 | if (histon) hist << s << endl;
|
---|
452 |
|
---|
453 |
|
---|
454 | string toks,kw;
|
---|
455 |
|
---|
456 | // >>>> Substitution d'alias (1er mot)
|
---|
457 | int als = 0;
|
---|
458 | while (als < 2) {
|
---|
459 | p = s.find_first_not_of(" ");
|
---|
460 | s = s.substr(p);
|
---|
461 | p = 0;
|
---|
462 | q = s.find_first_of(" ");
|
---|
463 | l = s.length();
|
---|
464 | string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
|
---|
465 | it = mAliases.find(w1);
|
---|
466 | if (it != mAliases.end()) s = (*it).second + s.substr(q);
|
---|
467 | else als++;
|
---|
468 | als++;
|
---|
469 | }
|
---|
470 |
|
---|
471 | // >>>> Separating keyword
|
---|
472 | if (q < l)
|
---|
473 | { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
|
---|
474 | else { kw = s.substr(p,l-p); toks = ""; }
|
---|
475 |
|
---|
476 | // On verifie si nous sommes dans un bloc
|
---|
477 | if ( (curblk != NULL) && (kw != "foreach") ) { // On est dans un bloc
|
---|
478 | if (kw != "end") { curblk->AddLine(s); return(0); }
|
---|
479 | else {
|
---|
480 | PIACmdBloc* curb = curblk;
|
---|
481 | curblk = curb->Parent();
|
---|
482 | felevel--;
|
---|
483 | if (curblk == NULL) {
|
---|
484 | mImgApp->GetConsole()->SetPrompt("Cmd> ");
|
---|
485 | // cout << " *DBG* Executing bloc " << endl;
|
---|
486 | curb->Execute();
|
---|
487 | }
|
---|
488 | else {
|
---|
489 | char prompt[64];
|
---|
490 | sprintf(prompt, "foreach-%d> ", felevel);
|
---|
491 | mImgApp->GetConsole()->SetPrompt(prompt);
|
---|
492 | }
|
---|
493 | return(0);
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | // Nous ne sommes donc pas dans un bloc ....
|
---|
498 |
|
---|
499 | // >>>> Variable substitution
|
---|
500 | string s2="";
|
---|
501 | p = 0;
|
---|
502 | l = s.length();
|
---|
503 | string vn;
|
---|
504 | while (p < l) {
|
---|
505 | q = s.find('$',p);
|
---|
506 | // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
|
---|
507 | if (q > l) break;
|
---|
508 | if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
|
---|
509 | s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
|
---|
510 | continue;
|
---|
511 | }
|
---|
512 | if (q >= l-1) {
|
---|
513 | cerr << " Syntax error !!! " << endl;
|
---|
514 | return(0);
|
---|
515 | }
|
---|
516 | vn = "";
|
---|
517 | if ( s[q+1] == '{' ) { // Variable in the form ${name}
|
---|
518 | q2 = s.find('}',q+1);
|
---|
519 | if (q2 >= l) {
|
---|
520 | cerr << " Syntax error !!! " << endl;
|
---|
521 | return(0);
|
---|
522 | }
|
---|
523 | vn = s.substr(q+2,q2-q-2);
|
---|
524 | q2++;
|
---|
525 | }
|
---|
526 | else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
|
---|
527 | q2 = s.find(']',q+1);
|
---|
528 | if (q2 >= l) {
|
---|
529 | cerr << " Syntax error !!! " << endl;
|
---|
530 | return(0);
|
---|
531 | }
|
---|
532 | vn = s.substr(q+2,q2-q-2);
|
---|
533 | it = mVars.find(vn);
|
---|
534 | if ( (vn.length() < 1) || (it == mVars.end()) ) {
|
---|
535 | cerr << " Error: Undefined variable " << vn << " ! " << endl;
|
---|
536 | return(0);
|
---|
537 | }
|
---|
538 | vn = mVars[vn];
|
---|
539 | q2++;
|
---|
540 | }
|
---|
541 | else {
|
---|
542 | q2 = s.find_first_of(" .:/,]()$",q+1);
|
---|
543 | if (q2 > l) q2 = l;
|
---|
544 | vn = s.substr(q+1, q2-q-1);
|
---|
545 | }
|
---|
546 | it = mVars.find(vn);
|
---|
547 | if ( (vn.length() < 1) || (it == mVars.end()) ) {
|
---|
548 | cerr << " Error: Undefined variable " << vn << " ! " << endl;
|
---|
549 | return(0);
|
---|
550 | }
|
---|
551 | s2 += (s.substr(p, q-p) + (*it).second);
|
---|
552 | p = q2;
|
---|
553 | }
|
---|
554 | if (p < l) s2 += s.substr(p);
|
---|
555 |
|
---|
556 | p = s2.find_first_not_of(" \t");
|
---|
557 | if (p < l) s2 = s2.substr(p);
|
---|
558 |
|
---|
559 |
|
---|
560 | // >>>> Separating keyword and tokens
|
---|
561 | q = s2.find(' ');
|
---|
562 | l = s2.length();
|
---|
563 | if (q < l)
|
---|
564 | { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
|
---|
565 | else { kw = s2; toks = ""; }
|
---|
566 |
|
---|
567 | q = 0;
|
---|
568 | while (q < l) {
|
---|
569 | p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
|
---|
570 | if (p>=l) break;
|
---|
571 | q = toks.find_first_of(" \t",p); // la fin du token;
|
---|
572 | string token = toks.substr(p,q-p);
|
---|
573 | tokens.push_back(token);
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | // Si c'est un foreach, on cree un nouveau bloc
|
---|
578 | if (kw == "foreach") {
|
---|
579 | // cout << " *DBG* We got a foreach... " << endl;
|
---|
580 | PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
|
---|
581 | if (!bloc->CheckOK()) {
|
---|
582 | cerr << "foreach syntax Error ! " << endl;
|
---|
583 | delete bloc;
|
---|
584 | return(0);
|
---|
585 | }
|
---|
586 | felevel++;
|
---|
587 | if (curblk) curblk->AddBloc(bloc);
|
---|
588 | else {
|
---|
589 | char prompt[64];
|
---|
590 | sprintf(prompt, "foreach-%d> ", felevel);
|
---|
591 | mImgApp->GetConsole()->SetPrompt(prompt);
|
---|
592 | }
|
---|
593 | curblk = bloc;
|
---|
594 | // cout << " *DBG* New Bloc created ... " << endl;
|
---|
595 | return(0);
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | // cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
|
---|
600 | // for(int ii=0; ii<tokens.size(); ii++)
|
---|
601 | // cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
|
---|
602 |
|
---|
603 | // >>>>>>>>>>> Commande d'interpreteur
|
---|
604 | else if (kw == "helpwindow") ShowHelpWindow();
|
---|
605 | else if (kw == "help") {
|
---|
606 | if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
|
---|
607 | else {
|
---|
608 | string kwh = "piacmd";
|
---|
609 | cout << GetUsage(kwh) << endl;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | else if (kw == "set") {
|
---|
614 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
|
---|
615 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
616 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
617 | return(0);
|
---|
618 | }
|
---|
619 | string xx = tokens[1];
|
---|
620 | for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
|
---|
621 | mVars[tokens[0]] = xx;
|
---|
622 | }
|
---|
623 |
|
---|
624 | else if (kw == "getvar") {
|
---|
625 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
|
---|
626 | it = mVars.find(tokens[1]);
|
---|
627 | if (it == mVars.end()) {
|
---|
628 | cerr << "Error - No " << tokens[1] << " Variable " << endl;
|
---|
629 | return(0);
|
---|
630 | }
|
---|
631 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
632 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
633 | return(0);
|
---|
634 | }
|
---|
635 | mVars[tokens[0]] = (*it).second;
|
---|
636 | }
|
---|
637 |
|
---|
638 | else if (kw == "alias") {
|
---|
639 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
|
---|
640 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
641 | cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
|
---|
642 | return(0);
|
---|
643 | }
|
---|
644 | string xx = tokens[1];
|
---|
645 | for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
|
---|
646 | mAliases[tokens[0]] = xx;
|
---|
647 | }
|
---|
648 |
|
---|
649 | else if (kw == "setol") {
|
---|
650 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
|
---|
651 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
652 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
653 | return(0);
|
---|
654 | }
|
---|
655 | vector<string> ol;
|
---|
656 | mObjMgr->GetObjList(tokens[1], ol);
|
---|
657 | string vol;
|
---|
658 | if (ol.size() < 1) vol = "";
|
---|
659 | else {
|
---|
660 | vol = ol[0];
|
---|
661 | for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
|
---|
662 | }
|
---|
663 | mVars[tokens[0]] = vol;
|
---|
664 | }
|
---|
665 | else if (kw == "unset") {
|
---|
666 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
|
---|
667 | CmdVarList::iterator it = mVars.find(tokens[0]);
|
---|
668 | if (it != mVars.end()) mVars.erase(it);
|
---|
669 | else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
|
---|
670 | }
|
---|
671 | else if (kw == "echo") {
|
---|
672 | for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
|
---|
673 | cout << endl;
|
---|
674 | }
|
---|
675 | else if (kw == "listvars") {
|
---|
676 | cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
|
---|
677 | CmdVarList::iterator it;
|
---|
678 | for(it = mVars.begin(); it != mVars.end(); it++)
|
---|
679 | cout << (*it).first << " = " << (*it).second << "\n";
|
---|
680 | cout << endl;
|
---|
681 | }
|
---|
682 | else if (kw == "listvars") {
|
---|
683 | cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
|
---|
684 | CmdExmap::iterator it;
|
---|
685 | int kc = 0;
|
---|
686 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
---|
687 | cout << (*it).first << " ";
|
---|
688 | kc++;
|
---|
689 | if (kc >= 5) { cout << "\n"; kc = 0; }
|
---|
690 | }
|
---|
691 | cout << endl;
|
---|
692 | }
|
---|
693 | else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
|
---|
694 | else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
|
---|
695 | else if (kw == "timingon") {
|
---|
696 | cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
|
---|
697 | if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
|
---|
698 | }
|
---|
699 | else if (kw == "timingoff") {
|
---|
700 | cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
|
---|
701 | if (gltimer) delete gltimer; gltimer = NULL; timing = false;
|
---|
702 | }
|
---|
703 | else if (kw == "exec") {
|
---|
704 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
|
---|
705 | ExecFile(tokens[0], tokens);
|
---|
706 | }
|
---|
707 | else if (kw == "shell") {
|
---|
708 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
|
---|
709 | system(toks.c_str());
|
---|
710 | }
|
---|
711 | // Execution d'une commande enregistree
|
---|
712 | else rc = ExecuteCommand(kw, tokens);
|
---|
713 |
|
---|
714 | if (timing) gltimer->Split();
|
---|
715 | return(rc);
|
---|
716 | }
|
---|
717 |
|
---|
718 | /* --Methode-- */
|
---|
719 | int PIACmd::ExecuteCommandLine(string& line)
|
---|
720 | {
|
---|
721 | cmdtok tokens;
|
---|
722 | if (line.length() < 1) return(0);
|
---|
723 |
|
---|
724 | string toks,kw;
|
---|
725 | size_t p = line.find_first_not_of(" ");
|
---|
726 | line = line.substr(p);
|
---|
727 | p = 0;
|
---|
728 | size_t q = line.find_first_of(" ");
|
---|
729 | size_t l = line.length();
|
---|
730 |
|
---|
731 | if (q < l)
|
---|
732 | { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
|
---|
733 | else { kw = line.substr(p,l-p); toks = ""; }
|
---|
734 |
|
---|
735 | q = 0;
|
---|
736 | while (q < l) {
|
---|
737 | p = toks.find_first_not_of(" ",q+1); // au debut d'un token
|
---|
738 | if (p>=l) break;
|
---|
739 | q = toks.find_first_of(" ",p); // la fin du token;
|
---|
740 | string token = toks.substr(p,q-p);
|
---|
741 | tokens.push_back(token);
|
---|
742 | }
|
---|
743 |
|
---|
744 | return(ExecuteCommand(kw, tokens));
|
---|
745 | }
|
---|
746 |
|
---|
747 | /* --Methode-- */
|
---|
748 | int PIACmd::ExecuteCommand(string& keyw, vector<string>& args)
|
---|
749 | {
|
---|
750 | int rc = -1;
|
---|
751 | CmdExmap::iterator it = cmdexmap.find(keyw);
|
---|
752 | if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
|
---|
753 | else {
|
---|
754 | if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args);
|
---|
755 | else cout << "Dont know how to execute " << keyw << " ? " << endl;
|
---|
756 | }
|
---|
757 | return(rc);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /* --Methode-- */
|
---|
761 | int PIACmd::ExecFile(string& file, vector<string>& args)
|
---|
762 | {
|
---|
763 | char line_buff[512];
|
---|
764 | FILE *fip;
|
---|
765 |
|
---|
766 | if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
|
---|
767 | cerr << "PIACmd::Exec() Error opening file " << file << endl;
|
---|
768 | hist << "##! PIACmd::Exec() Error opening file " << file << endl;
|
---|
769 | return(0);
|
---|
770 | }
|
---|
771 |
|
---|
772 | // hist << "### Executing commands from " << file << endl;
|
---|
773 |
|
---|
774 | // Setting $0 ... $99 variables
|
---|
775 | int k;
|
---|
776 | CmdVarList::iterator it;
|
---|
777 | char buff[32];
|
---|
778 | // First, we clear all previous values
|
---|
779 | string vn="#";
|
---|
780 | it = mVars.find(vn);
|
---|
781 | if (it != mVars.end()) mVars.erase(it);
|
---|
782 | for(k=0; k<99; k++) {
|
---|
783 | sprintf(buff,"%d",k);
|
---|
784 | vn = buff;
|
---|
785 | it = mVars.find(vn);
|
---|
786 | if (it != mVars.end()) mVars.erase(it);
|
---|
787 | }
|
---|
788 | // We then set them
|
---|
789 | vn="#";
|
---|
790 | sprintf(buff,"%d",(int)args.size());
|
---|
791 | mVars[vn] = buff;
|
---|
792 | for(k=0; k<args.size(); k++) {
|
---|
793 | sprintf(buff,"%d",k);
|
---|
794 | vn = buff;
|
---|
795 | mVars[vn] = args[k];
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (trace) {
|
---|
799 | mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
|
---|
800 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
---|
801 | mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
|
---|
802 | }
|
---|
803 |
|
---|
804 | histon = false;
|
---|
805 | while (fgets(line_buff,511,fip) != NULL)
|
---|
806 | {
|
---|
807 | if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
|
---|
808 | line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
|
---|
809 | string line(line_buff);
|
---|
810 | Interpret(line);
|
---|
811 | }
|
---|
812 | histon = false;
|
---|
813 |
|
---|
814 | // hist << "### End of Exec( " << file << " ) " << endl;
|
---|
815 | if (trace) {
|
---|
816 | mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
|
---|
817 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
---|
818 | mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
|
---|
819 | }
|
---|
820 |
|
---|
821 | return(0);
|
---|
822 | }
|
---|
823 |
|
---|
824 |
|
---|
825 | static string* videstr = NULL;
|
---|
826 | /* --Methode-- */
|
---|
827 | string& PIACmd::GetUsage(const string& kw)
|
---|
828 | {
|
---|
829 | bool fndok = false;
|
---|
830 | CmdExmap::iterator it = cmdexmap.find(kw);
|
---|
831 | if (it == cmdexmap.end()) {
|
---|
832 | it = helpexmap.find(kw);
|
---|
833 | if (it != helpexmap.end()) fndok = true;
|
---|
834 | }
|
---|
835 | else fndok = true;
|
---|
836 | if (fndok) return( (*it).second.us );
|
---|
837 | // Keyword pas trouve
|
---|
838 | if (videstr == NULL) videstr = new string("");
|
---|
839 | *videstr = "Nothing known about " + kw + " ?? ";
|
---|
840 | return(*videstr);
|
---|
841 |
|
---|
842 | }
|
---|
843 |
|
---|
844 | /* --Methode-- */
|
---|
845 | void PIACmd::ShowHelpWindow()
|
---|
846 | {
|
---|
847 | helpwin->Show();
|
---|
848 | }
|
---|