query.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 = 0x7f00;
  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("line=%s\n", query->line);
  59. printf("path=%s\n", query->path);
  60. printf("pwdc=%u\n", query->pwdc);
  61. }
  62. void konf_query_add_pwd(konf_query_t *query, char *str)
  63. {
  64. size_t new_size;
  65. char **tmp;
  66. if (!query)
  67. return;
  68. new_size = ((query->pwdc + 1) * sizeof(char *));
  69. /* resize the pwd vector */
  70. tmp = realloc(query->pwd, new_size);
  71. assert(tmp);
  72. query->pwd = tmp;
  73. /* insert reference to the pwd component */
  74. query->pwd[query->pwdc++] = lub_string_dup(str);
  75. }
  76. void konf_query_free(konf_query_t *query)
  77. {
  78. unsigned i;
  79. lub_string_free(query->pattern);
  80. lub_string_free(query->line);
  81. lub_string_free(query->path);
  82. if (query->pwdc > 0) {
  83. for (i = 0; i < query->pwdc; i++)
  84. lub_string_free(query->pwd[i]);
  85. free(query->pwd);
  86. }
  87. free(query);
  88. }
  89. /* Parse query */
  90. int konf_query_parse(konf_query_t *query, int argc, char **argv)
  91. {
  92. unsigned i = 0;
  93. int pwdc = 0;
  94. static const char *shortopts = "suoedtp:qn:r:l:f:i";
  95. /* static const struct option longopts[] = {
  96. {"set", 0, NULL, 's'},
  97. {"unset", 0, NULL, 'u'},
  98. {"ok", 0, NULL, 'o'},
  99. {"error", 0, NULL, 'e'},
  100. {"dump", 0, NULL, 'd'},
  101. {"stream", 0, NULL, 't'},
  102. {"priority", 1, NULL, 'p'},
  103. {"seq", 0, NULL, 'q'},
  104. {"seq_num", 1, NULL, 'n'},
  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. query->seq = BOOL_TRUE;
  152. break;
  153. case 'n':
  154. {
  155. long val = 0;
  156. char *endptr;
  157. val = strtol(optarg, &endptr, 0);
  158. if (endptr == optarg)
  159. break;
  160. if ((val > 0xffff) || (val < 0))
  161. break;
  162. query->seq_num = (unsigned short)val;
  163. break;
  164. }
  165. case 'r':
  166. query->pattern = lub_string_dup(optarg);
  167. break;
  168. case 'l':
  169. query->line = lub_string_dup(optarg);
  170. break;
  171. case 'f':
  172. query->path = lub_string_dup(optarg);
  173. break;
  174. case 'i':
  175. query->splitter = BOOL_FALSE;
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. /* Check options */
  182. if (KONF_QUERY_OP_NONE == query->op)
  183. return -1;
  184. if (KONF_QUERY_OP_SET == query->op) {
  185. if (NULL == query->pattern)
  186. return -1;
  187. if (NULL == query->line)
  188. return -1;
  189. }
  190. if ((pwdc = argc - optind) < 0)
  191. return -1;
  192. for (i = 0; i < pwdc; i ++)
  193. konf_query_add_pwd(query, argv[optind + i]);
  194. return 0;
  195. }
  196. /* Parse query string */
  197. int konf_query_parse_str(konf_query_t *query, char *str)
  198. {
  199. int res;
  200. lub_argv_t *lub_argv;
  201. char **str_argv;
  202. int str_argc;
  203. /* Make args from string */
  204. lub_argv = lub_argv_new(str, 0);
  205. str_argv = lub_argv__get_argv(lub_argv, "");
  206. str_argc = lub_argv__get_count(lub_argv) + 1;
  207. /* Parse query */
  208. res = konf_query_parse(query, str_argc, str_argv);
  209. free(str_argv);
  210. lub_argv_delete(lub_argv);
  211. return res;
  212. }
  213. char * konf_query__get_pwd(konf_query_t *query, unsigned index)
  214. {
  215. if (!query)
  216. return NULL;
  217. if (index >= query->pwdc)
  218. return NULL;
  219. return query->pwd[index];
  220. }
  221. int konf_query__get_pwdc(konf_query_t *query)
  222. {
  223. return query->pwdc;
  224. }
  225. konf_query_op_t konf_query__get_op(konf_query_t *query)
  226. {
  227. return query->op;
  228. }
  229. char * konf_query__get_path(konf_query_t *query)
  230. {
  231. return query->path;
  232. }
  233. const char * konf_query__get_pattern(konf_query_t *this)
  234. {
  235. return this->pattern;
  236. }
  237. const char * konf_query__get_line(konf_query_t *this)
  238. {
  239. return this->line;
  240. }
  241. unsigned short konf_query__get_priority(konf_query_t *this)
  242. {
  243. return this->priority;
  244. }
  245. bool_t konf_query__get_splitter(konf_query_t *this)
  246. {
  247. return this->splitter;
  248. }
  249. bool_t konf_query__get_seq(konf_query_t *this)
  250. {
  251. return this->seq;
  252. }
  253. unsigned short konf_query__get_seq_num(konf_query_t *this)
  254. {
  255. return this->seq_num;
  256. }