shell_xml.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * ------------------------------------------------------
  3. * shell_xml.c
  4. *
  5. * This file implements the means to read an XML encoded file and populate the
  6. * CLI tree based on the contents.
  7. * ------------------------------------------------------
  8. */
  9. #include "private.h"
  10. #include "xmlapi.h"
  11. #include "lub/string.h"
  12. #include "lub/ctype.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <errno.h>
  17. typedef void (PROCESS_FN) (clish_shell_t * instance,
  18. clish_xmlnode_t * 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. * ------------------------------------------------------
  58. * This function reads an element from the XML stream and processes it.
  59. * ------------------------------------------------------
  60. */
  61. static void process_node(clish_shell_t * shell, clish_xmlnode_t * node, void *parent)
  62. {
  63. switch (clish_xmlnode_get_type(node)) {
  64. case CLISH_XMLNODE_ELM: {
  65. clish_xml_cb_t * cb;
  66. char name[128];
  67. unsigned int namelen = sizeof(name);
  68. if (clish_xmlnode_get_name(node, name, &namelen) == 0) {
  69. for (cb = &xml_elements[0]; cb->element; cb++) {
  70. if (0 == strcmp(name, cb->element)) {
  71. #ifdef DEBUG
  72. fprintf(stderr, "NODE:");
  73. clish_xmlnode_print(node, stderr);
  74. fprintf(stderr, "\n");
  75. #endif
  76. /* process the elements at this level */
  77. cb->handler(shell, node, parent);
  78. break;
  79. }
  80. }
  81. }
  82. break;
  83. }
  84. case CLISH_XMLNODE_DOC:
  85. case CLISH_XMLNODE_TEXT:
  86. case CLISH_XMLNODE_ATTR:
  87. case CLISH_XMLNODE_PI:
  88. case CLISH_XMLNODE_COMMENT:
  89. case CLISH_XMLNODE_DECL:
  90. case CLISH_XMLNODE_UNKNOWN:
  91. default:
  92. break;
  93. }
  94. }
  95. /* ------------------------------------------------------ */
  96. static void process_children(clish_shell_t * shell,
  97. clish_xmlnode_t * element, void *parent)
  98. {
  99. clish_xmlnode_t *node = NULL;
  100. while ((node = clish_xmlnode_next_child(element, node)) != NULL) {
  101. /* Now deal with all the contained elements */
  102. process_node(shell, node, parent);
  103. }
  104. }
  105. /* ------------------------------------------------------ */
  106. static void
  107. process_clish_module(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  108. {
  109. // create the global view
  110. if (!shell->global)
  111. shell->global = clish_shell_find_create_view(shell,
  112. "global", "");
  113. process_children(shell, element, shell->global);
  114. }
  115. /* ------------------------------------------------------ */
  116. static void process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  117. {
  118. clish_view_t *view;
  119. int allowed = 1;
  120. char *name = clish_xmlnode_fetch_attr(element, "name");
  121. char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
  122. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  123. char *restore = clish_xmlnode_fetch_attr(element, "restore");
  124. char *access = clish_xmlnode_fetch_attr(element, "access");
  125. /* Check permissions */
  126. if (access) {
  127. allowed = 0;
  128. if (shell->client_hooks->access_fn)
  129. allowed = shell->client_hooks->access_fn(shell, access);
  130. }
  131. if (!allowed)
  132. goto process_view_end;
  133. assert(name);
  134. /* re-use a view if it already exists */
  135. view = clish_shell_find_create_view(shell, name, prompt);
  136. if (depth && (lub_ctype_isdigit(*depth))) {
  137. unsigned res = atoi(depth);
  138. clish_view__set_depth(view, res);
  139. }
  140. if (restore) {
  141. if (!lub_string_nocasecmp(restore, "depth"))
  142. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  143. else if (!lub_string_nocasecmp(restore, "view"))
  144. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  145. else
  146. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  147. }
  148. process_children(shell, element, view);
  149. process_view_end:
  150. clish_xml_release(name);
  151. clish_xml_release(prompt);
  152. clish_xml_release(depth);
  153. clish_xml_release(restore);
  154. clish_xml_release(access);
  155. }
  156. /* ------------------------------------------------------ */
  157. static void process_ptype(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  158. {
  159. clish_ptype_method_e method;
  160. clish_ptype_preprocess_e preprocess;
  161. clish_ptype_t *ptype;
  162. char *name = clish_xmlnode_fetch_attr(element, "name");
  163. char *help = clish_xmlnode_fetch_attr(element, "help");
  164. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  165. char *method_name = clish_xmlnode_fetch_attr(element, "method");
  166. char *preprocess_name = clish_xmlnode_fetch_attr(element, "preprocess");
  167. assert(name);
  168. assert(pattern);
  169. method = clish_ptype_method_resolve(method_name);
  170. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  171. ptype = clish_shell_find_create_ptype(shell,
  172. name, help, pattern, method, preprocess);
  173. assert(ptype);
  174. clish_xml_release(name);
  175. clish_xml_release(help);
  176. clish_xml_release(pattern);
  177. clish_xml_release(method_name);
  178. clish_xml_release(preprocess_name);
  179. }
  180. /* ------------------------------------------------------ */
  181. static void
  182. process_overview(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  183. {
  184. char *content = NULL;
  185. unsigned int content_len = 2048;
  186. int result;
  187. /*
  188. * the code below faithfully assume that we'll be able fully store
  189. * the content of the node. If it's really, really big, we may have
  190. * an issue (but then, if it's that big, how the hell does it
  191. * already fits in allocated memory?)
  192. * Ergo, it -should- be safe.
  193. */
  194. do {
  195. content = (char*)realloc(content, content_len);
  196. result = clish_xmlnode_get_content(element, content,
  197. &content_len);
  198. } while (result == -E2BIG);
  199. if (result == 0 && content) {
  200. /* set the overview text for this view */
  201. assert(NULL == shell->overview);
  202. /* store the overview */
  203. shell->overview = lub_string_dup(content);
  204. }
  205. if (content)
  206. free(content);
  207. }
  208. /* ------------------------------------------------------ */
  209. static void
  210. process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  211. {
  212. clish_view_t *v = (clish_view_t *) parent;
  213. clish_command_t *cmd = NULL;
  214. clish_command_t *old;
  215. char *alias_name = NULL;
  216. clish_view_t *alias_view = NULL;
  217. int allowed = 1;
  218. char *access = clish_xmlnode_fetch_attr(element, "access");
  219. char *name = clish_xmlnode_fetch_attr(element, "name");
  220. char *help = clish_xmlnode_fetch_attr(element, "help");
  221. char *view = clish_xmlnode_fetch_attr(element, "view");
  222. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  223. char *escape_chars = clish_xmlnode_fetch_attr(element, "escape_chars");
  224. char *args_name = clish_xmlnode_fetch_attr(element, "args");
  225. char *args_help = clish_xmlnode_fetch_attr(element, "args_help");
  226. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  227. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  228. char *ref = clish_xmlnode_fetch_attr(element, "ref");
  229. /* Check permissions */
  230. if (access) {
  231. allowed = 0;
  232. if (shell->client_hooks->access_fn)
  233. allowed = shell->client_hooks->access_fn(shell, access);
  234. }
  235. if (!allowed)
  236. goto process_command_end;
  237. assert(name);
  238. /* check this command doesn't already exist */
  239. old = clish_view_find_command(v, name, BOOL_FALSE);
  240. if (old) {
  241. /* flag the duplication then ignore further definition */
  242. printf("DUPLICATE COMMAND: %s\n",
  243. clish_command__get_name(old));
  244. goto process_command_end;
  245. }
  246. assert(help);
  247. /* Reference 'ref' field */
  248. if (ref) {
  249. char *saveptr;
  250. const char *delim = "@";
  251. char *view_name = NULL;
  252. char *cmdn = NULL;
  253. char *str = lub_string_dup(ref);
  254. cmdn = strtok_r(str, delim, &saveptr);
  255. if (!cmdn) {
  256. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  257. lub_string_free(str);
  258. goto process_command_end;
  259. }
  260. alias_name = lub_string_dup(cmdn);
  261. view_name = strtok_r(NULL, delim, &saveptr);
  262. if (!view_name)
  263. alias_view = v;
  264. else
  265. alias_view = clish_shell_find_create_view(shell,
  266. view_name, NULL);
  267. lub_string_free(str);
  268. }
  269. /* create a command */
  270. cmd = clish_view_new_command(v, name, help);
  271. assert(cmd);
  272. clish_command__set_pview(cmd, v);
  273. /* define some specialist escape characters */
  274. if (escape_chars)
  275. clish_command__set_escape_chars(cmd, escape_chars);
  276. if (args_name) {
  277. /* define a "rest of line" argument */
  278. clish_param_t *param;
  279. clish_ptype_t *tmp = NULL;
  280. assert(args_help);
  281. tmp = clish_shell_find_ptype(shell, "internal_ARGS");
  282. assert(tmp);
  283. param = clish_param_new(args_name, args_help, tmp);
  284. clish_command__set_args(cmd, param);
  285. }
  286. /* define the view which this command changes to */
  287. if (view) {
  288. clish_view_t *next = clish_shell_find_create_view(shell, view,
  289. NULL);
  290. /* reference the next view */
  291. clish_command__set_view(cmd, next);
  292. }
  293. /* define the view id which this command changes to */
  294. if (viewid)
  295. clish_command__set_viewid(cmd, viewid);
  296. /* lock field */
  297. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  298. clish_command__set_lock(cmd, BOOL_FALSE);
  299. else
  300. clish_command__set_lock(cmd, BOOL_TRUE);
  301. /* interrupt field */
  302. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  303. clish_command__set_interrupt(cmd, BOOL_TRUE);
  304. else
  305. clish_command__set_interrupt(cmd, BOOL_FALSE);
  306. /* Set alias */
  307. if (alias_name) {
  308. assert(!((alias_view == v) && (!strcmp(alias_name, name))));
  309. clish_command__set_alias(cmd, alias_name);
  310. assert(alias_view);
  311. clish_command__set_alias_view(cmd, alias_view);
  312. lub_string_free(alias_name);
  313. }
  314. process_children(shell, element, cmd);
  315. process_command_end:
  316. clish_xml_release(access);
  317. clish_xml_release(name);
  318. clish_xml_release(help);
  319. clish_xml_release(view);
  320. clish_xml_release(viewid);
  321. clish_xml_release(escape_chars);
  322. clish_xml_release(args_name);
  323. clish_xml_release(args_help);
  324. clish_xml_release(lock);
  325. clish_xml_release(interrupt);
  326. clish_xml_release(ref);
  327. }
  328. /* ------------------------------------------------------ */
  329. static void
  330. process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  331. {
  332. clish_view_t *v = (clish_view_t *) parent;
  333. clish_command_t *cmd = NULL;
  334. char *view = clish_xmlnode_fetch_attr(element, "view");
  335. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  336. char *default_shebang =
  337. clish_xmlnode_fetch_attr(element, "default_shebang");
  338. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  339. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  340. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  341. assert(!shell->startup);
  342. assert(view);
  343. /* create a command with NULL help */
  344. cmd = clish_view_new_command(v, "startup", NULL);
  345. clish_command__set_lock(cmd, BOOL_FALSE);
  346. /* define the view which this command changes to */
  347. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  348. /* reference the next view */
  349. clish_command__set_view(cmd, next);
  350. /* define the view id which this command changes to */
  351. if (viewid)
  352. clish_command__set_viewid(cmd, viewid);
  353. if (default_shebang)
  354. clish_shell__set_default_shebang(shell, default_shebang);
  355. if (timeout)
  356. clish_shell__set_timeout(shell, atoi(timeout));
  357. /* lock field */
  358. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  359. clish_command__set_lock(cmd, BOOL_FALSE);
  360. else
  361. clish_command__set_lock(cmd, BOOL_TRUE);
  362. /* interrupt field */
  363. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  364. clish_command__set_interrupt(cmd, BOOL_TRUE);
  365. else
  366. clish_command__set_interrupt(cmd, BOOL_FALSE);
  367. /* remember this command */
  368. shell->startup = cmd;
  369. clish_xml_release(view);
  370. clish_xml_release(viewid);
  371. clish_xml_release(default_shebang);
  372. clish_xml_release(timeout);
  373. clish_xml_release(lock);
  374. clish_xml_release(interrupt);
  375. process_children(shell, element, cmd);
  376. }
  377. /* ------------------------------------------------------ */
  378. static void
  379. process_param(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  380. {
  381. clish_command_t *cmd = NULL;
  382. clish_param_t *p_param = NULL;
  383. clish_xmlnode_t *pelement;
  384. char *pname;
  385. pelement = clish_xmlnode_parent(element);
  386. pname = clish_xmlnode_get_all_name(pelement);
  387. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  388. p_param = (clish_param_t *)parent;
  389. else
  390. cmd = (clish_command_t *)parent;
  391. if (pname)
  392. free(pname);
  393. if (cmd || p_param) {
  394. char *name = clish_xmlnode_fetch_attr(element, "name");
  395. char *help = clish_xmlnode_fetch_attr(element, "help");
  396. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  397. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  398. char *defval = clish_xmlnode_fetch_attr(element, "default");
  399. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  400. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  401. char *order = clish_xmlnode_fetch_attr(element, "order");
  402. char *value = clish_xmlnode_fetch_attr(element, "value");
  403. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  404. char *test = clish_xmlnode_fetch_attr(element, "test");
  405. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  406. clish_param_t *param;
  407. clish_ptype_t *tmp = NULL;
  408. assert((!cmd) || (cmd != shell->startup));
  409. /* create a command */
  410. assert(name);
  411. assert(help);
  412. assert(ptype);
  413. if (*ptype) {
  414. tmp = clish_shell_find_create_ptype(shell, ptype,
  415. NULL, NULL,
  416. CLISH_PTYPE_REGEXP,
  417. CLISH_PTYPE_NONE);
  418. assert(tmp);
  419. }
  420. param = clish_param_new(name, help, tmp);
  421. /* If prefix is set clish will emulate old optional
  422. * command syntax over newer optional command mechanism.
  423. * It will create nested PARAM.
  424. */
  425. if (prefix) {
  426. const char *ptype_name = "__SUBCOMMAND";
  427. clish_param_t *opt_param = NULL;
  428. /* Create a ptype for prefix-named subcommand that
  429. * will contain the nested optional parameter. The
  430. * name of ptype is hardcoded. It's not good but
  431. * it's only the service ptype.
  432. */
  433. tmp = (clish_ptype_t *)lub_bintree_find(
  434. &shell->ptype_tree, ptype_name);
  435. if (!tmp)
  436. tmp = clish_shell_find_create_ptype(shell,
  437. ptype_name, "Option", "[^\\\\]+",
  438. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  439. assert(tmp);
  440. opt_param = clish_param_new(prefix, help, tmp);
  441. clish_param__set_mode(opt_param,
  442. CLISH_PARAM_SUBCOMMAND);
  443. clish_param__set_optional(opt_param, BOOL_TRUE);
  444. if (test)
  445. clish_param__set_test(opt_param, test);
  446. /* add the parameter to the command */
  447. if (cmd)
  448. clish_command_insert_param(cmd, opt_param);
  449. /* add the parameter to the param */
  450. if (p_param)
  451. clish_param_insert_param(p_param, opt_param);
  452. /* Unset cmd and set parent param to opt_param */
  453. cmd = NULL;
  454. p_param = opt_param;
  455. }
  456. if (defval)
  457. clish_param__set_default(param, defval);
  458. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  459. clish_param__set_hidden(param, BOOL_TRUE);
  460. else
  461. clish_param__set_hidden(param, BOOL_FALSE);
  462. if (mode) {
  463. if (lub_string_nocasecmp(mode, "switch") == 0) {
  464. clish_param__set_mode(param,
  465. CLISH_PARAM_SWITCH);
  466. /* Force hidden attribute */
  467. clish_param__set_hidden(param, BOOL_TRUE);
  468. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  469. clish_param__set_mode(param,
  470. CLISH_PARAM_SUBCOMMAND);
  471. else
  472. clish_param__set_mode(param,
  473. CLISH_PARAM_COMMON);
  474. }
  475. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  476. clish_param__set_optional(param, BOOL_TRUE);
  477. else
  478. clish_param__set_optional(param, BOOL_FALSE);
  479. if (order && lub_string_nocasecmp(order, "true") == 0)
  480. clish_param__set_order(param, BOOL_TRUE);
  481. else
  482. clish_param__set_order(param, BOOL_FALSE);
  483. if (value) {
  484. clish_param__set_value(param, value);
  485. /* Force mode to subcommand */
  486. clish_param__set_mode(param,
  487. CLISH_PARAM_SUBCOMMAND);
  488. }
  489. if (test && !prefix)
  490. clish_param__set_test(param, test);
  491. if (completion)
  492. clish_param__set_completion(param, completion);
  493. /* add the parameter to the command */
  494. if (cmd)
  495. clish_command_insert_param(cmd, param);
  496. /* add the parameter to the param */
  497. if (p_param)
  498. clish_param_insert_param(p_param, param);
  499. clish_xml_release(name);
  500. clish_xml_release(help);
  501. clish_xml_release(ptype);
  502. clish_xml_release(prefix);
  503. clish_xml_release(defval);
  504. clish_xml_release(mode);
  505. clish_xml_release(optional);
  506. clish_xml_release(order);
  507. clish_xml_release(value);
  508. clish_xml_release(hidden);
  509. clish_xml_release(test);
  510. clish_xml_release(completion);
  511. process_children(shell, element, param);
  512. }
  513. }
  514. /* ------------------------------------------------------ */
  515. static void
  516. process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  517. {
  518. clish_action_t *action = NULL;
  519. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  520. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  521. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  522. char *pname = clish_xmlnode_get_all_name(pelement);
  523. char *text;
  524. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  525. action = clish_var__get_action((clish_var_t *)parent);
  526. else
  527. action = clish_command__get_action((clish_command_t *)parent);
  528. assert(action);
  529. if (pname)
  530. free(pname);
  531. text = clish_xmlnode_get_all_content(element);
  532. if (text && *text) {
  533. /* store the action */
  534. clish_action__set_script(action, text);
  535. }
  536. if (text)
  537. free(text);
  538. if (builtin)
  539. clish_action__set_builtin(action, builtin);
  540. if (shebang)
  541. clish_action__set_shebang(action, shebang);
  542. clish_xml_release(builtin);
  543. clish_xml_release(shebang);
  544. }
  545. /* ------------------------------------------------------ */
  546. static void
  547. process_detail(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  548. {
  549. clish_command_t *cmd = (clish_command_t *) parent;
  550. /* read the following text element */
  551. char *text = clish_xmlnode_get_all_content(element);
  552. if (text && *text) {
  553. /* store the action */
  554. clish_command__set_detail(cmd, text);
  555. }
  556. if (text)
  557. free(text);
  558. }
  559. /* ------------------------------------------------------ */
  560. static void
  561. process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  562. {
  563. clish_view_t *v = (clish_view_t *) parent;
  564. clish_nspace_t *nspace = NULL;
  565. char *view = clish_xmlnode_fetch_attr(element, "ref");
  566. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  567. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  568. char *help = clish_xmlnode_fetch_attr(element, "help");
  569. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  570. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  571. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  572. char *access = clish_xmlnode_fetch_attr(element, "access");
  573. int allowed = 1;
  574. if (access) {
  575. allowed = 0;
  576. if (shell->client_hooks->access_fn)
  577. allowed = shell->client_hooks->access_fn(shell, access);
  578. }
  579. if (!allowed)
  580. goto process_namespace_end;
  581. assert(view);
  582. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  583. view, NULL);
  584. assert(ref_view);
  585. /* Don't include itself without prefix */
  586. if ((ref_view == v) && !prefix)
  587. goto process_namespace_end;
  588. nspace = clish_nspace_new(ref_view);
  589. assert(nspace);
  590. clish_view_insert_nspace(v, nspace);
  591. if (prefix) {
  592. clish_nspace__set_prefix(nspace, prefix);
  593. if (prefix_help)
  594. clish_nspace_create_prefix_cmd(nspace,
  595. "prefix",
  596. prefix_help);
  597. else
  598. clish_nspace_create_prefix_cmd(nspace,
  599. "prefix",
  600. "Prefix for the imported commands.");
  601. }
  602. if (help && lub_string_nocasecmp(help, "true") == 0)
  603. clish_nspace__set_help(nspace, BOOL_TRUE);
  604. else
  605. clish_nspace__set_help(nspace, BOOL_FALSE);
  606. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  607. clish_nspace__set_completion(nspace, BOOL_FALSE);
  608. else
  609. clish_nspace__set_completion(nspace, BOOL_TRUE);
  610. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  611. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  612. else
  613. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  614. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  615. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  616. else
  617. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  618. process_namespace_end:
  619. clish_xml_release(view);
  620. clish_xml_release(prefix);
  621. clish_xml_release(prefix_help);
  622. clish_xml_release(help);
  623. clish_xml_release(completion);
  624. clish_xml_release(context_help);
  625. clish_xml_release(inherit);
  626. clish_xml_release(access);
  627. }
  628. /* ------------------------------------------------------ */
  629. static void
  630. process_config(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  631. {
  632. clish_command_t *cmd = (clish_command_t *)parent;
  633. clish_config_t *config;
  634. if (!cmd)
  635. return;
  636. config = clish_command__get_config(cmd);
  637. /* read the following text element */
  638. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  639. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  640. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  641. char *file = clish_xmlnode_fetch_attr(element, "file");
  642. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  643. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  644. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  645. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  646. if (operation && !lub_string_nocasecmp(operation, "unset"))
  647. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  648. else if (operation && !lub_string_nocasecmp(operation, "none"))
  649. clish_config__set_op(config, CLISH_CONFIG_NONE);
  650. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  651. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  652. else {
  653. clish_config__set_op(config, CLISH_CONFIG_SET);
  654. /* The priority if no clearly specified */
  655. clish_config__set_priority(config, 0x7f00);
  656. }
  657. if (priority) {
  658. long val = 0;
  659. char *endptr;
  660. unsigned short pri;
  661. val = strtol(priority, &endptr, 0);
  662. if (endptr == priority)
  663. pri = 0;
  664. else if (val > 0xffff)
  665. pri = 0xffff;
  666. else if (val < 0)
  667. pri = 0;
  668. else
  669. pri = (unsigned short)val;
  670. clish_config__set_priority(config, pri);
  671. }
  672. if (pattern)
  673. clish_config__set_pattern(config, pattern);
  674. else
  675. clish_config__set_pattern(config, "^${__cmd}");
  676. if (file)
  677. clish_config__set_file(config, file);
  678. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  679. clish_config__set_splitter(config, BOOL_FALSE);
  680. else
  681. clish_config__set_splitter(config, BOOL_TRUE);
  682. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  683. clish_config__set_unique(config, BOOL_FALSE);
  684. else
  685. clish_config__set_unique(config, BOOL_TRUE);
  686. if (seq)
  687. clish_config__set_seq(config, seq);
  688. else
  689. /* The entries without sequence cannot be non-unique */
  690. clish_config__set_unique(config, BOOL_TRUE);
  691. if (depth)
  692. clish_config__set_depth(config, depth);
  693. clish_xml_release(operation);
  694. clish_xml_release(priority);
  695. clish_xml_release(pattern);
  696. clish_xml_release(file);
  697. clish_xml_release(splitter);
  698. clish_xml_release(seq);
  699. clish_xml_release(unique);
  700. clish_xml_release(depth);
  701. }
  702. /* ------------------------------------------------------ */
  703. static void process_var(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  704. {
  705. clish_var_t *var = NULL;
  706. char *name = clish_xmlnode_fetch_attr(element, "name");
  707. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  708. char *value = clish_xmlnode_fetch_attr(element, "value");
  709. assert(name);
  710. /* Check if this var doesn't already exist */
  711. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  712. if (var) {
  713. printf("DUPLICATE VAR: %s\n", name);
  714. assert(!var);
  715. }
  716. /* Create var instance */
  717. var = clish_var_new(name);
  718. lub_bintree_insert(&shell->var_tree, var);
  719. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  720. clish_var__set_dynamic(var, BOOL_TRUE);
  721. if (value)
  722. clish_var__set_value(var, value);
  723. clish_xml_release(name);
  724. clish_xml_release(dynamic);
  725. clish_xml_release(value);
  726. process_children(shell, element, var);
  727. }
  728. /* ------------------------------------------------------ */
  729. static void process_wdog(clish_shell_t *shell,
  730. clish_xmlnode_t *element, void *parent)
  731. {
  732. clish_view_t *v = (clish_view_t *)parent;
  733. clish_command_t *cmd = NULL;
  734. assert(!shell->wdog);
  735. /* create a command with NULL help */
  736. cmd = clish_view_new_command(v, "watchdog", NULL);
  737. clish_command__set_lock(cmd, BOOL_FALSE);
  738. /* Remember this command */
  739. shell->wdog = cmd;
  740. process_children(shell, element, cmd);
  741. }
  742. /* ------------------------------------------------------ */
  743. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  744. {
  745. int ret = -1;
  746. clish_xmldoc_t *doc;
  747. doc = clish_xmldoc_read(filename);
  748. if (clish_xmldoc_is_valid(doc)) {
  749. clish_xmlnode_t *root = clish_xmldoc_get_root(doc);
  750. process_node(shell, root, NULL);
  751. ret = 0;
  752. } else {
  753. int errcaps = clish_xmldoc_error_caps(doc);
  754. printf("Unable to open file '%s'", filename);
  755. if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
  756. printf(", at line %d", clish_xmldoc_get_err_line(doc));
  757. if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
  758. printf(", at column %d", clish_xmldoc_get_err_col(doc));
  759. if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
  760. printf(", message is %s", clish_xmldoc_get_err_msg(doc));
  761. printf("\n");
  762. }
  763. clish_xmldoc_release(doc);
  764. return ret;
  765. }
  766. /* ------------------------------------------------------ */