pline.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /** @file pline.c
  2. */
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <syslog.h>
  9. #include <limits.h>
  10. #include <faux/faux.h>
  11. #include <faux/str.h>
  12. #include <faux/list.h>
  13. #include <faux/argv.h>
  14. #include <faux/ini.h>
  15. #include <faux/conv.h>
  16. #include <sysrepo.h>
  17. #include <sysrepo/xpath.h>
  18. #include <sysrepo/values.h>
  19. #include <libyang/tree_edit.h>
  20. #include "klish_plugin_sysrepo.h"
  21. static int sr_ly_module_is_internal(const struct lys_module *ly_mod)
  22. {
  23. if (!ly_mod->revision) {
  24. return 0;
  25. }
  26. if (!strcmp(ly_mod->name, "ietf-yang-metadata")
  27. && !strcmp(ly_mod->revision, "2016-08-05")) {
  28. return 1;
  29. } else if (!strcmp(ly_mod->name, "yang")
  30. && !strcmp(ly_mod->revision, "2021-04-07")) {
  31. return 1;
  32. } else if (!strcmp(ly_mod->name, "ietf-inet-types")
  33. && !strcmp(ly_mod->revision, "2013-07-15")) {
  34. return 1;
  35. } else if (!strcmp(ly_mod->name, "ietf-yang-types")
  36. && !strcmp(ly_mod->revision, "2013-07-15")) {
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static int sr_module_is_internal(const struct lys_module *ly_mod, bool_t enable_nacm)
  42. {
  43. if (!ly_mod->revision) {
  44. return 0;
  45. }
  46. if (sr_ly_module_is_internal(ly_mod)) {
  47. return 1;
  48. }
  49. if (!strcmp(ly_mod->name, "ietf-datastores")
  50. && !strcmp(ly_mod->revision, "2018-02-14")) {
  51. return 1;
  52. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  53. return 1;
  54. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  55. return 1;
  56. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  57. return 1;
  58. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults")
  59. && !strcmp(ly_mod->revision, "2011-06-01")) {
  60. return 1;
  61. } else if (!strcmp(ly_mod->name, "ietf-origin")
  62. && !strcmp(ly_mod->revision, "2018-02-14")) {
  63. return 1;
  64. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications")
  65. && !strcmp(ly_mod->revision, "2012-02-06")) {
  66. return 1;
  67. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  68. return 1;
  69. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  70. return 1;
  71. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  72. return 1;
  73. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm") && !enable_nacm) {
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static pexpr_t *pexpr_new(void)
  79. {
  80. pexpr_t *pexpr = NULL;
  81. pexpr = faux_zmalloc(sizeof(*pexpr));
  82. assert(pexpr);
  83. if (!pexpr)
  84. return NULL;
  85. // Initialize
  86. pexpr->xpath = NULL;
  87. pexpr->value = NULL;
  88. pexpr->active = BOOL_FALSE;
  89. pexpr->pat = PAT_NONE;
  90. pexpr->args_num = 0;
  91. pexpr->list_pos = 0;
  92. pexpr->last_keys = NULL;
  93. return pexpr;
  94. }
  95. static void pexpr_free(pexpr_t *pexpr)
  96. {
  97. if (!pexpr)
  98. return;
  99. faux_str_free(pexpr->xpath);
  100. faux_str_free(pexpr->value);
  101. faux_str_free(pexpr->last_keys);
  102. free(pexpr);
  103. }
  104. static pcompl_t *pcompl_new(void)
  105. {
  106. pcompl_t *pcompl = NULL;
  107. pcompl = faux_zmalloc(sizeof(*pcompl));
  108. assert(pcompl);
  109. if (!pcompl)
  110. return NULL;
  111. // Initialize
  112. pcompl->type = PCOMPL_NODE;
  113. pcompl->node = NULL;
  114. pcompl->xpath = NULL;
  115. pcompl->xpath_ds = SRP_REPO_EDIT;
  116. pcompl->pat = PAT_NONE;
  117. return pcompl;
  118. }
  119. static void pcompl_free(pcompl_t *pcompl)
  120. {
  121. if (!pcompl)
  122. return;
  123. faux_str_free(pcompl->xpath);
  124. free(pcompl);
  125. }
  126. pline_t *pline_new(sr_session_ctx_t *sess)
  127. {
  128. pline_t *pline = NULL;
  129. pline = faux_zmalloc(sizeof(*pline));
  130. assert(pline);
  131. if (!pline)
  132. return NULL;
  133. // Init
  134. pline->sess = sess;
  135. pline->invalid = BOOL_FALSE;
  136. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  137. NULL, NULL, (faux_list_free_fn)pexpr_free);
  138. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  139. NULL, NULL, (faux_list_free_fn)pcompl_free);
  140. return pline;
  141. }
  142. void pline_free(pline_t *pline)
  143. {
  144. if (!pline)
  145. return;
  146. faux_list_free(pline->exprs);
  147. faux_list_free(pline->compls);
  148. faux_free(pline);
  149. }
  150. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  151. size_t args_num, size_t list_pos, size_t tree_depth)
  152. {
  153. pexpr_t *pexpr = NULL;
  154. assert(pline);
  155. pexpr = pexpr_new();
  156. if (xpath)
  157. pexpr->xpath = faux_str_dup(xpath);
  158. pexpr->args_num = args_num;
  159. pexpr->list_pos = list_pos;
  160. pexpr->tree_depth = tree_depth;
  161. faux_list_add(pline->exprs, pexpr);
  162. return pexpr;
  163. }
  164. pexpr_t *pline_current_expr(pline_t *pline)
  165. {
  166. assert(pline);
  167. if (faux_list_len(pline->exprs) == 0)
  168. pline_add_expr(pline, NULL, 0, 0, 0);
  169. return (pexpr_t *)faux_list_data(faux_list_tail(pline->exprs));
  170. }
  171. static void pline_add_compl(pline_t *pline,
  172. pcompl_type_e type, const struct lysc_node *node,
  173. const char *xpath, sr_datastore_t ds, pat_e pat)
  174. {
  175. pcompl_t *pcompl = NULL;
  176. assert(pline);
  177. pcompl = pcompl_new();
  178. pcompl->type = type;
  179. pcompl->node = node;
  180. pcompl->pat = pat;
  181. if (xpath) {
  182. pcompl->xpath = faux_str_dup(xpath);
  183. pcompl->xpath_ds = ds;
  184. }
  185. faux_list_add(pline->compls, pcompl);
  186. }
  187. static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
  188. const struct lysc_node *node, const char *xpath)
  189. {
  190. const struct lysc_node *subtree = NULL;
  191. const struct lysc_node *iter = NULL;
  192. assert(pline);
  193. assert(module);
  194. if (node)
  195. subtree = lysc_node_child(node);
  196. else
  197. subtree = module->compiled->data;
  198. LY_LIST_FOR(subtree, iter) {
  199. pat_e pat = PAT_NONE;
  200. char *node_xpath = NULL;
  201. if (!(iter->nodetype & SRP_NODETYPE_CONF))
  202. continue;
  203. if (!(iter->flags & LYS_CONFIG_W))
  204. continue;
  205. if ((iter->nodetype & LYS_LEAF) && (iter->flags & LYS_KEY))
  206. continue;
  207. if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
  208. pline_add_compl_subtree(pline, module, iter, xpath);
  209. continue;
  210. }
  211. switch(iter->nodetype) {
  212. case LYS_CONTAINER:
  213. pat = PAT_CONTAINER;
  214. break;
  215. case LYS_LEAF:
  216. pat = PAT_LEAF;
  217. break;
  218. case LYS_LEAFLIST:
  219. pat = PAT_LEAFLIST;
  220. break;
  221. case LYS_LIST:
  222. pat = PAT_LIST;
  223. break;
  224. default:
  225. continue;
  226. break;
  227. }
  228. node_xpath = faux_str_sprintf("%s/%s:%s", xpath ? xpath : "",
  229. iter->module->name, iter->name);
  230. pline_add_compl(pline, PCOMPL_NODE, iter, node_xpath,
  231. SRP_REPO_EDIT, pat);
  232. faux_str_free(node_xpath);
  233. }
  234. }
  235. static const char *pat2str(pat_e pat)
  236. {
  237. const char *str = NULL;
  238. switch (pat) {
  239. case PAT_NONE:
  240. str = "NONE";
  241. break;
  242. case PAT_CONTAINER:
  243. str = "CONTAINER";
  244. break;
  245. case PAT_LIST:
  246. str = "LIST";
  247. break;
  248. case PAT_LIST_KEY:
  249. str = "LIST_KEY";
  250. break;
  251. case PAT_LIST_KEY_INCOMPLETED:
  252. str = "LIST_KEY_INCOMPLETED";
  253. break;
  254. case PAT_LEAF:
  255. str = "LEAF";
  256. break;
  257. case PAT_LEAF_VALUE:
  258. str = "LEAF_VALUE";
  259. break;
  260. case PAT_LEAF_EMPTY:
  261. str = "LEAF_EMPTY";
  262. break;
  263. case PAT_LEAFLIST:
  264. str = "LEAFLIST";
  265. break;
  266. case PAT_LEAFLIST_VALUE:
  267. str = "LEAFLIST_VALUE";
  268. break;
  269. default:
  270. str = "UNKNOWN";
  271. break;
  272. }
  273. return str;
  274. }
  275. void pline_debug(pline_t *pline)
  276. {
  277. faux_list_node_t *iter = NULL;
  278. pexpr_t *pexpr = NULL;
  279. pcompl_t *pcompl = NULL;
  280. syslog(LOG_ERR, "====== Pline:");
  281. syslog(LOG_ERR, "invalid = %s", pline->invalid ? "true" : "false");
  282. syslog(LOG_ERR, "=== Expressions:");
  283. iter = faux_list_head(pline->exprs);
  284. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  285. syslog(LOG_ERR, "pexpr.xpath = %s", pexpr->xpath ? pexpr->xpath : "NULL");
  286. syslog(LOG_ERR, "pexpr.value = %s", pexpr->value ? pexpr->value : "NULL");
  287. syslog(LOG_ERR, "pexpr.active = %s", pexpr->active ? "true" : "false");
  288. syslog(LOG_ERR, "pexpr.pat = %s", pat2str(pexpr->pat));
  289. syslog(LOG_ERR, "pexpr.args_num = %lu", pexpr->args_num);
  290. syslog(LOG_ERR, "pexpr.list_pos = %lu", pexpr->list_pos);
  291. syslog(LOG_ERR, "pexpr.last_keys = %s", pexpr->last_keys ? pexpr->last_keys : "NULL");
  292. syslog(LOG_ERR, "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. void pline_print_completions(const pline_t *pline, bool_t help,
  1074. pt_e enabled_types, bool_t existing_nodes_only)
  1075. {
  1076. faux_list_node_t *iter = NULL;
  1077. pcompl_t *pcompl = NULL;
  1078. sr_datastore_t current_ds = SRP_REPO_EDIT;
  1079. iter = faux_list_head(pline->compls);
  1080. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1081. struct lysc_type *type = NULL;
  1082. const struct lysc_node *node = pcompl->node;
  1083. if (!(pcompl->pat & enabled_types))
  1084. continue;
  1085. if (pcompl->xpath && !help) {
  1086. sr_val_t *vals = NULL;
  1087. size_t val_num = 0;
  1088. size_t i = 0;
  1089. // Switch to necessary DS
  1090. if (current_ds != pcompl->xpath_ds) {
  1091. sr_session_switch_ds(pline->sess, pcompl->xpath_ds);
  1092. current_ds = pcompl->xpath_ds;
  1093. }
  1094. if (PCOMPL_TYPE == pcompl->type) {
  1095. sr_get_items(pline->sess, pcompl->xpath,
  1096. 0, 0, &vals, &val_num);
  1097. for (i = 0; i < val_num; i++) {
  1098. char *tmp = sr_val_to_str(&vals[i]);
  1099. char *esc_tmp = NULL;
  1100. if (!tmp)
  1101. continue;
  1102. esc_tmp = faux_str_c_esc_space(tmp);
  1103. free(tmp);
  1104. printf("%s\n", esc_tmp);
  1105. free(esc_tmp);
  1106. }
  1107. sr_free_values(vals, val_num);
  1108. } else if (existing_nodes_only) {
  1109. bool_t dflt = BOOL_TRUE;
  1110. sr_get_items(pline->sess, pcompl->xpath,
  1111. 0, 0, &vals, &val_num);
  1112. for (i = 0; i < val_num; i++) {
  1113. if (!vals[i].dflt) {
  1114. dflt = BOOL_FALSE;
  1115. break;
  1116. }
  1117. }
  1118. if (!dflt)
  1119. printf("%s\n", node->name);
  1120. sr_free_values(vals, val_num);
  1121. }
  1122. }
  1123. if (!node)
  1124. continue;
  1125. if (existing_nodes_only)
  1126. continue;
  1127. // Node
  1128. if (PCOMPL_NODE == pcompl->type) {
  1129. printf("%s\n", node->name);
  1130. if (help) {
  1131. if (!node->dsc) {
  1132. printf("%s\n", node->name);
  1133. } else {
  1134. char *dsc = faux_str_getline(node->dsc,
  1135. NULL);
  1136. printf("%s\n", dsc);
  1137. faux_str_free(dsc);
  1138. }
  1139. }
  1140. continue;
  1141. }
  1142. // Type
  1143. if (node->nodetype & LYS_LEAF)
  1144. type = ((struct lysc_node_leaf *)node)->type;
  1145. else if (node->nodetype & LYS_LEAFLIST)
  1146. type = ((struct lysc_node_leaflist *)node)->type;
  1147. else
  1148. continue;
  1149. if (help)
  1150. pline_print_type_help(node, type);
  1151. else
  1152. pline_print_type_completions(type);
  1153. }
  1154. // Restore default DS
  1155. if (current_ds != SRP_REPO_EDIT)
  1156. sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
  1157. }
  1158. void pline_opts_init(pline_opts_t *opts)
  1159. {
  1160. opts->begin_bracket = '{';
  1161. opts->end_bracket = '}';
  1162. opts->show_brackets = BOOL_TRUE;
  1163. opts->show_semicolons = BOOL_TRUE;
  1164. opts->first_key_w_stmt = BOOL_FALSE;
  1165. opts->keys_w_stmt = BOOL_TRUE;
  1166. opts->colorize = BOOL_TRUE;
  1167. opts->indent = 2;
  1168. opts->default_keys = BOOL_FALSE;
  1169. opts->show_default_keys = BOOL_FALSE;
  1170. opts->hide_passwords = BOOL_TRUE;
  1171. opts->enable_nacm = BOOL_FALSE;
  1172. opts->oneliners = BOOL_TRUE;
  1173. }
  1174. static int pline_opts_parse_ini(const faux_ini_t *ini, pline_opts_t *opts)
  1175. {
  1176. const char *val = NULL;
  1177. if (!opts)
  1178. return -1;
  1179. if (!ini)
  1180. return 0; // Nothing to parse
  1181. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1182. if (faux_str_cmp(val, "y") == 0)
  1183. opts->show_brackets = BOOL_TRUE;
  1184. else if (faux_str_cmp(val, "n") == 0)
  1185. opts->show_brackets = BOOL_FALSE;
  1186. }
  1187. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1188. if (faux_str_cmp(val, "y") == 0)
  1189. opts->show_semicolons = BOOL_TRUE;
  1190. else if (faux_str_cmp(val, "n") == 0)
  1191. opts->show_semicolons = BOOL_FALSE;
  1192. }
  1193. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1194. if (faux_str_cmp(val, "y") == 0)
  1195. opts->first_key_w_stmt = BOOL_TRUE;
  1196. else if (faux_str_cmp(val, "n") == 0)
  1197. opts->first_key_w_stmt = BOOL_FALSE;
  1198. }
  1199. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1200. if (faux_str_cmp(val, "y") == 0)
  1201. opts->keys_w_stmt = BOOL_TRUE;
  1202. else if (faux_str_cmp(val, "n") == 0)
  1203. opts->keys_w_stmt = BOOL_FALSE;
  1204. }
  1205. if ((val = faux_ini_find(ini, "Colorize"))) {
  1206. if (faux_str_cmp(val, "y") == 0)
  1207. opts->colorize = BOOL_TRUE;
  1208. else if (faux_str_cmp(val, "n") == 0)
  1209. opts->colorize = BOOL_FALSE;
  1210. }
  1211. if ((val = faux_ini_find(ini, "Indent"))) {
  1212. unsigned char indent = 0;
  1213. if (faux_conv_atouc(val, &indent, 10))
  1214. opts->indent = indent;
  1215. }
  1216. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1217. if (faux_str_cmp(val, "y") == 0)
  1218. opts->default_keys = BOOL_TRUE;
  1219. else if (faux_str_cmp(val, "n") == 0)
  1220. opts->default_keys = BOOL_FALSE;
  1221. }
  1222. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1223. if (faux_str_cmp(val, "y") == 0)
  1224. opts->show_default_keys = BOOL_TRUE;
  1225. else if (faux_str_cmp(val, "n") == 0)
  1226. opts->show_default_keys = BOOL_FALSE;
  1227. }
  1228. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1229. if (faux_str_cmp(val, "y") == 0)
  1230. opts->hide_passwords = BOOL_TRUE;
  1231. else if (faux_str_cmp(val, "n") == 0)
  1232. opts->hide_passwords = BOOL_FALSE;
  1233. }
  1234. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1235. if (faux_str_cmp(val, "y") == 0)
  1236. opts->enable_nacm = BOOL_TRUE;
  1237. else if (faux_str_cmp(val, "n") == 0)
  1238. opts->enable_nacm = BOOL_FALSE;
  1239. }
  1240. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1241. if (faux_str_cmp(val, "y") == 0)
  1242. opts->oneliners = BOOL_TRUE;
  1243. else if (faux_str_cmp(val, "n") == 0)
  1244. opts->oneliners = BOOL_FALSE;
  1245. }
  1246. return 0;
  1247. }
  1248. int pline_opts_parse(const char *conf, pline_opts_t *opts)
  1249. {
  1250. faux_ini_t *ini = NULL;
  1251. int rc = -1;
  1252. if (!opts)
  1253. return -1;
  1254. if (!conf)
  1255. return 0; // Use defaults
  1256. ini = faux_ini_new();
  1257. if (!faux_ini_parse_str(ini, conf)) {
  1258. faux_ini_free(ini);
  1259. return -1;
  1260. }
  1261. rc = pline_opts_parse_ini(ini, opts);
  1262. faux_ini_free(ini);
  1263. return rc;
  1264. }
  1265. int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts)
  1266. {
  1267. faux_ini_t *ini = NULL;
  1268. int rc = -1;
  1269. if (!opts)
  1270. return -1;
  1271. if (!conf_name)
  1272. return 0; // Use defaults
  1273. ini = faux_ini_new();
  1274. if (!faux_ini_parse_file(ini, conf_name)) {
  1275. faux_ini_free(ini);
  1276. return -1;
  1277. }
  1278. rc = pline_opts_parse_ini(ini, opts);
  1279. faux_ini_free(ini);
  1280. return rc;
  1281. }