shell_new.c 5.2 KB

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