shell_new.c 5.3 KB

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