blob: 4c30d7546697d8ab8c90003915a6606fa7ab8714 [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
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000032#if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
Daniel Veillard344cee72001-08-20 00:08:40 +000033static int shell = 0;
Daniel Veillard82d75332001-10-08 15:01:59 +000034static int sgml = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +000035static int noout = 0;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +000036static int create = 0;
Daniel Veillardcda96922001-08-21 10:56:31 +000037static int add = 0;
38static int del = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +000039static int convert = 0;
Daniel Veillard1f8658a2004-08-14 21:46:31 +000040static int no_super_update = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +000041static int verbose = 0;
Daniel Veillardefe6c742003-12-12 14:56:03 +000042static char *filename = NULL;
Daniel Veillard344cee72001-08-20 00:08:40 +000043
Daniel Veillardcd21dc72001-11-04 20:03:38 +000044
Daniel Veillard3be27512003-01-26 19:49:04 +000045#ifndef XML_SGML_DEFAULT_CATALOG
Daniel Veillardcd21dc72001-11-04 20:03:38 +000046#define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
Daniel Veillard3be27512003-01-26 19:49:04 +000047#endif
Daniel Veillardcd21dc72001-11-04 20:03:38 +000048
Daniel Veillard344cee72001-08-20 00:08:40 +000049/************************************************************************
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];
Daniel Veillard572577e2002-01-18 16:23:55 +000078 char *ret;
79 int len;
Daniel Veillard344cee72001-08-20 00:08:40 +000080
81 if (prompt != NULL)
82 fprintf(stdout, "%s", prompt);
83 if (!fgets(line_read, 500, stdin))
84 return(NULL);
85 line_read[500] = 0;
Daniel Veillard572577e2002-01-18 16:23:55 +000086 len = strlen(line_read);
87 ret = (char *) malloc(len + 1);
88 if (ret != NULL) {
89 memcpy (ret, line_read, len + 1);
90 }
91 return(ret);
Daniel Veillard344cee72001-08-20 00:08:40 +000092#endif
93}
94
Daniel Veillard344cee72001-08-20 00:08:40 +000095static void usershell(void) {
96 char *cmdline = NULL, *cur;
97 int nbargs;
98 char command[100];
99 char arg[400];
Daniel Veillardcda96922001-08-21 10:56:31 +0000100 char *argv[20];
101 int i, ret;
Daniel Veillardcda96922001-08-21 10:56:31 +0000102 xmlChar *ans;
Daniel Veillard344cee72001-08-20 00:08:40 +0000103
104 while (1) {
105 cmdline = xmlShellReadline("> ");
106 if (cmdline == NULL)
107 return;
108
109 /*
110 * Parse the command itself
111 */
112 cur = cmdline;
113 nbargs = 0;
114 while ((*cur == ' ') || (*cur == '\t')) cur++;
115 i = 0;
116 while ((*cur != ' ') && (*cur != '\t') &&
117 (*cur != '\n') && (*cur != '\r')) {
118 if (*cur == 0)
119 break;
120 command[i++] = *cur++;
121 }
122 command[i] = 0;
123 if (i == 0) continue;
124 nbargs++;
125
126 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000127 * Parse the argument string
Daniel Veillard344cee72001-08-20 00:08:40 +0000128 */
Daniel Veillardcda96922001-08-21 10:56:31 +0000129 memset(arg, 0, sizeof(arg));
Daniel Veillard344cee72001-08-20 00:08:40 +0000130 while ((*cur == ' ') || (*cur == '\t')) cur++;
131 i = 0;
132 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
133 if (*cur == 0)
134 break;
135 arg[i++] = *cur++;
136 }
137 arg[i] = 0;
138 if (i != 0)
139 nbargs++;
140
141 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000142 * Parse the arguments
143 */
144 i = 0;
145 nbargs = 0;
146 cur = arg;
147 memset(argv, 0, sizeof(argv));
148 while (*cur != 0) {
149 while ((*cur == ' ') || (*cur == '\t')) cur++;
150 if (*cur == '\'') {
151 cur++;
152 argv[i] = cur;
153 while ((*cur != 0) && (*cur != '\'')) cur++;
154 if (*cur == '\'') {
155 *cur = 0;
156 nbargs++;
157 i++;
158 cur++;
159 }
160 } else if (*cur == '"') {
161 cur++;
162 argv[i] = cur;
163 while ((*cur != 0) && (*cur != '"')) cur++;
164 if (*cur == '"') {
165 *cur = 0;
166 nbargs++;
167 i++;
168 cur++;
169 }
170 } else {
171 argv[i] = cur;
172 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
173 cur++;
174 *cur = 0;
175 nbargs++;
176 i++;
177 cur++;
178 }
179 }
180
181 /*
Daniel Veillard344cee72001-08-20 00:08:40 +0000182 * start interpreting the command
183 */
184 if (!strcmp(command, "exit"))
185 break;
186 if (!strcmp(command, "quit"))
187 break;
188 if (!strcmp(command, "bye"))
189 break;
190 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000191 if (nbargs != 1) {
192 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000193 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000194 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
195 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000196 printf("No entry for PUBLIC %s\n", argv[0]);
197 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000198 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000199 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000200 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000201 }
202 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000203 if (nbargs != 1) {
204 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000205 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000206 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
207 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000208 printf("No entry for SYSTEM %s\n", argv[0]);
209 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000210 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000211 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000212 }
213 }
214 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000215 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000216 if ((nbargs != 3) && (nbargs != 2)) {
217 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000218 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000219 if (argv[2] == NULL)
220 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
221 BAD_CAST argv[1]);
222 else
223 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
224 BAD_CAST argv[2]);
225 if (ret != 0)
226 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000227 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000228 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000229 if ((nbargs != 3) && (nbargs != 2)) {
230 printf("add requires 2 or 3 arguments\n");
231 } else {
232 if (argv[2] == NULL)
233 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
234 BAD_CAST argv[1]);
235 else
236 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
237 BAD_CAST argv[2]);
238 if (ret != 0)
239 printf("add command failed\n");
240 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000241 }
242 } else if (!strcmp(command, "del")) {
243 if (nbargs != 1) {
244 printf("del requires 1\n");
245 } else {
246 ret = xmlCatalogRemove(BAD_CAST argv[0]);
247 if (ret <= 0)
248 printf("del command failed\n");
249
250 }
251 } else if (!strcmp(command, "resolve")) {
252 if (nbargs != 2) {
253 printf("resolve requires 2 arguments\n");
254 } else {
255 ans = xmlCatalogResolve(BAD_CAST argv[0],
256 BAD_CAST argv[1]);
257 if (ans == NULL) {
258 printf("Resolver failed to find an answer\n");
259 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000260 printf("%s\n", (char *) ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000261 xmlFree(ans);
262 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000263 }
264 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000265 if (nbargs != 0) {
266 printf("dump has no arguments\n");
267 } else {
268 xmlCatalogDump(stdout);
269 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000270 } else if (!strcmp(command, "debug")) {
271 if (nbargs != 0) {
272 printf("debug has no arguments\n");
273 } else {
274 verbose++;
275 xmlCatalogSetDebug(verbose);
276 }
277 } else if (!strcmp(command, "quiet")) {
278 if (nbargs != 0) {
279 printf("quiet has no arguments\n");
280 } else {
281 if (verbose > 0)
282 verbose--;
283 xmlCatalogSetDebug(verbose);
284 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000285 } else {
286 if (strcmp(command, "help")) {
287 printf("Unrecognized command %s\n", command);
288 }
289 printf("Commands available:\n");
290 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
291 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000292 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
293 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
294 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000295 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000296 printf("\tdebug: increase the verbosity level\n");
297 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000298 printf("\texit: quit the shell\n");
299 }
300 free(cmdline); /* not xmlFree here ! */
301 }
302}
303
304/************************************************************************
305 * *
306 * Main *
307 * *
308 ************************************************************************/
309static void usage(const char *name) {
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000310 printf("\
311Usage : %s [options] catalogfile entities...\n\
312\tParse the catalog file and query it for the entities\n\
313\t--sgml : handle SGML Super catalogs for --add and --del\n\
314\t--shell : run a shell allowing interactive queries\n\
315\t--create : create a new catalog\n\
316\t--add 'type' 'orig' 'replace' : add an XML entry\n\
317\t--add 'entry' : add an SGML entry\n\
318\t--del 'values' : remove values\n\
319\t--noout: avoid dumping the result on stdout\n\
320\t used with --add or --del, it saves the catalog changes\n\
321\t and with --sgml it automatically updates the super catalog\n\
322\t--no-super-update: do not update the SGML super catalog\n\
323\t-v --verbose : provide debug informations\n", name);
Daniel Veillard344cee72001-08-20 00:08:40 +0000324}
325int main(int argc, char **argv) {
326 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000327 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000328 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000329
Daniel Veillard344cee72001-08-20 00:08:40 +0000330
331 if (argc <= 1) {
332 usage(argv[0]);
333 return(1);
334 }
335
336 LIBXML_TEST_VERSION
337 for (i = 1; i < argc ; i++) {
338 if (!strcmp(argv[i], "-"))
339 break;
340
341 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000342 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000343 if ((!strcmp(argv[i], "-verbose")) ||
344 (!strcmp(argv[i], "-v")) ||
345 (!strcmp(argv[i], "--verbose"))) {
346 verbose++;
347 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000348 } else if ((!strcmp(argv[i], "-noout")) ||
349 (!strcmp(argv[i], "--noout"))) {
350 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000351 } else if ((!strcmp(argv[i], "-shell")) ||
352 (!strcmp(argv[i], "--shell"))) {
353 shell++;
354 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000355 } else if ((!strcmp(argv[i], "-sgml")) ||
356 (!strcmp(argv[i], "--sgml"))) {
357 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000358 } else if ((!strcmp(argv[i], "-create")) ||
359 (!strcmp(argv[i], "--create"))) {
360 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000361 } else if ((!strcmp(argv[i], "-convert")) ||
362 (!strcmp(argv[i], "--convert"))) {
363 convert++;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000364 } else if ((!strcmp(argv[i], "-no-super-update")) ||
365 (!strcmp(argv[i], "--no-super-update"))) {
366 no_super_update++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000367 } else if ((!strcmp(argv[i], "-add")) ||
368 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000369 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000370 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000371 else
372 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000373 add++;
374 } else if ((!strcmp(argv[i], "-del")) ||
375 (!strcmp(argv[i], "--del"))) {
376 i += 1;
377 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000378 } else {
379 fprintf(stderr, "Unknown option %s\n", argv[i]);
380 usage(argv[0]);
381 return(1);
382 }
383 }
384
385 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000386 if ((!strcmp(argv[i], "-add")) ||
387 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000388 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000389 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000390 else
391 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000392 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000393 } else if ((!strcmp(argv[i], "-del")) ||
394 (!strcmp(argv[i], "--del"))) {
395 i += 1;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000396
397 /* No catalog entry specified */
398 if (i == argc || (sgml && i + 1 == argc)) {
399 fprintf(stderr, "No catalog entry specified to remove from\n");
400 usage (argv[0]);
401 return(1);
402 }
403
Daniel Veillardcda96922001-08-21 10:56:31 +0000404 continue;
405 } else if (argv[i][0] == '-')
406 continue;
407 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000408 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000409 if ((ret < 0) && (create)) {
410 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
411 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000412 break;
413 }
414
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000415 if (convert)
416 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000417
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000418 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000419 for (i = 1; i < argc ; i++) {
420 if (!strcmp(argv[i], "-"))
421 break;
422
423 if (argv[i][0] != '-')
424 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000425 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
426 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
427 continue;
428
429 if (sgml) {
430 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000431 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000432 */
433 xmlCatalogPtr catal = NULL;
434 xmlCatalogPtr super = NULL;
435
436 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
437
438 if ((!strcmp(argv[i], "-add")) ||
439 (!strcmp(argv[i], "--add"))) {
440 if (catal == NULL)
441 catal = xmlNewCatalog(1);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000442 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
443 BAD_CAST argv[i + 2], NULL);
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000444
445 if (!no_super_update) {
446 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
447 if (super == NULL)
448 super = xmlNewCatalog(1);
449
450 xmlACatalogAdd(super, BAD_CAST "CATALOG",
451 BAD_CAST argv[i + 1], NULL);
452 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000453 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000454 if (catal != NULL)
455 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000456 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000457 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000458 if (ret < 0) {
459 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000460 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000461 exit_value = 1;
462 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000463 if ((!no_super_update) && (noout) && (catal != NULL) &&
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000464 (xmlCatalogIsEmpty(catal))) {
465 super = xmlLoadSGMLSuperCatalog(
466 XML_SGML_DEFAULT_CATALOG);
467 if (super != NULL) {
468 ret = xmlACatalogRemove(super,
469 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000470 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000471 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000472 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000473 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000474 exit_value = 1;
475 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000476 }
477 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000478 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000479 if (noout) {
480 FILE *out;
481
482 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000483 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000484 } else {
485 out = fopen(argv[i + 1], "w");
486 if (out == NULL) {
487 fprintf(stderr, "could not open %s for saving\n",
488 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000489 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000490 noout = 0;
491 } else {
492 xmlACatalogDump(catal, out);
493 fclose(out);
494 }
495 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000496 if (!no_super_update && super != NULL) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000497 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000498 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000499 } else {
500 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
501 if (out == NULL) {
502 fprintf(stderr,
503 "could not open %s for saving\n",
504 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000505 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000506 noout = 0;
507 } else {
508
509 xmlACatalogDump(super, out);
510 fclose(out);
511 }
512 }
513 }
514 } else {
515 xmlACatalogDump(catal, stdout);
516 }
517 i += 2;
518 } else {
519 if ((!strcmp(argv[i], "-add")) ||
520 (!strcmp(argv[i], "--add"))) {
521 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
522 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
523 BAD_CAST argv[i + 2]);
524 else
525 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
526 BAD_CAST argv[i + 2],
527 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000528 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000529 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000530 exit_value = 3;
531 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000532 i += 3;
533 } else if ((!strcmp(argv[i], "-del")) ||
534 (!strcmp(argv[i], "--del"))) {
535 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000536 if (ret < 0) {
537 fprintf(stderr, "Failed to remove entry %s\n",
538 argv[i + 1]);
539 exit_value = 1;
540 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000541 i += 1;
542 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000543 }
544 }
545
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000546 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000547 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000548 } else {
549 for (i++; i < argc; i++) {
550 xmlURIPtr uri;
551 xmlChar *ans;
552
553 uri = xmlParseURI(argv[i]);
554 if (uri == NULL) {
555 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
556 if (ans == NULL) {
557 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000558 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000559 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000560 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000561 xmlFree(ans);
562 }
563 } else {
564 xmlFreeURI(uri);
565 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
566 if (ans == NULL) {
567 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillardcccd4a02004-03-04 14:02:13 +0000568 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
569 if (ans == NULL) {
570 printf ("No entry for URI %s\n", argv[i]);
571 exit_value = 4;
572 } else {
573 printf("%s\n", (char *) ans);
574 xmlFree (ans);
575 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000576 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000577 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000578 xmlFree(ans);
579 }
580 }
581 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000582 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000583 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillardefe6c742003-12-12 14:56:03 +0000584 if (noout && filename && *filename) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000585 FILE *out;
586
587 out = fopen(filename, "w");
588 if (out == NULL) {
589 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000590 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000591 noout = 0;
592 } else {
593 xmlCatalogDump(out);
594 }
595 } else {
596 xmlCatalogDump(stdout);
597 }
598 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000599
600 /*
601 * Cleanup and check for memory leaks
602 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000603 xmlCleanupParser();
604 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000605 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000606}
607#else
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000608int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
609 fprintf(stderr, "libxml was not compiled with catalog and output support\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000610 return(1);
611}
612#endif