shell_execute.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #include <signal.h>
  16. /*
  17. * These are the internal commands for this framework.
  18. */
  19. static clish_shell_builtin_fn_t
  20. clish_close,
  21. clish_overview,
  22. clish_source,
  23. clish_source_nostop,
  24. clish_history,
  25. clish_nested_up,
  26. clish_nop;
  27. static clish_shell_builtin_t clish_cmd_list[] = {
  28. {"clish_close", clish_close},
  29. {"clish_overview", clish_overview},
  30. {"clish_source", clish_source},
  31. {"clish_source_nostop", clish_source_nostop},
  32. {"clish_history", clish_history},
  33. {"clish_nested_up", clish_nested_up},
  34. {"clish_nop", clish_nop},
  35. {NULL, NULL}
  36. };
  37. /*----------------------------------------------------------- */
  38. /* Terminate the current shell session */
  39. static bool_t clish_close(clish_context_t *context, const lub_argv_t * argv)
  40. {
  41. /* the exception proves the rule... */
  42. clish_shell_t *this = (clish_shell_t *)context->shell;
  43. argv = argv; /* not used */
  44. this->state = SHELL_STATE_CLOSING;
  45. return BOOL_TRUE;
  46. }
  47. /*----------------------------------------------------------- */
  48. /*
  49. Open a file and interpret it as a script in the context of a new
  50. thread. Whether the script continues after command, but not script,
  51. errors depends on the value of the stop_on_error flag.
  52. */
  53. static bool_t clish_source_internal(clish_context_t *context,
  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. /* the exception proves the rule... */
  60. clish_shell_t *this = (clish_shell_t *)context->shell;
  61. /*
  62. * Check file specified is not a directory
  63. */
  64. if ((0 == stat((char *)filename, &fileStat)) &&
  65. (!S_ISDIR(fileStat.st_mode))) {
  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 = clish_shell_push_file(this, filename,
  72. stop_on_error);
  73. }
  74. return result;
  75. }
  76. /*----------------------------------------------------------- */
  77. /*
  78. Open a file and interpret it as a script in the context of a new
  79. thread. Invoking a script in this way will cause the script to
  80. stop on the first error
  81. */
  82. static bool_t clish_source(clish_context_t *context, const lub_argv_t * argv)
  83. {
  84. return (clish_source_internal(context, argv, BOOL_TRUE));
  85. }
  86. /*----------------------------------------------------------- */
  87. /*
  88. Open a file and interpret it as a script in the context of a new
  89. thread. Invoking a script in this way will cause the script to
  90. continue after command, but not script, errors.
  91. */
  92. static bool_t clish_source_nostop(clish_context_t *context, const lub_argv_t * argv)
  93. {
  94. return (clish_source_internal(context, argv, BOOL_FALSE));
  95. }
  96. /*----------------------------------------------------------- */
  97. /*
  98. Show the shell overview
  99. */
  100. static bool_t
  101. clish_overview(clish_context_t *context, const lub_argv_t * argv)
  102. {
  103. clish_shell_t *this = context->shell;
  104. argv = argv; /* not used */
  105. tinyrl_printf(this->tinyrl, "%s\n", context->shell->overview);
  106. return BOOL_TRUE;
  107. }
  108. /*----------------------------------------------------------- */
  109. static bool_t clish_history(clish_context_t *context, const lub_argv_t * argv)
  110. {
  111. clish_shell_t *this = context->shell;
  112. tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
  113. tinyrl_history_iterator_t iter;
  114. const tinyrl_history_entry_t *entry;
  115. unsigned limit = 0;
  116. const char *arg = lub_argv__get_arg(argv, 0);
  117. if (arg && ('\0' != *arg)) {
  118. limit = (unsigned)atoi(arg);
  119. if (0 == limit) {
  120. /* unlimit the history list */
  121. (void)tinyrl_history_unstifle(history);
  122. } else {
  123. /* limit the scope of the history list */
  124. tinyrl_history_stifle(history, limit);
  125. }
  126. }
  127. for (entry = tinyrl_history_getfirst(history, &iter);
  128. entry; entry = tinyrl_history_getnext(&iter)) {
  129. /* dump the details of this entry */
  130. tinyrl_printf(this->tinyrl,
  131. "%5d %s\n",
  132. tinyrl_history_entry__get_index(entry),
  133. tinyrl_history_entry__get_line(entry));
  134. }
  135. return BOOL_TRUE;
  136. }
  137. /*----------------------------------------------------------- */
  138. /*
  139. * Searches for a builtin command to execute
  140. */
  141. static clish_shell_builtin_fn_t *find_builtin_callback(const
  142. clish_shell_builtin_t * cmd_list, const char *name)
  143. {
  144. const clish_shell_builtin_t *result;
  145. /* search a list of commands */
  146. for (result = cmd_list; result && result->name; result++) {
  147. if (0 == strcmp(name, result->name))
  148. break;
  149. }
  150. return (result && result->name) ? result->callback : NULL;
  151. }
  152. /*----------------------------------------------------------- */
  153. void clish_shell_cleanup_script(void *script)
  154. {
  155. /* simply release the memory */
  156. lub_string_free(script);
  157. }
  158. /*----------------------------------------------------------- */
  159. bool_t clish_shell_execute(clish_context_t *context, char **out)
  160. {
  161. clish_shell_t *this = context->shell;
  162. const clish_command_t *cmd = context->cmd;
  163. clish_action_t *action;
  164. bool_t result = BOOL_TRUE;
  165. char *lock_path = clish_shell__get_lockfile(this);
  166. int lock_fd = -1;
  167. sigset_t old_sigs;
  168. struct sigaction old_sigint, old_sigquit;
  169. clish_view_t *cur_view = clish_shell__get_view(this);
  170. assert(cmd);
  171. action = clish_command__get_action(cmd);
  172. /* Pre-change view if the command is from another depth/view */
  173. {
  174. clish_view_restore_t restore = clish_command__get_restore(cmd);
  175. if ((CLISH_RESTORE_VIEW == restore) &&
  176. (clish_command__get_pview(cmd) != cur_view)) {
  177. clish_view_t *view = clish_command__get_pview(cmd);
  178. clish_shell__set_pwd(this, NULL, view, NULL, context);
  179. } else if ((CLISH_RESTORE_DEPTH == restore) &&
  180. (clish_command__get_depth(cmd) < this->depth)) {
  181. this->depth = clish_command__get_depth(cmd);
  182. }
  183. }
  184. /* Lock the lockfile */
  185. if (lock_path && clish_command__get_lock(cmd)) {
  186. int i;
  187. int res;
  188. lock_fd = open(lock_path, O_RDONLY | O_CREAT, 00644);
  189. if (-1 == lock_fd) {
  190. fprintf(stderr, "Can't open lockfile %s.\n",
  191. lock_path);
  192. return BOOL_FALSE; /* can't open file */
  193. }
  194. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  195. res = flock(lock_fd, LOCK_EX | LOCK_NB);
  196. if (!res)
  197. break;
  198. if ((EBADF == errno) ||
  199. (EINVAL == errno) ||
  200. (ENOLCK == errno))
  201. break;
  202. if (EINTR == errno)
  203. continue;
  204. if (0 == i)
  205. fprintf(stderr,
  206. "Try to get lock. Please wait...\n");
  207. sleep(1);
  208. }
  209. if (res) {
  210. fprintf(stderr, "Can't get lock.\n");
  211. return BOOL_FALSE; /* can't get the lock */
  212. }
  213. }
  214. /* Ignore and block SIGINT and SIGQUIT */
  215. if (!clish_command__get_interrupt(cmd)) {
  216. struct sigaction sa;
  217. sigset_t sigs;
  218. sa.sa_flags = 0;
  219. sigemptyset(&sa.sa_mask);
  220. sa.sa_handler = SIG_IGN;
  221. sigaction(SIGINT, &sa, &old_sigint);
  222. sigaction(SIGQUIT, &sa, &old_sigquit);
  223. sigemptyset(&sigs);
  224. sigaddset(&sigs, SIGINT);
  225. sigaddset(&sigs, SIGQUIT);
  226. sigprocmask(SIG_BLOCK, &sigs, &old_sigs);
  227. }
  228. /* Execute ACTION */
  229. result = clish_shell_exec_action(action, context, out);
  230. /* Restore SIGINT and SIGQUIT */
  231. if (!clish_command__get_interrupt(cmd)) {
  232. sigprocmask(SIG_SETMASK, &old_sigs, NULL);
  233. /* Is the signals delivery guaranteed here (before
  234. sigaction restore) for previously blocked and
  235. pending signals? The simple test is working well.
  236. I don't want to use sigtimedwait() function bacause
  237. it needs a realtime extensions. The sigpending() with
  238. the sleep() is not nice too. Report bug if clish will
  239. get the SIGINT after non-interruptable action.
  240. */
  241. sigaction(SIGINT, &old_sigint, NULL);
  242. sigaction(SIGQUIT, &old_sigquit, NULL);
  243. }
  244. /* Call config callback */
  245. if (result && this->client_hooks->config_fn)
  246. this->client_hooks->config_fn(context);
  247. /* Unlock the lockfile */
  248. if (lock_fd != -1) {
  249. flock(lock_fd, LOCK_UN);
  250. close(lock_fd);
  251. }
  252. /* Move into the new view */
  253. if (result) {
  254. clish_view_t *view = clish_command__get_view(cmd);
  255. /* Save the PWD */
  256. if (view) {
  257. char *line = clish_shell__get_line(context);
  258. clish_shell__set_pwd(this, line, view,
  259. clish_command__get_viewid(cmd), context);
  260. lub_string_free(line);
  261. }
  262. }
  263. return result;
  264. }
  265. /*----------------------------------------------------------- */
  266. bool_t clish_shell_exec_action(clish_action_t *action,
  267. clish_context_t *context, char **out)
  268. {
  269. clish_shell_t *this = context->shell;
  270. bool_t result = BOOL_TRUE;
  271. const char *builtin;
  272. char *script;
  273. builtin = clish_action__get_builtin(action);
  274. script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
  275. if (builtin) {
  276. clish_shell_builtin_fn_t *callback;
  277. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  278. result = BOOL_FALSE;
  279. /* search for an internal command */
  280. callback = find_builtin_callback(clish_cmd_list, builtin);
  281. if (!callback) {
  282. /* search for a client command */
  283. callback = find_builtin_callback(
  284. this->client_hooks->cmd_list, builtin);
  285. }
  286. /* invoke the builtin callback */
  287. if (callback)
  288. result = callback(context, argv);
  289. if (argv)
  290. lub_argv_delete(argv);
  291. } else if (script) {
  292. /* now get the client to interpret the resulting script */
  293. result = this->client_hooks->script_fn(context, script, out);
  294. }
  295. lub_string_free(script);
  296. return result;
  297. }
  298. /*----------------------------------------------------------- */
  299. /*
  300. * Find out the previous view in the stack and go to it
  301. */
  302. static bool_t clish_nested_up(clish_context_t *context, const lub_argv_t * argv)
  303. {
  304. clish_shell_t *this = context->shell;
  305. if (!this)
  306. return BOOL_FALSE;
  307. argv = argv; /* not used */
  308. /* If depth=0 than exit */
  309. if (0 == this->depth) {
  310. this->state = SHELL_STATE_CLOSING;
  311. return BOOL_TRUE;
  312. }
  313. this->depth--;
  314. return BOOL_TRUE;
  315. }
  316. /*----------------------------------------------------------- */
  317. /*
  318. * Builtin: NOP function
  319. */
  320. static bool_t clish_nop(clish_context_t *context, const lub_argv_t * argv)
  321. {
  322. return BOOL_TRUE;
  323. }
  324. /*----------------------------------------------------------- */
  325. const char * clish_shell__get_fifo(clish_shell_t * this)
  326. {
  327. char *name;
  328. int res;
  329. if (this->fifo_name) {
  330. if (0 == access(this->fifo_name, R_OK | W_OK))
  331. return this->fifo_name;
  332. unlink(this->fifo_name);
  333. lub_string_free(this->fifo_name);
  334. this->fifo_name = NULL;
  335. }
  336. do {
  337. char template[] = "/tmp/klish.fifo.XXXXXX";
  338. name = mktemp(template);
  339. if (name[0] == '\0')
  340. return NULL;
  341. res = mkfifo(name, 0600);
  342. if (res == 0)
  343. this->fifo_name = lub_string_dup(name);
  344. } while ((res < 0) && (EEXIST == errno));
  345. return this->fifo_name;
  346. }
  347. /*--------------------------------------------------------- */
  348. void *clish_shell__get_client_cookie(const clish_shell_t * this)
  349. {
  350. return this->client_cookie;
  351. }
  352. /*----------------------------------------------------------- */