clish.c 9.4 KB

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