shell.h 13 KB

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