clish.cpp 5.2 KB

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