1
0

shell_new.c 6.4 KB

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