pline.c 34 KB

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