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. const char *p = path;
  26. const char *segment = path;
  27. int count = 0;
  28. while (*p) {
  29. if ('~' == *p) {
  30. if (count) {
  31. lub_string_catn(&result, segment, count);
  32. segment += (count + 1); /* skip the tilde in the path */
  33. count = -1;
  34. }
  35. lub_string_cat(&result, home_dir);
  36. }
  37. count++;
  38. p++;
  39. }
  40. if (count)
  41. lub_string_catn(&result, segment, count);
  42. return result;
  43. }
  44. /*-------------------------------------------------------- */
  45. void clish_shell_load_scheme(clish_shell_t * this, const char *xml_path)
  46. {
  47. const char *path = xml_path;
  48. char *buffer;
  49. char *dirname;
  50. char *saveptr;
  51. /* use the default path */
  52. if (NULL == path)
  53. path = default_path;
  54. /* take a copy of the path */
  55. buffer = clish_shell_tilde_expand(path);
  56. /* now loop though each directory */
  57. for (dirname = strtok_r(buffer, ";", &saveptr);
  58. dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
  59. DIR *dir;
  60. struct dirent *entry;
  61. /* search this directory for any XML files */
  62. dir = opendir(dirname);
  63. if (NULL == dir) {
  64. #ifdef DEBUG
  65. tinyrl_printf(this->tinyrl,
  66. "*** Failed to open '%s' directory\n",
  67. dirname);
  68. #endif
  69. continue;
  70. }
  71. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  72. const char *extension = strrchr(entry->d_name, '.');
  73. /* check the filename */
  74. if ((NULL != extension) &&
  75. (0 == strcmp(".xml", extension))) {
  76. char *filename = NULL;
  77. /* build the filename */
  78. lub_string_cat(&filename, dirname);
  79. lub_string_cat(&filename, "/");
  80. lub_string_cat(&filename, entry->d_name);
  81. /* load this file */
  82. (void)clish_shell_xml_read(this, filename);
  83. /* release the resource */
  84. lub_string_free(filename);
  85. }
  86. }
  87. /* all done for this directory */
  88. closedir(dir);
  89. }
  90. /* tidy up */
  91. lub_string_free(buffer);
  92. #ifdef DEBUG
  93. clish_shell_dump(this);
  94. #endif
  95. }
  96. /*-------------------------------------------------------- */
  97. static bool_t _loop(clish_shell_t * this, bool_t is_thread)
  98. {
  99. bool_t running = BOOL_TRUE;
  100. assert(this);
  101. if (!tinyrl__get_istream(this->tinyrl))
  102. return BOOL_FALSE;
  103. /* Check the shell isn't closing down */
  104. if (this && (SHELL_STATE_CLOSING == this->state))
  105. return BOOL_TRUE;
  106. if (is_thread)
  107. pthread_testcancel();
  108. /* Loop reading and executing lines until the user quits */
  109. while (running) {
  110. /* Get input from the stream */
  111. running = clish_shell_readline(this, NULL);
  112. if (!running) {
  113. switch (this->state) {
  114. case SHELL_STATE_SCRIPT_ERROR:
  115. case SHELL_STATE_SYNTAX_ERROR:
  116. /* Interactive session doesn't exit on error */
  117. if (tinyrl__get_isatty(this->tinyrl) ||
  118. !this->current_file->stop_on_error)
  119. running = BOOL_TRUE;
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. if (SHELL_STATE_CLOSING == this->state)
  126. running = BOOL_FALSE;
  127. if (!running)
  128. running = clish_shell_pop_file(this);
  129. /* test for cancellation */
  130. if (is_thread)
  131. pthread_testcancel();
  132. }
  133. return BOOL_TRUE;
  134. }
  135. /*-------------------------------------------------------- */
  136. /*
  137. * This is invoked when the thread ends or is cancelled.
  138. */
  139. static void clish_shell_thread_cleanup(clish_shell_t * this)
  140. {
  141. #ifdef __vxworks
  142. int last_state;
  143. /* we need to avoid recursion issues that exit in VxWorks */
  144. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  145. #endif /* __vxworks */
  146. /* Nothing to do now. The context will be free later. */
  147. #ifdef __vxworks
  148. pthread_setcancelstate(last_state, &last_state);
  149. #endif /* __vxworks */
  150. }
  151. /*-------------------------------------------------------- */
  152. /*
  153. * This provides the thread of execution for a shell instance
  154. */
  155. static void *clish_shell_thread(void *arg)
  156. {
  157. clish_shell_t *this = arg;
  158. int last_type;
  159. /* make sure we can only be cancelled at controlled points */
  160. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  161. /* register a cancellation handler */
  162. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  163. if (this)
  164. _loop(this, BOOL_TRUE);
  165. /* be a good pthread citizen */
  166. pthread_cleanup_pop(1);
  167. return (void *)BOOL_TRUE;
  168. }
  169. /*-------------------------------------------------------- */
  170. int clish_shell_spawn(clish_shell_t * this,
  171. const pthread_attr_t * attr)
  172. {
  173. if (!this)
  174. return -1;
  175. return pthread_create(&this->pthread,
  176. attr, clish_shell_thread, this);
  177. }
  178. /*-------------------------------------------------------- */
  179. int clish_shell_wait(clish_shell_t * this)
  180. {
  181. void *result = NULL;
  182. if (!this)
  183. return BOOL_FALSE;
  184. (void)pthread_join(this->pthread, &result);
  185. return result ? BOOL_TRUE : BOOL_FALSE;
  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. bool_t clish_shell_loop(clish_shell_t * this)
  197. {
  198. return _loop(this, BOOL_FALSE);
  199. }
  200. /*-------------------------------------------------------- */