clish.c 10.0 KB

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