shell_new.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. int i;
  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. /* Initialise the list of unresolved (yet) symbols */
  36. this->syms = lub_list_new(clish_sym_compare);
  37. /* Create userdata storage */
  38. this->udata = lub_list_new(clish_udata_compare);
  39. assert(this->udata);
  40. /* Hooks */
  41. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  42. this->hooks[i] = clish_sym_new(NULL, NULL, i);
  43. this->hooks_use[i] = BOOL_FALSE;
  44. }
  45. /* Set up defaults */
  46. this->client_cookie = cookie;
  47. this->global = NULL;
  48. this->startup = NULL;
  49. this->idle_timeout = 0; /* No idle timeout by default */
  50. this->wdog = NULL;
  51. this->wdog_timeout = 0; /* No watchdog timeout by default */
  52. this->wdog_active = BOOL_FALSE;
  53. this->state = SHELL_STATE_INITIALISING;
  54. this->overview = NULL;
  55. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  56. this->current_file = NULL;
  57. this->pwdv = NULL;
  58. this->pwdc = 0;
  59. this->depth = -1; /* Current depth is undefined */
  60. this->client = NULL;
  61. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  62. this->default_shebang = lub_string_dup("/bin/sh");
  63. this->fifo_name = NULL;
  64. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  65. this->log = BOOL_FALSE; /* Disable logging by default */
  66. this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
  67. this->user = lub_db_getpwuid(getuid()); /* Get user information */
  68. this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
  69. /* Create internal ptypes and params */
  70. /* Current depth */
  71. tmp_ptype = clish_shell_find_create_ptype(this,
  72. "__DEPTH", "Depth", "[0-9]+",
  73. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  74. assert(tmp_ptype);
  75. this->param_depth = clish_param_new("_cur_depth",
  76. "Current depth", tmp_ptype);
  77. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  78. /* Current pwd */
  79. tmp_ptype = clish_shell_find_create_ptype(this,
  80. "__PWD", "Path", ".+",
  81. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  82. assert(tmp_ptype);
  83. this->param_pwd = clish_param_new("_cur_pwd",
  84. "Current path", tmp_ptype);
  85. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  86. /* Args */
  87. tmp_ptype = clish_shell_find_create_ptype(this,
  88. "internal_ARGS",
  89. "Arguments", "[^\\\\]+",
  90. CLISH_PTYPE_REGEXP,
  91. CLISH_PTYPE_NONE);
  92. assert(tmp_ptype);
  93. /* Push non-NULL istream */
  94. if (istream)
  95. clish_shell_push_fd(this, istream, stop_on_error);
  96. }
  97. /*--------------------------------------------------------- */
  98. static void clish_shell_fini(clish_shell_t *this)
  99. {
  100. clish_view_t *view;
  101. clish_ptype_t *ptype;
  102. clish_var_t *var;
  103. unsigned i;
  104. lub_list_node_t *iter;
  105. /* Free all loaded plugins */
  106. while ((iter = lub_list__get_head(this->plugins))) {
  107. /* Remove the symbol from the list */
  108. lub_list_del(this->plugins, iter);
  109. /* Free the instance */
  110. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter),
  111. (void *)this);
  112. lub_list_node_free(iter);
  113. }
  114. lub_list_free(this->plugins);
  115. /* delete each VIEW held */
  116. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  117. lub_bintree_remove(&this->view_tree, view);
  118. clish_view_delete(view);
  119. }
  120. /* delete each PTYPE held */
  121. while ((ptype = lub_bintree_findfirst(&this->ptype_tree))) {
  122. lub_bintree_remove(&this->ptype_tree, ptype);
  123. clish_ptype_delete(ptype);
  124. }
  125. /* delete each VAR held */
  126. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  127. lub_bintree_remove(&this->var_tree, var);
  128. clish_var_delete(var);
  129. }
  130. /* Free empty hooks */
  131. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  132. if (clish_sym__get_name(this->hooks[i]))
  133. continue;
  134. clish_sym_free(this->hooks[i]);
  135. }
  136. /* Free symbol list */
  137. while ((iter = lub_list__get_head(this->syms))) {
  138. /* Remove the symbol from the list */
  139. lub_list_del(this->syms, iter);
  140. /* Free the instance */
  141. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  142. lub_list_node_free(iter);
  143. }
  144. lub_list_free(this->syms);
  145. /* Free user data storage */
  146. while ((iter = lub_list__get_head(this->udata))) {
  147. /* Remove the symbol from the list */
  148. lub_list_del(this->udata, iter);
  149. /* Free the instance */
  150. clish_udata_free((clish_udata_t *)lub_list_node__get_data(iter));
  151. lub_list_node_free(iter);
  152. }
  153. lub_list_free(this->udata);
  154. /* free the textual details */
  155. lub_string_free(this->overview);
  156. /* Remove the startup command */
  157. if (this->startup)
  158. clish_command_delete(this->startup);
  159. /* Remove the watchdog command */
  160. if (this->wdog)
  161. clish_command_delete(this->wdog);
  162. /* clean up the file stack */
  163. while (!clish_shell_pop_file(this));
  164. /* delete the tinyrl object */
  165. clish_shell_tinyrl_delete(this->tinyrl);
  166. /* finalize each of the pwd strings */
  167. for (i = 0; i < this->pwdc; i++) {
  168. clish_shell__fini_pwd(this->pwdv[i]);
  169. free(this->pwdv[i]);
  170. }
  171. /* free the pwd vector */
  172. free(this->pwdv);
  173. konf_client_free(this->client);
  174. /* Free internal params */
  175. clish_param_delete(this->param_depth);
  176. clish_param_delete(this->param_pwd);
  177. lub_string_free(this->lockfile);
  178. lub_string_free(this->default_shebang);
  179. free(this->user);
  180. if (this->fifo_name) {
  181. unlink(this->fifo_name);
  182. lub_string_free(this->fifo_name);
  183. }
  184. }
  185. /*-------------------------------------------------------- */
  186. clish_shell_t *clish_shell_new(
  187. void *cookie,
  188. FILE * istream,
  189. FILE * ostream,
  190. bool_t stop_on_error)
  191. {
  192. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  193. if (this)
  194. clish_shell_init(this, cookie,
  195. istream, ostream, stop_on_error);
  196. return this;
  197. }
  198. /*--------------------------------------------------------- */
  199. void clish_shell_delete(clish_shell_t *this)
  200. {
  201. clish_context_t context;
  202. context.shell = this;
  203. /* Now call the client finalization */
  204. clish_shell_exec_fini(&context);
  205. clish_shell_fini(this);
  206. free(this);
  207. }
  208. /*--------------------------------------------------------- */
  209. struct passwd *clish_shell__get_user(clish_shell_t * this)
  210. {
  211. return this->user;
  212. }
  213. /*-------------------------------------------------------- */