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

Last change on this file since 2751 was 2671, checked in by ansari, 20 years ago

Modifs pour introduire la possibilite d'execution multi-thread de commandes ds l'interpreteur Commander - Reza 18/4/2005

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