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

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

Amelioration classe SOPHYA::Commander, en particulier Ajout description d'un HelpGroup - Reza 27 Nov 2003

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