clish.c 9.6 KB

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