pline.c 29 KB

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