blob: 1830f5e236e9cbaa26ac3a716c5878304e849531 [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
Daniel Veillard3be27512003-01-26 19:49:04 +000044#ifndef XML_SGML_DEFAULT_CATALOG
Daniel Veillardcd21dc72001-11-04 20:03:38 +000045#define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
Daniel Veillard3be27512003-01-26 19:49:04 +000046#endif
Daniel Veillardcd21dc72001-11-04 20:03:38 +000047
Daniel Veillard344cee72001-08-20 00:08:40 +000048/************************************************************************
49 * *
50 * Shell Interface *
51 * *
52 ************************************************************************/
53/**
54 * xmlShellReadline:
55 * @prompt: the prompt value
56 *
57 * Read a string
58 *
59 * Returns a pointer to it or NULL on EOF the caller is expected to
60 * free the returned string.
61 */
62static char *
63xmlShellReadline(const char *prompt) {
64#ifdef HAVE_LIBREADLINE
65 char *line_read;
66
67 /* Get a line from the user. */
68 line_read = readline (prompt);
69
70 /* If the line has any text in it, save it on the history. */
71 if (line_read && *line_read)
72 add_history (line_read);
73
74 return (line_read);
75#else
76 char line_read[501];
Daniel Veillard572577e2002-01-18 16:23:55 +000077 char *ret;
78 int len;
Daniel Veillard344cee72001-08-20 00:08:40 +000079
80 if (prompt != NULL)
81 fprintf(stdout, "%s", prompt);
82 if (!fgets(line_read, 500, stdin))
83 return(NULL);
84 line_read[500] = 0;
Daniel Veillard572577e2002-01-18 16:23:55 +000085 len = strlen(line_read);
86 ret = (char *) malloc(len + 1);
87 if (ret != NULL) {
88 memcpy (ret, line_read, len + 1);
89 }
90 return(ret);
Daniel Veillard344cee72001-08-20 00:08:40 +000091#endif
92}
93
Daniel Veillard344cee72001-08-20 00:08:40 +000094static void usershell(void) {
95 char *cmdline = NULL, *cur;
96 int nbargs;
97 char command[100];
98 char arg[400];
Daniel Veillardcda96922001-08-21 10:56:31 +000099 char *argv[20];
100 int i, ret;
Daniel Veillardcda96922001-08-21 10:56:31 +0000101 xmlChar *ans;
Daniel Veillard344cee72001-08-20 00:08:40 +0000102
103 while (1) {
104 cmdline = xmlShellReadline("> ");
105 if (cmdline == NULL)
106 return;
107
108 /*
109 * Parse the command itself
110 */
111 cur = cmdline;
112 nbargs = 0;
113 while ((*cur == ' ') || (*cur == '\t')) cur++;
114 i = 0;
115 while ((*cur != ' ') && (*cur != '\t') &&
116 (*cur != '\n') && (*cur != '\r')) {
117 if (*cur == 0)
118 break;
119 command[i++] = *cur++;
120 }
121 command[i] = 0;
122 if (i == 0) continue;
123 nbargs++;
124
125 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000126 * Parse the argument string
Daniel Veillard344cee72001-08-20 00:08:40 +0000127 */
Daniel Veillardcda96922001-08-21 10:56:31 +0000128 memset(arg, 0, sizeof(arg));
Daniel Veillard344cee72001-08-20 00:08:40 +0000129 while ((*cur == ' ') || (*cur == '\t')) cur++;
130 i = 0;
131 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
132 if (*cur == 0)
133 break;
134 arg[i++] = *cur++;
135 }
136 arg[i] = 0;
137 if (i != 0)
138 nbargs++;
139
140 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000141 * Parse the arguments
142 */
143 i = 0;
144 nbargs = 0;
145 cur = arg;
146 memset(argv, 0, sizeof(argv));
147 while (*cur != 0) {
148 while ((*cur == ' ') || (*cur == '\t')) cur++;
149 if (*cur == '\'') {
150 cur++;
151 argv[i] = cur;
152 while ((*cur != 0) && (*cur != '\'')) cur++;
153 if (*cur == '\'') {
154 *cur = 0;
155 nbargs++;
156 i++;
157 cur++;
158 }
159 } else if (*cur == '"') {
160 cur++;
161 argv[i] = cur;
162 while ((*cur != 0) && (*cur != '"')) cur++;
163 if (*cur == '"') {
164 *cur = 0;
165 nbargs++;
166 i++;
167 cur++;
168 }
169 } else {
170 argv[i] = cur;
171 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
172 cur++;
173 *cur = 0;
174 nbargs++;
175 i++;
176 cur++;
177 }
178 }
179
180 /*
Daniel Veillard344cee72001-08-20 00:08:40 +0000181 * start interpreting the command
182 */
183 if (!strcmp(command, "exit"))
184 break;
185 if (!strcmp(command, "quit"))
186 break;
187 if (!strcmp(command, "bye"))
188 break;
189 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000190 if (nbargs != 1) {
191 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000192 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000193 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
194 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000195 printf("No entry for PUBLIC %s\n", argv[0]);
196 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000197 printf("%s\n", ans);
198 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000199 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000200 }
201 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000202 if (nbargs != 1) {
203 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000204 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000205 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
206 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000207 printf("No entry for SYSTEM %s\n", argv[0]);
208 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000209 printf("%s\n", ans);
210 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000211 }
212 }
213 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000214 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000215 if ((nbargs != 3) && (nbargs != 2)) {
216 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000217 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000218 if (argv[2] == NULL)
219 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
220 BAD_CAST argv[1]);
221 else
222 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
223 BAD_CAST argv[2]);
224 if (ret != 0)
225 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000226 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000227 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000228 if ((nbargs != 3) && (nbargs != 2)) {
229 printf("add requires 2 or 3 arguments\n");
230 } else {
231 if (argv[2] == NULL)
232 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
233 BAD_CAST argv[1]);
234 else
235 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
236 BAD_CAST argv[2]);
237 if (ret != 0)
238 printf("add command failed\n");
239 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000240 }
241 } else if (!strcmp(command, "del")) {
242 if (nbargs != 1) {
243 printf("del requires 1\n");
244 } else {
245 ret = xmlCatalogRemove(BAD_CAST argv[0]);
246 if (ret <= 0)
247 printf("del command failed\n");
248
249 }
250 } else if (!strcmp(command, "resolve")) {
251 if (nbargs != 2) {
252 printf("resolve requires 2 arguments\n");
253 } else {
254 ans = xmlCatalogResolve(BAD_CAST argv[0],
255 BAD_CAST argv[1]);
256 if (ans == NULL) {
257 printf("Resolver failed to find an answer\n");
258 } else {
259 printf("%s\n", ans);
260 xmlFree(ans);
261 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000262 }
263 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000264 if (nbargs != 0) {
265 printf("dump has no arguments\n");
266 } else {
267 xmlCatalogDump(stdout);
268 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000269 } else if (!strcmp(command, "debug")) {
270 if (nbargs != 0) {
271 printf("debug has no arguments\n");
272 } else {
273 verbose++;
274 xmlCatalogSetDebug(verbose);
275 }
276 } else if (!strcmp(command, "quiet")) {
277 if (nbargs != 0) {
278 printf("quiet has no arguments\n");
279 } else {
280 if (verbose > 0)
281 verbose--;
282 xmlCatalogSetDebug(verbose);
283 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000284 } else {
285 if (strcmp(command, "help")) {
286 printf("Unrecognized command %s\n", command);
287 }
288 printf("Commands available:\n");
289 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
290 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000291 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
292 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
293 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000294 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000295 printf("\tdebug: increase the verbosity level\n");
296 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000297 printf("\texit: quit the shell\n");
298 }
299 free(cmdline); /* not xmlFree here ! */
300 }
301}
302
303/************************************************************************
304 * *
305 * Main *
306 * *
307 ************************************************************************/
308static void usage(const char *name) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000309 printf("Usage : %s [options] catalogfile entities...\n", name);
310 printf("\tParse the catalog file and query it for the entities\n");
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000311 printf("\t--sgml : handle SGML Super catalogs for --add and --del\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000312 printf("\t--shell : run a shell allowing interactive queries\n");
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000313 printf("\t--create : create a new catalog\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000314 printf("\t--add 'type' 'orig' 'replace' : add an entry\n");
315 printf("\t--del 'values' : remove values\n");
316 printf("\t--noout: avoid dumping the result on stdout\n");
317 printf("\t used with add or del, it saves the catalog changes\n");
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000318 printf("\t and with --sgml it also updates the super catalog\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000319 printf("\t-v --verbose : provide debug informations\n");
320}
321int main(int argc, char **argv) {
322 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000323 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000324 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000325
Daniel Veillard344cee72001-08-20 00:08:40 +0000326
327 if (argc <= 1) {
328 usage(argv[0]);
329 return(1);
330 }
331
332 LIBXML_TEST_VERSION
333 for (i = 1; i < argc ; i++) {
334 if (!strcmp(argv[i], "-"))
335 break;
336
337 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000338 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000339 if ((!strcmp(argv[i], "-verbose")) ||
340 (!strcmp(argv[i], "-v")) ||
341 (!strcmp(argv[i], "--verbose"))) {
342 verbose++;
343 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000344 } else if ((!strcmp(argv[i], "-noout")) ||
345 (!strcmp(argv[i], "--noout"))) {
346 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000347 } else if ((!strcmp(argv[i], "-shell")) ||
348 (!strcmp(argv[i], "--shell"))) {
349 shell++;
350 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000351 } else if ((!strcmp(argv[i], "-sgml")) ||
352 (!strcmp(argv[i], "--sgml"))) {
353 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000354 } else if ((!strcmp(argv[i], "-create")) ||
355 (!strcmp(argv[i], "--create"))) {
356 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000357 } else if ((!strcmp(argv[i], "-convert")) ||
358 (!strcmp(argv[i], "--convert"))) {
359 convert++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000360 } else if ((!strcmp(argv[i], "-add")) ||
361 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000362 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000363 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000364 else
365 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000366 add++;
367 } else if ((!strcmp(argv[i], "-del")) ||
368 (!strcmp(argv[i], "--del"))) {
369 i += 1;
370 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000371 } else {
372 fprintf(stderr, "Unknown option %s\n", argv[i]);
373 usage(argv[0]);
374 return(1);
375 }
376 }
377
378 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000379 if ((!strcmp(argv[i], "-add")) ||
380 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000381 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000382 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000383 else
384 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000385 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000386 } else if ((!strcmp(argv[i], "-del")) ||
387 (!strcmp(argv[i], "--del"))) {
388 i += 1;
389 continue;
390 } else if (argv[i][0] == '-')
391 continue;
392 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000393 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000394 if ((ret < 0) && (create)) {
395 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
396 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000397 break;
398 }
399
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000400 if (convert)
401 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000402
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000403 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000404 for (i = 1; i < argc ; i++) {
405 if (!strcmp(argv[i], "-"))
406 break;
407
408 if (argv[i][0] != '-')
409 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000410 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
411 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
412 continue;
413
414 if (sgml) {
415 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000416 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000417 */
418 xmlCatalogPtr catal = NULL;
419 xmlCatalogPtr super = NULL;
420
421 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
422
423 if ((!strcmp(argv[i], "-add")) ||
424 (!strcmp(argv[i], "--add"))) {
425 if (catal == NULL)
426 catal = xmlNewCatalog(1);
427 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
428 if (super == NULL)
429 super = xmlNewCatalog(1);
430
431 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
432 BAD_CAST argv[i + 2], NULL);
433 xmlACatalogAdd(super, BAD_CAST "CATALOG",
434 BAD_CAST argv[i + 1], NULL);
Daniel Veillard82d75332001-10-08 15:01:59 +0000435 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000436 if (catal != NULL)
437 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000438 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000439 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000440 if (ret < 0) {
441 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000442 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000443 exit_value = 1;
444 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000445 if ((noout) && (catal != NULL) &&
446 (xmlCatalogIsEmpty(catal))) {
447 super = xmlLoadSGMLSuperCatalog(
448 XML_SGML_DEFAULT_CATALOG);
449 if (super != NULL) {
450 ret = xmlACatalogRemove(super,
451 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000452 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000453 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000454 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000455 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000456 exit_value = 1;
457 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000458 }
459 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000460 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000461 if (noout) {
462 FILE *out;
463
464 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000465 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000466 } else {
467 out = fopen(argv[i + 1], "w");
468 if (out == NULL) {
469 fprintf(stderr, "could not open %s for saving\n",
470 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000471 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000472 noout = 0;
473 } else {
474 xmlACatalogDump(catal, out);
475 fclose(out);
476 }
477 }
478 if (super != NULL) {
479 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000480 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000481 } else {
482 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
483 if (out == NULL) {
484 fprintf(stderr,
485 "could not open %s for saving\n",
486 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000487 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000488 noout = 0;
489 } else {
490
491 xmlACatalogDump(super, out);
492 fclose(out);
493 }
494 }
495 }
496 } else {
497 xmlACatalogDump(catal, stdout);
498 }
499 i += 2;
500 } else {
501 if ((!strcmp(argv[i], "-add")) ||
502 (!strcmp(argv[i], "--add"))) {
503 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
504 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
505 BAD_CAST argv[i + 2]);
506 else
507 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
508 BAD_CAST argv[i + 2],
509 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000510 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000511 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000512 exit_value = 3;
513 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000514 i += 3;
515 } else if ((!strcmp(argv[i], "-del")) ||
516 (!strcmp(argv[i], "--del"))) {
517 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000518 if (ret < 0) {
519 fprintf(stderr, "Failed to remove entry %s\n",
520 argv[i + 1]);
521 exit_value = 1;
522 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000523 i += 1;
524 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000525 }
526 }
527
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000528 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000529 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000530 } else {
531 for (i++; i < argc; i++) {
532 xmlURIPtr uri;
533 xmlChar *ans;
534
535 uri = xmlParseURI(argv[i]);
536 if (uri == NULL) {
537 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
538 if (ans == NULL) {
539 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000540 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000541 } else {
542 printf("%s\n", ans);
543 xmlFree(ans);
544 }
545 } else {
546 xmlFreeURI(uri);
547 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
548 if (ans == NULL) {
549 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000550 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000551 } else {
552 printf("%s\n", ans);
553 xmlFree(ans);
554 }
555 }
556 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000557 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000558 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000559 if (noout) {
560 FILE *out;
561
562 out = fopen(filename, "w");
563 if (out == NULL) {
564 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000565 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000566 noout = 0;
567 } else {
568 xmlCatalogDump(out);
569 }
570 } else {
571 xmlCatalogDump(stdout);
572 }
573 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000574
575 /*
576 * Cleanup and check for memory leaks
577 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000578 xmlCleanupParser();
579 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000580 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000581}
582#else
583int main(int argc, char **argv) {
584 fprintf(stderr, "libxml was not compiled with catalog support\n");
585 return(1);
586}
587#endif