shell_xml.c 27 KB

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