shell_spawn.c 5.7 KB

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