clish.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /* Set source of command stream: files or interactive tty */
  267. if(optind < argc) {
  268. int i;
  269. /* Run the commands from the files */
  270. for (i = argc - 1; i >= optind; i--)
  271. clish_shell_push_file(shell, argv[i], stop_on_error);
  272. } else {
  273. /* The interactive shell */
  274. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  275. stop_on_error);
  276. }
  277. /* Execute startup */
  278. running = clish_shell_startup(shell);
  279. if (running) {
  280. fprintf(stderr, "Error: Can't startup clish.\n");
  281. goto end;
  282. }
  283. if (cmd) {
  284. /* Iterate cmds */
  285. for(iter = lub_list__get_head(cmds);
  286. iter; iter = lub_list_node__get_next(iter)) {
  287. char *str = (char *)lub_list_node__get_data(iter);
  288. clish_shell_forceline(shell, str, NULL);
  289. }
  290. } else {
  291. /* Main loop */
  292. result = clish_shell_loop(shell);
  293. }
  294. end:
  295. /* Cleanup */
  296. if (shell) {
  297. if (histfile_expanded) {
  298. clish_shell__save_history(shell, histfile_expanded);
  299. free(histfile_expanded);
  300. }
  301. clish_shell_delete(shell);
  302. }
  303. if (quiet)
  304. fclose(outfd);
  305. /* Delete each cmds element */
  306. while ((iter = lub_list__get_head(cmds))) {
  307. lub_list_del(cmds, iter);
  308. free(lub_list_node__get_data(iter));
  309. lub_list_node_free(iter);
  310. }
  311. lub_list_free(cmds);
  312. return result;
  313. }
  314. /*--------------------------------------------------------- */
  315. /* Print help message */
  316. static void help(int status, const char *argv0)
  317. {
  318. const char *name = NULL;
  319. if (!argv0)
  320. return;
  321. /* Find the basename */
  322. name = strrchr(argv0, '/');
  323. if (name)
  324. name++;
  325. else
  326. name = argv0;
  327. if (status != 0) {
  328. fprintf(stderr, "Try `%s -h' for more information.\n",
  329. name);
  330. } else {
  331. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  332. printf("CLI utility. Command line shell."
  333. "The part of the klish project.\n");
  334. printf("Options:\n");
  335. printf("\t-v, --version\tPrint version.\n");
  336. printf("\t-h, --help\tPrint this help.\n");
  337. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  338. "\n\t\tof the konfd daemon.\n");
  339. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  340. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  341. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  342. printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
  343. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  344. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  345. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  346. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  347. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  348. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  349. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  350. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  351. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  352. printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
  353. printf("\t-f <path>, --histfile=<path>\tFile to save command history.\n");
  354. printf("\t-z <num>, --histsize=<num>\tCommand history size in lines.\n");
  355. }
  356. }