1
0

shell_xml.c 32 KB

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