clish.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = getenv("CLISH_VIEW");
  44. const char *viewid = getenv("CLISH_VIEWID");
  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. /* Create shell instance */
  104. shell = clish_shell_new(&my_hooks, NULL, NULL, stdout, stop_on_error);
  105. if (!shell) {
  106. fprintf(stderr, "Cannot run clish.\n");
  107. return -1;
  108. }
  109. /* Load the XML files */
  110. clish_shell_load_scheme(shell, xml_path);
  111. /* Set communication to the konfd */
  112. clish_shell__set_socket(shell, socket_path);
  113. /* Set lockless mode */
  114. if (lockless)
  115. clish_shell__set_lockfile(shell, NULL);
  116. /* Set startup view */
  117. if (view)
  118. clish_shell__set_startup_view(shell, view);
  119. /* Set startup viewid */
  120. if (viewid)
  121. clish_shell__set_startup_viewid(shell, viewid);
  122. /* Execute startup */
  123. running = clish_shell_startup(shell);
  124. if (!running) {
  125. fprintf(stderr, "Cannot startup clish.\n");
  126. clish_shell_delete(shell);
  127. return -1;
  128. }
  129. if(optind < argc) {
  130. int i;
  131. /* Run the commands from the files */
  132. for (i = argc - 1; i >= optind; i--)
  133. clish_shell_push_file(shell, argv[i], stop_on_error);
  134. } else {
  135. /* The interactive shell */
  136. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"), stop_on_error);
  137. }
  138. result = clish_shell_loop(shell);
  139. /* Cleanup */
  140. clish_shell_delete(shell);
  141. return result ? 0 : -1;
  142. }
  143. /*--------------------------------------------------------- */
  144. /* Print help message */
  145. static void help(int status, const char *argv0)
  146. {
  147. const char *name = NULL;
  148. if (!argv0)
  149. return;
  150. /* Find the basename */
  151. name = strrchr(argv0, '/');
  152. if (name)
  153. name++;
  154. else
  155. name = argv0;
  156. if (status != 0) {
  157. fprintf(stderr, "Try `%s -h' for more information.\n",
  158. name);
  159. } else {
  160. printf("Usage: %s [options]\n", name);
  161. printf("CLI utility. "
  162. "The part of the klish project.\n");
  163. printf("Options:\n");
  164. printf("\t-v, --version\tPrint version.\n");
  165. printf("\t-h, --help\tPrint this help.\n");
  166. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  167. "of the konfd daemon.\n");
  168. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  169. printf("\t-e, --stop-on-error\tStop programm execution on error.\n");
  170. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  171. printf("\t-x, --xml-path\tPath to XML scheme files.\n");
  172. printf("\t-w, --view\tSet the startup view.\n");
  173. printf("\t-i, --viewid\tSet the startup viewid.\n");
  174. }
  175. }