query.c 5.4 KB

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