clish.c 11 KB

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