1
0

clish.c 11 KB

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