shell_new.c 6.4 KB

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