1
0

shell_xml.c 31 KB

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