1
0

clish.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * --------------------------------------
  3. * clish.c
  4. *
  5. * A console client for libclish
  6. * --------------------------------------
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif /* HAVE_CONFIG_H */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #ifdef HAVE_GETOPT_H
  16. #include <getopt.h>
  17. #endif
  18. #include <signal.h>
  19. #if HAVE_LOCALE_H
  20. #include <locale.h>
  21. #endif
  22. #if HAVE_LANGINFO_CODESET
  23. #include <langinfo.h>
  24. #endif
  25. #include "lub/list.h"
  26. #include "lub/system.h"
  27. #include "clish/shell.h"
  28. #define QUOTE(t) #t
  29. /* #define version(v) printf("%s\n", QUOTE(v)) */
  30. #define version(v) printf("%s\n", v)
  31. static void sighandler(int signo);
  32. static void help(int status, const char *argv0);
  33. /*--------------------------------------------------------- */
  34. int main(int argc, char **argv)
  35. {
  36. int running;
  37. int result = -1;
  38. clish_shell_t *shell = NULL;
  39. /* Command line options */
  40. const char *socket_path = KONFD_SOCKET_PATH;
  41. bool_t lockless = BOOL_FALSE;
  42. bool_t stop_on_error = BOOL_FALSE;
  43. bool_t interactive = BOOL_TRUE;
  44. bool_t quiet = BOOL_FALSE;
  45. bool_t utf8 = BOOL_FALSE;
  46. bool_t bit8 = BOOL_FALSE;
  47. bool_t log = BOOL_FALSE;
  48. bool_t dryrun = BOOL_FALSE;
  49. bool_t dryrun_config = BOOL_FALSE;
  50. const char *xml_path = getenv("CLISH_PATH");
  51. const char *view = getenv("CLISH_VIEW");
  52. const char *viewid = getenv("CLISH_VIEWID");
  53. FILE *outfd = stdout;
  54. bool_t istimeout = BOOL_FALSE;
  55. int timeout = 0;
  56. bool_t cmd = BOOL_FALSE; /* -c option */
  57. lub_list_t *cmds; /* Commands defined by -c */
  58. lub_list_node_t *iter;
  59. const char *histfile = "~/.clish_history";
  60. char *histfile_expanded = NULL;
  61. unsigned int histsize = 50;
  62. clish_sym_t *sym = NULL;
  63. /* Signal vars */
  64. struct sigaction sigpipe_act;
  65. sigset_t sigpipe_set;
  66. static const char *shortopts = "hvs:ledx:w:i:bqu8okt:c:f:z:";
  67. #ifdef HAVE_GETOPT_H
  68. static const struct option longopts[] = {
  69. {"help", 0, NULL, 'h'},
  70. {"version", 0, NULL, 'v'},
  71. {"socket", 1, NULL, 's'},
  72. {"lockless", 0, NULL, 'l'},
  73. {"stop-on-error", 0, NULL, 'e'},
  74. {"dry-run", 0, NULL, 'd'},
  75. {"xml-path", 1, NULL, 'x'},
  76. {"view", 1, NULL, 'w'},
  77. {"viewid", 1, NULL, 'i'},
  78. {"background", 0, NULL, 'b'},
  79. {"quiet", 0, NULL, 'q'},
  80. {"utf8", 0, NULL, 'u'},
  81. {"8bit", 0, NULL, '8'},
  82. {"log", 0, NULL, 'o'},
  83. {"check", 0, NULL, 'k'},
  84. {"timeout", 1, NULL, 't'},
  85. {"command", 1, NULL, 'c'},
  86. {"histfile", 1, NULL, 'f'},
  87. {"histsize", 1, NULL, 'z'},
  88. {NULL, 0, NULL, 0}
  89. };
  90. #endif
  91. /* Ignore SIGPIPE */
  92. sigemptyset(&sigpipe_set);
  93. sigaddset(&sigpipe_set, SIGPIPE);
  94. sigpipe_act.sa_flags = 0;
  95. sigpipe_act.sa_mask = sigpipe_set;
  96. sigpipe_act.sa_handler = &sighandler;
  97. sigaction(SIGPIPE, &sigpipe_act, NULL);
  98. #if HAVE_LOCALE_H
  99. /* Set current locale */
  100. setlocale(LC_ALL, "");
  101. #endif
  102. /* Var initialization */
  103. cmds = lub_list_new(NULL);
  104. /* Parse command line options */
  105. while(1) {
  106. int opt;
  107. #ifdef HAVE_GETOPT_H
  108. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  109. #else
  110. opt = getopt(argc, argv, shortopts);
  111. #endif
  112. if (-1 == opt)
  113. break;
  114. switch (opt) {
  115. case 's':
  116. socket_path = optarg;
  117. break;
  118. case 'l':
  119. lockless = BOOL_TRUE;
  120. break;
  121. case 'e':
  122. stop_on_error = BOOL_TRUE;
  123. break;
  124. case 'b':
  125. interactive = BOOL_FALSE;
  126. break;
  127. case 'q':
  128. quiet = BOOL_TRUE;
  129. break;
  130. case 'u':
  131. utf8 = BOOL_TRUE;
  132. break;
  133. case '8':
  134. bit8 = BOOL_TRUE;
  135. break;
  136. case 'o':
  137. log = BOOL_TRUE;
  138. break;
  139. case 'd':
  140. dryrun = BOOL_TRUE;
  141. break;
  142. case 'x':
  143. xml_path = optarg;
  144. break;
  145. case 'w':
  146. view = optarg;
  147. break;
  148. case 'i':
  149. viewid = optarg;
  150. break;
  151. case 'k':
  152. lockless = BOOL_TRUE;
  153. dryrun = BOOL_TRUE;
  154. dryrun_config = BOOL_TRUE;
  155. break;
  156. case 't':
  157. istimeout = BOOL_TRUE;
  158. timeout = atoi(optarg);
  159. break;
  160. case 'c': {
  161. char *str;
  162. cmd = BOOL_TRUE;
  163. quiet = BOOL_TRUE;
  164. str = strdup(optarg);
  165. lub_list_add(cmds, str);
  166. }
  167. break;
  168. case 'f':
  169. if (!strcmp(optarg, "/dev/null"))
  170. histfile = NULL;
  171. else
  172. histfile = optarg;
  173. break;
  174. case 'z': {
  175. int itmp = 0;
  176. itmp = atoi(optarg);
  177. if (itmp <= 0) {
  178. fprintf(stderr, "Error: Illegal histsize option value.\n");
  179. help(-1, argv[0]);
  180. goto end;
  181. }
  182. histsize = itmp;
  183. }
  184. break;
  185. case 'h':
  186. help(0, argv[0]);
  187. exit(0);
  188. break;
  189. case 'v':
  190. version(VERSION);
  191. exit(0);
  192. break;
  193. default:
  194. help(-1, argv[0]);
  195. goto end;
  196. break;
  197. }
  198. }
  199. /* Validate command line options */
  200. if (utf8 && bit8) {
  201. fprintf(stderr, "Error: The -u and -8 options can't be used together.\n");
  202. goto end;
  203. }
  204. /* Create shell instance */
  205. if (quiet)
  206. outfd = fopen("/dev/null", "w");
  207. shell = clish_shell_new(NULL, outfd, stop_on_error);
  208. if (!shell) {
  209. fprintf(stderr, "Error: Can't run clish.\n");
  210. goto end;
  211. }
  212. /* Load the XML files */
  213. if (clish_shell_load_scheme(shell, xml_path))
  214. goto end;
  215. /* Set communication to the konfd */
  216. clish_shell__set_socket(shell, socket_path);
  217. /* Set lockless mode */
  218. if (lockless)
  219. clish_shell__set_lockfile(shell, NULL);
  220. /* Set interactive mode */
  221. if (!interactive)
  222. clish_shell__set_interactive(shell, interactive);
  223. /* Set startup view */
  224. if (view)
  225. clish_shell__set_startup_view(shell, view);
  226. /* Set startup viewid */
  227. if (viewid)
  228. clish_shell__set_startup_viewid(shell, viewid);
  229. /* Set UTF-8 or 8-bit mode */
  230. if (utf8 || bit8)
  231. clish_shell__set_utf8(shell, utf8);
  232. else {
  233. #if HAVE_LANGINFO_CODESET
  234. /* Autodetect encoding */
  235. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  236. clish_shell__set_utf8(shell, BOOL_TRUE);
  237. #else
  238. /* The default is 8-bit if locale is not supported */
  239. clish_shell__set_utf8(shell, BOOL_FALSE);
  240. #endif
  241. }
  242. /* Set logging */
  243. if (log)
  244. clish_shell__set_log(shell, log);
  245. /* Set dry-run */
  246. if (dryrun)
  247. clish_shell__set_dryrun(shell, dryrun);
  248. /* Set idle timeout */
  249. if (istimeout)
  250. clish_shell__set_timeout(shell, timeout);
  251. /* Set history settings */
  252. clish_shell__stifle_history(shell, histsize);
  253. if (histfile)
  254. histfile_expanded = lub_system_tilde_expand(histfile);
  255. if (histfile_expanded)
  256. clish_shell__restore_history(shell, histfile_expanded);
  257. /* Load plugins */
  258. if (clish_shell_load_plugins(shell) < 0)
  259. goto end;
  260. if (clish_shell_link_plugins(shell) < 0)
  261. goto end;
  262. /* Dryrun config and log hooks */
  263. if (dryrun_config) {
  264. if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_CONFIG)))
  265. clish_sym__set_permanent(sym, BOOL_FALSE);
  266. if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_LOG)))
  267. clish_sym__set_permanent(sym, BOOL_FALSE);
  268. }
  269. /* Set source of command stream: files or interactive tty */
  270. if(optind < argc) {
  271. int i;
  272. /* Run the commands from the files */
  273. for (i = argc - 1; i >= optind; i--)
  274. clish_shell_push_file(shell, argv[i], stop_on_error);
  275. } else {
  276. /* The interactive shell */
  277. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  278. stop_on_error);
  279. }
  280. /* Execute startup */
  281. running = clish_shell_startup(shell);
  282. if (running) {
  283. fprintf(stderr, "Error: Can't startup clish.\n");
  284. goto end;
  285. }
  286. if (cmd) {
  287. /* Iterate cmds */
  288. for(iter = lub_list__get_head(cmds);
  289. iter; iter = lub_list_node__get_next(iter)) {
  290. char *str = (char *)lub_list_node__get_data(iter);
  291. result = clish_shell_forceline(shell, str, NULL);
  292. if (stop_on_error && result)
  293. break;
  294. }
  295. } else {
  296. /* Main loop */
  297. result = clish_shell_loop(shell);
  298. }
  299. end:
  300. /* Cleanup */
  301. if (shell) {
  302. if (histfile_expanded) {
  303. clish_shell__save_history(shell, histfile_expanded);
  304. free(histfile_expanded);
  305. }
  306. clish_shell_delete(shell);
  307. }
  308. if (quiet)
  309. fclose(outfd);
  310. /* Delete each cmds element */
  311. while ((iter = lub_list__get_head(cmds))) {
  312. lub_list_del(cmds, iter);
  313. free(lub_list_node__get_data(iter));
  314. lub_list_node_free(iter);
  315. }
  316. lub_list_free(cmds);
  317. return result;
  318. }
  319. /*--------------------------------------------------------- */
  320. /* Print help message */
  321. static void help(int status, const char *argv0)
  322. {
  323. const char *name = NULL;
  324. if (!argv0)
  325. return;
  326. /* Find the basename */
  327. name = strrchr(argv0, '/');
  328. if (name)
  329. name++;
  330. else
  331. name = argv0;
  332. if (status != 0) {
  333. fprintf(stderr, "Try `%s -h' for more information.\n",
  334. name);
  335. } else {
  336. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  337. printf("CLI utility. Command line shell."
  338. "The part of the klish project.\n");
  339. printf("Options:\n");
  340. printf("\t-v, --version\tPrint version.\n");
  341. printf("\t-h, --help\tPrint this help.\n");
  342. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  343. "\n\t\tof the konfd daemon.\n");
  344. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  345. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  346. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  347. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  348. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  349. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  350. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  351. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  352. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  353. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  354. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  355. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  356. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  357. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  358. printf("\t-f <path>, --histfile=<path>\tFile to save command history.\n");
  359. printf("\t-z <num>, --histsize=<num>\tCommand history size in lines.\n");
  360. }
  361. }
  362. /*--------------------------------------------------------- */
  363. /*
  364. * Signal handler for SIGPIPE.
  365. * It's empty but it's needed to don't ignore SIGPIPE because
  366. * SIG_IGN will be inherited while ACTION execution.
  367. */
  368. static void sighandler(int signo)
  369. {
  370. return;
  371. }