1
0

shell_spawn.c 9.3 KB

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