1
0

shell_spawn.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 &&
  111. !this->current_file->stop_on_error))
  112. running = 0;
  113. retval = this->state;
  114. default:
  115. break;
  116. }
  117. }
  118. if (SHELL_STATE_CLOSING == this->state)
  119. running = -1;
  120. if (running)
  121. running = clish_shell_pop_file(this);
  122. /* test for cancellation */
  123. if (is_thread)
  124. pthread_testcancel();
  125. }
  126. return retval;
  127. }
  128. /*-------------------------------------------------------- */
  129. /*
  130. * This is invoked when the thread ends or is cancelled.
  131. */
  132. static void clish_shell_thread_cleanup(clish_shell_t * this)
  133. {
  134. #ifdef __vxworks
  135. int last_state;
  136. /* we need to avoid recursion issues that exit in VxWorks */
  137. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  138. #endif /* __vxworks */
  139. /* Nothing to do now. The context will be free later. */
  140. #ifdef __vxworks
  141. pthread_setcancelstate(last_state, &last_state);
  142. #endif /* __vxworks */
  143. }
  144. /*-------------------------------------------------------- */
  145. /*
  146. * This provides the thread of execution for a shell instance
  147. */
  148. static void *clish_shell_thread(void *arg)
  149. {
  150. clish_shell_t *this = arg;
  151. int last_type;
  152. int *res = NULL;
  153. res = malloc(sizeof(*res));
  154. *res = 0;
  155. /* make sure we can only be cancelled at controlled points */
  156. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  157. /* register a cancellation handler */
  158. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  159. if (this)
  160. *res = _loop(this, BOOL_TRUE);
  161. /* be a good pthread citizen */
  162. pthread_cleanup_pop(1);
  163. return res;
  164. }
  165. /*-------------------------------------------------------- */
  166. int clish_shell_spawn(clish_shell_t *this,
  167. const pthread_attr_t *attr)
  168. {
  169. if (!this)
  170. return -1;
  171. return pthread_create(&this->pthread,
  172. attr, clish_shell_thread, this);
  173. }
  174. /*-------------------------------------------------------- */
  175. int clish_shell_wait(clish_shell_t *this)
  176. {
  177. void *res = NULL;
  178. int retval = 0;
  179. if (!this)
  180. return -1;
  181. (void)pthread_join(this->pthread, &res);
  182. if (res) {
  183. retval = *(int *)res;
  184. free(res);
  185. }
  186. return retval;
  187. }
  188. /*-------------------------------------------------------- */
  189. int clish_shell_spawn_and_wait(clish_shell_t *this,
  190. const pthread_attr_t *attr)
  191. {
  192. if (clish_shell_spawn(this, attr) < 0)
  193. return -1;
  194. return clish_shell_wait(this);
  195. }
  196. /*-------------------------------------------------------- */
  197. int clish_shell_loop(clish_shell_t *this)
  198. {
  199. return _loop(this, BOOL_FALSE);
  200. }
  201. /*-------------------------------------------------------- */