klish_plugin_sysrepo.h 8.1 KB

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