query.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <errno.h>
  7. #include <assert.h>
  8. #ifdef HAVE_GETOPT_H
  9. #include <getopt.h>
  10. #endif
  11. #include "lub/types.h"
  12. #include "lub/argv.h"
  13. #include "lub/string.h"
  14. #include "private.h"
  15. /*-------------------------------------------------------- */
  16. konf_query_t *konf_query_new(void)
  17. {
  18. konf_query_t *this;
  19. if (!(this = malloc(sizeof(*this))))
  20. return NULL;
  21. this->op = KONF_QUERY_OP_NONE;
  22. this->pattern = NULL;
  23. this->priority = 0;
  24. this->seq = BOOL_FALSE;
  25. this->seq_num = 0;
  26. this->pwdc = 0;
  27. this->pwd = NULL;
  28. this->line = NULL;
  29. this->path = NULL;
  30. this->splitter = BOOL_TRUE;
  31. this->unique = BOOL_TRUE;
  32. return this;
  33. }
  34. /*-------------------------------------------------------- */
  35. void konf_query_add_pwd(konf_query_t *this, char *str)
  36. {
  37. size_t new_size;
  38. char **tmp;
  39. if (!this)
  40. return;
  41. new_size = ((this->pwdc + 1) * sizeof(char *));
  42. /* resize the pwd vector */
  43. tmp = realloc(this->pwd, new_size);
  44. assert(tmp);
  45. this->pwd = tmp;
  46. /* insert reference to the pwd component */
  47. this->pwd[this->pwdc++] = lub_string_dup(str);
  48. }
  49. /*-------------------------------------------------------- */
  50. void konf_query_free(konf_query_t *this)
  51. {
  52. unsigned i;
  53. lub_string_free(this->pattern);
  54. lub_string_free(this->line);
  55. lub_string_free(this->path);
  56. if (this->pwdc > 0) {
  57. for (i = 0; i < this->pwdc; i++)
  58. lub_string_free(this->pwd[i]);
  59. free(this->pwd);
  60. }
  61. free(this);
  62. }
  63. /*-------------------------------------------------------- */
  64. /* Parse query */
  65. int konf_query_parse(konf_query_t *this, int argc, char **argv)
  66. {
  67. unsigned i = 0;
  68. int pwdc = 0;
  69. static const char *shortopts = "suoedtp:q:r:l:f:in";
  70. #ifdef HAVE_GETOPT_H
  71. static const struct option longopts[] = {
  72. {"set", 0, NULL, 's'},
  73. {"unset", 0, NULL, 'u'},
  74. {"ok", 0, NULL, 'o'},
  75. {"error", 0, NULL, 'e'},
  76. {"dump", 0, NULL, 'd'},
  77. {"stream", 0, NULL, 't'},
  78. {"priority", 1, NULL, 'p'},
  79. {"seq", 1, NULL, 'q'},
  80. {"pattern", 1, NULL, 'r'},
  81. {"line", 1, NULL, 'l'},
  82. {"file", 1, NULL, 'f'},
  83. {"splitter", 0, NULL, 'i'},
  84. {"non-unique", 0, NULL, 'n'},
  85. {NULL, 0, NULL, 0}
  86. };
  87. #endif
  88. optind = 0;
  89. while(1) {
  90. int opt;
  91. #ifdef HAVE_GETOPT_H
  92. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  93. #else
  94. opt = getopt(argc, argv, shortopts);
  95. #endif
  96. if (-1 == opt)
  97. break;
  98. switch (opt) {
  99. case 'o':
  100. this->op = KONF_QUERY_OP_OK;
  101. break;
  102. case 'e':
  103. this->op = KONF_QUERY_OP_ERROR;
  104. break;
  105. case 's':
  106. this->op = KONF_QUERY_OP_SET;
  107. break;
  108. case 'u':
  109. this->op = KONF_QUERY_OP_UNSET;
  110. break;
  111. case 'd':
  112. this->op = KONF_QUERY_OP_DUMP;
  113. break;
  114. case 't':
  115. this->op = KONF_QUERY_OP_STREAM;
  116. break;
  117. case 'p':
  118. {
  119. long val = 0;
  120. char *endptr;
  121. val = strtol(optarg, &endptr, 0);
  122. if (endptr == optarg)
  123. break;
  124. if ((val > 0xffff) || (val < 0))
  125. break;
  126. this->priority = (unsigned short)val;
  127. break;
  128. }
  129. case 'q':
  130. {
  131. long val = 0;
  132. char *endptr;
  133. this->seq = BOOL_TRUE;
  134. val = strtol(optarg, &endptr, 0);
  135. if (endptr == optarg)
  136. break;
  137. if ((val > 0xffff) || (val < 0))
  138. break;
  139. this->seq_num = (unsigned short)val;
  140. break;
  141. }
  142. case 'r':
  143. this->pattern = lub_string_dup(optarg);
  144. break;
  145. case 'l':
  146. this->line = lub_string_dup(optarg);
  147. break;
  148. case 'f':
  149. this->path = lub_string_dup(optarg);
  150. break;
  151. case 'i':
  152. this->splitter = BOOL_FALSE;
  153. break;
  154. case 'n':
  155. this->unique = BOOL_FALSE;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. /* Check options */
  162. if (KONF_QUERY_OP_NONE == this->op)
  163. return -1;
  164. if (KONF_QUERY_OP_SET == this->op) {
  165. if (!this->pattern)
  166. return -1;
  167. if (!this->line)
  168. return -1;
  169. }
  170. if ((pwdc = argc - optind) < 0)
  171. return -1;
  172. for (i = 0; i < pwdc; i ++)
  173. konf_query_add_pwd(this, argv[optind + i]);
  174. return 0;
  175. }
  176. /*-------------------------------------------------------- */
  177. /* Parse query string */
  178. int konf_query_parse_str(konf_query_t *this, char *str)
  179. {
  180. int res;
  181. lub_argv_t *lub_argv;
  182. char **str_argv;
  183. int str_argc;
  184. /* Make args from string */
  185. lub_argv = lub_argv_new(str, 0);
  186. str_argv = lub_argv__get_argv(lub_argv, "");
  187. str_argc = lub_argv__get_count(lub_argv) + 1;
  188. /* Parse query */
  189. res = konf_query_parse(this, str_argc, str_argv);
  190. lub_argv__free_argv(str_argv);
  191. lub_argv_delete(lub_argv);
  192. return res;
  193. }
  194. /*-------------------------------------------------------- */
  195. char * konf_query__get_pwd(konf_query_t *this, unsigned index)
  196. {
  197. if (!this)
  198. return NULL;
  199. if (index >= this->pwdc)
  200. return NULL;
  201. return this->pwd[index];
  202. }
  203. /*-------------------------------------------------------- */
  204. int konf_query__get_pwdc(konf_query_t *this)
  205. {
  206. return this->pwdc;
  207. }
  208. /*-------------------------------------------------------- */
  209. konf_query_op_t konf_query__get_op(konf_query_t *this)
  210. {
  211. return this->op;
  212. }
  213. /*-------------------------------------------------------- */
  214. char * konf_query__get_path(konf_query_t *this)
  215. {
  216. return this->path;
  217. }
  218. /*-------------------------------------------------------- */
  219. const char * konf_query__get_pattern(konf_query_t *this)
  220. {
  221. return this->pattern;
  222. }
  223. /*-------------------------------------------------------- */
  224. const char * konf_query__get_line(konf_query_t *this)
  225. {
  226. return this->line;
  227. }
  228. /*-------------------------------------------------------- */
  229. unsigned short konf_query__get_priority(konf_query_t *this)
  230. {
  231. return this->priority;
  232. }
  233. /*-------------------------------------------------------- */
  234. bool_t konf_query__get_splitter(konf_query_t *this)
  235. {
  236. return this->splitter;
  237. }
  238. /*-------------------------------------------------------- */
  239. bool_t konf_query__get_seq(konf_query_t *this)
  240. {
  241. return this->seq;
  242. }
  243. /*-------------------------------------------------------- */
  244. unsigned short konf_query__get_seq_num(konf_query_t *this)
  245. {
  246. return this->seq_num;
  247. }
  248. /*-------------------------------------------------------- */
  249. bool_t konf_query__get_unique(konf_query_t *this)
  250. {
  251. return this->unique;
  252. }