1
0

shell_xml.c 33 KB

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