clish.c 9.4 KB

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