shell_xml.c 35 KB

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