kly.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <syslog.h>
  7. #include <faux/faux.h>
  8. #include <faux/str.h>
  9. #include <faux/list.h>
  10. #include <faux/argv.h>
  11. #include <sysrepo.h>
  12. #include <sysrepo/xpath.h>
  13. #include <sysrepo/values.h>
  14. #include <libyang/tree_edit.h>
  15. #include "private.h"
  16. #include "pline.h"
  17. int klysc_key_compare(const void *first, const void *second)
  18. {
  19. const klysc_key_t *f = (const klysc_key_t *)first;
  20. const klysc_key_t *s = (const klysc_key_t *)second;
  21. return strcmp(f->node->name, s->node->name);
  22. }
  23. int klysc_key_kcompare(const void *key, const void *list_item)
  24. {
  25. const char *f = (const char *)key;
  26. const klysc_key_t *s = (const klysc_key_t *)list_item;
  27. return strcmp(f, s->node->name);
  28. }
  29. // Get extension by name from schema node
  30. static bool_t klysc_ext(const struct lysc_ext_instance *exts,
  31. const char *module, const char *name, const char **argument)
  32. {
  33. LY_ARRAY_COUNT_TYPE u = 0;
  34. if (!exts)
  35. return BOOL_FALSE;
  36. LY_ARRAY_FOR(exts, u) {
  37. const struct lysc_ext_instance *ext = &exts[u];
  38. //syslog(LOG_ERR, "mod: %s, ext: %s", ext->def->module->name, ext->def->name);
  39. if (faux_str_cmp(ext->def->module->name, module) != 0)
  40. continue;
  41. if (faux_str_cmp(ext->def->name, name) != 0)
  42. continue;
  43. if (argument)
  44. *argument = ext->argument;
  45. return BOOL_TRUE;
  46. }
  47. return BOOL_FALSE;
  48. }
  49. // Get extension by name
  50. bool_t klysc_node_ext(const struct lysc_node *node,
  51. const char *module, const char *name, const char **argument)
  52. {
  53. if (!node)
  54. return BOOL_FALSE;
  55. if (klysc_ext(node->exts, module, name, argument))
  56. return BOOL_TRUE;
  57. return BOOL_FALSE;
  58. }
  59. bool_t klysc_node_ext_is_password(const struct lysc_node *node)
  60. {
  61. return klysc_node_ext(node, "klish", "password", NULL);
  62. }
  63. const char *klysc_node_ext_completion(const struct lysc_node *node)
  64. {
  65. const char *xpath = NULL;
  66. klysc_node_ext(node, "klish", "completion", &xpath);
  67. return xpath;
  68. }
  69. const char *klysc_node_ext_default(const struct lysc_node *node)
  70. {
  71. const char *dflt = NULL;
  72. klysc_node_ext(node, "klish", "default", &dflt);
  73. return dflt;
  74. }
  75. // Get value from data lyd node
  76. char *klyd_node_value(const struct lyd_node *node)
  77. {
  78. const struct lysc_node *schema = NULL;
  79. const struct lysc_type *type = NULL;
  80. const char *origin_value = NULL;
  81. char *space = NULL;
  82. char *escaped = NULL;
  83. char *result = NULL;
  84. if (!node)
  85. return NULL;
  86. schema = node->schema;
  87. if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  88. return NULL;
  89. if (schema->nodetype & LYS_LEAF)
  90. type = ((const struct lysc_node_leaf *)schema)->type;
  91. else
  92. type = ((const struct lysc_node_leaflist *)schema)->type;
  93. if (type->basetype != LY_TYPE_IDENT) {
  94. origin_value = lyd_get_value(node);
  95. } else {
  96. // Identity
  97. const struct lyd_value *value = NULL;
  98. value = &((const struct lyd_node_term *)node)->value;
  99. origin_value = value->ident->name;
  100. }
  101. escaped = faux_str_c_esc(origin_value);
  102. // String with space must have quotes
  103. space = strchr(origin_value, ' ');
  104. if (space) {
  105. result = faux_str_sprintf("\"%s\"", escaped);
  106. faux_str_free(escaped);
  107. } else {
  108. result = escaped;
  109. }
  110. return result;
  111. }
  112. // Don't use standard lys_find_child() because it checks given module to be
  113. // equal to found node's module. So augmented nodes will not be found.
  114. const struct lysc_node *klysc_find_child(const struct lysc_node *node,
  115. const char *name)
  116. {
  117. const struct lysc_node *iter = NULL;
  118. if (!node)
  119. return NULL;
  120. LY_LIST_FOR(node, iter) {
  121. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  122. continue;
  123. if (!(iter->flags & LYS_CONFIG_W))
  124. continue;
  125. // Special case. LYS_CHOICE and LYS_CASE must search for
  126. // specified name inside themselfs.
  127. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  128. const struct lysc_node *node_in = NULL;
  129. node_in = klysc_find_child(lysc_node_child(iter), name);
  130. if (node_in)
  131. return node_in;
  132. continue;
  133. }
  134. if (!faux_str_cmp(iter->name, name))
  135. return iter;
  136. }
  137. return NULL;
  138. }
  139. struct lysc_ident *klysc_find_ident(struct lysc_ident *ident, const char *name)
  140. {
  141. LY_ARRAY_COUNT_TYPE u = 0;
  142. if (!ident)
  143. return NULL;
  144. if (!ident->derived) {
  145. if (!faux_str_cmp(name, ident->name))
  146. return ident;
  147. return NULL;
  148. }
  149. LY_ARRAY_FOR(ident->derived, u) {
  150. struct lysc_ident *identity = klysc_find_ident(ident->derived[u], name);
  151. if (identity)
  152. return identity;
  153. }
  154. return NULL;
  155. }