clish.c 9.6 KB

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