1
0

query.c 5.7 KB

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