1
0

shell_new.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* Args */
  70. tmp_ptype = clish_shell_find_create_ptype(this,
  71. "internal_ARGS",
  72. "Arguments", "[^\\\\]+",
  73. CLISH_PTYPE_REGEXP,
  74. CLISH_PTYPE_NONE);
  75. assert(tmp_ptype);
  76. /* Push non-NULL istream */
  77. if (istream)
  78. clish_shell_push_fd(this, istream, stop_on_error);
  79. }
  80. /*--------------------------------------------------------- */
  81. static void clish_shell_fini(clish_shell_t *this)
  82. {
  83. clish_view_t *view;
  84. clish_ptype_t *ptype;
  85. clish_var_t *var;
  86. unsigned i;
  87. lub_list_node_t *iter;
  88. /* Free all loaded plugins */
  89. while ((iter = lub_list__get_head(this->plugins))) {
  90. /* Remove the symbol from the list */
  91. lub_list_del(this->plugins, iter);
  92. /* Free the instance */
  93. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter),
  94. (void *)this);
  95. lub_list_node_free(iter);
  96. }
  97. lub_list_free(this->plugins);
  98. /* delete each VIEW held */
  99. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  100. lub_bintree_remove(&this->view_tree, view);
  101. clish_view_delete(view);
  102. }
  103. /* delete each PTYPE held */
  104. while ((ptype = lub_bintree_findfirst(&this->ptype_tree))) {
  105. lub_bintree_remove(&this->ptype_tree, ptype);
  106. clish_ptype_delete(ptype);
  107. }
  108. /* delete each VAR held */
  109. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  110. lub_bintree_remove(&this->var_tree, var);
  111. clish_var_delete(var);
  112. }
  113. /* Free empty hooks */
  114. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  115. if (clish_sym__get_name(this->hooks[i]))
  116. continue;
  117. clish_sym_free(this->hooks[i]);
  118. }
  119. /* Free symbol list */
  120. while ((iter = lub_list__get_head(this->syms))) {
  121. /* Remove the symbol from the list */
  122. lub_list_del(this->syms, iter);
  123. /* Free the instance */
  124. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  125. lub_list_node_free(iter);
  126. }
  127. lub_list_free(this->syms);
  128. /* Free user data storage */
  129. while ((iter = lub_list__get_head(this->udata))) {
  130. /* Remove the symbol from the list */
  131. lub_list_del(this->udata, iter);
  132. /* Free the instance */
  133. clish_udata_free((clish_udata_t *)lub_list_node__get_data(iter));
  134. lub_list_node_free(iter);
  135. }
  136. lub_list_free(this->udata);
  137. /* free the textual details */
  138. lub_string_free(this->overview);
  139. /* Remove the startup command */
  140. if (this->startup)
  141. clish_command_delete(this->startup);
  142. /* Remove the watchdog command */
  143. if (this->wdog)
  144. clish_command_delete(this->wdog);
  145. /* clean up the file stack */
  146. while (!clish_shell_pop_file(this));
  147. /* delete the tinyrl object */
  148. clish_shell_tinyrl_delete(this->tinyrl);
  149. /* finalize each of the pwd strings */
  150. for (i = 0; i < this->pwdc; i++) {
  151. clish_shell__fini_pwd(this->pwdv[i]);
  152. free(this->pwdv[i]);
  153. }
  154. /* free the pwd vector */
  155. free(this->pwdv);
  156. konf_client_free(this->client);
  157. lub_string_free(this->lockfile);
  158. lub_string_free(this->default_shebang);
  159. free(this->user);
  160. if (this->fifo_name) {
  161. unlink(this->fifo_name);
  162. lub_string_free(this->fifo_name);
  163. }
  164. }
  165. /*-------------------------------------------------------- */
  166. clish_shell_t *clish_shell_new(
  167. FILE * istream,
  168. FILE * ostream,
  169. bool_t stop_on_error)
  170. {
  171. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  172. if (this)
  173. clish_shell_init(this, istream, ostream, stop_on_error);
  174. return this;
  175. }
  176. /*--------------------------------------------------------- */
  177. void clish_shell_delete(clish_shell_t *this)
  178. {
  179. clish_shell_fini(this);
  180. free(this);
  181. }
  182. /*--------------------------------------------------------- */
  183. struct passwd *clish_shell__get_user(clish_shell_t * this)
  184. {
  185. return this->user;
  186. }
  187. /*-------------------------------------------------------- */