shell_new.c 4.9 KB

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