1
0

shell_execute.c 7.5 KB

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