shell_tinyxml_read.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, process_detail, process_namespace, process_config;
  34. static clish_xml_cb_t xml_elements[] = {
  35. {"CLISH_MODULE", process_clish_module},
  36. {"STARTUP", process_startup},
  37. {"VIEW", process_view},
  38. {"COMMAND", process_command},
  39. {"PARAM", process_param},
  40. {"ACTION", process_action},
  41. {"PTYPE", process_ptype},
  42. {"OVERVIEW", process_overview},
  43. {"DETAIL", process_detail},
  44. {"NAMESPACE", process_namespace},
  45. {"CONFIG", process_config},
  46. {NULL, NULL}
  47. };
  48. ///////////////////////////////////////
  49. // This function reads an element from the XML stream and processes it.
  50. ///////////////////////////////////////
  51. static void process_node(clish_shell_t * shell, TiXmlNode * node, void *parent)
  52. {
  53. switch (node->Type()) {
  54. case TiXmlNode::DOCUMENT:
  55. break;
  56. case TiXmlNode::ELEMENT:
  57. clish_xml_cb_t * cb;
  58. for (cb = &xml_elements[0]; cb->element; cb++) {
  59. if (0 == strcmp(node->Value(), cb->element)) {
  60. TiXmlElement *element = (TiXmlElement *) node;
  61. #ifdef DEBUG
  62. fprintf(stderr, "NODE:");
  63. element->Print(stderr, 0);
  64. fprintf(stderr, "\n***\n");
  65. #endif
  66. // process the elements at this level
  67. cb->handler(shell, element, parent);
  68. break;
  69. }
  70. }
  71. break;
  72. case TiXmlNode::COMMENT:
  73. case TiXmlNode::TEXT:
  74. case TiXmlNode::DECLARATION:
  75. case TiXmlNode::TYPECOUNT:
  76. case TiXmlNode::UNKNOWN:
  77. default:
  78. break;
  79. }
  80. }
  81. ///////////////////////////////////////
  82. static void
  83. process_children(clish_shell_t * shell,
  84. TiXmlElement * element, void *parent = NULL)
  85. {
  86. for (TiXmlNode * node = element->FirstChild();
  87. NULL != node; node = element->IterateChildren(node)) {
  88. // Now deal with all the contained elements
  89. process_node(shell, node, parent);
  90. }
  91. }
  92. ///////////////////////////////////////
  93. static void
  94. process_clish_module(clish_shell_t * shell, TiXmlElement * element, void *)
  95. {
  96. // create the global view
  97. if (NULL == shell->global) {
  98. shell->global =
  99. clish_shell_find_create_view(shell, "global", "");
  100. }
  101. process_children(shell, element, shell->global);
  102. }
  103. ///////////////////////////////////////
  104. static void process_view(clish_shell_t * shell, TiXmlElement * element, void *)
  105. {
  106. clish_view_t *view;
  107. const char *name = element->Attribute("name");
  108. const char *prompt = element->Attribute("prompt");
  109. const char *depth = element->Attribute("depth");
  110. const char *restore = element->Attribute("restore");
  111. // re-use a view if it already exists
  112. view = clish_shell_find_create_view(shell, name, prompt);
  113. if ((depth != NULL) && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
  114. unsigned res = atoi(depth);
  115. clish_view__set_depth(view, res);
  116. }
  117. if (restore) {
  118. if (!lub_string_nocasecmp(restore, "depth"))
  119. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  120. else if (!lub_string_nocasecmp(restore, "view"))
  121. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  122. else
  123. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  124. }
  125. process_children(shell, element, view);
  126. }
  127. ///////////////////////////////////////
  128. static void process_ptype(clish_shell_t * shell, TiXmlElement * element, void *)
  129. {
  130. clish_ptype_method_e method;
  131. clish_ptype_preprocess_e preprocess;
  132. clish_ptype_t *ptype;
  133. const char *name = element->Attribute("name");
  134. const char *help = element->Attribute("help");
  135. const char *pattern = element->Attribute("pattern");
  136. const char *method_name = element->Attribute("method");
  137. const char *preprocess_name = element->Attribute("preprocess");
  138. assert(name);
  139. assert(pattern);
  140. method = clish_ptype_method_resolve(method_name);
  141. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  142. ptype = clish_shell_find_create_ptype(shell,
  143. name,
  144. help,
  145. pattern, method, preprocess);
  146. assert(ptype);
  147. }
  148. ///////////////////////////////////////
  149. static void
  150. process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
  151. {
  152. // read the following text element
  153. TiXmlNode *text = element->FirstChild();
  154. if (NULL != text) {
  155. assert(TiXmlNode::TEXT == text->Type());
  156. // set the overview text for this view
  157. assert(NULL == shell->overview);
  158. // store the overview
  159. shell->overview = lub_string_dup(text->Value());
  160. }
  161. }
  162. ////////////////////////////////////////
  163. static void
  164. process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
  165. {
  166. clish_view_t *v = (clish_view_t *) parent;
  167. clish_command_t *cmd = NULL;
  168. const char *access = element->Attribute("access");
  169. bool allowed = true;
  170. if (NULL != access) {
  171. allowed = false; // err on the side of caution
  172. if (shell->client_hooks->access_fn) {
  173. // get the client to authenticate
  174. allowed =
  175. shell->client_hooks->access_fn(shell,
  176. access) ? true :
  177. false;
  178. }
  179. }
  180. if (allowed) {
  181. const char *name = element->Attribute("name");
  182. const char *help = element->Attribute("help");
  183. const char *view = element->Attribute("view");
  184. const char *viewid = element->Attribute("viewid");
  185. const char *escape_chars = element->Attribute("escape_chars");
  186. const char *args_name = element->Attribute("args");
  187. const char *args_help = element->Attribute("args_help");
  188. clish_command_t *old = clish_view_find_command(v, name, BOOL_FALSE);
  189. // check this command doesn't already exist
  190. if (NULL != old) {
  191. // flag the duplication then ignore further definition
  192. printf("DUPLICATE COMMAND: %s\n",
  193. clish_command__get_name(old));
  194. } else {
  195. assert(name);
  196. assert(help);
  197. /* create a command */
  198. cmd = clish_view_new_command(v, name, help);
  199. assert(cmd);
  200. clish_command__set_pview(cmd, v);
  201. if (NULL != escape_chars) {
  202. /* define some specialist escape characters */
  203. clish_command__set_escape_chars(cmd,
  204. escape_chars);
  205. }
  206. if (NULL != args_name) {
  207. /* define a "rest of line" argument */
  208. clish_param_t *param;
  209. clish_ptype_t *tmp = NULL;
  210. assert(NULL != args_help);
  211. tmp = clish_shell_find_create_ptype(shell,
  212. "internal_ARGS",
  213. "Arguments", "[^\\]+",
  214. CLISH_PTYPE_REGEXP,
  215. CLISH_PTYPE_NONE);
  216. assert(tmp);
  217. param =
  218. clish_param_new(args_name, args_help, tmp);
  219. clish_command__set_args(cmd, param);
  220. }
  221. // define the view which this command changes to
  222. if (NULL != view) {
  223. clish_view_t *next =
  224. clish_shell_find_create_view(shell, view,
  225. NULL);
  226. // reference the next view
  227. clish_command__set_view(cmd, next);
  228. }
  229. // define the view id which this command changes to
  230. if (NULL != viewid) {
  231. clish_command__set_viewid(cmd, viewid);
  232. }
  233. process_children(shell, element, cmd);
  234. }
  235. }
  236. }
  237. ///////////////////////////////////////
  238. static void
  239. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  240. {
  241. clish_view_t *v = (clish_view_t *) parent;
  242. clish_command_t *cmd = NULL;
  243. const char *view = element->Attribute("view");
  244. const char *viewid = element->Attribute("viewid");
  245. const char *env_view = getenv("CLISH_VIEW");
  246. const char *env_viewid = getenv("CLISH_VIEWID");
  247. /* Redefine startup view if environment is set. */
  248. if (env_view)
  249. view = env_view;
  250. if (env_viewid)
  251. viewid = env_viewid;
  252. assert(NULL == shell->startup);
  253. assert(view);
  254. /* create a command with NULL help */
  255. cmd = clish_view_new_command(v, "startup", NULL);
  256. // define the view which this command changes to
  257. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  258. // reference the next view
  259. clish_command__set_view(cmd, next);
  260. // define the view id which this command changes to
  261. if (NULL != viewid) {
  262. clish_command__set_viewid(cmd, viewid);
  263. }
  264. // remember this command
  265. shell->startup = cmd;
  266. process_children(shell, element, cmd);
  267. }
  268. ///////////////////////////////////////
  269. static void
  270. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  271. {
  272. clish_command_t *cmd = NULL;
  273. clish_param_t *p_param = NULL;
  274. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  275. p_param = (clish_param_t *) parent;
  276. else
  277. cmd = (clish_command_t *) parent;
  278. if ((NULL != cmd) || (NULL != p_param)) {
  279. assert((!cmd) || (cmd != shell->startup));
  280. const char *name = element->Attribute("name");
  281. const char *help = element->Attribute("help");
  282. const char *ptype = element->Attribute("ptype");
  283. const char *prefix = element->Attribute("prefix");
  284. const char *defval = element->Attribute("default");
  285. const char *mode = element->Attribute("mode");
  286. const char *optional = element->Attribute("optional");
  287. const char *value = element->Attribute("value");
  288. clish_param_t *param;
  289. clish_ptype_t *tmp = NULL;
  290. // create a command
  291. assert(name);
  292. assert(help);
  293. assert(ptype);
  294. if (ptype && *ptype) {
  295. tmp = clish_shell_find_create_ptype(shell, ptype,
  296. NULL, NULL,
  297. CLISH_PTYPE_REGEXP,
  298. CLISH_PTYPE_NONE);
  299. assert(tmp);
  300. }
  301. param = clish_param_new(name, help, tmp);
  302. /* If prefix is set clish will emulate old optional
  303. * command syntax over newer optional command mechanism.
  304. * It will create nested PARAM.
  305. */
  306. if (NULL != prefix) {
  307. const char *ptype_name = "internal_SUBCOMMAND";
  308. clish_param_t *opt_param = NULL;
  309. /* Create a ptype for prefix-named subcommand that
  310. * will contain the nested optional parameter. The
  311. * name of ptype is hardcoded. It's not good but
  312. * it's only the service ptype.
  313. */
  314. tmp = (clish_ptype_t *)lub_bintree_find(
  315. &shell->ptype_tree, ptype_name);
  316. if (!tmp)
  317. tmp = clish_shell_find_create_ptype(shell,
  318. ptype_name, "Option", "[^\\]+",
  319. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  320. assert(tmp);
  321. opt_param = clish_param_new(prefix, help, tmp);
  322. clish_param__set_mode(opt_param,
  323. CLISH_PARAM_SUBCOMMAND);
  324. clish_param__set_optional(opt_param, BOOL_TRUE);
  325. if (cmd)
  326. // add the parameter to the command
  327. clish_command_insert_param(cmd, opt_param);
  328. if (p_param)
  329. // add the parameter to the param
  330. clish_param_insert_param(p_param, opt_param);
  331. /* Unset cmd and set parent param to opt_param */
  332. cmd = NULL;
  333. p_param = opt_param;
  334. }
  335. if (NULL != defval) {
  336. clish_param__set_default(param, defval);
  337. }
  338. if (NULL != mode) {
  339. if (!lub_string_nocasecmp(mode, "switch"))
  340. clish_param__set_mode(param,
  341. CLISH_PARAM_SWITCH);
  342. else if (!lub_string_nocasecmp(mode, "subcommand"))
  343. clish_param__set_mode(param,
  344. CLISH_PARAM_SUBCOMMAND);
  345. else
  346. clish_param__set_mode(param,
  347. CLISH_PARAM_COMMON);
  348. }
  349. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  350. clish_param__set_optional(param, BOOL_TRUE);
  351. else
  352. clish_param__set_optional(param, BOOL_FALSE);
  353. if (NULL != value) {
  354. clish_param__set_value(param, value);
  355. /* Force mode to subcommand */
  356. clish_param__set_mode(param,
  357. CLISH_PARAM_SUBCOMMAND);
  358. }
  359. if (cmd)
  360. // add the parameter to the command
  361. clish_command_insert_param(cmd, param);
  362. if (p_param)
  363. // add the parameter to the param
  364. clish_param_insert_param(p_param, param);
  365. process_children(shell, element, param);
  366. }
  367. }
  368. ////////////////////////////////////////
  369. static void
  370. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  371. {
  372. clish_command_t *cmd = (clish_command_t *) parent;
  373. if (NULL != cmd) {
  374. // read the following text element
  375. TiXmlNode *text = element->FirstChild();
  376. const char *builtin = element->Attribute("builtin");
  377. if (NULL != text) {
  378. assert(TiXmlNode::TEXT == text->Type());
  379. // store the action
  380. clish_command__set_action(cmd, text->Value());
  381. }
  382. if (NULL != builtin) {
  383. // store the action
  384. clish_command__set_builtin(cmd, builtin);
  385. }
  386. }
  387. }
  388. ////////////////////////////////////////
  389. static void
  390. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  391. {
  392. clish_command_t *cmd = (clish_command_t *) parent;
  393. // read the following text element
  394. TiXmlNode *text = element->FirstChild();
  395. if ((NULL != cmd) && (NULL != text)) {
  396. assert(TiXmlNode::TEXT == text->Type());
  397. // store the action
  398. clish_command__set_detail(cmd, text->Value());
  399. }
  400. }
  401. ///////////////////////////////////////
  402. static void
  403. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  404. {
  405. clish_view_t *v = (clish_view_t *) parent;
  406. clish_nspace_t *nspace = NULL;
  407. const char *view = element->Attribute("ref");
  408. const char *prefix = element->Attribute("prefix");
  409. const char *help = element->Attribute("help");
  410. const char *completion = element->Attribute("completion");
  411. const char *context_help = element->Attribute("context_help");
  412. const char *inherit = element->Attribute("inherit");
  413. assert(view);
  414. clish_view_t *ref_view =
  415. clish_shell_find_create_view(shell, view, NULL);
  416. assert(ref_view);
  417. nspace = clish_nspace_new(ref_view);
  418. assert(nspace);
  419. clish_view_insert_nspace(v, nspace);
  420. if (NULL != prefix) {
  421. clish_nspace__set_prefix(nspace, prefix);
  422. }
  423. if (help && (lub_string_nocasecmp(help, "true") == 0))
  424. clish_nspace__set_help(nspace, BOOL_TRUE);
  425. else
  426. clish_nspace__set_help(nspace, BOOL_FALSE);
  427. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  428. clish_nspace__set_completion(nspace, BOOL_FALSE);
  429. else
  430. clish_nspace__set_completion(nspace, BOOL_TRUE);
  431. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  432. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  433. else
  434. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  435. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  436. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  437. else
  438. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  439. }
  440. ////////////////////////////////////////
  441. static void
  442. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  443. {
  444. clish_command_t *cmd = (clish_command_t *) parent;
  445. if (NULL == cmd)
  446. return;
  447. // read the following text element
  448. const char *operation = element->Attribute("operation");
  449. const char *priority = element->Attribute("priority");
  450. const char *pattern = element->Attribute("pattern");
  451. const char *file = element->Attribute("file");
  452. const char *splitter = element->Attribute("splitter");
  453. const char *seq = element->Attribute("sequence");
  454. const char *seq_num = element->Attribute("sequence_num");
  455. if (operation && !lub_string_nocasecmp(operation, "unset"))
  456. clish_command__set_cfg_op(cmd, CLISH_CONFIG_UNSET);
  457. else if (operation && !lub_string_nocasecmp(operation, "none"))
  458. clish_command__set_cfg_op(cmd, CLISH_CONFIG_NONE);
  459. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  460. clish_command__set_cfg_op(cmd, CLISH_CONFIG_DUMP);
  461. else if (operation && !lub_string_nocasecmp(operation, "copy"))
  462. clish_command__set_cfg_op(cmd, CLISH_CONFIG_COPY);
  463. else
  464. clish_command__set_cfg_op(cmd, CLISH_CONFIG_SET);
  465. if ((priority != NULL) && (*priority != '\0')) {
  466. long val = 0;
  467. char *endptr;
  468. unsigned short pri;
  469. val = strtol(priority, &endptr, 0);
  470. if (endptr == priority)
  471. pri = 0;
  472. else if (val > 0xffff)
  473. pri = 0xffff;
  474. else if (val < 0)
  475. pri = 0;
  476. else
  477. pri = (unsigned short)val;
  478. clish_command__set_priority(cmd, pri);
  479. }
  480. if (pattern != NULL)
  481. clish_command__set_pattern(cmd, pattern);
  482. else
  483. clish_command__set_pattern(cmd, "^${__cmd}");
  484. if (file != NULL)
  485. clish_command__set_file(cmd, file);
  486. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  487. clish_command__set_splitter(cmd, BOOL_FALSE);
  488. else
  489. clish_command__set_splitter(cmd, BOOL_TRUE);
  490. if (seq && (lub_string_nocasecmp(seq, "true") == 0))
  491. clish_command__set_seq(cmd, BOOL_TRUE);
  492. else
  493. clish_command__set_seq(cmd, BOOL_FALSE);
  494. if (seq_num && (*seq_num != '\0')) {
  495. clish_command__set_seq(cmd, BOOL_TRUE);
  496. clish_command__set_seq_num(cmd, seq_num);
  497. }
  498. }
  499. ///////////////////////////////////////
  500. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  501. {
  502. int ret = -1;
  503. TiXmlDocument doc;
  504. // keep the white space
  505. TiXmlBase::SetCondenseWhiteSpace(false);
  506. if (doc.LoadFile(filename)) {
  507. TiXmlNode *child = 0;
  508. while ((child = doc.IterateChildren(child))) {
  509. process_node(shell, child, NULL);
  510. }
  511. ret = 0;
  512. } else {
  513. printf("Unable to open %s\n", filename);
  514. }
  515. return ret;
  516. }
  517. ///////////////////////////////////////