shell_new.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <limits.h>
  11. #include "lub/string.h"
  12. #include "lub/db.h"
  13. #include "lub/list.h"
  14. #include "clish/plugin.h"
  15. /*-------------------------------------------------------- */
  16. static void clish_shell_init(clish_shell_t * this,
  17. FILE * istream, FILE * ostream, bool_t stop_on_error)
  18. {
  19. clish_ptype_t *tmp_ptype = NULL;
  20. int i;
  21. char template[PATH_MAX];
  22. /* Initialise VIEW list */
  23. this->view_tree = lub_list_new(clish_view_compare, clish_view_delete);
  24. /* Init PTYPE list */
  25. this->ptype_tree = lub_list_new(clish_ptype_compare, clish_ptype_free);
  26. /* initialise the tree of vars */
  27. lub_bintree_init(&this->var_tree,
  28. clish_var_bt_offset(),
  29. clish_var_bt_compare, clish_var_bt_getkey);
  30. /* Initialize plugin list */
  31. this->plugins = lub_list_new(NULL, clish_plugin_free);
  32. /* Initialise the list of unresolved (yet) symbols */
  33. this->syms = lub_list_new(clish_sym_compare, clish_sym_free);
  34. /* Create userdata storage */
  35. this->udata = lub_list_new(clish_udata_compare, clish_udata_delete);
  36. /* Hooks */
  37. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  38. this->hooks[i] = clish_sym_new(NULL, NULL, i);
  39. this->hooks_use[i] = BOOL_FALSE;
  40. }
  41. /* Set up defaults */
  42. this->global = NULL;
  43. this->startup = NULL;
  44. this->idle_timeout = 0; /* No idle timeout by default */
  45. this->wdog = NULL;
  46. this->wdog_timeout = 0; /* No watchdog timeout by default */
  47. this->wdog_active = BOOL_FALSE;
  48. this->state = SHELL_STATE_INITIALISING;
  49. this->overview = NULL;
  50. this->tinyrl = clish_shell_tinyrl_new(istream, ostream, 0);
  51. this->current_file = NULL;
  52. this->pwdv = NULL;
  53. this->pwdc = 0;
  54. this->depth = -1; /* Current depth is undefined */
  55. this->client = NULL;
  56. this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
  57. this->default_shebang = lub_string_dup("/bin/sh");
  58. this->interactive = BOOL_TRUE; /* The interactive shell by default. */
  59. this->log = BOOL_FALSE; /* Disable logging by default */
  60. this->log_facility = LOG_LOCAL0; /* LOCAL0 for compatibility */
  61. this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
  62. this->user = lub_db_getpwuid(getuid()); /* Get user information */
  63. this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
  64. this->canon_out = BOOL_FALSE; /* A canonical output is needed in special cases only */
  65. /* Create template (string) for FIFO name generation */
  66. snprintf(template, sizeof(template),
  67. "%s/klish.fifo.%u.XXXXXX", "/tmp", getpid());
  68. template[sizeof(template) - 1] = '\0';
  69. this->fifo_temp = lub_string_dup(template);
  70. /* Create internal ptypes and params */
  71. /* Args */
  72. tmp_ptype = clish_shell_find_create_ptype(this,
  73. "__ptype_ARGS",
  74. "Arguments", "[^\\\\]+",
  75. CLISH_PTYPE_METHOD_REGEXP,
  76. CLISH_PTYPE_PRE_NONE);
  77. assert(tmp_ptype);
  78. /* Push non-NULL istream */
  79. if (istream)
  80. clish_shell_push_fd(this, istream, stop_on_error);
  81. }
  82. /*--------------------------------------------------------- */
  83. static void clish_shell_fini(clish_shell_t *this)
  84. {
  85. clish_var_t *var;
  86. unsigned int i;
  87. /* Free all loaded plugins */
  88. lub_list_free_all(this->plugins);
  89. /* Delete each VIEW */
  90. lub_list_free_all(this->view_tree);
  91. /* Delete each PTYPE */
  92. lub_list_free_all(this->ptype_tree);
  93. /* delete each VAR held */
  94. while ((var = lub_bintree_findfirst(&this->var_tree))) {
  95. lub_bintree_remove(&this->var_tree, var);
  96. clish_var_delete(var);
  97. }
  98. /* Free empty hooks */
  99. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  100. if (clish_sym__get_name(this->hooks[i]))
  101. continue;
  102. clish_sym_free(this->hooks[i]);
  103. }
  104. /* Free symbol list */
  105. lub_list_free_all(this->syms);
  106. /* Free user data storage */
  107. lub_list_free_all(this->udata);
  108. /* free the textual details */
  109. lub_string_free(this->overview);
  110. /* Remove the startup command */
  111. if (this->startup)
  112. clish_command_delete(this->startup);
  113. /* Remove the watchdog command */
  114. if (this->wdog)
  115. clish_command_delete(this->wdog);
  116. /* clean up the file stack */
  117. while (!clish_shell_pop_file(this));
  118. /* delete the tinyrl object */
  119. clish_shell_tinyrl_delete(this->tinyrl);
  120. /* finalize each of the pwd strings */
  121. for (i = 0; i < this->pwdc; i++) {
  122. clish_shell__fini_pwd(this->pwdv[i]);
  123. free(this->pwdv[i]);
  124. }
  125. /* free the pwd vector */
  126. free(this->pwdv);
  127. konf_client_free(this->client);
  128. lub_string_free(this->lockfile);
  129. lub_string_free(this->default_shebang);
  130. free(this->user);
  131. if (this->fifo_temp)
  132. lub_string_free(this->fifo_temp);
  133. }
  134. /*-------------------------------------------------------- */
  135. clish_shell_t *clish_shell_new(
  136. FILE * istream,
  137. FILE * ostream,
  138. bool_t stop_on_error)
  139. {
  140. clish_shell_t *this = malloc(sizeof(clish_shell_t));
  141. if (this)
  142. clish_shell_init(this, istream, ostream, stop_on_error);
  143. return this;
  144. }
  145. /*--------------------------------------------------------- */
  146. void clish_shell_delete(clish_shell_t *this)
  147. {
  148. clish_shell_fini(this);
  149. free(this);
  150. }
  151. CLISH_GET(shell, struct passwd *, user);