pline.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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,
  1061. pt_e enabled_types, bool_t existing_nodes_only)
  1062. {
  1063. faux_list_node_t *iter = NULL;
  1064. pcompl_t *pcompl = NULL;
  1065. sr_datastore_t current_ds = SRP_REPO_EDIT;
  1066. iter = faux_list_head(pline->compls);
  1067. while ((pcompl = (pcompl_t *)faux_list_each(&iter))) {
  1068. struct lysc_type *type = NULL;
  1069. const struct lysc_node *node = pcompl->node;
  1070. if (!(pcompl->pat & enabled_types))
  1071. continue;
  1072. if (pcompl->xpath && !help) {
  1073. sr_val_t *vals = NULL;
  1074. size_t val_num = 0;
  1075. size_t i = 0;
  1076. //printf("%s\n", pcompl->xpath);
  1077. // Switch to necessary DS
  1078. if (current_ds != pcompl->xpath_ds) {
  1079. sr_session_switch_ds(pline->sess, pcompl->xpath_ds);
  1080. current_ds = pcompl->xpath_ds;
  1081. }
  1082. sr_get_items(pline->sess, pcompl->xpath,
  1083. 0, 0, &vals, &val_num);
  1084. for (i = 0; i < val_num; i++) {
  1085. char *tmp = sr_val_to_str(&vals[i]);
  1086. if (!tmp)
  1087. continue;
  1088. printf("%s\n", tmp);
  1089. free(tmp);
  1090. }
  1091. sr_free_values(vals, val_num);
  1092. }
  1093. if (!node)
  1094. continue;
  1095. if (existing_nodes_only)
  1096. continue;
  1097. // Node
  1098. if (PCOMPL_NODE == pcompl->type) {
  1099. printf("%s\n", node->name);
  1100. if (help) {
  1101. if (!node->dsc) {
  1102. printf("%s\n", node->name);
  1103. } else {
  1104. char *dsc = faux_str_getline(node->dsc,
  1105. NULL);
  1106. printf("%s\n", dsc);
  1107. faux_str_free(dsc);
  1108. }
  1109. }
  1110. continue;
  1111. }
  1112. // Type
  1113. if (node->nodetype & LYS_LEAF)
  1114. type = ((struct lysc_node_leaf *)node)->type;
  1115. else if (node->nodetype & LYS_LEAFLIST)
  1116. type = ((struct lysc_node_leaflist *)node)->type;
  1117. else
  1118. continue;
  1119. if (help)
  1120. pline_print_type_help(node, type);
  1121. else
  1122. pline_print_type_completions(type);
  1123. }
  1124. // Restore default DS
  1125. if (current_ds != SRP_REPO_EDIT)
  1126. sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
  1127. }
  1128. void pline_opts_init(pline_opts_t *opts)
  1129. {
  1130. opts->begin_bracket = '{';
  1131. opts->end_bracket = '}';
  1132. opts->show_brackets = BOOL_TRUE;
  1133. opts->show_semicolons = BOOL_TRUE;
  1134. opts->first_key_w_stmt = BOOL_FALSE;
  1135. opts->keys_w_stmt = BOOL_TRUE;
  1136. opts->colorize = BOOL_TRUE;
  1137. opts->indent = 2;
  1138. opts->default_keys = BOOL_FALSE;
  1139. opts->show_default_keys = BOOL_FALSE;
  1140. opts->hide_passwords = BOOL_TRUE;
  1141. opts->enable_nacm = BOOL_FALSE;
  1142. opts->oneliners = BOOL_TRUE;
  1143. }
  1144. static int pline_opts_parse_ini(const faux_ini_t *ini, pline_opts_t *opts)
  1145. {
  1146. const char *val = NULL;
  1147. if (!opts)
  1148. return -1;
  1149. if (!ini)
  1150. return 0; // Nothing to parse
  1151. if ((val = faux_ini_find(ini, "ShowBrackets"))) {
  1152. if (faux_str_cmp(val, "y") == 0)
  1153. opts->show_brackets = BOOL_TRUE;
  1154. else if (faux_str_cmp(val, "n") == 0)
  1155. opts->show_brackets = BOOL_FALSE;
  1156. }
  1157. if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
  1158. if (faux_str_cmp(val, "y") == 0)
  1159. opts->show_semicolons = BOOL_TRUE;
  1160. else if (faux_str_cmp(val, "n") == 0)
  1161. opts->show_semicolons = BOOL_FALSE;
  1162. }
  1163. if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
  1164. if (faux_str_cmp(val, "y") == 0)
  1165. opts->first_key_w_stmt = BOOL_TRUE;
  1166. else if (faux_str_cmp(val, "n") == 0)
  1167. opts->first_key_w_stmt = BOOL_FALSE;
  1168. }
  1169. if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
  1170. if (faux_str_cmp(val, "y") == 0)
  1171. opts->keys_w_stmt = BOOL_TRUE;
  1172. else if (faux_str_cmp(val, "n") == 0)
  1173. opts->keys_w_stmt = BOOL_FALSE;
  1174. }
  1175. if ((val = faux_ini_find(ini, "Colorize"))) {
  1176. if (faux_str_cmp(val, "y") == 0)
  1177. opts->colorize = BOOL_TRUE;
  1178. else if (faux_str_cmp(val, "n") == 0)
  1179. opts->colorize = BOOL_FALSE;
  1180. }
  1181. if ((val = faux_ini_find(ini, "Indent"))) {
  1182. unsigned char indent = 0;
  1183. if (faux_conv_atouc(val, &indent, 10))
  1184. opts->indent = indent;
  1185. }
  1186. if ((val = faux_ini_find(ini, "DefaultKeys"))) {
  1187. if (faux_str_cmp(val, "y") == 0)
  1188. opts->default_keys = BOOL_TRUE;
  1189. else if (faux_str_cmp(val, "n") == 0)
  1190. opts->default_keys = BOOL_FALSE;
  1191. }
  1192. if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
  1193. if (faux_str_cmp(val, "y") == 0)
  1194. opts->show_default_keys = BOOL_TRUE;
  1195. else if (faux_str_cmp(val, "n") == 0)
  1196. opts->show_default_keys = BOOL_FALSE;
  1197. }
  1198. if ((val = faux_ini_find(ini, "HidePasswords"))) {
  1199. if (faux_str_cmp(val, "y") == 0)
  1200. opts->hide_passwords = BOOL_TRUE;
  1201. else if (faux_str_cmp(val, "n") == 0)
  1202. opts->hide_passwords = BOOL_FALSE;
  1203. }
  1204. if ((val = faux_ini_find(ini, "EnableNACM"))) {
  1205. if (faux_str_cmp(val, "y") == 0)
  1206. opts->enable_nacm = BOOL_TRUE;
  1207. else if (faux_str_cmp(val, "n") == 0)
  1208. opts->enable_nacm = BOOL_FALSE;
  1209. }
  1210. if ((val = faux_ini_find(ini, "Oneliners"))) {
  1211. if (faux_str_cmp(val, "y") == 0)
  1212. opts->oneliners = BOOL_TRUE;
  1213. else if (faux_str_cmp(val, "n") == 0)
  1214. opts->oneliners = BOOL_FALSE;
  1215. }
  1216. return 0;
  1217. }
  1218. int pline_opts_parse(const char *conf, pline_opts_t *opts)
  1219. {
  1220. faux_ini_t *ini = NULL;
  1221. int rc = -1;
  1222. if (!opts)
  1223. return -1;
  1224. if (!conf)
  1225. return 0; // Use defaults
  1226. ini = faux_ini_new();
  1227. if (!faux_ini_parse_str(ini, conf)) {
  1228. faux_ini_free(ini);
  1229. return -1;
  1230. }
  1231. rc = pline_opts_parse_ini(ini, opts);
  1232. faux_ini_free(ini);
  1233. return rc;
  1234. }
  1235. int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts)
  1236. {
  1237. faux_ini_t *ini = NULL;
  1238. int rc = -1;
  1239. if (!opts)
  1240. return -1;
  1241. if (!conf_name)
  1242. return 0; // Use defaults
  1243. ini = faux_ini_new();
  1244. if (!faux_ini_parse_file(ini, conf_name)) {
  1245. faux_ini_free(ini);
  1246. return -1;
  1247. }
  1248. rc = pline_opts_parse_ini(ini, opts);
  1249. faux_ini_free(ini);
  1250. return rc;
  1251. }