shell.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/macros.h"
  20. #include "clish/view.h"
  21. #include "clish/ptype.h"
  22. #include "clish/var.h"
  23. #include "clish/plugin.h"
  24. #include "konf/net.h"
  25. #define CLISH_LOCK_PATH "/tmp/clish.lock"
  26. #define CLISH_LOCK_WAIT 20
  27. #define CLISH_STDOUT_CHUNK 1024
  28. #define CLISH_STDOUT_MAXBUF (CLISH_STDOUT_CHUNK * 1024)
  29. #define CLISH_XML_ERROR_STR "Error parsing XML: "
  30. #define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n"
  31. typedef struct clish_shell_s clish_shell_t;
  32. typedef struct clish_context_s clish_context_t;
  33. /* Context functions */
  34. _BEGIN_C_DECL
  35. clish_context_t *clish_context_new(clish_shell_t *shell);
  36. int clish_context_init(clish_context_t *instance, clish_shell_t *shell);
  37. void clish_context_free(clish_context_t *instance);
  38. int clish_context_dup(clish_context_t *dst, const clish_context_t *src);
  39. clish_shell_t *clish_context__get_shell(const void *instance);
  40. void clish_context__set_cmd(void *instance, const clish_command_t *cmd);
  41. const clish_command_t *clish_context__get_cmd(const void *instance);
  42. void clish_context__set_pargv(void *instance, clish_pargv_t *pargv);
  43. clish_pargv_t *clish_context__get_pargv(const void *instance);
  44. void clish_context__set_action(void *instance, const clish_action_t *action);
  45. const clish_action_t *clish_context__get_action(const void *instance);
  46. bool_t clish_context__get_expand(const clish_context_t *context);
  47. _END_C_DECL
  48. /* Shell */
  49. typedef enum {
  50. SHELL_STATE_OK = 0,
  51. SHELL_STATE_UNKNOWN = 1,
  52. SHELL_STATE_IO_ERROR = 2,
  53. SHELL_STATE_SCRIPT_ERROR = 3,/* Script execution error */
  54. SHELL_STATE_SYNTAX_ERROR = 4, /* Illegal line entered */
  55. SHELL_STATE_SYSTEM_ERROR = 5, /* Some internal system error */
  56. SHELL_STATE_INITIALISING = 6,
  57. SHELL_STATE_HELPING = 7,
  58. SHELL_STATE_EOF = 8, /* EOF of input stream */
  59. SHELL_STATE_CLOSING = 9
  60. } clish_shell_state_e;
  61. typedef enum {
  62. SHELL_VAR_NONE, /* Nothing to escape */
  63. SHELL_VAR_ACTION, /* Variable expanding for ACTION script */
  64. SHELL_VAR_REGEX /* Variable expanding for regex usage */
  65. } clish_shell_var_e;
  66. typedef enum {
  67. SHELL_EXPAND_PARAM = 1,
  68. SHELL_EXPAND_VIEW = 2,
  69. SHELL_EXPAND_CONTEXT = 4,
  70. SHELL_EXPAND_VAR = 8,
  71. SHELL_EXPAND_ENV = 16,
  72. SHELL_EXPAND_ALL = 255
  73. } clish_shell_expand_e;
  74. _BEGIN_C_DECL
  75. clish_shell_t *clish_shell_new(FILE *istream, FILE *ostream, bool_t stop_on_error);
  76. int clish_shell_startup(clish_shell_t *instance);
  77. void clish_shell_delete(clish_shell_t *instance);
  78. clish_view_t *clish_shell_find_create_view(clish_shell_t *instance, const char *name);
  79. clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t *instance,
  80. const char *name,
  81. const char *text,
  82. const char *pattern,
  83. clish_ptype_method_e method,
  84. clish_ptype_preprocess_e preprocess);
  85. clish_view_t *clish_shell_find_view(clish_shell_t * instance,
  86. const char *name);
  87. clish_ptype_t *clish_shell_find_ptype(clish_shell_t *instance,
  88. const char *name);
  89. void clish_shell_help(clish_shell_t * instance, const char *line);
  90. int clish_shell_exec_action(clish_context_t *context, char **out);
  91. int clish_shell_execute(clish_context_t *context, char **out);
  92. int clish_shell_forceline(clish_shell_t *instance, const char *line, char ** out);
  93. int clish_shell_readline(clish_shell_t *instance, char ** out);
  94. void clish_shell_dump(clish_shell_t * instance);
  95. /**
  96. * Push the specified file handle on to the stack of file handles
  97. * for this shell. The specified file will become the source of
  98. * commands, until it is exhausted.
  99. *
  100. * \return
  101. * BOOL_TRUE - the file was successfully associated with the shell.
  102. * BOOL_FALSE - there was insufficient resource to associate this file.
  103. */
  104. int clish_shell_push_file(clish_shell_t * instance, const char * fname,
  105. bool_t stop_on_error);
  106. int clish_shell_push_fd(clish_shell_t * instance, FILE * file,
  107. bool_t stop_on_error);
  108. clish_var_t *clish_shell_find_var(clish_shell_t *instance, const char *name);
  109. char *clish_shell_expand_var(const char *name, clish_context_t *context);
  110. char *clish_shell_expand_var_ex(const char *name, clish_context_t *context, clish_shell_expand_e flags);
  111. char *clish_shell_expand(const char *str, clish_shell_var_e vtype, clish_context_t *context);
  112. char * clish_shell_mkfifo(clish_shell_t * instance, char *name, size_t n);
  113. int clish_shell_rmfifo(clish_shell_t * instance, const char *name);
  114. /*-----------------
  115. * attributes
  116. *----------------- */
  117. _CLISH_GET_STR(shell, overview);
  118. _CLISH_SET_STR(shell, lockfile);
  119. _CLISH_GET_STR(shell, lockfile);
  120. _CLISH_SET_STR(shell, default_shebang);
  121. _CLISH_GET_STR(shell, default_shebang);
  122. _CLISH_SET(shell, bool_t, default_expand);
  123. _CLISH_GET(shell, bool_t, default_expand);
  124. _CLISH_SET(shell, unsigned int, idle_timeout);
  125. _CLISH_SET(shell, unsigned int, wdog_timeout);
  126. _CLISH_GET(shell, unsigned int, wdog_timeout);
  127. _CLISH_GET(shell, unsigned int, depth);
  128. _CLISH_SET(shell, int, log_facility);
  129. _CLISH_GET(shell, int, log_facility);
  130. _CLISH_GET(shell, konf_client_t *, client);
  131. _CLISH_GET(shell, struct passwd *, user);
  132. _CLISH_SET(shell, clish_shell_state_e, state);
  133. _CLISH_GET(shell, clish_shell_state_e, state);
  134. _CLISH_SET(shell, bool_t, interactive);
  135. _CLISH_GET(shell, bool_t, interactive);
  136. _CLISH_SET(shell, bool_t, log);
  137. _CLISH_GET(shell, bool_t, log);
  138. _CLISH_SET(shell, bool_t, dryrun);
  139. _CLISH_GET(shell, bool_t, dryrun);
  140. _CLISH_SET(shell, bool_t, canon_out);
  141. _CLISH_GET(shell, bool_t, canon_out);
  142. clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
  143. clish_view_t *clish_shell__set_depth(clish_shell_t *instance, unsigned int depth);
  144. const char *clish_shell__get_viewid(const clish_shell_t * instance);
  145. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
  146. void clish_shell__set_pwd(clish_shell_t *instance, const char * line,
  147. clish_view_t * view, const char * viewid, clish_context_t *context);
  148. char *clish_shell__get_pwd_line(const clish_shell_t * instance,
  149. unsigned int index);
  150. clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *instance,
  151. unsigned int index);
  152. char *clish_shell__get_pwd_cmd(const clish_shell_t *instance,
  153. unsigned int index);
  154. char *clish_shell__get_pwd_prefix(const clish_shell_t *instance,
  155. unsigned int index);
  156. char *clish_shell__get_pwd_full(const clish_shell_t * instance,
  157. unsigned int depth);
  158. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
  159. unsigned int index);
  160. FILE *clish_shell__get_istream(const clish_shell_t * instance);
  161. FILE *clish_shell__get_ostream(const clish_shell_t * instance);
  162. int clish_shell__set_socket(clish_shell_t * instance, const char * path);
  163. int clish_shell_load_scheme(clish_shell_t * instance, const char * xml_path, const char *xslt_path);
  164. int clish_shell_loop(clish_shell_t * instance);
  165. void clish_shell__set_startup_view(clish_shell_t * instance, const char * viewname);
  166. void clish_shell__set_startup_viewid(clish_shell_t * instance, const char * viewid);
  167. bool_t clish_shell__get_utf8(const clish_shell_t * instance);
  168. void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
  169. char *clish_shell__get_line(clish_context_t *context);
  170. char *clish_shell__get_full_line(clish_context_t *context);
  171. char *clish_shell__get_params(clish_context_t *context);
  172. int clish_shell_wdog(clish_shell_t *instance);
  173. int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
  174. int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
  175. void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
  176. char *clish_shell_format_username(const clish_shell_t *instance);
  177. void clish_shell__disable_echo(clish_shell_t *instance);
  178. /* Plugin functions */
  179. clish_plugin_t * clish_shell_create_plugin(clish_shell_t *instance,
  180. const char *name);
  181. clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance,
  182. const char *name);
  183. clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *instance,
  184. const char *name);
  185. int clish_shell_load_plugins(clish_shell_t *instance);
  186. int clish_shell_link_plugins(clish_shell_t *instance);
  187. /* Unresolved symbols functions */
  188. clish_sym_t *clish_shell_find_sym(clish_shell_t *instance,
  189. const char *name, int type);
  190. clish_sym_t *clish_shell_add_sym(clish_shell_t *instance,
  191. void *func, const char *name, int type);
  192. clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance,
  193. const char *name, int type);
  194. clish_sym_t *clish_shell_get_hook(const clish_shell_t *instance, int type);
  195. /* Hook wrappers */
  196. const void *clish_shell_check_hook(const clish_context_t *clish_context, int type);
  197. CLISH_HOOK_CONFIG(clish_shell_exec_config);
  198. CLISH_HOOK_LOG(clish_shell_exec_log);
  199. /* User data functions */
  200. void *clish_shell__get_udata(const clish_shell_t *instance, const char *name);
  201. void *clish_shell__del_udata(clish_shell_t *instance, const char *name);
  202. int clish_shell__set_udata(clish_shell_t *instance,
  203. const char *name, void *data);
  204. /* Access functions */
  205. int clish_shell_prepare(clish_shell_t *instance);
  206. /* Machine/Human oriented protocol */
  207. bool_t clish_shell_is_machine_interface(const clish_shell_t *shell);
  208. void clish_shell_set_machine_interface(clish_shell_t *shell);
  209. void clish_shell_set_human_interface(clish_shell_t *shell);
  210. void clish_shell_machine_retval(clish_shell_t *shell, int retval);
  211. /*
  212. * Non shell specific functions.
  213. * Start and Stop XML parser engine.
  214. */
  215. int clish_xmldoc_start(void);
  216. int clish_xmldoc_stop(void);
  217. _END_C_DECL
  218. #endif /* _clish_shell_h */
  219. /** @} clish_shell */