pline.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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, faux_argv_t *argv,
  408. pline_t *pline, 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, faux_argv_t *argv, pline_opts_t *opts)
  709. {
  710. const struct ly_ctx *ctx = NULL;
  711. struct lys_module *module = NULL;
  712. pline_t *pline = NULL;
  713. uint32_t i = 0;
  714. faux_list_node_t *last_expr_node = NULL;
  715. assert(sess);
  716. if (!sess)
  717. return NULL;
  718. pline = pline_new(sess);
  719. if (!pline)
  720. return NULL;
  721. ctx = sr_session_acquire_context(pline->sess);
  722. if (!ctx)
  723. return NULL;
  724. // Iterate all modules
  725. i = 0;
  726. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  727. if (sr_module_is_internal(module, opts->enable_nacm))
  728. continue;
  729. if (!module->compiled)
  730. continue;
  731. if (!module->implemented)
  732. continue;
  733. if (!module->compiled->data)
  734. continue;
  735. if (pline_parse_module(module, argv, pline, opts))
  736. break; // Found
  737. }
  738. sr_session_release_context(pline->sess);
  739. // Last parsed expression can be inactive so remove it from list
  740. last_expr_node = faux_list_tail(pline->exprs);
  741. if (last_expr_node) {
  742. pexpr_t *expr = (pexpr_t *)faux_list_data(last_expr_node);
  743. if (!expr->active)
  744. faux_list_del(pline->exprs, last_expr_node);
  745. }
  746. return pline;
  747. }
  748. static void identityref_compl(struct lysc_ident *ident)
  749. {
  750. LY_ARRAY_COUNT_TYPE u = 0;
  751. if (!ident)
  752. return;
  753. if (!ident->derived) {
  754. printf("%s\n", ident->name);
  755. return;
  756. }
  757. LY_ARRAY_FOR(ident->derived, u) {
  758. identityref_compl(ident->derived[u]);
  759. }
  760. }
  761. static void identityref_help(struct lysc_ident *ident)
  762. {
  763. LY_ARRAY_COUNT_TYPE u = 0;
  764. if (!ident)
  765. return;
  766. if (!ident->derived) {
  767. if (ident->dsc) {
  768. char *dsc = faux_str_getline(ident->dsc, NULL);
  769. printf("%s\n%s\n", ident->name, dsc);
  770. faux_str_free(dsc);
  771. } else {
  772. printf("%s\n%s\n", ident->name, ident->name);
  773. }
  774. return;
  775. }
  776. LY_ARRAY_FOR(ident->derived, u) {
  777. identityref_help(ident->derived[u]);
  778. }
  779. }
  780. static void pline_print_type_completions(const struct lysc_type *type)
  781. {
  782. assert(type);
  783. switch (type->basetype) {
  784. case LY_TYPE_BOOL: {
  785. printf("true\nfalse\n");
  786. break;
  787. }
  788. case LY_TYPE_ENUM: {
  789. const struct lysc_type_enum *t =
  790. (const struct lysc_type_enum *)type;
  791. LY_ARRAY_COUNT_TYPE u = 0;
  792. LY_ARRAY_FOR(t->enums, u) {
  793. printf("%s\n",t->enums[u].name);
  794. }
  795. break;
  796. }
  797. case LY_TYPE_IDENT: {
  798. struct lysc_type_identityref *t =
  799. (struct lysc_type_identityref *)type;
  800. LY_ARRAY_COUNT_TYPE u = 0;
  801. LY_ARRAY_FOR(t->bases, u) {
  802. identityref_compl(t->bases[u]);
  803. }
  804. break;
  805. }
  806. case LY_TYPE_UNION: {
  807. struct lysc_type_union *t =
  808. (struct lysc_type_union *)type;
  809. LY_ARRAY_COUNT_TYPE u = 0;
  810. LY_ARRAY_FOR(t->types, u) {
  811. pline_print_type_completions(t->types[u]);
  812. }
  813. break;
  814. }
  815. case LY_TYPE_LEAFREF: {
  816. struct lysc_type_leafref *t =
  817. (struct lysc_type_leafref *)type;
  818. pline_print_type_completions(t->realtype);
  819. break;
  820. }
  821. default:
  822. break;
  823. }
  824. }
  825. static void uint_range(const struct lysc_type *type, uint64_t def_min, uint64_t def_max)
  826. {
  827. struct lysc_range *range = NULL;
  828. LY_ARRAY_COUNT_TYPE u = 0;
  829. char *r = NULL;
  830. assert(type);
  831. range = ((struct lysc_type_num *)type)->range;
  832. // Show defaults
  833. if (!range) {
  834. printf("[%" PRIu64 "..%" PRIu64 "]\n", def_min, def_max);
  835. return;
  836. }
  837. // Range
  838. faux_str_cat(&r, "[");
  839. LY_ARRAY_FOR(range->parts, u) {
  840. char *t = NULL;
  841. if (u != 0)
  842. faux_str_cat(&r, "|");
  843. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  844. range->parts[u].min_u64, range->parts[u].max_u64);
  845. faux_str_cat(&r, t);
  846. faux_str_free(t);
  847. }
  848. faux_str_cat(&r, "]\n");
  849. printf("%s", r);
  850. faux_free(r);
  851. }
  852. static void int_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  853. {
  854. struct lysc_range *range = NULL;
  855. LY_ARRAY_COUNT_TYPE u = 0;
  856. char *r = NULL;
  857. assert(type);
  858. range = ((struct lysc_type_num *)type)->range;
  859. // Show defaults
  860. if (!range) {
  861. printf("[%" PRId64 "..%" PRId64 "]\n", def_min, def_max);
  862. return;
  863. }
  864. // Range
  865. faux_str_cat(&r, "[");
  866. LY_ARRAY_FOR(range->parts, u) {
  867. char *t = NULL;
  868. if (u != 0)
  869. faux_str_cat(&r, "|");
  870. t = faux_str_sprintf("%" PRId64 "..%" PRId64,
  871. range->parts[u].min_64, range->parts[u].max_64);
  872. faux_str_cat(&r, t);
  873. faux_str_free(t);
  874. }
  875. faux_str_cat(&r, "]\n");
  876. printf("%s", r);
  877. faux_free(r);
  878. }
  879. static void dec_range(const struct lysc_type *type, int64_t def_min, int64_t def_max)
  880. {
  881. struct lysc_range *range = NULL;
  882. uint8_t fraction_digits = 0;
  883. LY_ARRAY_COUNT_TYPE u = 0;
  884. char *r = NULL;
  885. int64_t div = 1;
  886. uint8_t i = 0;
  887. assert(type);
  888. range = ((struct lysc_type_dec *)type)->range;
  889. fraction_digits = ((struct lysc_type_dec *)type)->fraction_digits;
  890. for (i = 0; i < fraction_digits; i++)
  891. div = div * 10;
  892. // Show defaults
  893. if (!range) {
  894. printf("[%.*f..%.*f]\n",
  895. fraction_digits, (double)def_min / div,
  896. fraction_digits, (double)def_max / div);
  897. return;
  898. }
  899. // Range
  900. faux_str_cat(&r, "[");
  901. LY_ARRAY_FOR(range->parts, u) {
  902. char *t = NULL;
  903. if (u != 0)
  904. faux_str_cat(&r, "|");
  905. t = faux_str_sprintf("%.*f..%.*f",
  906. fraction_digits, (double)range->parts[u].min_64 / div,
  907. fraction_digits, (double)range->parts[u].max_64 / div);
  908. faux_str_cat(&r, t);
  909. faux_str_free(t);
  910. }
  911. faux_str_cat(&r, "]\n");
  912. printf("%s", r);
  913. faux_free(r);
  914. }
  915. static void str_range(const struct lysc_type *type)
  916. {
  917. struct lysc_range *range = NULL;
  918. LY_ARRAY_COUNT_TYPE u = 0;
  919. char *r = NULL;
  920. assert(type);
  921. range = ((struct lysc_type_str *)type)->length;
  922. // Show defaults
  923. if (!range) {
  924. printf("<string>\n");
  925. return;
  926. }
  927. // Range
  928. faux_str_cat(&r, "<string[");
  929. LY_ARRAY_FOR(range->parts, u) {
  930. char *t = NULL;
  931. if (u != 0)
  932. faux_str_cat(&r, "|");
  933. t = faux_str_sprintf("%" PRIu64 "..%" PRIu64,
  934. range->parts[u].min_u64, range->parts[u].max_u64);
  935. faux_str_cat(&r, t);
  936. faux_str_free(t);
  937. }
  938. faux_str_cat(&r, "]>\n");
  939. printf("%s", r);
  940. faux_free(r);
  941. }
  942. static void pline_print_type_help(const struct lysc_node *node,
  943. const struct lysc_type *type)
  944. {
  945. const char *units = NULL;
  946. assert(type);
  947. assert(node);
  948. if (node->nodetype & LYS_LEAF)
  949. units = ((struct lysc_node_leaf *)node)->units;
  950. else if (node->nodetype & LYS_LEAFLIST)
  951. units = ((struct lysc_node_leaflist *)node)->units;
  952. else
  953. return;
  954. if (units) {
  955. printf("%s\n", units);
  956. } else {
  957. switch (type->basetype) {
  958. case LY_TYPE_UINT8:
  959. uint_range(type, 0, UCHAR_MAX);
  960. break;
  961. case LY_TYPE_UINT16:
  962. uint_range(type, 0, USHRT_MAX);
  963. break;
  964. case LY_TYPE_UINT32:
  965. uint_range(type, 0, UINT_MAX);
  966. break;
  967. case LY_TYPE_UINT64:
  968. uint_range(type, 0, ULLONG_MAX);
  969. break;
  970. case LY_TYPE_INT8:
  971. int_range(type, CHAR_MIN, CHAR_MAX);
  972. break;
  973. case LY_TYPE_INT16:
  974. int_range(type, SHRT_MIN, SHRT_MAX);
  975. break;
  976. case LY_TYPE_INT32:
  977. int_range(type, INT_MIN, INT_MAX);
  978. break;
  979. case LY_TYPE_INT64:
  980. int_range(type, LLONG_MIN, LLONG_MAX);
  981. break;
  982. case LY_TYPE_DEC64:
  983. dec_range(type, LLONG_MIN, LLONG_MAX);
  984. break;
  985. case LY_TYPE_STRING:
  986. str_range(type);
  987. break;
  988. case LY_TYPE_BOOL:
  989. printf("<true/false>\n");
  990. break;
  991. case LY_TYPE_LEAFREF: {
  992. const struct lysc_type_leafref *t =
  993. (const struct lysc_type_leafref *)type;
  994. const struct lysc_node *ref_node = NULL;
  995. const struct lysc_type *ref_type = NULL;
  996. char *node_path = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
  997. char *path = klysc_leafref_xpath(node, type, node_path);
  998. faux_str_free(node_path);
  999. ref_node = lys_find_path(NULL, node, path, 0);
  1000. faux_str_free(path);
  1001. if (!ref_node) {
  1002. pline_print_type_help(node, t->realtype);
  1003. return; // Because it prints whole info itself
  1004. }
  1005. if (ref_node->nodetype & LYS_LEAF)
  1006. ref_type = ((struct lysc_node_leaf *)ref_node)->type;
  1007. else
  1008. ref_type = ((struct lysc_node_leaflist *)ref_node)->type;
  1009. pline_print_type_help(ref_node, ref_type);
  1010. return; // Because it prints whole info itself
  1011. }
  1012. case LY_TYPE_UNION: {
  1013. const struct lysc_type_union *t =
  1014. (const struct lysc_type_union *)type;
  1015. LY_ARRAY_COUNT_TYPE u = 0;
  1016. LY_ARRAY_FOR(t->types, u)
  1017. pline_print_type_help(node, t->types[u]);
  1018. return; // Because it prints whole info itself
  1019. }
  1020. case LY_TYPE_ENUM: {
  1021. const struct lysc_type_enum *t =
  1022. (const struct lysc_type_enum *)type;
  1023. LY_ARRAY_COUNT_TYPE u = 0;
  1024. LY_ARRAY_FOR(t->enums, u)
  1025. if (t->enums[u].dsc) {
  1026. char *dsc = faux_str_getline(
  1027. t->enums[u].dsc, NULL);
  1028. printf("%s\n%s\n",
  1029. t->enums[u].name, dsc);
  1030. faux_str_free(dsc);
  1031. } else {
  1032. printf("%s\n%s\n",
  1033. t->enums[u].name,
  1034. t->enums[u].name);
  1035. }
  1036. return; // Because it prints whole info itself
  1037. }
  1038. case LY_TYPE_IDENT: {
  1039. struct lysc_type_identityref *t =
  1040. (struct lysc_type_identityref *)type;
  1041. LY_ARRAY_COUNT_TYPE u = 0;
  1042. LY_ARRAY_FOR(t->bases, u)
  1043. identityref_help(t->bases[u]);
  1044. return; // Because it prints whole info itself
  1045. }
  1046. default:
  1047. printf("<unknown>\n");
  1048. break;
  1049. }
  1050. }
  1051. if (node->dsc) {
  1052. char *dsc = faux_str_getline(node->dsc, NULL);
  1053. printf("%s\n", dsc);
  1054. faux_str_free(dsc);
  1055. } else {
  1056. printf("%s\n", node->name);
  1057. }
  1058. }
  1059. void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_types)
  1060. {
  1061. faux_list_node_t *iter = NULL;
  1062. pcompl_t *pcompl = NULL;
  1063. sr_datastore_t current_ds = SRP_REPO_EDIT;
  1064. iter = faux_list_head(pline->compls);
  1065. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1066. struct lysc_type *type = NULL;
  1067. const struct lysc_node *node = pcompl->node;
  1068. if (!(pcompl->pat & enabled_types))
  1069. continue;
  1070. if (pcompl->xpath && !help) {
  1071. sr_val_t *vals = NULL;
  1072. size_t val_num = 0;
  1073. size_t i = 0;
  1074. //printf("%s\n", pcompl->xpath);
  1075. // Switch to necessary DS
  1076. if (current_ds != pcompl->xpath_ds) {
  1077. sr_session_switch_ds(pline->sess, pcompl->xpath_ds);
  1078. current_ds = pcompl->xpath_ds;
  1079. }
  1080. sr_get_items(pline->sess, pcompl->xpath,
  1081. 0, 0, &vals, &val_num);
  1082. for (i = 0; i < val_num; i++) {
  1083. char *tmp = sr_val_to_str(&vals[i]);
  1084. if (!tmp)
  1085. continue;
  1086. printf("%s\n", tmp);
  1087. free(tmp);
  1088. }
  1089. sr_free_values(vals, val_num);
  1090. }
  1091. if (!node)
  1092. continue;
  1093. // Node
  1094. if (PCOMPL_NODE == pcompl->type) {
  1095. printf("%s\n", node->name);
  1096. if (help) {
  1097. if (!node->dsc) {
  1098. printf("%s\n", node->name);
  1099. } else {
  1100. char *dsc = faux_str_getline(node->dsc,
  1101. NULL);
  1102. printf("%s\n", dsc);
  1103. faux_str_free(dsc);
  1104. }
  1105. }
  1106. continue;
  1107. }
  1108. // Type
  1109. if (node->nodetype & LYS_LEAF)
  1110. type = ((struct lysc_node_leaf *)node)->type;
  1111. else if (node->nodetype & LYS_LEAFLIST)
  1112. type = ((struct lysc_node_leaflist *)node)->type;
  1113. else
  1114. continue;
  1115. if (help)
  1116. pline_print_type_help(node, type);
  1117. else
  1118. pline_print_type_completions(type);
  1119. }
  1120. // Restore default DS
  1121. if (current_ds != SRP_REPO_EDIT)
  1122. sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
  1123. }
  1124. void pline_opts_init(pline_opts_t *opts)
  1125. {
  1126. opts->begin_bracket = '{';
  1127. opts->end_bracket = '}';
  1128. opts->show_brackets = BOOL_TRUE;
  1129. opts->show_semicolons = BOOL_TRUE;
  1130. opts->first_key_w_stmt = BOOL_FALSE;
  1131. opts->keys_w_stmt = BOOL_TRUE;
  1132. opts->colorize = BOOL_TRUE;
  1133. opts->indent = 2;
  1134. opts->default_keys = BOOL_FALSE;
  1135. opts->show_default_keys = BOOL_FALSE;
  1136. opts->hide_passwords = BOOL_TRUE;
  1137. opts->enable_nacm = BOOL_FALSE;
  1138. opts->oneliners = BOOL_TRUE;
  1139. }
  1140. int pline_opts_parse(const char *conf, pline_opts_t *opts)
  1141. {
  1142. faux_ini_t *ini = NULL;
  1143. const char *val = NULL;
  1144. if (!opts)
  1145. return -1;
  1146. if (!conf)
  1147. return 0; // Use defaults
  1148. ini = faux_ini_new();
  1149. if (!faux_ini_parse_str(ini, conf)) {
  1150. faux_ini_free(ini);
  1151. return -1;
  1152. }
  1153. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1154. if (faux_str_cmp(val, "y") == 0)
  1155. opts->show_brackets = BOOL_TRUE;
  1156. else if (faux_str_cmp(val, "n") == 0)
  1157. opts->show_brackets = BOOL_FALSE;
  1158. }
  1159. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1160. if (faux_str_cmp(val, "y") == 0)
  1161. opts->show_semicolons = BOOL_TRUE;
  1162. else if (faux_str_cmp(val, "n") == 0)
  1163. opts->show_semicolons = BOOL_FALSE;
  1164. }
  1165. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1166. if (faux_str_cmp(val, "y") == 0)
  1167. opts->first_key_w_stmt = BOOL_TRUE;
  1168. else if (faux_str_cmp(val, "n") == 0)
  1169. opts->first_key_w_stmt = BOOL_FALSE;
  1170. }
  1171. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1172. if (faux_str_cmp(val, "y") == 0)
  1173. opts->keys_w_stmt = BOOL_TRUE;
  1174. else if (faux_str_cmp(val, "n") == 0)
  1175. opts->keys_w_stmt = BOOL_FALSE;
  1176. }
  1177. if ((val = faux_ini_find(ini, "Colorize"))) {
  1178. if (faux_str_cmp(val, "y") == 0)
  1179. opts->colorize = BOOL_TRUE;
  1180. else if (faux_str_cmp(val, "n") == 0)
  1181. opts->colorize = BOOL_FALSE;
  1182. }
  1183. if ((val = faux_ini_find(ini, "Indent"))) {
  1184. unsigned char indent = 0;
  1185. if (faux_conv_atouc(val, &indent, 10))
  1186. opts->indent = indent;
  1187. }
  1188. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1189. if (faux_str_cmp(val, "y") == 0)
  1190. opts->default_keys = BOOL_TRUE;
  1191. else if (faux_str_cmp(val, "n") == 0)
  1192. opts->default_keys = BOOL_FALSE;
  1193. }
  1194. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1195. if (faux_str_cmp(val, "y") == 0)
  1196. opts->show_default_keys = BOOL_TRUE;
  1197. else if (faux_str_cmp(val, "n") == 0)
  1198. opts->show_default_keys = BOOL_FALSE;
  1199. }
  1200. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1201. if (faux_str_cmp(val, "y") == 0)
  1202. opts->hide_passwords = BOOL_TRUE;
  1203. else if (faux_str_cmp(val, "n") == 0)
  1204. opts->hide_passwords = BOOL_FALSE;
  1205. }
  1206. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1207. if (faux_str_cmp(val, "y") == 0)
  1208. opts->enable_nacm = BOOL_TRUE;
  1209. else if (faux_str_cmp(val, "n") == 0)
  1210. opts->enable_nacm = BOOL_FALSE;
  1211. }
  1212. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1213. if (faux_str_cmp(val, "y") == 0)
  1214. opts->oneliners = BOOL_TRUE;
  1215. else if (faux_str_cmp(val, "n") == 0)
  1216. opts->oneliners = BOOL_FALSE;
  1217. }
  1218. faux_ini_free(ini);
  1219. return 0;
  1220. }