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
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:
[1262]132 enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
133
134 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
135 ~PIACmdBloc();
[349]136 inline PIACmdBloc* Parent() { return(parent); }
[1262]137 inline bool CheckOK() { return blkok; }
[349]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;
[1262]146 bool blkok; // true -> block OK
147 BType typ; // foreach , integer loop, float loop, test
[349]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;
[1262]162blkok = false;
163typ = BT_None;
[349]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];
[1262]168if (isalpha((int)args[1][0]) ) { // This is a foreach bloc with string list
[349]169 for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
[1262]170 typ = BT_ForeachList;
[349]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) {
[1262]190 typ = BT_ForeachFloat;
[349]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 {
[1262]197 typ = BT_ForeachInt;
[349]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];
[1262]222if (typ == BT_ForeachList) // foreach string loop
[349]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 }
[1262]232else if (typ == BT_ForeachInt) // Integer loop
[349]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 }
[1262]248else if (typ == BT_ForeachFloat) // float loop
[349]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
[293]272static PIACmd* curpiacmd = NULL;
273/* --Methode-- */
[165]274PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
275{
[293]276mObjMgr = omg;
[165]277mImgApp = app;
278system("cp history.pic hisold.pic");
279hist.open("history.pic");
[349]280histon = true;
[165]281trace = false; timing = false;
282gltimer = NULL;
[349]283curblk = NULL;
284felevel = 0;
[1262]285bloccxx = false;
286bloctest = false;
[293]287
[330]288cmdhgrp["All"] = 0;
289cmdgrpid = 1;
290cmdhgrp["Commands"] = 1;
[293]291helpwin = new PIAHelpWind(app, this);
[330]292helpwin->AddHelpGroup("All", 0);
293helpwin->AddHelpGroup("Commands", 1);
[293]294
295string kw = "piacmd";
296string usage;
297usage = ">>> (piacmd) Interpreter's keywords : \n";
[349]298usage += " > set varname string # To set a variable, $varname \n";
299usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
[333]300usage += " > setol varname patt # Fills varname with object list \n";
[293]301usage += " > unset varname # clear variable definition \n";
[333]302usage += " > echo string # output string \n";
[353]303usage += " > alias name string # define a command alias \n";
304usage += " > readstdin varname # reads a line from stdin into $varname \n";
[349]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";
[353]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";
[293]313usage += " > shell comand_string # Execute shell command \n";
314usage += " > help <command_name> # <command_name> usage info \n";
315usage += " > helpwindow # Displays help window \n";
[349]316usage += " > timingon timingoff traceon traceoff \n";
[330]317string grp = "Commands";
318RegisterHelp(kw, usage, grp);
[293]319
320basexec = new PIABaseExecutor(this, omg, app);
[361]321fitexec = new PIAFitter(this, app);
[463]322pawexec = new PAWExecutor(this, app);
[1251]323CxxExecutor * cxxe = new CxxExecutor(this, app);
324cxxexec = cxxe;
325cxxoptwin = new CxxOptionWind(app, cxxe);
326cxxexwin = new CxxExecWind(app, cxxe);
327
[293]328AddInterpreter(this);
329curcmdi = this;
[165]330}
331
332/* --Methode-- */
333PIACmd::~PIACmd()
334{
335hist.close();
336if (gltimer) { delete gltimer; gltimer = NULL; }
[293]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;
[1251]345delete cxxexwin;
346delete cxxoptwin;
[293]347if (curpiacmd == this) curpiacmd = NULL;
[361]348delete basexec;
349delete fitexec;
[463]350delete pawexec;
[1224]351delete cxxexec;
[165]352}
353
354/* --Methode-- */
[293]355PIACmd* PIACmd::GetInterpreter()
[165]356{
[293]357return(curpiacmd);
358}
[165]359
[293]360/* --Methode-- */
361string PIACmd::Name()
362{
363return("piacmd");
364}
[165]365
[293]366/* --Methode-- */
[330]367void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
[293]368{
[330]369if (!ce) {
370 RegisterHelp(keyw, usage, grp);
371 return;
372 }
373int gid = CheckHelpGrp(grp);
[293]374cmdex cme;
[330]375cme.group = gid;
[293]376cme.us = usage;
377cme.cex = ce;
378cmdexmap[keyw] = cme;
379}
380
381/* --Methode-- */
[330]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-- */
[293]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;
[165]428 }
[293]429string fname = name + "_init";
430DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
431if (!finit) {
432 cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
433 return;
[165]434 }
[293]435cout << "PIACmd/LoadModule_Info: Initialisation module" << name
436 << " " << fname << "() ..." << endl;
437finit();
438modmap[name] = dynlink;
439return;
440}
[165]441
[293]442/* --Methode-- */
443void PIACmd::AddInterpreter(CmdInterpreter * cl)
444{
445if (!cl) return;
[349]446interpmap[cl->Name()] = cl;}
[165]447
448/* --Methode-- */
[293]449void PIACmd::SelInterpreter(string& name)
[165]450{
[293]451InterpMap::iterator it = interpmap.find(name);
452if (it == interpmap.end()) return;
453curcmdi = (*it).second;
454}
[165]455
[293]456
457
[357]458/* Fonction */
[368]459static string GetStringFrStdin(PIACmd* piac)
[357]460{
[368]461PIStdImgApp* piapp = piac->GetImgApp();
462if (piapp) {
463 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
464 if (wdg) wdg->Refresh();
[357]465#ifndef __mac__
[368]466 /* On vide le buffer X-Window */
[374]467 XSync(PIXDisplay(),False);
[357]468#endif
[368]469}
[357]470char buff[128];
471fgets(buff, 128, stdin);
472buff[127] = '\0';
473return((string)buff);
474}
475
[293]476/* --Methode-- */
477int PIACmd::Interpret(string& s)
478{
[333]479int rc = 0;
[1262]480NamedObjMgr omg;
[165]481
[1262]482// On saute de commandes vides
483size_t l;
[349]484l = s.length();
485if (l < 1) return(0);
[165]486
[349]487// On enregistre les commandes
488if (histon) hist << s << endl;
489
[1262]490if (s[0] == '#') return(0); // si c'est un commentaire
[349]491
[165]492
[1262]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
[349]501// >>>> Substitution d'alias (1er mot)
[1262]502CmdVarList::iterator it;
[449]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);
[349]513 p = 0;
514 q = s.find_first_of(" ");
515 }
516
517// >>>> Separating keyword
[1262]518string toks,kw;
[165]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
[349]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];
[353]537 sprintf(prompt, "foreach-%d? ", felevel);
[349]538 mImgApp->GetConsole()->SetPrompt(prompt);
539 }
540 return(0);
541 }
542 }
543
544
[1262]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="";
[349]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);
[1262]658 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
[349]659 cerr << " Error: Undefined variable " << vn << " ! " << endl;
660 return(0);
661 }
[1262]662 vn = omg.GetVar(vn);
[349]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 }
[1262]670 if ( (vn.length() < 1) || (!omg.HasVar(vn)) ) {
[349]671 cerr << " Error: Undefined variable " << vn << " ! " << endl;
672 return(0);
673 }
[1262]674 s2 += (s.substr(p, q-p) + omg.GetVar(vn));
[349]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
[1262]682return(0);
683}
[349]684
[1262]685/* --Methode-- */
686int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
687{
688int rc = 0;
689NamedObjMgr omg;
[349]690
[165]691// >>>>>>>>>>> Commande d'interpreteur
[1262]692if (kw == "helpwindow") ShowHelpWindow();
[293]693else if (kw == "help") {
[330]694 if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
[293]695 else {
[330]696 string kwh = "piacmd";
697 cout << GetUsage(kwh) << endl;
[293]698 }
[165]699 }
700
701else if (kw == "set") {
[293]702 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
[349]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] );
[1262]709 omg.SetVar(tokens[0], xx);
[165]710 }
[349]711
712else if (kw == "getvar") {
713 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
[1262]714 if (!omg.HasVar(tokens[1])) {
[349]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 }
[1262]722 omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
[349]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
[333]736else if (kw == "setol") {
737 if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
[349]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 }
[333]742 vector<string> ol;
[349]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 }
[1262]750 omg.SetVar(tokens[0], vol);
[333]751 }
[293]752else if (kw == "unset") {
753 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
[1262]754 if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
[293]755 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
[165]756 }
[333]757else if (kw == "echo") {
758 for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
759 cout << endl;
760 }
[353]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 }
[357]768 mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
769 cout << tokens[0] << " ? " << endl;
[1262]770 omg.SetVar(tokens[0], GetStringFrStdin(this) );
[353]771 }
772
[165]773else if (kw == "listvars") {
[293]774 cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
[1262]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 }
[165]782 cout << endl;
783 }
[353]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") {
[330]792 cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
[293]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; }
[165]804else if (kw == "timingon") {
[293]805 cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
806 if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
807 }
[165]808else if (kw == "timingoff") {
[293]809 cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
810 if (gltimer) delete gltimer; gltimer = NULL; timing = false;
811 }
[165]812else if (kw == "exec") {
[293]813 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
[349]814 ExecFile(tokens[0], tokens);
[165]815 }
816else if (kw == "shell") {
[293]817 if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
[165]818 system(toks.c_str());
819 }
[293]820// Execution d'une commande enregistree
[1265]821// CMV c'est ici qu'il faut passer la string
[333]822else rc = ExecuteCommand(kw, tokens);
[165]823
824if (timing) gltimer->Split();
[333]825return(rc);
[165]826}
827
[333]828/* --Methode-- */
[1262]829int PIACmd::ParseLineExecute(string& line)
[333]830{
[1262]831vector<string> tokens;
832
[333]833if (line.length() < 1) return(0);
[165]834
[333]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
[165]858/* --Methode-- */
[333]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-- */
[349]872int PIACmd::ExecFile(string& file, vector<string>& args)
[165]873{
[293]874char line_buff[512];
875FILE *fip;
[165]876
[293]877if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
[384]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) {
[293]887 cerr << "PIACmd::Exec() Error opening file " << file << endl;
888 hist << "##! PIACmd::Exec() Error opening file " << file << endl;
889 return(0);
[165]890 }
[349]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
[1262]899NamedObjMgr omg;
[349]900string vn="#";
[1262]901omg.DeleteVar(vn);
[349]902for(k=0; k<99; k++) {
903 sprintf(buff,"%d",k);
904 vn = buff;
[1262]905 omg.DeleteVar(vn);
[349]906 }
907// We then set them
[1262]908string vval;
[349]909vn="#";
910sprintf(buff,"%d",(int)args.size());
[1262]911omg.SetVar(vn, buff);
[349]912for(k=0; k<args.size(); k++) {
913 sprintf(buff,"%d",k);
914 vn = buff;
[1262]915 omg.SetVar(vn, args[k]);
[349]916 }
917
[293]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 }
[165]923
[349]924histon = false;
[293]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);
[165]931 }
[357]932histon = true;
[349]933
934// hist << "### End of Exec( " << file << " ) " << endl;
[293]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);
[165]939 }
940
941return(0);
942}
943
[293]944
945static string* videstr = NULL;
946/* --Methode-- */
947string& PIACmd::GetUsage(const string& kw)
[165]948{
[330]949bool fndok = false;
[293]950CmdExmap::iterator it = cmdexmap.find(kw);
[330]951if (it == cmdexmap.end()) {
952 it = helpexmap.find(kw);
953 if (it != helpexmap.end()) fndok = true;
[165]954 }
[330]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
[165]962}
963
[293]964/* --Methode-- */
965void PIACmd::ShowHelpWindow()
[165]966{
[293]967helpwin->Show();
[165]968}
[357]969
[463]970/* --Methode-- */
[1251]971void PIACmd::ShowCxxOptionWindow()
972{
973cxxoptwin->Show();
974}
975
976/* --Methode-- */
977void PIACmd::ShowCxxExecWindow()
978{
979cxxexwin->Show();
980}
981
982/* --Methode-- */
[463]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 }
[357]990
[463]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++)
[484]996 fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
997 (*it).first.c_str(), (*it).first.c_str());
[463]998
999fputs("\\end{itemize} \n",fip);
1000
[484]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
[463]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;
[484]1045 fprintf(fip,"\\subsection{%s} \\label{%s} \n",
1046 (*it).first.c_str(), (*it).first.c_str());
[463]1047 for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
1048 if ((*ite).second.group != gid) continue;
[484]1049 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
1050 (*ite).first.c_str(), (*ite).first.c_str());
[463]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;
[484]1057 fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
1058 (*ite).first.c_str(), (*ite).first.c_str());
[463]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.