shell_tinyxml.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. const char *lock = element->Attribute("lock");
  296. const char *interrupt = element->Attribute("interrupt");
  297. assert(!shell->startup);
  298. assert(view);
  299. /* create a command with NULL help */
  300. cmd = clish_view_new_command(v, "startup", NULL);
  301. clish_command__set_lock(cmd, BOOL_FALSE);
  302. // define the view which this command changes to
  303. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  304. // reference the next view
  305. clish_command__set_view(cmd, next);
  306. // define the view id which this command changes to
  307. if (viewid)
  308. clish_command__set_viewid(cmd, viewid);
  309. if (default_shebang)
  310. clish_shell__set_default_shebang(shell, default_shebang);
  311. if (timeout)
  312. clish_shell__set_timeout(shell, atoi(timeout));
  313. /* lock field */
  314. if (lock && (lub_string_nocasecmp(lock, "false") == 0))
  315. clish_command__set_lock(cmd, BOOL_FALSE);
  316. else
  317. clish_command__set_lock(cmd, BOOL_TRUE);
  318. /* interrupt field */
  319. if (interrupt && (lub_string_nocasecmp(interrupt, "true") == 0))
  320. clish_command__set_interrupt(cmd, BOOL_TRUE);
  321. else
  322. clish_command__set_interrupt(cmd, BOOL_FALSE);
  323. // remember this command
  324. shell->startup = cmd;
  325. process_children(shell, element, cmd);
  326. }
  327. ///////////////////////////////////////
  328. static void
  329. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  330. {
  331. clish_command_t *cmd = NULL;
  332. clish_param_t *p_param = NULL;
  333. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  334. p_param = (clish_param_t *)parent;
  335. else
  336. cmd = (clish_command_t *)parent;
  337. if (cmd || p_param) {
  338. assert((!cmd) || (cmd != shell->startup));
  339. const char *name = element->Attribute("name");
  340. const char *help = element->Attribute("help");
  341. const char *ptype = element->Attribute("ptype");
  342. const char *prefix = element->Attribute("prefix");
  343. const char *defval = element->Attribute("default");
  344. const char *mode = element->Attribute("mode");
  345. const char *optional = element->Attribute("optional");
  346. const char *order = element->Attribute("order");
  347. const char *value = element->Attribute("value");
  348. const char *hidden = element->Attribute("hidden");
  349. const char *test = element->Attribute("test");
  350. const char *completion = element->Attribute("completion");
  351. clish_param_t *param;
  352. clish_ptype_t *tmp = NULL;
  353. // create a command
  354. assert(name);
  355. assert(help);
  356. assert(ptype);
  357. if (ptype && *ptype) {
  358. tmp = clish_shell_find_create_ptype(shell, ptype,
  359. NULL, NULL,
  360. CLISH_PTYPE_REGEXP,
  361. CLISH_PTYPE_NONE);
  362. assert(tmp);
  363. }
  364. param = clish_param_new(name, help, tmp);
  365. /* If prefix is set clish will emulate old optional
  366. * command syntax over newer optional command mechanism.
  367. * It will create nested PARAM.
  368. */
  369. if (prefix) {
  370. const char *ptype_name = "__SUBCOMMAND";
  371. clish_param_t *opt_param = NULL;
  372. /* Create a ptype for prefix-named subcommand that
  373. * will contain the nested optional parameter. The
  374. * name of ptype is hardcoded. It's not good but
  375. * it's only the service ptype.
  376. */
  377. tmp = (clish_ptype_t *)lub_bintree_find(
  378. &shell->ptype_tree, ptype_name);
  379. if (!tmp)
  380. tmp = clish_shell_find_create_ptype(shell,
  381. ptype_name, "Option", "[^\\\\]+",
  382. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  383. assert(tmp);
  384. opt_param = clish_param_new(prefix, help, tmp);
  385. clish_param__set_mode(opt_param,
  386. CLISH_PARAM_SUBCOMMAND);
  387. clish_param__set_optional(opt_param, BOOL_TRUE);
  388. if (test)
  389. clish_param__set_test(opt_param, test);
  390. if (cmd)
  391. // add the parameter to the command
  392. clish_command_insert_param(cmd, opt_param);
  393. if (p_param)
  394. // add the parameter to the param
  395. clish_param_insert_param(p_param, opt_param);
  396. /* Unset cmd and set parent param to opt_param */
  397. cmd = NULL;
  398. p_param = opt_param;
  399. }
  400. if (defval)
  401. clish_param__set_default(param, defval);
  402. if (hidden && (lub_string_nocasecmp(hidden, "true") == 0))
  403. clish_param__set_hidden(param, BOOL_TRUE);
  404. else
  405. clish_param__set_hidden(param, BOOL_FALSE);
  406. if (mode) {
  407. if (!lub_string_nocasecmp(mode, "switch")) {
  408. clish_param__set_mode(param,
  409. CLISH_PARAM_SWITCH);
  410. /* Force hidden attribute */
  411. clish_param__set_hidden(param, BOOL_TRUE);
  412. } else if (!lub_string_nocasecmp(mode, "subcommand"))
  413. clish_param__set_mode(param,
  414. CLISH_PARAM_SUBCOMMAND);
  415. else
  416. clish_param__set_mode(param,
  417. CLISH_PARAM_COMMON);
  418. }
  419. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  420. clish_param__set_optional(param, BOOL_TRUE);
  421. else
  422. clish_param__set_optional(param, BOOL_FALSE);
  423. if (order && (lub_string_nocasecmp(order, "true") == 0))
  424. clish_param__set_order(param, BOOL_TRUE);
  425. else
  426. clish_param__set_order(param, BOOL_FALSE);
  427. if (value) {
  428. clish_param__set_value(param, value);
  429. /* Force mode to subcommand */
  430. clish_param__set_mode(param,
  431. CLISH_PARAM_SUBCOMMAND);
  432. }
  433. if (test && !prefix)
  434. clish_param__set_test(param, test);
  435. if (completion)
  436. clish_param__set_completion(param, completion);
  437. if (cmd)
  438. // add the parameter to the command
  439. clish_command_insert_param(cmd, param);
  440. if (p_param)
  441. // add the parameter to the param
  442. clish_param_insert_param(p_param, param);
  443. process_children(shell, element, param);
  444. }
  445. }
  446. ////////////////////////////////////////
  447. static void
  448. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  449. {
  450. clish_action_t *action = NULL;
  451. TiXmlNode *text = element->FirstChild();
  452. const char *builtin = element->Attribute("builtin");
  453. const char *shebang = element->Attribute("shebang");
  454. if (!lub_string_nocasecmp(element->Parent()->Value(), "VAR"))
  455. action = clish_var__get_action((clish_var_t *)parent);
  456. else
  457. action = clish_command__get_action((clish_command_t *)parent);
  458. assert(action);
  459. if (text) {
  460. assert(TiXmlNode::TEXT == text->Type());
  461. // store the action
  462. clish_action__set_script(action, text->Value());
  463. }
  464. if (builtin)
  465. clish_action__set_builtin(action, builtin);
  466. if (shebang)
  467. clish_action__set_shebang(action, shebang);
  468. }
  469. ////////////////////////////////////////
  470. static void
  471. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  472. {
  473. clish_command_t *cmd = (clish_command_t *) parent;
  474. // read the following text element
  475. TiXmlNode *text = element->FirstChild();
  476. if (cmd && text) {
  477. assert(TiXmlNode::TEXT == text->Type());
  478. // store the action
  479. clish_command__set_detail(cmd, text->Value());
  480. }
  481. }
  482. ///////////////////////////////////////
  483. static void
  484. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  485. {
  486. clish_view_t *v = (clish_view_t *) parent;
  487. clish_nspace_t *nspace = NULL;
  488. const char *view = element->Attribute("ref");
  489. const char *prefix = element->Attribute("prefix");
  490. const char *prefix_help = element->Attribute("prefix_help");
  491. const char *help = element->Attribute("help");
  492. const char *completion = element->Attribute("completion");
  493. const char *context_help = element->Attribute("context_help");
  494. const char *inherit = element->Attribute("inherit");
  495. const char *access = element->Attribute("access");
  496. bool allowed = true;
  497. if (access) {
  498. allowed = false;
  499. if (shell->client_hooks->access_fn)
  500. allowed = shell->client_hooks->access_fn(shell, access)
  501. ? true : false;
  502. }
  503. if (!allowed)
  504. return;
  505. assert(view);
  506. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  507. view, NULL);
  508. assert(ref_view);
  509. /* Don't include itself without prefix */
  510. if ((ref_view == v) && !prefix)
  511. return;
  512. nspace = clish_nspace_new(ref_view);
  513. assert(nspace);
  514. clish_view_insert_nspace(v, nspace);
  515. if (prefix) {
  516. clish_nspace__set_prefix(nspace, prefix);
  517. if (prefix_help)
  518. clish_nspace_create_prefix_cmd(nspace,
  519. "prefix",
  520. prefix_help);
  521. else
  522. clish_nspace_create_prefix_cmd(nspace,
  523. "prefix",
  524. "Prefix for the imported commands.");
  525. }
  526. if (help && (lub_string_nocasecmp(help, "true") == 0))
  527. clish_nspace__set_help(nspace, BOOL_TRUE);
  528. else
  529. clish_nspace__set_help(nspace, BOOL_FALSE);
  530. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  531. clish_nspace__set_completion(nspace, BOOL_FALSE);
  532. else
  533. clish_nspace__set_completion(nspace, BOOL_TRUE);
  534. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  535. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  536. else
  537. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  538. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  539. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  540. else
  541. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  542. }
  543. ////////////////////////////////////////
  544. static void
  545. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  546. {
  547. clish_command_t *cmd = (clish_command_t *)parent;
  548. clish_config_t *config;
  549. if (!cmd)
  550. return;
  551. config = clish_command__get_config(cmd);
  552. // read the following text element
  553. const char *operation = element->Attribute("operation");
  554. const char *priority = element->Attribute("priority");
  555. const char *pattern = element->Attribute("pattern");
  556. const char *file = element->Attribute("file");
  557. const char *splitter = element->Attribute("splitter");
  558. const char *seq = element->Attribute("sequence");
  559. const char *unique = element->Attribute("unique");
  560. const char *depth = element->Attribute("depth");
  561. if (operation && !lub_string_nocasecmp(operation, "unset"))
  562. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  563. else if (operation && !lub_string_nocasecmp(operation, "none"))
  564. clish_config__set_op(config, CLISH_CONFIG_NONE);
  565. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  566. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  567. else {
  568. clish_config__set_op(config, CLISH_CONFIG_SET);
  569. /* The priority if no clearly specified */
  570. clish_config__set_priority(config, 0x7f00);
  571. }
  572. if (priority && (*priority != '\0')) {
  573. long val = 0;
  574. char *endptr;
  575. unsigned short pri;
  576. val = strtol(priority, &endptr, 0);
  577. if (endptr == priority)
  578. pri = 0;
  579. else if (val > 0xffff)
  580. pri = 0xffff;
  581. else if (val < 0)
  582. pri = 0;
  583. else
  584. pri = (unsigned short)val;
  585. clish_config__set_priority(config, pri);
  586. }
  587. if (pattern)
  588. clish_config__set_pattern(config, pattern);
  589. else
  590. clish_config__set_pattern(config, "^${__cmd}");
  591. if (file)
  592. clish_config__set_file(config, file);
  593. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  594. clish_config__set_splitter(config, BOOL_FALSE);
  595. else
  596. clish_config__set_splitter(config, BOOL_TRUE);
  597. if (unique && (lub_string_nocasecmp(unique, "false") == 0))
  598. clish_config__set_unique(config, BOOL_FALSE);
  599. else
  600. clish_config__set_unique(config, BOOL_TRUE);
  601. if (seq)
  602. clish_config__set_seq(config, seq);
  603. else
  604. /* The entries without sequence cannot be non-unique */
  605. clish_config__set_unique(config, BOOL_TRUE);
  606. if (depth)
  607. clish_config__set_depth(config, depth);
  608. }
  609. ///////////////////////////////////////
  610. static void process_var(clish_shell_t * shell, TiXmlElement * element, void *)
  611. {
  612. clish_var_t *var = NULL;
  613. const char *name = element->Attribute("name");
  614. const char *dynamic = element->Attribute("dynamic");
  615. const char *value = element->Attribute("value");
  616. assert(name);
  617. /* Check if this var doesn't already exist */
  618. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  619. if (var) {
  620. printf("DUPLICATE VAR: %s\n", name);
  621. assert(!var);
  622. }
  623. /* Create var instance */
  624. var = clish_var_new(name);
  625. lub_bintree_insert(&shell->var_tree, var);
  626. if (dynamic && (lub_string_nocasecmp(dynamic, "true") == 0))
  627. clish_var__set_dynamic(var, BOOL_TRUE);
  628. if (value)
  629. clish_var__set_value(var, value);
  630. process_children(shell, element, var);
  631. }
  632. ///////////////////////////////////////
  633. static void process_wdog(clish_shell_t *shell,
  634. TiXmlElement *element, void *parent)
  635. {
  636. clish_view_t *v = (clish_view_t *)parent;
  637. clish_command_t *cmd = NULL;
  638. assert(!shell->wdog);
  639. /* create a command with NULL help */
  640. cmd = clish_view_new_command(v, "watchdog", NULL);
  641. clish_command__set_lock(cmd, BOOL_FALSE);
  642. /* Remember this command */
  643. shell->wdog = cmd;
  644. process_children(shell, element, cmd);
  645. }
  646. ///////////////////////////////////////
  647. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  648. {
  649. int ret = -1;
  650. TiXmlDocument doc;
  651. // keep the white space
  652. TiXmlBase::SetCondenseWhiteSpace(false);
  653. if (doc.LoadFile(filename)) {
  654. TiXmlNode *child = 0;
  655. while ((child = doc.IterateChildren(child))) {
  656. process_node(shell, child, NULL);
  657. }
  658. ret = 0;
  659. } else {
  660. printf("Unable to open %s (%s at line %d, col %d)\n",
  661. filename, doc.ErrorDesc(),
  662. doc.ErrorRow(), doc.ErrorCol());
  663. }
  664. return ret;
  665. }
  666. ///////////////////////////////////////