shell_new.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include "lub/string.h"
  10. #include "lub/db.h"
  11. #include "lub/list.h"
  12. #include "clish/plugin.h"
  13. /*-------------------------------------------------------- */
  14. static void clish_shell_init(clish_shell_t * this,
  15. void *cookie, FILE * istream,
  16. FILE * ostream,
  17. bool_t stop_on_error)
  18. {
  19. clish_ptype_t *tmp_ptype = NULL;
  20. clish_plugin_t *plugin = NULL;
  21. /* initialise the tree of views */
  22. lub_bintree_init(&this->view_tree,
  23. clish_view_bt_offset(),
  24. clish_view_bt_compare, clish_view_bt_getkey);
  25. /* initialise the tree of ptypes */
  26. lub_bintree_init(&this->ptype_tree,
  27. clish_ptype_bt_offset(),
  28. clish_ptype_bt_compare, clish_ptype_bt_getkey);
  29. /* initialise the tree of vars */
  30. lub_bintree_init(&this->var_tree,
  31. clish_var_bt_offset(),
  32. clish_var_bt_compare, clish_var_bt_getkey);
  33. /* Initialize plugin list */
  34. this->plugins = lub_list_new(NULL);
  35. /* Create internal plugin "clish" */
  36. plugin = clish_plugin_new("clish", NULL);
  37. lub_list_add(this->plugins, plugin);
  38. /* Initialise the list of unresolved (yet) symbols */
  39. this->syms = lub_list_new(clish_sym_compare);
  40. /* Create userdata storage */
  41. this->udata = lub_list_new(clish_udata_compare);
  42. assert(this->udata);
  43. /* Default syms and hooks */
  44. this->hooks[CLISH_SYM_TYPE_NONE] = NULL;
  45. this->hooks[CLISH_SYM_TYPE_ACTION] = clish_sym_new(
  46. CLISH_DEFAULT_SYM, NULL, CLISH_SYM_TYPE_ACTION);
  47. this->hooks[CLISH_SYM_TYPE_INIT] = NULL;
  48. this->hooks[CLISH_SYM_TYPE_FINI] = NULL;
  49. this->hooks[CLISH_SYM_TYPE_ACCESS] = clish_sym_new(
  50. CLISH_DEFAULT_ACCESS, NULL, CLISH_SYM_TYPE_ACCESS);
  51. this->hooks[CLISH_SYM_TYPE_CONFIG] = clish_sym_new(
  52. CLISH_DEFAULT_CONFIG, NULL, CLISH_SYM_TYPE_CONFIG);
  53. this->hooks[CLISH_SYM_TYPE_LOG] = clish_sym_new(
  54. CLISH_DEFAULT_LOG, NULL, CLISH_SYM_TYPE_LOG);
  55. /* set up defaults */
  56. this->client_cookie = cookie;
  57. this->global = NULL;
  58. this->startup = NULL;
  59. this->idle_timeout = 0; /* No idle timeout by default */
  60. this->wdog = NULL;
  61. this->wdog_timeout = 0; /* No watchdog timeout by default */
  62. this->wdog_active = BOOL_FALSE;
  63. this->state = SHELL_STATE_INITIALISING;
  64. this->overview = NULL;
  65. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  66. this->current_file = NULL;
  67. this->pwdv = NULL;
  68. this->pwdc = 0;
  69. this->depth = -1; /* Current depth is undefined */
  70. this->client = NULL;
  71. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  72. this->default_shebang = lub_string_dup("/bin/sh");
  73. this->fifo_name = NULL;
  74. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  75. this->log = BOOL_FALSE; /* Disable logging by default */
  76. this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
  77. this->user = lub_db_getpwuid(getuid()); /* Get user information */
  78. /* Create internal ptypes and params */
  79. /* Current depth */
  80. tmp_ptype = clish_shell_find_create_ptype(this,
  81. "__DEPTH", "Depth", "[0-9]+",
  82. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  83. assert(tmp_ptype);
  84. this->param_depth = clish_param_new("_cur_depth",
  85. "Current depth", tmp_ptype);
  86. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  87. /* Current pwd */
  88. tmp_ptype = clish_shell_find_create_ptype(this,
  89. "__PWD", "Path", ".+",
  90. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  91. assert(tmp_ptype);
  92. this->param_pwd = clish_param_new("_cur_pwd",
  93. "Current path", tmp_ptype);
  94. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  95. /* Args */
  96. tmp_ptype = clish_shell_find_create_ptype(this,
  97. "internal_ARGS",
  98. "Arguments", "[^\\\\]+",
  99. CLISH_PTYPE_REGEXP,
  100. CLISH_PTYPE_NONE);
  101. assert(tmp_ptype);
  102. /* Push non-NULL istream */
  103. if (istream)
  104. clish_shell_push_fd(this, istream, stop_on_error);
  105. }
  106. /*--------------------------------------------------------- */
  107. static void clish_shell_fini(clish_shell_t * this)
  108. {
  109. clish_view_t *view;
  110. clish_ptype_t *ptype;
  111. clish_var_t *var;
  112. unsigned i;
  113. lub_list_node_t *iter;
  114. /* delete each VIEW held */
  115. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  116. lub_bintree_remove(&this->view_tree, view);
  117. clish_view_delete(view);
  118. }
  119. /* delete each PTYPE held */
  120. while ((ptype = lub_bintree_findfirst(&this->ptype_tree))) {
  121. lub_bintree_remove(&this->ptype_tree, ptype);
  122. clish_ptype_delete(ptype);
  123. }
  124. /* delete each VAR held */
  125. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  126. lub_bintree_remove(&this->var_tree, var);
  127. clish_var_delete(var);
  128. }
  129. /* Free all loaded plugins */
  130. while ((iter = lub_list__get_head(this->plugins))) {
  131. /* Remove the symbol from the list */
  132. lub_list_del(this->plugins, iter);
  133. /* Free the instance */
  134. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter));
  135. lub_list_node_free(iter);
  136. }
  137. lub_list_free(this->plugins);
  138. /* Free symbol list */
  139. while ((iter = lub_list__get_head(this->syms))) {
  140. /* Remove the symbol from the list */
  141. lub_list_del(this->syms, iter);
  142. /* Free the instance */
  143. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  144. lub_list_node_free(iter);
  145. }
  146. lub_list_free(this->syms);
  147. /* Free user data storage */
  148. while ((iter = lub_list__get_head(this->udata))) {
  149. /* Remove the symbol from the list */
  150. lub_list_del(this->udata, iter);
  151. /* Free the instance */
  152. clish_udata_free((clish_udata_t *)lub_list_node__get_data(iter));
  153. lub_list_node_free(iter);
  154. }
  155. lub_list_free(this->udata);
  156. /* free the textual details */
  157. lub_string_free(this->overview);
  158. /* Remove the startup command */
  159. if (this->startup)
  160. clish_command_delete(this->startup);
  161. /* Remove the watchdog command */
  162. if (this->wdog)
  163. clish_command_delete(this->wdog);
  164. /* clean up the file stack */
  165. while (!clish_shell_pop_file(this));
  166. /* delete the tinyrl object */
  167. clish_shell_tinyrl_delete(this->tinyrl);
  168. /* finalize each of the pwd strings */
  169. for (i = 0; i < this->pwdc; i++) {
  170. clish_shell__fini_pwd(this->pwdv[i]);
  171. free(this->pwdv[i]);
  172. }
  173. /* free the pwd vector */
  174. free(this->pwdv);
  175. konf_client_free(this->client);
  176. /* Free internal params */
  177. clish_param_delete(this->param_depth);
  178. clish_param_delete(this->param_pwd);
  179. lub_string_free(this->lockfile);
  180. lub_string_free(this->default_shebang);
  181. free(this->user);
  182. if (this->fifo_name) {
  183. unlink(this->fifo_name);
  184. lub_string_free(this->fifo_name);
  185. }
  186. }
  187. /*-------------------------------------------------------- */
  188. clish_shell_t *clish_shell_new(
  189. void *cookie,
  190. FILE * istream,
  191. FILE * ostream,
  192. bool_t stop_on_error)
  193. {
  194. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  195. if (this) {
  196. clish_shell_init(this, cookie,
  197. istream, ostream, stop_on_error);
  198. // if (hooks->init_fn) {
  199. /* now call the client initialisation */
  200. // if (BOOL_TRUE != hooks->init_fn(this))
  201. // this->state = SHELL_STATE_CLOSING;
  202. // }
  203. }
  204. return this;
  205. }
  206. /*--------------------------------------------------------- */
  207. void clish_shell_delete(clish_shell_t *this)
  208. {
  209. clish_context_t context;
  210. context.shell = this;
  211. /* Now call the client finalization */
  212. clish_shell_exec_fini(&context);
  213. clish_shell_fini(this);
  214. free(this);
  215. }
  216. /*--------------------------------------------------------- */
  217. struct passwd *clish_shell__get_user(clish_shell_t * this)
  218. {
  219. return this->user;
  220. }
  221. /*-------------------------------------------------------- */