shell_new.c 6.7 KB

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