shell_xml.c 34 KB

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