query.c 6.1 KB

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