blob: 6f193b17720d1d063d70a97f86b26cf3293bddc4 [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;
Daniel Veillard11ce4002006-03-10 00:36:23 +0000123 if (i == 0) {
124 free(cmdline);
125 continue;
126 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000127 nbargs++;
128
129 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000130 * Parse the argument string
Daniel Veillard344cee72001-08-20 00:08:40 +0000131 */
Daniel Veillardcda96922001-08-21 10:56:31 +0000132 memset(arg, 0, sizeof(arg));
Daniel Veillard344cee72001-08-20 00:08:40 +0000133 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 if (i != 0)
142 nbargs++;
143
144 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000145 * Parse the arguments
146 */
147 i = 0;
148 nbargs = 0;
149 cur = arg;
150 memset(argv, 0, sizeof(argv));
151 while (*cur != 0) {
152 while ((*cur == ' ') || (*cur == '\t')) cur++;
153 if (*cur == '\'') {
154 cur++;
155 argv[i] = cur;
156 while ((*cur != 0) && (*cur != '\'')) cur++;
157 if (*cur == '\'') {
158 *cur = 0;
159 nbargs++;
160 i++;
161 cur++;
162 }
163 } else if (*cur == '"') {
164 cur++;
165 argv[i] = cur;
166 while ((*cur != 0) && (*cur != '"')) cur++;
167 if (*cur == '"') {
168 *cur = 0;
169 nbargs++;
170 i++;
171 cur++;
172 }
173 } else {
174 argv[i] = cur;
175 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
176 cur++;
177 *cur = 0;
178 nbargs++;
179 i++;
180 cur++;
181 }
182 }
183
184 /*
Daniel Veillard344cee72001-08-20 00:08:40 +0000185 * start interpreting the command
186 */
187 if (!strcmp(command, "exit"))
188 break;
189 if (!strcmp(command, "quit"))
190 break;
191 if (!strcmp(command, "bye"))
192 break;
193 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000194 if (nbargs != 1) {
195 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000196 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000197 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
198 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000199 printf("No entry for PUBLIC %s\n", argv[0]);
200 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000201 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000202 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000203 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000204 }
205 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000206 if (nbargs != 1) {
207 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000208 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000209 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
210 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000211 printf("No entry for SYSTEM %s\n", argv[0]);
212 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000213 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000214 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000215 }
216 }
217 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000218 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000219 if ((nbargs != 3) && (nbargs != 2)) {
220 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000221 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000222 if (argv[2] == NULL)
223 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
224 BAD_CAST argv[1]);
225 else
226 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
227 BAD_CAST argv[2]);
228 if (ret != 0)
229 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000230 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000231 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000232 if ((nbargs != 3) && (nbargs != 2)) {
233 printf("add requires 2 or 3 arguments\n");
234 } else {
235 if (argv[2] == NULL)
236 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
237 BAD_CAST argv[1]);
238 else
239 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
240 BAD_CAST argv[2]);
241 if (ret != 0)
242 printf("add command failed\n");
243 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000244 }
245 } else if (!strcmp(command, "del")) {
246 if (nbargs != 1) {
247 printf("del requires 1\n");
248 } else {
249 ret = xmlCatalogRemove(BAD_CAST argv[0]);
250 if (ret <= 0)
251 printf("del command failed\n");
252
253 }
254 } else if (!strcmp(command, "resolve")) {
255 if (nbargs != 2) {
256 printf("resolve requires 2 arguments\n");
257 } else {
258 ans = xmlCatalogResolve(BAD_CAST argv[0],
259 BAD_CAST argv[1]);
260 if (ans == NULL) {
261 printf("Resolver failed to find an answer\n");
262 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000263 printf("%s\n", (char *) ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000264 xmlFree(ans);
265 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000266 }
267 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000268 if (nbargs != 0) {
269 printf("dump has no arguments\n");
270 } else {
271 xmlCatalogDump(stdout);
272 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000273 } else if (!strcmp(command, "debug")) {
274 if (nbargs != 0) {
275 printf("debug has no arguments\n");
276 } else {
277 verbose++;
278 xmlCatalogSetDebug(verbose);
279 }
280 } else if (!strcmp(command, "quiet")) {
281 if (nbargs != 0) {
282 printf("quiet has no arguments\n");
283 } else {
284 if (verbose > 0)
285 verbose--;
286 xmlCatalogSetDebug(verbose);
287 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000288 } else {
289 if (strcmp(command, "help")) {
290 printf("Unrecognized command %s\n", command);
291 }
292 printf("Commands available:\n");
293 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
294 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000295 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
296 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
297 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000298 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000299 printf("\tdebug: increase the verbosity level\n");
300 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000301 printf("\texit: quit the shell\n");
302 }
303 free(cmdline); /* not xmlFree here ! */
304 }
305}
306
307/************************************************************************
308 * *
309 * Main *
310 * *
311 ************************************************************************/
312static void usage(const char *name) {
William M. Brackb7b54de2004-10-06 16:38:01 +0000313 /* split into 2 printf's to avoid overly long string (gcc warning) */
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000314 printf("\
315Usage : %s [options] catalogfile entities...\n\
316\tParse the catalog file and query it for the entities\n\
317\t--sgml : handle SGML Super catalogs for --add and --del\n\
318\t--shell : run a shell allowing interactive queries\n\
319\t--create : create a new catalog\n\
320\t--add 'type' 'orig' 'replace' : add an XML entry\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000321\t--add 'entry' : add an SGML entry\n", name);
322 printf("\
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000323\t--del 'values' : remove values\n\
324\t--noout: avoid dumping the result on stdout\n\
325\t used with --add or --del, it saves the catalog changes\n\
326\t and with --sgml it automatically updates the super catalog\n\
327\t--no-super-update: do not update the SGML super catalog\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000328\t-v --verbose : provide debug informations\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000329}
330int main(int argc, char **argv) {
331 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000332 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000333 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000334
Daniel Veillard344cee72001-08-20 00:08:40 +0000335
336 if (argc <= 1) {
337 usage(argv[0]);
338 return(1);
339 }
340
341 LIBXML_TEST_VERSION
342 for (i = 1; i < argc ; i++) {
343 if (!strcmp(argv[i], "-"))
344 break;
345
346 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000347 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000348 if ((!strcmp(argv[i], "-verbose")) ||
349 (!strcmp(argv[i], "-v")) ||
350 (!strcmp(argv[i], "--verbose"))) {
351 verbose++;
352 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000353 } else if ((!strcmp(argv[i], "-noout")) ||
354 (!strcmp(argv[i], "--noout"))) {
355 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000356 } else if ((!strcmp(argv[i], "-shell")) ||
357 (!strcmp(argv[i], "--shell"))) {
358 shell++;
359 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000360 } else if ((!strcmp(argv[i], "-sgml")) ||
361 (!strcmp(argv[i], "--sgml"))) {
362 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000363 } else if ((!strcmp(argv[i], "-create")) ||
364 (!strcmp(argv[i], "--create"))) {
365 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000366 } else if ((!strcmp(argv[i], "-convert")) ||
367 (!strcmp(argv[i], "--convert"))) {
368 convert++;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000369 } else if ((!strcmp(argv[i], "-no-super-update")) ||
370 (!strcmp(argv[i], "--no-super-update"))) {
371 no_super_update++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000372 } else if ((!strcmp(argv[i], "-add")) ||
373 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000374 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000375 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000376 else
377 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000378 add++;
379 } else if ((!strcmp(argv[i], "-del")) ||
380 (!strcmp(argv[i], "--del"))) {
381 i += 1;
382 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000383 } else {
384 fprintf(stderr, "Unknown option %s\n", argv[i]);
385 usage(argv[0]);
386 return(1);
387 }
388 }
389
390 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000391 if ((!strcmp(argv[i], "-add")) ||
392 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000393 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000394 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000395 else
396 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000397 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000398 } else if ((!strcmp(argv[i], "-del")) ||
399 (!strcmp(argv[i], "--del"))) {
400 i += 1;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000401
402 /* No catalog entry specified */
403 if (i == argc || (sgml && i + 1 == argc)) {
404 fprintf(stderr, "No catalog entry specified to remove from\n");
405 usage (argv[0]);
406 return(1);
407 }
408
Daniel Veillardcda96922001-08-21 10:56:31 +0000409 continue;
410 } else if (argv[i][0] == '-')
411 continue;
412 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000413 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000414 if ((ret < 0) && (create)) {
415 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
416 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000417 break;
418 }
419
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000420 if (convert)
421 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000422
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000423 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000424 for (i = 1; i < argc ; i++) {
425 if (!strcmp(argv[i], "-"))
426 break;
427
428 if (argv[i][0] != '-')
429 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000430 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
431 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
432 continue;
433
434 if (sgml) {
435 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000436 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000437 */
438 xmlCatalogPtr catal = NULL;
439 xmlCatalogPtr super = NULL;
440
441 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
442
443 if ((!strcmp(argv[i], "-add")) ||
444 (!strcmp(argv[i], "--add"))) {
445 if (catal == NULL)
446 catal = xmlNewCatalog(1);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000447 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
448 BAD_CAST argv[i + 2], NULL);
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000449
450 if (!no_super_update) {
451 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
452 if (super == NULL)
453 super = xmlNewCatalog(1);
454
455 xmlACatalogAdd(super, BAD_CAST "CATALOG",
456 BAD_CAST argv[i + 1], NULL);
457 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000458 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000459 if (catal != NULL)
460 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000461 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000462 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000463 if (ret < 0) {
464 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000465 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000466 exit_value = 1;
467 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000468 if ((!no_super_update) && (noout) && (catal != NULL) &&
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000469 (xmlCatalogIsEmpty(catal))) {
470 super = xmlLoadSGMLSuperCatalog(
471 XML_SGML_DEFAULT_CATALOG);
472 if (super != NULL) {
473 ret = xmlACatalogRemove(super,
474 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000475 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000476 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000477 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000478 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000479 exit_value = 1;
480 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000481 }
482 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000483 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000484 if (noout) {
485 FILE *out;
486
487 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000488 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000489 } else {
490 out = fopen(argv[i + 1], "w");
491 if (out == NULL) {
492 fprintf(stderr, "could not open %s for saving\n",
493 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000494 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000495 noout = 0;
496 } else {
497 xmlACatalogDump(catal, out);
498 fclose(out);
499 }
500 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000501 if (!no_super_update && super != NULL) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000502 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000503 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000504 } else {
505 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
506 if (out == NULL) {
507 fprintf(stderr,
508 "could not open %s for saving\n",
509 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000510 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000511 noout = 0;
512 } else {
513
514 xmlACatalogDump(super, out);
515 fclose(out);
516 }
517 }
518 }
519 } else {
520 xmlACatalogDump(catal, stdout);
521 }
522 i += 2;
523 } else {
524 if ((!strcmp(argv[i], "-add")) ||
525 (!strcmp(argv[i], "--add"))) {
526 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
527 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
528 BAD_CAST argv[i + 2]);
529 else
530 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
531 BAD_CAST argv[i + 2],
532 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000533 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000534 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000535 exit_value = 3;
536 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000537 i += 3;
538 } else if ((!strcmp(argv[i], "-del")) ||
539 (!strcmp(argv[i], "--del"))) {
540 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000541 if (ret < 0) {
542 fprintf(stderr, "Failed to remove entry %s\n",
543 argv[i + 1]);
544 exit_value = 1;
545 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000546 i += 1;
547 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000548 }
549 }
550
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000551 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000552 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000553 } else {
554 for (i++; i < argc; i++) {
555 xmlURIPtr uri;
556 xmlChar *ans;
557
558 uri = xmlParseURI(argv[i]);
559 if (uri == NULL) {
560 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
561 if (ans == NULL) {
562 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000563 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000564 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000565 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000566 xmlFree(ans);
567 }
568 } else {
569 xmlFreeURI(uri);
570 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
571 if (ans == NULL) {
572 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillardcccd4a02004-03-04 14:02:13 +0000573 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
574 if (ans == NULL) {
575 printf ("No entry for URI %s\n", argv[i]);
576 exit_value = 4;
577 } else {
578 printf("%s\n", (char *) ans);
579 xmlFree (ans);
580 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000581 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000582 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000583 xmlFree(ans);
584 }
585 }
586 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000587 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000588 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillardefe6c742003-12-12 14:56:03 +0000589 if (noout && filename && *filename) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000590 FILE *out;
591
592 out = fopen(filename, "w");
593 if (out == NULL) {
594 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000595 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000596 noout = 0;
597 } else {
598 xmlCatalogDump(out);
599 }
600 } else {
601 xmlCatalogDump(stdout);
602 }
603 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000604
605 /*
606 * Cleanup and check for memory leaks
607 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000608 xmlCleanupParser();
609 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000610 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000611}
612#else
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000613int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
614 fprintf(stderr, "libxml was not compiled with catalog and output support\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000615 return(1);
616}
617#endif