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

Last change on this file since 1265 was 1265, checked in by ercodmgr, 25 years ago

GetTmpDir SetTmpDir ds nobjmgr.h
re-ecriture commande sur piconsole en bleu/bleu_gras
suppression OBJ ds CrMakeFile

rz+cmv 31/10/00

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