shell_new.c 6.0 KB

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