1
0

query.c 5.8 KB

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