pline.c 30 KB

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