1
0

konf.c 3.5 KB

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