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