query.c 4.9 KB

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