clish.c 11 KB

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