clish.c 9.1 KB

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