shell.h 13 KB

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