shell_spawn.c 5.2 KB

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