shell.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * shell.h
  3. */
  4. /**
  5. \ingroup clish
  6. \defgroup clish_shell shell
  7. @{
  8. \brief This class represents the top level container for a CLI session.
  9. */
  10. #ifndef _clish_shell_h
  11. #define _clish_shell_h
  12. #include <stdio.h>
  13. #include <pthread.h>
  14. #include "lub/c_decl.h"
  15. #include "lub/types.h"
  16. #include "lub/argv.h"
  17. #include "tinyrl/tinyrl.h"
  18. #include "clish/view.h"
  19. #include "clish/ptype.h"
  20. #include "clish/var.h"
  21. #include "konf/net.h"
  22. #define CLISH_LOCK_PATH "/tmp/clish.lock"
  23. #define CLISH_LOCK_WAIT 20
  24. typedef struct clish_shell_s clish_shell_t;
  25. /* This is used to hold context during callbacks */
  26. struct clish_context_s {
  27. clish_shell_t *shell;
  28. const clish_command_t *cmd;
  29. clish_pargv_t *pargv;
  30. };
  31. typedef struct clish_context_s clish_context_t;
  32. typedef enum {
  33. SHELL_STATE_OK = 0,
  34. SHELL_STATE_UNKNOWN = 1,
  35. SHELL_STATE_IO_ERROR = 2,
  36. SHELL_STATE_SCRIPT_ERROR = 3,/* Script execution error */
  37. SHELL_STATE_SYNTAX_ERROR = 4, /* Illegal line entered */
  38. SHELL_STATE_SYSTEM_ERROR = 5, /* Some internal system error */
  39. SHELL_STATE_INITIALISING = 6,
  40. SHELL_STATE_HELPING = 7,
  41. SHELL_STATE_EOF = 8, /* EOF of input stream */
  42. SHELL_STATE_CLOSING = 9
  43. } clish_shell_state_t;
  44. typedef enum {
  45. SHELL_VAR_NONE, /* Nothing to escape */
  46. SHELL_VAR_ACTION, /* Variable expanding for ACTION script */
  47. SHELL_VAR_REGEX /* Variable expanding for regex usage */
  48. } clish_shell_var_t;
  49. _BEGIN_C_DECL
  50. /*=====================================
  51. * SHELL INTERFACE
  52. *===================================== */
  53. /**
  54. * A hook function used during the spawning of a new shell.
  55. *
  56. * This will be invoked from the context of the spawned shell's thread
  57. * and will be invoked just after the shell instance is created.
  58. *
  59. * This enables the client-specific initialisation of the spawned shell's
  60. * thread
  61. * e.g. to map the I/O streams, authenticate a user.
  62. *
  63. * N.B. It is possible for a client to have this invoked multiple times
  64. * if the user is spawning new shells using a commmand which uses the
  65. * "clish_spawn" builtin function. Hence the client should remember the
  66. * shell which first calls this function, and only assign resource (e.g.
  67. * setting up a script interpreter) for that call.
  68. *
  69. * \return
  70. * - BOOL_TRUE if everything is OK
  71. * - BOOL_FALSE if the shell should be immediately shut down.
  72. *
  73. */
  74. typedef bool_t clish_shell_init_fn_t(
  75. /**
  76. * The shell instance which invoked this call
  77. */
  78. const clish_shell_t * shell);
  79. /**
  80. * A hook function used during the shutting down of a spawned shell
  81. *
  82. * This will be invoked from the context of the spawned shell's thread
  83. * and will be invoked just before the shell is destroyed.
  84. *
  85. * This enables the client-specific finalisation to occur.
  86. * e.g. releasing any resource held by the cookie,
  87. * shutting down telnet connections
  88. *
  89. * NB. This function may be called multiple times if a user is spawning
  90. * new commands (via the "clish_spawn" builtin command), hence should use
  91. * the reference to the root shell (remembered by the first call to clish_shell_init_fn_t callback)
  92. * to signal when the cleanup should occur.
  93. */
  94. typedef void clish_shell_fini_fn_t(
  95. /**
  96. * The shell instance which invoked this call
  97. */
  98. const clish_shell_t * shell);
  99. /**
  100. * A hook function used to indicate a command line has been executed and the
  101. * shell is about to prompt for the next command.
  102. *
  103. * This will be invoked from the context of the spawned shell's thread
  104. * and will be called once an ACTION has been performed.
  105. *
  106. * A client may use this as a periodic indicator of CLI activity,
  107. * e.g. to manage session timeouts. In addition any required logging of
  108. * commands may be performed.
  109. */
  110. typedef void clish_shell_cmd_line_fn_t(
  111. /**
  112. * The shell instance which invoked this call
  113. */
  114. clish_context_t *context,
  115. /**
  116. * The text of the command line entered
  117. */
  118. const char *cmd_line);
  119. /**
  120. * A hook function used to invoke the script associated with a command
  121. *
  122. * This will be invoked from the context of the spawned shell's thread
  123. * and will be invoked with the ACTION script which is to be performed.
  124. *
  125. * The clish component will only pass down a call when a command has been
  126. * correctly input.
  127. *
  128. * The client may choose to implement invocation of the script in a number of
  129. * ways, which may include forking a sub-process or thread. It is important
  130. * that the call doesn't return until the script has been fully evaluated.
  131. *
  132. * \return
  133. * - Retval (int)
  134. *
  135. * \post
  136. * - If the script executes successfully then any "view" tag associated with the
  137. * command will be honored. i.e. the CLI will switch to the new view
  138. */
  139. typedef int clish_shell_script_fn_t(
  140. clish_context_t *context,
  141. clish_action_t *action,
  142. const char *script,
  143. char **out);
  144. /**
  145. * A hook function used to control config file write
  146. *
  147. */
  148. typedef bool_t clish_shell_config_fn_t(
  149. clish_context_t *context);
  150. /**
  151. * A hook function used to control access for the current user.
  152. *
  153. * This will be invoked from the context of the spawned shell's thread
  154. * and will be called during the parsing of the XML files.
  155. *
  156. * The clish component will only insert a command into a view if the access
  157. * call is sucessfull.
  158. *
  159. * The client may choose to implement invocation of the script in a number of
  160. * ways, which may include forking a sub-process or thread. It is important
  161. * that the call doesn't return until the script has been fully evaluated.
  162. *
  163. * \return
  164. * - BOOL_TRUE - if the user of the current CLISH session is permitted access
  165. * - BOOL_FALSE - if the user of the current CLISH session is not permitted access
  166. *
  167. * \post
  168. * - If access is granted then the associated command will be inserted into the
  169. * appropriate view.
  170. */
  171. typedef bool_t clish_shell_access_fn_t(
  172. /**
  173. * The shell instance which invoked this call
  174. */
  175. const clish_shell_t * instance,
  176. /**
  177. * A textual string which describes a limitation for a command. This
  178. * string typically may be the name of a user group, of which the
  179. * current user must be a member to grant access to a command.
  180. */
  181. const char *access);
  182. typedef int clish_shell_log_fn_t(
  183. clish_context_t *context,
  184. const char *line, int retcode);
  185. /**
  186. * A hook function used as a built in command callback
  187. *
  188. * This will be invoked from the context of the spawned shell's thread
  189. * and will be called during the execution of a builting command.
  190. *
  191. * A client may register any number of these callbacks in its
  192. * clish_shell_builtin_cmds_t structure.
  193. *
  194. * \return
  195. * - Retval (int)
  196. *
  197. */
  198. typedef int clish_shell_builtin_fn_t(
  199. /**
  200. * The shell instance which invoked this call
  201. */
  202. clish_context_t *context,
  203. /**
  204. * A vector of textual command line arguments.
  205. */
  206. const lub_argv_t * argv);
  207. /**
  208. * A client of libclish may provide some builtin commands which will be
  209. * interpreted by the framework, instead of the client's script engine.
  210. */
  211. typedef struct {
  212. const char *name; /**< The textual name to be used in
  213. * the 'builtin' XML attribute"
  214. */
  215. clish_shell_builtin_fn_t *callback;
  216. /**< The function to be invoked */
  217. } clish_shell_builtin_t;
  218. /**
  219. * A client of libclish will provide hooks for the control of the CLI within
  220. * a particular system.
  221. * They will populate an instance of this structure and pass it into the
  222. */
  223. typedef struct {
  224. clish_shell_init_fn_t *init_fn; /* Initialisation call */
  225. clish_shell_access_fn_t *access_fn; /* Access control call */
  226. clish_shell_cmd_line_fn_t *cmd_line_fn; /* Command line logging call */
  227. clish_shell_script_fn_t *script_fn; /* script evaluation call */
  228. clish_shell_fini_fn_t *fini_fn; /* Finalisation call */
  229. clish_shell_config_fn_t *config_fn; /* Config call */
  230. clish_shell_log_fn_t *log_fn; /* Logging call */
  231. const clish_shell_builtin_t *cmd_list; /* NULL terminated list */
  232. } clish_shell_hooks_t;
  233. /*-----------------
  234. * meta functions
  235. *----------------- */
  236. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  237. void *cookie,
  238. FILE * istream,
  239. FILE * ostream,
  240. bool_t stop_on_error);
  241. /*-----------------
  242. * methods
  243. *----------------- */
  244. /*
  245. * Called to invoke the startup command for this shell
  246. */
  247. int clish_shell_startup(clish_shell_t * instance);
  248. void clish_shell_delete(clish_shell_t * instance);
  249. clish_view_t *clish_shell_find_create_view(clish_shell_t * instance,
  250. const char *name,
  251. const char *prompt);
  252. clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * instance,
  253. const char *name,
  254. const char *text,
  255. const char *pattern,
  256. clish_ptype_method_e method,
  257. clish_ptype_preprocess_e preprocess);
  258. clish_ptype_t *clish_shell_find_ptype(clish_shell_t *instance,
  259. const char *name);
  260. int clish_shell_xml_read(clish_shell_t * instance, const char *filename);
  261. void clish_shell_help(clish_shell_t * instance, const char *line);
  262. int clish_shell_exec_action(clish_action_t *action,
  263. clish_context_t *context, char **out);
  264. int clish_shell_execute(clish_context_t *context, char **out);
  265. int clish_shell_forceline(clish_shell_t *instance, const char *line, char ** out);
  266. int clish_shell_readline(clish_shell_t *instance, char ** out);
  267. void clish_shell_dump(clish_shell_t * instance);
  268. void clish_shell_close(clish_shell_t * instance);
  269. /**
  270. * Push the specified file handle on to the stack of file handles
  271. * for this shell. The specified file will become the source of
  272. * commands, until it is exhausted.
  273. *
  274. * \return
  275. * BOOL_TRUE - the file was successfully associated with the shell.
  276. * BOOL_FALSE - there was insufficient resource to associate this file.
  277. */
  278. int clish_shell_push_file(clish_shell_t * instance, const char * fname,
  279. bool_t stop_on_error);
  280. int clish_shell_push_fd(clish_shell_t * instance, FILE * file,
  281. bool_t stop_on_error);
  282. void clish_shell_insert_var(clish_shell_t *instance, clish_var_t *var);
  283. clish_var_t *clish_shell_find_var(clish_shell_t *instance, const char *name);
  284. char *clish_shell_expand_var(const char *name, clish_context_t *context);
  285. char *clish_shell_expand(const char *str, clish_shell_var_t vtype, clish_context_t *context);
  286. /*-----------------
  287. * attributes
  288. *----------------- */
  289. clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
  290. unsigned clish_shell__get_depth(const clish_shell_t * instance);
  291. const char *clish_shell__get_viewid(const clish_shell_t * instance);
  292. const char *clish_shell__get_overview(const clish_shell_t * instance);
  293. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
  294. void *clish_shell__get_client_cookie(const clish_shell_t * instance);
  295. void
  296. clish_shell__set_pwd(clish_shell_t *instance, const char * line,
  297. clish_view_t * view, char * viewid, clish_context_t *context);
  298. char *clish_shell__get_pwd_line(const clish_shell_t * instance,
  299. unsigned int index);
  300. char *clish_shell__get_pwd_full(const clish_shell_t * instance, unsigned depth);
  301. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
  302. unsigned int index);
  303. konf_client_t *clish_shell__get_client(const clish_shell_t * instance);
  304. FILE *clish_shell__get_istream(const clish_shell_t * instance);
  305. FILE *clish_shell__get_ostream(const clish_shell_t * instance);
  306. void clish_shell__set_lockfile(clish_shell_t * instance, const char * path);
  307. char * clish_shell__get_lockfile(clish_shell_t * instance);
  308. int clish_shell__set_socket(clish_shell_t * instance, const char * path);
  309. int clish_shell_spawn(clish_shell_t * instance,
  310. const pthread_attr_t * attr);
  311. int clish_shell_wait(clish_shell_t * instance);
  312. int clish_shell_spawn_and_wait(clish_shell_t * instance,
  313. const pthread_attr_t * attr);
  314. void clish_shell_load_scheme(clish_shell_t * instance, const char * xml_path);
  315. int clish_shell_loop(clish_shell_t * instance);
  316. clish_shell_state_t clish_shell__get_state(const clish_shell_t * instance);
  317. void clish_shell__set_state(clish_shell_t * instance,
  318. clish_shell_state_t state);
  319. void clish_shell__set_startup_view(clish_shell_t * instance, const char * viewname);
  320. void clish_shell__set_startup_viewid(clish_shell_t * instance, const char * viewid);
  321. void clish_shell__set_default_shebang(clish_shell_t * instance, const char * shebang);
  322. const char * clish_shell__get_default_shebang(const clish_shell_t * instance);
  323. const char * clish_shell__get_fifo(clish_shell_t * instance);
  324. void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
  325. bool_t clish_shell__get_interactive(const clish_shell_t * instance);
  326. bool_t clish_shell__get_utf8(const clish_shell_t * instance);
  327. void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
  328. void clish_shell__set_timeout(clish_shell_t *instance, int timeout);
  329. char *clish_shell__get_line(clish_context_t *context);
  330. char *clish_shell__get_full_line(clish_context_t *context);
  331. char *clish_shell__get_params(clish_context_t *context);
  332. void clish_shell__set_log(clish_shell_t *instance, bool_t log);
  333. bool_t clish_shell__get_log(const clish_shell_t *instance);
  334. int clish_shell_wdog(clish_shell_t *instance);
  335. void clish_shell__set_wdog_timeout(clish_shell_t *instance,
  336. unsigned int timeout);
  337. unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *instance);
  338. _END_C_DECL
  339. #endif /* _clish_shell_h */
  340. /** @} clish_shell */