clish.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <getopt.h>
  13. #include <unistd.h>
  14. #include "clish/shell.h"
  15. #include "clish/internal.h"
  16. #ifndef VERSION
  17. #define VERSION 1.2.2
  18. #endif
  19. #define QUOTE(t) #t
  20. /* #define version(v) printf("%s\n", QUOTE(v)) */
  21. #define version(v) printf("%s\n", v)
  22. static clish_shell_hooks_t my_hooks = {
  23. NULL, /* don't worry about init callback */
  24. clish_access_callback,
  25. NULL, /* don't worry about cmd_line callback */
  26. clish_script_callback,
  27. NULL, /* don't worry about fini callback */
  28. clish_config_callback,
  29. NULL /* don't register any builtin functions */
  30. };
  31. static void help(int status, const char *argv0);
  32. /*--------------------------------------------------------- */
  33. int main(int argc, char **argv)
  34. {
  35. bool_t running;
  36. int result = -1;
  37. clish_shell_t * shell;
  38. /* Command line options */
  39. const char *socket_path = KONFD_SOCKET_PATH;
  40. bool_t lockless = BOOL_FALSE;
  41. bool_t stop_on_error = BOOL_FALSE;
  42. const char *xml_path = getenv("CLISH_PATH");
  43. const char *view = NULL;
  44. const char *viewid = NULL;
  45. static const char *shortopts = "hvs:ledx:w:i:";
  46. /* static const struct option longopts[] = {
  47. {"help", 0, NULL, 'h'},
  48. {"version", 0, NULL, 'v'},
  49. {"socket", 1, NULL, 's'},
  50. {"lockless", 0, NULL, 'l'},
  51. {"stop-on-error", 0, NULL, 'e'},
  52. {"dry-run", 0, NULL, 'd'},
  53. {"xml-path", 1, NULL, 'x'},
  54. {"view", 1, NULL, 'w'},
  55. {"viewid", 1, NULL, 'i'},
  56. {NULL, 0, NULL, 0}
  57. };
  58. */
  59. /* Parse command line options */
  60. optind = 0;
  61. while(1) {
  62. int opt;
  63. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  64. opt = getopt(argc, argv, shortopts);
  65. if (-1 == opt)
  66. break;
  67. switch (opt) {
  68. case 's':
  69. socket_path = optarg;
  70. break;
  71. case 'l':
  72. lockless = BOOL_TRUE;
  73. break;
  74. case 'e':
  75. stop_on_error = BOOL_TRUE;
  76. break;
  77. case 'd':
  78. my_hooks.script_fn = clish_dryrun_callback;
  79. break;
  80. case 'x':
  81. xml_path = optarg;
  82. break;
  83. case 'w':
  84. view = optarg;
  85. break;
  86. case 'i':
  87. viewid = optarg;
  88. break;
  89. case 'h':
  90. help(0, argv[0]);
  91. exit(0);
  92. break;
  93. case 'v':
  94. version(VERSION);
  95. exit(0);
  96. break;
  97. default:
  98. help(-1, argv[0]);
  99. exit(-1);
  100. break;
  101. }
  102. }
  103. shell = clish_shell_new(&my_hooks, NULL, NULL, stdout, stop_on_error);
  104. if (!shell) {
  105. fprintf(stderr, "Cannot run clish.\n");
  106. return -1;
  107. }
  108. /* Load the XML files */
  109. clish_shell_load_scheme(shell, xml_path);
  110. /* Set communication to the konfd */
  111. clish_shell__set_socket(shell, socket_path);
  112. /* Set lockless mode */
  113. if (lockless)
  114. clish_shell__set_lockfile(shell, NULL);
  115. /* Execute startup */
  116. running = clish_shell_startup(shell);
  117. if (!running) {
  118. fprintf(stderr, "Cannot startup clish.\n");
  119. clish_shell_delete(shell);
  120. return -1;
  121. }
  122. if(optind < argc) {
  123. int i;
  124. /* Run the commands from the files */
  125. for (i = argc - 1; i >= optind; i--)
  126. clish_shell_push_file(shell, argv[i], stop_on_error);
  127. } else {
  128. /* The interactive shell */
  129. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"), stop_on_error);
  130. }
  131. result = clish_shell_loop(shell);
  132. /* Cleanup */
  133. clish_shell_delete(shell);
  134. return result ? 0 : -1;
  135. }
  136. /*--------------------------------------------------------- */
  137. /* Print help message */
  138. static void help(int status, const char *argv0)
  139. {
  140. const char *name = NULL;
  141. if (!argv0)
  142. return;
  143. /* Find the basename */
  144. name = strrchr(argv0, '/');
  145. if (name)
  146. name++;
  147. else
  148. name = argv0;
  149. if (status != 0) {
  150. fprintf(stderr, "Try `%s -h' for more information.\n",
  151. name);
  152. } else {
  153. printf("Usage: %s [options]\n", name);
  154. printf("CLI utility. "
  155. "The part of the klish project.\n");
  156. printf("Options:\n");
  157. printf("\t-v, --version\tPrint version.\n");
  158. printf("\t-h, --help\tPrint this help.\n");
  159. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  160. "of the konfd daemon.\n");
  161. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  162. printf("\t-e, --stop-on-error\tStop programm execution on error.\n");
  163. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  164. }
  165. }