clish.cpp 5.5 KB

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