shell_execute.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /* the exception proves the rule... */
  60. clish_shell_t *this = (clish_shell_t *) 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(const clish_shell_t * shell, const lub_argv_t * argv)
  83. {
  84. return (clish_source_internal(shell, 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
  93. clish_source_nostop(const clish_shell_t * shell, const lub_argv_t * argv)
  94. {
  95. return (clish_source_internal(shell, argv, BOOL_FALSE));
  96. }
  97. /*----------------------------------------------------------- */
  98. /*
  99. Show the shell overview
  100. */
  101. static bool_t
  102. clish_overview(const clish_shell_t * this, const lub_argv_t * argv)
  103. {
  104. argv = argv; /* not used */
  105. tinyrl_printf(this->tinyrl, "%s\n", this->overview);
  106. return BOOL_TRUE;
  107. }
  108. /*----------------------------------------------------------- */
  109. static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
  110. {
  111. tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
  112. tinyrl_history_iterator_t iter;
  113. const tinyrl_history_entry_t *entry;
  114. unsigned limit = 0;
  115. const char *arg = lub_argv__get_arg(argv, 0);
  116. if ((NULL != arg) && ('\0' != *arg)) {
  117. limit = (unsigned)atoi(arg);
  118. if (0 == limit) {
  119. /* unlimit the history list */
  120. (void)tinyrl_history_unstifle(history);
  121. } else {
  122. /* limit the scope of the history list */
  123. tinyrl_history_stifle(history, limit);
  124. }
  125. }
  126. for (entry = tinyrl_history_getfirst(history, &iter);
  127. entry; entry = tinyrl_history_getnext(&iter)) {
  128. /* dump the details of this entry */
  129. tinyrl_printf(this->tinyrl,
  130. "%5d %s\n",
  131. tinyrl_history_entry__get_index(entry),
  132. tinyrl_history_entry__get_line(entry));
  133. }
  134. return BOOL_TRUE;
  135. }
  136. /*----------------------------------------------------------- */
  137. /*
  138. * Searches for a builtin command to execute
  139. */
  140. static clish_shell_builtin_fn_t *find_builtin_callback(const
  141. clish_shell_builtin_t *
  142. cmd_list,
  143. const char *name)
  144. {
  145. const clish_shell_builtin_t *result;
  146. /* search a list of commands */
  147. for (result = cmd_list; result && result->name; result++) {
  148. if (0 == strcmp(name, result->name)) {
  149. break;
  150. }
  151. }
  152. return (result && result->name) ? result->callback : NULL;
  153. }
  154. /*----------------------------------------------------------- */
  155. void clish_shell_cleanup_script(void *script)
  156. {
  157. /* simply release the memory */
  158. lub_string_free(script);
  159. }
  160. /*----------------------------------------------------------- */
  161. bool_t
  162. clish_shell_execute(clish_shell_t * this,
  163. const clish_command_t * cmd, clish_pargv_t * pargv, char ** out)
  164. {
  165. bool_t result = BOOL_TRUE;
  166. const char *builtin;
  167. char *script;
  168. char *lock_path = clish_shell__get_lockfile(this);
  169. int lock_fd = -1;
  170. assert(NULL != cmd);
  171. /* Pre-change view if the command is from another depth/view */
  172. {
  173. clish_view_t *view = NULL;
  174. char *viewid = NULL;
  175. clish_view_restore_t restore = clish_command__get_restore(cmd);
  176. if ((CLISH_RESTORE_VIEW == restore) &&
  177. (clish_command__get_pview(cmd) != this->view))
  178. view = clish_command__get_pview(cmd);
  179. else if ((CLISH_RESTORE_DEPTH == restore) &&
  180. (clish_command__get_depth(cmd) <
  181. clish_view__get_depth(this->view))) {
  182. view = clish_shell__get_pwd_view(this,
  183. clish_command__get_depth(cmd));
  184. viewid = clish_shell__get_pwd_viewid(this,
  185. clish_command__get_depth(cmd));
  186. }
  187. if (NULL != view) {
  188. this->view = view;
  189. /* cleanup */
  190. lub_string_free(this->viewid);
  191. this->viewid = lub_string_dup(viewid);
  192. }
  193. }
  194. /* Lock the lockfile */
  195. if (lock_path && clish_command__get_lock(cmd)) {
  196. int i;
  197. int res;
  198. lock_fd = open(lock_path, O_RDONLY | O_CREAT, 00644);
  199. if (-1 == lock_fd) {
  200. fprintf(stderr, "Can't open lockfile %s.\n",
  201. lock_path);
  202. return BOOL_FALSE; /* can't open file */
  203. }
  204. for (i = 0; i < CLISH_LOCK_WAIT; i++) {
  205. res = flock(lock_fd, LOCK_EX | LOCK_NB);
  206. if (!res)
  207. break;
  208. if ((EBADF == errno) ||
  209. (EINVAL == errno) ||
  210. (ENOLCK == errno))
  211. break;
  212. if (EINTR == errno)
  213. continue;
  214. if (0 == i)
  215. fprintf(stderr,
  216. "Try to get lock. Please wait...\n");
  217. sleep(1);
  218. }
  219. if (res) {
  220. fprintf(stderr, "Can't get lock.\n");
  221. return BOOL_FALSE; /* can't get the lock */
  222. }
  223. }
  224. /* Execute ACTION */
  225. builtin = clish_command__get_builtin(cmd);
  226. script = clish_command__get_action(cmd, this->viewid, pargv);
  227. /* account for thread cancellation whilst running a script */
  228. pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
  229. script);
  230. if (NULL != builtin) {
  231. clish_shell_builtin_fn_t *callback;
  232. lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
  233. result = BOOL_FALSE;
  234. /* search for an internal command */
  235. callback = find_builtin_callback(clish_cmd_list, builtin);
  236. if (NULL == callback) {
  237. /* search for a client command */
  238. callback =
  239. find_builtin_callback(this->client_hooks->cmd_list,
  240. builtin);
  241. }
  242. if (NULL != callback) {
  243. /* invoke the builtin callback */
  244. result = callback(this, argv);
  245. }
  246. if (NULL != argv) {
  247. lub_argv_delete(argv);
  248. }
  249. } else if (NULL != script) {
  250. /* now get the client to interpret the resulting script */
  251. result = this->client_hooks->script_fn(this, cmd, script, out);
  252. }
  253. pthread_cleanup_pop(1);
  254. /* Call config callback */
  255. if ((BOOL_TRUE == result) && this->client_hooks->config_fn)
  256. this->client_hooks->config_fn(this, cmd, pargv);
  257. /* Unlock the lockfile */
  258. if (lock_fd != -1) {
  259. flock(lock_fd, LOCK_UN);
  260. close(lock_fd);
  261. }
  262. /* Move into the new view */
  263. if (BOOL_TRUE == result) {
  264. clish_view_t *view = clish_command__get_view(cmd);
  265. char *viewid = clish_command__get_viewid(cmd,
  266. this->viewid, pargv);
  267. if (NULL != view) {
  268. /* Save the current config PWD */
  269. char *line = clish_variable__get_line(cmd, pargv);
  270. clish_shell__set_pwd(this,
  271. clish_command__get_depth(cmd),
  272. line, this->view, this->viewid);
  273. lub_string_free(line);
  274. /* Change view */
  275. this->view = view;
  276. }
  277. if (viewid || view) {
  278. /* cleanup */
  279. lub_string_free(this->viewid);
  280. this->viewid = viewid;
  281. }
  282. }
  283. return result;
  284. }
  285. /*----------------------------------------------------------- */
  286. /*
  287. * Find out the previous view in the stack and go to it
  288. */
  289. static bool_t clish_nested_up(const clish_shell_t * shell, const lub_argv_t * argv)
  290. {
  291. clish_shell_t *this = (clish_shell_t *) shell;
  292. clish_view_t *view = NULL;
  293. char *viewid = NULL;
  294. int depth = 0;
  295. if (!shell)
  296. return BOOL_FALSE;
  297. argv = argv; /* not used */
  298. depth = clish_view__get_depth(this->view);
  299. /* If depth=0 than exit */
  300. if (0 == depth) {
  301. this->state = SHELL_STATE_CLOSING;
  302. return BOOL_TRUE;
  303. }
  304. depth--;
  305. view = clish_shell__get_pwd_view(this, depth);
  306. viewid = clish_shell__get_pwd_viewid(this, depth);
  307. if (!view)
  308. return BOOL_FALSE;
  309. this->view = view;
  310. lub_string_free(this->viewid);
  311. this->viewid = viewid ? lub_string_dup(viewid) : NULL;
  312. return BOOL_TRUE;
  313. }
  314. /*----------------------------------------------------------- */
  315. const char * clish_shell__get_fifo(clish_shell_t * this)
  316. {
  317. char *name;
  318. int res;
  319. if (this->fifo_name) {
  320. if (0 == access(this->fifo_name, R_OK | W_OK))
  321. return this->fifo_name;
  322. unlink(this->fifo_name);
  323. lub_string_free(this->fifo_name);
  324. this->fifo_name = NULL;
  325. }
  326. do {
  327. char template[] = "/tmp/klish.fifo.XXXXXX";
  328. name = mktemp(template);
  329. if (name[0] == '\0')
  330. return NULL;
  331. res = mkfifo(name, 0600);
  332. if (res == 0)
  333. this->fifo_name = lub_string_dup(name);
  334. } while ((res < 0) && (EEXIST == errno));
  335. return this->fifo_name;
  336. }
  337. /*----------------------------------------------------------- */