1
0

shell_execute.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. clish_nested_up;
  22. static clish_shell_builtin_t clish_cmd_list[] = {
  23. {"clish_close", clish_close},
  24. {"clish_overview", clish_overview},
  25. {"clish_source", clish_source},
  26. {"clish_source_nostop", clish_source_nostop},
  27. {"clish_history", clish_history},
  28. {"clish_nested_up", clish_nested_up},
  29. {NULL, NULL}
  30. };
  31. /*----------------------------------------------------------- */
  32. /*
  33. Terminate the current shell session
  34. */
  35. static bool_t clish_close(const clish_shell_t * shell, const lub_argv_t * argv)
  36. {
  37. /* the exception proves the rule... */
  38. clish_shell_t *this = (clish_shell_t *) shell;
  39. argv = argv; /* not used */
  40. this->state = SHELL_STATE_CLOSING;
  41. return BOOL_TRUE;
  42. }
  43. /*----------------------------------------------------------- */
  44. /*
  45. Open a file and interpret it as a script in the context of a new
  46. thread. Whether the script continues after command, but not script,
  47. errors depends on the value of the stop_on_error flag.
  48. */
  49. static bool_t
  50. clish_source_internal(const clish_shell_t * shell,
  51. const lub_argv_t * argv, bool_t stop_on_error)
  52. {
  53. bool_t result = BOOL_FALSE;
  54. const char *filename = lub_argv__get_arg(argv, 0);
  55. struct stat fileStat;
  56. FILE *file;
  57. /* the exception proves the rule... */
  58. clish_shell_t *this = (clish_shell_t *) shell;
  59. /*
  60. * Check file specified is not a directory
  61. */
  62. if (0 == stat((char *)filename, &fileStat)) {
  63. if (!S_ISDIR(fileStat.st_mode)) {
  64. file = fopen(filename, "r");
  65. if (NULL != file) {
  66. /*
  67. * push this file onto the file stack associated with this
  68. * session. This will be closed by clish_shell_pop_file()
  69. * when it is finished with.
  70. */
  71. result =
  72. clish_shell_push_file((clish_shell_t *)
  73. this, file,
  74. stop_on_error);
  75. if (BOOL_FALSE == result) {
  76. /* close the file here */
  77. fclose(file);
  78. }
  79. }
  80. }
  81. }
  82. return result;
  83. }
  84. /*----------------------------------------------------------- */
  85. /*
  86. Open a file and interpret it as a script in the context of a new
  87. thread. Invoking a script in this way will cause the script to
  88. stop on the first error
  89. */
  90. static bool_t clish_source(const clish_shell_t * shell, const lub_argv_t * argv)
  91. {
  92. return (clish_source_internal(shell, argv, BOOL_TRUE));
  93. }
  94. /*----------------------------------------------------------- */
  95. /*
  96. Open a file and interpret it as a script in the context of a new
  97. thread. Invoking a script in this way will cause the script to
  98. continue after command, but not script, errors.
  99. */
  100. static bool_t
  101. clish_source_nostop(const clish_shell_t * shell, const lub_argv_t * argv)
  102. {
  103. return (clish_source_internal(shell, argv, BOOL_FALSE));
  104. }
  105. /*----------------------------------------------------------- */
  106. /*
  107. Show the shell overview
  108. */
  109. static bool_t
  110. clish_overview(const clish_shell_t * this, const lub_argv_t * argv)
  111. {
  112. argv = argv; /* not used */
  113. tinyrl_printf(this->tinyrl, "%s\n", this->overview);
  114. return BOOL_TRUE;
  115. }
  116. /*----------------------------------------------------------- */
  117. static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
  118. {
  119. tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
  120. tinyrl_history_iterator_t iter;
  121. const tinyrl_history_entry_t *entry;
  122. unsigned limit = 0;
  123. const char *arg = lub_argv__get_arg(argv, 0);
  124. if ((NULL != arg) && ('\0' != *arg)) {
  125. limit = (unsigned)atoi(arg);
  126. if (0 == limit) {
  127. /* unlimit the history list */
  128. (void)tinyrl_history_unstifle(history);
  129. } else {
  130. /* limit the scope of the history list */
  131. tinyrl_history_stifle(history, limit);
  132. }
  133. }
  134. for (entry = tinyrl_history_getfirst(history, &iter);
  135. entry; entry = tinyrl_history_getnext(&iter)) {
  136. /* dump the details of this entry */
  137. tinyrl_printf(this->tinyrl,
  138. "%5d %s\n",
  139. tinyrl_history_entry__get_index(entry),
  140. tinyrl_history_entry__get_line(entry));
  141. }
  142. return BOOL_TRUE;
  143. }
  144. /*----------------------------------------------------------- */
  145. /*
  146. * Searches for a builtin command to execute
  147. */
  148. static clish_shell_builtin_fn_t *find_builtin_callback(const
  149. clish_shell_builtin_t *
  150. cmd_list,
  151. const char *name)
  152. {
  153. const clish_shell_builtin_t *result;
  154. /* search a list of commands */
  155. for (result = cmd_list; result && result->name; result++) {
  156. if (0 == strcmp(name, result->name)) {
  157. break;
  158. }
  159. }
  160. return (result && result->name) ? result->callback : NULL;
  161. }
  162. /*----------------------------------------------------------- */
  163. void clish_shell_cleanup_script(void *script)
  164. {
  165. /* simply release the memory */
  166. lub_string_free(script);
  167. }
  168. /*----------------------------------------------------------- */
  169. bool_t
  170. clish_shell_execute(clish_shell_t * this,
  171. const clish_command_t * cmd, clish_pargv_t ** pargv)
  172. {
  173. bool_t result = BOOL_TRUE;
  174. const char *builtin;
  175. char *script;
  176. assert(NULL != cmd);
  177. /* Pre-change view if the command is from another depth/view */
  178. {
  179. clish_view_t *view = NULL;
  180. char *viewid = NULL;
  181. clish_view_restore_t restore = clish_command__get_restore(cmd);
  182. if ((CLISH_RESTORE_VIEW == restore) &&
  183. (clish_command__get_pview(cmd) != this->view))
  184. view = clish_command__get_pview(cmd);
  185. else if ((CLISH_RESTORE_DEPTH == restore) &&
  186. (clish_command__get_depth(cmd) <
  187. clish_view__get_depth(this->view))) {
  188. view = clish_shell__get_pwd_view(this,
  189. clish_command__get_depth(cmd));
  190. viewid = clish_shell__get_pwd_viewid(this,
  191. clish_command__get_depth(cmd));
  192. }
  193. if (NULL != view) {
  194. this->view = view;
  195. /* cleanup */
  196. lub_string_free(this->viewid);
  197. this->viewid = lub_string_dup(viewid);
  198. }
  199. }
  200. /* Execute action */
  201. builtin = clish_command__get_builtin(cmd);
  202. script = clish_command__get_action(cmd, this->viewid, *pargv);
  203. /* account for thread cancellation whilst running a script */
  204. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
  205. script);
  206. if (NULL != builtin) {
  207. clish_shell_builtin_fn_t *callback;
  208. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  209. result = BOOL_FALSE;
  210. /* search for an internal command */
  211. callback = find_builtin_callback(clish_cmd_list, builtin);
  212. if (NULL == callback) {
  213. /* search for a client command */
  214. callback =
  215. find_builtin_callback(this->client_hooks->cmd_list,
  216. builtin);
  217. }
  218. if (NULL != callback) {
  219. /* invoke the builtin callback */
  220. result = callback(this, argv);
  221. }
  222. if (NULL != argv) {
  223. lub_argv_delete(argv);
  224. }
  225. } else if (NULL != script) {
  226. /* now get the client to interpret the resulting script */
  227. result = this->client_hooks->script_fn(this, script);
  228. }
  229. pthread_cleanup_pop(1);
  230. if (BOOL_TRUE == result) {
  231. clish_view_t *view = NULL;
  232. char *viewid = NULL;
  233. /* Now get the client to config operations */
  234. if (this->client_hooks->config_fn)
  235. this->client_hooks->config_fn(this, cmd, *pargv);
  236. /* Move into the new view */
  237. view = clish_command__get_view(cmd);
  238. viewid = clish_command__get_viewid(cmd, this->viewid, *pargv);
  239. if (NULL != view) {
  240. /* Save the current config PWD */
  241. char *line = clish_variable__get_line(cmd, *pargv);
  242. clish_shell__set_pwd(this,
  243. clish_command__get_depth(cmd),
  244. line, this->view, this->viewid);
  245. lub_string_free(line);
  246. /* Change view */
  247. this->view = view;
  248. }
  249. if (viewid || view) {
  250. /* cleanup */
  251. lub_string_free(this->viewid);
  252. this->viewid = viewid;
  253. }
  254. }
  255. if (NULL != *pargv) {
  256. clish_pargv_delete(*pargv);
  257. *pargv = NULL;
  258. }
  259. return result;
  260. }
  261. /*----------------------------------------------------------- */
  262. /*
  263. Find out the previous view in the stack and go to it
  264. */
  265. static bool_t clish_nested_up(const clish_shell_t * shell, const lub_argv_t * argv)
  266. {
  267. clish_shell_t *this = (clish_shell_t *) shell;
  268. clish_view_t *view = NULL;
  269. char *viewid = NULL;
  270. int depth = 0;
  271. if (!shell)
  272. return BOOL_FALSE;
  273. argv = argv; /* not used */
  274. depth = clish_view__get_depth(this->view);
  275. /* If depth=0 than exit */
  276. if (0 == depth) {
  277. this->state = SHELL_STATE_CLOSING;
  278. return BOOL_TRUE;
  279. }
  280. depth--;
  281. view = clish_shell__get_pwd_view(this, depth);
  282. viewid = clish_shell__get_pwd_viewid(this, depth);
  283. if (!view)
  284. return BOOL_FALSE;
  285. this->view = view;
  286. lub_string_free(this->viewid);
  287. this->viewid = viewid ? lub_string_dup(viewid) : NULL;
  288. return BOOL_TRUE;
  289. }
  290. /*----------------------------------------------------------- */