clish.c 8.3 KB

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