PageRenderTime 1330ms CodeModel.GetById 34ms RepoModel.GetById 4ms app.codeStats 0ms

/src/shell/shell.h

https://bitbucket.org/toponado/nfs-proxy
C Header | 338 lines | 104 code | 54 blank | 180 comment | 0 complexity | 3085d972c83174c66203cfaf432d630f MD5 | raw file
  1. /**
  2. *
  3. * \file shell.h
  4. * \author $Author: leibovic $
  5. * \date $Date: 2006/02/23 07:42:53 $
  6. * \version $Revision: 1.7 $
  7. * \brief Internal routines for the shell.
  8. *
  9. *
  10. * $Log: shell.h,v $
  11. * Revision 1.7 2006/02/23 07:42:53 leibovic
  12. * Adding -n option to shell.
  13. *
  14. * Revision 1.6 2005/07/26 12:54:47 leibovic
  15. * Multi-thread shell with synchronisation routines.
  16. *
  17. * Revision 1.5 2005/07/25 12:50:46 leibovic
  18. * Adding thr_create and thr_join commands.
  19. *
  20. * Revision 1.4 2005/05/27 12:01:48 leibovic
  21. * Adding write command.
  22. *
  23. * Revision 1.3 2005/05/11 15:53:37 leibovic
  24. * Adding time function.
  25. *
  26. * Revision 1.2 2005/05/09 14:54:59 leibovic
  27. * Adding if.
  28. *
  29. * Revision 1.1 2005/05/09 12:23:55 leibovic
  30. * Version 2 of ganeshell.
  31. *
  32. *
  33. */
  34. #ifndef _SHELL_H
  35. #define _SHELL_H
  36. #include "shell_types.h"
  37. #define MAX_LINE_LEN 1024
  38. #define MAX_ARGS 256
  39. /*------------------------------------------------------------------
  40. * Internal error codes.
  41. *-----------------------------------------------------------------*/
  42. #define SHELL_SUCCESS 0
  43. #define SHELL_ERROR -1
  44. #define SHELL_NOT_FOUND -2
  45. #define SHELL_SYNTAX_ERROR -22
  46. /*------------------------------------------------------------------
  47. * Main shell routines.
  48. *-----------------------------------------------------------------*/
  49. /**
  50. * Initialize the shell.
  51. * The command line for the shell is given as parameter.
  52. * \param input_file the file to read from (NULL if stdin).
  53. */
  54. int shell_Init(int verbose, char *input_file, char *prompt, int shell_index);
  55. /**
  56. * Launch the interpreter.
  57. */
  58. int shell_Launch();
  59. /**
  60. * Initialize the barrier for shell synchronization routines.
  61. * The number of threads to wait for is given as parameter.
  62. */
  63. int shell_BarrierInit(int nb_threads);
  64. /*------------------------------------------------------------------
  65. * Parsing and execution routines.
  66. *-----------------------------------------------------------------*/
  67. /**
  68. * shell_ParseLine:
  69. * Extract an arglist from a command line.
  70. *
  71. * \param in_out_line The command line (modified).
  72. * \param out_arglist The list of command line tokens.
  73. * \param p_argcount The number of command line tokens.
  74. *
  75. * \return 0 if no errors.
  76. */
  77. int shell_ParseLine(char *in_out_line, char **out_arglist, int *p_argcount);
  78. /**
  79. * shell_CleanArgs:
  80. * Free allocated arguments.
  81. *
  82. * \param argc The number of command line tokens.
  83. * \param in_out_argv The list of command line tokens (modified).
  84. * \param in_allocated Indicates which tokens must be freed.
  85. *
  86. * \return 0 if no errors.
  87. */
  88. void shell_CleanArgs(int argc, char **in_out_argv, int *in_allocated);
  89. /**
  90. * shell_SolveArgs:
  91. * Interprets arguments if they are vars or commands.
  92. *
  93. * \param argc The number of command line tokens.
  94. * \param in_out_argv The list of command line tokens (modified).
  95. * \param out_allocated Indicates which tokens must be freed.
  96. *
  97. * \return 0 if no errors.
  98. */
  99. int shell_SolveArgs(int argc, char **in_out_argv, int *out_allocated);
  100. /**
  101. * shell_Execute:
  102. * Execute a command.
  103. *
  104. * \param argc The number of arguments of this command.
  105. * \param argv The arguments for this command.
  106. * \param output The output stream of this command.
  107. *
  108. * \return The returned status of this command.
  109. */
  110. int shell_Execute(int argc, char **argv, FILE * output);
  111. /*------------------------------------------------------------------
  112. * Shell ouput routines.
  113. *-----------------------------------------------------------------*/
  114. /**
  115. * shell_PrintError:
  116. * Prints an error.
  117. */
  118. void shell_PrintError(shell_state_t * context, char *error_msg);
  119. /**
  120. * shell_PrintTrace:
  121. * Prints a verbose trace.
  122. */
  123. void shell_PrintTrace(shell_state_t * context, char *msg);
  124. /*------------------------------------------------------------------
  125. * Shell state management routines.
  126. *-----------------------------------------------------------------*/
  127. /** List of the shell special variables */
  128. extern char *shell_special_vars[];
  129. /**
  130. * shell_SetLayer:
  131. * Set the current active layer.
  132. * \return 0 if OK.
  133. * else, an error code.
  134. */
  135. int shell_SetLayer(shell_state_t * context, char *layer_name);
  136. /**
  137. * shell_GetLayer:
  138. * Retrieves the current active layer (internal use).
  139. */
  140. layer_def_t *shell_GetLayer(shell_state_t * context);
  141. /**
  142. * shell_SetStatus
  143. * Set the special variables $? and $STATUS.
  144. */
  145. int shell_SetStatus(shell_state_t * context, int returned_status);
  146. /**
  147. * shell_GetStatus
  148. * Get the special variables $? or $STATUS (internal use).
  149. */
  150. int shell_GetStatus(shell_state_t * context);
  151. /**
  152. * shell_SetVerbose
  153. * Set the special variable $VERBOSE.
  154. */
  155. int shell_SetVerbose(shell_state_t * context, char *str_verbose);
  156. /**
  157. * shell_GetVerbose
  158. * Get the special variable $VERBOSE (internal use).
  159. */
  160. int shell_GetVerbose(shell_state_t * context);
  161. /**
  162. * shell_SetDbgLvl
  163. * Set the special variables $DEBUG_LEVEL and $DBG_LVL
  164. */
  165. int shell_SetDbgLvl(shell_state_t * context, char *str_debug_level);
  166. /**
  167. * shell_GetDbgLvl
  168. * Get the special variable $DEBUG_LEVEL and $DBG_LVL (internal use).
  169. */
  170. int shell_GetDbgLvl(shell_state_t * context);
  171. /**
  172. * shell_GetInputStream
  173. * Get the input stream for reading commands (internal use).
  174. */
  175. FILE *shell_GetInputStream(shell_state_t * context);
  176. /**
  177. * shell_SetInput
  178. * Set the input for reading commands
  179. * and set the value of $INPUT and $INTERACTIVE.
  180. *
  181. * \param file_name:
  182. * a script file or NULL for reading from stdin.
  183. */
  184. int shell_SetInput(shell_state_t * context, char *file_name);
  185. /**
  186. * shell_SetPrompt
  187. * Set the special variable $PROMPT
  188. */
  189. int shell_SetPrompt(shell_state_t * context, char *str_prompt);
  190. /**
  191. * shell_GetPrompt
  192. * Get the special variable $PROMPT
  193. */
  194. char *shell_GetPrompt(shell_state_t * context);
  195. /**
  196. * shell_SetShellId
  197. * Set the special variable $SHELLID
  198. */
  199. int shell_SetShellId(shell_state_t * context, int shell_index);
  200. /**
  201. * shell_SetLine
  202. * Set the special variable $LINE
  203. */
  204. int shell_SetLine(shell_state_t * context, int lineno);
  205. /**
  206. * shell_GetLine
  207. * Get the special variable $LINE
  208. */
  209. int shell_GetLine(shell_state_t * context);
  210. /*------------------------------------------------------------------
  211. * Shell commands.
  212. *-----------------------------------------------------------------*/
  213. int shellcmd_help(int argc, /* IN : number of args in argv */
  214. char **argv, /* IN : arg list */
  215. FILE * output /* IN : output stream */
  216. );
  217. int shellcmd_if(int argc, /* IN : number of args in argv */
  218. char **argv, /* IN : arg list */
  219. FILE * output /* IN : output stream */
  220. );
  221. int shellcmd_interactive(int argc, /* IN : number of args in argv */
  222. char **argv, /* IN : arg list */
  223. FILE * output /* IN : output stream */
  224. );
  225. int shellcmd_set(int argc, /* IN : number of args in argv */
  226. char **argv, /* IN : arg list */
  227. FILE * output /* IN : output stream */
  228. );
  229. int shellcmd_unset(int argc, /* IN : number of args in argv */
  230. char **argv, /* IN : arg list */
  231. FILE * output /* IN : output stream */
  232. );
  233. int shellcmd_print(int argc, /* IN : number of args in argv */
  234. char **argv, /* IN : arg list */
  235. FILE * output /* IN : output stream */
  236. );
  237. int shellcmd_varlist(int argc, /* IN : number of args in argv */
  238. char **argv, /* IN : arg list */
  239. FILE * output /* IN : output stream */
  240. );
  241. int shellcmd_time(int argc, /* IN : number of args in argv */
  242. char **argv, /* IN : arg list */
  243. FILE * output /* IN : output stream */
  244. );
  245. int shellcmd_quit(int argc, /* IN : number of args in argv */
  246. char **argv, /* IN : arg list */
  247. FILE * output /* IN : output stream */
  248. );
  249. int shellcmd_barrier(int argc, /* IN : number of args in argv */
  250. char **argv, /* IN : arg list */
  251. FILE * output /* IN : output stream */
  252. );
  253. /** List of the shell commands */
  254. static command_def_t __attribute__ ((__unused__)) shell_commands[] =
  255. {
  256. {
  257. "barrier", shellcmd_barrier, "synchronization in a multi-thread shell"},
  258. {
  259. "echo", shellcmd_print, "print one or more arguments"},
  260. {
  261. "exit", shellcmd_quit, "exit this shell"},
  262. {
  263. "help", shellcmd_help, "print this help"},
  264. {
  265. "if", shellcmd_if, "conditionnal execution"},
  266. {
  267. "interactive", shellcmd_interactive, "close script file and start interactive mode"},
  268. {
  269. "print", shellcmd_print, "print one or more arguments"},
  270. {
  271. "quit", shellcmd_quit, "exit this shell"},
  272. {
  273. "set", shellcmd_set, "set the value of a shell variable"},
  274. {
  275. "time", shellcmd_time, "measures the time for executing a command"},
  276. {
  277. "unset", shellcmd_unset, "free a shell variable"},
  278. {
  279. "varlist", shellcmd_varlist, "print the list of shell variables"},
  280. {
  281. NULL, NULL, NULL} /* End of command list */
  282. };
  283. /* @todo: if, source */
  284. #endif