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

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

Ajout de cxxexecutor (Execution C++ en ligne ds piapp) - CMV+Reza 11/10/2000

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