clish.cpp 3.6 KB

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