pline.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. printf("%s\n%s\n", ident->name,
  714. ident->dsc ? ident->dsc : ident->name);
  715. return;
  716. }
  717. LY_ARRAY_FOR(ident->derived, u) {
  718. identityref_help(ident->derived[u]);
  719. }
  720. }
  721. static void pline_print_type_completions(const struct lysc_type *type)
  722. {
  723. assert(type);
  724. switch (type->basetype) {
  725. case LY_TYPE_BOOL: {
  726. printf("true\nfalse\n");
  727. break;
  728. }
  729. case LY_TYPE_ENUM: {
  730. const struct lysc_type_enum *t =
  731. (const struct lysc_type_enum *)type;
  732. LY_ARRAY_COUNT_TYPE u = 0;
  733. LY_ARRAY_FOR(t->enums, u) {
  734. printf("%s\n",t->enums[u].name);
  735. }
  736. break;
  737. }
  738. case LY_TYPE_IDENT: {
  739. struct lysc_type_identityref *t =
  740. (struct lysc_type_identityref *)type;
  741. LY_ARRAY_COUNT_TYPE u = 0;
  742. LY_ARRAY_FOR(t->bases, u) {
  743. identityref_compl(t->bases[u]);
  744. }
  745. break;
  746. }
  747. case LY_TYPE_UNION: {
  748. struct lysc_type_union *t =
  749. (struct lysc_type_union *)type;
  750. LY_ARRAY_COUNT_TYPE u = 0;
  751. LY_ARRAY_FOR(t->types, u) {
  752. pline_print_type_completions(t->types[u]);
  753. }
  754. break;
  755. }
  756. case LY_TYPE_LEAFREF: {
  757. struct lysc_type_leafref *t =
  758. (struct lysc_type_leafref *)type;
  759. pline_print_type_completions(t->realtype);
  760. break;
  761. }
  762. default:
  763. break;
  764. }
  765. }
  766. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  767. {
  768. struct lysc_range *range = NULL;
  769. LY_ARRAY_COUNT_TYPE u = 0;
  770. char *r = NULL;
  771. assert(type);
  772. range = ((struct lysc_type_num *)type)->range;
  773. // Show defaults
  774. if (!range) {
  775. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  776. return;
  777. }
  778. // Range
  779. faux_str_cat(&r, "[");
  780. LY_ARRAY_FOR(range->parts, u) {
  781. char *t = NULL;
  782. if (u != 0)
  783. faux_str_cat(&r, "|");
  784. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  785. range->parts[u].min_u64, range->parts[u].max_u64);
  786. faux_str_cat(&r, t);
  787. faux_str_free(t);
  788. }
  789. faux_str_cat(&r, "]\n");
  790. printf("%s", r);
  791. faux_free(r);
  792. }
  793. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  794. {
  795. struct lysc_range *range = NULL;
  796. LY_ARRAY_COUNT_TYPE u = 0;
  797. char *r = NULL;
  798. assert(type);
  799. range = ((struct lysc_type_num *)type)->range;
  800. // Show defaults
  801. if (!range) {
  802. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  803. return;
  804. }
  805. // Range
  806. faux_str_cat(&r, "[");
  807. LY_ARRAY_FOR(range->parts, u) {
  808. char *t = NULL;
  809. if (u != 0)
  810. faux_str_cat(&r, "|");
  811. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  812. range->parts[u].min_64, range->parts[u].max_64);
  813. faux_str_cat(&r, t);
  814. faux_str_free(t);
  815. }
  816. faux_str_cat(&r, "]\n");
  817. printf("%s", r);
  818. faux_free(r);
  819. }
  820. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  821. {
  822. struct lysc_range *range = NULL;
  823. uint8_t fraction_digits = 0;
  824. LY_ARRAY_COUNT_TYPE u = 0;
  825. char *r = NULL;
  826. int64_t div = 1;
  827. uint8_t i = 0;
  828. assert(type);
  829. range = ((struct lysc_type_dec *)type)->range;
  830. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  831. for (i = 0; i < fraction_digits; i++)
  832. div = div * 10;
  833. // Show defaults
  834. if (!range) {
  835. printf("[%.*f..%.*f]\n",
  836. fraction_digits, (double)def_min / div,
  837. fraction_digits, (double)def_max / div);
  838. return;
  839. }
  840. // Range
  841. faux_str_cat(&r, "[");
  842. LY_ARRAY_FOR(range->parts, u) {
  843. char *t = NULL;
  844. if (u != 0)
  845. faux_str_cat(&r, "|");
  846. t = faux_str_sprintf("%.*f..%.*f",
  847. fraction_digits, (double)range->parts[u].min_64 / div,
  848. fraction_digits, (double)range->parts[u].max_64 / div);
  849. faux_str_cat(&r, t);
  850. faux_str_free(t);
  851. }
  852. faux_str_cat(&r, "]\n");
  853. printf("%s", r);
  854. faux_free(r);
  855. }
  856. static void str_range(const struct lysc_type *type)
  857. {
  858. struct lysc_range *range = NULL;
  859. LY_ARRAY_COUNT_TYPE u = 0;
  860. char *r = NULL;
  861. assert(type);
  862. range = ((struct lysc_type_str *)type)->length;
  863. // Show defaults
  864. if (!range) {
  865. printf("<string>\n");
  866. return;
  867. }
  868. // Range
  869. faux_str_cat(&r, "<string[");
  870. LY_ARRAY_FOR(range->parts, u) {
  871. char *t = NULL;
  872. if (u != 0)
  873. faux_str_cat(&r, "|");
  874. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  875. range->parts[u].min_u64, range->parts[u].max_u64);
  876. faux_str_cat(&r, t);
  877. faux_str_free(t);
  878. }
  879. faux_str_cat(&r, "]>\n");
  880. printf("%s", r);
  881. faux_free(r);
  882. }
  883. static void pline_print_type_help(const struct lysc_node *node,
  884. const struct lysc_type *type)
  885. {
  886. const char *units = NULL;
  887. assert(type);
  888. assert(node);
  889. if (node->nodetype & LYS_LEAF)
  890. units = ((struct lysc_node_leaf *)node)->units;
  891. else if (node->nodetype & LYS_LEAFLIST)
  892. units = ((struct lysc_node_leaflist *)node)->units;
  893. else
  894. return;
  895. if (units) {
  896. printf("%s\n", units);
  897. } else {
  898. switch (type->basetype) {
  899. case LY_TYPE_UINT8:
  900. uint_range(type, 0, UCHAR_MAX);
  901. break;
  902. case LY_TYPE_UINT16:
  903. uint_range(type, 0, USHRT_MAX);
  904. break;
  905. case LY_TYPE_UINT32:
  906. uint_range(type, 0, UINT_MAX);
  907. break;
  908. case LY_TYPE_UINT64:
  909. uint_range(type, 0, ULLONG_MAX);
  910. break;
  911. case LY_TYPE_INT8:
  912. int_range(type, CHAR_MIN, CHAR_MAX);
  913. break;
  914. case LY_TYPE_INT16:
  915. int_range(type, SHRT_MIN, SHRT_MAX);
  916. break;
  917. case LY_TYPE_INT32:
  918. int_range(type, INT_MIN, INT_MAX);
  919. break;
  920. case LY_TYPE_INT64:
  921. int_range(type, LLONG_MIN, LLONG_MAX);
  922. break;
  923. case LY_TYPE_DEC64:
  924. dec_range(type, LLONG_MIN, LLONG_MAX);
  925. break;
  926. case LY_TYPE_STRING:
  927. str_range(type);
  928. break;
  929. case LY_TYPE_BOOL:
  930. printf("<true/false>\n");
  931. break;
  932. case LY_TYPE_LEAFREF: {
  933. const struct lysc_type_leafref *t =
  934. (const struct lysc_type_leafref *)type;
  935. const struct lysc_node *ref_node = NULL;
  936. const struct lysc_type *ref_type = NULL;
  937. char *node_path = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
  938. char *path = klysc_leafref_xpath(node, type, node_path);
  939. faux_str_free(node_path);
  940. ref_node = lys_find_path(NULL, node, path, 0);
  941. faux_str_free(path);
  942. if (!ref_node) {
  943. pline_print_type_help(node, t->realtype);
  944. return; // Because it prints whole info itself
  945. }
  946. if (ref_node->nodetype & LYS_LEAF)
  947. ref_type = ((struct lysc_node_leaf *)ref_node)->type;
  948. else
  949. ref_type = ((struct lysc_node_leaflist *)ref_node)->type;
  950. pline_print_type_help(ref_node, ref_type);
  951. return; // Because it prints whole info itself
  952. }
  953. case LY_TYPE_UNION: {
  954. const struct lysc_type_union *t =
  955. (const struct lysc_type_union *)type;
  956. LY_ARRAY_COUNT_TYPE u = 0;
  957. LY_ARRAY_FOR(t->types, u)
  958. pline_print_type_help(node, t->types[u]);
  959. return; // Because it prints whole info itself
  960. }
  961. case LY_TYPE_ENUM: {
  962. const struct lysc_type_enum *t =
  963. (const struct lysc_type_enum *)type;
  964. LY_ARRAY_COUNT_TYPE u = 0;
  965. LY_ARRAY_FOR(t->enums, u)
  966. printf("%s\n%s\n",
  967. t->enums[u].name,
  968. t->enums[u].dsc ? t->enums[u].dsc : t->enums[u].name);
  969. return; // Because it prints whole info itself
  970. }
  971. case LY_TYPE_IDENT: {
  972. struct lysc_type_identityref *t =
  973. (struct lysc_type_identityref *)type;
  974. LY_ARRAY_COUNT_TYPE u = 0;
  975. LY_ARRAY_FOR(t->bases, u)
  976. identityref_help(t->bases[u]);
  977. return; // Because it prints whole info itself
  978. }
  979. default:
  980. printf("<unknown>\n");
  981. break;
  982. }
  983. }
  984. printf("%s\n", node->dsc ? node->dsc : node->name);
  985. }
  986. void pline_print_completions(const pline_t *pline, bool_t help)
  987. {
  988. faux_list_node_t *iter = NULL;
  989. pcompl_t *pcompl = NULL;
  990. iter = faux_list_head(pline->compls);
  991. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  992. struct lysc_type *type = NULL;
  993. const struct lysc_node *node = pcompl->node;
  994. if (pcompl->xpath && !help) {
  995. sr_val_t *vals = NULL;
  996. size_t val_num = 0;
  997. size_t i = 0;
  998. //printf("%s\n", pcompl->xpath);
  999. sr_get_items(pline->sess, pcompl->xpath,
  1000. 0, 0, &vals, &val_num);
  1001. for (i = 0; i < val_num; i++) {
  1002. char *tmp = sr_val_to_str(&vals[i]);
  1003. if (!tmp)
  1004. continue;
  1005. printf("%s\n", tmp);
  1006. free(tmp);
  1007. }
  1008. sr_free_values(vals, val_num);
  1009. }
  1010. if (!node)
  1011. continue;
  1012. // Node
  1013. if (PCOMPL_NODE == pcompl->type) {
  1014. printf("%s\n", node->name);
  1015. if (help) {
  1016. if (!node->dsc) {
  1017. printf("%s\n", node->name);
  1018. } else {
  1019. char *dsc = faux_str_getline(node->dsc,
  1020. NULL);
  1021. printf("%s\n", dsc);
  1022. faux_str_free(dsc);
  1023. }
  1024. }
  1025. continue;
  1026. }
  1027. // Type
  1028. if (node->nodetype & LYS_LEAF)
  1029. type = ((struct lysc_node_leaf *)node)->type;
  1030. else if (node->nodetype & LYS_LEAFLIST)
  1031. type = ((struct lysc_node_leaflist *)node)->type;
  1032. else
  1033. continue;
  1034. if (help)
  1035. pline_print_type_help(node, type);
  1036. else
  1037. pline_print_type_completions(type);
  1038. }
  1039. }