konf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * konf.c
  3. *
  4. *
  5. * The client to communicate to konfd configuration daemon.
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifdef HAVE_GETOPT_H
  14. #include <getopt.h>
  15. #endif
  16. #include "konf/net.h"
  17. #include "konf/query.h"
  18. #include "konf/buf.h"
  19. #include "lub/string.h"
  20. #ifndef UNIX_PATH_MAX
  21. #define UNIX_PATH_MAX 108
  22. #endif
  23. #define MAXMSG 1024
  24. #ifndef VERSION
  25. #define VERSION 1.2.2
  26. #endif
  27. #define QUOTE(t) #t
  28. #define version(v) printf("%s\n", v)
  29. static void help(int status, const char *argv0);
  30. static const char *escape_chars = "\"\\'";
  31. /*--------------------------------------------------------- */
  32. int main(int argc, char **argv)
  33. {
  34. int res = -1;
  35. konf_client_t *client = NULL;
  36. konf_buf_t *buf = NULL;
  37. char *line = NULL;
  38. char *str = NULL;
  39. const char *socket_path = KONFD_SOCKET_PATH;
  40. unsigned i = 0;
  41. static const char *shortopts = "hvs:";
  42. #ifdef HAVE_GETOPT_H
  43. static const struct option longopts[] = {
  44. {"help", 0, NULL, 'h'},
  45. {"version", 0, NULL, 'v'},
  46. {"socket", 1, NULL, 's'},
  47. {NULL, 0, NULL, 0}
  48. };
  49. #endif
  50. /* Parse command line options */
  51. optind = 0;
  52. while(1) {
  53. int opt;
  54. #ifdef HAVE_GETOPT_H
  55. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  56. #else
  57. opt = getopt(argc, argv, shortopts);
  58. #endif
  59. if (-1 == opt)
  60. break;
  61. switch (opt) {
  62. case 's':
  63. socket_path = optarg;
  64. break;
  65. case 'h':
  66. help(0, argv[0]);
  67. exit(0);
  68. break;
  69. case 'v':
  70. version(VERSION);
  71. exit(0);
  72. break;
  73. default:
  74. help(-1, argv[0]);
  75. exit(-1);
  76. break;
  77. }
  78. }
  79. /* Get request line from the args */
  80. for (i = optind; i < argc; i++) {
  81. char *space = NULL;
  82. if (NULL != line)
  83. lub_string_cat(&line, " ");
  84. space = strchr(argv[i], ' ');
  85. if (space)
  86. lub_string_cat(&line, "\"");
  87. str = lub_string_encode(argv[i], escape_chars);
  88. lub_string_cat(&line, str);
  89. lub_string_free(str);
  90. if (space)
  91. lub_string_cat(&line, "\"");
  92. }
  93. if (!line) {
  94. help(-1, argv[0]);
  95. goto err;
  96. }
  97. #ifdef DEBUG
  98. printf("REQUEST: %s\n", line);
  99. #endif
  100. if (!(client = konf_client_new(socket_path))) {
  101. fprintf(stderr, "Can't create internal data structures.\n");
  102. goto err;
  103. }
  104. if (konf_client_connect(client) < 0) {
  105. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  106. goto err;
  107. }
  108. if (konf_client_send(client, line) < 0) {
  109. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  110. goto err;
  111. }
  112. if (konf_client_recv_answer(client, &buf) < 0) {
  113. fprintf(stderr, "The error while request to the konfd daemon.\n");
  114. }
  115. if (buf) {
  116. konf_buf_lseek(buf, 0);
  117. while ((str = konf_buf_preparse(buf))) {
  118. if (strlen(str) == 0) {
  119. lub_string_free(str);
  120. break;
  121. }
  122. fprintf(stdout, "%s\n", str);
  123. lub_string_free(str);
  124. }
  125. konf_buf_delete(buf);
  126. }
  127. res = 0;
  128. err:
  129. lub_string_free(line);
  130. konf_client_free(client);
  131. return res;
  132. }
  133. /*--------------------------------------------------------- */
  134. /* Print help message */
  135. static void help(int status, const char *argv0)
  136. {
  137. const char *name = NULL;
  138. if (!argv0)
  139. return;
  140. /* Find the basename */
  141. name = strrchr(argv0, '/');
  142. if (name)
  143. name++;
  144. else
  145. name = argv0;
  146. if (status != 0) {
  147. fprintf(stderr, "Try `%s -h' for more information.\n",
  148. name);
  149. } else {
  150. printf("Usage: %s [options] -- <command for konfd daemon>\n", name);
  151. printf("Utility for communication to the konfd "
  152. "configuration daemon.\n");
  153. printf("Options:\n");
  154. printf("\t-v, --version\tPrint utility version.\n");
  155. printf("\t-h, --help\tPrint this help.\n");
  156. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  157. "of the konfd daemon.\n");
  158. }
  159. }