shell_xml.c 36 KB

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