shell.h 12 KB

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