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

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

fenetre pour CxxExecutor - Reza 24/10/2000

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