1
0

shell_xml.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. clish_param_t *param;
  614. clish_ptype_t *tmp = NULL;
  615. /* Check syntax */
  616. if (cmd && (cmd == shell->startup)) {
  617. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP can't contain PARAMs.\n");
  618. goto error;
  619. }
  620. if (!name) {
  621. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  622. goto error;
  623. }
  624. if (!help) {
  625. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  626. goto error;
  627. }
  628. if (!ptype) {
  629. fprintf(stderr, CLISH_XML_ERROR_ATTR("ptype"));
  630. goto error;
  631. }
  632. if (*ptype) {
  633. tmp = clish_shell_find_create_ptype(shell, ptype,
  634. NULL, NULL,
  635. CLISH_PTYPE_REGEXP,
  636. CLISH_PTYPE_NONE);
  637. }
  638. param = clish_param_new(name, help, tmp);
  639. /* If prefix is set clish will emulate old optional
  640. * command syntax over newer optional command mechanism.
  641. * It will create nested PARAM.
  642. */
  643. if (prefix) {
  644. const char *ptype_name = "__SUBCOMMAND";
  645. clish_param_t *opt_param = NULL;
  646. char *str = NULL;
  647. /* Create a ptype for prefix-named subcommand that
  648. * will contain the nested optional parameter. The
  649. * name of ptype is hardcoded. It's not good but
  650. * it's only the service ptype.
  651. */
  652. tmp = (clish_ptype_t *)lub_bintree_find(
  653. &shell->ptype_tree, ptype_name);
  654. if (!tmp)
  655. tmp = clish_shell_find_create_ptype(shell,
  656. ptype_name, "Option", "[^\\\\]+",
  657. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  658. assert(tmp);
  659. lub_string_cat(&str, "_prefix_");
  660. lub_string_cat(&str, name);
  661. opt_param = clish_param_new(str, help, tmp);
  662. lub_string_free(str);
  663. clish_param__set_mode(opt_param,
  664. CLISH_PARAM_SUBCOMMAND);
  665. clish_param__set_value(opt_param, prefix);
  666. clish_param__set_optional(opt_param, BOOL_TRUE);
  667. if (test)
  668. clish_param__set_test(opt_param, test);
  669. /* add the parameter to the command */
  670. if (cmd)
  671. clish_command_insert_param(cmd, opt_param);
  672. /* add the parameter to the param */
  673. if (p_param)
  674. clish_param_insert_param(p_param, opt_param);
  675. /* Unset cmd and set parent param to opt_param */
  676. cmd = NULL;
  677. p_param = opt_param;
  678. }
  679. if (defval)
  680. clish_param__set_default(param, defval);
  681. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  682. clish_param__set_hidden(param, BOOL_TRUE);
  683. else
  684. clish_param__set_hidden(param, BOOL_FALSE);
  685. if (mode) {
  686. if (lub_string_nocasecmp(mode, "switch") == 0) {
  687. clish_param__set_mode(param,
  688. CLISH_PARAM_SWITCH);
  689. /* Force hidden attribute */
  690. clish_param__set_hidden(param, BOOL_TRUE);
  691. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  692. clish_param__set_mode(param,
  693. CLISH_PARAM_SUBCOMMAND);
  694. else
  695. clish_param__set_mode(param,
  696. CLISH_PARAM_COMMON);
  697. }
  698. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  699. clish_param__set_optional(param, BOOL_TRUE);
  700. else
  701. clish_param__set_optional(param, BOOL_FALSE);
  702. if (order && lub_string_nocasecmp(order, "true") == 0)
  703. clish_param__set_order(param, BOOL_TRUE);
  704. else
  705. clish_param__set_order(param, BOOL_FALSE);
  706. if (value) {
  707. clish_param__set_value(param, value);
  708. /* Force mode to subcommand */
  709. clish_param__set_mode(param,
  710. CLISH_PARAM_SUBCOMMAND);
  711. }
  712. if (test && !prefix)
  713. clish_param__set_test(param, test);
  714. if (completion)
  715. clish_param__set_completion(param, completion);
  716. /* add the parameter to the command */
  717. if (cmd)
  718. clish_command_insert_param(cmd, param);
  719. /* add the parameter to the param */
  720. if (p_param)
  721. clish_param_insert_param(p_param, param);
  722. res = process_children(shell, element, param);
  723. error:
  724. clish_xml_release(name);
  725. clish_xml_release(help);
  726. clish_xml_release(ptype);
  727. clish_xml_release(prefix);
  728. clish_xml_release(defval);
  729. clish_xml_release(mode);
  730. clish_xml_release(optional);
  731. clish_xml_release(order);
  732. clish_xml_release(value);
  733. clish_xml_release(hidden);
  734. clish_xml_release(test);
  735. clish_xml_release(completion);
  736. }
  737. return res;
  738. }
  739. /* ------------------------------------------------------ */
  740. static int process_action(clish_shell_t *shell, clish_xmlnode_t *element,
  741. void *parent)
  742. {
  743. clish_action_t *action = NULL;
  744. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  745. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  746. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  747. char *pname = clish_xmlnode_get_all_name(pelement);
  748. char *text;
  749. clish_sym_t *sym = NULL;
  750. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  751. action = clish_var__get_action((clish_var_t *)parent);
  752. else
  753. action = clish_command__get_action((clish_command_t *)parent);
  754. if (pname)
  755. free(pname);
  756. text = clish_xmlnode_get_all_content(element);
  757. if (text && *text) {
  758. /* store the action */
  759. clish_action__set_script(action, text);
  760. }
  761. if (text)
  762. free(text);
  763. if (builtin)
  764. sym = clish_shell_add_unresolved_sym(shell, builtin,
  765. CLISH_SYM_TYPE_ACTION);
  766. else
  767. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  768. clish_action__set_builtin(action, sym);
  769. if (shebang)
  770. clish_action__set_shebang(action, shebang);
  771. clish_xml_release(builtin);
  772. clish_xml_release(shebang);
  773. return 0;
  774. }
  775. /* ------------------------------------------------------ */
  776. static int process_detail(clish_shell_t *shell, clish_xmlnode_t *element,
  777. void *parent)
  778. {
  779. clish_command_t *cmd = (clish_command_t *) parent;
  780. /* read the following text element */
  781. char *text = clish_xmlnode_get_all_content(element);
  782. if (text && *text) {
  783. /* store the action */
  784. clish_command__set_detail(cmd, text);
  785. }
  786. if (text)
  787. free(text);
  788. shell = shell; /* Happy compiler */
  789. return 0;
  790. }
  791. /* ------------------------------------------------------ */
  792. static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
  793. void *parent)
  794. {
  795. clish_view_t *v = (clish_view_t *) parent;
  796. clish_nspace_t *nspace = NULL;
  797. int res = -1;
  798. char *view = clish_xmlnode_fetch_attr(element, "ref");
  799. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  800. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  801. char *help = clish_xmlnode_fetch_attr(element, "help");
  802. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  803. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  804. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  805. char *access = clish_xmlnode_fetch_attr(element, "access");
  806. /* Check syntax */
  807. if (!view) {
  808. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  809. goto error;
  810. }
  811. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  812. view, NULL);
  813. /* Don't include itself without prefix */
  814. if ((ref_view == v) && !prefix)
  815. goto process_namespace_end;
  816. nspace = clish_nspace_new(ref_view);
  817. clish_view_insert_nspace(v, nspace);
  818. if (prefix) {
  819. clish_nspace__set_prefix(nspace, prefix);
  820. if (prefix_help)
  821. clish_nspace_create_prefix_cmd(nspace,
  822. "prefix",
  823. prefix_help);
  824. else
  825. clish_nspace_create_prefix_cmd(nspace,
  826. "prefix",
  827. "Prefix for the imported commands.");
  828. }
  829. if (help && lub_string_nocasecmp(help, "true") == 0)
  830. clish_nspace__set_help(nspace, BOOL_TRUE);
  831. else
  832. clish_nspace__set_help(nspace, BOOL_FALSE);
  833. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  834. clish_nspace__set_completion(nspace, BOOL_FALSE);
  835. else
  836. clish_nspace__set_completion(nspace, BOOL_TRUE);
  837. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  838. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  839. else
  840. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  841. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  842. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  843. else
  844. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  845. if (access)
  846. clish_nspace__set_access(nspace, access);
  847. process_namespace_end:
  848. res = 0;
  849. error:
  850. clish_xml_release(view);
  851. clish_xml_release(prefix);
  852. clish_xml_release(prefix_help);
  853. clish_xml_release(help);
  854. clish_xml_release(completion);
  855. clish_xml_release(context_help);
  856. clish_xml_release(inherit);
  857. clish_xml_release(access);
  858. return res;
  859. }
  860. /* ------------------------------------------------------ */
  861. static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
  862. void *parent)
  863. {
  864. clish_command_t *cmd = (clish_command_t *)parent;
  865. clish_config_t *config;
  866. if (!cmd)
  867. return 0;
  868. config = clish_command__get_config(cmd);
  869. /* read the following text element */
  870. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  871. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  872. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  873. char *file = clish_xmlnode_fetch_attr(element, "file");
  874. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  875. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  876. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  877. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  878. if (operation && !lub_string_nocasecmp(operation, "unset"))
  879. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  880. else if (operation && !lub_string_nocasecmp(operation, "none"))
  881. clish_config__set_op(config, CLISH_CONFIG_NONE);
  882. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  883. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  884. else {
  885. clish_config__set_op(config, CLISH_CONFIG_SET);
  886. /* The priority if no clearly specified */
  887. clish_config__set_priority(config, 0x7f00);
  888. }
  889. if (priority) {
  890. long val = 0;
  891. char *endptr;
  892. unsigned short pri;
  893. val = strtol(priority, &endptr, 0);
  894. if (endptr == priority)
  895. pri = 0;
  896. else if (val > 0xffff)
  897. pri = 0xffff;
  898. else if (val < 0)
  899. pri = 0;
  900. else
  901. pri = (unsigned short)val;
  902. clish_config__set_priority(config, pri);
  903. }
  904. if (pattern)
  905. clish_config__set_pattern(config, pattern);
  906. else
  907. clish_config__set_pattern(config, "^${__cmd}");
  908. if (file)
  909. clish_config__set_file(config, file);
  910. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  911. clish_config__set_splitter(config, BOOL_FALSE);
  912. else
  913. clish_config__set_splitter(config, BOOL_TRUE);
  914. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  915. clish_config__set_unique(config, BOOL_FALSE);
  916. else
  917. clish_config__set_unique(config, BOOL_TRUE);
  918. if (seq)
  919. clish_config__set_seq(config, seq);
  920. else
  921. /* The entries without sequence cannot be non-unique */
  922. clish_config__set_unique(config, BOOL_TRUE);
  923. if (depth)
  924. clish_config__set_depth(config, depth);
  925. clish_xml_release(operation);
  926. clish_xml_release(priority);
  927. clish_xml_release(pattern);
  928. clish_xml_release(file);
  929. clish_xml_release(splitter);
  930. clish_xml_release(seq);
  931. clish_xml_release(unique);
  932. clish_xml_release(depth);
  933. shell = shell; /* Happy compiler */
  934. return 0;
  935. }
  936. /* ------------------------------------------------------ */
  937. static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
  938. void *parent)
  939. {
  940. clish_var_t *var = NULL;
  941. int res = -1;
  942. char *name = clish_xmlnode_fetch_attr(element, "name");
  943. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  944. char *value = clish_xmlnode_fetch_attr(element, "value");
  945. /* Check syntax */
  946. if (!name) {
  947. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  948. goto error;
  949. }
  950. /* Check if this var doesn't already exist */
  951. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  952. if (var) {
  953. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  954. goto error;
  955. }
  956. /* Create var instance */
  957. var = clish_var_new(name);
  958. lub_bintree_insert(&shell->var_tree, var);
  959. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  960. clish_var__set_dynamic(var, BOOL_TRUE);
  961. if (value)
  962. clish_var__set_value(var, value);
  963. res = process_children(shell, element, var);
  964. error:
  965. clish_xml_release(name);
  966. clish_xml_release(dynamic);
  967. clish_xml_release(value);
  968. parent = parent; /* Happy compiler */
  969. return res;
  970. }
  971. /* ------------------------------------------------------ */
  972. static int process_wdog(clish_shell_t *shell,
  973. clish_xmlnode_t *element, void *parent)
  974. {
  975. clish_view_t *v = (clish_view_t *)parent;
  976. clish_command_t *cmd = NULL;
  977. int res = -1;
  978. /* Check syntax */
  979. if (shell->wdog) {
  980. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  981. goto error;
  982. }
  983. /* Create a command with NULL help */
  984. cmd = clish_view_new_command(v, "watchdog", NULL);
  985. clish_command__set_lock(cmd, BOOL_FALSE);
  986. /* Remember this command */
  987. shell->wdog = cmd;
  988. res = process_children(shell, element, cmd);
  989. error:
  990. return res;
  991. }
  992. /* ------------------------------------------------------ */
  993. static int process_hotkey(clish_shell_t *shell, clish_xmlnode_t *element,
  994. void *parent)
  995. {
  996. clish_view_t *v = (clish_view_t *)parent;
  997. int res = -1;
  998. char *key = clish_xmlnode_fetch_attr(element, "key");
  999. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  1000. /* Check syntax */
  1001. if (!key) {
  1002. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  1003. goto error;
  1004. }
  1005. if (!cmd) {
  1006. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  1007. goto error;
  1008. }
  1009. clish_view_insert_hotkey(v, key, cmd);
  1010. res = 0;
  1011. error:
  1012. clish_xml_release(key);
  1013. clish_xml_release(cmd);
  1014. shell = shell; /* Happy compiler */
  1015. return res;
  1016. }
  1017. /* ------------------------------------------------------ */
  1018. static int process_plugin(clish_shell_t *shell, clish_xmlnode_t *element,
  1019. void *parent)
  1020. {
  1021. clish_plugin_t *plugin;
  1022. char *file = clish_xmlnode_fetch_attr(element, "file");
  1023. char *name = clish_xmlnode_fetch_attr(element, "name");
  1024. char *alias = clish_xmlnode_fetch_attr(element, "alias");
  1025. int res = -1;
  1026. char *text;
  1027. /* Check syntax */
  1028. if (!name) {
  1029. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1030. goto error;
  1031. }
  1032. plugin = clish_plugin_new(name);
  1033. lub_list_add(shell->plugins, plugin);
  1034. if (alias && *alias)
  1035. clish_plugin__set_alias(plugin, alias);
  1036. if (file && *file)
  1037. clish_plugin__set_file(plugin, file);
  1038. /* Get PLUGIN body content */
  1039. text = clish_xmlnode_get_all_content(element);
  1040. if (text && *text)
  1041. clish_plugin__set_conf(plugin, text);
  1042. if (text)
  1043. free(text);
  1044. res = 0;
  1045. error:
  1046. clish_xml_release(file);
  1047. clish_xml_release(name);
  1048. clish_xml_release(alias);
  1049. parent = parent; /* Happy compiler */
  1050. return res;
  1051. }
  1052. /* ------------------------------------------------------ */
  1053. static int process_hook(clish_shell_t *shell, clish_xmlnode_t *element,
  1054. void *parent)
  1055. {
  1056. char *name = clish_xmlnode_fetch_attr(element, "name");
  1057. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1058. int res = -1;
  1059. int type = CLISH_SYM_TYPE_NONE;
  1060. /* Check syntax */
  1061. if (!name) {
  1062. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1063. goto error;
  1064. }
  1065. /* Find out HOOK type */
  1066. if (!strcmp(name, "action"))
  1067. type = CLISH_SYM_TYPE_ACTION;
  1068. else if (!strcmp(name, "access"))
  1069. type = CLISH_SYM_TYPE_ACCESS;
  1070. else if (!strcmp(name, "config"))
  1071. type = CLISH_SYM_TYPE_CONFIG;
  1072. else if (!strcmp(name, "log"))
  1073. type = CLISH_SYM_TYPE_LOG;
  1074. if (CLISH_SYM_TYPE_NONE == type) {
  1075. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1076. goto error;
  1077. }
  1078. /* Check duplicate HOOK tag */
  1079. if (shell->hooks_use[type]) {
  1080. fprintf(stderr,
  1081. CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
  1082. goto error;
  1083. }
  1084. shell->hooks_use[type] = BOOL_TRUE;
  1085. clish_sym__set_name(shell->hooks[type], builtin);
  1086. res = 0;
  1087. error:
  1088. clish_xml_release(name);
  1089. clish_xml_release(builtin);
  1090. parent = parent; /* Happy compiler */
  1091. return res;
  1092. }
  1093. /* ------------------------------------------------------ */