shell_spawn.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /* 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. clish_context_t *context = arg;
  123. bool_t running = BOOL_TRUE;
  124. clish_shell_t *this = context->shell;
  125. int last_type;
  126. /* make sure we can only be cancelled at controlled points */
  127. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
  128. /* register a cancellation handler */
  129. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup, context);
  130. /*
  131. * Check the shell isn't closing down
  132. */
  133. if (this && (SHELL_STATE_CLOSING != this->state)) {
  134. /* start off with the default inputs stream */
  135. (void)clish_shell_push_file(this,
  136. fdopen(fileno(tinyrl__get_istream(this->tinyrl)),"r"), BOOL_TRUE);
  137. pthread_testcancel();
  138. /* Loop reading and executing lines until the user quits. */
  139. while (running) {
  140. const clish_command_t *cmd;
  141. const clish_view_t *view;
  142. if ((SHELL_STATE_SCRIPT_ERROR == this->state) &&
  143. (BOOL_TRUE == tinyrl__get_isatty(this->tinyrl))) {
  144. /* interactive session doesn't automatically exit on error */
  145. this->state = SHELL_STATE_READY;
  146. }
  147. /* only bother to read the next line if there hasn't been a script error */
  148. if (this->state != SHELL_STATE_SCRIPT_ERROR) {
  149. /* get input from the user */
  150. running = clish_context_readline(context);
  151. /*
  152. * what we do now depends on whether we are set up to
  153. * stop on error on not.
  154. */
  155. if (!running && (BOOL_TRUE ==
  156. this->current_file->stop_on_error) &&
  157. (BOOL_FALSE ==
  158. tinyrl__get_isatty(this->tinyrl)))
  159. this->state = SHELL_STATE_SCRIPT_ERROR;
  160. }
  161. if ((BOOL_FALSE == running) ||
  162. (this->state == SHELL_STATE_SCRIPT_ERROR)) {
  163. /* we've reached the end of a file (or a script error has occured)
  164. * unwind the file stack to see whether
  165. * we need to exit
  166. */
  167. running = clish_shell_pop_file(this);
  168. }
  169. /* test for cancellation */
  170. pthread_testcancel();
  171. }
  172. }
  173. /* be a good pthread citizen */
  174. pthread_cleanup_pop(1);
  175. return (void *)BOOL_TRUE;
  176. }
  177. /*-------------------------------------------------------- */
  178. int clish_context_spawn(clish_context_t * context,
  179. const pthread_attr_t * attr)
  180. {
  181. if (!context)
  182. return -1;
  183. return pthread_create(&context->pthread,
  184. attr, clish_shell_thread, context);
  185. }
  186. /*-------------------------------------------------------- */
  187. int clish_context_wait(clish_context_t * context)
  188. {
  189. void *result = NULL;
  190. if (!context)
  191. return BOOL_FALSE;
  192. (void)pthread_join(context->pthread, &result);
  193. return result ? BOOL_TRUE : BOOL_FALSE;
  194. }
  195. /*-------------------------------------------------------- */
  196. int clish_context_spawn_and_wait(clish_context_t * context,
  197. const pthread_attr_t * attr)
  198. {
  199. if (clish_context_spawn(context, attr) < 0)
  200. return -1;
  201. return clish_context_wait(context);
  202. }
  203. /*-------------------------------------------------------- */
  204. bool_t clish_context_spawn_from_file(clish_context_t * context,
  205. const pthread_attr_t * attr, const char *filename)
  206. {
  207. bool_t result = BOOL_FALSE;
  208. FILE *file;
  209. clish_shell_t *this;
  210. if (!context || !filename)
  211. return result;
  212. this = context->shell;
  213. file = fopen(filename, "r");
  214. if (NULL == file)
  215. return result;
  216. tinyrl__set_istream(this->tinyrl, file);
  217. /* spawn the thread and wait for it to exit */
  218. result = clish_context_spawn_and_wait(context, attr) ?
  219. BOOL_TRUE : BOOL_FALSE;
  220. fclose(file);
  221. return result;
  222. }
  223. /*-------------------------------------------------------- */
  224. clish_context_t * clish_context_new(const clish_shell_hooks_t * hooks,
  225. void *cookie, FILE * istream, FILE * ostream)
  226. {
  227. bool_t running;
  228. clish_context_t *this = malloc(sizeof(clish_context_t));
  229. if (!this)
  230. return NULL;
  231. this->hooks = hooks;
  232. this->cookie = cookie;
  233. this->shell = NULL;
  234. this->prompt = NULL;
  235. this->pargv = NULL;
  236. /* Create a shell */
  237. this->shell = clish_shell_new(this->hooks, this->cookie,
  238. istream, ostream);
  239. /* Load the XML files */
  240. clish_shell_load_files(this->shell);
  241. /* Execute startup */
  242. running = clish_shell_startup(this->shell);
  243. if (!running) {
  244. clish_context_free(this);
  245. return NULL;
  246. }
  247. return this;
  248. }
  249. /*-------------------------------------------------------- */
  250. void clish_context_free(clish_context_t *this)
  251. {
  252. if (this->shell) {
  253. /* Clean shell */
  254. clish_shell_delete(this->shell);
  255. this->shell = NULL;
  256. }
  257. if (this->pargv) {
  258. clish_pargv_delete(this->pargv);
  259. this->pargv = NULL;
  260. }
  261. if (this->prompt) {
  262. lub_string_free(this->prompt);
  263. this->prompt = NULL;
  264. }
  265. free(this);
  266. }
  267. /*-------------------------------------------------------- */
  268. static bool_t _clish_context_line(clish_context_t *context, const char *line)
  269. {
  270. const clish_command_t *cmd;
  271. const clish_view_t *view;
  272. bool_t running = BOOL_TRUE;
  273. clish_shell_t *this;
  274. if (!context)
  275. return BOOL_FALSE;
  276. this = context->shell;
  277. /* obtain the prompt */
  278. view = clish_shell__get_view(this);
  279. assert(view);
  280. context->prompt = clish_view__get_prompt(view,
  281. clish_shell__get_viewid(this));
  282. assert(context->prompt);
  283. if (line) {
  284. /* push the specified line */
  285. running = clish_shell_forceline(this, context->prompt,
  286. &cmd, &context->pargv, line);
  287. } else {
  288. running = clish_shell_readline(this, context->prompt,
  289. &cmd, &context->pargv);
  290. }
  291. lub_string_free(context->prompt);
  292. context->prompt = NULL;
  293. if (running && cmd && context->pargv)
  294. /* execute the provided command */
  295. return clish_shell_execute(this, cmd, &context->pargv);
  296. return running;
  297. }
  298. /*-------------------------------------------------------- */
  299. bool_t clish_context_forceline(clish_context_t *context, const char *line)
  300. {
  301. return _clish_context_line(context, line);
  302. }
  303. /*-------------------------------------------------------- */
  304. bool_t clish_context_readline(clish_context_t *context)
  305. {
  306. return _clish_context_line(context, NULL);
  307. }
  308. /*-------------------------------------------------------- */