shell_xml.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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;
  293. int res = -1;
  294. char *name = clish_xmlnode_fetch_attr(element, "name");
  295. char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
  296. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  297. char *restore = clish_xmlnode_fetch_attr(element, "restore");
  298. char *access = clish_xmlnode_fetch_attr(element, "access");
  299. /* Check syntax */
  300. if (!name) {
  301. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  302. goto error;
  303. }
  304. /* re-use a view if it already exists */
  305. view = clish_shell_find_create_view(shell, name);
  306. if (prompt)
  307. clish_view__set_prompt(view, prompt);
  308. if (depth && (lub_ctype_isdigit(*depth))) {
  309. unsigned int res = 0;
  310. lub_conv_atoui(depth, &res, 0);
  311. clish_view__set_depth(view, res);
  312. }
  313. if (restore) {
  314. if (!lub_string_nocasecmp(restore, "depth"))
  315. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  316. else if (!lub_string_nocasecmp(restore, "view"))
  317. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  318. else
  319. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  320. }
  321. if (access)
  322. clish_view__set_access(view, access);
  323. //process_view_end:
  324. res = process_children(shell, element, view);
  325. error:
  326. clish_xml_release(name);
  327. clish_xml_release(prompt);
  328. clish_xml_release(depth);
  329. clish_xml_release(restore);
  330. clish_xml_release(access);
  331. parent = parent; /* Happy compiler */
  332. return res;
  333. }
  334. /* ------------------------------------------------------ */
  335. static int process_ptype(clish_shell_t *shell, clish_xmlnode_t *element,
  336. void *parent)
  337. {
  338. clish_ptype_method_e method;
  339. clish_ptype_preprocess_e preprocess;
  340. int res = -1;
  341. clish_ptype_t *ptype;
  342. char *name = clish_xmlnode_fetch_attr(element, "name");
  343. char *help = clish_xmlnode_fetch_attr(element, "help");
  344. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  345. char *method_name = clish_xmlnode_fetch_attr(element, "method");
  346. char *preprocess_name = clish_xmlnode_fetch_attr(element, "preprocess");
  347. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  348. /* Check syntax */
  349. if (!name) {
  350. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  351. goto error;
  352. }
  353. method = clish_ptype_method_resolve(method_name);
  354. if (CLISH_PTYPE_METHOD_MAX == method) {
  355. fprintf(stderr, CLISH_XML_ERROR_ATTR("method"));
  356. goto error;
  357. }
  358. if ((method != CLISH_PTYPE_METHOD_CODE) && !pattern) {
  359. fprintf(stderr, CLISH_XML_ERROR_ATTR("pattern"));
  360. goto error;
  361. }
  362. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  363. ptype = clish_shell_find_create_ptype(shell,
  364. name, help, pattern, method, preprocess);
  365. if (completion)
  366. clish_ptype__set_completion(ptype, completion);
  367. res = process_children(shell, element, ptype);
  368. error:
  369. clish_xml_release(name);
  370. clish_xml_release(help);
  371. clish_xml_release(pattern);
  372. clish_xml_release(method_name);
  373. clish_xml_release(preprocess_name);
  374. clish_xml_release(completion);
  375. parent = parent; /* Happy compiler */
  376. return res;
  377. }
  378. /* ------------------------------------------------------ */
  379. static int process_overview(clish_shell_t *shell, clish_xmlnode_t *element,
  380. void *parent)
  381. {
  382. char *content = NULL;
  383. unsigned int content_len = 2048;
  384. int result;
  385. /*
  386. * the code below faithfully assume that we'll be able fully store
  387. * the content of the node. If it's really, really big, we may have
  388. * an issue (but then, if it's that big, how the hell does it
  389. * already fits in allocated memory?)
  390. * Ergo, it -should- be safe.
  391. */
  392. do {
  393. char *new = (char*)realloc(content, content_len);
  394. if (!new) {
  395. if (content)
  396. free(content);
  397. return -1;
  398. }
  399. content = new;
  400. result = clish_xmlnode_get_content(element, content,
  401. &content_len);
  402. } while (result == -E2BIG);
  403. if (result == 0 && content) {
  404. /* set the overview text for this view */
  405. assert(NULL == shell->overview);
  406. /* store the overview */
  407. shell->overview = lub_string_dup(content);
  408. }
  409. if (content)
  410. free(content);
  411. parent = parent; /* Happy compiler */
  412. return 0;
  413. }
  414. /* ------------------------------------------------------ */
  415. static int process_command(clish_shell_t *shell, clish_xmlnode_t *element,
  416. void *parent)
  417. {
  418. clish_view_t *v = (clish_view_t *) parent;
  419. clish_command_t *cmd = NULL;
  420. clish_command_t *old;
  421. int res = -1;
  422. char *access = clish_xmlnode_fetch_attr(element, "access");
  423. char *name = clish_xmlnode_fetch_attr(element, "name");
  424. char *help = clish_xmlnode_fetch_attr(element, "help");
  425. char *view = clish_xmlnode_fetch_attr(element, "view");
  426. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  427. char *escape_chars = clish_xmlnode_fetch_attr(element, "escape_chars");
  428. char *args_name = clish_xmlnode_fetch_attr(element, "args");
  429. char *args_help = clish_xmlnode_fetch_attr(element, "args_help");
  430. char *ref = clish_xmlnode_fetch_attr(element, "ref");
  431. #ifdef LEGACY
  432. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  433. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  434. clish_action_t *action;
  435. #endif
  436. /* Check syntax */
  437. if (!name) {
  438. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  439. goto error;
  440. }
  441. if (!help) {
  442. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  443. goto error;
  444. }
  445. /* check this command doesn't already exist */
  446. old = clish_view_find_command(v, name, BOOL_FALSE);
  447. if (old) {
  448. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate COMMAND name=\"%s\".\n", name);
  449. goto error;
  450. }
  451. /* create a command */
  452. cmd = clish_view_new_command(v, name, help);
  453. clish_command__set_pview(cmd, v);
  454. /* Reference 'ref' field */
  455. if (ref) {
  456. char *saveptr = NULL;
  457. const char *delim = "@";
  458. char *view_name = NULL;
  459. char *cmdn = NULL;
  460. char *str = lub_string_dup(ref);
  461. cmdn = strtok_r(str, delim, &saveptr);
  462. if (!cmdn) {
  463. fprintf(stderr, CLISH_XML_ERROR_STR"Invalid \"ref\" attribute value.\n");
  464. lub_string_free(str);
  465. goto error;
  466. }
  467. clish_command__set_alias(cmd, cmdn); /* alias name */
  468. view_name = strtok_r(NULL, delim, &saveptr);
  469. clish_command__set_alias_view(cmd, view_name);
  470. lub_string_free(str);
  471. }
  472. /* define some specialist escape characters */
  473. if (escape_chars)
  474. clish_command__set_escape_chars(cmd, escape_chars);
  475. if (args_name) {
  476. /* define a "rest of line" argument */
  477. clish_param_t *param;
  478. /* Check syntax */
  479. if (!args_help) {
  480. fprintf(stderr, CLISH_XML_ERROR_ATTR("args_help"));
  481. goto error;
  482. }
  483. param = clish_param_new(args_name, args_help, "__ptype_ARGS");
  484. clish_command__set_args(cmd, param);
  485. }
  486. /* define the view which this command changes to */
  487. if (view) {
  488. /* reference the next view */
  489. clish_command__set_viewname(cmd, view);
  490. }
  491. /* define the view id which this command changes to */
  492. if (viewid)
  493. clish_command__set_viewid(cmd, viewid);
  494. #ifdef LEGACY
  495. action = clish_command__get_action(cmd);
  496. /* lock */
  497. if (lock) { // Don't change anything if lock is not specified
  498. if (lub_string_nocasecmp(lock, "false") == 0)
  499. clish_action__set_lock(action, BOOL_FALSE);
  500. else
  501. clish_action__set_lock(action, BOOL_TRUE);
  502. }
  503. /* interrupt */
  504. if (interrupt) { // Don't change anything if lock is not specified
  505. if (lub_string_nocasecmp(interrupt, "true") == 0)
  506. clish_action__set_interrupt(action, BOOL_TRUE);
  507. else
  508. clish_action__set_interrupt(action, BOOL_FALSE);
  509. }
  510. #endif
  511. if (access)
  512. clish_command__set_access(cmd, access);
  513. //process_command_end:
  514. res = process_children(shell, element, cmd);
  515. error:
  516. clish_xml_release(access);
  517. clish_xml_release(name);
  518. clish_xml_release(help);
  519. clish_xml_release(view);
  520. clish_xml_release(viewid);
  521. clish_xml_release(escape_chars);
  522. clish_xml_release(args_name);
  523. clish_xml_release(args_help);
  524. clish_xml_release(ref);
  525. #ifdef LEGACY
  526. clish_xml_release(lock);
  527. clish_xml_release(interrupt);
  528. #endif
  529. return res;
  530. }
  531. /* ------------------------------------------------------ */
  532. static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
  533. void *parent)
  534. {
  535. clish_view_t *v = (clish_view_t *) parent;
  536. clish_command_t *cmd = NULL;
  537. int res = -1;
  538. char *view = clish_xmlnode_fetch_attr(element, "view");
  539. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  540. char *default_shebang =
  541. clish_xmlnode_fetch_attr(element, "default_shebang");
  542. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  543. char *default_plugin = clish_xmlnode_fetch_attr(element,
  544. "default_plugin");
  545. #ifdef LEGACY
  546. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  547. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  548. clish_action_t *action;
  549. #endif
  550. /* Check syntax */
  551. if (!view) {
  552. fprintf(stderr, CLISH_XML_ERROR_ATTR("view"));
  553. goto error;
  554. }
  555. if (shell->startup) {
  556. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP tag duplication.\n");
  557. goto error;
  558. }
  559. /* create a command with NULL help */
  560. cmd = clish_view_new_command(v, "startup", NULL);
  561. clish_command__set_internal(cmd, BOOL_TRUE);
  562. /* reference the next view */
  563. clish_command__set_viewname(cmd, view);
  564. /* define the view id which this command changes to */
  565. if (viewid)
  566. clish_command__set_viewid(cmd, viewid);
  567. if (default_shebang)
  568. clish_shell__set_default_shebang(shell, default_shebang);
  569. if (timeout) {
  570. unsigned int to = 0;
  571. lub_conv_atoui(timeout, &to, 0);
  572. clish_shell__set_idle_timeout(shell, to);
  573. }
  574. #ifdef LEGACY
  575. action = clish_command__get_action(cmd);
  576. /* lock */
  577. if (lock) { // Don't change anything if lock is not specified
  578. if (lub_string_nocasecmp(lock, "false") == 0)
  579. clish_action__set_lock(action, BOOL_FALSE);
  580. else
  581. clish_action__set_lock(action, BOOL_TRUE);
  582. }
  583. /* interrupt */
  584. if (interrupt) { // Don't change anything if lock is not specified
  585. if (lub_string_nocasecmp(interrupt, "true") == 0)
  586. clish_action__set_interrupt(action, BOOL_TRUE);
  587. else
  588. clish_action__set_interrupt(action, BOOL_FALSE);
  589. }
  590. #endif
  591. /* If we need the default plugin */
  592. if (default_plugin && (0 == strcmp(default_plugin, "false")))
  593. shell->default_plugin = BOOL_FALSE;
  594. /* remember this command */
  595. shell->startup = cmd;
  596. res = process_children(shell, element, cmd);
  597. error:
  598. clish_xml_release(view);
  599. clish_xml_release(viewid);
  600. clish_xml_release(default_shebang);
  601. clish_xml_release(timeout);
  602. #ifdef LEGACY
  603. clish_xml_release(lock);
  604. clish_xml_release(interrupt);
  605. #endif
  606. return res;
  607. }
  608. /* ------------------------------------------------------ */
  609. static int process_param(clish_shell_t *shell, clish_xmlnode_t *element,
  610. void *parent)
  611. {
  612. clish_command_t *cmd = NULL;
  613. clish_param_t *p_param = NULL;
  614. clish_xmlnode_t *pelement;
  615. clish_param_t *param;
  616. char *pname;
  617. int res = -1;
  618. char *name = clish_xmlnode_fetch_attr(element, "name");
  619. char *help = clish_xmlnode_fetch_attr(element, "help");
  620. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  621. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  622. char *defval = clish_xmlnode_fetch_attr(element, "default");
  623. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  624. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  625. char *order = clish_xmlnode_fetch_attr(element, "order");
  626. char *value = clish_xmlnode_fetch_attr(element, "value");
  627. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  628. char *test = clish_xmlnode_fetch_attr(element, "test");
  629. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  630. char *access = clish_xmlnode_fetch_attr(element, "access");
  631. /* The PARAM can be child of COMMAND or another PARAM */
  632. pelement = clish_xmlnode_parent(element);
  633. pname = clish_xmlnode_get_all_name(pelement);
  634. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  635. p_param = (clish_param_t *)parent;
  636. else
  637. cmd = (clish_command_t *)parent;
  638. if (pname)
  639. free(pname);
  640. if (!cmd && !p_param)
  641. goto error;
  642. /* Check syntax */
  643. if (cmd && (cmd == shell->startup)) {
  644. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP can't contain PARAMs.\n");
  645. goto error;
  646. }
  647. if (!name) {
  648. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  649. goto error;
  650. }
  651. if (!help) {
  652. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  653. goto error;
  654. }
  655. if (!ptype) {
  656. fprintf(stderr, CLISH_XML_ERROR_ATTR("ptype"));
  657. goto error;
  658. }
  659. param = clish_param_new(name, help, ptype);
  660. /* If prefix is set clish will emulate old optional
  661. * command syntax over newer optional command mechanism.
  662. * It will create nested PARAM.
  663. */
  664. if (prefix) {
  665. const char *ptype_name = "__ptype_SUBCOMMAND";
  666. clish_param_t *opt_param = NULL;
  667. char *str = NULL;
  668. clish_ptype_t *tmp;
  669. /* Create a ptype for prefix-named subcommand that
  670. * will contain the nested optional parameter. The
  671. * name of ptype is hardcoded. It's not good but
  672. * it's only the service ptype.
  673. */
  674. tmp = clish_shell_find_create_ptype(shell,
  675. ptype_name, "Option", "[^\\\\]+",
  676. CLISH_PTYPE_METHOD_REGEXP, CLISH_PTYPE_PRE_NONE);
  677. assert(tmp);
  678. lub_string_cat(&str, "_prefix_");
  679. lub_string_cat(&str, name);
  680. opt_param = clish_param_new(str, help, ptype_name);
  681. lub_string_free(str);
  682. clish_param__set_mode(opt_param,
  683. CLISH_PARAM_SUBCOMMAND);
  684. clish_param__set_value(opt_param, prefix);
  685. clish_param__set_optional(opt_param, BOOL_TRUE);
  686. if (test)
  687. clish_param__set_test(opt_param, test);
  688. /* Add the parameter to the command */
  689. if (cmd)
  690. clish_command_insert_param(cmd, opt_param);
  691. /* Add the parameter to the param */
  692. if (p_param)
  693. clish_param_insert_param(p_param, opt_param);
  694. /* Unset cmd and set parent param to opt_param */
  695. cmd = NULL;
  696. p_param = opt_param;
  697. }
  698. if (defval)
  699. clish_param__set_defval(param, defval);
  700. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  701. clish_param__set_hidden(param, BOOL_TRUE);
  702. else
  703. clish_param__set_hidden(param, BOOL_FALSE);
  704. if (mode) {
  705. if (lub_string_nocasecmp(mode, "switch") == 0) {
  706. clish_param__set_mode(param,
  707. CLISH_PARAM_SWITCH);
  708. /* Force hidden attribute */
  709. clish_param__set_hidden(param, BOOL_TRUE);
  710. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  711. clish_param__set_mode(param,
  712. CLISH_PARAM_SUBCOMMAND);
  713. else
  714. clish_param__set_mode(param,
  715. CLISH_PARAM_COMMON);
  716. }
  717. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  718. clish_param__set_optional(param, BOOL_TRUE);
  719. else
  720. clish_param__set_optional(param, BOOL_FALSE);
  721. if (order && lub_string_nocasecmp(order, "true") == 0)
  722. clish_param__set_order(param, BOOL_TRUE);
  723. else
  724. clish_param__set_order(param, BOOL_FALSE);
  725. if (value) {
  726. clish_param__set_value(param, value);
  727. /* Force mode to subcommand */
  728. clish_param__set_mode(param,
  729. CLISH_PARAM_SUBCOMMAND);
  730. }
  731. if (test && !prefix)
  732. clish_param__set_test(param, test);
  733. if (completion)
  734. clish_param__set_completion(param, completion);
  735. if (access)
  736. clish_param__set_access(param, access);
  737. /* Add the parameter to the command */
  738. if (cmd)
  739. clish_command_insert_param(cmd, param);
  740. /* Add the parameter to the param */
  741. if (p_param)
  742. clish_param_insert_param(p_param, param);
  743. res = process_children(shell, element, param);
  744. error:
  745. clish_xml_release(name);
  746. clish_xml_release(help);
  747. clish_xml_release(ptype);
  748. clish_xml_release(prefix);
  749. clish_xml_release(defval);
  750. clish_xml_release(mode);
  751. clish_xml_release(optional);
  752. clish_xml_release(order);
  753. clish_xml_release(value);
  754. clish_xml_release(hidden);
  755. clish_xml_release(test);
  756. clish_xml_release(completion);
  757. clish_xml_release(access);
  758. return res;
  759. }
  760. /* ------------------------------------------------------ */
  761. static int process_action(clish_shell_t *shell, clish_xmlnode_t *element,
  762. void *parent)
  763. {
  764. clish_action_t *action = NULL;
  765. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  766. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  767. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  768. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  769. char *interactive = clish_xmlnode_fetch_attr(element, "interactive");
  770. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  771. char *pname = clish_xmlnode_get_all_name(pelement);
  772. char *text;
  773. clish_sym_t *sym = NULL;
  774. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  775. action = clish_var__get_action((clish_var_t *)parent);
  776. else if (pname && lub_string_nocasecmp(pname, "PTYPE") == 0)
  777. action = clish_ptype__get_action((clish_ptype_t *)parent);
  778. else
  779. action = clish_command__get_action((clish_command_t *)parent);
  780. if (pname)
  781. free(pname);
  782. text = clish_xmlnode_get_all_content(element);
  783. if (text && *text) {
  784. /* store the action */
  785. clish_action__set_script(action, text);
  786. }
  787. if (text)
  788. free(text);
  789. if (builtin)
  790. sym = clish_shell_add_unresolved_sym(shell, builtin,
  791. CLISH_SYM_TYPE_ACTION);
  792. else
  793. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  794. clish_action__set_builtin(action, sym);
  795. if (shebang)
  796. clish_action__set_shebang(action, shebang);
  797. /* lock */
  798. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  799. clish_action__set_lock(action, BOOL_FALSE);
  800. /* interrupt */
  801. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  802. clish_action__set_interrupt(action, BOOL_TRUE);
  803. clish_xml_release(builtin);
  804. clish_xml_release(shebang);
  805. clish_xml_release(lock);
  806. clish_xml_release(interrupt);
  807. clish_xml_release(interactive);
  808. return 0;
  809. }
  810. /* ------------------------------------------------------ */
  811. static int process_detail(clish_shell_t *shell, clish_xmlnode_t *element,
  812. void *parent)
  813. {
  814. clish_command_t *cmd = (clish_command_t *) parent;
  815. /* read the following text element */
  816. char *text = clish_xmlnode_get_all_content(element);
  817. if (text && *text) {
  818. /* store the action */
  819. clish_command__set_detail(cmd, text);
  820. }
  821. if (text)
  822. free(text);
  823. shell = shell; /* Happy compiler */
  824. return 0;
  825. }
  826. /* ------------------------------------------------------ */
  827. static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
  828. void *parent)
  829. {
  830. clish_view_t *v = (clish_view_t *)parent;
  831. clish_nspace_t *nspace = NULL;
  832. int res = -1;
  833. char *view = clish_xmlnode_fetch_attr(element, "ref");
  834. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  835. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  836. char *help = clish_xmlnode_fetch_attr(element, "help");
  837. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  838. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  839. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  840. char *access = clish_xmlnode_fetch_attr(element, "access");
  841. /* Check syntax */
  842. if (!view) {
  843. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  844. goto error;
  845. }
  846. clish_view_t *ref_view = clish_shell_find_view(shell, view);
  847. /* Don't include itself without prefix */
  848. if ((ref_view == v) && !prefix)
  849. goto process_namespace_end;
  850. nspace = clish_nspace_new(view);
  851. clish_view_insert_nspace(v, nspace);
  852. if (prefix) {
  853. clish_nspace__set_prefix(nspace, prefix);
  854. if (prefix_help)
  855. clish_nspace_create_prefix_cmd(nspace,
  856. "prefix",
  857. prefix_help);
  858. else
  859. clish_nspace_create_prefix_cmd(nspace,
  860. "prefix",
  861. "Prefix for the imported commands.");
  862. }
  863. if (help && lub_string_nocasecmp(help, "true") == 0)
  864. clish_nspace__set_help(nspace, BOOL_TRUE);
  865. else
  866. clish_nspace__set_help(nspace, BOOL_FALSE);
  867. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  868. clish_nspace__set_completion(nspace, BOOL_FALSE);
  869. else
  870. clish_nspace__set_completion(nspace, BOOL_TRUE);
  871. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  872. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  873. else
  874. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  875. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  876. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  877. else
  878. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  879. if (access)
  880. clish_nspace__set_access(nspace, access);
  881. process_namespace_end:
  882. res = 0;
  883. error:
  884. clish_xml_release(view);
  885. clish_xml_release(prefix);
  886. clish_xml_release(prefix_help);
  887. clish_xml_release(help);
  888. clish_xml_release(completion);
  889. clish_xml_release(context_help);
  890. clish_xml_release(inherit);
  891. clish_xml_release(access);
  892. return res;
  893. }
  894. /* ------------------------------------------------------ */
  895. static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
  896. void *parent)
  897. {
  898. clish_command_t *cmd = (clish_command_t *)parent;
  899. clish_config_t *config;
  900. if (!cmd)
  901. return 0;
  902. config = clish_command__get_config(cmd);
  903. /* read the following text element */
  904. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  905. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  906. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  907. char *file = clish_xmlnode_fetch_attr(element, "file");
  908. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  909. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  910. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  911. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  912. if (operation && !lub_string_nocasecmp(operation, "unset"))
  913. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  914. else if (operation && !lub_string_nocasecmp(operation, "none"))
  915. clish_config__set_op(config, CLISH_CONFIG_NONE);
  916. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  917. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  918. else {
  919. clish_config__set_op(config, CLISH_CONFIG_SET);
  920. /* The priority if no clearly specified */
  921. clish_config__set_priority(config, 0x7f00);
  922. }
  923. if (priority) {
  924. unsigned short pri = 0;
  925. lub_conv_atous(priority, &pri, 0);
  926. clish_config__set_priority(config, pri);
  927. }
  928. if (pattern)
  929. clish_config__set_pattern(config, pattern);
  930. else
  931. clish_config__set_pattern(config, "^${__cmd}");
  932. if (file)
  933. clish_config__set_file(config, file);
  934. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  935. clish_config__set_splitter(config, BOOL_FALSE);
  936. else
  937. clish_config__set_splitter(config, BOOL_TRUE);
  938. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  939. clish_config__set_unique(config, BOOL_FALSE);
  940. else
  941. clish_config__set_unique(config, BOOL_TRUE);
  942. if (seq)
  943. clish_config__set_seq(config, seq);
  944. else
  945. /* The entries without sequence cannot be non-unique */
  946. clish_config__set_unique(config, BOOL_TRUE);
  947. if (depth)
  948. clish_config__set_depth(config, depth);
  949. clish_xml_release(operation);
  950. clish_xml_release(priority);
  951. clish_xml_release(pattern);
  952. clish_xml_release(file);
  953. clish_xml_release(splitter);
  954. clish_xml_release(seq);
  955. clish_xml_release(unique);
  956. clish_xml_release(depth);
  957. shell = shell; /* Happy compiler */
  958. return 0;
  959. }
  960. /* ------------------------------------------------------ */
  961. static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
  962. void *parent)
  963. {
  964. clish_var_t *var = NULL;
  965. int res = -1;
  966. char *name = clish_xmlnode_fetch_attr(element, "name");
  967. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  968. char *value = clish_xmlnode_fetch_attr(element, "value");
  969. /* Check syntax */
  970. if (!name) {
  971. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  972. goto error;
  973. }
  974. /* Check if this var doesn't already exist */
  975. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  976. if (var) {
  977. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  978. goto error;
  979. }
  980. /* Create var instance */
  981. var = clish_var_new(name);
  982. lub_bintree_insert(&shell->var_tree, var);
  983. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  984. clish_var__set_dynamic(var, BOOL_TRUE);
  985. if (value)
  986. clish_var__set_value(var, value);
  987. res = process_children(shell, element, var);
  988. error:
  989. clish_xml_release(name);
  990. clish_xml_release(dynamic);
  991. clish_xml_release(value);
  992. parent = parent; /* Happy compiler */
  993. return res;
  994. }
  995. /* ------------------------------------------------------ */
  996. static int process_wdog(clish_shell_t *shell,
  997. clish_xmlnode_t *element, void *parent)
  998. {
  999. clish_view_t *v = (clish_view_t *)parent;
  1000. clish_command_t *cmd = NULL;
  1001. int res = -1;
  1002. /* Check syntax */
  1003. if (shell->wdog) {
  1004. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  1005. goto error;
  1006. }
  1007. /* Create a command with NULL help */
  1008. cmd = clish_view_new_command(v, "watchdog", NULL);
  1009. #ifdef LEGACY
  1010. // Legacy watchdog has lockless ACTION
  1011. clish_action__set_lock(clish_command__get_action(cmd), BOOL_FALSE);
  1012. #endif
  1013. /* Remember this command */
  1014. shell->wdog = cmd;
  1015. res = process_children(shell, element, cmd);
  1016. error:
  1017. return res;
  1018. }
  1019. /* ------------------------------------------------------ */
  1020. static int process_hotkey(clish_shell_t *shell, clish_xmlnode_t *element,
  1021. void *parent)
  1022. {
  1023. clish_view_t *v = (clish_view_t *)parent;
  1024. int res = -1;
  1025. char *key = clish_xmlnode_fetch_attr(element, "key");
  1026. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  1027. /* Check syntax */
  1028. if (!key) {
  1029. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  1030. goto error;
  1031. }
  1032. if (!cmd) {
  1033. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  1034. goto error;
  1035. }
  1036. clish_view_insert_hotkey(v, key, cmd);
  1037. res = 0;
  1038. error:
  1039. clish_xml_release(key);
  1040. clish_xml_release(cmd);
  1041. shell = shell; /* Happy compiler */
  1042. return res;
  1043. }
  1044. /* ------------------------------------------------------ */
  1045. static int process_plugin(clish_shell_t *shell, clish_xmlnode_t *element,
  1046. void *parent)
  1047. {
  1048. clish_plugin_t *plugin;
  1049. char *file = clish_xmlnode_fetch_attr(element, "file");
  1050. char *name = clish_xmlnode_fetch_attr(element, "name");
  1051. char *alias = clish_xmlnode_fetch_attr(element, "alias");
  1052. char *rtld_global = clish_xmlnode_fetch_attr(element, "rtld_global");
  1053. int res = -1;
  1054. char *text;
  1055. /* Check syntax */
  1056. if (!name) {
  1057. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1058. goto error;
  1059. }
  1060. plugin = clish_shell_find_plugin(shell, name);
  1061. if (plugin) {
  1062. fprintf(stderr,
  1063. CLISH_XML_ERROR_STR"PLUGIN %s duplication.\n", name);
  1064. goto error;
  1065. }
  1066. plugin = clish_shell_create_plugin(shell, name);
  1067. if (alias && *alias)
  1068. clish_plugin__set_alias(plugin, alias);
  1069. if (file && *file)
  1070. clish_plugin__set_file(plugin, file);
  1071. if (rtld_global && lub_string_nocasecmp(rtld_global, "true") == 0)
  1072. clish_plugin__set_rtld_global(plugin, BOOL_TRUE);
  1073. /* Get PLUGIN body content */
  1074. text = clish_xmlnode_get_all_content(element);
  1075. if (text && *text)
  1076. clish_plugin__set_conf(plugin, text);
  1077. if (text)
  1078. free(text);
  1079. res = 0;
  1080. error:
  1081. clish_xml_release(file);
  1082. clish_xml_release(name);
  1083. clish_xml_release(alias);
  1084. clish_xml_release(rtld_global);
  1085. parent = parent; /* Happy compiler */
  1086. return res;
  1087. }
  1088. /* ------------------------------------------------------ */
  1089. static int process_hook(clish_shell_t *shell, clish_xmlnode_t *element,
  1090. void *parent)
  1091. {
  1092. char *name = clish_xmlnode_fetch_attr(element, "name");
  1093. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1094. int res = -1;
  1095. int type = CLISH_SYM_TYPE_NONE;
  1096. /* Check syntax */
  1097. if (!name) {
  1098. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1099. goto error;
  1100. }
  1101. /* Find out HOOK type */
  1102. if (!strcmp(name, "action"))
  1103. type = CLISH_SYM_TYPE_ACTION;
  1104. else if (!strcmp(name, "access"))
  1105. type = CLISH_SYM_TYPE_ACCESS;
  1106. else if (!strcmp(name, "config"))
  1107. type = CLISH_SYM_TYPE_CONFIG;
  1108. else if (!strcmp(name, "log"))
  1109. type = CLISH_SYM_TYPE_LOG;
  1110. if (CLISH_SYM_TYPE_NONE == type) {
  1111. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1112. goto error;
  1113. }
  1114. /* Check duplicate HOOK tag */
  1115. if (shell->hooks_use[type]) {
  1116. fprintf(stderr,
  1117. CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
  1118. goto error;
  1119. }
  1120. shell->hooks_use[type] = BOOL_TRUE;
  1121. clish_sym__set_name(shell->hooks[type], builtin);
  1122. res = 0;
  1123. error:
  1124. clish_xml_release(name);
  1125. clish_xml_release(builtin);
  1126. parent = parent; /* Happy compiler */
  1127. return res;
  1128. }
  1129. /* ------------------------------------------------------ */