shell.h 7.6 KB

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