clish.c 9.1 KB

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