shell_new.c 6.5 KB

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