shell_spawn.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* Check the shell isn't closing down */
  109. if (this && (SHELL_STATE_CLOSING == this->state))
  110. return BOOL_TRUE;
  111. if (is_thread)
  112. pthread_testcancel();
  113. /* Loop reading and executing lines until the user quits. */
  114. while (running) {
  115. /* Get input from the stream */
  116. running = clish_shell_readline(this, NULL);
  117. if ((SHELL_STATE_SCRIPT_ERROR == this->state) &&
  118. (BOOL_TRUE == tinyrl__get_isatty(this->tinyrl))) {
  119. /* interactive session doesn't automatically exit on error */
  120. this->state = SHELL_STATE_READY;
  121. }
  122. if ((BOOL_FALSE == running) ||
  123. (this->state == SHELL_STATE_SCRIPT_ERROR)) {
  124. /* we've reached the end of a file (or a script error has occured)
  125. * unwind the file stack to see whether
  126. * we need to exit
  127. */
  128. running = clish_shell_pop_file(this);
  129. }
  130. /* test for cancellation */
  131. if (is_thread)
  132. pthread_testcancel();
  133. }
  134. return BOOL_TRUE;
  135. }
  136. /*-------------------------------------------------------- */
  137. /*
  138. * This is invoked when the thread ends or is cancelled.
  139. */
  140. static void clish_shell_thread_cleanup(clish_shell_t * this)
  141. {
  142. #ifdef __vxworks
  143. int last_state;
  144. /* we need to avoid recursion issues that exit in VxWorks */
  145. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  146. #endif /* __vxworks */
  147. /* Nothing to do now. The context will be free later. */
  148. #ifdef __vxworks
  149. pthread_setcancelstate(last_state, &last_state);
  150. #endif /* __vxworks */
  151. }
  152. /*-------------------------------------------------------- */
  153. /*
  154. * This provides the thread of execution for a shell instance
  155. */
  156. static void *clish_shell_thread(void *arg)
  157. {
  158. clish_shell_t *this = arg;
  159. int last_type;
  160. /* make sure we can only be cancelled at controlled points */
  161. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  162. /* register a cancellation handler */
  163. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  164. if (this)
  165. _loop(this, BOOL_TRUE);
  166. /* be a good pthread citizen */
  167. pthread_cleanup_pop(1);
  168. return (void *)BOOL_TRUE;
  169. }
  170. /*-------------------------------------------------------- */
  171. int clish_shell_spawn(clish_shell_t * this,
  172. const pthread_attr_t * attr)
  173. {
  174. if (!this)
  175. return -1;
  176. return pthread_create(&this->pthread,
  177. attr, clish_shell_thread, this);
  178. }
  179. /*-------------------------------------------------------- */
  180. int clish_shell_wait(clish_shell_t * this)
  181. {
  182. void *result = NULL;
  183. if (!this)
  184. return BOOL_FALSE;
  185. (void)pthread_join(this->pthread, &result);
  186. return result ? BOOL_TRUE : BOOL_FALSE;
  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. bool_t clish_shell_loop(clish_shell_t * this)
  198. {
  199. return _loop(this, BOOL_FALSE);
  200. }
  201. /*-------------------------------------------------------- */