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

Last change on this file since 2615 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

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