clish.cpp 6.2 KB

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