blob: 006f0cc94c952e0e0102578a77451b53c4e871f0 [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);
Xin Li28c53d32017-03-07 00:33:02 +000083 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\
315\tParse the catalog file and query it for the entities\n\
316\t--sgml : handle SGML Super catalogs for --add and --del\n\
317\t--shell : run a shell allowing interactive queries\n\
318\t--create : create a new catalog\n\
319\t--add 'type' 'orig' 'replace' : add an XML entry\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000320\t--add 'entry' : add an SGML entry\n", name);
321 printf("\
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000322\t--del 'values' : remove values\n\
323\t--noout: avoid dumping the result on stdout\n\
324\t used with --add or --del, it saves the catalog changes\n\
325\t and with --sgml it automatically updates the super catalog\n\
326\t--no-super-update: do not update the SGML super catalog\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000327\t-v --verbose : provide debug informations\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000328}
329int main(int argc, char **argv) {
330 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000331 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000332 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000333
Daniel Veillard344cee72001-08-20 00:08:40 +0000334
335 if (argc <= 1) {
336 usage(argv[0]);
337 return(1);
338 }
339
340 LIBXML_TEST_VERSION
341 for (i = 1; i < argc ; i++) {
342 if (!strcmp(argv[i], "-"))
343 break;
344
345 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000346 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000347 if ((!strcmp(argv[i], "-verbose")) ||
348 (!strcmp(argv[i], "-v")) ||
349 (!strcmp(argv[i], "--verbose"))) {
350 verbose++;
351 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000352 } else if ((!strcmp(argv[i], "-noout")) ||
353 (!strcmp(argv[i], "--noout"))) {
354 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000355 } else if ((!strcmp(argv[i], "-shell")) ||
356 (!strcmp(argv[i], "--shell"))) {
357 shell++;
358 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000359 } else if ((!strcmp(argv[i], "-sgml")) ||
360 (!strcmp(argv[i], "--sgml"))) {
361 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000362 } else if ((!strcmp(argv[i], "-create")) ||
363 (!strcmp(argv[i], "--create"))) {
364 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000365 } else if ((!strcmp(argv[i], "-convert")) ||
366 (!strcmp(argv[i], "--convert"))) {
367 convert++;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000368 } else if ((!strcmp(argv[i], "-no-super-update")) ||
369 (!strcmp(argv[i], "--no-super-update"))) {
370 no_super_update++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000371 } else if ((!strcmp(argv[i], "-add")) ||
372 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000373 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000374 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000375 else
376 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000377 add++;
378 } else if ((!strcmp(argv[i], "-del")) ||
379 (!strcmp(argv[i], "--del"))) {
380 i += 1;
381 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000382 } else {
383 fprintf(stderr, "Unknown option %s\n", argv[i]);
384 usage(argv[0]);
385 return(1);
386 }
387 }
388
389 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000390 if ((!strcmp(argv[i], "-add")) ||
391 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000392 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000393 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000394 else
395 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000396 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000397 } else if ((!strcmp(argv[i], "-del")) ||
398 (!strcmp(argv[i], "--del"))) {
399 i += 1;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000400
401 /* No catalog entry specified */
402 if (i == argc || (sgml && i + 1 == argc)) {
403 fprintf(stderr, "No catalog entry specified to remove from\n");
404 usage (argv[0]);
405 return(1);
406 }
407
Daniel Veillardcda96922001-08-21 10:56:31 +0000408 continue;
409 } else if (argv[i][0] == '-')
410 continue;
411 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000412 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000413 if ((ret < 0) && (create)) {
414 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
415 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000416 break;
417 }
418
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000419 if (convert)
420 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000421
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000422 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000423 for (i = 1; i < argc ; i++) {
424 if (!strcmp(argv[i], "-"))
425 break;
426
427 if (argv[i][0] != '-')
428 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000429 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
430 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
431 continue;
432
433 if (sgml) {
434 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000435 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000436 */
437 xmlCatalogPtr catal = NULL;
438 xmlCatalogPtr super = NULL;
439
440 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
441
442 if ((!strcmp(argv[i], "-add")) ||
443 (!strcmp(argv[i], "--add"))) {
444 if (catal == NULL)
445 catal = xmlNewCatalog(1);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000446 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
447 BAD_CAST argv[i + 2], NULL);
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000448
449 if (!no_super_update) {
450 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
451 if (super == NULL)
452 super = xmlNewCatalog(1);
453
454 xmlACatalogAdd(super, BAD_CAST "CATALOG",
455 BAD_CAST argv[i + 1], NULL);
456 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000457 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000458 if (catal != NULL)
459 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000460 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000461 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000462 if (ret < 0) {
463 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000464 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000465 exit_value = 1;
466 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000467 if ((!no_super_update) && (noout) && (catal != NULL) &&
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000468 (xmlCatalogIsEmpty(catal))) {
469 super = xmlLoadSGMLSuperCatalog(
470 XML_SGML_DEFAULT_CATALOG);
471 if (super != NULL) {
472 ret = xmlACatalogRemove(super,
473 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000474 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000475 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000476 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000477 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000478 exit_value = 1;
479 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000480 }
481 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000482 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000483 if (noout) {
484 FILE *out;
485
486 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000487 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000488 } else {
489 out = fopen(argv[i + 1], "w");
490 if (out == NULL) {
491 fprintf(stderr, "could not open %s for saving\n",
492 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000493 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000494 noout = 0;
495 } else {
496 xmlACatalogDump(catal, out);
497 fclose(out);
498 }
499 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000500 if (!no_super_update && super != NULL) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000501 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000502 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000503 } else {
504 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
505 if (out == NULL) {
506 fprintf(stderr,
507 "could not open %s for saving\n",
508 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000509 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000510 noout = 0;
511 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800512
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000513 xmlACatalogDump(super, out);
514 fclose(out);
515 }
516 }
517 }
518 } else {
519 xmlACatalogDump(catal, stdout);
520 }
521 i += 2;
522 } else {
523 if ((!strcmp(argv[i], "-add")) ||
524 (!strcmp(argv[i], "--add"))) {
525 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
526 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
527 BAD_CAST argv[i + 2]);
528 else
529 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
530 BAD_CAST argv[i + 2],
531 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000532 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000533 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000534 exit_value = 3;
535 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000536 i += 3;
537 } else if ((!strcmp(argv[i], "-del")) ||
538 (!strcmp(argv[i], "--del"))) {
539 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000540 if (ret < 0) {
541 fprintf(stderr, "Failed to remove entry %s\n",
542 argv[i + 1]);
543 exit_value = 1;
544 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000545 i += 1;
546 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000547 }
548 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800549
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000550 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000551 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000552 } else {
553 for (i++; i < argc; i++) {
554 xmlURIPtr uri;
555 xmlChar *ans;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800556
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000557 uri = xmlParseURI(argv[i]);
558 if (uri == NULL) {
559 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
560 if (ans == NULL) {
561 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000562 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000563 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000564 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000565 xmlFree(ans);
566 }
567 } else {
568 xmlFreeURI(uri);
569 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
570 if (ans == NULL) {
571 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillardcccd4a02004-03-04 14:02:13 +0000572 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
573 if (ans == NULL) {
574 printf ("No entry for URI %s\n", argv[i]);
575 exit_value = 4;
576 } else {
577 printf("%s\n", (char *) ans);
578 xmlFree (ans);
579 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000580 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000581 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000582 xmlFree(ans);
583 }
584 }
585 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000586 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000587 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillardefe6c742003-12-12 14:56:03 +0000588 if (noout && filename && *filename) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000589 FILE *out;
590
591 out = fopen(filename, "w");
592 if (out == NULL) {
593 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000594 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000595 noout = 0;
596 } else {
597 xmlCatalogDump(out);
598 }
599 } else {
600 xmlCatalogDump(stdout);
601 }
602 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000603
604 /*
605 * Cleanup and check for memory leaks
606 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000607 xmlCleanupParser();
608 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000609 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000610}
611#else
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000612int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
613 fprintf(stderr, "libxml was not compiled with catalog and output support\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000614 return(1);
615}
616#endif