pline.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /** @file pline.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <sysrepo.h>
  9. #include <sysrepo/xpath.h>
  10. #include <libyang/tree_edit.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include <faux/list.h>
  14. #include <faux/argv.h>
  15. #include "pline.h"
  16. #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
  17. pexpr_t *pexpr_new(void)
  18. {
  19. pexpr_t *pexpr = NULL;
  20. pexpr = faux_zmalloc(sizeof(*pexpr));
  21. assert(pexpr);
  22. if (!pexpr)
  23. return NULL;
  24. return pexpr;
  25. }
  26. void pexpr_free(pexpr_t *pexpr)
  27. {
  28. if (!pexpr)
  29. return;
  30. faux_str_free(pexpr->xpath);
  31. faux_str_free(pexpr->value);
  32. free(pexpr);
  33. }
  34. pcompl_t *pcompl_new(void)
  35. {
  36. pcompl_t *pcompl = NULL;
  37. pcompl = faux_zmalloc(sizeof(*pcompl));
  38. assert(pcompl);
  39. if (!pcompl)
  40. return NULL;
  41. return pcompl;
  42. }
  43. void pcompl_free(pcompl_t *pcompl)
  44. {
  45. if (!pcompl)
  46. return;
  47. faux_str_free(pcompl->xpath);
  48. free(pcompl);
  49. }
  50. pline_t *pline_new(void)
  51. {
  52. pline_t *pline = NULL;
  53. pline = faux_zmalloc(sizeof(*pline));
  54. assert(pline);
  55. if (!pline)
  56. return NULL;
  57. // Init
  58. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  59. NULL, NULL, (faux_list_free_fn)pexpr_free);
  60. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  61. NULL, NULL, (faux_list_free_fn)pcompl_free);
  62. return pline;
  63. }
  64. void pline_free(pline_t *pline)
  65. {
  66. LY_ARRAY_COUNT_TYPE u = 0;
  67. if (!pline)
  68. return;
  69. faux_list_free(pline->exprs);
  70. faux_list_free(pline->compls);
  71. faux_free(pline);
  72. }
  73. pexpr_t *pline_current_expr(pline_t *pline)
  74. {
  75. assert(pline);
  76. if (faux_list_len(pline->exprs) == 0)
  77. faux_list_add(pline->exprs, pexpr_new());
  78. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  79. }
  80. static int
  81. sr_ly_module_is_internal(const struct lys_module *ly_mod);
  82. int
  83. sr_module_is_internal(const struct lys_module *ly_mod);
  84. // Don't use standard lys_find_child() because it checks given module to be
  85. // equal to found node's module. So augmented nodes will not be found.
  86. static const struct lysc_node *find_child(const struct lysc_node *node,
  87. const char *name)
  88. {
  89. const struct lysc_node *iter = NULL;
  90. if (!node)
  91. return NULL;
  92. LY_LIST_FOR(node, iter) {
  93. if (!(iter->nodetype & NODETYPE_CONF))
  94. continue;
  95. if (!(iter->flags & LYS_CONFIG_W))
  96. continue;
  97. if (!faux_str_cmp(iter->name, name))
  98. return iter;
  99. }
  100. return NULL;
  101. }
  102. bool_t parse_module(const struct lys_module *module, faux_argv_node_t **arg,
  103. pline_t *pline)
  104. {
  105. const struct lysc_node *node = NULL;
  106. do {
  107. pexpr_t *pexpr = pline_current_expr(pline);
  108. const char *str = (const char *)faux_argv_current(*arg);
  109. if (node) {
  110. char *tmp = faux_str_sprintf("/%s:%s",
  111. node->module->name, node->name);
  112. faux_str_cat(&pexpr->xpath, tmp);
  113. faux_str_free(tmp);
  114. printf("%s\n", pexpr->xpath);
  115. }
  116. printf("for str = %s\n", str);
  117. if (!str)
  118. break;
  119. // Root of the module
  120. if (!node) {
  121. printf("Module\n");
  122. node = find_child(module->compiled->data, str);
  123. if (!node)
  124. return BOOL_FALSE;
  125. // continue; // Don't get next arg
  126. // Container
  127. } else if (node->nodetype & LYS_CONTAINER) {
  128. printf("Container\n");
  129. node = find_child(lysc_node_child(node), str);
  130. // List
  131. } else if (node->nodetype & LYS_LIST) {
  132. printf("List\n");
  133. const struct lysc_node *iter = NULL;
  134. printf("str = %s\n", str);
  135. LY_LIST_FOR(lysc_node_child(node), iter) {
  136. char *tmp = NULL;
  137. struct lysc_node_leaf *leaf =
  138. (struct lysc_node_leaf *)iter;
  139. if (!(iter->nodetype & LYS_LEAF))
  140. continue;
  141. if (!(iter->flags & LYS_KEY))
  142. continue;
  143. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  144. tmp = faux_str_sprintf("[%s='%s']",
  145. leaf->name, str);
  146. faux_str_cat(&pexpr->xpath, tmp);
  147. faux_str_free(tmp);
  148. printf("%s\n", pexpr->xpath);
  149. faux_argv_each(arg);
  150. str = (const char *)faux_argv_current(*arg);
  151. printf("list str = %s\n", str);
  152. if (!str)
  153. break;
  154. }
  155. if (!str)
  156. break;
  157. node = find_child(lysc_node_child(node), str);
  158. printf("list next node = %s\n", node ? node->name : "NULL");
  159. // Leaf
  160. } else if (node->nodetype & LYS_LEAF) {
  161. printf("Leaf\n");
  162. struct lysc_node_leaf *leaf =
  163. (struct lysc_node_leaf *)node;
  164. if (leaf->type->basetype != LY_TYPE_EMPTY) {
  165. pexpr->value = faux_str_dup(str);
  166. printf("value=%s\n", pexpr->value);
  167. }
  168. // Expression was completed
  169. node = node->parent; // For oneliners
  170. faux_list_add(pline->exprs, pexpr_new());
  171. }
  172. faux_argv_each(arg);
  173. } while (node);
  174. return BOOL_TRUE;
  175. }
  176. /*
  177. bool_t parse_tree(const struct lysc_node *tree, faux_argv_node_t **arg,
  178. pline_t *pline)
  179. {
  180. const struct lysc_node *node = NULL;
  181. const char *str = (const char *)faux_argv_current(*arg);
  182. node = find_child(tree, str);
  183. if (node) {
  184. parse_node(node, arg, pline);
  185. return BOOL_TRUE;
  186. }
  187. return BOOL_FALSE;
  188. }
  189. */
  190. pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
  191. {
  192. struct lys_module *module = NULL;
  193. pline_t *pline = pline_new();
  194. uint32_t i = 0;
  195. faux_argv_node_t *arg = faux_argv_iter(argv);
  196. assert(ctx);
  197. if (!ctx)
  198. return NULL;
  199. // Iterate all modules
  200. i = 0;
  201. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  202. if (sr_module_is_internal(module))
  203. continue;
  204. if (!module->compiled)
  205. continue;
  206. if (!module->implemented)
  207. continue;
  208. if (!module->compiled->data)
  209. continue;
  210. if (parse_module(module, &arg, pline))
  211. break; // Found
  212. }
  213. return pline;
  214. }