konf.c 3.5 KB

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