shell_xml.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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 = NULL;
  82. char *dirname = NULL;
  83. char *saveptr = NULL;
  84. int res = -1;
  85. clish_xmldoc_t *doc = NULL;
  86. DIR *dir = NULL;
  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 = NULL;
  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. doc = NULL;
  171. /* Error message */
  172. if (r) {
  173. fprintf(stderr, CLISH_XML_ERROR_STR"File %s\n",
  174. filename);
  175. lub_string_free(filename);
  176. goto error;
  177. }
  178. lub_string_free(filename);
  179. }
  180. closedir(dir);
  181. dir = NULL;
  182. }
  183. /* To don't free memory twice on cleanup */
  184. #ifdef HAVE_LIB_LIBXSLT
  185. if (!xslt_path)
  186. xslt = NULL;
  187. #endif
  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. /* interactive */
  806. if (interactive && lub_string_nocasecmp(interactive, "true") == 0)
  807. clish_action__set_interactive(action, BOOL_TRUE);
  808. /* interrupt */
  809. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  810. clish_action__set_interrupt(action, BOOL_TRUE);
  811. /* expand */
  812. if (expand)
  813. clish_action__set_expand(action, lub_tri_from_string(expand));
  814. clish_xml_release(builtin);
  815. clish_xml_release(shebang);
  816. clish_xml_release(lock);
  817. clish_xml_release(interrupt);
  818. clish_xml_release(interactive);
  819. clish_xml_release(expand);
  820. return 0;
  821. }
  822. /* ------------------------------------------------------ */
  823. static int process_detail(clish_shell_t *shell, clish_xmlnode_t *element,
  824. void *parent)
  825. {
  826. clish_command_t *cmd = (clish_command_t *) parent;
  827. /* read the following text element */
  828. char *text = clish_xmlnode_get_all_content(element);
  829. if (text && *text) {
  830. /* store the action */
  831. clish_command__set_detail(cmd, text);
  832. }
  833. if (text)
  834. free(text);
  835. shell = shell; /* Happy compiler */
  836. return 0;
  837. }
  838. /* ------------------------------------------------------ */
  839. static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
  840. void *parent)
  841. {
  842. clish_view_t *v = (clish_view_t *)parent;
  843. clish_nspace_t *nspace = NULL;
  844. int res = -1;
  845. char *view = clish_xmlnode_fetch_attr(element, "ref");
  846. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  847. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  848. char *help = clish_xmlnode_fetch_attr(element, "help");
  849. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  850. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  851. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  852. char *access = clish_xmlnode_fetch_attr(element, "access");
  853. /* Check syntax */
  854. if (!view) {
  855. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  856. goto error;
  857. }
  858. clish_view_t *ref_view = clish_shell_find_view(shell, view);
  859. /* Don't include itself without prefix */
  860. if ((ref_view == v) && !prefix)
  861. goto process_namespace_end;
  862. nspace = clish_nspace_new(view);
  863. clish_view_insert_nspace(v, nspace);
  864. if (prefix) {
  865. clish_nspace__set_prefix(nspace, prefix);
  866. if (prefix_help)
  867. clish_nspace_create_prefix_cmd(nspace,
  868. "prefix",
  869. prefix_help);
  870. else
  871. clish_nspace_create_prefix_cmd(nspace,
  872. "prefix",
  873. "Prefix for imported commands.");
  874. }
  875. if (help && lub_string_nocasecmp(help, "true") == 0)
  876. clish_nspace__set_help(nspace, BOOL_TRUE);
  877. else
  878. clish_nspace__set_help(nspace, BOOL_FALSE);
  879. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  880. clish_nspace__set_completion(nspace, BOOL_FALSE);
  881. else
  882. clish_nspace__set_completion(nspace, BOOL_TRUE);
  883. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  884. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  885. else
  886. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  887. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  888. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  889. else
  890. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  891. if (access)
  892. clish_nspace__set_access(nspace, access);
  893. process_namespace_end:
  894. res = 0;
  895. error:
  896. clish_xml_release(view);
  897. clish_xml_release(prefix);
  898. clish_xml_release(prefix_help);
  899. clish_xml_release(help);
  900. clish_xml_release(completion);
  901. clish_xml_release(context_help);
  902. clish_xml_release(inherit);
  903. clish_xml_release(access);
  904. return res;
  905. }
  906. /* ------------------------------------------------------ */
  907. static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
  908. void *parent)
  909. {
  910. clish_command_t *cmd = (clish_command_t *)parent;
  911. clish_config_t *config;
  912. if (!cmd)
  913. return 0;
  914. config = clish_command__get_config(cmd);
  915. /* read the following text element */
  916. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  917. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  918. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  919. char *file = clish_xmlnode_fetch_attr(element, "file");
  920. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  921. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  922. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  923. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  924. if (operation && !lub_string_nocasecmp(operation, "unset"))
  925. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  926. else if (operation && !lub_string_nocasecmp(operation, "none"))
  927. clish_config__set_op(config, CLISH_CONFIG_NONE);
  928. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  929. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  930. else {
  931. clish_config__set_op(config, CLISH_CONFIG_SET);
  932. /* The priority if no clearly specified */
  933. clish_config__set_priority(config, 0x7f00);
  934. }
  935. if (priority) {
  936. unsigned short pri = 0;
  937. lub_conv_atous(priority, &pri, 0);
  938. clish_config__set_priority(config, pri);
  939. }
  940. if (pattern)
  941. clish_config__set_pattern(config, pattern);
  942. else
  943. clish_config__set_pattern(config, "^${__cmd}");
  944. if (file)
  945. clish_config__set_file(config, file);
  946. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  947. clish_config__set_splitter(config, BOOL_FALSE);
  948. else
  949. clish_config__set_splitter(config, BOOL_TRUE);
  950. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  951. clish_config__set_unique(config, BOOL_FALSE);
  952. else
  953. clish_config__set_unique(config, BOOL_TRUE);
  954. if (seq)
  955. clish_config__set_seq(config, seq);
  956. else
  957. /* The entries without sequence cannot be non-unique */
  958. clish_config__set_unique(config, BOOL_TRUE);
  959. if (depth)
  960. clish_config__set_depth(config, depth);
  961. clish_xml_release(operation);
  962. clish_xml_release(priority);
  963. clish_xml_release(pattern);
  964. clish_xml_release(file);
  965. clish_xml_release(splitter);
  966. clish_xml_release(seq);
  967. clish_xml_release(unique);
  968. clish_xml_release(depth);
  969. shell = shell; /* Happy compiler */
  970. return 0;
  971. }
  972. /* ------------------------------------------------------ */
  973. static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
  974. void *parent)
  975. {
  976. clish_var_t *var = NULL;
  977. int res = -1;
  978. char *name = clish_xmlnode_fetch_attr(element, "name");
  979. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  980. char *value = clish_xmlnode_fetch_attr(element, "value");
  981. /* Check syntax */
  982. if (!name) {
  983. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  984. goto error;
  985. }
  986. /* Check if this var doesn't already exist */
  987. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  988. if (var) {
  989. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  990. goto error;
  991. }
  992. /* Create var instance */
  993. var = clish_var_new(name);
  994. lub_bintree_insert(&shell->var_tree, var);
  995. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  996. clish_var__set_dynamic(var, BOOL_TRUE);
  997. if (value)
  998. clish_var__set_value(var, value);
  999. res = process_children(shell, element, var);
  1000. error:
  1001. clish_xml_release(name);
  1002. clish_xml_release(dynamic);
  1003. clish_xml_release(value);
  1004. parent = parent; /* Happy compiler */
  1005. return res;
  1006. }
  1007. /* ------------------------------------------------------ */
  1008. static int process_wdog(clish_shell_t *shell,
  1009. clish_xmlnode_t *element, void *parent)
  1010. {
  1011. clish_command_t *cmd = NULL;
  1012. int res = -1;
  1013. /* Check syntax */
  1014. if (shell->wdog) {
  1015. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  1016. goto error;
  1017. }
  1018. /* Create a command with NULL help */
  1019. cmd = clish_command_new("watchdog", NULL);
  1020. #ifdef LEGACY
  1021. // Legacy watchdog has lockless ACTION
  1022. clish_action__set_lock(clish_command__get_action(cmd), BOOL_FALSE);
  1023. #endif
  1024. /* Remember this command */
  1025. shell->wdog = cmd;
  1026. res = process_children(shell, element, cmd);
  1027. error:
  1028. parent = parent; /* Happy compiler */
  1029. return res;
  1030. }
  1031. /* ------------------------------------------------------ */
  1032. static int process_hotkey(clish_shell_t *shell, clish_xmlnode_t *element,
  1033. void *parent)
  1034. {
  1035. clish_view_t *v = (clish_view_t *)parent;
  1036. int res = -1;
  1037. char *key = clish_xmlnode_fetch_attr(element, "key");
  1038. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  1039. /* Check syntax */
  1040. if (!key) {
  1041. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  1042. goto error;
  1043. }
  1044. if (!cmd) {
  1045. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  1046. goto error;
  1047. }
  1048. clish_view_insert_hotkey(v, key, cmd);
  1049. res = 0;
  1050. error:
  1051. clish_xml_release(key);
  1052. clish_xml_release(cmd);
  1053. shell = shell; /* Happy compiler */
  1054. return res;
  1055. }
  1056. /* ------------------------------------------------------ */
  1057. static int process_plugin(clish_shell_t *shell, clish_xmlnode_t *element,
  1058. void *parent)
  1059. {
  1060. clish_plugin_t *plugin;
  1061. char *file = clish_xmlnode_fetch_attr(element, "file");
  1062. char *name = clish_xmlnode_fetch_attr(element, "name");
  1063. char *alias = clish_xmlnode_fetch_attr(element, "alias");
  1064. char *rtld_global = clish_xmlnode_fetch_attr(element, "rtld_global");
  1065. int res = -1;
  1066. char *text;
  1067. /* Check syntax */
  1068. if (!name) {
  1069. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1070. goto error;
  1071. }
  1072. plugin = clish_shell_find_plugin(shell, name);
  1073. if (plugin) {
  1074. fprintf(stderr,
  1075. CLISH_XML_ERROR_STR"PLUGIN %s duplication.\n", name);
  1076. goto error;
  1077. }
  1078. plugin = clish_shell_create_plugin(shell, name);
  1079. if (alias && *alias)
  1080. clish_plugin__set_alias(plugin, alias);
  1081. if (file && *file)
  1082. clish_plugin__set_file(plugin, file);
  1083. if (rtld_global && lub_string_nocasecmp(rtld_global, "true") == 0)
  1084. clish_plugin__set_rtld_global(plugin, BOOL_TRUE);
  1085. /* Get PLUGIN body content */
  1086. text = clish_xmlnode_get_all_content(element);
  1087. if (text && *text)
  1088. clish_plugin__set_conf(plugin, text);
  1089. if (text)
  1090. free(text);
  1091. res = 0;
  1092. error:
  1093. clish_xml_release(file);
  1094. clish_xml_release(name);
  1095. clish_xml_release(alias);
  1096. clish_xml_release(rtld_global);
  1097. parent = parent; /* Happy compiler */
  1098. return res;
  1099. }
  1100. /* ------------------------------------------------------ */
  1101. static int process_hook(clish_shell_t *shell, clish_xmlnode_t *element,
  1102. void *parent)
  1103. {
  1104. char *name = clish_xmlnode_fetch_attr(element, "name");
  1105. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1106. int res = -1;
  1107. int type = CLISH_SYM_TYPE_NONE;
  1108. /* Check syntax */
  1109. if (!name) {
  1110. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1111. goto error;
  1112. }
  1113. /* Find out HOOK type */
  1114. if (!strcmp(name, "action"))
  1115. type = CLISH_SYM_TYPE_ACTION;
  1116. else if (!strcmp(name, "access"))
  1117. type = CLISH_SYM_TYPE_ACCESS;
  1118. else if (!strcmp(name, "config"))
  1119. type = CLISH_SYM_TYPE_CONFIG;
  1120. else if (!strcmp(name, "log"))
  1121. type = CLISH_SYM_TYPE_LOG;
  1122. if (CLISH_SYM_TYPE_NONE == type) {
  1123. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1124. goto error;
  1125. }
  1126. /* Check duplicate HOOK tag */
  1127. if (shell->hooks_use[type]) {
  1128. fprintf(stderr,
  1129. CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
  1130. goto error;
  1131. }
  1132. shell->hooks_use[type] = BOOL_TRUE;
  1133. clish_sym__set_name(shell->hooks[type], builtin);
  1134. res = 0;
  1135. error:
  1136. clish_xml_release(name);
  1137. clish_xml_release(builtin);
  1138. parent = parent; /* Happy compiler */
  1139. return res;
  1140. }
  1141. /* ------------------------------------------------------ */