query.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * clish_config_callback.c
  3. *
  4. *
  5. * Callback hook to execute config operations.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <errno.h>
  13. #include <assert.h>
  14. #include "clish/private.h"
  15. #include "cliconf/query.h"
  16. #include "lub/argv.h"
  17. #include "lub/string.h"
  18. query_t *query_new(void)
  19. {
  20. query_t *query;
  21. if (!(query = malloc(sizeof(*query))))
  22. return NULL;
  23. query->op = QUERY_OP_NONE;
  24. query->pattern = NULL;
  25. query->priority = 0x7f00;
  26. query->pwdc = 0;
  27. query->pwd = NULL;
  28. query->line = NULL;
  29. query->path = NULL;
  30. query->splitter = BOOL_TRUE;
  31. return query;
  32. }
  33. void query_dump(query_t *query)
  34. {
  35. switch (query->op) {
  36. case QUERY_OP_SET:
  37. printf("op=SET\n");
  38. break;
  39. case QUERY_OP_UNSET:
  40. printf("op=UNSET\n");
  41. break;
  42. case QUERY_OP_DUMP:
  43. printf("op=DUMP\n");
  44. break;
  45. case QUERY_OP_OK:
  46. printf("op=OK\n");
  47. break;
  48. case QUERY_OP_ERROR:
  49. printf("op=ERROR\n");
  50. break;
  51. case QUERY_OP_STREAM:
  52. printf("op=STREAM\n");
  53. break;
  54. default:
  55. printf("op=UNKNOWN\n");
  56. break;
  57. }
  58. printf("pattern=%s\n", query->pattern);
  59. printf("priority=%u\n", query->priority);
  60. printf("line=%s\n", query->line);
  61. printf("path=%s\n", query->path);
  62. printf("pwdc=%u\n", query->pwdc);
  63. }
  64. void query_add_pwd(query_t *query, char *str)
  65. {
  66. size_t new_size;
  67. char **tmp;
  68. if (!query)
  69. return;
  70. new_size = ((query->pwdc + 1) * sizeof(char *));
  71. /* resize the pwd vector */
  72. tmp = realloc(query->pwd, new_size);
  73. assert(tmp);
  74. query->pwd = tmp;
  75. /* insert reference to the pwd component */
  76. query->pwd[query->pwdc++] = lub_string_dup(str);
  77. }
  78. char * query__get_pwd(query_t *query, unsigned index)
  79. {
  80. if (!query)
  81. return NULL;
  82. if (index >= query->pwdc)
  83. return NULL;
  84. return query->pwd[index];
  85. }
  86. int query__get_pwdc(query_t *query)
  87. {
  88. return query->pwdc;
  89. }
  90. query_op_t query__get_op(query_t *query)
  91. {
  92. return query->op;
  93. }
  94. char * query__get_path(query_t *query)
  95. {
  96. return query->path;
  97. }
  98. void query_free(query_t *query)
  99. {
  100. unsigned i;
  101. lub_string_free(query->pattern);
  102. lub_string_free(query->line);
  103. lub_string_free(query->path);
  104. if (query->pwdc > 0) {
  105. for (i = 0; i < query->pwdc; i++)
  106. lub_string_free(query->pwd[i]);
  107. free(query->pwd);
  108. }
  109. free(query);
  110. }
  111. /* Parse query */
  112. int query_parse(query_t *query, int argc, char **argv)
  113. {
  114. unsigned i = 0;
  115. int pwdc = 0;
  116. static const char *shortopts = "suoedtp:r:l:f:i";
  117. /* static const struct option longopts[] = {
  118. {"set", 0, NULL, 's'},
  119. {"unset", 0, NULL, 'u'},
  120. {"ok", 0, NULL, 'o'},
  121. {"error", 0, NULL, 'e'},
  122. {"dump", 0, NULL, 'd'},
  123. {"stream", 0, NULL, 't'},
  124. {"priority", 1, NULL, 'p'},
  125. {"pattern", 1, NULL, 'r'},
  126. {"line", 1, NULL, 'l'},
  127. {"file", 1, NULL, 'f'},
  128. {"splitter", 0, NULL, 'i'},
  129. {NULL, 0, NULL, 0}
  130. };
  131. */
  132. optind = 0;
  133. while(1) {
  134. int opt;
  135. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  136. opt = getopt(argc, argv, shortopts);
  137. if (-1 == opt)
  138. break;
  139. switch (opt) {
  140. case 'o':
  141. query->op = QUERY_OP_OK;
  142. break;
  143. case 'e':
  144. query->op = QUERY_OP_ERROR;
  145. break;
  146. case 's':
  147. query->op = QUERY_OP_SET;
  148. break;
  149. case 'u':
  150. query->op = QUERY_OP_UNSET;
  151. break;
  152. case 'd':
  153. query->op = QUERY_OP_DUMP;
  154. break;
  155. case 't':
  156. query->op = QUERY_OP_STREAM;
  157. break;
  158. case 'p':
  159. {
  160. long val = 0;
  161. char *endptr;
  162. unsigned short pri;
  163. val = strtol(optarg, &endptr, 0);
  164. if (endptr == optarg)
  165. break;
  166. if ((val > 0xffff) || (val < 0))
  167. break;
  168. pri = (unsigned short)val;
  169. query->priority = pri;
  170. break;
  171. }
  172. case 'r':
  173. query->pattern = lub_string_dup(optarg);
  174. break;
  175. case 'l':
  176. query->line = lub_string_dup(optarg);
  177. break;
  178. case 'f':
  179. query->path = lub_string_dup(optarg);
  180. break;
  181. case 'i':
  182. query->splitter = BOOL_FALSE;
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. /* Check options */
  189. if (QUERY_OP_NONE == query->op)
  190. return -1;
  191. if (QUERY_OP_SET == query->op) {
  192. if (NULL == query->pattern)
  193. return -1;
  194. if (NULL == query->line)
  195. return -1;
  196. }
  197. if ((pwdc = argc - optind) < 0)
  198. return -1;
  199. for (i = 0; i < pwdc; i ++)
  200. query_add_pwd(query, argv[optind + i]);
  201. return 0;
  202. }
  203. /* Parse query string */
  204. int query_parse_str(query_t *query, char *str)
  205. {
  206. int res;
  207. lub_argv_t *lub_argv;
  208. char **str_argv;
  209. int str_argc;
  210. /* Make args from string */
  211. lub_argv = lub_argv_new(str, 0);
  212. str_argv = lub_argv__get_argv(lub_argv, "");
  213. str_argc = lub_argv__get_count(lub_argv) + 1;
  214. /* Parse query */
  215. res = query_parse(query, str_argc, str_argv);
  216. free(str_argv);
  217. lub_argv_delete(lub_argv);
  218. return res;
  219. }
  220. const char * query__get_pattern(query_t *this)
  221. {
  222. return this->pattern;
  223. }