pline.c 33 KB

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