pline.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 "private.h"
  21. #include "pline.h"
  22. static int sr_ly_module_is_internal(const struct lys_module *ly_mod)
  23. {
  24. if (!ly_mod->revision) {
  25. return 0;
  26. }
  27. if (!strcmp(ly_mod->name, "ietf-yang-metadata")
  28. && !strcmp(ly_mod->revision, "2016-08-05")) {
  29. return 1;
  30. } else if (!strcmp(ly_mod->name, "yang")
  31. && !strcmp(ly_mod->revision, "2021-04-07")) {
  32. return 1;
  33. } else if (!strcmp(ly_mod->name, "ietf-inet-types")
  34. && !strcmp(ly_mod->revision, "2013-07-15")) {
  35. return 1;
  36. } else if (!strcmp(ly_mod->name, "ietf-yang-types")
  37. && !strcmp(ly_mod->revision, "2013-07-15")) {
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. static int sr_module_is_internal(const struct lys_module *ly_mod, bool_t enable_nacm)
  43. {
  44. if (!ly_mod->revision) {
  45. return 0;
  46. }
  47. if (sr_ly_module_is_internal(ly_mod)) {
  48. return 1;
  49. }
  50. if (!strcmp(ly_mod->name, "ietf-datastores")
  51. && !strcmp(ly_mod->revision, "2018-02-14")) {
  52. return 1;
  53. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  54. return 1;
  55. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  56. return 1;
  57. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  58. return 1;
  59. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults")
  60. && !strcmp(ly_mod->revision, "2011-06-01")) {
  61. return 1;
  62. } else if (!strcmp(ly_mod->name, "ietf-origin")
  63. && !strcmp(ly_mod->revision, "2018-02-14")) {
  64. return 1;
  65. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications")
  66. && !strcmp(ly_mod->revision, "2012-02-06")) {
  67. return 1;
  68. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  69. return 1;
  70. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  71. return 1;
  72. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  73. return 1;
  74. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm") && !enable_nacm) {
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static pexpr_t *pexpr_new(void)
  80. {
  81. pexpr_t *pexpr = NULL;
  82. pexpr = faux_zmalloc(sizeof(*pexpr));
  83. assert(pexpr);
  84. if (!pexpr)
  85. return NULL;
  86. // Initialize
  87. pexpr->xpath = NULL;
  88. pexpr->value = NULL;
  89. pexpr->active = BOOL_FALSE;
  90. pexpr->pat = PAT_NONE;
  91. pexpr->args_num = 0;
  92. pexpr->list_pos = 0;
  93. pexpr->last_keys = NULL;
  94. return pexpr;
  95. }
  96. static void pexpr_free(pexpr_t *pexpr)
  97. {
  98. if (!pexpr)
  99. return;
  100. faux_str_free(pexpr->xpath);
  101. faux_str_free(pexpr->value);
  102. faux_str_free(pexpr->last_keys);
  103. free(pexpr);
  104. }
  105. static pcompl_t *pcompl_new(void)
  106. {
  107. pcompl_t *pcompl = NULL;
  108. pcompl = faux_zmalloc(sizeof(*pcompl));
  109. assert(pcompl);
  110. if (!pcompl)
  111. return NULL;
  112. // Initialize
  113. pcompl->type = PCOMPL_NODE;
  114. pcompl->node = NULL;
  115. pcompl->xpath = NULL;
  116. pcompl->xpath_ds = SRP_REPO_EDIT;
  117. pcompl->pat = PAT_NONE;
  118. return pcompl;
  119. }
  120. static void pcompl_free(pcompl_t *pcompl)
  121. {
  122. if (!pcompl)
  123. return;
  124. faux_str_free(pcompl->xpath);
  125. free(pcompl);
  126. }
  127. pline_t *pline_new(sr_session_ctx_t *sess)
  128. {
  129. pline_t *pline = NULL;
  130. pline = faux_zmalloc(sizeof(*pline));
  131. assert(pline);
  132. if (!pline)
  133. return NULL;
  134. // Init
  135. pline->sess = sess;
  136. pline->invalid = BOOL_FALSE;
  137. pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  138. NULL, NULL, (faux_list_free_fn)pexpr_free);
  139. pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  140. NULL, NULL, (faux_list_free_fn)pcompl_free);
  141. return pline;
  142. }
  143. void pline_free(pline_t *pline)
  144. {
  145. if (!pline)
  146. return;
  147. faux_list_free(pline->exprs);
  148. faux_list_free(pline->compls);
  149. faux_free(pline);
  150. }
  151. static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath,
  152. size_t args_num, size_t list_pos)
  153. {
  154. pexpr_t *pexpr = NULL;
  155. assert(pline);
  156. pexpr = pexpr_new();
  157. if (xpath)
  158. pexpr->xpath = faux_str_dup(xpath);
  159. pexpr->args_num = args_num;
  160. pexpr->list_pos = list_pos;
  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);
  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)
  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. 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);
  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. pline_add_compl(pline, PCOMPL_NODE, iter, NULL, SRP_REPO_EDIT, pat);
  228. }
  229. }
  230. static const char *pat2str(pat_e pat)
  231. {
  232. const char *str = NULL;
  233. switch (pat) {
  234. case PAT_NONE:
  235. str = "NONE";
  236. break;
  237. case PAT_CONTAINER:
  238. str = "CONTAINER";
  239. break;
  240. case PAT_LIST:
  241. str = "LIST";
  242. break;
  243. case PAT_LIST_KEY:
  244. str = "LIST_KEY";
  245. break;
  246. case PAT_LIST_KEY_INCOMPLETED:
  247. str = "LIST_KEY_INCOMPLETED";
  248. break;
  249. case PAT_LEAF:
  250. str = "LEAF";
  251. break;
  252. case PAT_LEAF_VALUE:
  253. str = "LEAF_VALUE";
  254. break;
  255. case PAT_LEAF_EMPTY:
  256. str = "LEAF_EMPTY";
  257. break;
  258. case PAT_LEAFLIST:
  259. str = "LEAFLIST";
  260. break;
  261. case PAT_LEAFLIST_VALUE:
  262. str = "LEAFLIST_VALUE";
  263. break;
  264. default:
  265. str = "UNKNOWN";
  266. break;
  267. }
  268. return str;
  269. }
  270. void pline_debug(pline_t *pline)
  271. {
  272. faux_list_node_t *iter = NULL;
  273. pexpr_t *pexpr = NULL;
  274. pcompl_t *pcompl = NULL;
  275. syslog(LOG_ERR, "====== Pline:");
  276. syslog(LOG_ERR, "invalid = %s", pline->invalid ? "true" : "false");
  277. syslog(LOG_ERR, "=== Expressions:");
  278. iter = faux_list_head(pline->exprs);
  279. while ((pexpr = (pexpr_t *)faux_list_each(&iter))) {
  280. syslog(LOG_ERR, "pexpr.xpath = %s", pexpr->xpath ? pexpr->xpath : "NULL");
  281. syslog(LOG_ERR, "pexpr.value = %s", pexpr->value ? pexpr->value : "NULL");
  282. syslog(LOG_ERR, "pexpr.active = %s", pexpr->active ? "true" : "false");
  283. syslog(LOG_ERR, "pexpr.pat = %s", pat2str(pexpr->pat));
  284. syslog(LOG_ERR, "pexpr.args_num = %lu", pexpr->args_num);
  285. syslog(LOG_ERR, "pexpr.list_pos = %lu", pexpr->list_pos);
  286. syslog(LOG_ERR, "pexpr.last_keys = %s", pexpr->last_keys ? pexpr->last_keys : "NULL");
  287. syslog(LOG_ERR, "---");
  288. }
  289. syslog(LOG_ERR, "=== Completions:");
  290. iter = faux_list_head(pline->compls);
  291. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  292. syslog(LOG_ERR, "pcompl.type = %s", (pcompl->type == PCOMPL_NODE) ?
  293. "PCOMPL_NODE" : "PCOMPL_TYPE");
  294. syslog(LOG_ERR, "pcompl.node = %s", pcompl->node ? pcompl->node->name : "NULL");
  295. syslog(LOG_ERR, "pcompl.xpath = %s", pcompl->xpath ? pcompl->xpath : "NULL");
  296. syslog(LOG_ERR, "pcompl.pat = %s", pat2str(pcompl->pat));
  297. syslog(LOG_ERR, "---");
  298. }
  299. }
  300. static bool_t pexpr_xpath_add_node(pexpr_t *pexpr,
  301. const char *prefix, const char *name)
  302. {
  303. char *tmp = NULL;
  304. assert(pexpr);
  305. assert(prefix);
  306. assert(name);
  307. tmp = faux_str_sprintf("/%s:%s", prefix, name);
  308. faux_str_cat(&pexpr->xpath, tmp);
  309. faux_str_free(tmp);
  310. pexpr->args_num++;
  311. // Activate current expression. Because it really has
  312. // new component
  313. pexpr->active = BOOL_TRUE;
  314. return BOOL_TRUE;
  315. }
  316. static bool_t pexpr_xpath_add_list_key(pexpr_t *pexpr,
  317. const char *key, const char *value, bool_t inc_args_num)
  318. {
  319. char *tmp = NULL;
  320. char *escaped = NULL;
  321. assert(pexpr);
  322. assert(key);
  323. assert(value);
  324. escaped = faux_str_c_esc(value);
  325. tmp = faux_str_sprintf("[%s=\"%s\"]", key, escaped);
  326. faux_str_free(escaped);
  327. faux_str_cat(&pexpr->xpath, tmp);
  328. faux_str_cat(&pexpr->last_keys, tmp);
  329. faux_str_free(tmp);
  330. if (inc_args_num)
  331. pexpr->args_num++;
  332. return BOOL_TRUE;
  333. }
  334. static bool_t pexpr_xpath_add_leaflist_key(pexpr_t *pexpr,
  335. const char *prefix, const char *value)
  336. {
  337. char *tmp = NULL;
  338. assert(pexpr);
  339. assert(value);
  340. tmp = faux_str_sprintf("[.='%s%s%s']",
  341. prefix ? prefix : "", prefix ? ":" : "", value);
  342. faux_str_cat(&pexpr->xpath, tmp);
  343. faux_str_cat(&pexpr->last_keys, value);
  344. faux_str_free(tmp);
  345. pexpr->args_num++;
  346. return BOOL_TRUE;
  347. }
  348. static void pline_add_compl_leafref(pline_t *pline, const struct lysc_node *node,
  349. const struct lysc_type *type, const char *xpath, pat_e pat)
  350. {
  351. if (!type)
  352. return;
  353. if (!node)
  354. return;
  355. if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
  356. return;
  357. switch (type->basetype) {
  358. case LY_TYPE_UNION: {
  359. struct lysc_type_union *t =
  360. (struct lysc_type_union *)type;
  361. LY_ARRAY_COUNT_TYPE u = 0;
  362. LY_ARRAY_FOR(t->types, u) {
  363. pline_add_compl_leafref(pline, node, t->types[u], xpath, pat);
  364. }
  365. break;
  366. }
  367. case LY_TYPE_LEAFREF: {
  368. char *compl_xpath = klysc_leafref_xpath(node, type, xpath);
  369. pline_add_compl(pline, PCOMPL_TYPE, NULL, compl_xpath, SRP_REPO_EDIT, pat);
  370. faux_str_free(compl_xpath);
  371. break;
  372. }
  373. default:
  374. break;
  375. }
  376. }
  377. static void pline_add_compl_leaf(pline_t *pline, const struct lysc_node *node,
  378. const char *xpath, pat_e pat)
  379. {
  380. struct lysc_type *type = NULL;
  381. const char *ext_xpath = NULL;
  382. assert(pline);
  383. if (!pline)
  384. return;
  385. assert(node);
  386. if (!node)
  387. return;
  388. switch (node->nodetype) {
  389. case LYS_LEAF:
  390. type = ((struct lysc_node_leaf *)node)->type;
  391. break;
  392. case LYS_LEAFLIST:
  393. type = ((struct lysc_node_leaflist *)node)->type;
  394. break;
  395. default:
  396. return;
  397. }
  398. ext_xpath = klysc_node_ext_completion(node);
  399. if (ext_xpath) {
  400. const char *raw_xpath = NULL;
  401. sr_datastore_t ds = SRP_REPO_EDIT;
  402. if (kly_parse_ext_xpath(ext_xpath, &raw_xpath, &ds))
  403. pline_add_compl(pline, PCOMPL_TYPE, NULL, raw_xpath, ds, pat);
  404. }
  405. pline_add_compl(pline, PCOMPL_TYPE, node, xpath, SRP_REPO_EDIT, pat);
  406. pline_add_compl_leafref(pline, node, type, xpath, pat);
  407. }
  408. static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
  409. pline_t *pline, pline_opts_t *opts)
  410. {
  411. faux_argv_node_t *arg = faux_argv_iter(argv);
  412. const struct lysc_node *node = NULL;
  413. char *rollback_xpath = NULL;
  414. size_t rollback_args_num = 0;
  415. size_t rollback_list_pos = 0;
  416. // Rollback is a mechanism to roll to previous node while
  417. // oneliners parsing
  418. bool_t rollback = BOOL_FALSE;
  419. pexpr_t *first_pexpr = NULL;
  420. // It's necessary because upper function can use the same pline object
  421. // for another modules before. It uses the same object to collect
  422. // possible completions. But pline is really invalid only when all
  423. // modules don't recognize argument.
  424. pline->invalid = BOOL_FALSE;
  425. do {
  426. pexpr_t *pexpr = pline_current_expr(pline);
  427. const char *str = (const char *)faux_argv_current(arg);
  428. bool_t is_rollback = rollback;
  429. bool_t next_arg = BOOL_TRUE;
  430. rollback = BOOL_FALSE;
  431. if (node && !is_rollback) {
  432. // Save rollback Xpath (for oneliners) before leaf node
  433. // Only leaf and leaf-list node allows to "rollback"
  434. // the path and add additional statements
  435. if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
  436. faux_str_free(rollback_xpath);
  437. rollback_xpath = faux_str_dup(pexpr->xpath);
  438. rollback_args_num = pexpr->args_num;
  439. rollback_list_pos = pexpr->list_pos;
  440. }
  441. // Add current node to Xpath
  442. pexpr_xpath_add_node(pexpr,
  443. node->module->name, node->name);
  444. }
  445. // Root of the module
  446. if (!node) {
  447. // Completion
  448. if (!str) {
  449. pline_add_compl_subtree(pline, module, node);
  450. break;
  451. }
  452. // Next element
  453. node = klysc_find_child(module->compiled->data, str);
  454. if (!node)
  455. break;
  456. // Container
  457. } else if (node->nodetype & LYS_CONTAINER) {
  458. pexpr->pat = PAT_CONTAINER;
  459. // Completion
  460. if (!str) {
  461. pline_add_compl_subtree(pline, module, node);
  462. break;
  463. }
  464. // Next element
  465. node = klysc_find_child(lysc_node_child(node), str);
  466. // List
  467. } else if (node->nodetype & LYS_LIST) {
  468. const struct lysc_node *iter = NULL;
  469. pexpr->pat = PAT_LIST;
  470. pexpr->list_pos = pexpr->args_num;
  471. faux_str_free(pexpr->last_keys);
  472. pexpr->last_keys = NULL;
  473. // Next element
  474. if (!is_rollback) {
  475. bool_t break_upper_loop = BOOL_FALSE;
  476. // Keys without statement. Positional parameters.
  477. if (!opts->keys_w_stmt) {
  478. LY_LIST_FOR(lysc_node_child(node), iter) {
  479. struct lysc_node_leaf *leaf =
  480. (struct lysc_node_leaf *)iter;
  481. if (!(iter->nodetype & LYS_LEAF))
  482. continue;
  483. if (!(iter->flags & LYS_KEY))
  484. continue;
  485. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  486. // Completion
  487. if (!str) {
  488. char *tmp = faux_str_sprintf("%s/%s",
  489. pexpr->xpath, leaf->name);
  490. pline_add_compl_leaf(pline, iter,
  491. tmp, PAT_LIST_KEY);
  492. faux_str_free(tmp);
  493. break_upper_loop = BOOL_TRUE;
  494. break;
  495. }
  496. pexpr_xpath_add_list_key(pexpr,
  497. leaf->name, str, BOOL_TRUE);
  498. faux_argv_each(&arg);
  499. str = (const char *)faux_argv_current(arg);
  500. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  501. }
  502. // Keys with statements. Arbitrary order of keys.
  503. } else {
  504. faux_list_t *keys = NULL;
  505. unsigned int specified_keys_num = 0;
  506. klysc_key_t *cur_key = NULL;
  507. bool_t first_key = BOOL_TRUE;
  508. bool_t first_key_is_optional = BOOL_FALSE;
  509. faux_list_node_t *key_iter = NULL;
  510. // List keys
  511. keys = faux_list_new(FAUX_LIST_UNSORTED,
  512. FAUX_LIST_UNIQUE,
  513. klysc_key_compare,
  514. klysc_key_kcompare,
  515. (faux_list_free_fn)faux_free);
  516. LY_LIST_FOR(lysc_node_child(node), iter) {
  517. struct lysc_node_leaf *leaf =
  518. (struct lysc_node_leaf *)iter;
  519. klysc_key_t *key = NULL;
  520. if (!(iter->nodetype & LYS_LEAF))
  521. continue;
  522. if (!(iter->flags & LYS_KEY))
  523. continue;
  524. assert (leaf->type->basetype != LY_TYPE_EMPTY);
  525. key = faux_zmalloc(sizeof(*key));
  526. assert(key);
  527. key->node = iter;
  528. if (opts->default_keys &&
  529. (key->dflt = klysc_node_ext_default(iter))) {
  530. if (first_key)
  531. first_key_is_optional = BOOL_TRUE;
  532. }
  533. faux_list_add(keys, key);
  534. first_key = BOOL_FALSE;
  535. }
  536. while (specified_keys_num < faux_list_len(keys)) {
  537. // First key without statement. Must be mandatory.
  538. if ((0 == specified_keys_num) &&
  539. !opts->first_key_w_stmt &&
  540. !first_key_is_optional) {
  541. cur_key = (klysc_key_t *)faux_list_data(faux_list_head(keys));
  542. } else {
  543. if (!str)
  544. break;
  545. cur_key = faux_list_kfind(keys, str);
  546. if (!cur_key || cur_key->value)
  547. break;
  548. pexpr->args_num++;
  549. faux_argv_each(&arg);
  550. str = (const char *)faux_argv_current(arg);
  551. }
  552. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  553. // Completion
  554. if (!str) {
  555. char *tmp = faux_str_sprintf("%s/%s",
  556. pexpr->xpath,
  557. cur_key->node->name);
  558. pline_add_compl_leaf(pline, cur_key->node,
  559. tmp, PAT_LIST_KEY);
  560. faux_str_free(tmp);
  561. break_upper_loop = BOOL_TRUE;
  562. break;
  563. }
  564. pexpr_xpath_add_list_key(pexpr,
  565. cur_key->node->name, str, BOOL_TRUE);
  566. cur_key->value = str;
  567. specified_keys_num++;
  568. faux_argv_each(&arg);
  569. str = (const char *)faux_argv_current(arg);
  570. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  571. }
  572. if (break_upper_loop) {
  573. faux_list_free(keys);
  574. break;
  575. }
  576. key_iter = faux_list_head(keys);
  577. while((cur_key = (klysc_key_t *)faux_list_each(&key_iter))) {
  578. if (cur_key->value)
  579. continue;
  580. // Completion
  581. if (!str)
  582. pline_add_compl(pline, PCOMPL_NODE,
  583. cur_key->node, NULL,
  584. SRP_REPO_EDIT, PAT_LIST_KEY);
  585. if (opts->default_keys && cur_key->dflt) {
  586. pexpr_xpath_add_list_key(pexpr,
  587. cur_key->node->name,
  588. cur_key->dflt, BOOL_FALSE);
  589. pexpr->pat = PAT_LIST_KEY_INCOMPLETED;
  590. } else { // Mandatory key is not specified
  591. break_upper_loop = BOOL_TRUE;
  592. }
  593. }
  594. faux_list_free(keys);
  595. }
  596. if (break_upper_loop)
  597. break;
  598. }
  599. pexpr->pat = PAT_LIST_KEY;
  600. // Completion
  601. if (!str) {
  602. pline_add_compl_subtree(pline, module, node);
  603. break;
  604. }
  605. // Next element
  606. node = klysc_find_child(lysc_node_child(node), str);
  607. // Leaf
  608. } else if (node->nodetype & LYS_LEAF) {
  609. struct lysc_node_leaf *leaf =
  610. (struct lysc_node_leaf *)node;
  611. // Next element
  612. if (LY_TYPE_EMPTY == leaf->type->basetype) {
  613. pexpr->pat = PAT_LEAF_EMPTY;
  614. // Completion
  615. if (!str) {
  616. pline_add_compl_subtree(pline,
  617. module, node->parent);
  618. break;
  619. }
  620. // Don't get next argument when argument is not
  621. // really consumed
  622. next_arg = BOOL_FALSE;
  623. } else {
  624. pexpr->pat = PAT_LEAF;
  625. // Completion
  626. if (!str) {
  627. pline_add_compl_leaf(pline, node,
  628. pexpr->xpath, PAT_LEAF_VALUE);
  629. break;
  630. }
  631. pexpr->pat = PAT_LEAF_VALUE;
  632. // Idenity must have prefix
  633. if (LY_TYPE_IDENT == leaf->type->basetype) {
  634. const char *prefix = NULL;
  635. prefix = klysc_identityref_prefix(
  636. (struct lysc_type_identityref *)
  637. leaf->type, str);
  638. if (prefix)
  639. pexpr->value = faux_str_sprintf(
  640. "%s:", prefix);
  641. }
  642. faux_str_cat(&pexpr->value, str);
  643. }
  644. // Expression was completed
  645. // So rollback (for oneliners)
  646. node = node->parent;
  647. pline_add_expr(pline, rollback_xpath,
  648. rollback_args_num, rollback_list_pos);
  649. rollback = BOOL_TRUE;
  650. // Leaf-list
  651. } else if (node->nodetype & LYS_LEAFLIST) {
  652. const char *prefix = NULL;
  653. struct lysc_node_leaflist *leaflist =
  654. (struct lysc_node_leaflist *)node;
  655. pexpr->pat = PAT_LEAFLIST;
  656. pexpr->list_pos = pexpr->args_num;
  657. faux_str_free(pexpr->last_keys);
  658. pexpr->last_keys = NULL;
  659. // Completion
  660. if (!str) {
  661. pline_add_compl_leaf(pline, node,
  662. pexpr->xpath, PAT_LEAFLIST_VALUE);
  663. break;
  664. }
  665. pexpr->pat = PAT_LEAFLIST_VALUE;
  666. // Idenity must have prefix
  667. if (LY_TYPE_IDENT == leaflist->type->basetype) {
  668. prefix = klysc_identityref_prefix(
  669. (struct lysc_type_identityref *)
  670. leaflist->type, str);
  671. }
  672. pexpr_xpath_add_leaflist_key(pexpr, prefix, str);
  673. // Expression was completed
  674. // So rollback (for oneliners)
  675. node = node->parent;
  676. pline_add_expr(pline, rollback_xpath,
  677. rollback_args_num, rollback_list_pos);
  678. rollback = BOOL_TRUE;
  679. // LYS_CHOICE and LYS_CASE can appear while rollback only
  680. } else if (node->nodetype & (LYS_CHOICE | LYS_CASE)) {
  681. // Don't set pexpr->pat because CHOICE and CASE can't
  682. // appear within data tree (schema only)
  683. // Completion
  684. if (!str) {
  685. pline_add_compl_subtree(pline, module, node);
  686. break;
  687. }
  688. // Next element
  689. node = klysc_find_child(lysc_node_child(node), str);
  690. } else {
  691. break;
  692. }
  693. // Current argument was not consumed.
  694. // Break before getting next arg.
  695. if (!node && !rollback)
  696. break;
  697. if (next_arg)
  698. faux_argv_each(&arg);
  699. } while (BOOL_TRUE);
  700. // There is not-consumed argument so whole pline is invalid
  701. if (faux_argv_current(arg))
  702. pline->invalid = BOOL_TRUE;
  703. faux_str_free(rollback_xpath);
  704. first_pexpr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
  705. if (!first_pexpr || !first_pexpr->xpath)
  706. return BOOL_FALSE; // Not found
  707. return BOOL_TRUE;
  708. }
  709. pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, 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. int pline_parse_conf(const char *conf, pline_opts_t *opts)
  1126. {
  1127. faux_ini_t *ini = NULL;
  1128. const char *val = NULL;
  1129. if (!opts)
  1130. return -1;
  1131. if (!conf)
  1132. return 0; // Use defaults
  1133. ini = faux_ini_new();
  1134. if (!faux_ini_parse_str(ini, conf)) {
  1135. faux_ini_free(ini);
  1136. return -1;
  1137. }
  1138. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1139. if (faux_str_cmp(val, "y") == 0)
  1140. opts->show_brackets = BOOL_TRUE;
  1141. else if (faux_str_cmp(val, "n") == 0)
  1142. opts->show_brackets = BOOL_FALSE;
  1143. }
  1144. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1145. if (faux_str_cmp(val, "y") == 0)
  1146. opts->show_semicolons = BOOL_TRUE;
  1147. else if (faux_str_cmp(val, "n") == 0)
  1148. opts->show_semicolons = BOOL_FALSE;
  1149. }
  1150. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1151. if (faux_str_cmp(val, "y") == 0)
  1152. opts->first_key_w_stmt = BOOL_TRUE;
  1153. else if (faux_str_cmp(val, "n") == 0)
  1154. opts->first_key_w_stmt = BOOL_FALSE;
  1155. }
  1156. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1157. if (faux_str_cmp(val, "y") == 0)
  1158. opts->keys_w_stmt = BOOL_TRUE;
  1159. else if (faux_str_cmp(val, "n") == 0)
  1160. opts->keys_w_stmt = BOOL_FALSE;
  1161. }
  1162. if ((val = faux_ini_find(ini, "Colorize"))) {
  1163. if (faux_str_cmp(val, "y") == 0)
  1164. opts->colorize = BOOL_TRUE;
  1165. else if (faux_str_cmp(val, "n") == 0)
  1166. opts->colorize = BOOL_FALSE;
  1167. }
  1168. if ((val = faux_ini_find(ini, "Indent"))) {
  1169. unsigned char indent = 0;
  1170. if (faux_conv_atouc(val, &indent, 10))
  1171. opts->indent = indent;
  1172. }
  1173. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1174. if (faux_str_cmp(val, "y") == 0)
  1175. opts->default_keys = BOOL_TRUE;
  1176. else if (faux_str_cmp(val, "n") == 0)
  1177. opts->default_keys = BOOL_FALSE;
  1178. }
  1179. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1180. if (faux_str_cmp(val, "y") == 0)
  1181. opts->show_default_keys = BOOL_TRUE;
  1182. else if (faux_str_cmp(val, "n") == 0)
  1183. opts->show_default_keys = BOOL_FALSE;
  1184. }
  1185. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1186. if (faux_str_cmp(val, "y") == 0)
  1187. opts->hide_passwords = BOOL_TRUE;
  1188. else if (faux_str_cmp(val, "n") == 0)
  1189. opts->hide_passwords = BOOL_FALSE;
  1190. }
  1191. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1192. if (faux_str_cmp(val, "y") == 0)
  1193. opts->enable_nacm = BOOL_TRUE;
  1194. else if (faux_str_cmp(val, "n") == 0)
  1195. opts->enable_nacm = BOOL_FALSE;
  1196. }
  1197. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1198. if (faux_str_cmp(val, "y") == 0)
  1199. opts->oneliners = BOOL_TRUE;
  1200. else if (faux_str_cmp(val, "n") == 0)
  1201. opts->oneliners = BOOL_FALSE;
  1202. }
  1203. faux_ini_free(ini);
  1204. return 0;
  1205. }