1
0

shell_xml.c 34 KB

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