1 /* 2 * shell.h 3 */ 4 /** 5 \ingroup clish 6 \defgroup clish_shell shell 7 @{ 8 9 \brief This class represents the top level container for a CLI session. 10 11 */ 12 #ifndef _clish_shell_h 13 #define _clish_shell_h 14 15 #include <stdio.h> 16 #include <sys/types.h> 17 #include <pwd.h> 18 19 #include "lub/c_decl.h" 20 #include "lub/types.h" 21 #include "lub/argv.h" 22 #include "tinyrl/tinyrl.h" 23 #include "clish/view.h" 24 #include "clish/ptype.h" 25 #include "clish/var.h" 26 #include "clish/plugin.h" 27 #include "konf/net.h" 28 29 #define CLISH_LOCK_PATH "/tmp/clish.lock" 30 #define CLISH_LOCK_WAIT 20 31 32 #define CLISH_STDOUT_CHUNK 1024 33 #define CLISH_STDOUT_MAXBUF (CLISH_STDOUT_CHUNK * 1024) 34 35 #define CLISH_XML_ERROR_STR "Error parsing XML: " 36 #define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n" 37 38 typedef struct clish_shell_s clish_shell_t; 39 typedef struct clish_context_s clish_context_t; 40 41 /* Context functions */ 42 _BEGIN_C_DECL 43 clish_context_t *clish_context_new(clish_shell_t *shell); 44 int clish_context_init(clish_context_t *instance, clish_shell_t *shell); 45 void clish_context_free(clish_context_t *instance); 46 int clish_context_dup(clish_context_t *dst, const clish_context_t *src); 47 clish_shell_t *clish_context__get_shell(const void *instance); 48 void clish_context__set_cmd(void *instance, const clish_command_t *cmd); 49 const clish_command_t *clish_context__get_cmd(const void *instance); 50 void clish_context__set_pargv(void *instance, clish_pargv_t *pargv); 51 clish_pargv_t *clish_context__get_pargv(const void *instance); 52 void clish_context__set_action(void *instance, const clish_action_t *action); 53 const clish_action_t *clish_context__get_action(const void *instance); 54 _END_C_DECL 55 56 /* Shell */ 57 typedef enum { 58 SHELL_STATE_OK = 0, 59 SHELL_STATE_UNKNOWN = 1, 60 SHELL_STATE_IO_ERROR = 2, 61 SHELL_STATE_SCRIPT_ERROR = 3,/* Script execution error */ 62 SHELL_STATE_SYNTAX_ERROR = 4, /* Illegal line entered */ 63 SHELL_STATE_SYSTEM_ERROR = 5, /* Some internal system error */ 64 SHELL_STATE_INITIALISING = 6, 65 SHELL_STATE_HELPING = 7, 66 SHELL_STATE_EOF = 8, /* EOF of input stream */ 67 SHELL_STATE_CLOSING = 9 68 } clish_shell_state_e; 69 70 typedef enum { 71 SHELL_VAR_NONE, /* Nothing to escape */ 72 SHELL_VAR_ACTION, /* Variable expanding for ACTION script */ 73 SHELL_VAR_REGEX /* Variable expanding for regex usage */ 74 } clish_shell_var_e; 75 76 typedef enum { 77 SHELL_EXPAND_PARAM = 1, 78 SHELL_EXPAND_VIEW = 2, 79 SHELL_EXPAND_CONTEXT = 4, 80 SHELL_EXPAND_VAR = 8, 81 SHELL_EXPAND_ENV = 16, 82 SHELL_EXPAND_ALL = 255 83 } clish_shell_expand_e; 84 85 _BEGIN_C_DECL 86 87 /*----------------- 88 * meta functions 89 *----------------- */ 90 91 clish_shell_t *clish_shell_new(FILE * istream, FILE * ostream, 92 bool_t stop_on_error); 93 /*----------------- 94 * methods 95 *----------------- */ 96 /* 97 * Called to invoke the startup command for this shell 98 */ 99 int clish_shell_startup(clish_shell_t * instance); 100 void clish_shell_delete(clish_shell_t * instance); 101 clish_view_t *clish_shell_find_create_view(clish_shell_t * instance, 102 const char *name, 103 const char *prompt); 104 clish_ptype_t *clish_shell_find_create_ptype(clish_shell_t * instance, 105 const char *name, 106 const char *text, 107 const char *pattern, 108 clish_ptype_method_e method, 109 clish_ptype_preprocess_e preprocess); 110 clish_ptype_t *clish_shell_find_ptype(clish_shell_t *instance, 111 const char *name); 112 void clish_shell_help(clish_shell_t * instance, const char *line); 113 int clish_shell_exec_action(clish_context_t *context, char **out, bool_t intr); 114 int clish_shell_execute(clish_context_t *context, char **out); 115 int clish_shell_forceline(clish_shell_t *instance, const char *line, char ** out); 116 int clish_shell_readline(clish_shell_t *instance, char ** out); 117 void clish_shell_dump(clish_shell_t * instance); 118 /** 119 * Push the specified file handle on to the stack of file handles 120 * for this shell. The specified file will become the source of 121 * commands, until it is exhausted. 122 * 123 * \return 124 * BOOL_TRUE - the file was successfully associated with the shell. 125 * BOOL_FALSE - there was insufficient resource to associate this file. 126 */ 127 int clish_shell_push_file(clish_shell_t * instance, const char * fname, 128 bool_t stop_on_error); 129 int clish_shell_push_fd(clish_shell_t * instance, FILE * file, 130 bool_t stop_on_error); 131 void clish_shell_insert_var(clish_shell_t *instance, clish_var_t *var); 132 clish_var_t *clish_shell_find_var(clish_shell_t *instance, const char *name); 133 char *clish_shell_expand_var(const char *name, clish_context_t *context); 134 char *clish_shell_expand_var_ex(const char *name, clish_context_t *context, clish_shell_expand_e flags); 135 char *clish_shell_expand(const char *str, clish_shell_var_e vtype, clish_context_t *context); 136 char * clish_shell_mkfifo(clish_shell_t * instance, char *name, size_t n); 137 int clish_shell_rmfifo(clish_shell_t * instance, const char *name); 138 139 /*----------------- 140 * attributes 141 *----------------- */ 142 clish_view_t *clish_shell__get_view(const clish_shell_t * instance); 143 unsigned int clish_shell__get_depth(const clish_shell_t * instance); 144 clish_view_t *clish_shell__set_depth(clish_shell_t *instance, unsigned int depth); 145 const char *clish_shell__get_viewid(const clish_shell_t * instance); 146 const char *clish_shell__get_overview(const clish_shell_t * instance); 147 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance); 148 void clish_shell__set_pwd(clish_shell_t *instance, const char * line, 149 clish_view_t * view, char * viewid, clish_context_t *context); 150 char *clish_shell__get_pwd_line(const clish_shell_t * instance, 151 unsigned int index); 152 clish_pargv_t *clish_shell__get_pwd_pargv(const clish_shell_t *instance, 153 unsigned int index); 154 char *clish_shell__get_pwd_cmd(const clish_shell_t *instance, 155 unsigned int index); 156 char *clish_shell__get_pwd_prefix(const clish_shell_t *instance, 157 unsigned int index); 158 char *clish_shell__get_pwd_full(const clish_shell_t * instance, 159 unsigned int depth); 160 clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance, 161 unsigned int index); 162 konf_client_t *clish_shell__get_client(const clish_shell_t * instance); 163 FILE *clish_shell__get_istream(const clish_shell_t * instance); 164 FILE *clish_shell__get_ostream(const clish_shell_t * instance); 165 void clish_shell__set_lockfile(clish_shell_t * instance, const char * path); 166 char * clish_shell__get_lockfile(clish_shell_t * instance); 167 int clish_shell__set_socket(clish_shell_t * instance, const char * path); 168 int clish_shell_load_scheme(clish_shell_t * instance, const char * xml_path, const char *xslt_path); 169 int clish_shell_loop(clish_shell_t * instance); 170 clish_shell_state_e clish_shell__get_state(const clish_shell_t * instance); 171 void clish_shell__set_state(clish_shell_t * instance, 172 clish_shell_state_e state); 173 void clish_shell__set_startup_view(clish_shell_t * instance, const char * viewname); 174 void clish_shell__set_startup_viewid(clish_shell_t * instance, const char * viewid); 175 void clish_shell__set_default_shebang(clish_shell_t * instance, const char * shebang); 176 const char * clish_shell__get_default_shebang(const clish_shell_t * instance); 177 void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive); 178 bool_t clish_shell__get_interactive(const clish_shell_t * instance); 179 bool_t clish_shell__get_utf8(const clish_shell_t * instance); 180 void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8); 181 void clish_shell__set_timeout(clish_shell_t *instance, unsigned int timeout); 182 char *clish_shell__get_line(clish_context_t *context); 183 char *clish_shell__get_full_line(clish_context_t *context); 184 char *clish_shell__get_params(clish_context_t *context); 185 186 /* Log functions */ 187 void clish_shell__set_log(clish_shell_t *instance, bool_t log); 188 bool_t clish_shell__get_log(const clish_shell_t *instance); 189 void clish_shell__set_facility(clish_shell_t *instance, int facility); 190 int clish_shell__get_facility(clish_shell_t *instance); 191 192 int clish_shell_wdog(clish_shell_t *instance); 193 void clish_shell__set_wdog_timeout(clish_shell_t *instance, 194 unsigned int timeout); 195 unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *instance); 196 int clish_shell__save_history(const clish_shell_t *instance, const char *fname); 197 int clish_shell__restore_history(clish_shell_t *instance, const char *fname); 198 void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle); 199 struct passwd *clish_shell__get_user(clish_shell_t *instance); 200 void clish_shell__set_dryrun(clish_shell_t *instance, bool_t dryrun); 201 bool_t clish_shell__get_dryrun(const clish_shell_t *instance); 202 void clish_shell__set_canon_out(clish_shell_t *instance, bool_t canon_out); 203 bool_t clish_shell__get_canon_out(const clish_shell_t *instance); 204 205 /* Plugin functions */ 206 clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance, 207 const char *name); 208 clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *instance, 209 const char *name); 210 int clish_shell_load_plugins(clish_shell_t *instance); 211 int clish_shell_link_plugins(clish_shell_t *instance); 212 213 /* Unresolved symbols functions */ 214 clish_sym_t *clish_shell_find_sym(clish_shell_t *instance, 215 const char *name, int type); 216 clish_sym_t *clish_shell_add_sym(clish_shell_t *instance, 217 void *func, const char *name, int type); 218 clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance, 219 const char *name, int type); 220 clish_sym_t *clish_shell_get_hook(const clish_shell_t *instance, int type); 221 222 /* Hook wrappers */ 223 void *clish_shell_check_hook(const clish_context_t *clish_context, int type); 224 CLISH_HOOK_CONFIG(clish_shell_exec_config); 225 CLISH_HOOK_LOG(clish_shell_exec_log); 226 227 /* User data functions */ 228 void *clish_shell__get_udata(const clish_shell_t *instance, const char *name); 229 void *clish_shell__del_udata(clish_shell_t *instance, const char *name); 230 int clish_shell__set_udata(clish_shell_t *instance, 231 const char *name, void *data); 232 233 /* Access functions */ 234 int clish_shell_prepare(clish_shell_t *instance); 235 236 /* 237 * Non shell specific functions. 238 * Start and Stop XML parser engine. 239 */ 240 int clish_xmldoc_start(void); 241 int clish_xmldoc_stop(void); 242 243 _END_C_DECL 244 245 #endif /* _clish_shell_h */ 246 /** @} clish_shell */ 247