1
0

shell_xml.c 34 KB

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