clish.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //-------------------------------------
  2. // clish.cpp
  3. //
  4. // A console client for libclish
  5. //-------------------------------------
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif /* HAVE_CONFIG_H */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #ifdef HAVE_GETOPT_H
  14. #include <getopt.h>
  15. #endif
  16. #include <signal.h>
  17. #if HAVE_LOCALE_H
  18. #include <locale.h>
  19. #endif
  20. #if HAVE_LANGINFO_CODESET
  21. #include <langinfo.h>
  22. #endif
  23. #include "clish/shell.h"
  24. #include "clish/internal.h"
  25. #ifndef VERSION
  26. #define VERSION 1.2.2
  27. #endif
  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;
  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. /* Signal vars */
  65. struct sigaction sigpipe_act;
  66. sigset_t sigpipe_set;
  67. static const char *shortopts = "hvs:ledx:w:i:bqu8okt:";
  68. #ifdef HAVE_GETOPT_H
  69. static const struct option longopts[] = {
  70. {"help", 0, NULL, 'h'},
  71. {"version", 0, NULL, 'v'},
  72. {"socket", 1, NULL, 's'},
  73. {"lockless", 0, NULL, 'l'},
  74. {"stop-on-error", 0, NULL, 'e'},
  75. {"dry-run", 0, NULL, 'd'},
  76. {"xml-path", 1, NULL, 'x'},
  77. {"view", 1, NULL, 'w'},
  78. {"viewid", 1, NULL, 'i'},
  79. {"background", 0, NULL, 'b'},
  80. {"quiet", 0, NULL, 'q'},
  81. {"utf8", 0, NULL, 'u'},
  82. {"8bit", 0, NULL, '8'},
  83. {"log", 0, NULL, 'o'},
  84. {"check", 0, NULL, 'k'},
  85. {"timeout", 1, NULL, 't'},
  86. {NULL, 0, NULL, 0}
  87. };
  88. #endif
  89. /* Ignore SIGPIPE */
  90. sigemptyset(&sigpipe_set);
  91. sigaddset(&sigpipe_set, SIGPIPE);
  92. sigpipe_act.sa_flags = 0;
  93. sigpipe_act.sa_mask = sigpipe_set;
  94. sigpipe_act.sa_handler = SIG_IGN;
  95. sigaction(SIGPIPE, &sigpipe_act, NULL);
  96. #if HAVE_LOCALE_H
  97. /* Set current locale */
  98. setlocale(LC_ALL, "");
  99. #endif
  100. /* Parse command line options */
  101. optind = 0;
  102. while(1) {
  103. int opt;
  104. #ifdef HAVE_GETOPT_H
  105. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  106. #else
  107. opt = getopt(argc, argv, shortopts);
  108. #endif
  109. if (-1 == opt)
  110. break;
  111. switch (opt) {
  112. case 's':
  113. socket_path = optarg;
  114. break;
  115. case 'l':
  116. lockless = BOOL_TRUE;
  117. break;
  118. case 'e':
  119. stop_on_error = BOOL_TRUE;
  120. break;
  121. case 'b':
  122. interactive = BOOL_FALSE;
  123. break;
  124. case 'q':
  125. quiet = BOOL_TRUE;
  126. break;
  127. case 'u':
  128. utf8 = BOOL_TRUE;
  129. break;
  130. case '8':
  131. bit8 = BOOL_TRUE;
  132. break;
  133. case 'o':
  134. log = BOOL_TRUE;
  135. break;
  136. case 'd':
  137. my_hooks.script_fn = clish_dryrun_callback;
  138. break;
  139. case 'x':
  140. xml_path = optarg;
  141. break;
  142. case 'w':
  143. view = optarg;
  144. break;
  145. case 'i':
  146. viewid = optarg;
  147. break;
  148. case 'k':
  149. lockless = BOOL_TRUE;
  150. my_hooks.script_fn = clish_dryrun_callback;
  151. my_hooks.config_fn = NULL;
  152. break;
  153. case 't':
  154. istimeout = BOOL_TRUE;
  155. timeout = atoi(optarg);
  156. break;
  157. case 'h':
  158. help(0, argv[0]);
  159. exit(0);
  160. break;
  161. case 'v':
  162. version(VERSION);
  163. exit(0);
  164. break;
  165. default:
  166. help(-1, argv[0]);
  167. exit(-1);
  168. break;
  169. }
  170. }
  171. /* Validate command line options */
  172. if (utf8 && bit8) {
  173. fprintf(stderr, "The -u and -8 options can't be used together.\n");
  174. return -1;
  175. }
  176. /* Create shell instance */
  177. if (quiet)
  178. outfd = fopen("/dev/null", "w");
  179. shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
  180. if (!shell) {
  181. fprintf(stderr, "Cannot run clish.\n");
  182. return -1;
  183. }
  184. /* Load the XML files */
  185. clish_shell_load_scheme(shell, xml_path);
  186. /* Set communication to the konfd */
  187. clish_shell__set_socket(shell, socket_path);
  188. /* Set lockless mode */
  189. if (lockless)
  190. clish_shell__set_lockfile(shell, NULL);
  191. /* Set interactive mode */
  192. if (!interactive)
  193. clish_shell__set_interactive(shell, interactive);
  194. /* Set startup view */
  195. if (view)
  196. clish_shell__set_startup_view(shell, view);
  197. /* Set startup viewid */
  198. if (viewid)
  199. clish_shell__set_startup_viewid(shell, viewid);
  200. /* Set UTF-8 or 8-bit mode */
  201. if (utf8 || bit8)
  202. clish_shell__set_utf8(shell, utf8);
  203. else {
  204. #if HAVE_LANGINFO_CODESET
  205. /* Autodetect encoding */
  206. if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
  207. clish_shell__set_utf8(shell, BOOL_TRUE);
  208. #else
  209. /* The default is 8-bit if locale is not supported */
  210. clish_shell__set_utf8(shell, BOOL_FALSE);
  211. #endif
  212. }
  213. /* Set logging */
  214. if (log)
  215. clish_shell__set_log(shell, log);
  216. /* Set idle timeout */
  217. if (istimeout)
  218. clish_shell__set_timeout(shell, timeout);
  219. /* Set source of command stream: files or interactive tty */
  220. if(optind < argc) {
  221. int i;
  222. /* Run the commands from the files */
  223. for (i = argc - 1; i >= optind; i--)
  224. clish_shell_push_file(shell, argv[i], stop_on_error);
  225. } else {
  226. /* The interactive shell */
  227. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
  228. stop_on_error);
  229. }
  230. /* Execute startup */
  231. running = clish_shell_startup(shell);
  232. if (running) {
  233. fprintf(stderr, "Cannot startup clish.\n");
  234. clish_shell_delete(shell);
  235. return -1;
  236. }
  237. /* Main loop */
  238. result = clish_shell_loop(shell);
  239. /* Cleanup */
  240. clish_shell_delete(shell);
  241. if (quiet)
  242. fclose(outfd);
  243. return result;
  244. }
  245. /*--------------------------------------------------------- */
  246. /* Print help message */
  247. static void help(int status, const char *argv0)
  248. {
  249. const char *name = NULL;
  250. if (!argv0)
  251. return;
  252. /* Find the basename */
  253. name = strrchr(argv0, '/');
  254. if (name)
  255. name++;
  256. else
  257. name = argv0;
  258. if (status != 0) {
  259. fprintf(stderr, "Try `%s -h' for more information.\n",
  260. name);
  261. } else {
  262. printf("Usage: %s [options] [script_file] [script_file] ...\n", name);
  263. printf("CLI utility. Command line shell."
  264. "The part of the klish project.\n");
  265. printf("Options:\n");
  266. printf("\t-v, --version\tPrint version.\n");
  267. printf("\t-h, --help\tPrint this help.\n");
  268. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  269. "of the konfd daemon.\n");
  270. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  271. printf("\t-e, --stop-on-error\tStop script execution on error.\n");
  272. printf("\t-b, --background\tStart shell using non-interactive mode.\n");
  273. printf("\t-q, --quiet\tDisable echo while executing commands from the file stream.\n");
  274. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  275. printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
  276. printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
  277. printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
  278. printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
  279. printf("\t-8, --8bit\tForce 8-bit encoding.\n");
  280. printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
  281. printf("\t-k, --check\tCheck input files for syntax errors only.\n");
  282. printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
  283. }
  284. }