]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
We can now use absolute/realtive path for scripts, as well as the '* notation'
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/01/29 10:12:45 $
7   Version:   $Revision: 1.5 $
8
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief class Interpreter : The bbtk language interpreter
21  */
22 /**
23  *  \class bbtk::Interpreter 
24  *  \brief The bbtk language interpreter
25  */
26
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
29
30 #include "bbtkVirtualExec.h"
31 #include "bbtkExecuter.h"
32 #include "bbtkTranscriptor.h"
33
34 #include <fstream>
35 #include <deque>
36
37 namespace bbtk
38 {
39   class BBTK_EXPORT Interpreter
40   {
41
42   private:
43
44     /// The enumeration of command codes == Command name
45     typedef enum
46     {
47       cNew,
48       cDelete,
49       cConnect,
50       cExec,
51       cPackage,
52       cEndPackage,
53       cDefine,
54       cEndDefine,
55       cInput,
56       cOutput,
57       cSet,
58       cConfig,  // JPR
59       cReset,   // EED
60       cAuthor,
61       cDescription,
62       cHelp,
63       cMessage,
64       cInclude,
65       cQuit,
66       cLoad,
67       cUnload,
68       cGraph,
69       cPrint,
70       cWorkspace // LG
71     } CommandCodeType;
72
73     /// The structure storing the informations on a command 
74     typedef struct 
75     {
76       std::string keyword;
77       int argmin, argmax;
78       CommandCodeType code;
79       std::string syntax;
80       std::string help;
81     } CommandInfoType;
82
83     /// The type of dictionnary of commands 
84     typedef std::map<std::string,CommandInfoType> CommandDictType;
85
86   public:
87     /// Constructor
88     Interpreter();
89
90     /// Destructor
91     ~Interpreter();
92
93    static Interpreter* mGlobalInterpreter;
94
95     /// Launches a command line interpreter (with a prompt)
96     void CommandLineInterpreter();
97
98     /// Sets the inputs of the workspace :
99     /// the map is passed as is to the Executer
100     void SetInputs(const std::map<std::string,std::string>& m)
101     { mExecuter->SetInputs(m); }
102
103     /// Puts the executer in "no exec" mode,
104     /// which creates but does not execute pipelines.
105     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
106
107     ///
108     //typedef Executer::DialogModeType DialogModeType;
109     typedef VirtualExec::DialogModeType DialogModeType;
110
111     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
112
113     /// Runs the interpretation of a file
114     void InterpretFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false);
115
116     /// Interprets a line (either from a file or typed interactively)
117     void InterpretLine( const std::string& line, bool &insideComment );
118
119     /// Reads a line from prompt
120     void GetLineFromPrompt( std::string& line );
121
122     /// Splits a line into words
123     void SplitLine ( const std::string& line,
124                      std::vector<std::string>& words );
125
126     /// Executes the right action depending on the command name
127     void InterpretCommand( const std::vector<std::string>& words,
128                            CommandInfoType& info );
129
130     /// Switch to the interpretation of a file
131     void SwitchToFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false );
132
133     /// Closes the currently open file
134     void CloseCurrentFile();
135
136     /// Closes all open files
137     void CloseAllFiles();
138
139     /// Displays help (entry point of any help)
140     void Help(const std::vector<std::string>& words);
141
142     /// Displays help on all the commands
143     void HelpCommands();
144
145     /// Displays help on a particular command 
146     void HelpCommand( const std::string& command );
147
148     ///
149     void Graph(const std::vector<std::string>& words);
150
151     ///
152     void Config(bool verbose) const; // JPR
153     /// 
154     void Print(const std::string&);
155
156     ///
157     void FindCommandsWithPrefix( char* buf,
158                                  int n,
159                                  std::vector<std::string>& commands );
160
161     /// Sets the bool that indicates wether we are in command line context
162     void SetCommandLine(bool v = true) { mCommandLine = v; }
163
164   private:
165
166   void LoadScript( std::string fullPathScriptName);
167
168   private:
169
170     //==================================================================
171     // ATTRIBUTES
172
173     /// The factory
174     //    bbtk::Factory* mFactory;
175
176     /// The command executer
177     bbtk::VirtualExec* mExecuter;
178
179     /// Vector of opened files 
180     std::vector<std::ifstream*> mFile;
181
182     /// Vector of names of open files
183     std::vector<std::string> mFileName;
184
185     /// Stores the current line number in each open file
186     std::vector<int> mLine;
187
188     /// The dictionnary of commands
189     CommandDictType mCommandDict;
190
191     /// Are we in a command line context ?
192     bool mCommandLine;
193
194     /// The history of commands
195     std::deque< char* > mHistory;
196
197     bool verbose; // true -> displays the search results (for packages)
198   };
199
200
201   // The "Quit" exception
202   struct QuitException : public bbtk::Exception
203   {
204     QuitException() : bbtk::Exception("","","") {}
205   };
206
207 }
208 #endif