shell_xml.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. #ifdef DEBUG
  105. fprintf(stderr, "Parse XML-file: %s\n", filename);
  106. #endif
  107. /* load this file */
  108. (void)clish_shell_xml_read(this, filename);
  109. /* release the resource */
  110. lub_string_free(filename);
  111. }
  112. }
  113. /* all done for this directory */
  114. closedir(dir);
  115. }
  116. /* tidy up */
  117. lub_string_free(buffer);
  118. #ifdef DEBUG
  119. clish_shell_dump(this);
  120. #endif
  121. }
  122. /*
  123. * ------------------------------------------------------
  124. * This function reads an element from the XML stream and processes it.
  125. * ------------------------------------------------------
  126. */
  127. static void process_node(clish_shell_t * shell, clish_xmlnode_t * node, void *parent)
  128. {
  129. switch (clish_xmlnode_get_type(node)) {
  130. case CLISH_XMLNODE_ELM: {
  131. clish_xml_cb_t * cb;
  132. char name[128];
  133. unsigned int namelen = sizeof(name);
  134. if (clish_xmlnode_get_name(node, name, &namelen) == 0) {
  135. for (cb = &xml_elements[0]; cb->element; cb++) {
  136. if (0 == strcmp(name, cb->element)) {
  137. #ifdef DEBUG
  138. fprintf(stderr, "NODE:");
  139. clish_xmlnode_print(node, stderr);
  140. fprintf(stderr, "\n");
  141. #endif
  142. /* process the elements at this level */
  143. cb->handler(shell, node, parent);
  144. break;
  145. }
  146. }
  147. }
  148. break;
  149. }
  150. case CLISH_XMLNODE_DOC:
  151. case CLISH_XMLNODE_TEXT:
  152. case CLISH_XMLNODE_ATTR:
  153. case CLISH_XMLNODE_PI:
  154. case CLISH_XMLNODE_COMMENT:
  155. case CLISH_XMLNODE_DECL:
  156. case CLISH_XMLNODE_UNKNOWN:
  157. default:
  158. break;
  159. }
  160. }
  161. /* ------------------------------------------------------ */
  162. static void process_children(clish_shell_t * shell,
  163. clish_xmlnode_t * element, void *parent)
  164. {
  165. clish_xmlnode_t *node = NULL;
  166. while ((node = clish_xmlnode_next_child(element, node)) != NULL) {
  167. /* Now deal with all the contained elements */
  168. process_node(shell, node, parent);
  169. }
  170. }
  171. /* ------------------------------------------------------ */
  172. static void
  173. process_clish_module(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  174. {
  175. // create the global view
  176. if (!shell->global)
  177. shell->global = clish_shell_find_create_view(shell,
  178. "global", "");
  179. process_children(shell, element, shell->global);
  180. }
  181. /* ------------------------------------------------------ */
  182. static void process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  183. {
  184. clish_view_t *view;
  185. int allowed = 1;
  186. char *name = clish_xmlnode_fetch_attr(element, "name");
  187. char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
  188. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  189. char *restore = clish_xmlnode_fetch_attr(element, "restore");
  190. char *access = clish_xmlnode_fetch_attr(element, "access");
  191. /* Check permissions */
  192. if (access) {
  193. allowed = 0;
  194. if (shell->client_hooks->access_fn)
  195. allowed = shell->client_hooks->access_fn(shell, access);
  196. }
  197. if (!allowed)
  198. goto process_view_end;
  199. assert(name);
  200. /* re-use a view if it already exists */
  201. view = clish_shell_find_create_view(shell, name, prompt);
  202. if (depth && (lub_ctype_isdigit(*depth))) {
  203. unsigned res = atoi(depth);
  204. clish_view__set_depth(view, res);
  205. }
  206. if (restore) {
  207. if (!lub_string_nocasecmp(restore, "depth"))
  208. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  209. else if (!lub_string_nocasecmp(restore, "view"))
  210. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  211. else
  212. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  213. }
  214. process_children(shell, element, view);
  215. process_view_end:
  216. clish_xml_release(name);
  217. clish_xml_release(prompt);
  218. clish_xml_release(depth);
  219. clish_xml_release(restore);
  220. clish_xml_release(access);
  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. }
  246. /* ------------------------------------------------------ */
  247. static void
  248. process_overview(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  249. {
  250. char *content = NULL;
  251. unsigned int content_len = 2048;
  252. int result;
  253. /*
  254. * the code below faithfully assume that we'll be able fully store
  255. * the content of the node. If it's really, really big, we may have
  256. * an issue (but then, if it's that big, how the hell does it
  257. * already fits in allocated memory?)
  258. * Ergo, it -should- be safe.
  259. */
  260. do {
  261. content = (char*)realloc(content, content_len);
  262. result = clish_xmlnode_get_content(element, content,
  263. &content_len);
  264. } while (result == -E2BIG);
  265. if (result == 0 && content) {
  266. /* set the overview text for this view */
  267. assert(NULL == shell->overview);
  268. /* store the overview */
  269. shell->overview = lub_string_dup(content);
  270. }
  271. if (content)
  272. free(content);
  273. }
  274. /* ------------------------------------------------------ */
  275. static void
  276. process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  277. {
  278. clish_view_t *v = (clish_view_t *) parent;
  279. clish_command_t *cmd = NULL;
  280. clish_command_t *old;
  281. char *alias_name = NULL;
  282. clish_view_t *alias_view = NULL;
  283. int allowed = 1;
  284. char *access = clish_xmlnode_fetch_attr(element, "access");
  285. char *name = clish_xmlnode_fetch_attr(element, "name");
  286. char *help = clish_xmlnode_fetch_attr(element, "help");
  287. char *view = clish_xmlnode_fetch_attr(element, "view");
  288. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  289. char *escape_chars = clish_xmlnode_fetch_attr(element, "escape_chars");
  290. char *args_name = clish_xmlnode_fetch_attr(element, "args");
  291. char *args_help = clish_xmlnode_fetch_attr(element, "args_help");
  292. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  293. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  294. char *ref = clish_xmlnode_fetch_attr(element, "ref");
  295. /* Check permissions */
  296. if (access) {
  297. allowed = 0;
  298. if (shell->client_hooks->access_fn)
  299. allowed = shell->client_hooks->access_fn(shell, access);
  300. }
  301. if (!allowed)
  302. goto process_command_end;
  303. assert(name);
  304. /* check this command doesn't already exist */
  305. old = clish_view_find_command(v, name, BOOL_FALSE);
  306. if (old) {
  307. /* flag the duplication then ignore further definition */
  308. printf("DUPLICATE COMMAND: %s\n",
  309. clish_command__get_name(old));
  310. goto process_command_end;
  311. }
  312. assert(help);
  313. /* Reference 'ref' field */
  314. if (ref) {
  315. char *saveptr;
  316. const char *delim = "@";
  317. char *view_name = NULL;
  318. char *cmdn = NULL;
  319. char *str = lub_string_dup(ref);
  320. cmdn = strtok_r(str, delim, &saveptr);
  321. if (!cmdn) {
  322. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  323. lub_string_free(str);
  324. goto process_command_end;
  325. }
  326. alias_name = lub_string_dup(cmdn);
  327. view_name = strtok_r(NULL, delim, &saveptr);
  328. if (!view_name)
  329. alias_view = v;
  330. else
  331. alias_view = clish_shell_find_create_view(shell,
  332. view_name, NULL);
  333. lub_string_free(str);
  334. }
  335. /* create a command */
  336. cmd = clish_view_new_command(v, name, help);
  337. assert(cmd);
  338. clish_command__set_pview(cmd, v);
  339. /* define some specialist escape characters */
  340. if (escape_chars)
  341. clish_command__set_escape_chars(cmd, escape_chars);
  342. if (args_name) {
  343. /* define a "rest of line" argument */
  344. clish_param_t *param;
  345. clish_ptype_t *tmp = NULL;
  346. assert(args_help);
  347. tmp = clish_shell_find_ptype(shell, "internal_ARGS");
  348. assert(tmp);
  349. param = clish_param_new(args_name, args_help, tmp);
  350. clish_command__set_args(cmd, param);
  351. }
  352. /* define the view which this command changes to */
  353. if (view) {
  354. clish_view_t *next = clish_shell_find_create_view(shell, view,
  355. NULL);
  356. /* reference the next view */
  357. clish_command__set_view(cmd, next);
  358. }
  359. /* define the view id which this command changes to */
  360. if (viewid)
  361. clish_command__set_viewid(cmd, viewid);
  362. /* lock field */
  363. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  364. clish_command__set_lock(cmd, BOOL_FALSE);
  365. else
  366. clish_command__set_lock(cmd, BOOL_TRUE);
  367. /* interrupt field */
  368. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  369. clish_command__set_interrupt(cmd, BOOL_TRUE);
  370. else
  371. clish_command__set_interrupt(cmd, BOOL_FALSE);
  372. /* Set alias */
  373. if (alias_name) {
  374. assert(!((alias_view == v) && (!strcmp(alias_name, name))));
  375. clish_command__set_alias(cmd, alias_name);
  376. assert(alias_view);
  377. clish_command__set_alias_view(cmd, alias_view);
  378. lub_string_free(alias_name);
  379. }
  380. process_children(shell, element, cmd);
  381. process_command_end:
  382. clish_xml_release(access);
  383. clish_xml_release(name);
  384. clish_xml_release(help);
  385. clish_xml_release(view);
  386. clish_xml_release(viewid);
  387. clish_xml_release(escape_chars);
  388. clish_xml_release(args_name);
  389. clish_xml_release(args_help);
  390. clish_xml_release(lock);
  391. clish_xml_release(interrupt);
  392. clish_xml_release(ref);
  393. }
  394. /* ------------------------------------------------------ */
  395. static void
  396. process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  397. {
  398. clish_view_t *v = (clish_view_t *) parent;
  399. clish_command_t *cmd = NULL;
  400. char *view = clish_xmlnode_fetch_attr(element, "view");
  401. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  402. char *default_shebang =
  403. clish_xmlnode_fetch_attr(element, "default_shebang");
  404. char *default_builtin =
  405. clish_xmlnode_fetch_attr(element, "default_builtin");
  406. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  407. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  408. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  409. assert(!shell->startup);
  410. assert(view);
  411. /* create a command with NULL help */
  412. cmd = clish_view_new_command(v, "startup", NULL);
  413. clish_command__set_lock(cmd, BOOL_FALSE);
  414. /* define the view which this command changes to */
  415. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  416. /* reference the next view */
  417. clish_command__set_view(cmd, next);
  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 (default_builtin)
  424. clish_sym__set_name(shell->default_sym, default_builtin);
  425. if (timeout)
  426. clish_shell__set_timeout(shell, atoi(timeout));
  427. /* lock field */
  428. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  429. clish_command__set_lock(cmd, BOOL_FALSE);
  430. else
  431. clish_command__set_lock(cmd, BOOL_TRUE);
  432. /* interrupt field */
  433. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  434. clish_command__set_interrupt(cmd, BOOL_TRUE);
  435. else
  436. clish_command__set_interrupt(cmd, BOOL_FALSE);
  437. /* remember this command */
  438. shell->startup = cmd;
  439. clish_xml_release(view);
  440. clish_xml_release(viewid);
  441. clish_xml_release(default_shebang);
  442. clish_xml_release(default_builtin);
  443. clish_xml_release(timeout);
  444. clish_xml_release(lock);
  445. clish_xml_release(interrupt);
  446. process_children(shell, element, cmd);
  447. }
  448. /* ------------------------------------------------------ */
  449. static void
  450. process_param(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  451. {
  452. clish_command_t *cmd = NULL;
  453. clish_param_t *p_param = NULL;
  454. clish_xmlnode_t *pelement;
  455. char *pname;
  456. pelement = clish_xmlnode_parent(element);
  457. pname = clish_xmlnode_get_all_name(pelement);
  458. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  459. p_param = (clish_param_t *)parent;
  460. else
  461. cmd = (clish_command_t *)parent;
  462. if (pname)
  463. free(pname);
  464. if (cmd || p_param) {
  465. char *name = clish_xmlnode_fetch_attr(element, "name");
  466. char *help = clish_xmlnode_fetch_attr(element, "help");
  467. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  468. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  469. char *defval = clish_xmlnode_fetch_attr(element, "default");
  470. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  471. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  472. char *order = clish_xmlnode_fetch_attr(element, "order");
  473. char *value = clish_xmlnode_fetch_attr(element, "value");
  474. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  475. char *test = clish_xmlnode_fetch_attr(element, "test");
  476. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  477. clish_param_t *param;
  478. clish_ptype_t *tmp = NULL;
  479. assert((!cmd) || (cmd != shell->startup));
  480. /* create a command */
  481. assert(name);
  482. assert(help);
  483. assert(ptype);
  484. if (*ptype) {
  485. tmp = clish_shell_find_create_ptype(shell, ptype,
  486. NULL, NULL,
  487. CLISH_PTYPE_REGEXP,
  488. CLISH_PTYPE_NONE);
  489. assert(tmp);
  490. }
  491. param = clish_param_new(name, help, tmp);
  492. /* If prefix is set clish will emulate old optional
  493. * command syntax over newer optional command mechanism.
  494. * It will create nested PARAM.
  495. */
  496. if (prefix) {
  497. const char *ptype_name = "__SUBCOMMAND";
  498. clish_param_t *opt_param = NULL;
  499. /* Create a ptype for prefix-named subcommand that
  500. * will contain the nested optional parameter. The
  501. * name of ptype is hardcoded. It's not good but
  502. * it's only the service ptype.
  503. */
  504. tmp = (clish_ptype_t *)lub_bintree_find(
  505. &shell->ptype_tree, ptype_name);
  506. if (!tmp)
  507. tmp = clish_shell_find_create_ptype(shell,
  508. ptype_name, "Option", "[^\\\\]+",
  509. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  510. assert(tmp);
  511. opt_param = clish_param_new(prefix, help, tmp);
  512. clish_param__set_mode(opt_param,
  513. CLISH_PARAM_SUBCOMMAND);
  514. clish_param__set_optional(opt_param, BOOL_TRUE);
  515. if (test)
  516. clish_param__set_test(opt_param, test);
  517. /* add the parameter to the command */
  518. if (cmd)
  519. clish_command_insert_param(cmd, opt_param);
  520. /* add the parameter to the param */
  521. if (p_param)
  522. clish_param_insert_param(p_param, opt_param);
  523. /* Unset cmd and set parent param to opt_param */
  524. cmd = NULL;
  525. p_param = opt_param;
  526. }
  527. if (defval)
  528. clish_param__set_default(param, defval);
  529. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  530. clish_param__set_hidden(param, BOOL_TRUE);
  531. else
  532. clish_param__set_hidden(param, BOOL_FALSE);
  533. if (mode) {
  534. if (lub_string_nocasecmp(mode, "switch") == 0) {
  535. clish_param__set_mode(param,
  536. CLISH_PARAM_SWITCH);
  537. /* Force hidden attribute */
  538. clish_param__set_hidden(param, BOOL_TRUE);
  539. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  540. clish_param__set_mode(param,
  541. CLISH_PARAM_SUBCOMMAND);
  542. else
  543. clish_param__set_mode(param,
  544. CLISH_PARAM_COMMON);
  545. }
  546. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  547. clish_param__set_optional(param, BOOL_TRUE);
  548. else
  549. clish_param__set_optional(param, BOOL_FALSE);
  550. if (order && lub_string_nocasecmp(order, "true") == 0)
  551. clish_param__set_order(param, BOOL_TRUE);
  552. else
  553. clish_param__set_order(param, BOOL_FALSE);
  554. if (value) {
  555. clish_param__set_value(param, value);
  556. /* Force mode to subcommand */
  557. clish_param__set_mode(param,
  558. CLISH_PARAM_SUBCOMMAND);
  559. }
  560. if (test && !prefix)
  561. clish_param__set_test(param, test);
  562. if (completion)
  563. clish_param__set_completion(param, completion);
  564. /* add the parameter to the command */
  565. if (cmd)
  566. clish_command_insert_param(cmd, param);
  567. /* add the parameter to the param */
  568. if (p_param)
  569. clish_param_insert_param(p_param, param);
  570. clish_xml_release(name);
  571. clish_xml_release(help);
  572. clish_xml_release(ptype);
  573. clish_xml_release(prefix);
  574. clish_xml_release(defval);
  575. clish_xml_release(mode);
  576. clish_xml_release(optional);
  577. clish_xml_release(order);
  578. clish_xml_release(value);
  579. clish_xml_release(hidden);
  580. clish_xml_release(test);
  581. clish_xml_release(completion);
  582. process_children(shell, element, param);
  583. }
  584. }
  585. /* ------------------------------------------------------ */
  586. static void
  587. process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  588. {
  589. clish_action_t *action = NULL;
  590. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  591. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  592. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  593. char *pname = clish_xmlnode_get_all_name(pelement);
  594. char *text;
  595. clish_sym_t *sym = NULL;
  596. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  597. action = clish_var__get_action((clish_var_t *)parent);
  598. else
  599. action = clish_command__get_action((clish_command_t *)parent);
  600. assert(action);
  601. if (pname)
  602. free(pname);
  603. text = clish_xmlnode_get_all_content(element);
  604. if (text && *text) {
  605. /* store the action */
  606. clish_action__set_script(action, text);
  607. }
  608. if (text)
  609. free(text);
  610. if (builtin)
  611. sym = clish_shell_add_unresolved_sym(shell, builtin);
  612. else
  613. sym = shell->default_sym;
  614. clish_action__set_builtin(action, sym);
  615. if (shebang)
  616. clish_action__set_shebang(action, shebang);
  617. clish_xml_release(builtin);
  618. clish_xml_release(shebang);
  619. }
  620. /* ------------------------------------------------------ */
  621. static void
  622. process_detail(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  623. {
  624. clish_command_t *cmd = (clish_command_t *) parent;
  625. /* read the following text element */
  626. char *text = clish_xmlnode_get_all_content(element);
  627. if (text && *text) {
  628. /* store the action */
  629. clish_command__set_detail(cmd, text);
  630. }
  631. if (text)
  632. free(text);
  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
  705. process_config(clish_shell_t * shell, clish_xmlnode_t * element, 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. }
  777. /* ------------------------------------------------------ */
  778. static void process_var(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  779. {
  780. clish_var_t *var = NULL;
  781. char *name = clish_xmlnode_fetch_attr(element, "name");
  782. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  783. char *value = clish_xmlnode_fetch_attr(element, "value");
  784. assert(name);
  785. /* Check if this var doesn't already exist */
  786. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  787. if (var) {
  788. printf("DUPLICATE VAR: %s\n", name);
  789. assert(!var);
  790. }
  791. /* Create var instance */
  792. var = clish_var_new(name);
  793. lub_bintree_insert(&shell->var_tree, var);
  794. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  795. clish_var__set_dynamic(var, BOOL_TRUE);
  796. if (value)
  797. clish_var__set_value(var, value);
  798. clish_xml_release(name);
  799. clish_xml_release(dynamic);
  800. clish_xml_release(value);
  801. process_children(shell, element, var);
  802. }
  803. /* ------------------------------------------------------ */
  804. static void process_wdog(clish_shell_t *shell,
  805. clish_xmlnode_t *element, void *parent)
  806. {
  807. clish_view_t *v = (clish_view_t *)parent;
  808. clish_command_t *cmd = NULL;
  809. assert(!shell->wdog);
  810. /* create a command with NULL help */
  811. cmd = clish_view_new_command(v, "watchdog", NULL);
  812. clish_command__set_lock(cmd, BOOL_FALSE);
  813. /* Remember this command */
  814. shell->wdog = cmd;
  815. process_children(shell, element, cmd);
  816. }
  817. /* ------------------------------------------------------ */
  818. static void
  819. process_hotkey(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  820. {
  821. clish_view_t *v = (clish_view_t *)parent;
  822. char *key = clish_xmlnode_fetch_attr(element, "key");
  823. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  824. assert(key);
  825. assert(cmd);
  826. assert (!clish_view_insert_hotkey(v, key, cmd));
  827. clish_xml_release(key);
  828. clish_xml_release(cmd);
  829. }
  830. /* ------------------------------------------------------ */
  831. static void
  832. process_plugin(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  833. {
  834. clish_plugin_t *plugin;
  835. char *file = clish_xmlnode_fetch_attr(element, "file");
  836. char *name = clish_xmlnode_fetch_attr(element, "name");
  837. assert(file);
  838. plugin = clish_plugin_new(name, file);
  839. assert(plugin);
  840. lub_list_add(shell->plugins, plugin);
  841. clish_xml_release(file);
  842. clish_xml_release(name);
  843. }
  844. /* ------------------------------------------------------ */
  845. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  846. {
  847. int ret = -1;
  848. clish_xmldoc_t *doc;
  849. doc = clish_xmldoc_read(filename);
  850. if (clish_xmldoc_is_valid(doc)) {
  851. clish_xmlnode_t *root = clish_xmldoc_get_root(doc);
  852. process_node(shell, root, NULL);
  853. ret = 0;
  854. } else {
  855. int errcaps = clish_xmldoc_error_caps(doc);
  856. printf("Unable to open file '%s'", filename);
  857. if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
  858. printf(", at line %d", clish_xmldoc_get_err_line(doc));
  859. if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
  860. printf(", at column %d", clish_xmldoc_get_err_col(doc));
  861. if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
  862. printf(", message is %s", clish_xmldoc_get_err_msg(doc));
  863. printf("\n");
  864. }
  865. clish_xmldoc_release(doc);
  866. return ret;
  867. }
  868. /* ------------------------------------------------------ */