clish.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. static const char *shortopts = "hvs:led";
  43. /* static const struct option longopts[] = {
  44. {"help", 0, NULL, 'h'},
  45. {"version", 0, NULL, 'v'},
  46. {"socket", 1, NULL, 's'},
  47. {"lockless", 0, NULL, 'l'},
  48. {"stop-on-error", 0, NULL, 'e'},
  49. {"dry-run", 0, NULL, 'd'},
  50. {NULL, 0, NULL, 0}
  51. };
  52. */
  53. /* Parse command line options */
  54. optind = 0;
  55. while(1) {
  56. int opt;
  57. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  58. opt = getopt(argc, argv, shortopts);
  59. if (-1 == opt)
  60. break;
  61. switch (opt) {
  62. case 's':
  63. socket_path = optarg;
  64. break;
  65. case 'l':
  66. lockless = BOOL_TRUE;
  67. break;
  68. case 'e':
  69. stop_on_error = BOOL_TRUE;
  70. break;
  71. case 'd':
  72. my_hooks.script_fn = clish_dryrun_callback;
  73. break;
  74. case 'h':
  75. help(0, argv[0]);
  76. exit(0);
  77. break;
  78. case 'v':
  79. version(VERSION);
  80. exit(0);
  81. break;
  82. default:
  83. help(-1, argv[0]);
  84. exit(-1);
  85. break;
  86. }
  87. }
  88. shell = clish_shell_new(&my_hooks, NULL, NULL, stdout, stop_on_error);
  89. if (!shell) {
  90. fprintf(stderr, "Cannot run clish.\n");
  91. return -1;
  92. }
  93. /* Load the XML files */
  94. clish_shell_load_files(shell);
  95. /* Set communication to the konfd */
  96. clish_shell__set_socket(shell, socket_path);
  97. /* Set lockless mode */
  98. if (lockless)
  99. clish_shell__set_lockfile(shell, NULL);
  100. /* Execute startup */
  101. running = clish_shell_startup(shell);
  102. if (!running) {
  103. fprintf(stderr, "Cannot startup clish.\n");
  104. clish_shell_delete(shell);
  105. return -1;
  106. }
  107. if(optind < argc) {
  108. int i;
  109. /* Run the commands from the files */
  110. for (i = argc - 1; i >= optind; i--)
  111. clish_shell_push_file(shell, argv[i], stop_on_error);
  112. } else {
  113. /* The interactive shell */
  114. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"), stop_on_error);
  115. }
  116. result = clish_shell_loop(shell);
  117. /* Cleanup */
  118. clish_shell_delete(shell);
  119. return result ? 0 : -1;
  120. }
  121. /*--------------------------------------------------------- */
  122. /* Print help message */
  123. static void help(int status, const char *argv0)
  124. {
  125. const char *name = NULL;
  126. if (!argv0)
  127. return;
  128. /* Find the basename */
  129. name = strrchr(argv0, '/');
  130. if (name)
  131. name++;
  132. else
  133. name = argv0;
  134. if (status != 0) {
  135. fprintf(stderr, "Try `%s -h' for more information.\n",
  136. name);
  137. } else {
  138. printf("Usage: %s [options]\n", name);
  139. printf("CLI utility. "
  140. "The part of the klish project.\n");
  141. printf("Options:\n");
  142. printf("\t-v, --version\tPrint version.\n");
  143. printf("\t-h, --help\tPrint this help.\n");
  144. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  145. "of the konfd daemon.\n");
  146. printf("\t-l, --lockless\tDon't use locking mechanism.\n");
  147. printf("\t-e, --stop-on-error\tStop programm execution on error.\n");
  148. printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
  149. }
  150. }