1
0

clish.cpp 6.5 KB

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