shell_spawn.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  102. * This is invoked when the thread ends or is cancelled.
  103. */
  104. static void clish_shell_thread_cleanup(clish_shell_t * this)
  105. {
  106. #ifdef __vxworks
  107. int last_state;
  108. /* we need to avoid recursion issues that exit in VxWorks */
  109. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  110. #endif /* __vxworks */
  111. /* Nothing to do now. The context will be free later. */
  112. #ifdef __vxworks
  113. pthread_setcancelstate(last_state, &last_state);
  114. #endif /* __vxworks */
  115. }
  116. /*-------------------------------------------------------- */
  117. /*
  118. * This provides the thread of execution for a shell instance
  119. */
  120. static void *clish_shell_thread(void *arg)
  121. {
  122. bool_t running = BOOL_TRUE;
  123. clish_shell_t *this = arg;
  124. int last_type;
  125. /* make sure we can only be cancelled at controlled points */
  126. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  127. /* register a cancellation handler */
  128. pthread_cleanup_push((void (*)(void *))clish_shell_thread_cleanup, this);
  129. /*
  130. * Check the shell isn't closing down
  131. */
  132. if (this && (SHELL_STATE_CLOSING != this->state)) {
  133. /* start off with the default inputs stream */
  134. (void)clish_shell_push_file(this,
  135. fdopen(fileno(tinyrl__get_istream(this->tinyrl)),"r"), BOOL_TRUE);
  136. pthread_testcancel();
  137. /* Loop reading and executing lines until the user quits. */
  138. while (running) {
  139. const clish_command_t *cmd;
  140. const clish_view_t *view;
  141. if ((SHELL_STATE_SCRIPT_ERROR == this->state) &&
  142. (BOOL_TRUE == tinyrl__get_isatty(this->tinyrl))) {
  143. /* interactive session doesn't automatically exit on error */
  144. this->state = SHELL_STATE_READY;
  145. }
  146. /* only bother to read the next line if there hasn't been a script error */
  147. if (this->state != SHELL_STATE_SCRIPT_ERROR) {
  148. /* get input from the user */
  149. running = clish_shell_readline(this);
  150. /*
  151. * what we do now depends on whether we are set up to
  152. * stop on error on not.
  153. */
  154. if (!running && (BOOL_TRUE ==
  155. this->current_file->stop_on_error) &&
  156. (BOOL_FALSE ==
  157. tinyrl__get_isatty(this->tinyrl)))
  158. this->state = SHELL_STATE_SCRIPT_ERROR;
  159. }
  160. if ((BOOL_FALSE == running) ||
  161. (this->state == SHELL_STATE_SCRIPT_ERROR)) {
  162. /* we've reached the end of a file (or a script error has occured)
  163. * unwind the file stack to see whether
  164. * we need to exit
  165. */
  166. running = clish_shell_pop_file(this);
  167. }
  168. /* test for cancellation */
  169. pthread_testcancel();
  170. }
  171. }
  172. /* be a good pthread citizen */
  173. pthread_cleanup_pop(1);
  174. return (void *)BOOL_TRUE;
  175. }
  176. /*-------------------------------------------------------- */
  177. int clish_shell_spawn(clish_shell_t * this,
  178. const pthread_attr_t * attr)
  179. {
  180. if (!this)
  181. return -1;
  182. return pthread_create(&this->pthread,
  183. attr, clish_shell_thread, this);
  184. }
  185. /*-------------------------------------------------------- */
  186. int clish_shell_wait(clish_shell_t * this)
  187. {
  188. void *result = NULL;
  189. if (!this)
  190. return BOOL_FALSE;
  191. (void)pthread_join(this->pthread, &result);
  192. return result ? BOOL_TRUE : BOOL_FALSE;
  193. }
  194. /*-------------------------------------------------------- */
  195. int clish_shell_spawn_and_wait(clish_shell_t * this,
  196. const pthread_attr_t * attr)
  197. {
  198. if (clish_shell_spawn(this, attr) < 0)
  199. return -1;
  200. return clish_shell_wait(this);
  201. }
  202. /*-------------------------------------------------------- */
  203. bool_t clish_shell_spawn_from_file(clish_shell_t * this,
  204. const pthread_attr_t * attr, const char *filename)
  205. {
  206. bool_t result = BOOL_FALSE;
  207. FILE *file;
  208. if (!this || !filename)
  209. return result;
  210. file = fopen(filename, "r");
  211. if (NULL == file)
  212. return result;
  213. tinyrl__set_istream(this->tinyrl, file);
  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. fclose(file);
  218. return result;
  219. }
  220. /*-------------------------------------------------------- */