shell_xml.c 28 KB

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