blob: 5b6ceafdd25998912b2deaa2275200a3c5f29d93 [file] [log] [blame]
Daniel Veillard344cee72001-08-20 00:08:40 +00001/*
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
9#include "libxml.h"
10
11#include <string.h>
12#include <stdio.h>
13#include <stdarg.h>
14
Daniel Veillardc0631a62001-09-20 13:56:06 +000015#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
Bjorn Reese45029602001-08-21 09:23:53 +000019#ifdef HAVE_LIBREADLINE
20#include <readline/readline.h>
21#ifdef HAVE_LIBHISTORY
22#include <readline/history.h>
23#endif
24#endif
25
Daniel Veillard344cee72001-08-20 00:08:40 +000026#include <libxml/xmlmemory.h>
27#include <libxml/uri.h>
28#include <libxml/catalog.h>
29#include <libxml/parser.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillard344cee72001-08-20 00:08:40 +000031
32static int shell = 0;
Daniel Veillard82d75332001-10-08 15:01:59 +000033static int sgml = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +000034static int noout = 0;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +000035static int create = 0;
Daniel Veillardcda96922001-08-21 10:56:31 +000036static int add = 0;
37static int del = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +000038static int convert = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +000039static int verbose = 0;
Daniel Veillardcda96922001-08-21 10:56:31 +000040static char *filename;
Daniel Veillard344cee72001-08-20 00:08:40 +000041
42#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardcd21dc72001-11-04 20:03:38 +000043
44#define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
45
Daniel Veillard344cee72001-08-20 00:08:40 +000046/************************************************************************
47 * *
48 * Shell Interface *
49 * *
50 ************************************************************************/
51/**
52 * xmlShellReadline:
53 * @prompt: the prompt value
54 *
55 * Read a string
56 *
57 * Returns a pointer to it or NULL on EOF the caller is expected to
58 * free the returned string.
59 */
60static char *
61xmlShellReadline(const char *prompt) {
62#ifdef HAVE_LIBREADLINE
63 char *line_read;
64
65 /* Get a line from the user. */
66 line_read = readline (prompt);
67
68 /* If the line has any text in it, save it on the history. */
69 if (line_read && *line_read)
70 add_history (line_read);
71
72 return (line_read);
73#else
74 char line_read[501];
Daniel Veillard572577e2002-01-18 16:23:55 +000075 char *ret;
76 int len;
Daniel Veillard344cee72001-08-20 00:08:40 +000077
78 if (prompt != NULL)
79 fprintf(stdout, "%s", prompt);
80 if (!fgets(line_read, 500, stdin))
81 return(NULL);
82 line_read[500] = 0;
Daniel Veillard572577e2002-01-18 16:23:55 +000083 len = strlen(line_read);
84 ret = (char *) malloc(len + 1);
85 if (ret != NULL) {
86 memcpy (ret, line_read, len + 1);
87 }
88 return(ret);
Daniel Veillard344cee72001-08-20 00:08:40 +000089#endif
90}
91
Daniel Veillard344cee72001-08-20 00:08:40 +000092static void usershell(void) {
93 char *cmdline = NULL, *cur;
94 int nbargs;
95 char command[100];
96 char arg[400];
Daniel Veillardcda96922001-08-21 10:56:31 +000097 char *argv[20];
98 int i, ret;
Daniel Veillardcda96922001-08-21 10:56:31 +000099 xmlChar *ans;
Daniel Veillard344cee72001-08-20 00:08:40 +0000100
101 while (1) {
102 cmdline = xmlShellReadline("> ");
103 if (cmdline == NULL)
104 return;
105
106 /*
107 * Parse the command itself
108 */
109 cur = cmdline;
110 nbargs = 0;
111 while ((*cur == ' ') || (*cur == '\t')) cur++;
112 i = 0;
113 while ((*cur != ' ') && (*cur != '\t') &&
114 (*cur != '\n') && (*cur != '\r')) {
115 if (*cur == 0)
116 break;
117 command[i++] = *cur++;
118 }
119 command[i] = 0;
120 if (i == 0) continue;
121 nbargs++;
122
123 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000124 * Parse the argument string
Daniel Veillard344cee72001-08-20 00:08:40 +0000125 */
Daniel Veillardcda96922001-08-21 10:56:31 +0000126 memset(arg, 0, sizeof(arg));
Daniel Veillard344cee72001-08-20 00:08:40 +0000127 while ((*cur == ' ') || (*cur == '\t')) cur++;
128 i = 0;
129 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
130 if (*cur == 0)
131 break;
132 arg[i++] = *cur++;
133 }
134 arg[i] = 0;
135 if (i != 0)
136 nbargs++;
137
138 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000139 * Parse the arguments
140 */
141 i = 0;
142 nbargs = 0;
143 cur = arg;
144 memset(argv, 0, sizeof(argv));
145 while (*cur != 0) {
146 while ((*cur == ' ') || (*cur == '\t')) cur++;
147 if (*cur == '\'') {
148 cur++;
149 argv[i] = cur;
150 while ((*cur != 0) && (*cur != '\'')) cur++;
151 if (*cur == '\'') {
152 *cur = 0;
153 nbargs++;
154 i++;
155 cur++;
156 }
157 } else if (*cur == '"') {
158 cur++;
159 argv[i] = cur;
160 while ((*cur != 0) && (*cur != '"')) cur++;
161 if (*cur == '"') {
162 *cur = 0;
163 nbargs++;
164 i++;
165 cur++;
166 }
167 } else {
168 argv[i] = cur;
169 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
170 cur++;
171 *cur = 0;
172 nbargs++;
173 i++;
174 cur++;
175 }
176 }
177
178 /*
Daniel Veillard344cee72001-08-20 00:08:40 +0000179 * start interpreting the command
180 */
181 if (!strcmp(command, "exit"))
182 break;
183 if (!strcmp(command, "quit"))
184 break;
185 if (!strcmp(command, "bye"))
186 break;
187 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000188 if (nbargs != 1) {
189 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000190 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000191 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
192 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000193 printf("No entry for PUBLIC %s\n", argv[0]);
194 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000195 printf("%s\n", ans);
196 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000197 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000198 }
199 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000200 if (nbargs != 1) {
201 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000202 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000203 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
204 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000205 printf("No entry for SYSTEM %s\n", argv[0]);
206 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000207 printf("%s\n", ans);
208 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000209 }
210 }
211 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000212 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000213 if ((nbargs != 3) && (nbargs != 2)) {
214 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000215 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000216 if (argv[2] == NULL)
217 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
218 BAD_CAST argv[1]);
219 else
220 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
221 BAD_CAST argv[2]);
222 if (ret != 0)
223 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000224 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000225 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000226 if ((nbargs != 3) && (nbargs != 2)) {
227 printf("add requires 2 or 3 arguments\n");
228 } else {
229 if (argv[2] == NULL)
230 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
231 BAD_CAST argv[1]);
232 else
233 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
234 BAD_CAST argv[2]);
235 if (ret != 0)
236 printf("add command failed\n");
237 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000238 }
239 } else if (!strcmp(command, "del")) {
240 if (nbargs != 1) {
241 printf("del requires 1\n");
242 } else {
243 ret = xmlCatalogRemove(BAD_CAST argv[0]);
244 if (ret <= 0)
245 printf("del command failed\n");
246
247 }
248 } else if (!strcmp(command, "resolve")) {
249 if (nbargs != 2) {
250 printf("resolve requires 2 arguments\n");
251 } else {
252 ans = xmlCatalogResolve(BAD_CAST argv[0],
253 BAD_CAST argv[1]);
254 if (ans == NULL) {
255 printf("Resolver failed to find an answer\n");
256 } else {
257 printf("%s\n", ans);
258 xmlFree(ans);
259 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000260 }
261 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000262 if (nbargs != 0) {
263 printf("dump has no arguments\n");
264 } else {
265 xmlCatalogDump(stdout);
266 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000267 } else if (!strcmp(command, "debug")) {
268 if (nbargs != 0) {
269 printf("debug has no arguments\n");
270 } else {
271 verbose++;
272 xmlCatalogSetDebug(verbose);
273 }
274 } else if (!strcmp(command, "quiet")) {
275 if (nbargs != 0) {
276 printf("quiet has no arguments\n");
277 } else {
278 if (verbose > 0)
279 verbose--;
280 xmlCatalogSetDebug(verbose);
281 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000282 } else {
283 if (strcmp(command, "help")) {
284 printf("Unrecognized command %s\n", command);
285 }
286 printf("Commands available:\n");
287 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
288 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000289 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
290 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
291 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000292 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000293 printf("\tdebug: increase the verbosity level\n");
294 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000295 printf("\texit: quit the shell\n");
296 }
297 free(cmdline); /* not xmlFree here ! */
298 }
299}
300
301/************************************************************************
302 * *
303 * Main *
304 * *
305 ************************************************************************/
306static void usage(const char *name) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000307 printf("Usage : %s [options] catalogfile entities...\n", name);
308 printf("\tParse the catalog file and query it for the entities\n");
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000309 printf("\t--sgml : handle SGML Super catalogs for --add and --del\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000310 printf("\t--shell : run a shell allowing interactive queries\n");
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000311 printf("\t--create : create a new catalog\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000312 printf("\t--add 'type' 'orig' 'replace' : add an entry\n");
313 printf("\t--del 'values' : remove values\n");
314 printf("\t--noout: avoid dumping the result on stdout\n");
315 printf("\t used with add or del, it saves the catalog changes\n");
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000316 printf("\t and with --sgml it also updates the super catalog\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000317 printf("\t-v --verbose : provide debug informations\n");
318}
319int main(int argc, char **argv) {
320 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000321 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000322 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000323
Daniel Veillard344cee72001-08-20 00:08:40 +0000324
325 if (argc <= 1) {
326 usage(argv[0]);
327 return(1);
328 }
329
330 LIBXML_TEST_VERSION
331 for (i = 1; i < argc ; i++) {
332 if (!strcmp(argv[i], "-"))
333 break;
334
335 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000336 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000337 if ((!strcmp(argv[i], "-verbose")) ||
338 (!strcmp(argv[i], "-v")) ||
339 (!strcmp(argv[i], "--verbose"))) {
340 verbose++;
341 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000342 } else if ((!strcmp(argv[i], "-noout")) ||
343 (!strcmp(argv[i], "--noout"))) {
344 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000345 } else if ((!strcmp(argv[i], "-shell")) ||
346 (!strcmp(argv[i], "--shell"))) {
347 shell++;
348 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000349 } else if ((!strcmp(argv[i], "-sgml")) ||
350 (!strcmp(argv[i], "--sgml"))) {
351 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000352 } else if ((!strcmp(argv[i], "-create")) ||
353 (!strcmp(argv[i], "--create"))) {
354 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000355 } else if ((!strcmp(argv[i], "-convert")) ||
356 (!strcmp(argv[i], "--convert"))) {
357 convert++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000358 } else if ((!strcmp(argv[i], "-add")) ||
359 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000360 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000361 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000362 else
363 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000364 add++;
365 } else if ((!strcmp(argv[i], "-del")) ||
366 (!strcmp(argv[i], "--del"))) {
367 i += 1;
368 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000369 } else {
370 fprintf(stderr, "Unknown option %s\n", argv[i]);
371 usage(argv[0]);
372 return(1);
373 }
374 }
375
376 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000377 if ((!strcmp(argv[i], "-add")) ||
378 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000379 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000380 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000381 else
382 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000383 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000384 } else if ((!strcmp(argv[i], "-del")) ||
385 (!strcmp(argv[i], "--del"))) {
386 i += 1;
387 continue;
388 } else if (argv[i][0] == '-')
389 continue;
390 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000391 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000392 if ((ret < 0) && (create)) {
393 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
394 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000395 break;
396 }
397
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000398 if (convert)
399 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000400
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000401 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000402 for (i = 1; i < argc ; i++) {
403 if (!strcmp(argv[i], "-"))
404 break;
405
406 if (argv[i][0] != '-')
407 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000408 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
409 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
410 continue;
411
412 if (sgml) {
413 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000414 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000415 */
416 xmlCatalogPtr catal = NULL;
417 xmlCatalogPtr super = NULL;
418
419 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
420
421 if ((!strcmp(argv[i], "-add")) ||
422 (!strcmp(argv[i], "--add"))) {
423 if (catal == NULL)
424 catal = xmlNewCatalog(1);
425 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
426 if (super == NULL)
427 super = xmlNewCatalog(1);
428
429 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
430 BAD_CAST argv[i + 2], NULL);
431 xmlACatalogAdd(super, BAD_CAST "CATALOG",
432 BAD_CAST argv[i + 1], NULL);
Daniel Veillard82d75332001-10-08 15:01:59 +0000433 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000434 if (catal != NULL)
435 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000436 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000437 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000438 if (ret < 0) {
439 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000440 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000441 exit_value = 1;
442 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000443 if ((noout) && (catal != NULL) &&
444 (xmlCatalogIsEmpty(catal))) {
445 super = xmlLoadSGMLSuperCatalog(
446 XML_SGML_DEFAULT_CATALOG);
447 if (super != NULL) {
448 ret = xmlACatalogRemove(super,
449 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000450 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000451 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000452 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000453 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000454 exit_value = 1;
455 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000456 }
457 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000458 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000459 if (noout) {
460 FILE *out;
461
462 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000463 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000464 } else {
465 out = fopen(argv[i + 1], "w");
466 if (out == NULL) {
467 fprintf(stderr, "could not open %s for saving\n",
468 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000469 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000470 noout = 0;
471 } else {
472 xmlACatalogDump(catal, out);
473 fclose(out);
474 }
475 }
476 if (super != NULL) {
477 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000478 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000479 } else {
480 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
481 if (out == NULL) {
482 fprintf(stderr,
483 "could not open %s for saving\n",
484 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000485 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000486 noout = 0;
487 } else {
488
489 xmlACatalogDump(super, out);
490 fclose(out);
491 }
492 }
493 }
494 } else {
495 xmlACatalogDump(catal, stdout);
496 }
497 i += 2;
498 } else {
499 if ((!strcmp(argv[i], "-add")) ||
500 (!strcmp(argv[i], "--add"))) {
501 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
502 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
503 BAD_CAST argv[i + 2]);
504 else
505 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
506 BAD_CAST argv[i + 2],
507 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000508 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000509 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000510 exit_value = 3;
511 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000512 i += 3;
513 } else if ((!strcmp(argv[i], "-del")) ||
514 (!strcmp(argv[i], "--del"))) {
515 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000516 if (ret < 0) {
517 fprintf(stderr, "Failed to remove entry %s\n",
518 argv[i + 1]);
519 exit_value = 1;
520 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000521 i += 1;
522 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000523 }
524 }
525
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000526 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000527 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000528 } else {
529 for (i++; i < argc; i++) {
530 xmlURIPtr uri;
531 xmlChar *ans;
532
533 uri = xmlParseURI(argv[i]);
534 if (uri == NULL) {
535 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
536 if (ans == NULL) {
537 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000538 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000539 } else {
540 printf("%s\n", ans);
541 xmlFree(ans);
542 }
543 } else {
544 xmlFreeURI(uri);
545 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
546 if (ans == NULL) {
547 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000548 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000549 } else {
550 printf("%s\n", ans);
551 xmlFree(ans);
552 }
553 }
554 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000555 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000556 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000557 if (noout) {
558 FILE *out;
559
560 out = fopen(filename, "w");
561 if (out == NULL) {
562 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000563 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000564 noout = 0;
565 } else {
566 xmlCatalogDump(out);
567 }
568 } else {
569 xmlCatalogDump(stdout);
570 }
571 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000572
573 /*
574 * Cleanup and check for memory leaks
575 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000576 xmlCleanupParser();
577 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000578 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000579}
580#else
581int main(int argc, char **argv) {
582 fprintf(stderr, "libxml was not compiled with catalog support\n");
583 return(1);
584}
585#endif