kly.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Get extension by name from schema node
  18. static bool_t klysc_ext(const struct lysc_ext_instance *exts,
  19. const char *module, const char *name, const char **argument)
  20. {
  21. LY_ARRAY_COUNT_TYPE u = 0;
  22. if (!exts)
  23. return BOOL_FALSE;
  24. LY_ARRAY_FOR(exts, u) {
  25. const struct lysc_ext_instance *ext = &exts[u];
  26. //syslog(LOG_ERR, "mod: %s, ext: %s", ext->def->module->name, ext->def->name);
  27. if (faux_str_cmp(ext->def->module->name, module) != 0)
  28. continue;
  29. if (faux_str_cmp(ext->def->name, name) != 0)
  30. continue;
  31. if (argument)
  32. *argument = ext->argument;
  33. return BOOL_TRUE;
  34. }
  35. return BOOL_FALSE;
  36. }
  37. // Get extension by name
  38. bool_t klysc_node_ext(const struct lysc_node *node,
  39. const char *module, const char *name, const char **argument)
  40. {
  41. if (!node)
  42. return BOOL_FALSE;
  43. if (klysc_ext(node->exts, module, name, argument))
  44. return BOOL_TRUE;
  45. return BOOL_FALSE;
  46. }
  47. bool_t klysc_node_ext_is_password(const struct lysc_node *node)
  48. {
  49. return klysc_node_ext(node, "klish", "password", NULL);
  50. }
  51. const char *klysc_node_ext_completion(const struct lysc_node *node)
  52. {
  53. const char *xpath = NULL;
  54. klysc_node_ext(node, "klish", "completion", &xpath);
  55. return xpath;
  56. }
  57. const char *klysc_node_ext_default(const struct lysc_node *node)
  58. {
  59. const char *dflt = NULL;
  60. klysc_node_ext(node, "klish", "default", &dflt);
  61. return dflt;
  62. }
  63. // Get value from data lyd node
  64. char *klyd_node_value(const struct lyd_node *node)
  65. {
  66. const struct lysc_node *schema = NULL;
  67. const struct lysc_type *type = NULL;
  68. const char *origin_value = NULL;
  69. char *space = NULL;
  70. char *escaped = NULL;
  71. char *result = NULL;
  72. if (!node)
  73. return NULL;
  74. schema = node->schema;
  75. if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  76. return NULL;
  77. if (schema->nodetype & LYS_LEAF)
  78. type = ((const struct lysc_node_leaf *)schema)->type;
  79. else
  80. type = ((const struct lysc_node_leaflist *)schema)->type;
  81. if (type->basetype != LY_TYPE_IDENT) {
  82. origin_value = lyd_get_value(node);
  83. } else {
  84. // Identity
  85. const struct lyd_value *value = NULL;
  86. value = &((const struct lyd_node_term *)node)->value;
  87. origin_value = value->ident->name;
  88. }
  89. escaped = faux_str_c_esc(origin_value);
  90. // String with space must have quotes
  91. space = strchr(origin_value, ' ');
  92. if (space) {
  93. result = faux_str_sprintf("\"%s\"", escaped);
  94. faux_str_free(escaped);
  95. } else {
  96. result = escaped;
  97. }
  98. return result;
  99. }