pline.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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 <faux/ini.h>
  15. #include <faux/conv.h>
  16. #include <sysrepo.h>
  17. #include <sysrepo/xpath.h>
  18. #include <sysrepo/values.h>
  19. #include <libyang/tree_edit.h>
  20. #include "klish_plugin_sysrepo.h"
  21. static int sr_ly_module_is_internal(const struct lys_module *ly_mod)
  22. {
  23. if (!ly_mod->revision) {
  24. return 0;
  25. }
  26. if (!strcmp(ly_mod->name, "ietf-yang-metadata")
  27. && !strcmp(ly_mod->revision, "2016-08-05")) {
  28. return 1;
  29. } else if (!strcmp(ly_mod->name, "yang")
  30. && !strcmp(ly_mod->revision, "2021-04-07")) {
  31. return 1;
  32. } else if (!strcmp(ly_mod->name, "ietf-inet-types")
  33. && !strcmp(ly_mod->revision, "2013-07-15")) {
  34. return 1;
  35. } else if (!strcmp(ly_mod->name, "ietf-yang-types")
  36. && !strcmp(ly_mod->revision, "2013-07-15")) {
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static int sr_module_is_internal(const struct lys_module *ly_mod, bool_t enable_nacm)
  42. {
  43. if (!ly_mod->revision) {
  44. return 0;
  45. }
  46. if (sr_ly_module_is_internal(ly_mod)) {
  47. return 1;
  48. }
  49. if (!strcmp(ly_mod->name, "ietf-datastores")
  50. && !strcmp(ly_mod->revision, "2018-02-14")) {
  51. return 1;
  52. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  53. return 1;
  54. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  55. return 1;
  56. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  57. return 1;
  58. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults")
  59. && !strcmp(ly_mod->revision, "2011-06-01")) {
  60. return 1;
  61. } else if (!strcmp(ly_mod->name, "ietf-origin")
  62. && !strcmp(ly_mod->revision, "2018-02-14")) {
  63. return 1;
  64. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications")
  65. && !strcmp(ly_mod->revision, "2012-02-06")) {
  66. return 1;
  67. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  68. return 1;
  69. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  70. return 1;
  71. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  72. return 1;
  73. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm") && !enable_nacm) {
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static pexpr_t *pexpr_new(void)
  79. {
  80. pexpr_t *pexpr = NULL;
  81. pexpr = faux_zmalloc(sizeof(*pexpr));
  82. assert(pexpr);
  83. if (!pexpr)
  84. return NULL;
  85. // Initialize
  86. pexpr->xpath = NULL;
  87. pexpr->value = NULL;
  88. pexpr->active = BOOL_FALSE;
  89. pexpr->pat = PAT_NONE;
  90. pexpr->args_num = 0;
  91. pexpr->list_pos = 0;
  92. pexpr->last_keys = NULL;
  93. return pexpr;
  94. }
  95. static void pexpr_free(pexpr_t *pexpr)
  96. {
  97. if (!pexpr)
  98. return;
  99. faux_str_free(pexpr->xpath);
  100. faux_str_free(pexpr->value);
  101. faux_str_free(pexpr->last_keys);
  102. free(pexpr);
  103. }
  104. static pcompl_t *pcompl_new(void)
  105. {
  106. pcompl_t *pcompl = NULL;
  107. pcompl = faux_zmalloc(sizeof(*pcompl));
  108. assert(pcompl);
  109. if (!pcompl)
  110. return NULL;
  111. // Initialize
  112. pcompl->type = PCOMPL_NODE;
  113. pcompl->node = NULL;
  114. pcompl->xpath = NULL;
  115. pcompl->xpath_ds = SRP_REPO_EDIT;
  116. pcompl->pat = PAT_NONE;
  117. return pcompl;
  118. }
  119. static void pcompl_free(pcompl_t *pcompl)
  120. {
  121. if (!pcompl)
  122. return;
  123. faux_str_free(pcompl->xpath);
  124. free(pcompl);
  125. }
  126. pline_t *pline_new(sr_session_ctx_t *sess)
  127. {
  128. pline_t *pline = NULL;
  129. pline = faux_zmalloc(sizeof(*pline));
  130. assert(pline);
  131. if (!pline)
  132. return NULL;
  133. // Init
  134. pline->sess = sess;
  135. pline->invalid = BOOL_FALSE;
  136. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  137. NULL, NULL, (faux_list_free_fn)pexpr_free);
  138. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  139. NULL, NULL, (faux_list_free_fn)pcompl_free);
  140. return pline;
  141. }
  142. void pline_free(pline_t *pline)
  143. {
  144. if (!pline)
  145. return;
  146. faux_list_free(pline->exprs);
  147. faux_list_free(pline->compls);
  148. faux_free(pline);
  149. }
  150. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  151. size_t args_num, size_t list_pos, size_t tree_depth)
  152. {
  153. pexpr_t *pexpr = NULL;
  154. assert(pline);
  155. pexpr = pexpr_new();
  156. if (xpath)
  157. pexpr->xpath = faux_str_dup(xpath);
  158. pexpr->args_num = args_num;
  159. pexpr->list_pos = list_pos;
  160. pexpr->tree_depth = tree_depth;
  161. faux_list_add(pline->exprs, pexpr);
  162. return pexpr;
  163. }
  164. pexpr_t *pline_current_expr(pline_t *pline)
  165. {
  166. assert(pline);
  167. if (faux_list_len(pline->exprs) == 0)
  168. pline_add_expr(pline, NULL, 0, 0, 0);
  169. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  170. }
  171. static void pline_add_compl(pline_t *pline,
  172. pcompl_type_e type, const struct lysc_node *node,
  173. const char *xpath, sr_datastore_t ds, pat_e pat)
  174. {
  175. pcompl_t *pcompl = NULL;
  176. assert(pline);
  177. pcompl = pcompl_new();
  178. pcompl->type = type;
  179. pcompl->node = node;
  180. pcompl->pat = pat;
  181. if (xpath) {
  182. pcompl->xpath = faux_str_dup(xpath);
  183. pcompl->xpath_ds = ds;
  184. }
  185. faux_list_add(pline->compls, pcompl);
  186. }
  187. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  188. const struct lysc_node *node, const char *xpath)
  189. {
  190. const struct lysc_node *subtree = NULL;
  191. const struct lysc_node *iter = NULL;
  192. assert(pline);
  193. assert(module);
  194. if (node)
  195. subtree = lysc_node_child(node);
  196. else
  197. subtree = module->compiled->data;
  198. LY_LIST_FOR(subtree, iter) {
  199. pat_e pat = PAT_NONE;
  200. char *node_xpath = NULL;
  201. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  202. continue;
  203. if (!(iter->flags & LYS_CONFIG_W))
  204. continue;
  205. if ((iter->nodetype & LYS_LEAF) && (iter->flags & LYS_KEY))
  206. continue;
  207. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  208. pline_add_compl_subtree(pline, module, iter, xpath);
  209. continue;
  210. }
  211. switch(iter->nodetype) {
  212. case LYS_CONTAINER:
  213. pat = PAT_CONTAINER;
  214. break;
  215. case LYS_LEAF:
  216. pat = PAT_LEAF;
  217. break;
  218. case LYS_LEAFLIST:
  219. pat = PAT_LEAFLIST;
  220. break;
  221. case LYS_LIST:
  222. pat = PAT_LIST;
  223. break;
  224. default:
  225. continue;
  226. break;
  227. }
  228. node_xpath = faux_str_sprintf("%s/%s:%s", xpath ? xpath : "",
  229. iter->module->name, iter->name);
  230. pline_add_compl(pline, PCOMPL_NODE, iter, node_xpath,
  231. SRP_REPO_EDIT, pat);
  232. faux_str_free(node_xpath);
  233. }
  234. }
  235. static const char *pat2str(pat_e pat)
  236. {
  237. const char *str = NULL;
  238. switch (pat) {
  239. case PAT_NONE:
  240. str = "NONE";
  241. break;
  242. case PAT_CONTAINER:
  243. str = "CONTAINER";
  244. break;
  245. case PAT_LIST:
  246. str = "LIST";
  247. break;
  248. case PAT_LIST_KEY:
  249. str = "LIST_KEY";
  250. break;
  251. case PAT_LIST_KEY_INCOMPLETED:
  252. str = "LIST_KEY_INCOMPLETED";
  253. break;
  254. case PAT_LEAF:
  255. str = "LEAF";
  256. break;
  257. case PAT_LEAF_VALUE:
  258. str = "LEAF_VALUE";
  259. break;
  260. case PAT_LEAF_EMPTY:
  261. str = "LEAF_EMPTY";
  262. break;
  263. case PAT_LEAFLIST:
  264. str = "LEAFLIST";
  265. break;
  266. case PAT_LEAFLIST_VALUE:
  267. str = "LEAFLIST_VALUE";
  268. break;
  269. default:
  270. str = "UNKNOWN";
  271. break;
  272. }
  273. return str;
  274. }
  275. void pline_debug(const pline_t *pline)
  276. {
  277. faux_list_node_t *iter = NULL;
  278. pexpr_t *pexpr = NULL;
  279. pcompl_t *pcompl = NULL;
  280. syslog(LOG_ERR, "====== Pline:");
  281. syslog(LOG_ERR, "invalid = %s", pline->invalid ? "true" : "false");
  282. syslog(LOG_ERR, "=== Expressions:");
  283. iter = faux_list_head(pline->exprs);
  284. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  285. syslog(LOG_ERR, "pexpr.xpath = %s", pexpr->xpath ? pexpr->xpath : "NULL");
  286. syslog(LOG_ERR, "pexpr.value = %s", pexpr->value ? pexpr->value : "NULL");
  287. syslog(LOG_ERR, "pexpr.active = %s", pexpr->active ? "true" : "false");
  288. syslog(LOG_ERR, "pexpr.pat = %s", pat2str(pexpr->pat));
  289. syslog(LOG_ERR, "pexpr.args_num = %lu", pexpr->args_num);
  290. syslog(LOG_ERR, "pexpr.list_pos = %lu", pexpr->list_pos);
  291. syslog(LOG_ERR, "pexpr.last_keys = %s", pexpr->last_keys ? pexpr->last_keys : "NULL");
  292. syslog(LOG_ERR, "pexpr.tree_depth = %lu", pexpr->tree_depth);
  293. syslog(LOG_ERR, "---");
  294. }
  295. syslog(LOG_ERR, "=== Completions:");
  296. iter = faux_list_head(pline->compls);
  297. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  298. syslog(LOG_ERR, "pcompl.type = %s", (pcompl->type == PCOMPL_NODE) ?
  299. "PCOMPL_NODE" : "PCOMPL_TYPE");
  300. syslog(LOG_ERR, "pcompl.node = %s", pcompl->node ? pcompl->node->name : "NULL");
  301. syslog(LOG_ERR, "pcompl.xpath = %s", pcompl->xpath ? pcompl->xpath : "NULL");
  302. syslog(LOG_ERR, "pcompl.pat = %s", pat2str(pcompl->pat));
  303. syslog(LOG_ERR, "---");
  304. }
  305. }
  306. static bool_t pexpr_xpath_add_node(pexpr_t *pexpr,
  307. const char *prefix, const char *name)
  308. {
  309. char *tmp = NULL;
  310. assert(pexpr);
  311. assert(prefix);
  312. assert(name);
  313. tmp = faux_str_sprintf("/%s:%s", prefix, name);
  314. faux_str_cat(&pexpr->xpath, tmp);
  315. faux_str_free(tmp);
  316. pexpr->args_num++;
  317. // Activate current expression. Because it really has
  318. // new component
  319. pexpr->active = BOOL_TRUE;
  320. return BOOL_TRUE;
  321. }
  322. static bool_t pexpr_xpath_add_list_key(pexpr_t *pexpr,
  323. const char *key, const char *value, bool_t inc_args_num)
  324. {
  325. char *tmp = NULL;
  326. char *escaped = NULL;
  327. assert(pexpr);
  328. assert(key);
  329. assert(value);
  330. escaped = faux_str_c_esc(value);
  331. tmp = faux_str_sprintf("[%s=\"%s\"]", key, escaped);
  332. faux_str_free(escaped);
  333. faux_str_cat(&pexpr->xpath, tmp);
  334. faux_str_cat(&pexpr->last_keys, tmp);
  335. faux_str_free(tmp);
  336. if (inc_args_num)
  337. pexpr->args_num++;
  338. return BOOL_TRUE;
  339. }
  340. static bool_t pexpr_xpath_add_leaflist_key(pexpr_t *pexpr,
  341. const char *prefix, const char *value)
  342. {
  343. char *tmp = NULL;
  344. assert(pexpr);
  345. assert(value);
  346. tmp = faux_str_sprintf("[.='%s%s%s']",
  347. prefix ? prefix : "", prefix ? ":" : "", value);
  348. faux_str_cat(&pexpr->xpath, tmp);
  349. faux_str_cat(&pexpr->last_keys, value);
  350. faux_str_free(tmp);
  351. pexpr->args_num++;
  352. return BOOL_TRUE;
  353. }
  354. static void pline_add_compl_leafref(pline_t *pline, const struct lysc_node *node,
  355. const struct lysc_type *type, const char *xpath, pat_e pat)
  356. {
  357. if (!type)
  358. return;
  359. if (!node)
  360. return;
  361. if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  362. return;
  363. switch (type->basetype) {
  364. case LY_TYPE_UNION: {
  365. struct lysc_type_union *t =
  366. (struct lysc_type_union *)type;
  367. LY_ARRAY_COUNT_TYPE u = 0;
  368. LY_ARRAY_FOR(t->types, u) {
  369. pline_add_compl_leafref(pline, node, t->types[u], xpath, pat);
  370. }
  371. break;
  372. }
  373. case LY_TYPE_LEAFREF: {
  374. char *compl_xpath = klysc_leafref_xpath(node, type, xpath);
  375. pline_add_compl(pline, PCOMPL_TYPE, NULL, compl_xpath, SRP_REPO_EDIT, pat);
  376. faux_str_free(compl_xpath);
  377. break;
  378. }
  379. default:
  380. break;
  381. }
  382. }
  383. static void pline_add_compl_leaf(pline_t *pline, const struct lysc_node *node,
  384. const char *xpath, pat_e pat)
  385. {
  386. struct lysc_type *type = NULL;
  387. const char *ext_xpath = NULL;
  388. assert(pline);
  389. if (!pline)
  390. return;
  391. assert(node);
  392. if (!node)
  393. return;
  394. switch (node->nodetype) {
  395. case LYS_LEAF:
  396. type = ((struct lysc_node_leaf *)node)->type;
  397. break;
  398. case LYS_LEAFLIST:
  399. type = ((struct lysc_node_leaflist *)node)->type;
  400. break;
  401. default:
  402. return;
  403. }
  404. ext_xpath = klysc_node_ext_completion(node);
  405. if (ext_xpath) {
  406. const char *raw_xpath = NULL;
  407. sr_datastore_t ds = SRP_REPO_EDIT;
  408. if (kly_parse_ext_xpath(ext_xpath, &raw_xpath, &ds))
  409. pline_add_compl(pline, PCOMPL_TYPE, NULL, raw_xpath, ds, pat);
  410. }
  411. pline_add_compl(pline, PCOMPL_TYPE, node, xpath, SRP_REPO_EDIT, pat);
  412. pline_add_compl_leafref(pline, node, type, xpath, pat);
  413. }
  414. static bool_t pline_parse_module(const struct lys_module *module,
  415. const faux_argv_t *argv, pline_t *pline, const pline_opts_t *opts)
  416. {
  417. faux_argv_node_t *arg = faux_argv_iter(argv);
  418. const struct lysc_node *node = NULL;
  419. char *rollback_xpath = NULL;
  420. size_t rollback_args_num = 0;
  421. size_t rollback_list_pos = 0;
  422. size_t rollback_tree_depth = 0;
  423. // Rollback is a mechanism to roll to previous node while
  424. // oneliners parsing
  425. bool_t rollback = BOOL_FALSE;
  426. pexpr_t *first_pexpr = NULL;
  427. // It's necessary because upper function can use the same pline object
  428. // for another modules before. It uses the same object to collect
  429. // possible completions. But pline is really invalid only when all
  430. // modules don't recognize argument.
  431. pline->invalid = BOOL_FALSE;
  432. do {
  433. pexpr_t *pexpr = pline_current_expr(pline);
  434. const char *str = (const char *)faux_argv_current(arg);
  435. bool_t is_rollback = rollback;
  436. bool_t next_arg = BOOL_TRUE;
  437. rollback = BOOL_FALSE;
  438. if (node && !is_rollback) {
  439. // Save rollback Xpath (for oneliners) before leaf node
  440. // Only leaf and leaf-list node allows to "rollback"
  441. // the path and add additional statements
  442. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  443. faux_str_free(rollback_xpath);
  444. rollback_xpath = faux_str_dup(pexpr->xpath);
  445. rollback_args_num = pexpr->args_num;
  446. rollback_list_pos = pexpr->list_pos;
  447. rollback_tree_depth = pexpr->tree_depth;
  448. }
  449. // Add current node to Xpath
  450. pexpr_xpath_add_node(pexpr,
  451. node->module->name, node->name);
  452. }
  453. // Root of the module
  454. if (!node) {
  455. // Completion
  456. if (!str) {
  457. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  458. break;
  459. }
  460. // Next element
  461. node = klysc_find_child(module->compiled->data, str);
  462. if (!node)
  463. break;
  464. // Container
  465. } else if (node->nodetype & LYS_CONTAINER) {
  466. pexpr->pat = PAT_CONTAINER;
  467. pexpr->tree_depth++;
  468. // Completion
  469. if (!str) {
  470. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  471. break;
  472. }
  473. // Next element
  474. node = klysc_find_child(lysc_node_child(node), str);
  475. // List
  476. } else if (node->nodetype & LYS_LIST) {
  477. const struct lysc_node *iter = NULL;
  478. pexpr->pat = PAT_LIST;
  479. pexpr->list_pos = pexpr->args_num;
  480. faux_str_free(pexpr->last_keys);
  481. pexpr->last_keys = NULL;
  482. // Next element
  483. if (!is_rollback) {
  484. bool_t break_upper_loop = BOOL_FALSE;
  485. // Keys without statement. Positional parameters.
  486. if (!opts->keys_w_stmt) {
  487. LY_LIST_FOR(lysc_node_child(node), iter) {
  488. struct lysc_node_leaf *leaf =
  489. (struct lysc_node_leaf *)iter;
  490. if (!(iter->nodetype & LYS_LEAF))
  491. continue;
  492. if (!(iter->flags & LYS_KEY))
  493. continue;
  494. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  495. // Completion
  496. if (!str) {
  497. char *tmp = faux_str_sprintf("%s/%s",
  498. pexpr->xpath, leaf->name);
  499. pline_add_compl_leaf(pline, iter,
  500. tmp, PAT_LIST_KEY);
  501. faux_str_free(tmp);
  502. break_upper_loop = BOOL_TRUE;
  503. break;
  504. }
  505. pexpr_xpath_add_list_key(pexpr,
  506. leaf->name, str, BOOL_TRUE);
  507. faux_argv_each(&arg);
  508. str = (const char *)faux_argv_current(arg);
  509. pexpr->pat = PAT_LIST_KEY;
  510. }
  511. // Keys with statements. Arbitrary order of keys.
  512. } else {
  513. faux_list_t *keys = NULL;
  514. unsigned int specified_keys_num = 0;
  515. klysc_key_t *cur_key = NULL;
  516. char *xpath_wo_default_keys = NULL;
  517. bool_t first_key = BOOL_TRUE;
  518. bool_t first_key_is_optional = BOOL_FALSE;
  519. faux_list_node_t *key_iter = NULL;
  520. // List keys
  521. keys = faux_list_new(FAUX_LIST_UNSORTED,
  522. FAUX_LIST_UNIQUE,
  523. klysc_key_compare,
  524. klysc_key_kcompare,
  525. (faux_list_free_fn)faux_free);
  526. LY_LIST_FOR(lysc_node_child(node), iter) {
  527. struct lysc_node_leaf *leaf =
  528. (struct lysc_node_leaf *)iter;
  529. klysc_key_t *key = NULL;
  530. if (!(iter->nodetype & LYS_LEAF))
  531. continue;
  532. if (!(iter->flags & LYS_KEY))
  533. continue;
  534. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  535. key = faux_zmalloc(sizeof(*key));
  536. assert(key);
  537. key->node = iter;
  538. if (opts->default_keys &&
  539. (key->dflt = klysc_node_ext_default(iter))) {
  540. if (first_key)
  541. first_key_is_optional = BOOL_TRUE;
  542. }
  543. faux_list_add(keys, key);
  544. first_key = BOOL_FALSE;
  545. }
  546. while (specified_keys_num < faux_list_len(keys)) {
  547. // First key without statement. Must be mandatory.
  548. if ((0 == specified_keys_num) &&
  549. !opts->first_key_w_stmt &&
  550. !first_key_is_optional) {
  551. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  552. } else {
  553. if (!str)
  554. break;
  555. cur_key = faux_list_kfind(keys, str);
  556. if (!cur_key || cur_key->value)
  557. break;
  558. pexpr->args_num++;
  559. faux_argv_each(&arg);
  560. str = (const char *)faux_argv_current(arg);
  561. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  562. }
  563. // Completion
  564. if (!str) {
  565. char *tmp = faux_str_sprintf("%s/%s",
  566. pexpr->xpath,
  567. cur_key->node->name);
  568. pline_add_compl_leaf(pline, cur_key->node,
  569. tmp, PAT_LIST_KEY);
  570. faux_str_free(tmp);
  571. break_upper_loop = BOOL_TRUE;
  572. break;
  573. }
  574. pexpr_xpath_add_list_key(pexpr,
  575. cur_key->node->name, str, BOOL_TRUE);
  576. cur_key->value = str;
  577. specified_keys_num++;
  578. faux_argv_each(&arg);
  579. str = (const char *)faux_argv_current(arg);
  580. pexpr->pat = PAT_LIST_KEY;
  581. }
  582. if (break_upper_loop) {
  583. faux_list_free(keys);
  584. break;
  585. }
  586. xpath_wo_default_keys = faux_str_dup(pexpr->xpath);
  587. key_iter = faux_list_head(keys);
  588. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  589. if (cur_key->value)
  590. continue;
  591. // Completion
  592. if (!str) {
  593. char *tmp = faux_str_sprintf("%s/%s",
  594. xpath_wo_default_keys,
  595. cur_key->node->name);
  596. pline_add_compl(pline, PCOMPL_NODE,
  597. cur_key->node, tmp,
  598. SRP_REPO_EDIT, PAT_LIST_KEY_INCOMPLETED);
  599. faux_str_free(tmp);
  600. }
  601. if (opts->default_keys && cur_key->dflt) {
  602. pexpr_xpath_add_list_key(pexpr,
  603. cur_key->node->name,
  604. cur_key->dflt, BOOL_FALSE);
  605. pexpr->pat = PAT_LIST_KEY;
  606. } else { // Mandatory key is not specified
  607. break_upper_loop = BOOL_TRUE;
  608. }
  609. }
  610. faux_str_free(xpath_wo_default_keys);
  611. faux_list_free(keys);
  612. }
  613. if (break_upper_loop)
  614. break;
  615. }
  616. pexpr->tree_depth++;
  617. // Completion
  618. if (!str) {
  619. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  620. break;
  621. }
  622. // Next element
  623. node = klysc_find_child(lysc_node_child(node), str);
  624. // Leaf
  625. } else if (node->nodetype & LYS_LEAF) {
  626. struct lysc_node_leaf *leaf =
  627. (struct lysc_node_leaf *)node;
  628. // Next element
  629. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  630. pexpr->pat = PAT_LEAF_EMPTY;
  631. // Completion
  632. if (!str) {
  633. pline_add_compl_subtree(pline,
  634. module, node->parent, pexpr->xpath);
  635. break;
  636. }
  637. // Don't get next argument when argument is not
  638. // really consumed
  639. next_arg = BOOL_FALSE;
  640. } else {
  641. pexpr->pat = PAT_LEAF;
  642. // Completion
  643. if (!str) {
  644. pline_add_compl_leaf(pline, node,
  645. pexpr->xpath, PAT_LEAF_VALUE);
  646. break;
  647. }
  648. pexpr->pat = PAT_LEAF_VALUE;
  649. // Idenity must have prefix
  650. if (LY_TYPE_IDENT == leaf->type->basetype) {
  651. const char *prefix = NULL;
  652. prefix = klysc_identityref_prefix(
  653. (struct lysc_type_identityref *)
  654. leaf->type, str);
  655. if (prefix)
  656. pexpr->value = faux_str_sprintf(
  657. "%s:", prefix);
  658. }
  659. faux_str_cat(&pexpr->value, str);
  660. }
  661. // Expression was completed
  662. // So rollback (for oneliners)
  663. node = node->parent;
  664. pline_add_expr(pline, rollback_xpath,
  665. rollback_args_num, rollback_list_pos,
  666. rollback_tree_depth);
  667. rollback = BOOL_TRUE;
  668. // Leaf-list
  669. } else if (node->nodetype & LYS_LEAFLIST) {
  670. const char *prefix = NULL;
  671. struct lysc_node_leaflist *leaflist =
  672. (struct lysc_node_leaflist *)node;
  673. pexpr->pat = PAT_LEAFLIST;
  674. pexpr->list_pos = pexpr->args_num;
  675. faux_str_free(pexpr->last_keys);
  676. pexpr->last_keys = NULL;
  677. // Completion
  678. if (!str) {
  679. pline_add_compl_leaf(pline, node,
  680. pexpr->xpath, PAT_LEAFLIST_VALUE);
  681. break;
  682. }
  683. pexpr->pat = PAT_LEAFLIST_VALUE;
  684. // Idenity must have prefix
  685. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  686. prefix = klysc_identityref_prefix(
  687. (struct lysc_type_identityref *)
  688. leaflist->type, str);
  689. }
  690. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  691. // Expression was completed
  692. // So rollback (for oneliners)
  693. node = node->parent;
  694. pline_add_expr(pline, rollback_xpath,
  695. rollback_args_num, rollback_list_pos,
  696. rollback_tree_depth);
  697. rollback = BOOL_TRUE;
  698. // LYS_CHOICE and LYS_CASE can appear while rollback only
  699. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  700. // Don't set pexpr->pat because CHOICE and CASE can't
  701. // appear within data tree (schema only)
  702. // Completion
  703. if (!str) {
  704. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  705. break;
  706. }
  707. // Next element
  708. node = klysc_find_child(lysc_node_child(node), str);
  709. } else {
  710. break;
  711. }
  712. // Current argument was not consumed.
  713. // Break before getting next arg.
  714. if (!node && !rollback)
  715. break;
  716. if (next_arg)
  717. faux_argv_each(&arg);
  718. } while (BOOL_TRUE);
  719. // There is not-consumed argument so whole pline is invalid
  720. if (faux_argv_current(arg))
  721. pline->invalid = BOOL_TRUE;
  722. faux_str_free(rollback_xpath);
  723. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  724. if (!first_pexpr || !first_pexpr->xpath)
  725. return BOOL_FALSE; // Not found
  726. return BOOL_TRUE;
  727. }
  728. pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
  729. const pline_opts_t *opts)
  730. {
  731. const struct ly_ctx *ctx = NULL;
  732. struct lys_module *module = NULL;
  733. pline_t *pline = NULL;
  734. uint32_t i = 0;
  735. faux_list_node_t *last_expr_node = NULL;
  736. assert(sess);
  737. if (!sess)
  738. return NULL;
  739. pline = pline_new(sess);
  740. if (!pline)
  741. return NULL;
  742. ctx = sr_session_acquire_context(pline->sess);
  743. if (!ctx)
  744. return NULL;
  745. // Iterate all modules
  746. i = 0;
  747. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  748. if (sr_module_is_internal(module, opts->enable_nacm))
  749. continue;
  750. if (!module->compiled)
  751. continue;
  752. if (!module->implemented)
  753. continue;
  754. if (!module->compiled->data)
  755. continue;
  756. if (pline_parse_module(module, argv, pline, opts))
  757. break; // Found
  758. }
  759. sr_session_release_context(pline->sess);
  760. // Last parsed expression can be inactive so remove it from list
  761. last_expr_node = faux_list_tail(pline->exprs);
  762. if (last_expr_node) {
  763. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  764. if (!expr->active)
  765. faux_list_del(pline->exprs, last_expr_node);
  766. }
  767. return pline;
  768. }
  769. static void identityref_compl(struct lysc_ident *ident)
  770. {
  771. LY_ARRAY_COUNT_TYPE u = 0;
  772. if (!ident)
  773. return;
  774. if (!ident->derived) {
  775. printf("%s\n", ident->name);
  776. return;
  777. }
  778. LY_ARRAY_FOR(ident->derived, u) {
  779. identityref_compl(ident->derived[u]);
  780. }
  781. }
  782. static void identityref_help(struct lysc_ident *ident)
  783. {
  784. LY_ARRAY_COUNT_TYPE u = 0;
  785. if (!ident)
  786. return;
  787. if (!ident->derived) {
  788. if (ident->dsc) {
  789. char *dsc = faux_str_getline(ident->dsc, NULL);
  790. printf("%s\n%s\n", ident->name, dsc);
  791. faux_str_free(dsc);
  792. } else {
  793. printf("%s\n%s\n", ident->name, ident->name);
  794. }
  795. return;
  796. }
  797. LY_ARRAY_FOR(ident->derived, u) {
  798. identityref_help(ident->derived[u]);
  799. }
  800. }
  801. static void pline_print_type_completions(const struct lysc_type *type)
  802. {
  803. assert(type);
  804. switch (type->basetype) {
  805. case LY_TYPE_BOOL: {
  806. printf("true\nfalse\n");
  807. break;
  808. }
  809. case LY_TYPE_ENUM: {
  810. const struct lysc_type_enum *t =
  811. (const struct lysc_type_enum *)type;
  812. LY_ARRAY_COUNT_TYPE u = 0;
  813. LY_ARRAY_FOR(t->enums, u) {
  814. printf("%s\n",t->enums[u].name);
  815. }
  816. break;
  817. }
  818. case LY_TYPE_IDENT: {
  819. struct lysc_type_identityref *t =
  820. (struct lysc_type_identityref *)type;
  821. LY_ARRAY_COUNT_TYPE u = 0;
  822. LY_ARRAY_FOR(t->bases, u) {
  823. identityref_compl(t->bases[u]);
  824. }
  825. break;
  826. }
  827. case LY_TYPE_UNION: {
  828. struct lysc_type_union *t =
  829. (struct lysc_type_union *)type;
  830. LY_ARRAY_COUNT_TYPE u = 0;
  831. LY_ARRAY_FOR(t->types, u) {
  832. pline_print_type_completions(t->types[u]);
  833. }
  834. break;
  835. }
  836. case LY_TYPE_LEAFREF: {
  837. struct lysc_type_leafref *t =
  838. (struct lysc_type_leafref *)type;
  839. pline_print_type_completions(t->realtype);
  840. break;
  841. }
  842. default:
  843. break;
  844. }
  845. }
  846. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  847. {
  848. struct lysc_range *range = NULL;
  849. LY_ARRAY_COUNT_TYPE u = 0;
  850. char *r = NULL;
  851. assert(type);
  852. range = ((struct lysc_type_num *)type)->range;
  853. // Show defaults
  854. if (!range) {
  855. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  856. return;
  857. }
  858. // Range
  859. faux_str_cat(&r, "[");
  860. LY_ARRAY_FOR(range->parts, u) {
  861. char *t = NULL;
  862. if (u != 0)
  863. faux_str_cat(&r, "|");
  864. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  865. range->parts[u].min_u64, range->parts[u].max_u64);
  866. faux_str_cat(&r, t);
  867. faux_str_free(t);
  868. }
  869. faux_str_cat(&r, "]\n");
  870. printf("%s", r);
  871. faux_free(r);
  872. }
  873. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  874. {
  875. struct lysc_range *range = NULL;
  876. LY_ARRAY_COUNT_TYPE u = 0;
  877. char *r = NULL;
  878. assert(type);
  879. range = ((struct lysc_type_num *)type)->range;
  880. // Show defaults
  881. if (!range) {
  882. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  883. return;
  884. }
  885. // Range
  886. faux_str_cat(&r, "[");
  887. LY_ARRAY_FOR(range->parts, u) {
  888. char *t = NULL;
  889. if (u != 0)
  890. faux_str_cat(&r, "|");
  891. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  892. range->parts[u].min_64, range->parts[u].max_64);
  893. faux_str_cat(&r, t);
  894. faux_str_free(t);
  895. }
  896. faux_str_cat(&r, "]\n");
  897. printf("%s", r);
  898. faux_free(r);
  899. }
  900. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  901. {
  902. struct lysc_range *range = NULL;
  903. uint8_t fraction_digits = 0;
  904. LY_ARRAY_COUNT_TYPE u = 0;
  905. char *r = NULL;
  906. int64_t div = 1;
  907. uint8_t i = 0;
  908. assert(type);
  909. range = ((struct lysc_type_dec *)type)->range;
  910. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  911. for (i = 0; i < fraction_digits; i++)
  912. div = div * 10;
  913. // Show defaults
  914. if (!range) {
  915. printf("[%.*f..%.*f]\n",
  916. fraction_digits, (double)def_min / div,
  917. fraction_digits, (double)def_max / div);
  918. return;
  919. }
  920. // Range
  921. faux_str_cat(&r, "[");
  922. LY_ARRAY_FOR(range->parts, u) {
  923. char *t = NULL;
  924. if (u != 0)
  925. faux_str_cat(&r, "|");
  926. t = faux_str_sprintf("%.*f..%.*f",
  927. fraction_digits, (double)range->parts[u].min_64 / div,
  928. fraction_digits, (double)range->parts[u].max_64 / div);
  929. faux_str_cat(&r, t);
  930. faux_str_free(t);
  931. }
  932. faux_str_cat(&r, "]\n");
  933. printf("%s", r);
  934. faux_free(r);
  935. }
  936. static void str_range(const struct lysc_type *type)
  937. {
  938. struct lysc_range *range = NULL;
  939. LY_ARRAY_COUNT_TYPE u = 0;
  940. char *r = NULL;
  941. assert(type);
  942. range = ((struct lysc_type_str *)type)->length;
  943. // Show defaults
  944. if (!range) {
  945. printf("<string>\n");
  946. return;
  947. }
  948. // Range
  949. faux_str_cat(&r, "<string[");
  950. LY_ARRAY_FOR(range->parts, u) {
  951. char *t = NULL;
  952. if (u != 0)
  953. faux_str_cat(&r, "|");
  954. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  955. range->parts[u].min_u64, range->parts[u].max_u64);
  956. faux_str_cat(&r, t);
  957. faux_str_free(t);
  958. }
  959. faux_str_cat(&r, "]>\n");
  960. printf("%s", r);
  961. faux_free(r);
  962. }
  963. static void pline_print_type_help(const struct lysc_node *node,
  964. const struct lysc_type *type)
  965. {
  966. const char *units = NULL;
  967. assert(type);
  968. assert(node);
  969. if (node->nodetype & LYS_LEAF)
  970. units = ((struct lysc_node_leaf *)node)->units;
  971. else if (node->nodetype & LYS_LEAFLIST)
  972. units = ((struct lysc_node_leaflist *)node)->units;
  973. else
  974. return;
  975. if (units) {
  976. printf("%s\n", units);
  977. } else {
  978. switch (type->basetype) {
  979. case LY_TYPE_UINT8:
  980. uint_range(type, 0, UCHAR_MAX);
  981. break;
  982. case LY_TYPE_UINT16:
  983. uint_range(type, 0, USHRT_MAX);
  984. break;
  985. case LY_TYPE_UINT32:
  986. uint_range(type, 0, UINT_MAX);
  987. break;
  988. case LY_TYPE_UINT64:
  989. uint_range(type, 0, ULLONG_MAX);
  990. break;
  991. case LY_TYPE_INT8:
  992. int_range(type, CHAR_MIN, CHAR_MAX);
  993. break;
  994. case LY_TYPE_INT16:
  995. int_range(type, SHRT_MIN, SHRT_MAX);
  996. break;
  997. case LY_TYPE_INT32:
  998. int_range(type, INT_MIN, INT_MAX);
  999. break;
  1000. case LY_TYPE_INT64:
  1001. int_range(type, LLONG_MIN, LLONG_MAX);
  1002. break;
  1003. case LY_TYPE_DEC64:
  1004. dec_range(type, LLONG_MIN, LLONG_MAX);
  1005. break;
  1006. case LY_TYPE_STRING:
  1007. str_range(type);
  1008. break;
  1009. case LY_TYPE_BOOL:
  1010. printf("<true/false>\n");
  1011. break;
  1012. case LY_TYPE_LEAFREF: {
  1013. const struct lysc_type_leafref *t =
  1014. (const struct lysc_type_leafref *)type;
  1015. const struct lysc_node *ref_node = NULL;
  1016. const struct lysc_type *ref_type = NULL;
  1017. char *node_path = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
  1018. char *path = klysc_leafref_xpath(node, type, node_path);
  1019. faux_str_free(node_path);
  1020. ref_node = lys_find_path(NULL, node, path, 0);
  1021. faux_str_free(path);
  1022. if (!ref_node) {
  1023. pline_print_type_help(node, t->realtype);
  1024. return; // Because it prints whole info itself
  1025. }
  1026. if (ref_node->nodetype & LYS_LEAF)
  1027. ref_type = ((struct lysc_node_leaf *)ref_node)->type;
  1028. else
  1029. ref_type = ((struct lysc_node_leaflist *)ref_node)->type;
  1030. pline_print_type_help(ref_node, ref_type);
  1031. return; // Because it prints whole info itself
  1032. }
  1033. case LY_TYPE_UNION: {
  1034. const struct lysc_type_union *t =
  1035. (const struct lysc_type_union *)type;
  1036. LY_ARRAY_COUNT_TYPE u = 0;
  1037. LY_ARRAY_FOR(t->types, u)
  1038. pline_print_type_help(node, t->types[u]);
  1039. return; // Because it prints whole info itself
  1040. }
  1041. case LY_TYPE_ENUM: {
  1042. const struct lysc_type_enum *t =
  1043. (const struct lysc_type_enum *)type;
  1044. LY_ARRAY_COUNT_TYPE u = 0;
  1045. LY_ARRAY_FOR(t->enums, u)
  1046. if (t->enums[u].dsc) {
  1047. char *dsc = faux_str_getline(
  1048. t->enums[u].dsc, NULL);
  1049. printf("%s\n%s\n",
  1050. t->enums[u].name, dsc);
  1051. faux_str_free(dsc);
  1052. } else {
  1053. printf("%s\n%s\n",
  1054. t->enums[u].name,
  1055. t->enums[u].name);
  1056. }
  1057. return; // Because it prints whole info itself
  1058. }
  1059. case LY_TYPE_IDENT: {
  1060. struct lysc_type_identityref *t =
  1061. (struct lysc_type_identityref *)type;
  1062. LY_ARRAY_COUNT_TYPE u = 0;
  1063. LY_ARRAY_FOR(t->bases, u)
  1064. identityref_help(t->bases[u]);
  1065. return; // Because it prints whole info itself
  1066. }
  1067. default:
  1068. printf("<unknown>\n");
  1069. break;
  1070. }
  1071. }
  1072. if (node->dsc) {
  1073. char *dsc = faux_str_getline(node->dsc, NULL);
  1074. printf("%s\n", dsc);
  1075. faux_str_free(dsc);
  1076. } else {
  1077. printf("%s\n", node->name);
  1078. }
  1079. }
  1080. static bool_t pline_find_node_within_tree(const struct lyd_node *nodes_list,
  1081. const struct lysc_node *node)
  1082. {
  1083. const struct lyd_node *iter = NULL;
  1084. if (!nodes_list)
  1085. return BOOL_FALSE;
  1086. LY_LIST_FOR(nodes_list, iter) {
  1087. const char *default_value = NULL;
  1088. char *value = NULL;
  1089. if (iter->schema != node) {
  1090. if (pline_find_node_within_tree(lyd_child(iter),
  1091. node))
  1092. return BOOL_TRUE;
  1093. continue;
  1094. }
  1095. if (iter->flags & LYD_DEFAULT) {
  1096. continue;
  1097. }
  1098. default_value = klysc_node_ext_default(iter->schema);
  1099. value = klyd_node_value(iter);
  1100. // Don't show "default" keys with default values
  1101. if (default_value && faux_str_cmp(default_value, value) == 0) {
  1102. faux_str_free(value);
  1103. continue;
  1104. }
  1105. faux_str_free(value);
  1106. return BOOL_TRUE;
  1107. }
  1108. return BOOL_FALSE;
  1109. }
  1110. static bool_t pline_node_exists(sr_session_ctx_t* sess, const char *xpath,
  1111. const struct lysc_node *node)
  1112. {
  1113. sr_data_t *data = NULL;
  1114. bool_t found = BOOL_FALSE;
  1115. if (!xpath)
  1116. return BOOL_FALSE;
  1117. if (sr_get_data(sess, xpath, 1, 0, 0, &data) != SR_ERR_OK)
  1118. return BOOL_FALSE;
  1119. if (!data) // Not found
  1120. return BOOL_FALSE;
  1121. if (pline_find_node_within_tree(data->tree, node))
  1122. found = BOOL_TRUE;
  1123. sr_release_data(data);
  1124. return found;
  1125. }
  1126. void pline_print_completions(const pline_t *pline, bool_t help,
  1127. pt_e enabled_types, bool_t existing_nodes_only)
  1128. {
  1129. faux_list_node_t *iter = NULL;
  1130. pcompl_t *pcompl = NULL;
  1131. sr_datastore_t current_ds = SRP_REPO_EDIT;
  1132. iter = faux_list_head(pline->compls);
  1133. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1134. struct lysc_type *type = NULL;
  1135. const struct lysc_node *node = pcompl->node;
  1136. if (!(pcompl->pat & enabled_types))
  1137. continue;
  1138. // Switch to necessary DS
  1139. if (pcompl->xpath && (current_ds != pcompl->xpath_ds)) {
  1140. sr_session_switch_ds(pline->sess, pcompl->xpath_ds);
  1141. current_ds = pcompl->xpath_ds;
  1142. }
  1143. // Help
  1144. if (help) {
  1145. // Note we can't show help without valid node
  1146. if (!node)
  1147. continue;
  1148. // Type (help)
  1149. if (pcompl->type == PCOMPL_TYPE) {
  1150. if (node->nodetype & LYS_LEAF)
  1151. type = ((struct lysc_node_leaf *)node)->type;
  1152. else if (node->nodetype & LYS_LEAFLIST)
  1153. type = ((struct lysc_node_leaflist *)node)->type;
  1154. else
  1155. continue;
  1156. pline_print_type_help(node, type);
  1157. continue;
  1158. }
  1159. // Check node for existing if necessary
  1160. if (existing_nodes_only &&
  1161. !pline_node_exists(pline->sess, pcompl->xpath,
  1162. node)) {
  1163. continue;
  1164. }
  1165. // Node (help)
  1166. if (!node->dsc) {
  1167. printf("%s\n%s\n", node->name, node->name);
  1168. } else {
  1169. char *dsc = faux_str_getline(node->dsc,
  1170. NULL);
  1171. printf("%s\n%s\n", node->name, dsc);
  1172. faux_str_free(dsc);
  1173. }
  1174. // Completion
  1175. } else {
  1176. // Type (completion)
  1177. if (pcompl->type == PCOMPL_TYPE) {
  1178. // Existing entries
  1179. if (pcompl->xpath) {
  1180. size_t i = 0;
  1181. sr_val_t *vals = NULL;
  1182. size_t val_num = 0;
  1183. sr_get_items(pline->sess, pcompl->xpath,
  1184. 0, 0, &vals, &val_num);
  1185. for (i = 0; i < val_num; i++) {
  1186. char *tmp = sr_val_to_str(&vals[i]);
  1187. char *esc_tmp = NULL;
  1188. if (!tmp)
  1189. continue;
  1190. esc_tmp = faux_str_c_esc_space(tmp);
  1191. free(tmp);
  1192. printf("%s\n", esc_tmp);
  1193. free(esc_tmp);
  1194. }
  1195. sr_free_values(vals, val_num);
  1196. }
  1197. if (!node)
  1198. continue;
  1199. if (existing_nodes_only)
  1200. continue;
  1201. // All entries
  1202. if (node->nodetype & LYS_LEAF)
  1203. type = ((struct lysc_node_leaf *)node)->type;
  1204. else if (node->nodetype & LYS_LEAFLIST)
  1205. type = ((struct lysc_node_leaflist *)node)->type;
  1206. else
  1207. continue;
  1208. pline_print_type_completions(type);
  1209. continue;
  1210. }
  1211. // Node (completion)
  1212. if (!node)
  1213. continue;
  1214. // Existing entries
  1215. if (existing_nodes_only &&
  1216. !pline_node_exists(pline->sess, pcompl->xpath,
  1217. node)) {
  1218. continue;
  1219. }
  1220. printf("%s\n", node->name);
  1221. } // Completion
  1222. } // while
  1223. // Restore default DS
  1224. if (current_ds != SRP_REPO_EDIT)
  1225. sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
  1226. }
  1227. void pline_opts_init(pline_opts_t *opts)
  1228. {
  1229. opts->begin_bracket = '{';
  1230. opts->end_bracket = '}';
  1231. opts->show_brackets = BOOL_TRUE;
  1232. opts->show_semicolons = BOOL_TRUE;
  1233. opts->first_key_w_stmt = BOOL_FALSE;
  1234. opts->keys_w_stmt = BOOL_TRUE;
  1235. opts->colorize = BOOL_TRUE;
  1236. opts->indent = 2;
  1237. opts->default_keys = BOOL_FALSE;
  1238. opts->show_default_keys = BOOL_FALSE;
  1239. opts->hide_passwords = BOOL_TRUE;
  1240. opts->enable_nacm = BOOL_FALSE;
  1241. opts->oneliners = BOOL_TRUE;
  1242. }
  1243. static int pline_opts_parse_ini(const faux_ini_t *ini, pline_opts_t *opts)
  1244. {
  1245. const char *val = NULL;
  1246. if (!opts)
  1247. return -1;
  1248. if (!ini)
  1249. return 0; // Nothing to parse
  1250. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1251. if (faux_str_cmp(val, "y") == 0)
  1252. opts->show_brackets = BOOL_TRUE;
  1253. else if (faux_str_cmp(val, "n") == 0)
  1254. opts->show_brackets = BOOL_FALSE;
  1255. }
  1256. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1257. if (faux_str_cmp(val, "y") == 0)
  1258. opts->show_semicolons = BOOL_TRUE;
  1259. else if (faux_str_cmp(val, "n") == 0)
  1260. opts->show_semicolons = BOOL_FALSE;
  1261. }
  1262. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1263. if (faux_str_cmp(val, "y") == 0)
  1264. opts->first_key_w_stmt = BOOL_TRUE;
  1265. else if (faux_str_cmp(val, "n") == 0)
  1266. opts->first_key_w_stmt = BOOL_FALSE;
  1267. }
  1268. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1269. if (faux_str_cmp(val, "y") == 0)
  1270. opts->keys_w_stmt = BOOL_TRUE;
  1271. else if (faux_str_cmp(val, "n") == 0)
  1272. opts->keys_w_stmt = BOOL_FALSE;
  1273. }
  1274. if ((val = faux_ini_find(ini, "Colorize"))) {
  1275. if (faux_str_cmp(val, "y") == 0)
  1276. opts->colorize = BOOL_TRUE;
  1277. else if (faux_str_cmp(val, "n") == 0)
  1278. opts->colorize = BOOL_FALSE;
  1279. }
  1280. if ((val = faux_ini_find(ini, "Indent"))) {
  1281. unsigned char indent = 0;
  1282. if (faux_conv_atouc(val, &indent, 10))
  1283. opts->indent = indent;
  1284. }
  1285. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1286. if (faux_str_cmp(val, "y") == 0)
  1287. opts->default_keys = BOOL_TRUE;
  1288. else if (faux_str_cmp(val, "n") == 0)
  1289. opts->default_keys = BOOL_FALSE;
  1290. }
  1291. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1292. if (faux_str_cmp(val, "y") == 0)
  1293. opts->show_default_keys = BOOL_TRUE;
  1294. else if (faux_str_cmp(val, "n") == 0)
  1295. opts->show_default_keys = BOOL_FALSE;
  1296. }
  1297. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1298. if (faux_str_cmp(val, "y") == 0)
  1299. opts->hide_passwords = BOOL_TRUE;
  1300. else if (faux_str_cmp(val, "n") == 0)
  1301. opts->hide_passwords = BOOL_FALSE;
  1302. }
  1303. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1304. if (faux_str_cmp(val, "y") == 0)
  1305. opts->enable_nacm = BOOL_TRUE;
  1306. else if (faux_str_cmp(val, "n") == 0)
  1307. opts->enable_nacm = BOOL_FALSE;
  1308. }
  1309. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1310. if (faux_str_cmp(val, "y") == 0)
  1311. opts->oneliners = BOOL_TRUE;
  1312. else if (faux_str_cmp(val, "n") == 0)
  1313. opts->oneliners = BOOL_FALSE;
  1314. }
  1315. return 0;
  1316. }
  1317. int pline_opts_parse(const char *conf, pline_opts_t *opts)
  1318. {
  1319. faux_ini_t *ini = NULL;
  1320. int rc = -1;
  1321. if (!opts)
  1322. return -1;
  1323. if (!conf)
  1324. return 0; // Use defaults
  1325. ini = faux_ini_new();
  1326. if (!faux_ini_parse_str(ini, conf)) {
  1327. faux_ini_free(ini);
  1328. return -1;
  1329. }
  1330. rc = pline_opts_parse_ini(ini, opts);
  1331. faux_ini_free(ini);
  1332. return rc;
  1333. }
  1334. int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts)
  1335. {
  1336. faux_ini_t *ini = NULL;
  1337. int rc = -1;
  1338. if (!opts)
  1339. return -1;
  1340. if (!conf_name)
  1341. return 0; // Use defaults
  1342. ini = faux_ini_new();
  1343. if (!faux_ini_parse_file(ini, conf_name)) {
  1344. faux_ini_free(ini);
  1345. return -1;
  1346. }
  1347. rc = pline_opts_parse_ini(ini, opts);
  1348. faux_ini_free(ini);
  1349. return rc;
  1350. }