shell_execute.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * shell_execute.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/argv.h"
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <sys/stat.h>
  12. /*
  13. * These are the internal commands for this framework.
  14. */
  15. static clish_shell_builtin_fn_t
  16. clish_close,
  17. clish_overview,
  18. clish_source,
  19. clish_source_nostop,
  20. clish_history;
  21. static clish_shell_builtin_t clish_cmd_list[] = {
  22. {"clish_close", clish_close},
  23. {"clish_overview", clish_overview},
  24. {"clish_source", clish_source},
  25. {"clish_source_nostop", clish_source_nostop},
  26. {"clish_history", clish_history},
  27. {NULL, NULL}
  28. };
  29. /*----------------------------------------------------------- */
  30. /*
  31. Terminate the current shell session
  32. */
  33. static bool_t clish_close(const clish_shell_t * shell, const lub_argv_t * argv)
  34. {
  35. /* the exception proves the rule... */
  36. clish_shell_t *this = (clish_shell_t *) shell;
  37. argv = argv; /* not used */
  38. this->state = SHELL_STATE_CLOSING;
  39. return BOOL_TRUE;
  40. }
  41. /*----------------------------------------------------------- */
  42. /*
  43. Open a file and interpret it as a script in the context of a new
  44. thread. Whether the script continues after command, but not script,
  45. errors depends on the value of the stop_on_error flag.
  46. */
  47. static bool_t
  48. clish_source_internal(const clish_shell_t * shell,
  49. const lub_argv_t * argv, bool_t stop_on_error)
  50. {
  51. bool_t result = BOOL_FALSE;
  52. const char *filename = lub_argv__get_arg(argv, 0);
  53. struct stat fileStat;
  54. FILE *file;
  55. /* the exception proves the rule... */
  56. clish_shell_t *this = (clish_shell_t *) shell;
  57. /*
  58. * Check file specified is not a directory
  59. */
  60. if (0 == stat((char *)filename, &fileStat)) {
  61. if (!S_ISDIR(fileStat.st_mode)) {
  62. file = fopen(filename, "r");
  63. if (NULL != file) {
  64. /*
  65. * push this file onto the file stack associated with this
  66. * session. This will be closed by clish_shell_pop_file()
  67. * when it is finished with.
  68. */
  69. result =
  70. clish_shell_push_file((clish_shell_t *)
  71. this, file,
  72. stop_on_error);
  73. if (BOOL_FALSE == result) {
  74. /* close the file here */
  75. fclose(file);
  76. }
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. /*----------------------------------------------------------- */
  83. /*
  84. Open a file and interpret it as a script in the context of a new
  85. thread. Invoking a script in this way will cause the script to
  86. stop on the first error
  87. */
  88. static bool_t clish_source(const clish_shell_t * shell, const lub_argv_t * argv)
  89. {
  90. return (clish_source_internal(shell, argv, BOOL_TRUE));
  91. }
  92. /*----------------------------------------------------------- */
  93. /*
  94. Open a file and interpret it as a script in the context of a new
  95. thread. Invoking a script in this way will cause the script to
  96. continue after command, but not script, errors.
  97. */
  98. static bool_t
  99. clish_source_nostop(const clish_shell_t * shell, const lub_argv_t * argv)
  100. {
  101. return (clish_source_internal(shell, argv, BOOL_FALSE));
  102. }
  103. /*----------------------------------------------------------- */
  104. /*
  105. Show the shell overview
  106. */
  107. static bool_t
  108. clish_overview(const clish_shell_t * this, const lub_argv_t * argv)
  109. {
  110. argv = argv; /* not used */
  111. tinyrl_printf(this->tinyrl, "%s\n", this->overview);
  112. return BOOL_TRUE;
  113. }
  114. /*----------------------------------------------------------- */
  115. static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
  116. {
  117. tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
  118. tinyrl_history_iterator_t iter;
  119. const tinyrl_history_entry_t *entry;
  120. unsigned limit = 0;
  121. const char *arg = lub_argv__get_arg(argv, 0);
  122. if ((NULL != arg) && ('\0' != *arg)) {
  123. limit = (unsigned)atoi(arg);
  124. if (0 == limit) {
  125. /* unlimit the history list */
  126. (void)tinyrl_history_unstifle(history);
  127. } else {
  128. /* limit the scope of the history list */
  129. tinyrl_history_stifle(history, limit);
  130. }
  131. }
  132. for (entry = tinyrl_history_getfirst(history, &iter);
  133. entry; entry = tinyrl_history_getnext(&iter)) {
  134. /* dump the details of this entry */
  135. tinyrl_printf(this->tinyrl,
  136. "%5d %s\n",
  137. tinyrl_history_entry__get_index(entry),
  138. tinyrl_history_entry__get_line(entry));
  139. }
  140. return BOOL_TRUE;
  141. }
  142. /*----------------------------------------------------------- */
  143. /*
  144. * Searches for a builtin command to execute
  145. */
  146. static clish_shell_builtin_fn_t *find_builtin_callback(const
  147. clish_shell_builtin_t *
  148. cmd_list,
  149. const char *name)
  150. {
  151. const clish_shell_builtin_t *result;
  152. /* search a list of commands */
  153. for (result = cmd_list; result && result->name; result++) {
  154. if (0 == strcmp(name, result->name)) {
  155. break;
  156. }
  157. }
  158. return (result && result->name) ? result->callback : NULL;
  159. }
  160. /*----------------------------------------------------------- */
  161. void clish_shell_cleanup_script(void *script)
  162. {
  163. /* simply release the memory */
  164. lub_string_free(script);
  165. }
  166. /*----------------------------------------------------------- */
  167. bool_t
  168. clish_shell_execute(clish_shell_t * this,
  169. const clish_command_t * cmd, clish_pargv_t ** pargv)
  170. {
  171. bool_t result = BOOL_TRUE;
  172. const char *builtin;
  173. char *script;
  174. assert(NULL != cmd);
  175. builtin = clish_command__get_builtin(cmd);
  176. script = clish_command__get_action(cmd, this->viewid, *pargv);
  177. /* account for thread cancellation whilst running a script */
  178. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
  179. script);
  180. if (NULL != builtin) {
  181. clish_shell_builtin_fn_t *callback;
  182. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  183. result = BOOL_FALSE;
  184. /* search for an internal command */
  185. callback = find_builtin_callback(clish_cmd_list, builtin);
  186. if (NULL == callback) {
  187. /* search for a client command */
  188. callback =
  189. find_builtin_callback(this->client_hooks->cmd_list,
  190. builtin);
  191. }
  192. if (NULL != callback) {
  193. /* invoke the builtin callback */
  194. result = callback(this, argv);
  195. }
  196. if (NULL != argv) {
  197. lub_argv_delete(argv);
  198. }
  199. } else if (NULL != script) {
  200. /* now get the client to interpret the resulting script */
  201. result = this->client_hooks->script_fn(this, script);
  202. }
  203. pthread_cleanup_pop(1);
  204. if (BOOL_TRUE == result) {
  205. /* move into the new view */
  206. clish_view_t *view = clish_command__get_view(cmd);
  207. char *viewid =
  208. clish_command__get_viewid(cmd, this->viewid, *pargv);
  209. /* now get the client to config operations */
  210. if (this->client_hooks->config_fn)
  211. this->client_hooks->config_fn(this, cmd, *pargv);
  212. if (NULL != view) {
  213. /* Save the current config PWD */
  214. char *line = clish_variable__get_line(cmd, *pargv);
  215. clish_shell__set_pwd(this,
  216. clish_command__get_depth(cmd),
  217. line);
  218. lub_string_free(line);
  219. /* Change view */
  220. this->view = view;
  221. }
  222. if (viewid) {
  223. /* cleanup */
  224. lub_string_free(this->viewid);
  225. this->viewid = viewid;
  226. }
  227. }
  228. if (NULL != *pargv) {
  229. clish_pargv_delete(*pargv);
  230. *pargv = NULL;
  231. }
  232. return result;
  233. }
  234. /*----------------------------------------------------------- */