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

Last change on this file since 357 was 357, checked in by ercodmgr, 26 years ago

Ajout de parametres de Loop (plot2d, ...), Ajout de fonction de
remplissage de matrices et vecteurs a partir de NTuples,
Rationalisation des commandes (arguments par defauts ...) Reza 6/8/99

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