query.c 6.5 KB

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