query.c 6.2 KB

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