query.c 4.8 KB

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