shell_xml.c 28 KB

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