PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cocos2dx/platform/third_party/marmalade/libxml2/sources/xmlcatalog.c

https://bitbucket.org/festevezga/cocos2d-x
C | 614 lines | 564 code | 18 blank | 32 comment | 67 complexity | f7a746be8cd8e527a8c4bb980c9fc271 MD5 | raw file
  1. /*
  2. * xmlcatalog.c : a small utility program to handle XML catalogs
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #include "libxml.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #ifdef HAVE_STDLIB_H
  13. #include <stdlib.h>
  14. #endif
  15. #ifdef HAVE_LIBREADLINE
  16. #include <readline/readline.h>
  17. #ifdef HAVE_LIBHISTORY
  18. #include <readline/history.h>
  19. #endif
  20. #endif
  21. #include <libxml/xmlmemory.h>
  22. #include <libxml/uri.h>
  23. #include <libxml/catalog.h>
  24. #include <libxml/parser.h>
  25. #include <libxml/globals.h>
  26. #if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
  27. static int shell = 0;
  28. static int sgml = 0;
  29. static int noout = 0;
  30. static int create = 0;
  31. static int add = 0;
  32. static int del = 0;
  33. static int convert = 0;
  34. static int no_super_update = 0;
  35. static int verbose = 0;
  36. static char *filename = NULL;
  37. #ifndef XML_SGML_DEFAULT_CATALOG
  38. #define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
  39. #endif
  40. /************************************************************************
  41. * *
  42. * Shell Interface *
  43. * *
  44. ************************************************************************/
  45. /**
  46. * xmlShellReadline:
  47. * @prompt: the prompt value
  48. *
  49. * Read a string
  50. *
  51. * Returns a pointer to it or NULL on EOF the caller is expected to
  52. * free the returned string.
  53. */
  54. static char *
  55. xmlShellReadline(const char *prompt) {
  56. #ifdef HAVE_LIBREADLINE
  57. char *line_read;
  58. /* Get a line from the user. */
  59. line_read = readline (prompt);
  60. /* If the line has any text in it, save it on the history. */
  61. if (line_read && *line_read)
  62. add_history (line_read);
  63. return (line_read);
  64. #else
  65. char line_read[501];
  66. char *ret;
  67. int len;
  68. if (prompt != NULL)
  69. fprintf(stdout, "%s", prompt);
  70. if (!fgets(line_read, 500, stdin))
  71. return(NULL);
  72. line_read[500] = 0;
  73. len = strlen(line_read);
  74. ret = (char *) malloc(len + 1);
  75. if (ret != NULL) {
  76. memcpy (ret, line_read, len + 1);
  77. }
  78. return(ret);
  79. #endif
  80. }
  81. static void usershell(void) {
  82. char *cmdline = NULL, *cur;
  83. int nbargs;
  84. char command[100];
  85. char arg[400];
  86. char *argv[20];
  87. int i, ret;
  88. xmlChar *ans;
  89. while (1) {
  90. cmdline = xmlShellReadline("> ");
  91. if (cmdline == NULL)
  92. return;
  93. /*
  94. * Parse the command itself
  95. */
  96. cur = cmdline;
  97. nbargs = 0;
  98. while ((*cur == ' ') || (*cur == '\t')) cur++;
  99. i = 0;
  100. while ((*cur != ' ') && (*cur != '\t') &&
  101. (*cur != '\n') && (*cur != '\r')) {
  102. if (*cur == 0)
  103. break;
  104. command[i++] = *cur++;
  105. }
  106. command[i] = 0;
  107. if (i == 0) {
  108. free(cmdline);
  109. continue;
  110. }
  111. /*
  112. * Parse the argument string
  113. */
  114. memset(arg, 0, sizeof(arg));
  115. while ((*cur == ' ') || (*cur == '\t')) cur++;
  116. i = 0;
  117. while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
  118. if (*cur == 0)
  119. break;
  120. arg[i++] = *cur++;
  121. }
  122. arg[i] = 0;
  123. /*
  124. * Parse the arguments
  125. */
  126. i = 0;
  127. nbargs = 0;
  128. cur = arg;
  129. memset(argv, 0, sizeof(argv));
  130. while (*cur != 0) {
  131. while ((*cur == ' ') || (*cur == '\t')) cur++;
  132. if (*cur == '\'') {
  133. cur++;
  134. argv[i] = cur;
  135. while ((*cur != 0) && (*cur != '\'')) cur++;
  136. if (*cur == '\'') {
  137. *cur = 0;
  138. nbargs++;
  139. i++;
  140. cur++;
  141. }
  142. } else if (*cur == '"') {
  143. cur++;
  144. argv[i] = cur;
  145. while ((*cur != 0) && (*cur != '"')) cur++;
  146. if (*cur == '"') {
  147. *cur = 0;
  148. nbargs++;
  149. i++;
  150. cur++;
  151. }
  152. } else {
  153. argv[i] = cur;
  154. while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
  155. cur++;
  156. *cur = 0;
  157. nbargs++;
  158. i++;
  159. cur++;
  160. }
  161. }
  162. /*
  163. * start interpreting the command
  164. */
  165. if (!strcmp(command, "exit"))
  166. break;
  167. if (!strcmp(command, "quit"))
  168. break;
  169. if (!strcmp(command, "bye"))
  170. break;
  171. if (!strcmp(command, "public")) {
  172. if (nbargs != 1) {
  173. printf("public requires 1 arguments\n");
  174. } else {
  175. ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
  176. if (ans == NULL) {
  177. printf("No entry for PUBLIC %s\n", argv[0]);
  178. } else {
  179. printf("%s\n", (char *) ans);
  180. xmlFree(ans);
  181. }
  182. }
  183. } else if (!strcmp(command, "system")) {
  184. if (nbargs != 1) {
  185. printf("system requires 1 arguments\n");
  186. } else {
  187. ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
  188. if (ans == NULL) {
  189. printf("No entry for SYSTEM %s\n", argv[0]);
  190. } else {
  191. printf("%s\n", (char *) ans);
  192. xmlFree(ans);
  193. }
  194. }
  195. } else if (!strcmp(command, "add")) {
  196. if (sgml) {
  197. if ((nbargs != 3) && (nbargs != 2)) {
  198. printf("add requires 2 or 3 arguments\n");
  199. } else {
  200. if (argv[2] == NULL)
  201. ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
  202. BAD_CAST argv[1]);
  203. else
  204. ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
  205. BAD_CAST argv[2]);
  206. if (ret != 0)
  207. printf("add command failed\n");
  208. }
  209. } else {
  210. if ((nbargs != 3) && (nbargs != 2)) {
  211. printf("add requires 2 or 3 arguments\n");
  212. } else {
  213. if (argv[2] == NULL)
  214. ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
  215. BAD_CAST argv[1]);
  216. else
  217. ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
  218. BAD_CAST argv[2]);
  219. if (ret != 0)
  220. printf("add command failed\n");
  221. }
  222. }
  223. } else if (!strcmp(command, "del")) {
  224. if (nbargs != 1) {
  225. printf("del requires 1\n");
  226. } else {
  227. ret = xmlCatalogRemove(BAD_CAST argv[0]);
  228. if (ret <= 0)
  229. printf("del command failed\n");
  230. }
  231. } else if (!strcmp(command, "resolve")) {
  232. if (nbargs != 2) {
  233. printf("resolve requires 2 arguments\n");
  234. } else {
  235. ans = xmlCatalogResolve(BAD_CAST argv[0],
  236. BAD_CAST argv[1]);
  237. if (ans == NULL) {
  238. printf("Resolver failed to find an answer\n");
  239. } else {
  240. printf("%s\n", (char *) ans);
  241. xmlFree(ans);
  242. }
  243. }
  244. } else if (!strcmp(command, "dump")) {
  245. if (nbargs != 0) {
  246. printf("dump has no arguments\n");
  247. } else {
  248. xmlCatalogDump(stdout);
  249. }
  250. } else if (!strcmp(command, "debug")) {
  251. if (nbargs != 0) {
  252. printf("debug has no arguments\n");
  253. } else {
  254. verbose++;
  255. xmlCatalogSetDebug(verbose);
  256. }
  257. } else if (!strcmp(command, "quiet")) {
  258. if (nbargs != 0) {
  259. printf("quiet has no arguments\n");
  260. } else {
  261. if (verbose > 0)
  262. verbose--;
  263. xmlCatalogSetDebug(verbose);
  264. }
  265. } else {
  266. if (strcmp(command, "help")) {
  267. printf("Unrecognized command %s\n", command);
  268. }
  269. printf("Commands available:\n");
  270. printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
  271. printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
  272. printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
  273. printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
  274. printf("\tdel 'values' : remove values\n");
  275. printf("\tdump: print the current catalog state\n");
  276. printf("\tdebug: increase the verbosity level\n");
  277. printf("\tquiet: decrease the verbosity level\n");
  278. printf("\texit: quit the shell\n");
  279. }
  280. free(cmdline); /* not xmlFree here ! */
  281. }
  282. }
  283. /************************************************************************
  284. * *
  285. * Main *
  286. * *
  287. ************************************************************************/
  288. static void usage(const char *name) {
  289. /* split into 2 printf's to avoid overly long string (gcc warning) */
  290. printf("\
  291. Usage : %s [options] catalogfile entities...\n\
  292. \tParse the catalog file and query it for the entities\n\
  293. \t--sgml : handle SGML Super catalogs for --add and --del\n\
  294. \t--shell : run a shell allowing interactive queries\n\
  295. \t--create : create a new catalog\n\
  296. \t--add 'type' 'orig' 'replace' : add an XML entry\n\
  297. \t--add 'entry' : add an SGML entry\n", name);
  298. printf("\
  299. \t--del 'values' : remove values\n\
  300. \t--noout: avoid dumping the result on stdout\n\
  301. \t used with --add or --del, it saves the catalog changes\n\
  302. \t and with --sgml it automatically updates the super catalog\n\
  303. \t--no-super-update: do not update the SGML super catalog\n\
  304. \t-v --verbose : provide debug informations\n");
  305. }
  306. int main(int argc, char **argv) {
  307. int i;
  308. int ret;
  309. int exit_value = 0;
  310. if (argc <= 1) {
  311. usage(argv[0]);
  312. return(1);
  313. }
  314. LIBXML_TEST_VERSION
  315. for (i = 1; i < argc ; i++) {
  316. if (!strcmp(argv[i], "-"))
  317. break;
  318. if (argv[i][0] != '-')
  319. break;
  320. if ((!strcmp(argv[i], "-verbose")) ||
  321. (!strcmp(argv[i], "-v")) ||
  322. (!strcmp(argv[i], "--verbose"))) {
  323. verbose++;
  324. xmlCatalogSetDebug(verbose);
  325. } else if ((!strcmp(argv[i], "-noout")) ||
  326. (!strcmp(argv[i], "--noout"))) {
  327. noout = 1;
  328. } else if ((!strcmp(argv[i], "-shell")) ||
  329. (!strcmp(argv[i], "--shell"))) {
  330. shell++;
  331. noout = 1;
  332. } else if ((!strcmp(argv[i], "-sgml")) ||
  333. (!strcmp(argv[i], "--sgml"))) {
  334. sgml++;
  335. } else if ((!strcmp(argv[i], "-create")) ||
  336. (!strcmp(argv[i], "--create"))) {
  337. create++;
  338. } else if ((!strcmp(argv[i], "-convert")) ||
  339. (!strcmp(argv[i], "--convert"))) {
  340. convert++;
  341. } else if ((!strcmp(argv[i], "-no-super-update")) ||
  342. (!strcmp(argv[i], "--no-super-update"))) {
  343. no_super_update++;
  344. } else if ((!strcmp(argv[i], "-add")) ||
  345. (!strcmp(argv[i], "--add"))) {
  346. if (sgml)
  347. i += 2;
  348. else
  349. i += 3;
  350. add++;
  351. } else if ((!strcmp(argv[i], "-del")) ||
  352. (!strcmp(argv[i], "--del"))) {
  353. i += 1;
  354. del++;
  355. } else {
  356. fprintf(stderr, "Unknown option %s\n", argv[i]);
  357. usage(argv[0]);
  358. return(1);
  359. }
  360. }
  361. for (i = 1; i < argc; i++) {
  362. if ((!strcmp(argv[i], "-add")) ||
  363. (!strcmp(argv[i], "--add"))) {
  364. if (sgml)
  365. i += 2;
  366. else
  367. i += 3;
  368. continue;
  369. } else if ((!strcmp(argv[i], "-del")) ||
  370. (!strcmp(argv[i], "--del"))) {
  371. i += 1;
  372. /* No catalog entry specified */
  373. if (i == argc || (sgml && i + 1 == argc)) {
  374. fprintf(stderr, "No catalog entry specified to remove from\n");
  375. usage (argv[0]);
  376. return(1);
  377. }
  378. continue;
  379. } else if (argv[i][0] == '-')
  380. continue;
  381. filename = argv[i];
  382. ret = xmlLoadCatalog(argv[i]);
  383. if ((ret < 0) && (create)) {
  384. xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
  385. }
  386. break;
  387. }
  388. if (convert)
  389. ret = xmlCatalogConvert();
  390. if ((add) || (del)) {
  391. for (i = 1; i < argc ; i++) {
  392. if (!strcmp(argv[i], "-"))
  393. break;
  394. if (argv[i][0] != '-')
  395. continue;
  396. if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
  397. strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
  398. continue;
  399. if (sgml) {
  400. /*
  401. * Maintenance of SGML catalogs.
  402. */
  403. xmlCatalogPtr catal = NULL;
  404. xmlCatalogPtr super = NULL;
  405. catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
  406. if ((!strcmp(argv[i], "-add")) ||
  407. (!strcmp(argv[i], "--add"))) {
  408. if (catal == NULL)
  409. catal = xmlNewCatalog(1);
  410. xmlACatalogAdd(catal, BAD_CAST "CATALOG",
  411. BAD_CAST argv[i + 2], NULL);
  412. if (!no_super_update) {
  413. super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
  414. if (super == NULL)
  415. super = xmlNewCatalog(1);
  416. xmlACatalogAdd(super, BAD_CAST "CATALOG",
  417. BAD_CAST argv[i + 1], NULL);
  418. }
  419. } else {
  420. if (catal != NULL)
  421. ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
  422. else
  423. ret = -1;
  424. if (ret < 0) {
  425. fprintf(stderr, "Failed to remove entry from %s\n",
  426. argv[i + 1]);
  427. exit_value = 1;
  428. }
  429. if ((!no_super_update) && (noout) && (catal != NULL) &&
  430. (xmlCatalogIsEmpty(catal))) {
  431. super = xmlLoadSGMLSuperCatalog(
  432. XML_SGML_DEFAULT_CATALOG);
  433. if (super != NULL) {
  434. ret = xmlACatalogRemove(super,
  435. BAD_CAST argv[i + 1]);
  436. if (ret < 0) {
  437. fprintf(stderr,
  438. "Failed to remove entry from %s\n",
  439. XML_SGML_DEFAULT_CATALOG);
  440. exit_value = 1;
  441. }
  442. }
  443. }
  444. }
  445. if (noout) {
  446. FILE *out;
  447. if (xmlCatalogIsEmpty(catal)) {
  448. remove(argv[i + 1]);
  449. } else {
  450. out = fopen(argv[i + 1], "w");
  451. if (out == NULL) {
  452. fprintf(stderr, "could not open %s for saving\n",
  453. argv[i + 1]);
  454. exit_value = 2;
  455. noout = 0;
  456. } else {
  457. xmlACatalogDump(catal, out);
  458. fclose(out);
  459. }
  460. }
  461. if (!no_super_update && super != NULL) {
  462. if (xmlCatalogIsEmpty(super)) {
  463. remove(XML_SGML_DEFAULT_CATALOG);
  464. } else {
  465. out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
  466. if (out == NULL) {
  467. fprintf(stderr,
  468. "could not open %s for saving\n",
  469. XML_SGML_DEFAULT_CATALOG);
  470. exit_value = 2;
  471. noout = 0;
  472. } else {
  473. xmlACatalogDump(super, out);
  474. fclose(out);
  475. }
  476. }
  477. }
  478. } else {
  479. xmlACatalogDump(catal, stdout);
  480. }
  481. i += 2;
  482. } else {
  483. if ((!strcmp(argv[i], "-add")) ||
  484. (!strcmp(argv[i], "--add"))) {
  485. if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
  486. ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
  487. BAD_CAST argv[i + 2]);
  488. else
  489. ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
  490. BAD_CAST argv[i + 2],
  491. BAD_CAST argv[i + 3]);
  492. if (ret != 0) {
  493. printf("add command failed\n");
  494. exit_value = 3;
  495. }
  496. i += 3;
  497. } else if ((!strcmp(argv[i], "-del")) ||
  498. (!strcmp(argv[i], "--del"))) {
  499. ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
  500. if (ret < 0) {
  501. fprintf(stderr, "Failed to remove entry %s\n",
  502. argv[i + 1]);
  503. exit_value = 1;
  504. }
  505. i += 1;
  506. }
  507. }
  508. }
  509. } else if (shell) {
  510. usershell();
  511. } else {
  512. for (i++; i < argc; i++) {
  513. xmlURIPtr uri;
  514. xmlChar *ans;
  515. uri = xmlParseURI(argv[i]);
  516. if (uri == NULL) {
  517. ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
  518. if (ans == NULL) {
  519. printf("No entry for PUBLIC %s\n", argv[i]);
  520. exit_value = 4;
  521. } else {
  522. printf("%s\n", (char *) ans);
  523. xmlFree(ans);
  524. }
  525. } else {
  526. xmlFreeURI(uri);
  527. ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
  528. if (ans == NULL) {
  529. printf("No entry for SYSTEM %s\n", argv[i]);
  530. ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
  531. if (ans == NULL) {
  532. printf ("No entry for URI %s\n", argv[i]);
  533. exit_value = 4;
  534. } else {
  535. printf("%s\n", (char *) ans);
  536. xmlFree (ans);
  537. }
  538. } else {
  539. printf("%s\n", (char *) ans);
  540. xmlFree(ans);
  541. }
  542. }
  543. }
  544. }
  545. if ((!sgml) && ((add) || (del) || (create) || (convert))) {
  546. if (noout && filename && *filename) {
  547. FILE *out;
  548. out = fopen(filename, "w");
  549. if (out == NULL) {
  550. fprintf(stderr, "could not open %s for saving\n", filename);
  551. exit_value = 2;
  552. noout = 0;
  553. } else {
  554. xmlCatalogDump(out);
  555. }
  556. } else {
  557. xmlCatalogDump(stdout);
  558. }
  559. }
  560. /*
  561. * Cleanup and check for memory leaks
  562. */
  563. xmlCleanupParser();
  564. xmlMemoryDump();
  565. return(exit_value);
  566. }
  567. #else
  568. int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  569. fprintf(stderr, "libxml was not compiled with catalog and output support\n");
  570. return(1);
  571. }
  572. #endif