shell_new.c 5.6 KB

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