clish.cpp 3.4 KB

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