TEMU  4.4
The Terma Emulator
CommandLine.h
Go to the documentation of this file.
1 //===-- temu-c/CommandLine.h - TEMU Command Line ---------------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2015, 2019
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEMU_COMMAND_LINE
10 #define TEMU_COMMAND_LINE
11 
12 #include "temu-c/Support/Attributes.h"
13 #include "temu-c/Support/Objsys.h"
14 #include <stddef.h>
15 #include <stdint.h>
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /*!
21  * Parses command line options
22  * The TEMU command supports a number of built in command line options. A
23  * simulator embedding TEMU may want initialise these options in the same way
24  * that the TEMU CLI does. Note that not all options are supported this way as
25  * some options will only be registered by the temu CLI application itself.
26  *
27  * In general interactive options will not work (options that can be interpreted
28  * as a command). It is possible to use temu_printCommandLineHelp() to list
29  * currently registered options from an application that embedds temu.
30  *
31  * \param argc Number of arguments
32  * \param argv Command line arguments
33  * \result 0 on success, otherwise non-zero value.
34  */
35 TEMU_API int temu_parseCommandLineOptions(int argc, const char *argv[]);
36 
37 /*!
38  * Print command line help to temu stderr stream.
39  */
41 
42 /*!
43  * Executes the commands in the file "File"
44  * \param File Path to the file with the commands to be executed
45  * \result 0 on success, otherwise a non-zero value
46  */
47 TEMU_API int temu_execCommandFile(const char *File);
48 
49 /*!
50  * Executes a single command
51  * \param Cmd The command to be executed
52  * \result 0 on success, otherwise a non-zero value
53  */
54 TEMU_API int temu_execCommand(const char *Cmd);
55 
56 //! User command handlers implement this interface. The pointer
57 //! argument is a pointer to an opaque context. Use the functions below
58 //! to access properties from the context.
59 typedef int (*temu_CommandFunc)(void *);
60 
61 typedef enum temu_CmdOptionKind {
63  teCOK_Path, //!< Path is a string, but with auto completion of file names
64  teCOK_Object, //!< Object is a named object
65  teCOK_Int, //!< Any integer number
66  teCOK_Double, //!< Any floating point number
67  teCOK_Prop, //!< Property reference
68  teCOK_Iface, //!< Interface reference
69  teCOK_Reg, //!< Register reference
70  teCOK_Field, //!< Register field reference
71  teCOK_Class, //!< Class option
72 } temu_CmdOptionKind;
73 
74 typedef struct {
75  const char *Name;
76  temu_CmdOptionKind Type;
77  union {
78  const char *String;
79  const char *Path;
80  temu_Object *Obj;
81  int64_t Integer;
82  double Real;
83  temu_PropName PRef;
84  temu_IfaceRef IRef;
85  temu_PropName RegRef;
86  temu_PropName FieldRef;
87  temu_Class *Class;
88  };
89 } temu_CmdArg;
90 
91 typedef int (*temu_ObjectCommandFunc)(temu_Object *Obj, void *I, int argc,
92  const temu_CmdArg args[]);
93 
94 /*!
95  * Register a class command
96  *
97  * \param Cls Class pointer
98  * \param Name Name of command
99  * \param Doc Documentation string for command
100  * \param F Command handler function
101  * \result Opaque handle to command. NULL in case of failure.
102  */
103 TEMU_API void *temu_createClassCmd(temu_Class *Cls, const char *Name,
104  const char *Doc, temu_ObjectCommandFunc F);
105 /*!
106  * Get an already registered command handle from class
107  *
108  * This function can be used to for example get commands
109  * registered in the metaclass, such as the new command.
110  * The command can then have arguments added.
111  *
112  * \param Cls Class pointer
113  * \param Name Name of command to get
114  * \result Opaque handle to command. NULL in case of failure.
115  */
116 TEMU_API void *temu_classGetCmd(temu_Class *Cls, const char *Name);
117 
118 /*!
119  * Add parameter to command
120  *
121  * \param Cmd Command handle
122  * \param Name Name of parameter to add
123  * \param Type Type of the parameter
124  * \param Required Set to 1 if the parameter must be set for the command
125  * \param Doc Documentation string for the parameter
126  * \result 0 on success, other values indicates errors
127  */
128 TEMU_API int temu_classCmdAddParam(void *Cmd, const char *Name,
130  const char *Doc);
131 
132 /*!
133  * Get named command option
134  *
135  * \param argc Number of arguments
136  * \param args Argument vector
137  * \param OptName Name of parameter / option to get
138  * \result Pointer to command argument or NULL in case it is not found.
139  */
140 TEMU_API const temu_CmdArg* temu_classCmdGetOption(int argc, const temu_CmdArg args[], const char *OptName);
141 /*!
142  * Get named command option as signed integer
143  *
144  * Can only get valid numeric parameters.
145  * The function will never fail if the type is numeric and the option is required.
146  * For optional arguments, use the generic access function `temu_classCmdGetOption()`
147  *
148  * \param argc Number of arguments
149  * \param args Argument vector
150  * \param OptName Name of parameter / option to get
151  * \result The value of the parameter converted to an integer.
152  */
153 TEMU_API int64_t temu_classCmdGetOptionAsInteger(int argc, const temu_CmdArg args[], const char *OptName);
154 
155 /*!
156  * Get named command option as unsigned integer
157  *
158  * Can only get valid numeric parameters.
159  * The function will never fail if the type is numeric and the option is required.
160  * For optional arguments, use the generic access function `temu_classCmdGetOption()`
161  *
162  * \param argc Number of arguments
163  * \param args Argument vector
164  * \param OptName Name of parameter / option to get
165  * \result The value of the parameter converted to an unsigned integer.
166  */
167 TEMU_API uint64_t temu_classCmdGetOptionAsUnsigned(int argc, const temu_CmdArg args[], const char *OptName);
168 
169 /*!
170  * Invoke command on object
171  *
172  * \param Obj Object
173  * \param I Interpreter context
174  * \param Name Name of command
175  * \param Argc Number of arguments
176  * \param Argv Argument vector
177  * \result Command result. 0 means success.
178  */
179 TEMU_API int temu_objectInvokeCmd(temu_Object *Obj, void *I, const char *Name,
180  int Argc, temu_CmdArg Argv[]);
181 
182 /*
183  * Raise an error in the interpreter for the current command
184  *
185  * \param I Interpreter (I-argument to command func)
186  * \param S Error string
187  * \result Returns -1 so it can be used as "return temu_raiseCmdError(I, "foo")"
188  * in the command handler.
189  */
190 TEMU_API int temu_raiseCmdError(void *I, const char *S, ...);
191 
192 //! Create and register global command
193 //! \param Name Name of command
194 //! \param F Command function to invoke
195 //! \param Doc Documentation string
196 //! \param Data Data pointer. Some commands e.g. disassemble will
197 //! increase the address between invocations. This must be
198 //! saved in some data object which can be provided when
199 //! the command is created.
200 
201 TEMU_API void *temu_createCmd(const char *Name, temu_CommandFunc F,
202  const char *Doc, void *Data);
203 
204 //! Add named argument to command
205 //! \param Cmd Pointer to the command object
206 //! \param OptName Option name
207 //! \param Type Option type
208 //! \param Required Pass 0 if the option is not required, otherwise required
209 //! \param Doc Option documentation
210 //! \param Default Default value of the option
211 TEMU_API void temu_cmdAddOption(void *Cmd, const char *OptName,
213  const char *Doc, const char *Default);
214 
215 //! Get data pointer from command context
216 //! This function shall be called in a command handler on the passed
217 //! context.
218 //! \param Ctxt Pointer to the context of the command
219 //! \result data pointer
220 TEMU_API void *temu_cmdGetData(void *Ctxt);
221 
222 //! Get pointer to the command interpreter
223 //! This function shall be called in a command handler on the passed
224 //! context.
225 //! \param Ctxt Pointer to the context of the command
226 //! \result Pointer to interpreter
228 
229 //! Get named option as integer from command context
230 //! This function shall be called in a command handler on the passed
231 //! context.
232 //! \param Ctxt Command context
233 //! \param OptName Option name
234 //! \result The integer bound to the named argument
235 TEMU_API int64_t temu_cmdGetOptionAsInteger(void *Ctxt, const char *OptName);
236 
237 //! Get named option as object pointer from command context
238 //! This function shall be called in a command handler on the passed
239 //! context.
240 //! \param Ctxt Command context
241 //! \param OptName Option name
242 //! \result Pointer to the option object
243 TEMU_API void *temu_cmdGetOptionAsObject(void *Ctxt, const char *OptName);
244 
245 //! Get named option as string from command context
246 //! This function shall be called in a command handler on the passed
247 //! context.
248 //! \param Ctxt Command context
249 //! \param OptName Option name
250 //! \result C-string of the name of the option
251 TEMU_API const char *temu_cmdGetOptionAsString(void *Ctxt, const char *OptName);
252 
253 //! Get named option as double from command context
254 //! This function shall be called in a command handler on the passed
255 //! context.
256 //! \param Ctxt Command context
257 //! \param OptName Option name
258 //! \result The real value of the option as double
259 TEMU_API double temu_cmdGetOptionAsReal(void *Ctxt, const char *OptName);
260 
261 //! Get number of positional options given
262 //! This function shall be called in a command handler on the passed
263 //! context.
264 //! \param Ctxt Command context
265 //! \result The number of position optionals in Ctxt
266 TEMU_API size_t temu_cmdGetPosOptSize(void *Ctxt);
267 
268 //! Get positional option at index
269 //! This function shall be called in a command handler on the passed
270 //! context.
271 //! \param Ctxt Command context
272 //! \param Idx Index of the option
273 //! \result The option at position Idx as a C-string
274 TEMU_API const char *temu_cmdGetPosOpt(void *Ctxt, size_t Idx);
275 
276 //! Return 1 if the option is valid, 0 if invalid / not set
277 //! This function shall be called in a command handler on the passed
278 //! context.
279 //! \param Ctxt Command context
280 //! \param OptName Option name
281 //! \result 1 if the option is valid, 0 if invalid / not set
282 TEMU_API int temu_cmdOptionIsValid(void *Ctxt, const char *OptName);
283 
284 //! Set a variable in the command line
285 //! \param Key Variable name (must match [A-Za-z_][A-Za-z0-9_]*)
286 //! \param Value Value to assign to variable
287 //! \result Non-zero on errors
288 TEMU_API int temu_cmdSetVariable(const char *Key, const char *Value);
289 
290 //! Get a variable in the command line
291 //! \param Key Variable name
292 //! \result In case the variable is not found NULL, otherwise
293 //! a borrowed string.
294 TEMU_API const char *temu_cmdGetVariable(const char *Key);
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif /* !TEMU_COMMAND_LINE */
temu_CmdArg::Type
temu_CmdOptionKind Type
Definition: CommandLine.h:76
temu_objectInvokeCmd
TEMU_API int temu_objectInvokeCmd(temu_Object *Obj, void *I, const char *Name, int Argc, temu_CmdArg Argv[])
teCOK_Object
@ teCOK_Object
Object is a named object.
Definition: CommandLine.h:64
temu_printCommandLineHelp
TEMU_API void temu_printCommandLineHelp(void)
temu_CmdArg::Name
const char * Name
Definition: CommandLine.h:75
temu_cmdGetOptionAsReal
TEMU_API double temu_cmdGetOptionAsReal(void *Ctxt, const char *OptName)
temu_cmdSetVariable
TEMU_API int temu_cmdSetVariable(const char *Key, const char *Value)
teCOK_Double
@ teCOK_Double
Any floating point number.
Definition: CommandLine.h:66
temu_CommandFunc
int(* temu_CommandFunc)(void *)
Definition: CommandLine.h:59
temu_raiseCmdError
TEMU_API int temu_raiseCmdError(void *I, const char *S,...)
temu_ObjectCommandFunc
int(* temu_ObjectCommandFunc)(temu_Object *Obj, void *I, int argc, const temu_CmdArg args[])
Definition: CommandLine.h:91
temu_cmdGetData
TEMU_API void * temu_cmdGetData(void *Ctxt)
temu_classGetCmd
TEMU_API void * temu_classGetCmd(temu_Class *Cls, const char *Name)
teCOK_Reg
@ teCOK_Reg
Register reference.
Definition: CommandLine.h:69
temu_parseCommandLineOptions
TEMU_API int temu_parseCommandLineOptions(int argc, const char *argv[])
teCOK_Int
@ teCOK_Int
Any integer number.
Definition: CommandLine.h:65
teCOK_Field
@ teCOK_Field
Register field reference.
Definition: CommandLine.h:70
temu_createClassCmd
TEMU_API void * temu_createClassCmd(temu_Class *Cls, const char *Name, const char *Doc, temu_ObjectCommandFunc F)
temu_execCommand
TEMU_API int temu_execCommand(const char *Cmd)
temu_createCmd
TEMU_API void * temu_createCmd(const char *Name, temu_CommandFunc F, const char *Doc, void *Data)
temu_CmdOptionKind
temu_CmdOptionKind
Definition: CommandLine.h:61
teCOK_String
@ teCOK_String
Definition: CommandLine.h:62
temu_classCmdAddParam
TEMU_API int temu_classCmdAddParam(void *Cmd, const char *Name, temu_CmdOptionKind Type, int Required, const char *Doc)
teCOK_Path
@ teCOK_Path
Path is a string, but with auto completion of file names.
Definition: CommandLine.h:63
temu_cmdGetVariable
const TEMU_API char * temu_cmdGetVariable(const char *Key)
teCOK_Class
@ teCOK_Class
Class option.
Definition: CommandLine.h:71
teCOK_Iface
@ teCOK_Iface
Interface reference.
Definition: CommandLine.h:68
temu_cmdGetPosOpt
const TEMU_API char * temu_cmdGetPosOpt(void *Ctxt, size_t Idx)
temu_cmdGetOptionAsString
const TEMU_API char * temu_cmdGetOptionAsString(void *Ctxt, const char *OptName)
temu_cmdOptionIsValid
TEMU_API int temu_cmdOptionIsValid(void *Ctxt, const char *OptName)
temu_execCommandFile
TEMU_API int temu_execCommandFile(const char *File)
temu_cmdGetInterpreter
TEMU_API void * temu_cmdGetInterpreter(void *Ctxt)
temu_cmdAddOption
TEMU_API void temu_cmdAddOption(void *Cmd, const char *OptName, temu_CmdOptionKind Type, int Required, const char *Doc, const char *Default)
teCOK_Prop
@ teCOK_Prop
Property reference.
Definition: CommandLine.h:67
temu_cmdGetOptionAsObject
TEMU_API void * temu_cmdGetOptionAsObject(void *Ctxt, const char *OptName)