1
0

clish.cpp 4.6 KB

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