clish.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-------------------------------------
  2. // clish.cpp
  3. //
  4. // A simple client for libclish
  5. //-------------------------------------
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <getopt.h>
  10. #include "clish/shell.h"
  11. #include "clish/internal.h"
  12. #ifndef VERSION
  13. #define VERSION 1.2.2
  14. #endif
  15. #define QUOTE(t) #t
  16. #define version(v) printf("%s\n", QUOTE(v))
  17. static clish_shell_hooks_t my_hooks = {
  18. NULL, /* don't worry about init callback */
  19. clish_access_callback,
  20. NULL, /* don't worry about cmd_line callback */
  21. clish_script_callback,
  22. NULL, /* don't worry about fini callback */
  23. clish_config_callback,
  24. NULL /* don't register any builtin functions */
  25. };
  26. static void help(int status, const char *argv0);
  27. /*--------------------------------------------------------- */
  28. int main(int argc, char **argv)
  29. {
  30. bool_t running;
  31. int result = -1;
  32. clish_shell_t * shell;
  33. /* Command line options */
  34. const char *socket_path = KONFD_SOCKET_PATH;
  35. static const char *shortopts = "hvs:";
  36. /* static const struct option longopts[] = {
  37. {"help", 0, NULL, 'h'},
  38. {"version", 0, NULL, 'v'},
  39. {"socket", 1, NULL, 's'},
  40. {NULL, 0, NULL, 0}
  41. };
  42. */
  43. /* Parse command line options */
  44. optind = 0;
  45. while(1) {
  46. int opt;
  47. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  48. opt = getopt(argc, argv, shortopts);
  49. if (-1 == opt)
  50. break;
  51. switch (opt) {
  52. case 's':
  53. socket_path = optarg;
  54. break;
  55. case 'h':
  56. help(0, argv[0]);
  57. exit(0);
  58. break;
  59. case 'v':
  60. version(VERSION);
  61. exit(0);
  62. break;
  63. default:
  64. help(-1, argv[0]);
  65. exit(-1);
  66. break;
  67. }
  68. }
  69. shell = clish_shell_new(&my_hooks, NULL, stdin, stdout);
  70. if (!shell) {
  71. fprintf(stderr, "Cannot run clish.\n");
  72. return -1;
  73. }
  74. /* Load the XML files */
  75. clish_shell_load_files(shell);
  76. /* Set communication to the konfd */
  77. clish_shell__set_socket(shell, socket_path);
  78. /* Execute startup */
  79. running = clish_shell_startup(shell);
  80. if (!running) {
  81. fprintf(stderr, "Cannot startup clish.\n");
  82. clish_shell_delete(shell);
  83. return -1;
  84. }
  85. if(optind < argc) {
  86. int i;
  87. for (i = optind; i < argc; i++) {
  88. /* Run the commands from the file */
  89. result = clish_shell_from_file(shell, argv[i]);
  90. }
  91. } else {
  92. /* The interactive shell */
  93. result = clish_shell_loop(shell);
  94. }
  95. /* Cleanup */
  96. clish_shell_delete(shell);
  97. return result ? 0 : -1;
  98. }
  99. /*--------------------------------------------------------- */
  100. /* Print help message */
  101. static void help(int status, const char *argv0)
  102. {
  103. const char *name = NULL;
  104. if (!argv0)
  105. return;
  106. /* Find the basename */
  107. name = strrchr(argv0, '/');
  108. if (name)
  109. name++;
  110. else
  111. name = argv0;
  112. if (status != 0) {
  113. fprintf(stderr, "Try `%s -h' for more information.\n",
  114. name);
  115. } else {
  116. printf("Usage: %s [options]\n", name);
  117. printf("CLI utility. "
  118. "The part of the klish project.\n");
  119. printf("Options:\n");
  120. printf("\t-v, --version\tPrint version.\n");
  121. printf("\t-h, --help\tPrint this help.\n");
  122. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  123. "of the konfd daemon.\n");
  124. }
  125. }