shell.h 14 KB

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