shell_xml.c 33 KB

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