1
0

shell_execute.c 9.3 KB

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