klish_plugin_sysrepo.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_SHOW =
  99. PAT_CONTAINER |
  100. PAT_LIST |
  101. PAT_LIST_KEY |
  102. PAT_LIST_KEY_INCOMPLETED |
  103. PAT_LEAF |
  104. PAT_LEAF_EMPTY |
  105. PAT_LEAFLIST |
  106. PAT_LEAFLIST_VALUE,
  107. PT_NOT_SHOW =
  108. PAT_LEAF_VALUE,
  109. PT_COMPL_SHOW =
  110. PAT_CONTAINER |
  111. PAT_LIST |
  112. PAT_LIST_KEY |
  113. PAT_LIST_KEY_INCOMPLETED |
  114. PAT_LEAF |
  115. PAT_LEAF_EMPTY |
  116. PAT_LEAFLIST |
  117. PAT_LEAFLIST_VALUE,
  118. } pt_e;
  119. // Plain EXPRession
  120. typedef struct {
  121. char *xpath;
  122. char *value;
  123. bool_t active;
  124. pat_e pat;
  125. size_t args_num;
  126. size_t list_pos;
  127. char *last_keys;
  128. } pexpr_t;
  129. // Possible types of completion source
  130. typedef enum {
  131. PCOMPL_NODE = 0,
  132. PCOMPL_TYPE = 1,
  133. } pcompl_type_e;
  134. // Plain COMPLetion
  135. typedef struct {
  136. pcompl_type_e type;
  137. const struct lysc_node *node;
  138. char *xpath;
  139. sr_datastore_t xpath_ds;
  140. pat_e pat;
  141. } pcompl_t;
  142. // Plain LINE
  143. typedef struct pline_s {
  144. sr_session_ctx_t *sess;
  145. bool_t invalid;
  146. faux_list_t *exprs;
  147. faux_list_t *compls;
  148. } pline_t;
  149. // Parse/show settings
  150. typedef struct {
  151. char begin_bracket;
  152. char end_bracket;
  153. bool_t show_brackets;
  154. bool_t show_semicolons;
  155. bool_t first_key_w_stmt;
  156. bool_t keys_w_stmt;
  157. bool_t colorize;
  158. uint8_t indent;
  159. bool_t default_keys;
  160. bool_t show_default_keys;
  161. bool_t hide_passwords;
  162. bool_t enable_nacm;
  163. bool_t oneliners;
  164. } pline_opts_t;
  165. #define SRP_NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE | LYS_CASE)
  166. C_DECL_BEGIN
  167. // PLine
  168. pline_t *pline_new(sr_session_ctx_t *sess);
  169. void pline_opts_init(pline_opts_t *opts);
  170. int pline_opts_parse(const char *conf, pline_opts_t *opts);
  171. int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts);
  172. pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
  173. const pline_opts_t *opts);
  174. pexpr_t *pline_current_expr(pline_t *pline);
  175. void pline_free(pline_t *pline);
  176. void pline_debug(pline_t *pline);
  177. void pline_print_completions(const pline_t *pline, bool_t help,
  178. pt_e enabled_types, bool_t existing_nodes_only);
  179. size_t num_of_keys(const struct lysc_node *node);
  180. C_DECL_END
  181. // Name of named klish's udata
  182. #define SRP_UDATA_NAME "sysrepo"
  183. // Plugin's user-data structure
  184. typedef struct {
  185. faux_argv_t *path; // Current data hierarchy path ('edit' operation)
  186. pline_opts_t opts; // Settings
  187. sr_conn_ctx_t *sr_conn; // Sysrepo connection
  188. sr_session_ctx_t *sr_sess; // Sysrepo session
  189. sr_subscription_ctx_t *nacm_sub;
  190. } srp_udata_t;
  191. // Repository to edit with srp commands
  192. #define SRP_REPO_EDIT SR_DS_CANDIDATE
  193. C_DECL_BEGIN
  194. // Types
  195. int srp_PLINE_SET(kcontext_t *context);
  196. int srp_PLINE_DEL(kcontext_t *context);
  197. int srp_PLINE_EDIT(kcontext_t *context);
  198. int srp_PLINE_EDIT_ABS(kcontext_t *context);
  199. int srp_PLINE_INSERT_FROM(kcontext_t *context);
  200. int srp_PLINE_INSERT_TO(kcontext_t *context);
  201. int srp_PLINE_SHOW(kcontext_t *context);
  202. int srp_PLINE_SHOW_ABS(kcontext_t *context);
  203. // Completion/Help/Prompt
  204. int srp_compl(kcontext_t *context);
  205. int srp_help(kcontext_t *context);
  206. int srp_compl_set(kcontext_t *context);
  207. int srp_help_set(kcontext_t *context);
  208. int srp_compl_del(kcontext_t *context);
  209. int srp_help_del(kcontext_t *context);
  210. int srp_compl_edit(kcontext_t *context);
  211. int srp_compl_edit_abs(kcontext_t *context);
  212. int srp_help_edit(kcontext_t *context);
  213. int srp_help_edit_abs(kcontext_t *context);
  214. int srp_compl_insert(kcontext_t *context);
  215. int srp_help_insert(kcontext_t *context);
  216. int srp_compl_insert_to(kcontext_t *context);
  217. int srp_help_insert_to(kcontext_t *context);
  218. int srp_prompt_edit_path(kcontext_t *context);
  219. int srp_compl_xpath(kcontext_t *context);
  220. int srp_compl_show(kcontext_t *context);
  221. int srp_compl_show_abs(kcontext_t *context);
  222. int srp_help_show(kcontext_t *context);
  223. int srp_help_show_abs(kcontext_t *context);
  224. // Operations
  225. int srp_set(kcontext_t *context);
  226. int srp_del(kcontext_t *context);
  227. int srp_edit(kcontext_t *context);
  228. int srp_top(kcontext_t *context);
  229. int srp_up(kcontext_t *context);
  230. int srp_insert(kcontext_t *context);
  231. int srp_verify(kcontext_t *context);
  232. int srp_commit(kcontext_t *context);
  233. int srp_reset(kcontext_t *context);
  234. int srp_show_abs(kcontext_t *context);
  235. int srp_show(kcontext_t *context);
  236. int srp_diff(kcontext_t *context);
  237. int srp_deactivate(kcontext_t *context);
  238. // Service functions
  239. int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
  240. const pline_opts_t *opts, const char *user, bool_t stop_on_error);
  241. // Plugin's user-data service functions
  242. pline_opts_t *srp_udata_opts(kcontext_t *context);
  243. faux_argv_t *srp_udata_path(kcontext_t *context);
  244. void srp_udata_set_path(kcontext_t *context, faux_argv_t *path);
  245. sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context);
  246. // Private
  247. enum diff_op {
  248. DIFF_OP_CREATE,
  249. DIFF_OP_DELETE,
  250. DIFF_OP_REPLACE,
  251. DIFF_OP_NONE,
  252. };
  253. bool_t show_xpath(sr_session_ctx_t *sess, const char *xpath, pline_opts_t *opts);
  254. void show_subtree(const struct lyd_node *nodes_list, size_t level,
  255. enum diff_op op, pline_opts_t *opts, bool_t parent_is_oneliner);
  256. // kly helper library
  257. typedef struct {
  258. const struct lysc_node *node;
  259. const char *value;
  260. const char *dflt;
  261. } klysc_key_t;
  262. int klysc_key_compare(const void *first, const void *second);
  263. int klysc_key_kcompare(const void *key, const void *list_item);
  264. bool_t klysc_node_ext(const struct lysc_node *node,
  265. const char *module, const char *name, const char **argument);
  266. bool_t klysc_node_ext_is_password(const struct lysc_node *node);
  267. const char *klysc_node_ext_completion(const struct lysc_node *node);
  268. const char *klysc_node_ext_default(const struct lysc_node *node);
  269. char *klyd_node_value(const struct lyd_node *node);
  270. const struct lysc_node *klysc_find_child(const struct lysc_node *node,
  271. const char *name);
  272. char *klysc_leafref_xpath(const struct lysc_node *node,
  273. const struct lysc_type *type, const char *node_path);
  274. const char *klysc_identityref_prefix(struct lysc_type_identityref *type,
  275. const char *name);
  276. size_t klyd_visible_child_num(const struct lyd_node *node);
  277. bool_t kly_str2ds(const char *str, size_t len, sr_datastore_t *ds);
  278. bool_t kly_parse_ext_xpath(const char *xpath, const char **raw_xpath,
  279. sr_datastore_t *ds);
  280. C_DECL_END
  281. #endif // _klish_plugin_sysrepo_h