clish.c 11 KB

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