blob: c02b97f67b2a14a0c5acf5a6915bcd15c37cb7e7 [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/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080050 * *
51 * Shell Interface *
52 * *
Daniel Veillard344cee72001-08-20 00:08:40 +000053 ************************************************************************/
54/**
55 * xmlShellReadline:
56 * @prompt: the prompt value
57 *
58 * Read a string
Daniel Veillardf8e3db02012-09-11 13:26:36 +080059 *
Daniel Veillard344cee72001-08-20 00:08:40 +000060 * 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);
Patrick Monnerat21373262015-04-20 15:00:58 +020083 fflush(stdout);
Daniel Veillard344cee72001-08-20 00:08:40 +000084 if (!fgets(line_read, 500, stdin))
85 return(NULL);
86 line_read[500] = 0;
Daniel Veillard572577e2002-01-18 16:23:55 +000087 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);
Daniel Veillard344cee72001-08-20 00:08:40 +000093#endif
94}
95
Daniel Veillard344cee72001-08-20 00:08:40 +000096static void usershell(void) {
97 char *cmdline = NULL, *cur;
98 int nbargs;
99 char command[100];
100 char arg[400];
Daniel Veillardcda96922001-08-21 10:56:31 +0000101 char *argv[20];
102 int i, ret;
Daniel Veillardcda96922001-08-21 10:56:31 +0000103 xmlChar *ans;
Daniel Veillard344cee72001-08-20 00:08:40 +0000104
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;
Daniel Veillard11ce4002006-03-10 00:36:23 +0000124 if (i == 0) {
125 free(cmdline);
126 continue;
127 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000128
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;
Daniel Veillard344cee72001-08-20 00:08:40 +0000141
142 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000143 * 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 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800161 } else if (*cur == '"') {
Daniel Veillardcda96922001-08-21 10:56:31 +0000162 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 /*
Daniel Veillard344cee72001-08-20 00:08:40 +0000183 * start interpreting the command
184 */
Philip Withnall7746f2f2014-06-20 21:05:33 +0100185 if (!strcmp(command, "exit") ||
186 !strcmp(command, "quit") ||
187 !strcmp(command, "bye")) {
188 free(cmdline);
Daniel Veillard344cee72001-08-20 00:08:40 +0000189 break;
Philip Withnall7746f2f2014-06-20 21:05:33 +0100190 }
191
Daniel Veillard344cee72001-08-20 00:08:40 +0000192 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000193 if (nbargs != 1) {
194 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000195 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000196 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
197 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000198 printf("No entry for PUBLIC %s\n", argv[0]);
199 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000200 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000201 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000202 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000203 }
204 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000205 if (nbargs != 1) {
206 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000207 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000208 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
209 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000210 printf("No entry for SYSTEM %s\n", argv[0]);
211 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000212 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000213 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000214 }
215 }
216 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000217 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000218 if ((nbargs != 3) && (nbargs != 2)) {
219 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000220 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000221 if (argv[2] == NULL)
222 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
223 BAD_CAST argv[1]);
224 else
225 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
226 BAD_CAST argv[2]);
227 if (ret != 0)
228 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000229 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000230 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000231 if ((nbargs != 3) && (nbargs != 2)) {
232 printf("add requires 2 or 3 arguments\n");
233 } else {
234 if (argv[2] == NULL)
235 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
236 BAD_CAST argv[1]);
237 else
238 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
239 BAD_CAST argv[2]);
240 if (ret != 0)
241 printf("add command failed\n");
242 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000243 }
244 } else if (!strcmp(command, "del")) {
245 if (nbargs != 1) {
246 printf("del requires 1\n");
247 } else {
248 ret = xmlCatalogRemove(BAD_CAST argv[0]);
249 if (ret <= 0)
250 printf("del command failed\n");
251
252 }
253 } else if (!strcmp(command, "resolve")) {
254 if (nbargs != 2) {
255 printf("resolve requires 2 arguments\n");
256 } else {
257 ans = xmlCatalogResolve(BAD_CAST argv[0],
258 BAD_CAST argv[1]);
259 if (ans == NULL) {
260 printf("Resolver failed to find an answer\n");
261 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000262 printf("%s\n", (char *) ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000263 xmlFree(ans);
264 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000265 }
266 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000267 if (nbargs != 0) {
268 printf("dump has no arguments\n");
269 } else {
270 xmlCatalogDump(stdout);
271 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000272 } else if (!strcmp(command, "debug")) {
273 if (nbargs != 0) {
274 printf("debug has no arguments\n");
275 } else {
276 verbose++;
277 xmlCatalogSetDebug(verbose);
278 }
279 } else if (!strcmp(command, "quiet")) {
280 if (nbargs != 0) {
281 printf("quiet has no arguments\n");
282 } else {
283 if (verbose > 0)
284 verbose--;
285 xmlCatalogSetDebug(verbose);
286 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000287 } else {
288 if (strcmp(command, "help")) {
289 printf("Unrecognized command %s\n", command);
290 }
291 printf("Commands available:\n");
292 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
293 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000294 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
295 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
296 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000297 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000298 printf("\tdebug: increase the verbosity level\n");
299 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000300 printf("\texit: quit the shell\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800301 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000302 free(cmdline); /* not xmlFree here ! */
303 }
304}
305
306/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800307 * *
308 * Main *
309 * *
Daniel Veillard344cee72001-08-20 00:08:40 +0000310 ************************************************************************/
311static void usage(const char *name) {
William M. Brackb7b54de2004-10-06 16:38:01 +0000312 /* split into 2 printf's to avoid overly long string (gcc warning) */
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000313 printf("\
314Usage : %s [options] catalogfile entities...\n\
Jan Pokorný6b780f62017-10-19 13:27:29 +0200315\tParse the catalog file (void specification possibly expressed as \"\"\n\
316\tappoints the default system one) and query it for the entities\n\
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000317\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;
Jan Pokorný6b780f62017-10-19 13:27:29 +0200412
413 if (filename == NULL && argv[i][0] == '\0') {
414 /* Interpret empty-string catalog specification as
415 a shortcut for a default system catalog. */
416 xmlInitializeCatalog();
417 } else {
418 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000419 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000420 if ((ret < 0) && (create)) {
421 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
422 }
Jan Pokorný6b780f62017-10-19 13:27:29 +0200423 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000424 break;
425 }
426
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000427 if (convert)
428 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000429
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000430 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000431 for (i = 1; i < argc ; i++) {
432 if (!strcmp(argv[i], "-"))
433 break;
434
435 if (argv[i][0] != '-')
436 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000437 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
438 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
439 continue;
440
441 if (sgml) {
442 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000443 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000444 */
445 xmlCatalogPtr catal = NULL;
446 xmlCatalogPtr super = NULL;
447
448 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
449
450 if ((!strcmp(argv[i], "-add")) ||
451 (!strcmp(argv[i], "--add"))) {
452 if (catal == NULL)
453 catal = xmlNewCatalog(1);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000454 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
455 BAD_CAST argv[i + 2], NULL);
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000456
457 if (!no_super_update) {
458 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
459 if (super == NULL)
460 super = xmlNewCatalog(1);
461
462 xmlACatalogAdd(super, BAD_CAST "CATALOG",
463 BAD_CAST argv[i + 1], NULL);
464 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000465 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000466 if (catal != NULL)
467 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000468 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000469 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000470 if (ret < 0) {
471 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000472 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000473 exit_value = 1;
474 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000475 if ((!no_super_update) && (noout) && (catal != NULL) &&
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000476 (xmlCatalogIsEmpty(catal))) {
477 super = xmlLoadSGMLSuperCatalog(
478 XML_SGML_DEFAULT_CATALOG);
479 if (super != NULL) {
480 ret = xmlACatalogRemove(super,
481 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000482 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000483 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000484 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000485 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000486 exit_value = 1;
487 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000488 }
489 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000490 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000491 if (noout) {
492 FILE *out;
493
494 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000495 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000496 } else {
497 out = fopen(argv[i + 1], "w");
498 if (out == NULL) {
499 fprintf(stderr, "could not open %s for saving\n",
500 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000501 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000502 noout = 0;
503 } else {
504 xmlACatalogDump(catal, out);
505 fclose(out);
506 }
507 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000508 if (!no_super_update && super != NULL) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000509 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000510 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000511 } else {
512 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
513 if (out == NULL) {
514 fprintf(stderr,
515 "could not open %s for saving\n",
516 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000517 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000518 noout = 0;
519 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800520
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000521 xmlACatalogDump(super, out);
522 fclose(out);
523 }
524 }
525 }
526 } else {
527 xmlACatalogDump(catal, stdout);
528 }
529 i += 2;
530 } else {
531 if ((!strcmp(argv[i], "-add")) ||
532 (!strcmp(argv[i], "--add"))) {
533 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
534 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
535 BAD_CAST argv[i + 2]);
536 else
537 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
538 BAD_CAST argv[i + 2],
539 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000540 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000541 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000542 exit_value = 3;
543 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000544 i += 3;
545 } else if ((!strcmp(argv[i], "-del")) ||
546 (!strcmp(argv[i], "--del"))) {
547 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000548 if (ret < 0) {
549 fprintf(stderr, "Failed to remove entry %s\n",
550 argv[i + 1]);
551 exit_value = 1;
552 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000553 i += 1;
554 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000555 }
556 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800557
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000558 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000559 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000560 } else {
561 for (i++; i < argc; i++) {
562 xmlURIPtr uri;
563 xmlChar *ans;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800564
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000565 uri = xmlParseURI(argv[i]);
566 if (uri == NULL) {
567 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
568 if (ans == NULL) {
569 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000570 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000571 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000572 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000573 xmlFree(ans);
574 }
575 } else {
576 xmlFreeURI(uri);
577 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
578 if (ans == NULL) {
579 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillardcccd4a02004-03-04 14:02:13 +0000580 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
581 if (ans == NULL) {
582 printf ("No entry for URI %s\n", argv[i]);
583 exit_value = 4;
584 } else {
585 printf("%s\n", (char *) ans);
586 xmlFree (ans);
587 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000588 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000589 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000590 xmlFree(ans);
591 }
592 }
593 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000594 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000595 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillardefe6c742003-12-12 14:56:03 +0000596 if (noout && filename && *filename) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000597 FILE *out;
598
599 out = fopen(filename, "w");
600 if (out == NULL) {
601 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000602 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000603 noout = 0;
604 } else {
605 xmlCatalogDump(out);
606 }
607 } else {
608 xmlCatalogDump(stdout);
609 }
610 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000611
612 /*
613 * Cleanup and check for memory leaks
614 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000615 xmlCleanupParser();
616 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000617 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000618}
619#else
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000620int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
621 fprintf(stderr, "libxml was not compiled with catalog and output support\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000622 return(1);
623}
624#endif