pline.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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(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, "---");
  293. }
  294. syslog(LOG_ERR, "=== Completions:");
  295. iter = faux_list_head(pline->compls);
  296. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  297. syslog(LOG_ERR, "pcompl.type = %s", (pcompl->type == PCOMPL_NODE) ?
  298. "PCOMPL_NODE" : "PCOMPL_TYPE");
  299. syslog(LOG_ERR, "pcompl.node = %s", pcompl->node ? pcompl->node->name : "NULL");
  300. syslog(LOG_ERR, "pcompl.xpath = %s", pcompl->xpath ? pcompl->xpath : "NULL");
  301. syslog(LOG_ERR, "pcompl.pat = %s", pat2str(pcompl->pat));
  302. syslog(LOG_ERR, "---");
  303. }
  304. }
  305. static bool_t pexpr_xpath_add_node(pexpr_t *pexpr,
  306. const char *prefix, const char *name)
  307. {
  308. char *tmp = NULL;
  309. assert(pexpr);
  310. assert(prefix);
  311. assert(name);
  312. tmp = faux_str_sprintf("/%s:%s", prefix, name);
  313. faux_str_cat(&pexpr->xpath, tmp);
  314. faux_str_free(tmp);
  315. pexpr->args_num++;
  316. pexpr->tree_depth++;
  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. // Completion
  468. if (!str) {
  469. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  470. break;
  471. }
  472. // Next element
  473. node = klysc_find_child(lysc_node_child(node), str);
  474. // List
  475. } else if (node->nodetype & LYS_LIST) {
  476. const struct lysc_node *iter = NULL;
  477. pexpr->pat = PAT_LIST;
  478. pexpr->list_pos = pexpr->args_num;
  479. faux_str_free(pexpr->last_keys);
  480. pexpr->last_keys = NULL;
  481. // Next element
  482. if (!is_rollback) {
  483. bool_t break_upper_loop = BOOL_FALSE;
  484. // Keys without statement. Positional parameters.
  485. if (!opts->keys_w_stmt) {
  486. LY_LIST_FOR(lysc_node_child(node), iter) {
  487. struct lysc_node_leaf *leaf =
  488. (struct lysc_node_leaf *)iter;
  489. if (!(iter->nodetype & LYS_LEAF))
  490. continue;
  491. if (!(iter->flags & LYS_KEY))
  492. continue;
  493. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  494. // Completion
  495. if (!str) {
  496. char *tmp = faux_str_sprintf("%s/%s",
  497. pexpr->xpath, leaf->name);
  498. pline_add_compl_leaf(pline, iter,
  499. tmp, PAT_LIST_KEY);
  500. faux_str_free(tmp);
  501. break_upper_loop = BOOL_TRUE;
  502. break;
  503. }
  504. pexpr_xpath_add_list_key(pexpr,
  505. leaf->name, str, BOOL_TRUE);
  506. faux_argv_each(&arg);
  507. str = (const char *)faux_argv_current(arg);
  508. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  509. }
  510. // Keys with statements. Arbitrary order of keys.
  511. } else {
  512. faux_list_t *keys = NULL;
  513. unsigned int specified_keys_num = 0;
  514. klysc_key_t *cur_key = NULL;
  515. bool_t first_key = BOOL_TRUE;
  516. bool_t first_key_is_optional = BOOL_FALSE;
  517. faux_list_node_t *key_iter = NULL;
  518. // List keys
  519. keys = faux_list_new(FAUX_LIST_UNSORTED,
  520. FAUX_LIST_UNIQUE,
  521. klysc_key_compare,
  522. klysc_key_kcompare,
  523. (faux_list_free_fn)faux_free);
  524. LY_LIST_FOR(lysc_node_child(node), iter) {
  525. struct lysc_node_leaf *leaf =
  526. (struct lysc_node_leaf *)iter;
  527. klysc_key_t *key = NULL;
  528. if (!(iter->nodetype & LYS_LEAF))
  529. continue;
  530. if (!(iter->flags & LYS_KEY))
  531. continue;
  532. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  533. key = faux_zmalloc(sizeof(*key));
  534. assert(key);
  535. key->node = iter;
  536. if (opts->default_keys &&
  537. (key->dflt = klysc_node_ext_default(iter))) {
  538. if (first_key)
  539. first_key_is_optional = BOOL_TRUE;
  540. }
  541. faux_list_add(keys, key);
  542. first_key = BOOL_FALSE;
  543. }
  544. while (specified_keys_num < faux_list_len(keys)) {
  545. // First key without statement. Must be mandatory.
  546. if ((0 == specified_keys_num) &&
  547. !opts->first_key_w_stmt &&
  548. !first_key_is_optional) {
  549. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  550. } else {
  551. if (!str)
  552. break;
  553. cur_key = faux_list_kfind(keys, str);
  554. if (!cur_key || cur_key->value)
  555. break;
  556. pexpr->args_num++;
  557. faux_argv_each(&arg);
  558. str = (const char *)faux_argv_current(arg);
  559. }
  560. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  561. // Completion
  562. if (!str) {
  563. char *tmp = faux_str_sprintf("%s/%s",
  564. pexpr->xpath,
  565. cur_key->node->name);
  566. pline_add_compl_leaf(pline, cur_key->node,
  567. tmp, PAT_LIST_KEY);
  568. faux_str_free(tmp);
  569. break_upper_loop = BOOL_TRUE;
  570. break;
  571. }
  572. pexpr_xpath_add_list_key(pexpr,
  573. cur_key->node->name, str, BOOL_TRUE);
  574. cur_key->value = str;
  575. specified_keys_num++;
  576. faux_argv_each(&arg);
  577. str = (const char *)faux_argv_current(arg);
  578. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  579. }
  580. if (break_upper_loop) {
  581. faux_list_free(keys);
  582. break;
  583. }
  584. key_iter = faux_list_head(keys);
  585. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  586. if (cur_key->value)
  587. continue;
  588. // Completion
  589. if (!str)
  590. pline_add_compl(pline, PCOMPL_NODE,
  591. cur_key->node, NULL,
  592. SRP_REPO_EDIT, PAT_LIST_KEY);
  593. if (opts->default_keys && cur_key->dflt) {
  594. pexpr_xpath_add_list_key(pexpr,
  595. cur_key->node->name,
  596. cur_key->dflt, BOOL_FALSE);
  597. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  598. } else { // Mandatory key is not specified
  599. break_upper_loop = BOOL_TRUE;
  600. }
  601. }
  602. faux_list_free(keys);
  603. }
  604. if (break_upper_loop)
  605. break;
  606. }
  607. pexpr->pat = PAT_LIST_KEY;
  608. // Completion
  609. if (!str) {
  610. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  611. break;
  612. }
  613. // Next element
  614. node = klysc_find_child(lysc_node_child(node), str);
  615. // Leaf
  616. } else if (node->nodetype & LYS_LEAF) {
  617. struct lysc_node_leaf *leaf =
  618. (struct lysc_node_leaf *)node;
  619. // Next element
  620. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  621. pexpr->pat = PAT_LEAF_EMPTY;
  622. // Completion
  623. if (!str) {
  624. pline_add_compl_subtree(pline,
  625. module, node->parent, pexpr->xpath);
  626. break;
  627. }
  628. // Don't get next argument when argument is not
  629. // really consumed
  630. next_arg = BOOL_FALSE;
  631. } else {
  632. pexpr->pat = PAT_LEAF;
  633. // Completion
  634. if (!str) {
  635. pline_add_compl_leaf(pline, node,
  636. pexpr->xpath, PAT_LEAF_VALUE);
  637. break;
  638. }
  639. pexpr->pat = PAT_LEAF_VALUE;
  640. // Idenity must have prefix
  641. if (LY_TYPE_IDENT == leaf->type->basetype) {
  642. const char *prefix = NULL;
  643. prefix = klysc_identityref_prefix(
  644. (struct lysc_type_identityref *)
  645. leaf->type, str);
  646. if (prefix)
  647. pexpr->value = faux_str_sprintf(
  648. "%s:", prefix);
  649. }
  650. faux_str_cat(&pexpr->value, str);
  651. }
  652. // Expression was completed
  653. // So rollback (for oneliners)
  654. node = node->parent;
  655. pline_add_expr(pline, rollback_xpath,
  656. rollback_args_num, rollback_list_pos,
  657. rollback_tree_depth);
  658. rollback = BOOL_TRUE;
  659. // Leaf-list
  660. } else if (node->nodetype & LYS_LEAFLIST) {
  661. const char *prefix = NULL;
  662. struct lysc_node_leaflist *leaflist =
  663. (struct lysc_node_leaflist *)node;
  664. pexpr->pat = PAT_LEAFLIST;
  665. pexpr->list_pos = pexpr->args_num;
  666. faux_str_free(pexpr->last_keys);
  667. pexpr->last_keys = NULL;
  668. // Completion
  669. if (!str) {
  670. pline_add_compl_leaf(pline, node,
  671. pexpr->xpath, PAT_LEAFLIST_VALUE);
  672. break;
  673. }
  674. pexpr->pat = PAT_LEAFLIST_VALUE;
  675. // Idenity must have prefix
  676. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  677. prefix = klysc_identityref_prefix(
  678. (struct lysc_type_identityref *)
  679. leaflist->type, str);
  680. }
  681. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  682. // Expression was completed
  683. // So rollback (for oneliners)
  684. node = node->parent;
  685. pline_add_expr(pline, rollback_xpath,
  686. rollback_args_num, rollback_list_pos,
  687. rollback_tree_depth);
  688. rollback = BOOL_TRUE;
  689. // LYS_CHOICE and LYS_CASE can appear while rollback only
  690. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  691. // Don't set pexpr->pat because CHOICE and CASE can't
  692. // appear within data tree (schema only)
  693. // Completion
  694. if (!str) {
  695. pline_add_compl_subtree(pline, module, node, pexpr->xpath);
  696. break;
  697. }
  698. // Next element
  699. node = klysc_find_child(lysc_node_child(node), str);
  700. } else {
  701. break;
  702. }
  703. // Current argument was not consumed.
  704. // Break before getting next arg.
  705. if (!node && !rollback)
  706. break;
  707. if (next_arg)
  708. faux_argv_each(&arg);
  709. } while (BOOL_TRUE);
  710. // There is not-consumed argument so whole pline is invalid
  711. if (faux_argv_current(arg))
  712. pline->invalid = BOOL_TRUE;
  713. faux_str_free(rollback_xpath);
  714. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  715. if (!first_pexpr || !first_pexpr->xpath)
  716. return BOOL_FALSE; // Not found
  717. return BOOL_TRUE;
  718. }
  719. pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
  720. const pline_opts_t *opts)
  721. {
  722. const struct ly_ctx *ctx = NULL;
  723. struct lys_module *module = NULL;
  724. pline_t *pline = NULL;
  725. uint32_t i = 0;
  726. faux_list_node_t *last_expr_node = NULL;
  727. assert(sess);
  728. if (!sess)
  729. return NULL;
  730. pline = pline_new(sess);
  731. if (!pline)
  732. return NULL;
  733. ctx = sr_session_acquire_context(pline->sess);
  734. if (!ctx)
  735. return NULL;
  736. // Iterate all modules
  737. i = 0;
  738. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  739. if (sr_module_is_internal(module, opts->enable_nacm))
  740. continue;
  741. if (!module->compiled)
  742. continue;
  743. if (!module->implemented)
  744. continue;
  745. if (!module->compiled->data)
  746. continue;
  747. if (pline_parse_module(module, argv, pline, opts))
  748. break; // Found
  749. }
  750. sr_session_release_context(pline->sess);
  751. // Last parsed expression can be inactive so remove it from list
  752. last_expr_node = faux_list_tail(pline->exprs);
  753. if (last_expr_node) {
  754. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  755. if (!expr->active)
  756. faux_list_del(pline->exprs, last_expr_node);
  757. }
  758. return pline;
  759. }
  760. static void identityref_compl(struct lysc_ident *ident)
  761. {
  762. LY_ARRAY_COUNT_TYPE u = 0;
  763. if (!ident)
  764. return;
  765. if (!ident->derived) {
  766. printf("%s\n", ident->name);
  767. return;
  768. }
  769. LY_ARRAY_FOR(ident->derived, u) {
  770. identityref_compl(ident->derived[u]);
  771. }
  772. }
  773. static void identityref_help(struct lysc_ident *ident)
  774. {
  775. LY_ARRAY_COUNT_TYPE u = 0;
  776. if (!ident)
  777. return;
  778. if (!ident->derived) {
  779. if (ident->dsc) {
  780. char *dsc = faux_str_getline(ident->dsc, NULL);
  781. printf("%s\n%s\n", ident->name, dsc);
  782. faux_str_free(dsc);
  783. } else {
  784. printf("%s\n%s\n", ident->name, ident->name);
  785. }
  786. return;
  787. }
  788. LY_ARRAY_FOR(ident->derived, u) {
  789. identityref_help(ident->derived[u]);
  790. }
  791. }
  792. static void pline_print_type_completions(const struct lysc_type *type)
  793. {
  794. assert(type);
  795. switch (type->basetype) {
  796. case LY_TYPE_BOOL: {
  797. printf("true\nfalse\n");
  798. break;
  799. }
  800. case LY_TYPE_ENUM: {
  801. const struct lysc_type_enum *t =
  802. (const struct lysc_type_enum *)type;
  803. LY_ARRAY_COUNT_TYPE u = 0;
  804. LY_ARRAY_FOR(t->enums, u) {
  805. printf("%s\n",t->enums[u].name);
  806. }
  807. break;
  808. }
  809. case LY_TYPE_IDENT: {
  810. struct lysc_type_identityref *t =
  811. (struct lysc_type_identityref *)type;
  812. LY_ARRAY_COUNT_TYPE u = 0;
  813. LY_ARRAY_FOR(t->bases, u) {
  814. identityref_compl(t->bases[u]);
  815. }
  816. break;
  817. }
  818. case LY_TYPE_UNION: {
  819. struct lysc_type_union *t =
  820. (struct lysc_type_union *)type;
  821. LY_ARRAY_COUNT_TYPE u = 0;
  822. LY_ARRAY_FOR(t->types, u) {
  823. pline_print_type_completions(t->types[u]);
  824. }
  825. break;
  826. }
  827. case LY_TYPE_LEAFREF: {
  828. struct lysc_type_leafref *t =
  829. (struct lysc_type_leafref *)type;
  830. pline_print_type_completions(t->realtype);
  831. break;
  832. }
  833. default:
  834. break;
  835. }
  836. }
  837. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  838. {
  839. struct lysc_range *range = NULL;
  840. LY_ARRAY_COUNT_TYPE u = 0;
  841. char *r = NULL;
  842. assert(type);
  843. range = ((struct lysc_type_num *)type)->range;
  844. // Show defaults
  845. if (!range) {
  846. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  847. return;
  848. }
  849. // Range
  850. faux_str_cat(&r, "[");
  851. LY_ARRAY_FOR(range->parts, u) {
  852. char *t = NULL;
  853. if (u != 0)
  854. faux_str_cat(&r, "|");
  855. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  856. range->parts[u].min_u64, range->parts[u].max_u64);
  857. faux_str_cat(&r, t);
  858. faux_str_free(t);
  859. }
  860. faux_str_cat(&r, "]\n");
  861. printf("%s", r);
  862. faux_free(r);
  863. }
  864. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  865. {
  866. struct lysc_range *range = NULL;
  867. LY_ARRAY_COUNT_TYPE u = 0;
  868. char *r = NULL;
  869. assert(type);
  870. range = ((struct lysc_type_num *)type)->range;
  871. // Show defaults
  872. if (!range) {
  873. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  874. return;
  875. }
  876. // Range
  877. faux_str_cat(&r, "[");
  878. LY_ARRAY_FOR(range->parts, u) {
  879. char *t = NULL;
  880. if (u != 0)
  881. faux_str_cat(&r, "|");
  882. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  883. range->parts[u].min_64, range->parts[u].max_64);
  884. faux_str_cat(&r, t);
  885. faux_str_free(t);
  886. }
  887. faux_str_cat(&r, "]\n");
  888. printf("%s", r);
  889. faux_free(r);
  890. }
  891. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  892. {
  893. struct lysc_range *range = NULL;
  894. uint8_t fraction_digits = 0;
  895. LY_ARRAY_COUNT_TYPE u = 0;
  896. char *r = NULL;
  897. int64_t div = 1;
  898. uint8_t i = 0;
  899. assert(type);
  900. range = ((struct lysc_type_dec *)type)->range;
  901. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  902. for (i = 0; i < fraction_digits; i++)
  903. div = div * 10;
  904. // Show defaults
  905. if (!range) {
  906. printf("[%.*f..%.*f]\n",
  907. fraction_digits, (double)def_min / div,
  908. fraction_digits, (double)def_max / div);
  909. return;
  910. }
  911. // Range
  912. faux_str_cat(&r, "[");
  913. LY_ARRAY_FOR(range->parts, u) {
  914. char *t = NULL;
  915. if (u != 0)
  916. faux_str_cat(&r, "|");
  917. t = faux_str_sprintf("%.*f..%.*f",
  918. fraction_digits, (double)range->parts[u].min_64 / div,
  919. fraction_digits, (double)range->parts[u].max_64 / div);
  920. faux_str_cat(&r, t);
  921. faux_str_free(t);
  922. }
  923. faux_str_cat(&r, "]\n");
  924. printf("%s", r);
  925. faux_free(r);
  926. }
  927. static void str_range(const struct lysc_type *type)
  928. {
  929. struct lysc_range *range = NULL;
  930. LY_ARRAY_COUNT_TYPE u = 0;
  931. char *r = NULL;
  932. assert(type);
  933. range = ((struct lysc_type_str *)type)->length;
  934. // Show defaults
  935. if (!range) {
  936. printf("<string>\n");
  937. return;
  938. }
  939. // Range
  940. faux_str_cat(&r, "<string[");
  941. LY_ARRAY_FOR(range->parts, u) {
  942. char *t = NULL;
  943. if (u != 0)
  944. faux_str_cat(&r, "|");
  945. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  946. range->parts[u].min_u64, range->parts[u].max_u64);
  947. faux_str_cat(&r, t);
  948. faux_str_free(t);
  949. }
  950. faux_str_cat(&r, "]>\n");
  951. printf("%s", r);
  952. faux_free(r);
  953. }
  954. static void pline_print_type_help(const struct lysc_node *node,
  955. const struct lysc_type *type)
  956. {
  957. const char *units = NULL;
  958. assert(type);
  959. assert(node);
  960. if (node->nodetype & LYS_LEAF)
  961. units = ((struct lysc_node_leaf *)node)->units;
  962. else if (node->nodetype & LYS_LEAFLIST)
  963. units = ((struct lysc_node_leaflist *)node)->units;
  964. else
  965. return;
  966. if (units) {
  967. printf("%s\n", units);
  968. } else {
  969. switch (type->basetype) {
  970. case LY_TYPE_UINT8:
  971. uint_range(type, 0, UCHAR_MAX);
  972. break;
  973. case LY_TYPE_UINT16:
  974. uint_range(type, 0, USHRT_MAX);
  975. break;
  976. case LY_TYPE_UINT32:
  977. uint_range(type, 0, UINT_MAX);
  978. break;
  979. case LY_TYPE_UINT64:
  980. uint_range(type, 0, ULLONG_MAX);
  981. break;
  982. case LY_TYPE_INT8:
  983. int_range(type, CHAR_MIN, CHAR_MAX);
  984. break;
  985. case LY_TYPE_INT16:
  986. int_range(type, SHRT_MIN, SHRT_MAX);
  987. break;
  988. case LY_TYPE_INT32:
  989. int_range(type, INT_MIN, INT_MAX);
  990. break;
  991. case LY_TYPE_INT64:
  992. int_range(type, LLONG_MIN, LLONG_MAX);
  993. break;
  994. case LY_TYPE_DEC64:
  995. dec_range(type, LLONG_MIN, LLONG_MAX);
  996. break;
  997. case LY_TYPE_STRING:
  998. str_range(type);
  999. break;
  1000. case LY_TYPE_BOOL:
  1001. printf("<true/false>\n");
  1002. break;
  1003. case LY_TYPE_LEAFREF: {
  1004. const struct lysc_type_leafref *t =
  1005. (const struct lysc_type_leafref *)type;
  1006. const struct lysc_node *ref_node = NULL;
  1007. const struct lysc_type *ref_type = NULL;
  1008. char *node_path = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
  1009. char *path = klysc_leafref_xpath(node, type, node_path);
  1010. faux_str_free(node_path);
  1011. ref_node = lys_find_path(NULL, node, path, 0);
  1012. faux_str_free(path);
  1013. if (!ref_node) {
  1014. pline_print_type_help(node, t->realtype);
  1015. return; // Because it prints whole info itself
  1016. }
  1017. if (ref_node->nodetype & LYS_LEAF)
  1018. ref_type = ((struct lysc_node_leaf *)ref_node)->type;
  1019. else
  1020. ref_type = ((struct lysc_node_leaflist *)ref_node)->type;
  1021. pline_print_type_help(ref_node, ref_type);
  1022. return; // Because it prints whole info itself
  1023. }
  1024. case LY_TYPE_UNION: {
  1025. const struct lysc_type_union *t =
  1026. (const struct lysc_type_union *)type;
  1027. LY_ARRAY_COUNT_TYPE u = 0;
  1028. LY_ARRAY_FOR(t->types, u)
  1029. pline_print_type_help(node, t->types[u]);
  1030. return; // Because it prints whole info itself
  1031. }
  1032. case LY_TYPE_ENUM: {
  1033. const struct lysc_type_enum *t =
  1034. (const struct lysc_type_enum *)type;
  1035. LY_ARRAY_COUNT_TYPE u = 0;
  1036. LY_ARRAY_FOR(t->enums, u)
  1037. if (t->enums[u].dsc) {
  1038. char *dsc = faux_str_getline(
  1039. t->enums[u].dsc, NULL);
  1040. printf("%s\n%s\n",
  1041. t->enums[u].name, dsc);
  1042. faux_str_free(dsc);
  1043. } else {
  1044. printf("%s\n%s\n",
  1045. t->enums[u].name,
  1046. t->enums[u].name);
  1047. }
  1048. return; // Because it prints whole info itself
  1049. }
  1050. case LY_TYPE_IDENT: {
  1051. struct lysc_type_identityref *t =
  1052. (struct lysc_type_identityref *)type;
  1053. LY_ARRAY_COUNT_TYPE u = 0;
  1054. LY_ARRAY_FOR(t->bases, u)
  1055. identityref_help(t->bases[u]);
  1056. return; // Because it prints whole info itself
  1057. }
  1058. default:
  1059. printf("<unknown>\n");
  1060. break;
  1061. }
  1062. }
  1063. if (node->dsc) {
  1064. char *dsc = faux_str_getline(node->dsc, NULL);
  1065. printf("%s\n", dsc);
  1066. faux_str_free(dsc);
  1067. } else {
  1068. printf("%s\n", node->name);
  1069. }
  1070. }
  1071. void pline_print_completions(const pline_t *pline, bool_t help,
  1072. pt_e enabled_types, bool_t existing_nodes_only)
  1073. {
  1074. faux_list_node_t *iter = NULL;
  1075. pcompl_t *pcompl = NULL;
  1076. sr_datastore_t current_ds = SRP_REPO_EDIT;
  1077. iter = faux_list_head(pline->compls);
  1078. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1079. struct lysc_type *type = NULL;
  1080. const struct lysc_node *node = pcompl->node;
  1081. if (!(pcompl->pat & enabled_types))
  1082. continue;
  1083. if (pcompl->xpath && !help) {
  1084. sr_val_t *vals = NULL;
  1085. size_t val_num = 0;
  1086. size_t i = 0;
  1087. // Switch to necessary DS
  1088. if (current_ds != pcompl->xpath_ds) {
  1089. sr_session_switch_ds(pline->sess, pcompl->xpath_ds);
  1090. current_ds = pcompl->xpath_ds;
  1091. }
  1092. if (PCOMPL_TYPE == pcompl->type) {
  1093. sr_get_items(pline->sess, pcompl->xpath,
  1094. 0, 0, &vals, &val_num);
  1095. for (i = 0; i < val_num; i++) {
  1096. char *tmp = sr_val_to_str(&vals[i]);
  1097. if (!tmp)
  1098. continue;
  1099. printf("%s\n", tmp);
  1100. free(tmp);
  1101. }
  1102. sr_free_values(vals, val_num);
  1103. } else if (existing_nodes_only) {
  1104. bool_t dflt = BOOL_TRUE;
  1105. sr_get_items(pline->sess, pcompl->xpath,
  1106. 0, 0, &vals, &val_num);
  1107. for (i = 0; i < val_num; i++) {
  1108. if (!vals[i].dflt) {
  1109. dflt = BOOL_FALSE;
  1110. break;
  1111. }
  1112. }
  1113. if (!dflt)
  1114. printf("%s\n", node->name);
  1115. sr_free_values(vals, val_num);
  1116. }
  1117. }
  1118. if (!node)
  1119. continue;
  1120. if (existing_nodes_only)
  1121. continue;
  1122. // Node
  1123. if (PCOMPL_NODE == pcompl->type) {
  1124. printf("%s\n", node->name);
  1125. if (help) {
  1126. if (!node->dsc) {
  1127. printf("%s\n", node->name);
  1128. } else {
  1129. char *dsc = faux_str_getline(node->dsc,
  1130. NULL);
  1131. printf("%s\n", dsc);
  1132. faux_str_free(dsc);
  1133. }
  1134. }
  1135. continue;
  1136. }
  1137. // Type
  1138. if (node->nodetype & LYS_LEAF)
  1139. type = ((struct lysc_node_leaf *)node)->type;
  1140. else if (node->nodetype & LYS_LEAFLIST)
  1141. type = ((struct lysc_node_leaflist *)node)->type;
  1142. else
  1143. continue;
  1144. if (help)
  1145. pline_print_type_help(node, type);
  1146. else
  1147. pline_print_type_completions(type);
  1148. }
  1149. // Restore default DS
  1150. if (current_ds != SRP_REPO_EDIT)
  1151. sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
  1152. }
  1153. void pline_opts_init(pline_opts_t *opts)
  1154. {
  1155. opts->begin_bracket = '{';
  1156. opts->end_bracket = '}';
  1157. opts->show_brackets = BOOL_TRUE;
  1158. opts->show_semicolons = BOOL_TRUE;
  1159. opts->first_key_w_stmt = BOOL_FALSE;
  1160. opts->keys_w_stmt = BOOL_TRUE;
  1161. opts->colorize = BOOL_TRUE;
  1162. opts->indent = 2;
  1163. opts->default_keys = BOOL_FALSE;
  1164. opts->show_default_keys = BOOL_FALSE;
  1165. opts->hide_passwords = BOOL_TRUE;
  1166. opts->enable_nacm = BOOL_FALSE;
  1167. opts->oneliners = BOOL_TRUE;
  1168. }
  1169. static int pline_opts_parse_ini(const faux_ini_t *ini, pline_opts_t *opts)
  1170. {
  1171. const char *val = NULL;
  1172. if (!opts)
  1173. return -1;
  1174. if (!ini)
  1175. return 0; // Nothing to parse
  1176. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1177. if (faux_str_cmp(val, "y") == 0)
  1178. opts->show_brackets = BOOL_TRUE;
  1179. else if (faux_str_cmp(val, "n") == 0)
  1180. opts->show_brackets = BOOL_FALSE;
  1181. }
  1182. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1183. if (faux_str_cmp(val, "y") == 0)
  1184. opts->show_semicolons = BOOL_TRUE;
  1185. else if (faux_str_cmp(val, "n") == 0)
  1186. opts->show_semicolons = BOOL_FALSE;
  1187. }
  1188. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1189. if (faux_str_cmp(val, "y") == 0)
  1190. opts->first_key_w_stmt = BOOL_TRUE;
  1191. else if (faux_str_cmp(val, "n") == 0)
  1192. opts->first_key_w_stmt = BOOL_FALSE;
  1193. }
  1194. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1195. if (faux_str_cmp(val, "y") == 0)
  1196. opts->keys_w_stmt = BOOL_TRUE;
  1197. else if (faux_str_cmp(val, "n") == 0)
  1198. opts->keys_w_stmt = BOOL_FALSE;
  1199. }
  1200. if ((val = faux_ini_find(ini, "Colorize"))) {
  1201. if (faux_str_cmp(val, "y") == 0)
  1202. opts->colorize = BOOL_TRUE;
  1203. else if (faux_str_cmp(val, "n") == 0)
  1204. opts->colorize = BOOL_FALSE;
  1205. }
  1206. if ((val = faux_ini_find(ini, "Indent"))) {
  1207. unsigned char indent = 0;
  1208. if (faux_conv_atouc(val, &indent, 10))
  1209. opts->indent = indent;
  1210. }
  1211. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1212. if (faux_str_cmp(val, "y") == 0)
  1213. opts->default_keys = BOOL_TRUE;
  1214. else if (faux_str_cmp(val, "n") == 0)
  1215. opts->default_keys = BOOL_FALSE;
  1216. }
  1217. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1218. if (faux_str_cmp(val, "y") == 0)
  1219. opts->show_default_keys = BOOL_TRUE;
  1220. else if (faux_str_cmp(val, "n") == 0)
  1221. opts->show_default_keys = BOOL_FALSE;
  1222. }
  1223. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1224. if (faux_str_cmp(val, "y") == 0)
  1225. opts->hide_passwords = BOOL_TRUE;
  1226. else if (faux_str_cmp(val, "n") == 0)
  1227. opts->hide_passwords = BOOL_FALSE;
  1228. }
  1229. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1230. if (faux_str_cmp(val, "y") == 0)
  1231. opts->enable_nacm = BOOL_TRUE;
  1232. else if (faux_str_cmp(val, "n") == 0)
  1233. opts->enable_nacm = BOOL_FALSE;
  1234. }
  1235. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1236. if (faux_str_cmp(val, "y") == 0)
  1237. opts->oneliners = BOOL_TRUE;
  1238. else if (faux_str_cmp(val, "n") == 0)
  1239. opts->oneliners = BOOL_FALSE;
  1240. }
  1241. return 0;
  1242. }
  1243. int pline_opts_parse(const char *conf, pline_opts_t *opts)
  1244. {
  1245. faux_ini_t *ini = NULL;
  1246. int rc = -1;
  1247. if (!opts)
  1248. return -1;
  1249. if (!conf)
  1250. return 0; // Use defaults
  1251. ini = faux_ini_new();
  1252. if (!faux_ini_parse_str(ini, conf)) {
  1253. faux_ini_free(ini);
  1254. return -1;
  1255. }
  1256. rc = pline_opts_parse_ini(ini, opts);
  1257. faux_ini_free(ini);
  1258. return rc;
  1259. }
  1260. int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts)
  1261. {
  1262. faux_ini_t *ini = NULL;
  1263. int rc = -1;
  1264. if (!opts)
  1265. return -1;
  1266. if (!conf_name)
  1267. return 0; // Use defaults
  1268. ini = faux_ini_new();
  1269. if (!faux_ini_parse_file(ini, conf_name)) {
  1270. faux_ini_free(ini);
  1271. return -1;
  1272. }
  1273. rc = pline_opts_parse_ini(ini, opts);
  1274. faux_ini_free(ini);
  1275. return rc;
  1276. }