shell_new.c 5.6 KB

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