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

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

Fin lecture NtupleASCII, amelioration/niveau de print ds NamedObjMgr Reza 1/10/99

File size: 24.5 KB
Line 
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
25class PIAHelpWind : public PIWindow {
26public :
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); }
37protected :
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-- */
49PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
50 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
51{
52dap = par;
53piac = piacmd;
54mNitem = 0;
55SetMsg(77);
56
57int bsx, bsy;
58int tsx, tsy;
59int spx, spy;
60PIApplicationPrefCompSize(bsx, bsy);
61spx = bsx/6; spy = bsy/6;
62tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
63SetSize(tsx,tsy);
64hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
65hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
66hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
67hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
68// hitemlist->SetBorderWidth(2);
69mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
70// mTxt->SetMutiLineMode(true);
71mTxt->SetTextEditable(false);
72mTxt->SetText("");
73mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
74mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
75mLab->SetBorderWidth(1);
76mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
77mLab->SetLabel("");
78mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
79mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
80}
81
82/* --Methode-- */
83PIAHelpWind::~PIAHelpWind()
84{
85delete hgrpom;
86delete hitemlist;
87delete mTxt;
88delete mLab;
89delete mBut;
90}
91
92/* --Methode-- */
93void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
94{
95PIMessage um = UserMsg(msg);
96if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
97 Hide();
98 return;
99 }
100else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
101 mTxt->SetText("");
102 mLab->SetLabel("");
103 piac->UpdateHelpList(this, um-20000);
104}
105else 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-- */
113void PIAHelpWind::Show()
114{
115hgrpom->SetValue(20000); // Groupe All
116mTxt->SetText("");
117mLab->SetLabel("");
118piac->UpdateHelpList(this, 0);
119PIWindow::Show();
120}
121
122// ------------------------------------------------------------
123// Bloc de commandes (Foreach, ...)
124// Classe PIACmdBloc
125// ------------------------------------------------------------
126
127class PIACmdBloc {
128public:
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();
138protected:
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-- */
152PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args)
153{
154piacmd = piac;
155parent = par;
156typ = -1;
157i1 = 0; i2 = -1; di = 1;
158f1 = 0.; f2 = -1.; df = 1.;
159if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
160varname = args[0];
161if (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 }
165else { // 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-- */
200PIACmdBloc::~PIACmdBloc()
201{
202for(int k=0; k<blocs.size(); k++) delete blocs[k];
203}
204
205/* --Methode-- */
206PIACmdBloc* PIACmdBloc::Execute()
207{
208// cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
209// " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
210string cmd;
211int k=0;
212int kj=0;
213int kk=0;
214char buff[32];
215if (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 }
225else 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 }
241else 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
258return(parent);
259}
260
261// ------------------------------------------------------------
262// Classe PIACmd
263// ------------------------------------------------------------
264
265static PIACmd* curpiacmd = NULL;
266/* --Methode-- */
267PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
268{
269mObjMgr = omg;
270mImgApp = app;
271system("cp history.pic hisold.pic");
272hist.open("history.pic");
273histon = true;
274trace = false; timing = false;
275gltimer = NULL;
276curblk = NULL;
277felevel = 0;
278
279cmdhgrp["All"] = 0;
280cmdgrpid = 1;
281cmdhgrp["Commands"] = 1;
282helpwin = new PIAHelpWind(app, this);
283helpwin->AddHelpGroup("All", 0);
284helpwin->AddHelpGroup("Commands", 1);
285
286string kw = "piacmd";
287string usage;
288usage = ">>> (piacmd) Interpreter's keywords : \n";
289usage += " > set varname string # To set a variable, $varname \n";
290usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
291usage += " > setol varname patt # Fills varname with object list \n";
292usage += " > unset varname # clear variable definition \n";
293usage += " > echo string # output string \n";
294usage += " > alias name string # define a command alias \n";
295usage += " > readstdin varname # reads a line from stdin into $varname \n";
296usage += " > foreach varname string-list # Loop \n";
297usage += " > foreach varname i1:i2[:di] # Integer loop \n";
298usage += " > foreach varname f1:f2[:df] # Float loop \n";
299usage += " > end # end loops \n";
300usage += " > listvars # List of variable names and values \n";
301usage += " > listalias # List of alias names and values \n";
302usage += " > listcommands # List of all known commands \n";
303usage += " > exec filename # Execute commands from file \n";
304usage += " > shell comand_string # Execute shell command \n";
305usage += " > help <command_name> # <command_name> usage info \n";
306usage += " > helpwindow # Displays help window \n";
307usage += " > timingon timingoff traceon traceoff \n";
308string grp = "Commands";
309RegisterHelp(kw, usage, grp);
310
311basexec = new PIABaseExecutor(this, omg, app);
312fitexec = new PIAFitter(this, app);
313AddInterpreter(this);
314curcmdi = this;
315}
316
317/* --Methode-- */
318PIACmd::~PIACmd()
319{
320hist.close();
321if (gltimer) { delete gltimer; gltimer = NULL; }
322Modmap::iterator it;
323for(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 }
329delete helpwin;
330if (curpiacmd == this) curpiacmd = NULL;
331delete basexec;
332delete fitexec;
333}
334
335/* --Methode-- */
336PIACmd* PIACmd::GetInterpreter()
337{
338return(curpiacmd);
339}
340
341/* --Methode-- */
342string PIACmd::Name()
343{
344return("piacmd");
345}
346
347/* --Methode-- */
348void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
349{
350if (!ce) {
351 RegisterHelp(keyw, usage, grp);
352 return;
353 }
354int gid = CheckHelpGrp(grp);
355cmdex cme;
356cme.group = gid;
357cme.us = usage;
358cme.cex = ce;
359cmdexmap[keyw] = cme;
360}
361
362/* --Methode-- */
363void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
364{
365int gid = CheckHelpGrp(grp);
366cmdex cme;
367cme.group = gid;
368cme.us = usage;
369cme.cex = NULL;
370helpexmap[keyw] = cme;
371}
372
373/* --Methode-- */
374int PIACmd::CheckHelpGrp(string& grp)
375{
376int gid=0;
377CmdHGroup::iterator it = cmdhgrp.find(grp);
378if (it == cmdhgrp.end()) {
379 cmdgrpid++; gid = cmdgrpid;
380 cmdhgrp[grp] = gid;
381 helpwin->AddHelpGroup(grp.c_str(), gid);
382 }
383else gid = (*it).second;
384return(gid);
385}
386
387/* --Methode-- */
388void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
389{
390helpwin->ClearHelpList();
391CmdExmap::iterator it;
392for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
393 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
394 helpwin->AddHelpItem((*it).first.c_str());
395 }
396for(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-- */
403void PIACmd::LoadModule(string& fnameso, string& name)
404{
405PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
406if (dynlink == NULL) {
407 cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
408 return;
409 }
410string fname = name + "_init";
411DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
412if (!finit) {
413 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
414 return;
415 }
416cout << "PIACmd/LoadModule_Info: Initialisation module" << name
417 << " " << fname << "() ..." << endl;
418finit();
419modmap[name] = dynlink;
420return;
421}
422
423/* --Methode-- */
424void PIACmd::AddInterpreter(CmdInterpreter * cl)
425{
426if (!cl) return;
427interpmap[cl->Name()] = cl;}
428
429/* --Methode-- */
430void PIACmd::SelInterpreter(string& name)
431{
432InterpMap::iterator it = interpmap.find(name);
433if (it == interpmap.end()) return;
434curcmdi = (*it).second;
435}
436
437
438// Pour le decoupage des commandes en lignes
439typedef vector<string> cmdtok;
440
441/* Fonction */
442static string GetStringFrStdin(PIACmd* piac)
443{
444PIStdImgApp* piapp = piac->GetImgApp();
445if (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}
453char buff[128];
454fgets(buff, 128, stdin);
455buff[127] = '\0';
456return((string)buff);
457}
458
459/* --Methode-- */
460int PIACmd::Interpret(string& s)
461{
462int rc = 0;
463cmdtok tokens;
464CmdVarList::iterator it;
465
466// Removing leading blanks
467size_t p,q,q2,l;
468l = s.length();
469if (l < 1) return(0);
470if (s[0] == '#') return(0); // si c'est un commentaire
471p=s.find_first_not_of(" \t");
472if (p < l) s = s.substr(p);
473// else return(0);
474
475// On enregistre les commandes
476if (histon) hist << s << endl;
477
478
479string toks,kw;
480
481// >>>> Substitution d'alias (1er mot)
482p = 0;
483q = s.find_first_of(" \t");
484l = s.length();
485string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
486it = mAliases.find(w1);
487if (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
497if (q < l)
498 { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
499else { kw = s.substr(p,l-p); toks = ""; }
500
501// On verifie si nous sommes dans un bloc
502if ( (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
525string s2="";
526p = 0;
527l = s.length();
528string vn;
529while (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 }
579if (p < l) s2 += s.substr(p);
580
581p = s2.find_first_not_of(" \t");
582if (p < l) s2 = s2.substr(p);
583
584
585// >>>> Separating keyword and tokens
586q = s2.find(' ');
587l = s2.length();
588if (q < l)
589 { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
590else { kw = s2; toks = ""; }
591
592q = 0;
593while (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
603if (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
629else if (kw == "helpwindow") ShowHelpWindow();
630else 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
638else 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
649else 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
663else 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
674else 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 }
690else 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 }
696else if (kw == "echo") {
697 for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
698 cout << endl;
699 }
700
701else 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
712else 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 }
719else 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 }
726else 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 }
737else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
738else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
739else if (kw == "timingon") {
740 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
741 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
742 }
743else if (kw == "timingoff") {
744 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
745 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
746 }
747else if (kw == "exec") {
748 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
749 ExecFile(tokens[0], tokens);
750 }
751else 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
756else rc = ExecuteCommand(kw, tokens);
757
758if (timing) gltimer->Split();
759return(rc);
760}
761
762/* --Methode-- */
763int PIACmd::ExecuteCommandLine(string& line)
764{
765cmdtok tokens;
766if (line.length() < 1) return(0);
767
768string toks,kw;
769size_t p = line.find_first_not_of(" ");
770line = line.substr(p);
771p = 0;
772size_t q = line.find_first_of(" ");
773size_t l = line.length();
774
775if (q < l)
776 { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
777else { kw = line.substr(p,l-p); toks = ""; }
778
779q = 0;
780while (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
788return(ExecuteCommand(kw, tokens));
789}
790
791/* --Methode-- */
792int 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-- */
805int PIACmd::ExecFile(string& file, vector<string>& args)
806{
807char line_buff[512];
808FILE *fip;
809
810if ( (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
819if(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
828int k;
829CmdVarList::iterator it;
830char buff[32];
831// First, we clear all previous values
832string vn="#";
833it = mVars.find(vn);
834if (it != mVars.end()) mVars.erase(it);
835for(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
842vn="#";
843sprintf(buff,"%d",(int)args.size());
844mVars[vn] = buff;
845for(k=0; k<args.size(); k++) {
846 sprintf(buff,"%d",k);
847 vn = buff;
848 mVars[vn] = args[k];
849 }
850
851if (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
857histon = false;
858while (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 }
865histon = true;
866
867// hist << "### End of Exec( " << file << " ) " << endl;
868if (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
874return(0);
875}
876
877
878static string* videstr = NULL;
879/* --Methode-- */
880string& PIACmd::GetUsage(const string& kw)
881{
882bool fndok = false;
883CmdExmap::iterator it = cmdexmap.find(kw);
884if (it == cmdexmap.end()) {
885 it = helpexmap.find(kw);
886 if (it != helpexmap.end()) fndok = true;
887 }
888 else fndok = true;
889if (fndok) return( (*it).second.us );
890// Keyword pas trouve
891if (videstr == NULL) videstr = new string("");
892*videstr = "Nothing known about " + kw + " ?? ";
893return(*videstr);
894
895}
896
897/* --Methode-- */
898void PIACmd::ShowHelpWindow()
899{
900helpwin->Show();
901}
902
903
Note: See TracBrowser for help on using the repository browser.