klish_plugin_sysrepo.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef _klish_pligin_sysrepo_h
  2. #define _klish_plugin_sysrepo_h
  3. #include <sysrepo.h>
  4. #include <sysrepo/xpath.h>
  5. #include <faux/faux.h>
  6. #include <faux/argv.h>
  7. #include <faux/list.h>
  8. #include <klish/kcontext_base.h>
  9. // Type of positional pline argument
  10. // P(line) A(rg) T(ype)
  11. typedef enum {
  12. PAT_NONE = 0x0000,
  13. PAT_CONTAINER = 0x0001,
  14. PAT_LIST = 0x0002,
  15. PAT_LIST_KEY = 0x0004,
  16. PAT_LIST_KEY_INCOMPLETED = 0x0008,
  17. PAT_LEAF = 0x0010,
  18. PAT_LEAF_VALUE = 0x0020,
  19. PAT_LEAF_EMPTY = 0x0040,
  20. PAT_LEAFLIST = 0x0080,
  21. PAT_LEAFLIST_VALUE = 0x0100,
  22. } pat_e;
  23. // Type of pline expression
  24. // P(line) T(ype)
  25. typedef enum {
  26. PT_COMPL_ALL =
  27. PAT_CONTAINER |
  28. PAT_LIST |
  29. PAT_LIST_KEY |
  30. PAT_LIST_KEY_INCOMPLETED |
  31. PAT_LEAF |
  32. PAT_LEAF_VALUE |
  33. PAT_LEAF_EMPTY |
  34. PAT_LEAFLIST |
  35. PAT_LEAFLIST_VALUE,
  36. PT_SET =
  37. PAT_CONTAINER |
  38. PAT_LIST_KEY |
  39. PAT_LEAF_VALUE |
  40. PAT_LEAF_EMPTY |
  41. PAT_LEAFLIST_VALUE,
  42. PT_NOT_SET =
  43. 0,
  44. PT_COMPL_SET =
  45. PAT_CONTAINER |
  46. PAT_LIST |
  47. PAT_LIST_KEY |
  48. PAT_LIST_KEY_INCOMPLETED |
  49. PAT_LEAF |
  50. PAT_LEAF_VALUE |
  51. PAT_LEAF_EMPTY |
  52. PAT_LEAFLIST |
  53. PAT_LEAFLIST_VALUE,
  54. PT_DEL =
  55. PAT_CONTAINER |
  56. PAT_LIST_KEY |
  57. PAT_LEAF |
  58. PAT_LEAF_EMPTY |
  59. PAT_LEAFLIST |
  60. PAT_LEAFLIST_VALUE,
  61. PT_NOT_DEL =
  62. PAT_LEAF_VALUE,
  63. PT_COMPL_DEL =
  64. PAT_CONTAINER |
  65. PAT_LIST |
  66. PAT_LIST_KEY |
  67. PAT_LIST_KEY_INCOMPLETED |
  68. PAT_LEAF |
  69. PAT_LEAF_EMPTY |
  70. PAT_LEAFLIST |
  71. PAT_LEAFLIST_VALUE,
  72. PT_EDIT =
  73. PAT_CONTAINER |
  74. PAT_LIST_KEY,
  75. PT_NOT_EDIT =
  76. PAT_LEAF |
  77. PAT_LEAF_VALUE |
  78. PAT_LEAFLIST |
  79. PAT_LEAFLIST_VALUE,
  80. PT_COMPL_EDIT =
  81. PAT_CONTAINER |
  82. PAT_LIST |
  83. PAT_LIST_KEY |
  84. PAT_LIST_KEY_INCOMPLETED,
  85. PT_INSERT =
  86. PAT_LIST_KEY |
  87. PAT_LEAFLIST_VALUE,
  88. PT_NOT_INSERT =
  89. PAT_LEAF |
  90. PAT_LEAF_VALUE,
  91. PT_COMPL_INSERT =
  92. PAT_CONTAINER |
  93. PAT_LIST |
  94. PAT_LIST_KEY |
  95. PAT_LIST_KEY_INCOMPLETED |
  96. PAT_LEAFLIST |
  97. PAT_LEAFLIST_VALUE,
  98. } pt_e;
  99. // Plain EXPRession
  100. typedef struct {
  101. char *xpath;
  102. char *value;
  103. bool_t active;
  104. pat_e pat;
  105. size_t args_num;
  106. size_t list_pos;
  107. char *last_keys;
  108. } pexpr_t;
  109. // Possible types of completion source
  110. typedef enum {
  111. PCOMPL_NODE = 0,
  112. PCOMPL_TYPE = 1,
  113. } pcompl_type_e;
  114. // Plain COMPLetion
  115. typedef struct {
  116. pcompl_type_e type;
  117. const struct lysc_node *node;
  118. char *xpath;
  119. sr_datastore_t xpath_ds;
  120. pat_e pat;
  121. } pcompl_t;
  122. // Plain LINE
  123. typedef struct pline_s {
  124. sr_session_ctx_t *sess;
  125. bool_t invalid;
  126. faux_list_t *exprs;
  127. faux_list_t *compls;
  128. } pline_t;
  129. // Parse/show settings
  130. typedef struct {
  131. char begin_bracket;
  132. char end_bracket;
  133. bool_t show_brackets;
  134. bool_t show_semicolons;
  135. bool_t first_key_w_stmt;
  136. bool_t keys_w_stmt;
  137. bool_t colorize;
  138. uint8_t indent;
  139. bool_t default_keys;
  140. bool_t show_default_keys;
  141. bool_t hide_passwords;
  142. bool_t enable_nacm;
  143. bool_t oneliners;
  144. } pline_opts_t;
  145. #define SRP_NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE | LYS_CASE)
  146. C_DECL_BEGIN
  147. // PLine
  148. pline_t *pline_new(sr_session_ctx_t *sess);
  149. void pline_opts_init(pline_opts_t *opts);
  150. int pline_opts_parse(const char *conf, pline_opts_t *opts);
  151. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts);
  152. pexpr_t *pline_current_expr(pline_t *pline);
  153. void pline_free(pline_t *pline);
  154. void pline_debug(pline_t *pline);
  155. void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_types);
  156. size_t num_of_keys(const struct lysc_node *node);
  157. C_DECL_END
  158. // Plugin's user-data structure
  159. typedef struct {
  160. faux_argv_t *path; // Current data hierarchy path ('edit' operation)
  161. pline_opts_t opts; // Settings
  162. sr_conn_ctx_t *sr_conn; // Sysrepo connection
  163. sr_session_ctx_t *sr_sess; // Sysrepo session
  164. sr_subscription_ctx_t *nacm_sub;
  165. } srp_udata_t;
  166. // Repository to edit with srp commands
  167. #define SRP_REPO_EDIT SR_DS_CANDIDATE
  168. C_DECL_BEGIN
  169. // Types
  170. int srp_PLINE_SET(kcontext_t *context);
  171. int srp_PLINE_DEL(kcontext_t *context);
  172. int srp_PLINE_EDIT(kcontext_t *context);
  173. int srp_PLINE_EDIT_ABS(kcontext_t *context);
  174. int srp_PLINE_INSERT_FROM(kcontext_t *context);
  175. int srp_PLINE_INSERT_TO(kcontext_t *context);
  176. // Completion/Help/Prompt
  177. int srp_compl(kcontext_t *context);
  178. int srp_help(kcontext_t *context);
  179. int srp_compl_set(kcontext_t *context);
  180. int srp_help_set(kcontext_t *context);
  181. int srp_compl_del(kcontext_t *context);
  182. int srp_help_del(kcontext_t *context);
  183. int srp_compl_edit(kcontext_t *context);
  184. int srp_compl_edit_abs(kcontext_t *context);
  185. int srp_help_edit(kcontext_t *context);
  186. int srp_help_edit_abs(kcontext_t *context);
  187. int srp_compl_insert(kcontext_t *context);
  188. int srp_help_insert(kcontext_t *context);
  189. int srp_compl_insert_to(kcontext_t *context);
  190. int srp_help_insert_to(kcontext_t *context);
  191. int srp_prompt_edit_path(kcontext_t *context);
  192. int srp_compl_xpath(kcontext_t *context);
  193. // Operations
  194. int srp_set(kcontext_t *context);
  195. int srp_del(kcontext_t *context);
  196. int srp_edit(kcontext_t *context);
  197. int srp_top(kcontext_t *context);
  198. int srp_up(kcontext_t *context);
  199. int srp_insert(kcontext_t *context);
  200. int srp_verify(kcontext_t *context);
  201. int srp_commit(kcontext_t *context);
  202. int srp_reset(kcontext_t *context);
  203. int srp_show_abs(kcontext_t *context);
  204. int srp_show(kcontext_t *context);
  205. int srp_diff(kcontext_t *context);
  206. int srp_deactivate(kcontext_t *context);
  207. // Plugin's user-data service functions
  208. pline_opts_t *srp_udata_opts(kcontext_t *context);
  209. faux_argv_t *srp_udata_path(kcontext_t *context);
  210. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path);
  211. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context);
  212. // Private
  213. enum diff_op {
  214. DIFF_OP_CREATE,
  215. DIFF_OP_DELETE,
  216. DIFF_OP_REPLACE,
  217. DIFF_OP_NONE,
  218. };
  219. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, pline_opts_t *opts);
  220. void show_subtree(const struct lyd_node *nodes_list, size_t level,
  221. enum diff_op op, pline_opts_t *opts, bool_t parent_is_oneliner);
  222. // kly helper library
  223. typedef struct {
  224. const struct lysc_node *node;
  225. const char *value;
  226. const char *dflt;
  227. } klysc_key_t;
  228. int klysc_key_compare(const void *first, const void *second);
  229. int klysc_key_kcompare(const void *key, const void *list_item);
  230. bool_t klysc_node_ext(const struct lysc_node *node,
  231. const char *module, const char *name, const char **argument);
  232. bool_t klysc_node_ext_is_password(const struct lysc_node *node);
  233. const char *klysc_node_ext_completion(const struct lysc_node *node);
  234. const char *klysc_node_ext_default(const struct lysc_node *node);
  235. char *klyd_node_value(const struct lyd_node *node);
  236. const struct lysc_node *klysc_find_child(const struct lysc_node *node,
  237. const char *name);
  238. char *klysc_leafref_xpath(const struct lysc_node *node,
  239. const struct lysc_type *type, const char *node_path);
  240. const char *klysc_identityref_prefix(struct lysc_type_identityref *type,
  241. const char *name);
  242. size_t klyd_visible_child_num(const struct lyd_node *node);
  243. bool_t kly_str2ds(const char *str, size_t len, sr_datastore_t *ds);
  244. bool_t kly_parse_ext_xpath(const char *xpath, const char **raw_xpath,
  245. sr_datastore_t *ds);
  246. C_DECL_END
  247. #endif // _klish_plugin_sysrepo_h