shell_spawn.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_cleanup(clish_context_t * context)
  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. if (context->shell) {
  112. /*
  113. * Time to kill off the instance and terminate the thread
  114. */
  115. clish_shell_delete(context->shell);
  116. context->shell = NULL;
  117. }
  118. if (context->pargv) {
  119. clish_pargv_delete(context->pargv);
  120. context->pargv = NULL;
  121. }
  122. if (context->prompt) {
  123. lub_string_free(context->prompt);
  124. context->prompt = NULL;
  125. }
  126. /* finished with this */
  127. free(context);
  128. #ifdef __vxworks
  129. pthread_setcancelstate(last_state, &last_state);
  130. #endif /* __vxworks */
  131. }
  132. /*-------------------------------------------------------- */
  133. /*
  134. * This provides the thread of execution for a shell instance
  135. */
  136. static void *clish_shell_thread(void *arg)
  137. {
  138. clish_context_t *context = arg;
  139. bool_t running = BOOL_TRUE;
  140. clish_shell_t *this;
  141. int last_type;
  142. /* make sure we can only be cancelled at controlled points */
  143. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  144. /* register a cancellation handler */
  145. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup, context);
  146. /* create a shell object... */
  147. this = context->shell = clish_shell_new(context->hooks,
  148. context->cookie,
  149. context->istream);
  150. assert(this);
  151. /*
  152. * Check the shell isn't closing down
  153. */
  154. if (this && (SHELL_STATE_CLOSING != this->state)) {
  155. /*
  156. * load the XML files found in the current CLISH path
  157. */
  158. clish_shell_load_files(this);
  159. /* start off with the default inputs stream */
  160. (void)clish_shell_push_file(this,
  161. fdopen(fileno(context->istream),
  162. "r"), BOOL_TRUE);
  163. /* This is the starting point for a shell, if this fails then
  164. * we cannot start the shell...
  165. */
  166. running = clish_shell_startup(this);
  167. pthread_testcancel();
  168. /* Loop reading and executing lines until the user quits. */
  169. while (running) {
  170. const clish_command_t *cmd;
  171. const clish_view_t *view;
  172. if ((SHELL_STATE_SCRIPT_ERROR == this->state) &&
  173. (BOOL_TRUE == tinyrl__get_isatty(this->tinyrl))) {
  174. /* interactive session doesn't automatically exit on error */
  175. this->state = SHELL_STATE_READY;
  176. }
  177. /* only bother to read the next line if there hasn't been a script error */
  178. if (this->state != SHELL_STATE_SCRIPT_ERROR) {
  179. /* obtain the prompt */
  180. view = clish_shell__get_view(this);
  181. assert(view);
  182. context->prompt =
  183. clish_view__get_prompt(view,
  184. clish_shell__get_viewid
  185. (this));
  186. assert(context->prompt);
  187. /* get input from the user */
  188. running =
  189. clish_shell_readline(this, context->prompt,
  190. &cmd, &context->pargv);
  191. lub_string_free(context->prompt);
  192. context->prompt = NULL;
  193. if (running && cmd && context->pargv) {
  194. /* execute the provided command */
  195. if (BOOL_FALSE ==
  196. clish_shell_execute(this, cmd,
  197. &context->
  198. pargv)) {
  199. /* there was an error */
  200. tinyrl_ding(this->tinyrl);
  201. /*
  202. * what we do now depends on whether we are set up to
  203. * stop on error on not.
  204. */
  205. if ((BOOL_TRUE ==
  206. this->current_file->
  207. stop_on_error)
  208. && (BOOL_FALSE ==
  209. tinyrl__get_isatty
  210. (this->tinyrl))) {
  211. this->state =
  212. SHELL_STATE_SCRIPT_ERROR;
  213. }
  214. }
  215. }
  216. }
  217. if ((BOOL_FALSE == running) ||
  218. (this->state == SHELL_STATE_SCRIPT_ERROR)) {
  219. /* we've reached the end of a file (or a script error has occured)
  220. * unwind the file stack to see whether
  221. * we need to exit
  222. */
  223. running = clish_shell_pop_file(this);
  224. }
  225. /* test for cancellation */
  226. pthread_testcancel();
  227. }
  228. }
  229. /* be a good pthread citizen */
  230. pthread_cleanup_pop(1);
  231. return (void *)BOOL_TRUE;
  232. }
  233. /*-------------------------------------------------------- */
  234. static clish_context_t *_clish_shell_spawn(const pthread_attr_t * attr,
  235. const clish_shell_hooks_t * hooks,
  236. void *cookie, FILE * istream)
  237. {
  238. int rtn;
  239. clish_context_t *context = malloc(sizeof(clish_context_t));
  240. assert(context);
  241. if (context) {
  242. context->hooks = hooks;
  243. context->cookie = cookie;
  244. context->istream = istream;
  245. context->shell = NULL;
  246. context->prompt = NULL;
  247. context->pargv = NULL;
  248. /* and set it free */
  249. rtn = pthread_create(&context->pthread,
  250. attr, clish_shell_thread, context);
  251. if (0 != rtn) {
  252. free(context);
  253. context = NULL;
  254. }
  255. }
  256. return context;
  257. }
  258. /*-------------------------------------------------------- */
  259. static int
  260. _clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks,
  261. void *cookie, FILE * file)
  262. {
  263. void *result = NULL;
  264. clish_context_t *context = _clish_shell_spawn(NULL, hooks, cookie, file);
  265. if (context) {
  266. /* join the shell's thread and wait for it to exit */
  267. (void)pthread_join(context->pthread, &result);
  268. }
  269. return result ? BOOL_TRUE : BOOL_FALSE;
  270. }
  271. /*-------------------------------------------------------- */
  272. int clish_context_wait(const clish_context_t * this)
  273. {
  274. void *result = NULL;
  275. if (!this || !this->pthread)
  276. return BOOL_FALSE;
  277. /* join the shell's thread and wait for it to exit */
  278. (void)pthread_join(this->pthread, &result);
  279. return result ? BOOL_TRUE : BOOL_FALSE;
  280. }
  281. /*-------------------------------------------------------- */
  282. clish_context_t *clish_context_spawn(const pthread_attr_t * attr,
  283. const clish_shell_hooks_t * hooks,
  284. void *cookie, FILE * istream)
  285. {
  286. return _clish_shell_spawn(attr, hooks, cookie, istream);
  287. }
  288. /*-------------------------------------------------------- */
  289. clish_context_t *clish_context_spawn_fd(const pthread_attr_t * attr,
  290. const clish_shell_hooks_t * hooks,
  291. void *cookie, int fd)
  292. {
  293. FILE *istream;
  294. istream = fdopen(fd, "r");
  295. return _clish_shell_spawn(attr, hooks, cookie, istream);
  296. }
  297. /*-------------------------------------------------------- */
  298. int clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks, void *cookie)
  299. {
  300. return _clish_shell_spawn_and_wait(hooks, cookie, stdin);
  301. }
  302. /*-------------------------------------------------------- */
  303. bool_t
  304. clish_shell_spawn(pthread_t * pthread,
  305. const pthread_attr_t * attr,
  306. const clish_shell_hooks_t * hooks, void *cookie)
  307. {
  308. clish_context_t *context;
  309. bool_t result = BOOL_FALSE;
  310. /* spawn the thread... */
  311. context = _clish_shell_spawn(attr, hooks, cookie, stdin);
  312. if (NULL != context) {
  313. result = BOOL_TRUE;
  314. if (NULL != pthread) {
  315. *pthread = context->pthread;
  316. }
  317. }
  318. return result;
  319. }
  320. /*-------------------------------------------------------- */
  321. bool_t
  322. clish_shell_spawn_from_file(const clish_shell_hooks_t * hooks,
  323. void *cookie, const char *filename)
  324. {
  325. bool_t result = BOOL_FALSE;
  326. if (NULL != filename) {
  327. FILE *file = fopen(filename, "r");
  328. if (NULL != file) {
  329. /* spawn the thread and wait for it to exit */
  330. result =
  331. _clish_shell_spawn_and_wait(hooks, cookie,
  332. file) ? BOOL_TRUE :
  333. BOOL_FALSE;
  334. fclose(file);
  335. }
  336. }
  337. return result;
  338. }
  339. /*-------------------------------------------------------- */