shell_xml.c 29 KB

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