query.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <errno.h>
  8. #include <assert.h>
  9. #ifdef HAVE_GETOPT_H
  10. #include <getopt.h>
  11. #endif
  12. #include "lub/types.h"
  13. #include "lub/argv.h"
  14. #include "lub/string.h"
  15. #include "private.h"
  16. /*-------------------------------------------------------- */
  17. konf_query_t *konf_query_new(void)
  18. {
  19. konf_query_t *this;
  20. if (!(this = malloc(sizeof(*this))))
  21. return NULL;
  22. this->op = KONF_QUERY_OP_NONE;
  23. this->pattern = NULL;
  24. this->priority = 0;
  25. this->seq = BOOL_FALSE;
  26. this->seq_num = 0;
  27. this->pwdc = 0;
  28. this->pwd = NULL;
  29. this->line = NULL;
  30. this->lower_line = NULL;
  31. this->path = NULL;
  32. this->splitter = BOOL_TRUE;
  33. this->unique = BOOL_TRUE;
  34. this->depth = -1;
  35. return this;
  36. }
  37. /*-------------------------------------------------------- */
  38. void konf_query_add_pwd(konf_query_t *this, char *str)
  39. {
  40. size_t new_size;
  41. char **tmp;
  42. if (!this)
  43. return;
  44. new_size = ((this->pwdc + 1) * sizeof(char *));
  45. /* resize the pwd vector */
  46. tmp = realloc(this->pwd, new_size);
  47. assert(tmp);
  48. this->pwd = tmp;
  49. /* insert reference to the pwd component */
  50. this->pwd[this->pwdc++] = strdup(str);
  51. }
  52. /*-------------------------------------------------------- */
  53. void konf_query_free(konf_query_t *this)
  54. {
  55. unsigned i;
  56. free(this->pattern);
  57. free(this->line);
  58. free(this->lower_line);
  59. free(this->path);
  60. if (this->pwdc > 0) {
  61. for (i = 0; i < this->pwdc; i++)
  62. free(this->pwd[i]);
  63. free(this->pwd);
  64. }
  65. free(this);
  66. }
  67. /*-------------------------------------------------------- */
  68. /* Parse query */
  69. int konf_query_parse(konf_query_t *this, int argc, char **argv)
  70. {
  71. unsigned i = 0;
  72. int pwdc = 0;
  73. static const char *shortopts = "suoedtp:q:r:l:f:inh:";
  74. #ifdef HAVE_GETOPT_H
  75. static const struct option longopts[] = {
  76. {"set", 0, NULL, 's'},
  77. {"unset", 0, NULL, 'u'},
  78. {"ok", 0, NULL, 'o'},
  79. {"error", 0, NULL, 'e'},
  80. {"dump", 0, NULL, 'd'},
  81. {"stream", 0, NULL, 't'},
  82. {"priority", 1, NULL, 'p'},
  83. {"seq", 1, NULL, 'q'},
  84. {"pattern", 1, NULL, 'r'},
  85. {"line", 1, NULL, 'l'},
  86. {"file", 1, NULL, 'f'},
  87. {"splitter", 0, NULL, 'i'},
  88. {"non-unique", 0, NULL, 'n'},
  89. {"depth", 1, NULL, 'h'},
  90. {NULL, 0, NULL, 0}
  91. };
  92. #endif
  93. optind = 0;
  94. while(1) {
  95. int opt;
  96. #ifdef HAVE_GETOPT_H
  97. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  98. #else
  99. opt = getopt(argc, argv, shortopts);
  100. #endif
  101. if (-1 == opt)
  102. break;
  103. switch (opt) {
  104. case 'o':
  105. this->op = KONF_QUERY_OP_OK;
  106. break;
  107. case 'e':
  108. this->op = KONF_QUERY_OP_ERROR;
  109. break;
  110. case 's':
  111. this->op = KONF_QUERY_OP_SET;
  112. break;
  113. case 'u':
  114. this->op = KONF_QUERY_OP_UNSET;
  115. break;
  116. case 'd':
  117. this->op = KONF_QUERY_OP_DUMP;
  118. break;
  119. case 't':
  120. this->op = KONF_QUERY_OP_STREAM;
  121. break;
  122. case 'p':
  123. {
  124. long val = 0;
  125. char *endptr;
  126. val = strtol(optarg, &endptr, 0);
  127. if (endptr == optarg)
  128. break;
  129. if ((val > 0xffff) || (val < 0))
  130. break;
  131. this->priority = (unsigned short)val;
  132. break;
  133. }
  134. case 'q':
  135. {
  136. long val = 0;
  137. char *endptr;
  138. this->seq = BOOL_TRUE;
  139. val = strtol(optarg, &endptr, 0);
  140. if (endptr == optarg)
  141. break;
  142. if ((val > 0xffff) || (val < 0))
  143. break;
  144. this->seq_num = (unsigned short)val;
  145. break;
  146. }
  147. case 'r':
  148. this->pattern = strdup(optarg);
  149. break;
  150. case 'l':
  151. this->line = strdup(optarg);
  152. this->lower_line = lub_string_tolower(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. const char * konf_query__get_lower_line(konf_query_t *this)
  248. {
  249. return this->lower_line;
  250. }
  251. /*-------------------------------------------------------- */
  252. unsigned short konf_query__get_priority(konf_query_t *this)
  253. {
  254. return this->priority;
  255. }
  256. /*-------------------------------------------------------- */
  257. bool_t konf_query__get_splitter(konf_query_t *this)
  258. {
  259. return this->splitter;
  260. }
  261. /*-------------------------------------------------------- */
  262. bool_t konf_query__get_seq(konf_query_t *this)
  263. {
  264. return this->seq;
  265. }
  266. /*-------------------------------------------------------- */
  267. unsigned short konf_query__get_seq_num(konf_query_t *this)
  268. {
  269. return this->seq_num;
  270. }
  271. /*-------------------------------------------------------- */
  272. bool_t konf_query__get_unique(konf_query_t *this)
  273. {
  274. return this->unique;
  275. }
  276. /*-------------------------------------------------------- */
  277. int konf_query__get_depth(konf_query_t *this)
  278. {
  279. return this->depth;
  280. }