VirtualBox

source: vbox/trunk/src/libs/libxml2-2.9.14/xmlcatalog.c@ 102239

Last change on this file since 102239 was 95312, checked in by vboxsync, 2 years ago

libs/{curl,libxml2}: OSE export fixes, bugref:8515

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette