1
0

shell_spawn.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /*
  19. * The context structure is used to simplify the cleanup of
  20. * a CLI session when a thread is cancelled.
  21. */
  22. typedef struct _context context_t;
  23. struct _context {
  24. pthread_t pthread;
  25. const clish_shell_hooks_t *hooks;
  26. void *cookie;
  27. FILE *istream;
  28. clish_shell_t *shell;
  29. clish_pargv_t *pargv;
  30. char *prompt;
  31. };
  32. /*-------------------------------------------------------- */
  33. /* perform a simple tilde substitution for the home directory
  34. * defined in HOME
  35. */
  36. static char *clish_shell_tilde_expand(const char *path)
  37. {
  38. char *home_dir = getenv("HOME");
  39. char *result = NULL;
  40. const char *p = path;
  41. const char *segment = path;
  42. int count = 0;
  43. while (*p) {
  44. if ('~' == *p) {
  45. if (count) {
  46. lub_string_catn(&result, segment, count);
  47. segment += (count + 1); /* skip the tilde in the path */
  48. count = -1;
  49. }
  50. lub_string_cat(&result, home_dir);
  51. }
  52. count++;
  53. p++;
  54. }
  55. if (count) {
  56. lub_string_catn(&result, segment, count);
  57. }
  58. return result;
  59. }
  60. /*-------------------------------------------------------- */
  61. void clish_shell_load_files(clish_shell_t * this)
  62. {
  63. const char *path = getenv("CLISH_PATH");
  64. char *buffer;
  65. char *dirname;
  66. if (NULL == path) {
  67. /* use the default path */
  68. path = default_path;
  69. }
  70. /* take a copy of the path */
  71. buffer = clish_shell_tilde_expand(path);
  72. /* now loop though each directory */
  73. for (dirname = strtok(buffer, ";");
  74. dirname; dirname = strtok(NULL, ";")) {
  75. DIR *dir;
  76. struct dirent *entry;
  77. /* search this directory for any XML files */
  78. dir = opendir(dirname);
  79. if (NULL == dir) {
  80. #ifdef DEBUG
  81. tinyrl_printf(this->tinyrl,
  82. "*** Failed to open '%s' directory\n",
  83. dirname);
  84. #endif
  85. continue;
  86. }
  87. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  88. const char *extension = strrchr(entry->d_name, '.');
  89. /* check the filename */
  90. if (NULL != extension) {
  91. if (0 == strcmp(".xml", extension)) {
  92. char *filename = NULL;
  93. /* build the filename */
  94. lub_string_cat(&filename, dirname);
  95. lub_string_cat(&filename, "/");
  96. lub_string_cat(&filename,
  97. entry->d_name);
  98. /* load this file */
  99. (void)clish_shell_xml_read(this,
  100. filename);
  101. /* release the resource */
  102. lub_string_free(filename);
  103. }
  104. }
  105. }
  106. /* all done for this directory */
  107. closedir(dir);
  108. }
  109. /* tidy up */
  110. lub_string_free(buffer);
  111. #ifdef DEBUG
  112. clish_shell_dump(this);
  113. #endif
  114. }
  115. /*-------------------------------------------------------- */
  116. /*
  117. * This is invoked when the thread ends or is cancelled.
  118. */
  119. static void clish_shell_cleanup(context_t * context)
  120. {
  121. #ifdef __vxworks
  122. int last_state;
  123. /* we need to avoid recursion issues that exit in VxWorks */
  124. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state);
  125. #endif /* __vxworks */
  126. if (context->shell) {
  127. /*
  128. * Time to kill off the instance and terminate the thread
  129. */
  130. clish_shell_delete(context->shell);
  131. context->shell = NULL;
  132. }
  133. if (context->pargv) {
  134. clish_pargv_delete(context->pargv);
  135. context->pargv = NULL;
  136. }
  137. if (context->prompt) {
  138. lub_string_free(context->prompt);
  139. context->prompt = NULL;
  140. }
  141. /* finished with this */
  142. free(context);
  143. #ifdef __vxworks
  144. pthread_setcancelstate(last_state, &last_state);
  145. #endif /* __vxworks */
  146. }
  147. /*-------------------------------------------------------- */
  148. /*
  149. * This provides the thread of execution for a shell instance
  150. */
  151. static void *clish_shell_thread(void *arg)
  152. {
  153. context_t *context = arg;
  154. bool_t running = BOOL_TRUE;
  155. clish_shell_t *this;
  156. int last_type;
  157. /* make sure we can only be cancelled at controlled points */
  158. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  159. /* register a cancellation handler */
  160. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup, context);
  161. /* create a shell object... */
  162. this = context->shell = clish_shell_new(context->hooks,
  163. context->cookie,
  164. context->istream);
  165. assert(this);
  166. /*
  167. * Check the shell isn't closing down
  168. */
  169. if (this && (SHELL_STATE_CLOSING != this->state)) {
  170. /*
  171. * load the XML files found in the current CLISH path
  172. */
  173. clish_shell_load_files(this);
  174. /* start off with the default inputs stream */
  175. (void)clish_shell_push_file(this,
  176. fdopen(fileno(context->istream),
  177. "r"), BOOL_TRUE);
  178. /* This is the starting point for a shell, if this fails then
  179. * we cannot start the shell...
  180. */
  181. running = clish_shell_startup(this);
  182. pthread_testcancel();
  183. /* Loop reading and executing lines until the user quits. */
  184. while (running) {
  185. const clish_command_t *cmd;
  186. const clish_view_t *view;
  187. if ((SHELL_STATE_SCRIPT_ERROR == this->state) &&
  188. (BOOL_TRUE == tinyrl__get_isatty(this->tinyrl))) {
  189. /* interactive session doesn't automatically exit on error */
  190. this->state = SHELL_STATE_READY;
  191. }
  192. /* only bother to read the next line if there hasn't been a script error */
  193. if (this->state != SHELL_STATE_SCRIPT_ERROR) {
  194. /* obtain the prompt */
  195. view = clish_shell__get_view(this);
  196. assert(view);
  197. context->prompt =
  198. clish_view__get_prompt(view,
  199. clish_shell__get_viewid
  200. (this));
  201. assert(context->prompt);
  202. /* get input from the user */
  203. running =
  204. clish_shell_readline(this, context->prompt,
  205. &cmd, &context->pargv);
  206. lub_string_free(context->prompt);
  207. context->prompt = NULL;
  208. if (running && cmd && context->pargv) {
  209. /* execute the provided command */
  210. if (BOOL_FALSE ==
  211. clish_shell_execute(this, cmd,
  212. &context->
  213. pargv)) {
  214. /* there was an error */
  215. tinyrl_ding(this->tinyrl);
  216. /*
  217. * what we do now depends on whether we are set up to
  218. * stop on error on not.
  219. */
  220. if ((BOOL_TRUE ==
  221. this->current_file->
  222. stop_on_error)
  223. && (BOOL_FALSE ==
  224. tinyrl__get_isatty
  225. (this->tinyrl))) {
  226. this->state =
  227. SHELL_STATE_SCRIPT_ERROR;
  228. }
  229. }
  230. }
  231. }
  232. if ((BOOL_FALSE == running) ||
  233. (this->state == SHELL_STATE_SCRIPT_ERROR)) {
  234. /* we've reached the end of a file (or a script error has occured)
  235. * unwind the file stack to see whether
  236. * we need to exit
  237. */
  238. running = clish_shell_pop_file(this);
  239. }
  240. /* test for cancellation */
  241. pthread_testcancel();
  242. }
  243. }
  244. /* be a good pthread citizen */
  245. pthread_cleanup_pop(1);
  246. return (void *)BOOL_TRUE;
  247. }
  248. /*-------------------------------------------------------- */
  249. static context_t *_clish_shell_spawn(const pthread_attr_t * attr,
  250. const clish_shell_hooks_t * hooks,
  251. void *cookie, FILE * istream)
  252. {
  253. int rtn;
  254. context_t *context = malloc(sizeof(context_t));
  255. assert(context);
  256. if (context) {
  257. context->hooks = hooks;
  258. context->cookie = cookie;
  259. context->istream = istream;
  260. context->shell = NULL;
  261. context->prompt = NULL;
  262. context->pargv = NULL;
  263. /* and set it free */
  264. rtn = pthread_create(&context->pthread,
  265. attr, clish_shell_thread, context);
  266. if (0 != rtn) {
  267. free(context);
  268. context = NULL;
  269. }
  270. }
  271. return context;
  272. }
  273. /*-------------------------------------------------------- */
  274. static int
  275. _clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks,
  276. void *cookie, FILE * file)
  277. {
  278. void *result = NULL;
  279. context_t *context = _clish_shell_spawn(NULL, hooks, cookie, file);
  280. if (context) {
  281. /* join the shell's thread and wait for it to exit */
  282. (void)pthread_join(context->pthread, &result);
  283. }
  284. return result ? BOOL_TRUE : BOOL_FALSE;
  285. }
  286. /*-------------------------------------------------------- */
  287. int clish_shell_spawn_and_wait(const clish_shell_hooks_t * hooks, void *cookie)
  288. {
  289. return _clish_shell_spawn_and_wait(hooks, cookie, stdin);
  290. }
  291. /*-------------------------------------------------------- */
  292. bool_t
  293. clish_shell_spawn(pthread_t * pthread,
  294. const pthread_attr_t * attr,
  295. const clish_shell_hooks_t * hooks, void *cookie)
  296. {
  297. context_t *context;
  298. bool_t result = BOOL_FALSE;
  299. /* spawn the thread... */
  300. context = _clish_shell_spawn(attr, hooks, cookie, stdin);
  301. if (NULL != context) {
  302. result = BOOL_TRUE;
  303. if (NULL != pthread) {
  304. *pthread = context->pthread;
  305. }
  306. }
  307. return result;
  308. }
  309. /*-------------------------------------------------------- */
  310. bool_t
  311. clish_shell_spawn_from_file(const clish_shell_hooks_t * hooks,
  312. void *cookie, const char *filename)
  313. {
  314. bool_t result = BOOL_FALSE;
  315. if (NULL != filename) {
  316. FILE *file = fopen(filename, "r");
  317. if (NULL != file) {
  318. /* spawn the thread and wait for it to exit */
  319. result =
  320. _clish_shell_spawn_and_wait(hooks, cookie,
  321. file) ? BOOL_TRUE :
  322. BOOL_FALSE;
  323. fclose(file);
  324. }
  325. }
  326. return result;
  327. }
  328. /*-------------------------------------------------------- */