clish.cpp 3.3 KB

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