1
0

clish.cpp 6.5 KB

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