source: Sophya/trunk/SophyaLib/SysTools/commander.h@ 4084

Last change on this file since 4084 was 4063, checked in by ansari, 13 years ago

Introduction de la classe interface CE_VarListInterface et modifs classe CExpressionEvaluator pour permettre l'inclusion de variables nommees dans les expressions traitees par CExpressionEvaluator, adaptation de la classe Commander afin que les variables de l'interpreteur soit visible par l'evaluateur CExpressionEvaluator, Reza 27/04/2012

File size: 9.9 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// Classe interpreteur de commande pour piapp
3// Reza Aout 97 , Juillet,Aout 98
4// Octobre 2003: de PIext -> SysTools
5// LAL-IN2P3/CNRS DAPNIA/CEA
6
7#ifndef COMMANDER_H_SEEN
8#define COMMANDER_H_SEEN
9
10#include "machdefs.h"
11#include <iostream>
12#include <fstream>
13#include <string>
14#include <vector>
15#include <list>
16#include <stack>
17#include <map>
18#include <functional>
19
20#include "pdlmgr.h"
21#include "dvlist.h"
22#include "ctimer.h"
23
24namespace SOPHYA {
25
26////// Classe definissant l'interface pour un interpreteur de commande
27
28/*!
29 \ingroup SysTools
30 \brief Interface definition for a generic command interpreter.
31*/
32class CmdInterpreter {
33public:
34 virtual ~CmdInterpreter() {} ;
35//! Returns the interpreter's name
36 virtual string Name()=0;
37//! Method to be called in order to interpret a line or string.
38 virtual int Interpret(string& line)=0;
39};
40
41
42/////// Classe definissant l'interface pour un executeur de commande
43
44/*!
45 \ingroup SysTools
46 \brief Interface definition for command executor, to be used with Commander
47
48 A command is defined by a keyword and a number of argument
49*/
50
51class CmdExecutor {
52public:
53 virtual ~CmdExecutor() {} ;
54 // keyw : Le mot cle associe , args: Arguments de la commande
55 //! command execution method for a command defined by keyword and its arguments.
56 virtual int Execute(string& keyw, vector<string>& args, string& toks)=0;
57 //! Return true if the command \b keyw is thread compatible (can be executed in a separate thread)
58 virtual bool IsThreadable(string const & keyw) { return false; }
59};
60
61
62
63class CommanderBloc; // Bloc de type foreach / for de l'interpreteur Commander
64class CommanderScript; // Script de commandes defini ds l'interpreteur Commander
65class CommandExeThr; // Thread d'execution de commande
66
67//! A simple command interpreter with c-shell like syntax and dynamic load capability.
68
69class Commander : public CmdInterpreter {
70public:
71 static Commander* GetInterpreter();
72
73 Commander(bool fgsigzt=true);
74 virtual ~Commander();
75 virtual string Name();
76
77 virtual void AddHelpGroup(string& grp, string& desc);
78 virtual void RegisterCommand(string& keyw, string& usage, CmdExecutor * ce,
79 string& grp);
80 inline void RegisterCommand(string& keyw, string& usage, CmdExecutor * ce,
81 const char* grp)
82 { string sgrp = grp; RegisterCommand(keyw, usage, ce, sgrp); }
83
84 virtual void RegisterHelp(string& keyw, string& usage, string& grp);
85
86 virtual void LoadModule(string& fnameso, string& name);
87
88 virtual void AddInterpreter(CmdInterpreter * cl);
89 virtual void SelInterpreter(string& name);
90
91 virtual int Interpret(string& line);
92 virtual void StopExecution();
93
94 virtual int ExecuteCommand(string& keyw, vector<string>& args, string& toks);
95 virtual int ExecFile(string& file, vector<string>& args);
96 virtual int CShellExecute(string cmd);
97 virtual string& GetUsage(const string& kw);
98
99 inline void SetMaxLoopLimit(int_8 lim=0) { maxlooplimit_ = lim; }
100 inline int_8 GetMaxLoopLimit() { return maxlooplimit_; }
101
102 string GetCurrentPrompt() { return curprompt; }
103
104
105 virtual void HelptoLaTeX(string const & flnm);
106
107 //! return the current selected interpreter (default : this)
108 inline CmdInterpreter* CurrentInterpreter() { return(curcmdi); }
109
110 // ----- Action / gestion des variables propres de l'interpreteur
111 // Verifie l'existence de la variable nomme vn et retourne sa valeur ds vv
112 // Retourne false si la variable n'existe pas
113 virtual bool GetVar(string const & vn, string & vv);
114 virtual bool GetVar(string const & vn, int idx, string & vv);
115 virtual bool GetVar(string const & vn, vector<string> & vv);
116 virtual bool SetVar(string const & vn, string const & vv);
117 virtual bool SetVar(string const & vn, int idx, string const & vv);
118 virtual bool SetVar(string const & vn, vector<string> const & vv);
119 virtual bool CheckVarName(string const & vn);
120 virtual bool DeleteVar(string const & vn);
121 virtual void ListVar();
122 // Variables de l'environnement application
123 virtual bool GetVarApp(string const & vn, string & vv);
124 virtual bool SetVarApp(string const & vn, string const & vv);
125 virtual bool DeleteVarApp(string const & vn);
126 virtual void ListVarApp();
127 // Variables d'environnement globales
128 virtual bool GetVarEnv(string const & vn, string & vv);
129 virtual bool SetVarEnv(string const & vn, string const & vv);
130 virtual bool DeleteVarEnv(string const & vn);
131 virtual void ListVarEnv();
132
133 // Utilitaire pour decoupage en mot
134 static int LineToWords(string& line, string& kw, vector<string>& tokens,
135 vector<bool>& qottoks, string& toks, bool uq=true);
136protected:
137 virtual int ParseLineExecute(string& line, bool qw=true);
138
139 virtual int ExecuteCommandLine(string & keyw, vector<string> & args,
140 string & toks);
141
142 virtual bool CheckHelpGrp(string& grp, int& gid, string& desc);
143 inline bool CheckHelpGrp(string& grp, int& gid)
144 { string desc=""; return CheckHelpGrp(grp, gid, desc); }
145
146 virtual int SubstituteVars(string & s, string & s2);
147 int EvaluateTest(vector<string> & args,
148 string & line, bool & res);
149
150 // variable de l'interpreteur = valeur - accepte la syntaxe de type varname[index]
151 virtual bool SetVariable(string const & vn, string const & vv);
152 // Acces aux variables
153 virtual bool Var2Str(string const & vn, string & vv);
154 inline bool Var2Str(string const & vn, int idx, string & vv)
155 { return GetVar(vn, idx, vv); }
156 inline bool Var2Str(string const & vn, vector<string> & vv)
157 { return GetVar(vn, vv); }
158
159 virtual string GetTmpDir();
160
161 virtual void SetCurrentPrompt(const char* pr);
162 inline void SetCurrentPrompt(string const & pr) { SetCurrentPrompt(pr.c_str()); }
163 inline void SetDefaultPrompt(string const & pr) { defprompt = pr; }
164
165 virtual void ShowMessage(const char * msg, int att);
166
167 void PushStack(vector<string> * pargs);
168 inline void PushStack() { PushStack(NULL); return; }
169 inline void PushStack(vector<string> & args) { PushStack(&args); return; }
170 void PopStack(bool psta=true);
171
172 // Gestion des threads d'execution de commandes
173 void ListThreads();
174 void StopThr(uint_8 thrid, bool fgkill=true);
175 void CleanThrList();
176 void WaitThreads();
177
178// ------ Attributs et variables ------
179 CmdInterpreter* curcmdi;
180
181// Gestion des variables
182 typedef map< string, vector<string>, less<string> > CmdVarList;
183 CmdVarList variables;
184
185// Pour enregistrer la liste de commandes et leurs executeurs et le help
186/*! \cond Pour supprimer la documentation par doxygen */
187// Command executor registration - For Commander internal use
188 struct cmdex {int group; string us; CmdExecutor * cex; } ;
189// Help text registration - For Commander internal use
190 struct hgrpst {int gid; string desc; } ; // Identification+description d'un groupe de help
191 /*! \endcond */
192
193 typedef map<string, hgrpst, less<string> > CmdHGroup; // Liste des groupes de commandes
194 CmdHGroup cmdhgrp;
195 int cmdgrpid; // Numero de groupe courant
196 typedef map<string, cmdex, less<string> > CmdExmap;
197 CmdExmap cmdexmap; // Liste des commandes et leurs executeurs
198 CmdExmap helpexmap; // Pour les helps sans commande
199// Pour garder la liste des threads d'execution de commande
200 list<CommandExeThr *> CmdThrExeList;
201 uint_8 ThrId;
202
203// Pour garder la liste des modules
204 typedef map<string, PDynLinkMgr* , less<string> > Modmap;
205 Modmap modmap;
206
207// Pour garder la liste des interpreteur
208 typedef map<string, CmdInterpreter*, less<string> > InterpMap;
209 InterpMap interpmap;
210
211// Pour stocker les scripts definis ds l'interpreteur
212 typedef map<string, CommanderScript*, less<string> > ScriptList;
213 ScriptList mScripts; // Liste des scripts
214 CommanderScript* curscript; // Script en cours de definition
215
216 // Code de retour execution commande
217 int _xstatus;
218 // Valeur de retour (par l'instruction return) -
219 string _retstr;
220
221 // Pour stocker les alias definies par l'interpreteur
222 typedef map<string, string, less<string> > CmdStrList;
223 CmdStrList mAliases; // Liste des alias
224
225 // Le stack pour les arguments des .pic et des scripts
226 stack< vector<string> > ArgsStack;
227 // Stack pour les Prompts
228 stack<string> PromptStack;
229
230 // Gestion des blocs de commandes et tests (if)
231 stack< CommanderBloc * > CmdBlks; // Bloc de commande courant (foreach, ...)
232 int felevel_; // foreach-for level
233 int_8 maxlooplimit_; // Limite maximum des boucles
234 /*! \cond Pour supprimer la documentation par doxygen */
235 typedef struct {list<char> tstlist; list<char>::iterator tstresit; bool tstcurres; } TstStatus;
236 /*! \endcond */
237 stack< TstStatus > TestsStack; // Stack des resultats de test
238 list<char>::iterator tresit_; // Test courant
239 bool curtestresult_; // Resultat courant des tests
240
241 // Controle du flot d'execution
242 bool fgexebrk;
243
244 // Commande splitees sur plusieurs lignes
245 bool mulinefg; // Bloc multi-lignes (ligne suite)
246 string mulinecmd; // Commande multi-lignes
247
248 // Texte de prompt (attente de commande)
249 string spromptmul; // Prompt console avant multi-ligne
250 string curprompt; // Prompt courant
251 string defprompt; // Prompt par defaut
252
253 // Gestion d'historique, trace, timing des commandes
254 ofstream hist; // History file
255 bool histon; // True -> history file
256 bool trace; // Trace flag
257 bool timing; // Display CPU Time
258 Timer* gltimer; // pour Display CPU Time
259 bool varcexp; // true -> decodage nom de variables lors d'evaluation d'expression
260friend class CommanderBloc;
261friend class CommanderScript;
262
263};
264
265} // namespace SOPHYA
266
267/* end of ifdef COMMANDER_H_SEEN */
268#endif
269
Note: See TracBrowser for help on using the repository browser.