shell.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "clish/plugin.h"
  23. #include "konf/net.h"
  24. #define CLISH_LOCK_PATH "/tmp/clish.lock"
  25. #define CLISH_LOCK_WAIT 20
  26. #define CLISH_XML_ERROR_STR "Error parsing XML: "
  27. #define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n"
  28. typedef struct clish_shell_s clish_shell_t;
  29. typedef struct clish_context_s clish_context_t;
  30. /* Context functions */
  31. _BEGIN_C_DECL
  32. clish_context_t *clish_context_new(clish_shell_t *shell);
  33. int clish_context_init(clish_context_t *instance, clish_shell_t *shell);
  34. void clish_context_free(clish_context_t *instance);
  35. clish_shell_t *clish_context__get_shell(const void *instance);
  36. void clish_context__set_cmd(void *instance, const clish_command_t *cmd);
  37. const clish_command_t *clish_context__get_cmd(const void *instance);
  38. void clish_context__set_pargv(void *instance, clish_pargv_t *pargv);
  39. clish_pargv_t *clish_context__get_pargv(const void *instance);
  40. void clish_context__set_action(void *instance, const clish_action_t *action);
  41. const clish_action_t *clish_context__get_action(const void *instance);
  42. _END_C_DECL
  43. /* Shell */
  44. typedef enum {
  45. SHELL_STATE_OK = 0,
  46. SHELL_STATE_UNKNOWN = 1,
  47. SHELL_STATE_IO_ERROR = 2,
  48. SHELL_STATE_SCRIPT_ERROR = 3,/* Script execution error */
  49. SHELL_STATE_SYNTAX_ERROR = 4, /* Illegal line entered */
  50. SHELL_STATE_SYSTEM_ERROR = 5, /* Some internal system error */
  51. SHELL_STATE_INITIALISING = 6,
  52. SHELL_STATE_HELPING = 7,
  53. SHELL_STATE_EOF = 8, /* EOF of input stream */
  54. SHELL_STATE_CLOSING = 9
  55. } clish_shell_state_t;
  56. typedef enum {
  57. SHELL_VAR_NONE, /* Nothing to escape */
  58. SHELL_VAR_ACTION, /* Variable expanding for ACTION script */
  59. SHELL_VAR_REGEX /* Variable expanding for regex usage */
  60. } clish_shell_var_t;
  61. _BEGIN_C_DECL
  62. /*-----------------
  63. * meta functions
  64. *----------------- */
  65. clish_shell_t *clish_shell_new(FILE * istream, FILE * ostream,
  66. bool_t stop_on_error);
  67. /*-----------------
  68. * methods
  69. *----------------- */
  70. /*
  71. * Called to invoke the startup command for this shell
  72. */
  73. int clish_shell_startup(clish_shell_t * instance);
  74. void clish_shell_delete(clish_shell_t * instance);
  75. clish_view_t *clish_shell_find_create_view(clish_shell_t * instance,
  76. const char *name,
  77. const char *prompt);
  78. clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * instance,
  79. const char *name,
  80. const char *text,
  81. const char *pattern,
  82. clish_ptype_method_e method,
  83. clish_ptype_preprocess_e preprocess);
  84. clish_ptype_t *clish_shell_find_ptype(clish_shell_t *instance,
  85. const char *name);
  86. int clish_shell_xml_read(clish_shell_t * instance, const char *filename);
  87. void clish_shell_help(clish_shell_t * instance, const char *line);
  88. int clish_shell_exec_action(clish_context_t *context, char **out);
  89. int clish_shell_execute(clish_context_t *context, char **out);
  90. int clish_shell_forceline(clish_shell_t *instance, const char *line, char ** out);
  91. int clish_shell_readline(clish_shell_t *instance, char ** out);
  92. void clish_shell_dump(clish_shell_t * instance);
  93. void clish_shell_close(clish_shell_t * instance);
  94. /**
  95. * Push the specified file handle on to the stack of file handles
  96. * for this shell. The specified file will become the source of
  97. * commands, until it is exhausted.
  98. *
  99. * \return
  100. * BOOL_TRUE - the file was successfully associated with the shell.
  101. * BOOL_FALSE - there was insufficient resource to associate this file.
  102. */
  103. int clish_shell_push_file(clish_shell_t * instance, const char * fname,
  104. bool_t stop_on_error);
  105. int clish_shell_push_fd(clish_shell_t * instance, FILE * file,
  106. bool_t stop_on_error);
  107. void clish_shell_insert_var(clish_shell_t *instance, clish_var_t *var);
  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(const char *str, clish_shell_var_t vtype, clish_context_t *context);
  111. /*-----------------
  112. * attributes
  113. *----------------- */
  114. clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
  115. unsigned int clish_shell__get_depth(const clish_shell_t * instance);
  116. void clish_shell__set_depth(clish_shell_t *instance, unsigned int depth);
  117. const char *clish_shell__get_viewid(const clish_shell_t * instance);
  118. const char *clish_shell__get_overview(const clish_shell_t * instance);
  119. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
  120. void clish_shell__set_pwd(clish_shell_t *instance, const char * line,
  121. clish_view_t * view, char * viewid, clish_context_t *context);
  122. char *clish_shell__get_pwd_line(const clish_shell_t * instance,
  123. unsigned int index);
  124. char *clish_shell__get_pwd_full(const clish_shell_t * instance,
  125. unsigned int depth);
  126. clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
  127. unsigned int index);
  128. konf_client_t *clish_shell__get_client(const clish_shell_t * instance);
  129. FILE *clish_shell__get_istream(const clish_shell_t * instance);
  130. FILE *clish_shell__get_ostream(const clish_shell_t * instance);
  131. void clish_shell__set_lockfile(clish_shell_t * instance, const char * path);
  132. char * clish_shell__get_lockfile(clish_shell_t * instance);
  133. int clish_shell__set_socket(clish_shell_t * instance, const char * path);
  134. int clish_shell_load_scheme(clish_shell_t * instance, const char * xml_path);
  135. int clish_shell_loop(clish_shell_t * instance);
  136. clish_shell_state_t clish_shell__get_state(const clish_shell_t * instance);
  137. void clish_shell__set_state(clish_shell_t * instance,
  138. clish_shell_state_t state);
  139. void clish_shell__set_startup_view(clish_shell_t * instance, const char * viewname);
  140. void clish_shell__set_startup_viewid(clish_shell_t * instance, const char * viewid);
  141. void clish_shell__set_default_shebang(clish_shell_t * instance, const char * shebang);
  142. const char * clish_shell__get_default_shebang(const clish_shell_t * instance);
  143. const char * clish_shell__get_fifo(clish_shell_t * instance);
  144. void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
  145. bool_t clish_shell__get_interactive(const clish_shell_t * instance);
  146. bool_t clish_shell__get_utf8(const clish_shell_t * instance);
  147. void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
  148. void clish_shell__set_timeout(clish_shell_t *instance, int timeout);
  149. char *clish_shell__get_line(clish_context_t *context);
  150. char *clish_shell__get_full_line(clish_context_t *context);
  151. char *clish_shell__get_params(clish_context_t *context);
  152. /* Log functions */
  153. void clish_shell__set_log(clish_shell_t *instance, bool_t log);
  154. bool_t clish_shell__get_log(const clish_shell_t *instance);
  155. void clish_shell__set_facility(clish_shell_t *instance, int facility);
  156. int clish_shell__get_facility(clish_shell_t *instance);
  157. int clish_shell_wdog(clish_shell_t *instance);
  158. void clish_shell__set_wdog_timeout(clish_shell_t *instance,
  159. unsigned int timeout);
  160. unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *instance);
  161. int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
  162. int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
  163. void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
  164. struct passwd *clish_shell__get_user(clish_shell_t *instance);
  165. void clish_shell__set_dryrun(clish_shell_t *instance, bool_t dryrun);
  166. bool_t clish_shell__get_dryrun(const clish_shell_t *instance);
  167. /* Plugin functions */
  168. clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance,
  169. const char *name);
  170. clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *instance,
  171. const char *name);
  172. int clish_shell_load_plugins(clish_shell_t *instance);
  173. int clish_shell_link_plugins(clish_shell_t *instance);
  174. /* Unresolved symbols functions */
  175. clish_sym_t *clish_shell_find_sym(clish_shell_t *instance,
  176. const char *name, int type);
  177. clish_sym_t *clish_shell_add_sym(clish_shell_t *instance,
  178. void *func, const char *name, int type);
  179. clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance,
  180. const char *name, int type);
  181. clish_sym_t *clish_shell_get_hook(const clish_shell_t *instance, int type);
  182. /* Hook wrappers */
  183. void *clish_shell_check_hook(const clish_context_t *clish_context, int type);
  184. CLISH_HOOK_CONFIG(clish_shell_exec_config);
  185. CLISH_HOOK_LOG(clish_shell_exec_log);
  186. /* User data functions */
  187. void *clish_shell__get_udata(const clish_shell_t *instance, const char *name);
  188. void *clish_shell__del_udata(clish_shell_t *instance, const char *name);
  189. int clish_shell__set_udata(clish_shell_t *instance,
  190. const char *name, void *data);
  191. /* Access functions */
  192. int clish_shell_prepare(clish_shell_t *instance);
  193. _END_C_DECL
  194. #endif /* _clish_shell_h */
  195. /** @} clish_shell */