shell_tinyxml.cpp 22 KB

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