1
0

clish.cpp 6.7 KB

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