pline.c 28 KB

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