shell_execute.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /* Pre-change view if the command is from another depth/view */
  176. {
  177. clish_view_t *view = NULL;
  178. char *viewid = NULL;
  179. clish_view_restore_t restore = clish_command__get_restore(cmd);
  180. if ((CLISH_RESTORE_VIEW == restore) &&
  181. (clish_command__get_pview(cmd) != this->view))
  182. view = clish_command__get_pview(cmd);
  183. else if ((CLISH_RESTORE_DEPTH == restore) &&
  184. (clish_command__get_depth(cmd) <
  185. clish_view__get_depth(this->view))) {
  186. view = clish_shell__get_pwd_view(this,
  187. clish_command__get_depth(cmd));
  188. viewid = clish_shell__get_pwd_viewid(this,
  189. clish_command__get_depth(cmd));
  190. }
  191. if (NULL != view) {
  192. this->view = view;
  193. /* cleanup */
  194. lub_string_free(this->viewid);
  195. this->viewid = lub_string_dup(viewid);
  196. }
  197. }
  198. /* Execute action */
  199. builtin = clish_command__get_builtin(cmd);
  200. script = clish_command__get_action(cmd, this->viewid, *pargv);
  201. /* account for thread cancellation whilst running a script */
  202. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
  203. script);
  204. if (NULL != builtin) {
  205. clish_shell_builtin_fn_t *callback;
  206. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  207. result = BOOL_FALSE;
  208. /* search for an internal command */
  209. callback = find_builtin_callback(clish_cmd_list, builtin);
  210. if (NULL == callback) {
  211. /* search for a client command */
  212. callback =
  213. find_builtin_callback(this->client_hooks->cmd_list,
  214. builtin);
  215. }
  216. if (NULL != callback) {
  217. /* invoke the builtin callback */
  218. result = callback(this, argv);
  219. }
  220. if (NULL != argv) {
  221. lub_argv_delete(argv);
  222. }
  223. } else if (NULL != script) {
  224. /* now get the client to interpret the resulting script */
  225. result = this->client_hooks->script_fn(this, script);
  226. }
  227. pthread_cleanup_pop(1);
  228. if (BOOL_TRUE == result) {
  229. clish_view_t *view = NULL;
  230. char *viewid = NULL;
  231. /* Now get the client to config operations */
  232. if (this->client_hooks->config_fn)
  233. this->client_hooks->config_fn(this, cmd, *pargv);
  234. /* Move into the new view */
  235. view = clish_command__get_view(cmd);
  236. viewid = clish_command__get_viewid(cmd, this->viewid, *pargv);
  237. if (NULL != view) {
  238. /* Save the current config PWD */
  239. char *line = clish_variable__get_line(cmd, *pargv);
  240. clish_shell__set_pwd(this,
  241. clish_command__get_depth(cmd),
  242. line, this->view, this->viewid);
  243. lub_string_free(line);
  244. /* Change view */
  245. this->view = view;
  246. }
  247. if (viewid || view) {
  248. /* cleanup */
  249. lub_string_free(this->viewid);
  250. this->viewid = viewid;
  251. }
  252. }
  253. if (NULL != *pargv) {
  254. clish_pargv_delete(*pargv);
  255. *pargv = NULL;
  256. }
  257. return result;
  258. }
  259. /*----------------------------------------------------------- */