shell_spawn.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /*-------------------------------------------------------- */
  87. static int _loop(clish_shell_t * this, bool_t is_thread)
  88. {
  89. int running = 0;
  90. int retval = SHELL_STATE_OK;
  91. assert(this);
  92. if (!tinyrl__get_istream(this->tinyrl))
  93. return SHELL_STATE_IO_ERROR;
  94. /* Check the shell isn't closing down */
  95. if (this && (SHELL_STATE_CLOSING == this->state))
  96. return retval;
  97. if (is_thread)
  98. pthread_testcancel();
  99. /* Loop reading and executing lines until the user quits */
  100. while (!running) {
  101. retval = SHELL_STATE_OK;
  102. /* Get input from the stream */
  103. running = clish_shell_readline(this, NULL);
  104. if (running) {
  105. switch (this->state) {
  106. case SHELL_STATE_SCRIPT_ERROR:
  107. case SHELL_STATE_SYNTAX_ERROR:
  108. /* Interactive session doesn't exit on error */
  109. if (tinyrl__get_isatty(this->tinyrl) ||
  110. !this->current_file->stop_on_error)
  111. running = 0;
  112. retval = this->state;
  113. default:
  114. break;
  115. }
  116. }
  117. if (SHELL_STATE_CLOSING == this->state)
  118. running = -1;
  119. if (running)
  120. running = clish_shell_pop_file(this);
  121. /* test for cancellation */
  122. if (is_thread)
  123. pthread_testcancel();
  124. }
  125. return retval;
  126. }
  127. /*-------------------------------------------------------- */
  128. /*
  129. * This is invoked when the thread ends or is cancelled.
  130. */
  131. static void clish_shell_thread_cleanup(clish_shell_t * this)
  132. {
  133. #ifdef __vxworks
  134. int last_state;
  135. /* we need to avoid recursion issues that exit in VxWorks */
  136. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  137. #endif /* __vxworks */
  138. /* Nothing to do now. The context will be free later. */
  139. #ifdef __vxworks
  140. pthread_setcancelstate(last_state, &last_state);
  141. #endif /* __vxworks */
  142. }
  143. /*-------------------------------------------------------- */
  144. /*
  145. * This provides the thread of execution for a shell instance
  146. */
  147. static void *clish_shell_thread(void *arg)
  148. {
  149. clish_shell_t *this = arg;
  150. int last_type;
  151. int *res = NULL;
  152. res = malloc(sizeof(*res));
  153. *res = 0;
  154. /* make sure we can only be cancelled at controlled points */
  155. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  156. /* register a cancellation handler */
  157. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  158. if (this)
  159. *res = _loop(this, BOOL_TRUE);
  160. /* be a good pthread citizen */
  161. pthread_cleanup_pop(1);
  162. return res;
  163. }
  164. /*-------------------------------------------------------- */
  165. int clish_shell_spawn(clish_shell_t *this,
  166. const pthread_attr_t *attr)
  167. {
  168. if (!this)
  169. return -1;
  170. return pthread_create(&this->pthread,
  171. attr, clish_shell_thread, this);
  172. }
  173. /*-------------------------------------------------------- */
  174. int clish_shell_wait(clish_shell_t *this)
  175. {
  176. void *res = NULL;
  177. int retval = 0;
  178. if (!this)
  179. return -1;
  180. (void)pthread_join(this->pthread, &res);
  181. if (res) {
  182. retval = *(int *)res;
  183. free(res);
  184. }
  185. return retval;
  186. }
  187. /*-------------------------------------------------------- */
  188. int clish_shell_spawn_and_wait(clish_shell_t *this,
  189. const pthread_attr_t *attr)
  190. {
  191. if (clish_shell_spawn(this, attr) < 0)
  192. return -1;
  193. return clish_shell_wait(this);
  194. }
  195. /*-------------------------------------------------------- */
  196. int clish_shell_loop(clish_shell_t *this)
  197. {
  198. return _loop(this, BOOL_FALSE);
  199. }
  200. /*-------------------------------------------------------- */