shell_tinyxml.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. ////////////////////////////////////////
  2. // shell_tinyxml_read.cpp
  3. //
  4. // This file implements the means to read an XML encoded file and populate the
  5. // CLI tree based on the contents.
  6. ////////////////////////////////////////
  7. extern "C" {
  8. #include "private.h"
  9. #include "lub/string.h"
  10. #include "lub/ctype.h"
  11. }
  12. /*lint +libh(tinyxml/tinyxml.h) Add this to the library file list */
  13. #include "tinyxml/tinyxml.h"
  14. #include <string.h>
  15. #include <assert.h>
  16. typedef void (PROCESS_FN) (clish_shell_t * instance,
  17. TiXmlElement * element, void *parent);
  18. // Define a control block for handling the decode of an XML file
  19. typedef struct clish_xml_cb_s clish_xml_cb_t;
  20. struct clish_xml_cb_s {
  21. const char *element;
  22. PROCESS_FN *handler;
  23. };
  24. // forward declare the handler functions
  25. static PROCESS_FN
  26. process_clish_module,
  27. process_startup,
  28. process_view,
  29. process_command,
  30. process_param,
  31. process_action,
  32. process_ptype,
  33. process_overview,
  34. process_detail,
  35. process_namespace,
  36. process_config,
  37. process_var;
  38. static clish_xml_cb_t xml_elements[] = {
  39. {"CLISH_MODULE", process_clish_module},
  40. {"STARTUP", process_startup},
  41. {"VIEW", process_view},
  42. {"COMMAND", process_command},
  43. {"PARAM", process_param},
  44. {"ACTION", process_action},
  45. {"PTYPE", process_ptype},
  46. {"OVERVIEW", process_overview},
  47. {"DETAIL", process_detail},
  48. {"NAMESPACE", process_namespace},
  49. {"CONFIG", process_config},
  50. {"VAR", process_var},
  51. {NULL, NULL}
  52. };
  53. ///////////////////////////////////////
  54. // This function reads an element from the XML stream and processes it.
  55. ///////////////////////////////////////
  56. static void process_node(clish_shell_t * shell, TiXmlNode * node, void *parent)
  57. {
  58. switch (node->Type()) {
  59. case TiXmlNode::DOCUMENT:
  60. break;
  61. case TiXmlNode::ELEMENT:
  62. clish_xml_cb_t * cb;
  63. for (cb = &xml_elements[0]; cb->element; cb++) {
  64. if (0 == strcmp(node->Value(), cb->element)) {
  65. TiXmlElement *element = (TiXmlElement *) node;
  66. #ifdef DEBUG
  67. fprintf(stderr, "NODE:");
  68. element->Print(stderr, 0);
  69. fprintf(stderr, "\n***\n");
  70. #endif
  71. // process the elements at this level
  72. cb->handler(shell, element, parent);
  73. break;
  74. }
  75. }
  76. break;
  77. case TiXmlNode::COMMENT:
  78. case TiXmlNode::TEXT:
  79. case TiXmlNode::DECLARATION:
  80. case TiXmlNode::TYPECOUNT:
  81. case TiXmlNode::UNKNOWN:
  82. default:
  83. break;
  84. }
  85. }
  86. ///////////////////////////////////////
  87. static void process_children(clish_shell_t * shell,
  88. TiXmlElement * element, void *parent = NULL)
  89. {
  90. for (TiXmlNode * node = element->FirstChild();
  91. node; node = element->IterateChildren(node)) {
  92. // Now deal with all the contained elements
  93. process_node(shell, node, parent);
  94. }
  95. }
  96. ///////////////////////////////////////
  97. static void
  98. process_clish_module(clish_shell_t * shell, TiXmlElement * element, void *)
  99. {
  100. // create the global view
  101. if (!shell->global)
  102. shell->global = clish_shell_find_create_view(shell,
  103. "global", "");
  104. process_children(shell, element, shell->global);
  105. }
  106. ///////////////////////////////////////
  107. static void process_view(clish_shell_t * shell, TiXmlElement * element, void *)
  108. {
  109. clish_view_t *view;
  110. const char *name = element->Attribute("name");
  111. const char *prompt = element->Attribute("prompt");
  112. const char *depth = element->Attribute("depth");
  113. const char *restore = element->Attribute("restore");
  114. const char *access = element->Attribute("access");
  115. bool allowed = true;
  116. /* Check permissions */
  117. if (access) {
  118. allowed = false;
  119. if (shell->client_hooks->access_fn)
  120. allowed = shell->client_hooks->access_fn(shell, access)
  121. ? true : false;
  122. }
  123. if (!allowed)
  124. return;
  125. // re-use a view if it already exists
  126. view = clish_shell_find_create_view(shell, name, prompt);
  127. if (depth && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
  128. unsigned res = atoi(depth);
  129. clish_view__set_depth(view, res);
  130. }
  131. if (restore) {
  132. if (!lub_string_nocasecmp(restore, "depth"))
  133. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  134. else if (!lub_string_nocasecmp(restore, "view"))
  135. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  136. else
  137. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  138. }
  139. process_children(shell, element, view);
  140. }
  141. ///////////////////////////////////////
  142. static void process_ptype(clish_shell_t * shell, TiXmlElement * element, void *)
  143. {
  144. clish_ptype_method_e method;
  145. clish_ptype_preprocess_e preprocess;
  146. clish_ptype_t *ptype;
  147. const char *name = element->Attribute("name");
  148. const char *help = element->Attribute("help");
  149. const char *pattern = element->Attribute("pattern");
  150. const char *method_name = element->Attribute("method");
  151. const char *preprocess_name = element->Attribute("preprocess");
  152. assert(name);
  153. assert(pattern);
  154. method = clish_ptype_method_resolve(method_name);
  155. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  156. ptype = clish_shell_find_create_ptype(shell,
  157. name, help, pattern, method, preprocess);
  158. assert(ptype);
  159. }
  160. ///////////////////////////////////////
  161. static void
  162. process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
  163. {
  164. // read the following text element
  165. TiXmlNode *text = element->FirstChild();
  166. if (text) {
  167. assert(TiXmlNode::TEXT == text->Type());
  168. // set the overview text for this view
  169. assert(NULL == shell->overview);
  170. // store the overview
  171. shell->overview = lub_string_dup(text->Value());
  172. }
  173. }
  174. ////////////////////////////////////////
  175. static void
  176. process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
  177. {
  178. clish_view_t *v = (clish_view_t *) parent;
  179. clish_command_t *cmd = NULL;
  180. bool allowed = true;
  181. clish_command_t *old;
  182. char *alias_name = NULL;
  183. clish_view_t *alias_view = NULL;
  184. const char *access = element->Attribute("access");
  185. const char *name = element->Attribute("name");
  186. const char *help = element->Attribute("help");
  187. const char *view = element->Attribute("view");
  188. const char *viewid = element->Attribute("viewid");
  189. const char *escape_chars = element->Attribute("escape_chars");
  190. const char *args_name = element->Attribute("args");
  191. const char *args_help = element->Attribute("args_help");
  192. const char *lock = element->Attribute("lock");
  193. const char *interrupt = element->Attribute("interrupt");
  194. const char *ref = element->Attribute("ref");
  195. /* Check permissions */
  196. if (access) {
  197. allowed = false;
  198. if (shell->client_hooks->access_fn)
  199. allowed = shell->client_hooks->access_fn(shell, access)
  200. ? true : false;
  201. }
  202. if (!allowed)
  203. return;
  204. old = clish_view_find_command(v, name, BOOL_FALSE);
  205. // check this command doesn't already exist
  206. if (old) {
  207. // flag the duplication then ignore further definition
  208. printf("DUPLICATE COMMAND: %s\n",
  209. clish_command__get_name(old));
  210. return;
  211. }
  212. assert(name);
  213. assert(help);
  214. /* Reference 'ref' field */
  215. if (ref) {
  216. char *saveptr;
  217. const char *delim = "@";
  218. char *view_name = NULL;
  219. char *cmd = NULL;
  220. char *str = lub_string_dup(ref);
  221. cmd = strtok_r(str, delim, &saveptr);
  222. if (!cmd) {
  223. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  224. lub_string_free(str);
  225. return;
  226. }
  227. alias_name = lub_string_dup(cmd);
  228. view_name = strtok_r(NULL, delim, &saveptr);
  229. if (!view_name)
  230. alias_view = v;
  231. else
  232. alias_view = clish_shell_find_create_view(shell,
  233. view_name, NULL);
  234. lub_string_free(str);
  235. }
  236. /* create a command */
  237. cmd = clish_view_new_command(v, name, help);
  238. assert(cmd);
  239. clish_command__set_pview(cmd, v);
  240. /* define some specialist escape characters */
  241. if (escape_chars)
  242. clish_command__set_escape_chars(cmd, escape_chars);
  243. if (args_name) {
  244. /* define a "rest of line" argument */
  245. clish_param_t *param;
  246. clish_ptype_t *tmp = NULL;
  247. assert(args_help);
  248. tmp = clish_shell_find_ptype(shell, "internal_ARGS");
  249. assert(tmp);
  250. param = clish_param_new(args_name, args_help, tmp);
  251. clish_command__set_args(cmd, param);
  252. }
  253. // define the view which this command changes to
  254. if (view) {
  255. clish_view_t *next = clish_shell_find_create_view(shell, view,
  256. NULL);
  257. // reference the next view
  258. clish_command__set_view(cmd, next);
  259. }
  260. // define the view id which this command changes to
  261. if (viewid)
  262. clish_command__set_viewid(cmd, viewid);
  263. /* lock field */
  264. if (lock && (lub_string_nocasecmp(lock, "false") == 0))
  265. clish_command__set_lock(cmd, BOOL_FALSE);
  266. else
  267. clish_command__set_lock(cmd, BOOL_TRUE);
  268. /* interrupt field */
  269. if (interrupt && (lub_string_nocasecmp(interrupt, "true") == 0))
  270. clish_command__set_interrupt(cmd, BOOL_TRUE);
  271. else
  272. clish_command__set_interrupt(cmd, BOOL_FALSE);
  273. /* Set alias */
  274. if (alias_name) {
  275. clish_command__set_alias(cmd, alias_name);
  276. assert(alias_view);
  277. clish_command__set_alias_view(cmd, alias_view);
  278. lub_string_free(alias_name);
  279. }
  280. process_children(shell, element, cmd);
  281. }
  282. ///////////////////////////////////////
  283. static void
  284. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  285. {
  286. clish_view_t *v = (clish_view_t *) parent;
  287. clish_command_t *cmd = NULL;
  288. const char *view = element->Attribute("view");
  289. const char *viewid = element->Attribute("viewid");
  290. const char *default_shebang = element->Attribute("default_shebang");
  291. assert(!shell->startup);
  292. assert(view);
  293. /* create a command with NULL help */
  294. cmd = clish_view_new_command(v, "startup", NULL);
  295. clish_command__set_lock(cmd, BOOL_FALSE);
  296. // define the view which this command changes to
  297. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  298. // reference the next view
  299. clish_command__set_view(cmd, next);
  300. // define the view id which this command changes to
  301. if (viewid)
  302. clish_command__set_viewid(cmd, viewid);
  303. if (default_shebang)
  304. clish_shell__set_default_shebang(shell, default_shebang);
  305. // remember this command
  306. shell->startup = cmd;
  307. process_children(shell, element, cmd);
  308. }
  309. ///////////////////////////////////////
  310. static void
  311. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  312. {
  313. clish_command_t *cmd = NULL;
  314. clish_param_t *p_param = NULL;
  315. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  316. p_param = (clish_param_t *)parent;
  317. else
  318. cmd = (clish_command_t *)parent;
  319. if (cmd || p_param) {
  320. assert((!cmd) || (cmd != shell->startup));
  321. const char *name = element->Attribute("name");
  322. const char *help = element->Attribute("help");
  323. const char *ptype = element->Attribute("ptype");
  324. const char *prefix = element->Attribute("prefix");
  325. const char *defval = element->Attribute("default");
  326. const char *mode = element->Attribute("mode");
  327. const char *optional = element->Attribute("optional");
  328. const char *value = element->Attribute("value");
  329. const char *hidden = element->Attribute("hidden");
  330. const char *test = element->Attribute("test");
  331. const char *completion = element->Attribute("completion");
  332. clish_param_t *param;
  333. clish_ptype_t *tmp = NULL;
  334. // create a command
  335. assert(name);
  336. assert(help);
  337. assert(ptype);
  338. if (ptype && *ptype) {
  339. tmp = clish_shell_find_create_ptype(shell, ptype,
  340. NULL, NULL,
  341. CLISH_PTYPE_REGEXP,
  342. CLISH_PTYPE_NONE);
  343. assert(tmp);
  344. }
  345. param = clish_param_new(name, help, tmp);
  346. /* If prefix is set clish will emulate old optional
  347. * command syntax over newer optional command mechanism.
  348. * It will create nested PARAM.
  349. */
  350. if (prefix) {
  351. const char *ptype_name = "__SUBCOMMAND";
  352. clish_param_t *opt_param = NULL;
  353. /* Create a ptype for prefix-named subcommand that
  354. * will contain the nested optional parameter. The
  355. * name of ptype is hardcoded. It's not good but
  356. * it's only the service ptype.
  357. */
  358. tmp = (clish_ptype_t *)lub_bintree_find(
  359. &shell->ptype_tree, ptype_name);
  360. if (!tmp)
  361. tmp = clish_shell_find_create_ptype(shell,
  362. ptype_name, "Option", "[^\\]+",
  363. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  364. assert(tmp);
  365. opt_param = clish_param_new(prefix, help, tmp);
  366. clish_param__set_mode(opt_param,
  367. CLISH_PARAM_SUBCOMMAND);
  368. clish_param__set_optional(opt_param, BOOL_TRUE);
  369. if (test)
  370. clish_param__set_test(opt_param, test);
  371. if (cmd)
  372. // add the parameter to the command
  373. clish_command_insert_param(cmd, opt_param);
  374. if (p_param)
  375. // add the parameter to the param
  376. clish_param_insert_param(p_param, opt_param);
  377. /* Unset cmd and set parent param to opt_param */
  378. cmd = NULL;
  379. p_param = opt_param;
  380. }
  381. if (defval)
  382. clish_param__set_default(param, defval);
  383. if (hidden && (lub_string_nocasecmp(hidden, "true") == 0))
  384. clish_param__set_hidden(param, BOOL_TRUE);
  385. else
  386. clish_param__set_hidden(param, BOOL_FALSE);
  387. if (mode) {
  388. if (!lub_string_nocasecmp(mode, "switch")) {
  389. clish_param__set_mode(param,
  390. CLISH_PARAM_SWITCH);
  391. /* Force hidden attribute */
  392. clish_param__set_hidden(param, BOOL_TRUE);
  393. } else if (!lub_string_nocasecmp(mode, "subcommand"))
  394. clish_param__set_mode(param,
  395. CLISH_PARAM_SUBCOMMAND);
  396. else
  397. clish_param__set_mode(param,
  398. CLISH_PARAM_COMMON);
  399. }
  400. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  401. clish_param__set_optional(param, BOOL_TRUE);
  402. else
  403. clish_param__set_optional(param, BOOL_FALSE);
  404. if (value) {
  405. clish_param__set_value(param, value);
  406. /* Force mode to subcommand */
  407. clish_param__set_mode(param,
  408. CLISH_PARAM_SUBCOMMAND);
  409. }
  410. if (test && !prefix)
  411. clish_param__set_test(param, test);
  412. if (completion)
  413. clish_param__set_completion(param, completion);
  414. if (cmd)
  415. // add the parameter to the command
  416. clish_command_insert_param(cmd, param);
  417. if (p_param)
  418. // add the parameter to the param
  419. clish_param_insert_param(p_param, param);
  420. process_children(shell, element, param);
  421. }
  422. }
  423. ////////////////////////////////////////
  424. static void
  425. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  426. {
  427. clish_action_t *action = NULL;
  428. TiXmlNode *text = element->FirstChild();
  429. const char *builtin = element->Attribute("builtin");
  430. const char *shebang = element->Attribute("shebang");
  431. if (!lub_string_nocasecmp(element->Parent()->Value(), "VAR"))
  432. action = clish_var__get_action((clish_var_t *)parent);
  433. else
  434. action = clish_command__get_action((clish_command_t *)parent);
  435. assert(action);
  436. if (text) {
  437. assert(TiXmlNode::TEXT == text->Type());
  438. // store the action
  439. clish_action__set_script(action, text->Value());
  440. }
  441. if (builtin)
  442. clish_action__set_builtin(action, builtin);
  443. if (shebang)
  444. clish_action__set_shebang(action, shebang);
  445. }
  446. ////////////////////////////////////////
  447. static void
  448. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  449. {
  450. clish_command_t *cmd = (clish_command_t *) parent;
  451. // read the following text element
  452. TiXmlNode *text = element->FirstChild();
  453. if (cmd && text) {
  454. assert(TiXmlNode::TEXT == text->Type());
  455. // store the action
  456. clish_command__set_detail(cmd, text->Value());
  457. }
  458. }
  459. ///////////////////////////////////////
  460. static void
  461. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  462. {
  463. clish_view_t *v = (clish_view_t *) parent;
  464. clish_nspace_t *nspace = NULL;
  465. const char *view = element->Attribute("ref");
  466. const char *prefix = element->Attribute("prefix");
  467. const char *prefix_help = element->Attribute("prefix_help");
  468. const char *help = element->Attribute("help");
  469. const char *completion = element->Attribute("completion");
  470. const char *context_help = element->Attribute("context_help");
  471. const char *inherit = element->Attribute("inherit");
  472. const char *access = element->Attribute("access");
  473. bool allowed = true;
  474. if (access) {
  475. allowed = false;
  476. if (shell->client_hooks->access_fn)
  477. allowed = shell->client_hooks->access_fn(shell, access)
  478. ? true : false;
  479. }
  480. if (!allowed)
  481. return;
  482. assert(view);
  483. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  484. view, NULL);
  485. assert(ref_view);
  486. nspace = clish_nspace_new(ref_view);
  487. assert(nspace);
  488. clish_view_insert_nspace(v, nspace);
  489. if (prefix) {
  490. clish_nspace__set_prefix(nspace, prefix);
  491. if (prefix_help)
  492. clish_nspace_create_prefix_cmd(nspace,
  493. "prefix",
  494. prefix_help);
  495. else
  496. clish_nspace_create_prefix_cmd(nspace,
  497. "prefix",
  498. "Prefix for the imported commands.");
  499. }
  500. if (help && (lub_string_nocasecmp(help, "true") == 0))
  501. clish_nspace__set_help(nspace, BOOL_TRUE);
  502. else
  503. clish_nspace__set_help(nspace, BOOL_FALSE);
  504. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  505. clish_nspace__set_completion(nspace, BOOL_FALSE);
  506. else
  507. clish_nspace__set_completion(nspace, BOOL_TRUE);
  508. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  509. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  510. else
  511. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  512. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  513. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  514. else
  515. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  516. }
  517. ////////////////////////////////////////
  518. static void
  519. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  520. {
  521. clish_command_t *cmd = (clish_command_t *)parent;
  522. clish_config_t *config;
  523. if (!cmd)
  524. return;
  525. config = clish_command__get_config(cmd);
  526. // read the following text element
  527. const char *operation = element->Attribute("operation");
  528. const char *priority = element->Attribute("priority");
  529. const char *pattern = element->Attribute("pattern");
  530. const char *file = element->Attribute("file");
  531. const char *splitter = element->Attribute("splitter");
  532. const char *seq = element->Attribute("sequence");
  533. const char *unique = element->Attribute("unique");
  534. const char *depth = element->Attribute("depth");
  535. if (operation && !lub_string_nocasecmp(operation, "unset"))
  536. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  537. else if (operation && !lub_string_nocasecmp(operation, "none"))
  538. clish_config__set_op(config, CLISH_CONFIG_NONE);
  539. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  540. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  541. else {
  542. clish_config__set_op(config, CLISH_CONFIG_SET);
  543. /* The priority if no clearly specified */
  544. clish_config__set_priority(config, 0x7f00);
  545. }
  546. if (priority && (*priority != '\0')) {
  547. long val = 0;
  548. char *endptr;
  549. unsigned short pri;
  550. val = strtol(priority, &endptr, 0);
  551. if (endptr == priority)
  552. pri = 0;
  553. else if (val > 0xffff)
  554. pri = 0xffff;
  555. else if (val < 0)
  556. pri = 0;
  557. else
  558. pri = (unsigned short)val;
  559. clish_config__set_priority(config, pri);
  560. }
  561. if (pattern)
  562. clish_config__set_pattern(config, pattern);
  563. else
  564. clish_config__set_pattern(config, "^${__cmd}");
  565. if (file)
  566. clish_config__set_file(config, file);
  567. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  568. clish_config__set_splitter(config, BOOL_FALSE);
  569. else
  570. clish_config__set_splitter(config, BOOL_TRUE);
  571. if (unique && (lub_string_nocasecmp(unique, "false") == 0))
  572. clish_config__set_unique(config, BOOL_FALSE);
  573. else
  574. clish_config__set_unique(config, BOOL_TRUE);
  575. if (seq)
  576. clish_config__set_seq(config, seq);
  577. else
  578. /* The entries without sequence cannot be non-unique */
  579. clish_config__set_unique(config, BOOL_TRUE);
  580. if (depth)
  581. clish_config__set_depth(config, depth);
  582. }
  583. ///////////////////////////////////////
  584. static void process_var(clish_shell_t * shell, TiXmlElement * element, void *)
  585. {
  586. clish_var_t *var = NULL;
  587. const char *name = element->Attribute("name");
  588. const char *dynamic = element->Attribute("dynamic");
  589. const char *value = element->Attribute("value");
  590. assert(name);
  591. /* Check if this var doesn't already exist */
  592. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  593. if (var) {
  594. printf("DUPLICATE VAR: %s\n", name);
  595. assert(!var);
  596. }
  597. /* Create var instance */
  598. var = clish_var_new(name);
  599. lub_bintree_insert(&shell->var_tree, var);
  600. if (dynamic && (lub_string_nocasecmp(dynamic, "true") == 0))
  601. clish_var__set_dynamic(var, BOOL_TRUE);
  602. if (value)
  603. clish_var__set_value(var, value);
  604. process_children(shell, element, var);
  605. }
  606. ///////////////////////////////////////
  607. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  608. {
  609. int ret = -1;
  610. TiXmlDocument doc;
  611. // keep the white space
  612. TiXmlBase::SetCondenseWhiteSpace(false);
  613. if (doc.LoadFile(filename)) {
  614. TiXmlNode *child = 0;
  615. while ((child = doc.IterateChildren(child))) {
  616. process_node(shell, child, NULL);
  617. }
  618. ret = 0;
  619. } else {
  620. printf("Unable to open %s (%s at line %d, col %d)\n",
  621. filename, doc.ErrorDesc(),
  622. doc.ErrorRow(), doc.ErrorCol());
  623. }
  624. return ret;
  625. }
  626. ///////////////////////////////////////