konf.c 3.4 KB

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