clish.cpp 7.0 KB

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