shell.h 12 KB

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