query.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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:qn: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", 0, NULL, 'q'},
  105. {"seq_num", 1, NULL, 'n'},
  106. {"pattern", 1, NULL, 'r'},
  107. {"line", 1, NULL, 'l'},
  108. {"file", 1, NULL, 'f'},
  109. {"splitter", 0, NULL, 'i'},
  110. {NULL, 0, NULL, 0}
  111. };
  112. */
  113. optind = 0;
  114. while(1) {
  115. int opt;
  116. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  117. opt = getopt(argc, argv, shortopts);
  118. if (-1 == opt)
  119. break;
  120. switch (opt) {
  121. case 'o':
  122. query->op = KONF_QUERY_OP_OK;
  123. break;
  124. case 'e':
  125. query->op = KONF_QUERY_OP_ERROR;
  126. break;
  127. case 's':
  128. query->op = KONF_QUERY_OP_SET;
  129. break;
  130. case 'u':
  131. query->op = KONF_QUERY_OP_UNSET;
  132. break;
  133. case 'd':
  134. query->op = KONF_QUERY_OP_DUMP;
  135. break;
  136. case 't':
  137. query->op = KONF_QUERY_OP_STREAM;
  138. break;
  139. case 'p':
  140. {
  141. long val = 0;
  142. char *endptr;
  143. val = strtol(optarg, &endptr, 0);
  144. if (endptr == optarg)
  145. break;
  146. if ((val > 0xffff) || (val < 0))
  147. break;
  148. query->priority = (unsigned short)val;
  149. break;
  150. }
  151. case 'q':
  152. query->seq = BOOL_TRUE;
  153. break;
  154. case 'n':
  155. {
  156. long val = 0;
  157. char *endptr;
  158. val = strtol(optarg, &endptr, 0);
  159. if (endptr == optarg)
  160. break;
  161. if ((val > 0xffff) || (val < 0))
  162. break;
  163. query->seq_num = (unsigned short)val;
  164. break;
  165. }
  166. case 'r':
  167. query->pattern = lub_string_dup(optarg);
  168. break;
  169. case 'l':
  170. query->line = lub_string_dup(optarg);
  171. break;
  172. case 'f':
  173. query->path = lub_string_dup(optarg);
  174. break;
  175. case 'i':
  176. query->splitter = BOOL_FALSE;
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. /* Check options */
  183. if (KONF_QUERY_OP_NONE == query->op)
  184. return -1;
  185. if (KONF_QUERY_OP_SET == query->op) {
  186. if (NULL == query->pattern)
  187. return -1;
  188. if (NULL == query->line)
  189. return -1;
  190. }
  191. if ((pwdc = argc - optind) < 0)
  192. return -1;
  193. for (i = 0; i < pwdc; i ++)
  194. konf_query_add_pwd(query, argv[optind + i]);
  195. return 0;
  196. }
  197. /* Parse query string */
  198. int konf_query_parse_str(konf_query_t *query, char *str)
  199. {
  200. int res;
  201. lub_argv_t *lub_argv;
  202. char **str_argv;
  203. int str_argc;
  204. /* Make args from string */
  205. lub_argv = lub_argv_new(str, 0);
  206. str_argv = lub_argv__get_argv(lub_argv, "");
  207. str_argc = lub_argv__get_count(lub_argv) + 1;
  208. /* Parse query */
  209. res = konf_query_parse(query, str_argc, str_argv);
  210. free(str_argv);
  211. lub_argv_delete(lub_argv);
  212. return res;
  213. }
  214. char * konf_query__get_pwd(konf_query_t *query, unsigned index)
  215. {
  216. if (!query)
  217. return NULL;
  218. if (index >= query->pwdc)
  219. return NULL;
  220. return query->pwd[index];
  221. }
  222. int konf_query__get_pwdc(konf_query_t *query)
  223. {
  224. return query->pwdc;
  225. }
  226. konf_query_op_t konf_query__get_op(konf_query_t *query)
  227. {
  228. return query->op;
  229. }
  230. char * konf_query__get_path(konf_query_t *query)
  231. {
  232. return query->path;
  233. }
  234. const char * konf_query__get_pattern(konf_query_t *this)
  235. {
  236. return this->pattern;
  237. }
  238. const char * konf_query__get_line(konf_query_t *this)
  239. {
  240. return this->line;
  241. }
  242. unsigned short konf_query__get_priority(konf_query_t *this)
  243. {
  244. return this->priority;
  245. }
  246. bool_t konf_query__get_splitter(konf_query_t *this)
  247. {
  248. return this->splitter;
  249. }
  250. bool_t konf_query__get_seq(konf_query_t *this)
  251. {
  252. return this->seq;
  253. }
  254. unsigned short konf_query__get_seq_num(konf_query_t *this)
  255. {
  256. return this->seq_num;
  257. }