1
0

query.c 6.1 KB

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