shell_spawn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * shell_new.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <unistd.h>
  11. #include <pthread.h>
  12. #include <dirent.h>
  13. /*
  14. * if CLISH_PATH is unset in the environment then this is the value used.
  15. */
  16. const char *default_path = "/etc/clish;~/.clish";
  17. /*-------------------------------------------------------- */
  18. /* perform a simple tilde substitution for the home directory
  19. * defined in HOME
  20. */
  21. static char *clish_shell_tilde_expand(const char *path)
  22. {
  23. char *home_dir = getenv("HOME");
  24. char *result = NULL;
  25. char *tilde;
  26. while ((tilde = strchr(path, '~'))) {
  27. lub_string_catn(&result, path, tilde - path);
  28. lub_string_cat(&result, home_dir);
  29. path = tilde + 1;
  30. }
  31. lub_string_cat(&result, path);
  32. return result;
  33. }
  34. /*-------------------------------------------------------- */
  35. void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
  36. {
  37. const char *path = xml_path;
  38. char *buffer;
  39. char *dirname;
  40. char *saveptr;
  41. /* use the default path */
  42. if (!path)
  43. path = default_path;
  44. /* take a copy of the path */
  45. buffer = clish_shell_tilde_expand(path);
  46. /* now loop though each directory */
  47. for (dirname = strtok_r(buffer, ";", &saveptr);
  48. dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
  49. DIR *dir;
  50. struct dirent *entry;
  51. /* search this directory for any XML files */
  52. dir = opendir(dirname);
  53. if (NULL == dir) {
  54. #ifdef DEBUG
  55. tinyrl_printf(this->tinyrl,
  56. "*** Failed to open '%s' directory\n",
  57. dirname);
  58. #endif
  59. continue;
  60. }
  61. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  62. const char *extension = strrchr(entry->d_name, '.');
  63. /* check the filename */
  64. if ((NULL != extension) &&
  65. (0 == strcmp(".xml", extension))) {
  66. char *filename = NULL;
  67. /* build the filename */
  68. lub_string_cat(&filename, dirname);
  69. lub_string_cat(&filename, "/");
  70. lub_string_cat(&filename, entry->d_name);
  71. /* load this file */
  72. (void)clish_shell_xml_read(this, filename);
  73. /* release the resource */
  74. lub_string_free(filename);
  75. }
  76. }
  77. /* all done for this directory */
  78. closedir(dir);
  79. }
  80. /* tidy up */
  81. lub_string_free(buffer);
  82. #ifdef DEBUG
  83. clish_shell_dump(this);
  84. #endif
  85. }
  86. enum {
  87. RETVAL_OK = 0, /* OK */
  88. RETVAL_UNKNOWN = 1, /* Unknown error */
  89. RETVAL_NOSTDIN = 2, /* No stdin found */
  90. RETVAL_SCRIPT = 3, /* Script execution error */
  91. RETVAL_SYNTAX = 4 /* Syntax error */
  92. };
  93. /*-------------------------------------------------------- */
  94. static int _loop(clish_shell_t * this, bool_t is_thread)
  95. {
  96. int running = 0;
  97. int retval = RETVAL_OK;
  98. assert(this);
  99. if (!tinyrl__get_istream(this->tinyrl))
  100. return RETVAL_NOSTDIN;
  101. /* Check the shell isn't closing down */
  102. if (this && (SHELL_STATE_CLOSING == this->state))
  103. return retval;
  104. if (is_thread)
  105. pthread_testcancel();
  106. /* Loop reading and executing lines until the user quits */
  107. while (!running) {
  108. retval = RETVAL_OK;
  109. /* Get input from the stream */
  110. running = clish_shell_readline(this, NULL);
  111. if (running) {
  112. switch (this->state) {
  113. case SHELL_STATE_SCRIPT_ERROR:
  114. case SHELL_STATE_SYNTAX_ERROR:
  115. /* Interactive session doesn't exit on error */
  116. if (tinyrl__get_isatty(this->tinyrl) ||
  117. !this->current_file->stop_on_error)
  118. running = 0;
  119. if (SHELL_STATE_SCRIPT_ERROR == this->state)
  120. retval = RETVAL_SCRIPT;
  121. else
  122. retval = RETVAL_SYNTAX;
  123. break;
  124. default:
  125. retval = RETVAL_UNKNOWN;
  126. break;
  127. }
  128. }
  129. if (SHELL_STATE_CLOSING == this->state)
  130. running = -1;
  131. if (running)
  132. running = clish_shell_pop_file(this);
  133. /* test for cancellation */
  134. if (is_thread)
  135. pthread_testcancel();
  136. }
  137. return retval;
  138. }
  139. /*-------------------------------------------------------- */
  140. /*
  141. * This is invoked when the thread ends or is cancelled.
  142. */
  143. static void clish_shell_thread_cleanup(clish_shell_t * this)
  144. {
  145. #ifdef __vxworks
  146. int last_state;
  147. /* we need to avoid recursion issues that exit in VxWorks */
  148. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  149. #endif /* __vxworks */
  150. /* Nothing to do now. The context will be free later. */
  151. #ifdef __vxworks
  152. pthread_setcancelstate(last_state, &last_state);
  153. #endif /* __vxworks */
  154. }
  155. /*-------------------------------------------------------- */
  156. /*
  157. * This provides the thread of execution for a shell instance
  158. */
  159. static void *clish_shell_thread(void *arg)
  160. {
  161. clish_shell_t *this = arg;
  162. int last_type;
  163. int *res = NULL;
  164. res = malloc(sizeof(*res));
  165. *res = 0;
  166. /* make sure we can only be cancelled at controlled points */
  167. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  168. /* register a cancellation handler */
  169. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  170. if (this)
  171. *res = _loop(this, BOOL_TRUE);
  172. /* be a good pthread citizen */
  173. pthread_cleanup_pop(1);
  174. return res;
  175. }
  176. /*-------------------------------------------------------- */
  177. int clish_shell_spawn(clish_shell_t *this,
  178. const pthread_attr_t *attr)
  179. {
  180. if (!this)
  181. return -1;
  182. return pthread_create(&this->pthread,
  183. attr, clish_shell_thread, this);
  184. }
  185. /*-------------------------------------------------------- */
  186. int clish_shell_wait(clish_shell_t *this)
  187. {
  188. void *res = NULL;
  189. int retval = 0;
  190. if (!this)
  191. return -1;
  192. (void)pthread_join(this->pthread, &res);
  193. if (res) {
  194. retval = *(int *)res;
  195. free(res);
  196. }
  197. return retval;
  198. }
  199. /*-------------------------------------------------------- */
  200. int clish_shell_spawn_and_wait(clish_shell_t *this,
  201. const pthread_attr_t *attr)
  202. {
  203. if (clish_shell_spawn(this, attr) < 0)
  204. return -1;
  205. return clish_shell_wait(this);
  206. }
  207. /*-------------------------------------------------------- */
  208. int clish_shell_loop(clish_shell_t *this)
  209. {
  210. return _loop(this, BOOL_FALSE);
  211. }
  212. /*-------------------------------------------------------- */