clish.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /* Dryrun config and log hooks */
  284. if (dryrun_config) {
  285. if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_CONFIG)))
  286. clish_sym__set_permanent(sym, BOOL_FALSE);
  287. if ((sym = clish_shell_get_hook(shell, CLISH_SYM_TYPE_LOG)))
  288. clish_sym__set_permanent(sym, BOOL_FALSE);
  289. }
  290. /* Set source of command stream: files or interactive tty */
  291. if(optind < argc) {
  292. int i;
  293. /* Run the commands from the files */
  294. for (i = argc - 1; i >= optind; i--)
  295. clish_shell_push_file(shell, argv[i], stop_on_error);
  296. } else {
  297. /* The interactive shell */
  298. int tmpfd = dup(fileno(stdin));
  299. #ifdef FD_CLOEXEC
  300. fcntl(tmpfd, F_SETFD, fcntl(tmpfd, F_GETFD) | FD_CLOEXEC);
  301. #endif
  302. clish_shell_push_fd(shell, fdopen(tmpfd, "r"), stop_on_error);
  303. }
  304. /* Execute startup */
  305. running = clish_shell_startup(shell);
  306. if (running) {
  307. fprintf(stderr, "Error: Can't startup clish.\n");
  308. goto end;
  309. }
  310. if (cmd) {
  311. /* Iterate cmds */
  312. for(iter = lub_list__get_head(cmds);
  313. iter; iter = lub_list_node__get_next(iter)) {
  314. char *str = (char *)lub_list_node__get_data(iter);
  315. result = clish_shell_forceline(shell, str, NULL);
  316. if (stop_on_error && result)
  317. break;
  318. }
  319. } else {
  320. /* Main loop */
  321. result = clish_shell_loop(shell);
  322. }
  323. end:
  324. /* Cleanup */
  325. if (shell) {
  326. if (histfile_expanded) {
  327. clish_shell__save_history(shell, histfile_expanded);
  328. free(histfile_expanded);
  329. }
  330. clish_shell_delete(shell);
  331. }
  332. if (quiet && (outfd != stdout))
  333. fclose(outfd);
  334. /* Delete each cmds element */
  335. while ((iter = lub_list__get_head(cmds))) {
  336. lub_list_del(cmds, iter);
  337. free(lub_list_node__get_data(iter));
  338. lub_list_node_free(iter);
  339. }
  340. lub_list_free(cmds);
  341. return result;
  342. }
  343. /*--------------------------------------------------------- */
  344. /* Print help message */
  345. static void help(int status, const char *argv0)
  346. {
  347. const char *name = NULL;
  348. if (!argv0)
  349. return;
  350. /* Find the basename */
  351. name = strrchr(argv0, '/');
  352. if (name)
  353. name++;
  354. else
  355. name = argv0;
  356. if (status != 0) {
  357. fprintf(stderr, "Try `%s -h' for more information.\n",
  358. name);
  359. } else {
  360. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  361. printf("CLI utility. Command line shell."
  362. "The part of the klish project.\n");
  363. printf("Options:\n");
  364. printf("\t-v, --version\tPrint version.\n");
  365. printf("\t-h, --help\tPrint this help.\n");
  366. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  367. "\n\t\tof the konfd daemon.\n");
  368. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  369. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  370. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  371. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  372. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  373. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  374. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  375. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  376. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  377. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  378. printf("\t-o, --log\tEnable command logging to syslog's.\n");
  379. printf("\t-O, --facility\tSyslog facility. Default is LOCAL0.\n");
  380. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  381. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  382. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  383. printf("\t-f <path>, --histfile=<path>\tFile to save command history.\n");
  384. printf("\t-z <num>, --histsize=<num>\tCommand history size in lines.\n");
  385. }
  386. }
  387. /*--------------------------------------------------------- */
  388. /*
  389. * Signal handler for SIGPIPE.
  390. * It's empty but it's needed to don't ignore SIGPIPE because
  391. * SIG_IGN will be inherited while ACTION execution.
  392. */
  393. static void sighandler(int signo)
  394. {
  395. signo = signo; /* Happy compiler */
  396. return;
  397. }