pline.c 35 KB

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