1
0

clish.c 11 KB

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