shell_tinyxml.cpp 21 KB

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