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

Last change on this file since 2515 was 2483, checked in by ansari, 22 years ago

Amelioration gestion variables ds interpreteur (classe Commander) - Introduction de trois niveaux : Var. interpreteur, env. appli, environnement global + amelioration gestion vecteur au niveau de l'interpreteur - Reza 22/12/2003

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