pline.c 29 KB

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