shell_xml.c 32 KB

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