konf.c 3.5 KB

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