shell_spawn.c 6.7 KB

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