pline.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /** @file pline.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <syslog.h>
  9. #include <limits.h>
  10. #include <faux/faux.h>
  11. #include <faux/str.h>
  12. #include <faux/list.h>
  13. #include <faux/argv.h>
  14. #include <sysrepo.h>
  15. #include <sysrepo/xpath.h>
  16. #include <sysrepo/values.h>
  17. #include <libyang/tree_edit.h>
  18. #include "private.h"
  19. #include "pline.h"
  20. static int sr_ly_module_is_internal(const struct lys_module *ly_mod)
  21. {
  22. if (!ly_mod->revision) {
  23. return 0;
  24. }
  25. if (!strcmp(ly_mod->name, "ietf-yang-metadata")
  26. && !strcmp(ly_mod->revision, "2016-08-05")) {
  27. return 1;
  28. } else if (!strcmp(ly_mod->name, "yang")
  29. && !strcmp(ly_mod->revision, "2021-04-07")) {
  30. return 1;
  31. } else if (!strcmp(ly_mod->name, "ietf-inet-types")
  32. && !strcmp(ly_mod->revision, "2013-07-15")) {
  33. return 1;
  34. } else if (!strcmp(ly_mod->name, "ietf-yang-types")
  35. && !strcmp(ly_mod->revision, "2013-07-15")) {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static int sr_module_is_internal(const struct lys_module *ly_mod, bool_t enable_nacm)
  41. {
  42. if (!ly_mod->revision) {
  43. return 0;
  44. }
  45. if (sr_ly_module_is_internal(ly_mod)) {
  46. return 1;
  47. }
  48. if (!strcmp(ly_mod->name, "ietf-datastores")
  49. && !strcmp(ly_mod->revision, "2018-02-14")) {
  50. return 1;
  51. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  52. return 1;
  53. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  54. return 1;
  55. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  56. return 1;
  57. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults")
  58. && !strcmp(ly_mod->revision, "2011-06-01")) {
  59. return 1;
  60. } else if (!strcmp(ly_mod->name, "ietf-origin")
  61. && !strcmp(ly_mod->revision, "2018-02-14")) {
  62. return 1;
  63. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications")
  64. && !strcmp(ly_mod->revision, "2012-02-06")) {
  65. return 1;
  66. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  67. return 1;
  68. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  69. return 1;
  70. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  71. return 1;
  72. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm") && !enable_nacm) {
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. static pexpr_t *pexpr_new(void)
  78. {
  79. pexpr_t *pexpr = NULL;
  80. pexpr = faux_zmalloc(sizeof(*pexpr));
  81. assert(pexpr);
  82. if (!pexpr)
  83. return NULL;
  84. // Initialize
  85. pexpr->xpath = NULL;
  86. pexpr->value = NULL;
  87. pexpr->active = BOOL_FALSE;
  88. pexpr->pat = PAT_NONE;
  89. pexpr->args_num = 0;
  90. pexpr->list_pos = 0;
  91. pexpr->last_keys = NULL;
  92. return pexpr;
  93. }
  94. static void pexpr_free(pexpr_t *pexpr)
  95. {
  96. if (!pexpr)
  97. return;
  98. faux_str_free(pexpr->xpath);
  99. faux_str_free(pexpr->value);
  100. faux_str_free(pexpr->last_keys);
  101. free(pexpr);
  102. }
  103. static pcompl_t *pcompl_new(void)
  104. {
  105. pcompl_t *pcompl = NULL;
  106. pcompl = faux_zmalloc(sizeof(*pcompl));
  107. assert(pcompl);
  108. if (!pcompl)
  109. return NULL;
  110. // Initialize
  111. pcompl->type = PCOMPL_NODE;
  112. pcompl->node = NULL;
  113. pcompl->xpath = NULL;
  114. return pcompl;
  115. }
  116. static void pcompl_free(pcompl_t *pcompl)
  117. {
  118. if (!pcompl)
  119. return;
  120. faux_str_free(pcompl->xpath);
  121. free(pcompl);
  122. }
  123. pline_t *pline_new(sr_session_ctx_t *sess)
  124. {
  125. pline_t *pline = NULL;
  126. pline = faux_zmalloc(sizeof(*pline));
  127. assert(pline);
  128. if (!pline)
  129. return NULL;
  130. // Init
  131. pline->sess = sess;
  132. pline->invalid = BOOL_FALSE;
  133. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  134. NULL, NULL, (faux_list_free_fn)pexpr_free);
  135. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  136. NULL, NULL, (faux_list_free_fn)pcompl_free);
  137. return pline;
  138. }
  139. void pline_free(pline_t *pline)
  140. {
  141. if (!pline)
  142. return;
  143. faux_list_free(pline->exprs);
  144. faux_list_free(pline->compls);
  145. faux_free(pline);
  146. }
  147. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  148. size_t args_num, size_t list_pos)
  149. {
  150. pexpr_t *pexpr = NULL;
  151. assert(pline);
  152. pexpr = pexpr_new();
  153. if (xpath)
  154. pexpr->xpath = faux_str_dup(xpath);
  155. pexpr->args_num = args_num;
  156. pexpr->list_pos = list_pos;
  157. faux_list_add(pline->exprs, pexpr);
  158. return pexpr;
  159. }
  160. pexpr_t *pline_current_expr(pline_t *pline)
  161. {
  162. assert(pline);
  163. if (faux_list_len(pline->exprs) == 0)
  164. pline_add_expr(pline, NULL, 0, 0);
  165. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  166. }
  167. static void pline_add_compl(pline_t *pline,
  168. pcompl_type_e type, const struct lysc_node *node, const char *xpath)
  169. {
  170. pcompl_t *pcompl = NULL;
  171. assert(pline);
  172. pcompl = pcompl_new();
  173. pcompl->type = type;
  174. pcompl->node = node;
  175. if (xpath)
  176. pcompl->xpath = faux_str_dup(xpath);
  177. faux_list_add(pline->compls, pcompl);
  178. }
  179. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  180. const struct lysc_node *node)
  181. {
  182. const struct lysc_node *subtree = NULL;
  183. const struct lysc_node *iter = NULL;
  184. assert(pline);
  185. assert(module);
  186. if (node)
  187. subtree = lysc_node_child(node);
  188. else
  189. subtree = module->compiled->data;
  190. LY_LIST_FOR(subtree, iter) {
  191. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  192. continue;
  193. if (!(iter->flags & LYS_CONFIG_W))
  194. continue;
  195. if ((iter->nodetype & LYS_LEAF) && (iter->flags & LYS_KEY))
  196. continue;
  197. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  198. pline_add_compl_subtree(pline, module, iter);
  199. continue;
  200. }
  201. pline_add_compl(pline, PCOMPL_NODE, iter, NULL);
  202. }
  203. }
  204. void pline_debug(pline_t *pline)
  205. {
  206. faux_list_node_t *iter = NULL;
  207. pexpr_t *pexpr = NULL;
  208. pcompl_t *pcompl = NULL;
  209. printf("====== Pline:\n\n");
  210. printf("invalid = %s\n", pline->invalid ? "true" : "false");
  211. printf("\n");
  212. printf("=== Expressions:\n\n");
  213. iter = faux_list_head(pline->exprs);
  214. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  215. char *pat = NULL;
  216. printf("pexpr.xpath = %s\n", pexpr->xpath ? pexpr->xpath : "NULL");
  217. printf("pexpr.value = %s\n", pexpr->value ? pexpr->value : "NULL");
  218. printf("pexpr.active = %s\n", pexpr->active ? "true" : "false");
  219. switch (pexpr->pat) {
  220. case 0x0001:
  221. pat = "NONE";
  222. break;
  223. case 0x0002:
  224. pat = "CONTAINER";
  225. break;
  226. case 0x0004:
  227. pat = "LIST";
  228. break;
  229. case 0x0008:
  230. pat = "LIST_KEY";
  231. break;
  232. case 0x0010:
  233. pat = "LIST_KEY_INCOMPLETED";
  234. break;
  235. case 0x0020:
  236. pat = "LEAF";
  237. break;
  238. case 0x0040:
  239. pat = "LEAF_VALUE";
  240. break;
  241. case 0x0080:
  242. pat = "LEAF_EMPTY";
  243. break;
  244. case 0x0100:
  245. pat = "LEAFLIST";
  246. break;
  247. case 0x0200:
  248. pat = "LEAFLIST_VALUE";
  249. break;
  250. default:
  251. pat = "UNKNOWN";
  252. break;
  253. }
  254. printf("pexpr.pat = %s\n", pat);
  255. printf("pexpr.args_num = %lu\n", pexpr->args_num);
  256. printf("pexpr.list_pos = %lu\n", pexpr->list_pos);
  257. printf("pexpr.last_keys = %s\n", pexpr->last_keys ? pexpr->last_keys : "NULL");
  258. printf("\n");
  259. }
  260. printf("=== Completions:\n\n");
  261. iter = faux_list_head(pline->compls);
  262. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  263. printf("pcompl.type = %s\n", (pcompl->type == PCOMPL_NODE) ?
  264. "PCOMPL_NODE" : "PCOMPL_TYPE");
  265. printf("pcompl.node = %s\n", pcompl->node ? pcompl->node->name : "NULL");
  266. printf("pcompl.xpath = %s\n", pcompl->xpath ? pcompl->xpath : "NULL");
  267. printf("\n");
  268. }
  269. }
  270. static bool_t pexpr_xpath_add_node(pexpr_t *pexpr,
  271. const char *prefix, const char *name)
  272. {
  273. char *tmp = NULL;
  274. assert(pexpr);
  275. assert(prefix);
  276. assert(name);
  277. tmp = faux_str_sprintf("/%s:%s", prefix, name);
  278. faux_str_cat(&pexpr->xpath, tmp);
  279. faux_str_free(tmp);
  280. pexpr->args_num++;
  281. // Activate current expression. Because it really has
  282. // new component
  283. pexpr->active = BOOL_TRUE;
  284. return BOOL_TRUE;
  285. }
  286. static bool_t pexpr_xpath_add_list_key(pexpr_t *pexpr,
  287. const char *key, const char *value, bool_t inc_args_num)
  288. {
  289. char *tmp = NULL;
  290. char *escaped = NULL;
  291. assert(pexpr);
  292. assert(key);
  293. assert(value);
  294. escaped = faux_str_c_esc(value);
  295. tmp = faux_str_sprintf("[%s=\"%s\"]", key, escaped);
  296. faux_str_free(escaped);
  297. faux_str_cat(&pexpr->xpath, tmp);
  298. faux_str_cat(&pexpr->last_keys, tmp);
  299. faux_str_free(tmp);
  300. if (inc_args_num)
  301. pexpr->args_num++;
  302. return BOOL_TRUE;
  303. }
  304. static bool_t pexpr_xpath_add_leaflist_key(pexpr_t *pexpr,
  305. const char *prefix, const char *value)
  306. {
  307. char *tmp = NULL;
  308. assert(pexpr);
  309. assert(value);
  310. tmp = faux_str_sprintf("[.='%s%s%s']",
  311. prefix ? prefix : "", prefix ? ":" : "", value);
  312. faux_str_cat(&pexpr->xpath, tmp);
  313. faux_str_cat(&pexpr->last_keys, value);
  314. faux_str_free(tmp);
  315. pexpr->args_num++;
  316. return BOOL_TRUE;
  317. }
  318. static void pline_add_compl_leafref(pline_t *pline, const struct lysc_node *node,
  319. const struct lysc_type *type, const char *xpath)
  320. {
  321. if (!type)
  322. return;
  323. if (!node)
  324. return;
  325. if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  326. return;
  327. switch (type->basetype) {
  328. case LY_TYPE_UNION: {
  329. struct lysc_type_union *t =
  330. (struct lysc_type_union *)type;
  331. LY_ARRAY_COUNT_TYPE u = 0;
  332. LY_ARRAY_FOR(t->types, u) {
  333. pline_add_compl_leafref(pline, node, t->types[u], xpath);
  334. }
  335. break;
  336. }
  337. case LY_TYPE_LEAFREF: {
  338. char *compl_xpath = klysc_leafref_xpath(node, type, xpath);
  339. pline_add_compl(pline, PCOMPL_TYPE, NULL, compl_xpath);
  340. faux_str_free(compl_xpath);
  341. break;
  342. }
  343. default:
  344. break;
  345. }
  346. }
  347. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  348. pline_t *pline, pline_opts_t *opts)
  349. {
  350. faux_argv_node_t *arg = faux_argv_iter(argv);
  351. const struct lysc_node *node = NULL;
  352. char *rollback_xpath = NULL;
  353. size_t rollback_args_num = 0;
  354. size_t rollback_list_pos = 0;
  355. // Rollback is a mechanism to roll to previous node while
  356. // oneliners parsing
  357. bool_t rollback = BOOL_FALSE;
  358. pexpr_t *first_pexpr = NULL;
  359. // It's necessary because upper function can use the same pline object
  360. // for another modules before. It uses the same object to collect
  361. // possible completions. But pline is really invalid only when all
  362. // modules don't recognize argument.
  363. pline->invalid = BOOL_FALSE;
  364. do {
  365. pexpr_t *pexpr = pline_current_expr(pline);
  366. const char *str = (const char *)faux_argv_current(arg);
  367. bool_t is_rollback = rollback;
  368. bool_t next_arg = BOOL_TRUE;
  369. rollback = BOOL_FALSE;
  370. if (node && !is_rollback) {
  371. // Save rollback Xpath (for oneliners) before leaf node
  372. // Only leaf and leaf-list node allows to "rollback"
  373. // the path and add additional statements
  374. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  375. faux_str_free(rollback_xpath);
  376. rollback_xpath = faux_str_dup(pexpr->xpath);
  377. rollback_args_num = pexpr->args_num;
  378. rollback_list_pos = pexpr->list_pos;
  379. }
  380. // Add current node to Xpath
  381. pexpr_xpath_add_node(pexpr,
  382. node->module->name, node->name);
  383. }
  384. // Root of the module
  385. if (!node) {
  386. // Completion
  387. if (!str) {
  388. pline_add_compl_subtree(pline, module, node);
  389. break;
  390. }
  391. // Next element
  392. node = klysc_find_child(module->compiled->data, str);
  393. if (!node)
  394. break;
  395. // Container
  396. } else if (node->nodetype & LYS_CONTAINER) {
  397. pexpr->pat = PAT_CONTAINER;
  398. // Completion
  399. if (!str) {
  400. pline_add_compl_subtree(pline, module, node);
  401. break;
  402. }
  403. // Next element
  404. node = klysc_find_child(lysc_node_child(node), str);
  405. // List
  406. } else if (node->nodetype & LYS_LIST) {
  407. const struct lysc_node *iter = NULL;
  408. pexpr->pat = PAT_LIST;
  409. pexpr->list_pos = pexpr->args_num;
  410. faux_str_free(pexpr->last_keys);
  411. pexpr->last_keys = NULL;
  412. // Next element
  413. if (!is_rollback) {
  414. bool_t break_upper_loop = BOOL_FALSE;
  415. // Keys without statement. Positional parameters.
  416. if (!opts->keys_w_stmt) {
  417. LY_LIST_FOR(lysc_node_child(node), iter) {
  418. struct lysc_node_leaf *leaf =
  419. (struct lysc_node_leaf *)iter;
  420. if (!(iter->nodetype & LYS_LEAF))
  421. continue;
  422. if (!(iter->flags & LYS_KEY))
  423. continue;
  424. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  425. // Completion
  426. if (!str) {
  427. char *tmp = faux_str_sprintf("%s/%s",
  428. pexpr->xpath, leaf->name);
  429. pline_add_compl(pline,
  430. PCOMPL_TYPE, iter, tmp);
  431. pline_add_compl_leafref(pline, iter,
  432. leaf->type, tmp);
  433. faux_str_free(tmp);
  434. break_upper_loop = BOOL_TRUE;
  435. break;
  436. }
  437. pexpr_xpath_add_list_key(pexpr,
  438. leaf->name, str, BOOL_TRUE);
  439. faux_argv_each(&arg);
  440. str = (const char *)faux_argv_current(arg);
  441. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  442. }
  443. // Keys with statements. Arbitrary order of keys.
  444. } else {
  445. faux_list_t *keys = NULL;
  446. unsigned int specified_keys_num = 0;
  447. klysc_key_t *cur_key = NULL;
  448. bool_t first_key = BOOL_TRUE;
  449. bool_t first_key_is_optional = BOOL_FALSE;
  450. faux_list_node_t *key_iter = NULL;
  451. // List keys
  452. keys = faux_list_new(FAUX_LIST_UNSORTED,
  453. FAUX_LIST_UNIQUE,
  454. klysc_key_compare,
  455. klysc_key_kcompare,
  456. (faux_list_free_fn)faux_free);
  457. LY_LIST_FOR(lysc_node_child(node), iter) {
  458. struct lysc_node_leaf *leaf =
  459. (struct lysc_node_leaf *)iter;
  460. klysc_key_t *key = NULL;
  461. if (!(iter->nodetype & LYS_LEAF))
  462. continue;
  463. if (!(iter->flags & LYS_KEY))
  464. continue;
  465. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  466. key = faux_zmalloc(sizeof(*key));
  467. assert(key);
  468. key->node = iter;
  469. if (opts->default_keys &&
  470. (key->dflt = klysc_node_ext_default(iter))) {
  471. if (first_key)
  472. first_key_is_optional = BOOL_TRUE;
  473. }
  474. faux_list_add(keys, key);
  475. first_key = BOOL_FALSE;
  476. }
  477. while (specified_keys_num < faux_list_len(keys)) {
  478. // First key without statement. Must be mandatory.
  479. if ((0 == specified_keys_num) &&
  480. !opts->first_key_w_stmt &&
  481. !first_key_is_optional) {
  482. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  483. } else {
  484. if (!str)
  485. break;
  486. cur_key = faux_list_kfind(keys, str);
  487. if (!cur_key || cur_key->value)
  488. break;
  489. pexpr->args_num++;
  490. faux_argv_each(&arg);
  491. str = (const char *)faux_argv_current(arg);
  492. }
  493. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  494. // Completion
  495. if (!str) {
  496. char *tmp = faux_str_sprintf("%s/%s",
  497. pexpr->xpath,
  498. cur_key->node->name);
  499. pline_add_compl(pline, PCOMPL_TYPE,
  500. cur_key->node, tmp);
  501. pline_add_compl_leafref(pline, cur_key->node,
  502. ((const struct lysc_node_leaf *)cur_key->node)->type,
  503. tmp);
  504. faux_str_free(tmp);
  505. break_upper_loop = BOOL_TRUE;
  506. break;
  507. }
  508. pexpr_xpath_add_list_key(pexpr,
  509. cur_key->node->name, str, BOOL_TRUE);
  510. cur_key->value = str;
  511. specified_keys_num++;
  512. faux_argv_each(&arg);
  513. str = (const char *)faux_argv_current(arg);
  514. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  515. }
  516. if (break_upper_loop) {
  517. faux_list_free(keys);
  518. break;
  519. }
  520. key_iter = faux_list_head(keys);
  521. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  522. if (cur_key->value)
  523. continue;
  524. // Completion
  525. if (!str)
  526. pline_add_compl(pline,
  527. PCOMPL_NODE, cur_key->node, NULL);
  528. if (opts->default_keys && cur_key->dflt) {
  529. pexpr_xpath_add_list_key(pexpr,
  530. cur_key->node->name,
  531. cur_key->dflt, BOOL_FALSE);
  532. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  533. } else { // Mandatory key is not specified
  534. break_upper_loop = BOOL_TRUE;
  535. }
  536. }
  537. faux_list_free(keys);
  538. }
  539. if (break_upper_loop)
  540. break;
  541. }
  542. pexpr->pat = PAT_LIST_KEY;
  543. // Completion
  544. if (!str) {
  545. pline_add_compl_subtree(pline, module, node);
  546. break;
  547. }
  548. // Next element
  549. node = klysc_find_child(lysc_node_child(node), str);
  550. // Leaf
  551. } else if (node->nodetype & LYS_LEAF) {
  552. struct lysc_node_leaf *leaf =
  553. (struct lysc_node_leaf *)node;
  554. // Next element
  555. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  556. pexpr->pat = PAT_LEAF_EMPTY;
  557. // Completion
  558. if (!str) {
  559. pline_add_compl_subtree(pline,
  560. module, node->parent);
  561. break;
  562. }
  563. // Don't get next argument when argument is not
  564. // really consumed
  565. next_arg = BOOL_FALSE;
  566. } else {
  567. pexpr->pat = PAT_LEAF;
  568. // Completion
  569. if (!str) {
  570. pline_add_compl(pline, PCOMPL_TYPE, node, NULL);
  571. pline_add_compl_leafref(pline, node,
  572. leaf->type, pexpr->xpath);
  573. break;
  574. }
  575. pexpr->pat = PAT_LEAF_VALUE;
  576. // Idenity must have prefix
  577. if (LY_TYPE_IDENT == leaf->type->basetype) {
  578. const char *prefix = NULL;
  579. prefix = klysc_identityref_prefix(
  580. (struct lysc_type_identityref *)
  581. leaf->type, str);
  582. if (prefix)
  583. pexpr->value = faux_str_sprintf(
  584. "%s:", prefix);
  585. }
  586. faux_str_cat(&pexpr->value, str);
  587. }
  588. // Expression was completed
  589. // So rollback (for oneliners)
  590. node = node->parent;
  591. pline_add_expr(pline, rollback_xpath,
  592. rollback_args_num, rollback_list_pos);
  593. rollback = BOOL_TRUE;
  594. // Leaf-list
  595. } else if (node->nodetype & LYS_LEAFLIST) {
  596. const char *prefix = NULL;
  597. struct lysc_node_leaflist *leaflist =
  598. (struct lysc_node_leaflist *)node;
  599. pexpr->pat = PAT_LEAFLIST;
  600. pexpr->list_pos = pexpr->args_num;
  601. faux_str_free(pexpr->last_keys);
  602. pexpr->last_keys = NULL;
  603. // Completion
  604. if (!str) {
  605. pline_add_compl(pline, PCOMPL_TYPE, node, pexpr->xpath);
  606. pline_add_compl_leafref(pline, node,
  607. leaflist->type, pexpr->xpath);
  608. break;
  609. }
  610. pexpr->pat = PAT_LEAFLIST_VALUE;
  611. // Idenity must have prefix
  612. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  613. prefix = klysc_identityref_prefix(
  614. (struct lysc_type_identityref *)
  615. leaflist->type, str);
  616. }
  617. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  618. // Expression was completed
  619. // So rollback (for oneliners)
  620. node = node->parent;
  621. pline_add_expr(pline, rollback_xpath,
  622. rollback_args_num, rollback_list_pos);
  623. rollback = BOOL_TRUE;
  624. // LYS_CHOICE and LYS_CASE can appear while rollback only
  625. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  626. // Don't set pexpr->pat because CHOICE and CASE can't
  627. // appear within data tree (schema only)
  628. // Completion
  629. if (!str) {
  630. pline_add_compl_subtree(pline, module, node);
  631. break;
  632. }
  633. // Next element
  634. node = klysc_find_child(lysc_node_child(node), str);
  635. } else {
  636. break;
  637. }
  638. // Current argument was not consumed.
  639. // Break before getting next arg.
  640. if (!node && !rollback)
  641. break;
  642. if (next_arg)
  643. faux_argv_each(&arg);
  644. } while (BOOL_TRUE);
  645. // There is not-consumed argument so whole pline is invalid
  646. if (faux_argv_current(arg))
  647. pline->invalid = BOOL_TRUE;
  648. faux_str_free(rollback_xpath);
  649. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  650. if (!first_pexpr || !first_pexpr->xpath)
  651. return BOOL_FALSE; // Not found
  652. return BOOL_TRUE;
  653. }
  654. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts)
  655. {
  656. const struct ly_ctx *ctx = NULL;
  657. struct lys_module *module = NULL;
  658. pline_t *pline = NULL;
  659. uint32_t i = 0;
  660. faux_list_node_t *last_expr_node = NULL;
  661. assert(sess);
  662. if (!sess)
  663. return NULL;
  664. pline = pline_new(sess);
  665. if (!pline)
  666. return NULL;
  667. ctx = sr_session_acquire_context(pline->sess);
  668. if (!ctx)
  669. return NULL;
  670. // Iterate all modules
  671. i = 0;
  672. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  673. if (sr_module_is_internal(module, opts->enable_nacm))
  674. continue;
  675. if (!module->compiled)
  676. continue;
  677. if (!module->implemented)
  678. continue;
  679. if (!module->compiled->data)
  680. continue;
  681. if (pline_parse_module(module, argv, pline, opts))
  682. break; // Found
  683. }
  684. sr_session_release_context(pline->sess);
  685. // Last parsed expression can be inactive so remove it from list
  686. last_expr_node = faux_list_tail(pline->exprs);
  687. if (last_expr_node) {
  688. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  689. if (!expr->active)
  690. faux_list_del(pline->exprs, last_expr_node);
  691. }
  692. return pline;
  693. }
  694. static void identityref_compl(struct lysc_ident *ident)
  695. {
  696. LY_ARRAY_COUNT_TYPE u = 0;
  697. if (!ident)
  698. return;
  699. if (!ident->derived) {
  700. printf("%s\n", ident->name);
  701. return;
  702. }
  703. LY_ARRAY_FOR(ident->derived, u) {
  704. identityref_compl(ident->derived[u]);
  705. }
  706. }
  707. static void identityref_help(struct lysc_ident *ident)
  708. {
  709. LY_ARRAY_COUNT_TYPE u = 0;
  710. if (!ident)
  711. return;
  712. if (!ident->derived) {
  713. if (ident->dsc) {
  714. char *dsc = faux_str_getline(ident->dsc, NULL);
  715. printf("%s\n%s\n", ident->name, dsc);
  716. faux_str_free(dsc);
  717. } else {
  718. printf("%s\n%s\n", ident->name, ident->name);
  719. }
  720. return;
  721. }
  722. LY_ARRAY_FOR(ident->derived, u) {
  723. identityref_help(ident->derived[u]);
  724. }
  725. }
  726. static void pline_print_type_completions(const struct lysc_type *type)
  727. {
  728. assert(type);
  729. switch (type->basetype) {
  730. case LY_TYPE_BOOL: {
  731. printf("true\nfalse\n");
  732. break;
  733. }
  734. case LY_TYPE_ENUM: {
  735. const struct lysc_type_enum *t =
  736. (const struct lysc_type_enum *)type;
  737. LY_ARRAY_COUNT_TYPE u = 0;
  738. LY_ARRAY_FOR(t->enums, u) {
  739. printf("%s\n",t->enums[u].name);
  740. }
  741. break;
  742. }
  743. case LY_TYPE_IDENT: {
  744. struct lysc_type_identityref *t =
  745. (struct lysc_type_identityref *)type;
  746. LY_ARRAY_COUNT_TYPE u = 0;
  747. LY_ARRAY_FOR(t->bases, u) {
  748. identityref_compl(t->bases[u]);
  749. }
  750. break;
  751. }
  752. case LY_TYPE_UNION: {
  753. struct lysc_type_union *t =
  754. (struct lysc_type_union *)type;
  755. LY_ARRAY_COUNT_TYPE u = 0;
  756. LY_ARRAY_FOR(t->types, u) {
  757. pline_print_type_completions(t->types[u]);
  758. }
  759. break;
  760. }
  761. case LY_TYPE_LEAFREF: {
  762. struct lysc_type_leafref *t =
  763. (struct lysc_type_leafref *)type;
  764. pline_print_type_completions(t->realtype);
  765. break;
  766. }
  767. default:
  768. break;
  769. }
  770. }
  771. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  772. {
  773. struct lysc_range *range = NULL;
  774. LY_ARRAY_COUNT_TYPE u = 0;
  775. char *r = NULL;
  776. assert(type);
  777. range = ((struct lysc_type_num *)type)->range;
  778. // Show defaults
  779. if (!range) {
  780. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  781. return;
  782. }
  783. // Range
  784. faux_str_cat(&r, "[");
  785. LY_ARRAY_FOR(range->parts, u) {
  786. char *t = NULL;
  787. if (u != 0)
  788. faux_str_cat(&r, "|");
  789. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  790. range->parts[u].min_u64, range->parts[u].max_u64);
  791. faux_str_cat(&r, t);
  792. faux_str_free(t);
  793. }
  794. faux_str_cat(&r, "]\n");
  795. printf("%s", r);
  796. faux_free(r);
  797. }
  798. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  799. {
  800. struct lysc_range *range = NULL;
  801. LY_ARRAY_COUNT_TYPE u = 0;
  802. char *r = NULL;
  803. assert(type);
  804. range = ((struct lysc_type_num *)type)->range;
  805. // Show defaults
  806. if (!range) {
  807. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  808. return;
  809. }
  810. // Range
  811. faux_str_cat(&r, "[");
  812. LY_ARRAY_FOR(range->parts, u) {
  813. char *t = NULL;
  814. if (u != 0)
  815. faux_str_cat(&r, "|");
  816. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  817. range->parts[u].min_64, range->parts[u].max_64);
  818. faux_str_cat(&r, t);
  819. faux_str_free(t);
  820. }
  821. faux_str_cat(&r, "]\n");
  822. printf("%s", r);
  823. faux_free(r);
  824. }
  825. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  826. {
  827. struct lysc_range *range = NULL;
  828. uint8_t fraction_digits = 0;
  829. LY_ARRAY_COUNT_TYPE u = 0;
  830. char *r = NULL;
  831. int64_t div = 1;
  832. uint8_t i = 0;
  833. assert(type);
  834. range = ((struct lysc_type_dec *)type)->range;
  835. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  836. for (i = 0; i < fraction_digits; i++)
  837. div = div * 10;
  838. // Show defaults
  839. if (!range) {
  840. printf("[%.*f..%.*f]\n",
  841. fraction_digits, (double)def_min / div,
  842. fraction_digits, (double)def_max / div);
  843. return;
  844. }
  845. // Range
  846. faux_str_cat(&r, "[");
  847. LY_ARRAY_FOR(range->parts, u) {
  848. char *t = NULL;
  849. if (u != 0)
  850. faux_str_cat(&r, "|");
  851. t = faux_str_sprintf("%.*f..%.*f",
  852. fraction_digits, (double)range->parts[u].min_64 / div,
  853. fraction_digits, (double)range->parts[u].max_64 / div);
  854. faux_str_cat(&r, t);
  855. faux_str_free(t);
  856. }
  857. faux_str_cat(&r, "]\n");
  858. printf("%s", r);
  859. faux_free(r);
  860. }
  861. static void str_range(const struct lysc_type *type)
  862. {
  863. struct lysc_range *range = NULL;
  864. LY_ARRAY_COUNT_TYPE u = 0;
  865. char *r = NULL;
  866. assert(type);
  867. range = ((struct lysc_type_str *)type)->length;
  868. // Show defaults
  869. if (!range) {
  870. printf("<string>\n");
  871. return;
  872. }
  873. // Range
  874. faux_str_cat(&r, "<string[");
  875. LY_ARRAY_FOR(range->parts, u) {
  876. char *t = NULL;
  877. if (u != 0)
  878. faux_str_cat(&r, "|");
  879. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  880. range->parts[u].min_u64, range->parts[u].max_u64);
  881. faux_str_cat(&r, t);
  882. faux_str_free(t);
  883. }
  884. faux_str_cat(&r, "]>\n");
  885. printf("%s", r);
  886. faux_free(r);
  887. }
  888. static void pline_print_type_help(const struct lysc_node *node,
  889. const struct lysc_type *type)
  890. {
  891. const char *units = NULL;
  892. assert(type);
  893. assert(node);
  894. if (node->nodetype & LYS_LEAF)
  895. units = ((struct lysc_node_leaf *)node)->units;
  896. else if (node->nodetype & LYS_LEAFLIST)
  897. units = ((struct lysc_node_leaflist *)node)->units;
  898. else
  899. return;
  900. if (units) {
  901. printf("%s\n", units);
  902. } else {
  903. switch (type->basetype) {
  904. case LY_TYPE_UINT8:
  905. uint_range(type, 0, UCHAR_MAX);
  906. break;
  907. case LY_TYPE_UINT16:
  908. uint_range(type, 0, USHRT_MAX);
  909. break;
  910. case LY_TYPE_UINT32:
  911. uint_range(type, 0, UINT_MAX);
  912. break;
  913. case LY_TYPE_UINT64:
  914. uint_range(type, 0, ULLONG_MAX);
  915. break;
  916. case LY_TYPE_INT8:
  917. int_range(type, CHAR_MIN, CHAR_MAX);
  918. break;
  919. case LY_TYPE_INT16:
  920. int_range(type, SHRT_MIN, SHRT_MAX);
  921. break;
  922. case LY_TYPE_INT32:
  923. int_range(type, INT_MIN, INT_MAX);
  924. break;
  925. case LY_TYPE_INT64:
  926. int_range(type, LLONG_MIN, LLONG_MAX);
  927. break;
  928. case LY_TYPE_DEC64:
  929. dec_range(type, LLONG_MIN, LLONG_MAX);
  930. break;
  931. case LY_TYPE_STRING:
  932. str_range(type);
  933. break;
  934. case LY_TYPE_BOOL:
  935. printf("<true/false>\n");
  936. break;
  937. case LY_TYPE_LEAFREF: {
  938. const struct lysc_type_leafref *t =
  939. (const struct lysc_type_leafref *)type;
  940. const struct lysc_node *ref_node = NULL;
  941. const struct lysc_type *ref_type = NULL;
  942. char *node_path = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
  943. char *path = klysc_leafref_xpath(node, type, node_path);
  944. faux_str_free(node_path);
  945. ref_node = lys_find_path(NULL, node, path, 0);
  946. faux_str_free(path);
  947. if (!ref_node) {
  948. pline_print_type_help(node, t->realtype);
  949. return; // Because it prints whole info itself
  950. }
  951. if (ref_node->nodetype & LYS_LEAF)
  952. ref_type = ((struct lysc_node_leaf *)ref_node)->type;
  953. else
  954. ref_type = ((struct lysc_node_leaflist *)ref_node)->type;
  955. pline_print_type_help(ref_node, ref_type);
  956. return; // Because it prints whole info itself
  957. }
  958. case LY_TYPE_UNION: {
  959. const struct lysc_type_union *t =
  960. (const struct lysc_type_union *)type;
  961. LY_ARRAY_COUNT_TYPE u = 0;
  962. LY_ARRAY_FOR(t->types, u)
  963. pline_print_type_help(node, t->types[u]);
  964. return; // Because it prints whole info itself
  965. }
  966. case LY_TYPE_ENUM: {
  967. const struct lysc_type_enum *t =
  968. (const struct lysc_type_enum *)type;
  969. LY_ARRAY_COUNT_TYPE u = 0;
  970. LY_ARRAY_FOR(t->enums, u)
  971. if (t->enums[u].dsc) {
  972. char *dsc = faux_str_getline(
  973. t->enums[u].dsc, NULL);
  974. printf("%s\n%s\n",
  975. t->enums[u].name, dsc);
  976. faux_str_free(dsc);
  977. } else {
  978. printf("%s\n%s\n",
  979. t->enums[u].name,
  980. t->enums[u].name);
  981. }
  982. return; // Because it prints whole info itself
  983. }
  984. case LY_TYPE_IDENT: {
  985. struct lysc_type_identityref *t =
  986. (struct lysc_type_identityref *)type;
  987. LY_ARRAY_COUNT_TYPE u = 0;
  988. LY_ARRAY_FOR(t->bases, u)
  989. identityref_help(t->bases[u]);
  990. return; // Because it prints whole info itself
  991. }
  992. default:
  993. printf("<unknown>\n");
  994. break;
  995. }
  996. }
  997. if (node->dsc) {
  998. char *dsc = faux_str_getline(node->dsc, NULL);
  999. printf("%s\n", dsc);
  1000. faux_str_free(dsc);
  1001. } else {
  1002. printf("%s\n", node->name);
  1003. }
  1004. }
  1005. void pline_print_completions(const pline_t *pline, bool_t help)
  1006. {
  1007. faux_list_node_t *iter = NULL;
  1008. pcompl_t *pcompl = NULL;
  1009. iter = faux_list_head(pline->compls);
  1010. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1011. struct lysc_type *type = NULL;
  1012. const struct lysc_node *node = pcompl->node;
  1013. if (pcompl->xpath && !help) {
  1014. sr_val_t *vals = NULL;
  1015. size_t val_num = 0;
  1016. size_t i = 0;
  1017. //printf("%s\n", pcompl->xpath);
  1018. sr_get_items(pline->sess, pcompl->xpath,
  1019. 0, 0, &vals, &val_num);
  1020. for (i = 0; i < val_num; i++) {
  1021. char *tmp = sr_val_to_str(&vals[i]);
  1022. if (!tmp)
  1023. continue;
  1024. printf("%s\n", tmp);
  1025. free(tmp);
  1026. }
  1027. sr_free_values(vals, val_num);
  1028. }
  1029. if (!node)
  1030. continue;
  1031. // Node
  1032. if (PCOMPL_NODE == pcompl->type) {
  1033. printf("%s\n", node->name);
  1034. if (help) {
  1035. if (!node->dsc) {
  1036. printf("%s\n", node->name);
  1037. } else {
  1038. char *dsc = faux_str_getline(node->dsc,
  1039. NULL);
  1040. printf("%s\n", dsc);
  1041. faux_str_free(dsc);
  1042. }
  1043. }
  1044. continue;
  1045. }
  1046. // Type
  1047. if (node->nodetype & LYS_LEAF)
  1048. type = ((struct lysc_node_leaf *)node)->type;
  1049. else if (node->nodetype & LYS_LEAFLIST)
  1050. type = ((struct lysc_node_leaflist *)node)->type;
  1051. else
  1052. continue;
  1053. if (help)
  1054. pline_print_type_help(node, type);
  1055. else
  1056. pline_print_type_completions(type);
  1057. }
  1058. }