1
0

query.c 6.3 KB

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