1
0

query.c 6.4 KB

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