shell_new.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. clish_plugin_t *plugin = NULL;
  22. /* initialise the tree of views */
  23. lub_bintree_init(&this->view_tree,
  24. clish_view_bt_offset(),
  25. clish_view_bt_compare, clish_view_bt_getkey);
  26. /* initialise the tree of ptypes */
  27. lub_bintree_init(&this->ptype_tree,
  28. clish_ptype_bt_offset(),
  29. clish_ptype_bt_compare, clish_ptype_bt_getkey);
  30. /* initialise the tree of vars */
  31. lub_bintree_init(&this->var_tree,
  32. clish_var_bt_offset(),
  33. clish_var_bt_compare, clish_var_bt_getkey);
  34. /* Initialize plugin list */
  35. this->plugins = lub_list_new(NULL);
  36. plugin = clish_plugin_new("clish", NULL);
  37. lub_list_add(this->plugins, plugin);
  38. /* Initialise the list of unresolved (yet) symbols */
  39. this->syms = lub_list_new(clish_sym_compare);
  40. assert((NULL != hooks) && (NULL != hooks->script_fn));
  41. /* set up defaults */
  42. this->client_hooks = hooks;
  43. this->client_cookie = cookie;
  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->user = lub_db_getpwuid(getuid()); /* Get user information */
  64. /* Create internal ptypes and params */
  65. /* Current depth */
  66. tmp_ptype = clish_shell_find_create_ptype(this,
  67. "__DEPTH", "Depth", "[0-9]+",
  68. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  69. assert(tmp_ptype);
  70. this->param_depth = clish_param_new("_cur_depth",
  71. "Current depth", tmp_ptype);
  72. clish_param__set_hidden(this->param_depth, BOOL_TRUE);
  73. /* Current pwd */
  74. tmp_ptype = clish_shell_find_create_ptype(this,
  75. "__PWD", "Path", ".+",
  76. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  77. assert(tmp_ptype);
  78. this->param_pwd = clish_param_new("_cur_pwd",
  79. "Current path", tmp_ptype);
  80. clish_param__set_hidden(this->param_pwd, BOOL_TRUE);
  81. /* Args */
  82. tmp_ptype = clish_shell_find_create_ptype(this,
  83. "internal_ARGS",
  84. "Arguments", "[^\\\\]+",
  85. CLISH_PTYPE_REGEXP,
  86. CLISH_PTYPE_NONE);
  87. assert(tmp_ptype);
  88. /* Push non-NULL istream */
  89. if (istream)
  90. clish_shell_push_fd(this, istream, stop_on_error);
  91. }
  92. /*--------------------------------------------------------- */
  93. static void clish_shell_fini(clish_shell_t * this)
  94. {
  95. clish_view_t *view;
  96. clish_ptype_t *ptype;
  97. clish_var_t *var;
  98. unsigned i;
  99. lub_list_node_t *iter;
  100. /* delete each VIEW held */
  101. while ((view = lub_bintree_findfirst(&this->view_tree))) {
  102. lub_bintree_remove(&this->view_tree, view);
  103. clish_view_delete(view);
  104. }
  105. /* delete each PTYPE held */
  106. while ((ptype = lub_bintree_findfirst(&this->ptype_tree))) {
  107. lub_bintree_remove(&this->ptype_tree, ptype);
  108. clish_ptype_delete(ptype);
  109. }
  110. /* delete each VAR held */
  111. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  112. lub_bintree_remove(&this->var_tree, var);
  113. clish_var_delete(var);
  114. }
  115. /* Free all loaded plugins */
  116. while ((iter = lub_list__get_head(this->plugins))) {
  117. /* Remove the symbol from the list */
  118. lub_list_del(this->plugins, iter);
  119. /* Free the instance */
  120. clish_plugin_free((clish_plugin_t *)lub_list_node__get_data(iter));
  121. lub_list_node_free(iter);
  122. }
  123. lub_list_free(this->plugins);
  124. /* Free symbol list */
  125. while ((iter = lub_list__get_head(this->syms))) {
  126. /* Remove the symbol from the list */
  127. lub_list_del(this->syms, iter);
  128. /* Free the instance */
  129. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  130. lub_list_node_free(iter);
  131. }
  132. lub_list_free(this->syms);
  133. /* free the textual details */
  134. lub_string_free(this->overview);
  135. /* Remove the startup command */
  136. if (this->startup)
  137. clish_command_delete(this->startup);
  138. /* Remove the watchdog command */
  139. if (this->wdog)
  140. clish_command_delete(this->wdog);
  141. /* clean up the file stack */
  142. while (!clish_shell_pop_file(this));
  143. /* delete the tinyrl object */
  144. clish_shell_tinyrl_delete(this->tinyrl);
  145. /* finalize each of the pwd strings */
  146. for (i = 0; i < this->pwdc; i++) {
  147. clish_shell__fini_pwd(this->pwdv[i]);
  148. free(this->pwdv[i]);
  149. }
  150. /* free the pwd vector */
  151. free(this->pwdv);
  152. konf_client_free(this->client);
  153. /* Free internal params */
  154. clish_param_delete(this->param_depth);
  155. clish_param_delete(this->param_pwd);
  156. lub_string_free(this->lockfile);
  157. lub_string_free(this->default_shebang);
  158. free(this->user);
  159. if (this->fifo_name) {
  160. unlink(this->fifo_name);
  161. lub_string_free(this->fifo_name);
  162. }
  163. }
  164. /*-------------------------------------------------------- */
  165. clish_shell_t *clish_shell_new(const clish_shell_hooks_t * hooks,
  166. void *cookie,
  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, hooks, cookie,
  174. istream, ostream, stop_on_error);
  175. if (hooks->init_fn) {
  176. /* now call the client initialisation */
  177. if (BOOL_TRUE != hooks->init_fn(this))
  178. this->state = SHELL_STATE_CLOSING;
  179. }
  180. }
  181. return this;
  182. }
  183. /*--------------------------------------------------------- */
  184. void clish_shell_delete(clish_shell_t * this)
  185. {
  186. /* now call the client finalisation */
  187. if (this->client_hooks->fini_fn)
  188. this->client_hooks->fini_fn(this);
  189. clish_shell_fini(this);
  190. free(this);
  191. }
  192. /*--------------------------------------------------------- */
  193. struct passwd *clish_shell__get_user(clish_shell_t * this)
  194. {
  195. return this->user;
  196. }
  197. /*-------------------------------------------------------- */