clish.c 10 KB

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