blob: b9ed6a488bdf32a9bf6d1ba67eafa4f73051dc97 [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);
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
128 /*
Daniel Veillardcda96922001-08-21 10:56:31 +0000129 * Parse the argument string
Daniel Veillard344cee72001-08-20 00:08:40 +0000130 */
Daniel Veillardcda96922001-08-21 10:56:31 +0000131 memset(arg, 0, sizeof(arg));
Daniel Veillard344cee72001-08-20 00:08:40 +0000132 while ((*cur == ' ') || (*cur == '\t')) cur++;
133 i = 0;
134 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
135 if (*cur == 0)
136 break;
137 arg[i++] = *cur++;
138 }
139 arg[i] = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +0000140
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 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800160 } else if (*cur == '"') {
Daniel Veillardcda96922001-08-21 10:56:31 +0000161 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 */
Philip Withnall7746f2f2014-06-20 21:05:33 +0100184 if (!strcmp(command, "exit") ||
185 !strcmp(command, "quit") ||
186 !strcmp(command, "bye")) {
187 free(cmdline);
Daniel Veillard344cee72001-08-20 00:08:40 +0000188 break;
Philip Withnall7746f2f2014-06-20 21:05:33 +0100189 }
190
Daniel Veillard344cee72001-08-20 00:08:40 +0000191 if (!strcmp(command, "public")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000192 if (nbargs != 1) {
193 printf("public requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000194 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000195 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
196 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000197 printf("No entry for PUBLIC %s\n", argv[0]);
198 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000199 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000200 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000201 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000202 }
203 } else if (!strcmp(command, "system")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000204 if (nbargs != 1) {
205 printf("system requires 1 arguments\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000206 } else {
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000207 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
208 if (ans == NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000209 printf("No entry for SYSTEM %s\n", argv[0]);
210 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000211 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000212 xmlFree(ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000213 }
214 }
215 } else if (!strcmp(command, "add")) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000216 if (sgml) {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000217 if ((nbargs != 3) && (nbargs != 2)) {
218 printf("add requires 2 or 3 arguments\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000219 } else {
Daniel Veillard9cdcf362002-10-22 14:23:59 +0000220 if (argv[2] == NULL)
221 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
222 BAD_CAST argv[1]);
223 else
224 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
225 BAD_CAST argv[2]);
226 if (ret != 0)
227 printf("add command failed\n");
Daniel Veillard82d75332001-10-08 15:01:59 +0000228 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000229 } else {
Daniel Veillard82d75332001-10-08 15:01:59 +0000230 if ((nbargs != 3) && (nbargs != 2)) {
231 printf("add requires 2 or 3 arguments\n");
232 } else {
233 if (argv[2] == NULL)
234 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
235 BAD_CAST argv[1]);
236 else
237 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
238 BAD_CAST argv[2]);
239 if (ret != 0)
240 printf("add command failed\n");
241 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000242 }
243 } else if (!strcmp(command, "del")) {
244 if (nbargs != 1) {
245 printf("del requires 1\n");
246 } else {
247 ret = xmlCatalogRemove(BAD_CAST argv[0]);
248 if (ret <= 0)
249 printf("del command failed\n");
250
251 }
252 } else if (!strcmp(command, "resolve")) {
253 if (nbargs != 2) {
254 printf("resolve requires 2 arguments\n");
255 } else {
256 ans = xmlCatalogResolve(BAD_CAST argv[0],
257 BAD_CAST argv[1]);
258 if (ans == NULL) {
259 printf("Resolver failed to find an answer\n");
260 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000261 printf("%s\n", (char *) ans);
Daniel Veillardcda96922001-08-21 10:56:31 +0000262 xmlFree(ans);
263 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000264 }
265 } else if (!strcmp(command, "dump")) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000266 if (nbargs != 0) {
267 printf("dump has no arguments\n");
268 } else {
269 xmlCatalogDump(stdout);
270 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000271 } else if (!strcmp(command, "debug")) {
272 if (nbargs != 0) {
273 printf("debug has no arguments\n");
274 } else {
275 verbose++;
276 xmlCatalogSetDebug(verbose);
277 }
278 } else if (!strcmp(command, "quiet")) {
279 if (nbargs != 0) {
280 printf("quiet has no arguments\n");
281 } else {
282 if (verbose > 0)
283 verbose--;
284 xmlCatalogSetDebug(verbose);
285 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000286 } else {
287 if (strcmp(command, "help")) {
288 printf("Unrecognized command %s\n", command);
289 }
290 printf("Commands available:\n");
291 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
292 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
Daniel Veillardcda96922001-08-21 10:56:31 +0000293 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
294 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
295 printf("\tdel 'values' : remove values\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000296 printf("\tdump: print the current catalog state\n");
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000297 printf("\tdebug: increase the verbosity level\n");
298 printf("\tquiet: decrease the verbosity level\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000299 printf("\texit: quit the shell\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800300 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000301 free(cmdline); /* not xmlFree here ! */
302 }
303}
304
305/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800306 * *
307 * Main *
308 * *
Daniel Veillard344cee72001-08-20 00:08:40 +0000309 ************************************************************************/
310static void usage(const char *name) {
William M. Brackb7b54de2004-10-06 16:38:01 +0000311 /* split into 2 printf's to avoid overly long string (gcc warning) */
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000312 printf("\
313Usage : %s [options] catalogfile entities...\n\
314\tParse the catalog file and query it for the entities\n\
315\t--sgml : handle SGML Super catalogs for --add and --del\n\
316\t--shell : run a shell allowing interactive queries\n\
317\t--create : create a new catalog\n\
318\t--add 'type' 'orig' 'replace' : add an XML entry\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000319\t--add 'entry' : add an SGML entry\n", name);
320 printf("\
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000321\t--del 'values' : remove values\n\
322\t--noout: avoid dumping the result on stdout\n\
323\t used with --add or --del, it saves the catalog changes\n\
324\t and with --sgml it automatically updates the super catalog\n\
325\t--no-super-update: do not update the SGML super catalog\n\
William M. Brackb7b54de2004-10-06 16:38:01 +0000326\t-v --verbose : provide debug informations\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000327}
328int main(int argc, char **argv) {
329 int i;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000330 int ret;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000331 int exit_value = 0;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000332
Daniel Veillard344cee72001-08-20 00:08:40 +0000333
334 if (argc <= 1) {
335 usage(argv[0]);
336 return(1);
337 }
338
339 LIBXML_TEST_VERSION
340 for (i = 1; i < argc ; i++) {
341 if (!strcmp(argv[i], "-"))
342 break;
343
344 if (argv[i][0] != '-')
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000345 break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000346 if ((!strcmp(argv[i], "-verbose")) ||
347 (!strcmp(argv[i], "-v")) ||
348 (!strcmp(argv[i], "--verbose"))) {
349 verbose++;
350 xmlCatalogSetDebug(verbose);
Daniel Veillardcda96922001-08-21 10:56:31 +0000351 } else if ((!strcmp(argv[i], "-noout")) ||
352 (!strcmp(argv[i], "--noout"))) {
353 noout = 1;
Daniel Veillard344cee72001-08-20 00:08:40 +0000354 } else if ((!strcmp(argv[i], "-shell")) ||
355 (!strcmp(argv[i], "--shell"))) {
356 shell++;
357 noout = 1;
Daniel Veillard82d75332001-10-08 15:01:59 +0000358 } else if ((!strcmp(argv[i], "-sgml")) ||
359 (!strcmp(argv[i], "--sgml"))) {
360 sgml++;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000361 } else if ((!strcmp(argv[i], "-create")) ||
362 (!strcmp(argv[i], "--create"))) {
363 create++;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000364 } else if ((!strcmp(argv[i], "-convert")) ||
365 (!strcmp(argv[i], "--convert"))) {
366 convert++;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000367 } else if ((!strcmp(argv[i], "-no-super-update")) ||
368 (!strcmp(argv[i], "--no-super-update"))) {
369 no_super_update++;
Daniel Veillardcda96922001-08-21 10:56:31 +0000370 } else if ((!strcmp(argv[i], "-add")) ||
371 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000372 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000373 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000374 else
375 i += 3;
Daniel Veillardcda96922001-08-21 10:56:31 +0000376 add++;
377 } else if ((!strcmp(argv[i], "-del")) ||
378 (!strcmp(argv[i], "--del"))) {
379 i += 1;
380 del++;
Daniel Veillard344cee72001-08-20 00:08:40 +0000381 } else {
382 fprintf(stderr, "Unknown option %s\n", argv[i]);
383 usage(argv[0]);
384 return(1);
385 }
386 }
387
388 for (i = 1; i < argc; i++) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000389 if ((!strcmp(argv[i], "-add")) ||
390 (!strcmp(argv[i], "--add"))) {
Daniel Veillard82d75332001-10-08 15:01:59 +0000391 if (sgml)
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000392 i += 2;
Daniel Veillard82d75332001-10-08 15:01:59 +0000393 else
394 i += 3;
Daniel Veillard344cee72001-08-20 00:08:40 +0000395 continue;
Daniel Veillardcda96922001-08-21 10:56:31 +0000396 } else if ((!strcmp(argv[i], "-del")) ||
397 (!strcmp(argv[i], "--del"))) {
398 i += 1;
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000399
400 /* No catalog entry specified */
401 if (i == argc || (sgml && i + 1 == argc)) {
402 fprintf(stderr, "No catalog entry specified to remove from\n");
403 usage (argv[0]);
404 return(1);
405 }
406
Daniel Veillardcda96922001-08-21 10:56:31 +0000407 continue;
408 } else if (argv[i][0] == '-')
409 continue;
410 filename = argv[i];
Daniel Veillard82d75332001-10-08 15:01:59 +0000411 ret = xmlLoadCatalog(argv[i]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000412 if ((ret < 0) && (create)) {
413 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
414 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000415 break;
416 }
417
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000418 if (convert)
419 ret = xmlCatalogConvert();
Daniel Veillardcda96922001-08-21 10:56:31 +0000420
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000421 if ((add) || (del)) {
Daniel Veillardcda96922001-08-21 10:56:31 +0000422 for (i = 1; i < argc ; i++) {
423 if (!strcmp(argv[i], "-"))
424 break;
425
426 if (argv[i][0] != '-')
427 continue;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000428 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
429 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
430 continue;
431
432 if (sgml) {
433 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000434 * Maintenance of SGML catalogs.
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000435 */
436 xmlCatalogPtr catal = NULL;
437 xmlCatalogPtr super = NULL;
438
439 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
440
441 if ((!strcmp(argv[i], "-add")) ||
442 (!strcmp(argv[i], "--add"))) {
443 if (catal == NULL)
444 catal = xmlNewCatalog(1);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000445 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
446 BAD_CAST argv[i + 2], NULL);
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000447
448 if (!no_super_update) {
449 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
450 if (super == NULL)
451 super = xmlNewCatalog(1);
452
453 xmlACatalogAdd(super, BAD_CAST "CATALOG",
454 BAD_CAST argv[i + 1], NULL);
455 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000456 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000457 if (catal != NULL)
458 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
Daniel Veillard82d75332001-10-08 15:01:59 +0000459 else
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000460 ret = -1;
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000461 if (ret < 0) {
462 fprintf(stderr, "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000463 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000464 exit_value = 1;
465 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000466 if ((!no_super_update) && (noout) && (catal != NULL) &&
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000467 (xmlCatalogIsEmpty(catal))) {
468 super = xmlLoadSGMLSuperCatalog(
469 XML_SGML_DEFAULT_CATALOG);
470 if (super != NULL) {
471 ret = xmlACatalogRemove(super,
472 BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000473 if (ret < 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000474 fprintf(stderr,
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000475 "Failed to remove entry from %s\n",
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000476 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000477 exit_value = 1;
478 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000479 }
480 }
Daniel Veillard82d75332001-10-08 15:01:59 +0000481 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000482 if (noout) {
483 FILE *out;
484
485 if (xmlCatalogIsEmpty(catal)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000486 remove(argv[i + 1]);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000487 } else {
488 out = fopen(argv[i + 1], "w");
489 if (out == NULL) {
490 fprintf(stderr, "could not open %s for saving\n",
491 argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000492 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000493 noout = 0;
494 } else {
495 xmlACatalogDump(catal, out);
496 fclose(out);
497 }
498 }
Daniel Veillard1f8658a2004-08-14 21:46:31 +0000499 if (!no_super_update && super != NULL) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000500 if (xmlCatalogIsEmpty(super)) {
Daniel Veillard6eb17722001-11-04 22:19:27 +0000501 remove(XML_SGML_DEFAULT_CATALOG);
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000502 } else {
503 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
504 if (out == NULL) {
505 fprintf(stderr,
506 "could not open %s for saving\n",
507 XML_SGML_DEFAULT_CATALOG);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000508 exit_value = 2;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000509 noout = 0;
510 } else {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800511
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000512 xmlACatalogDump(super, out);
513 fclose(out);
514 }
515 }
516 }
517 } else {
518 xmlACatalogDump(catal, stdout);
519 }
520 i += 2;
521 } else {
522 if ((!strcmp(argv[i], "-add")) ||
523 (!strcmp(argv[i], "--add"))) {
524 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
525 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
526 BAD_CAST argv[i + 2]);
527 else
528 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
529 BAD_CAST argv[i + 2],
530 BAD_CAST argv[i + 3]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000531 if (ret != 0) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000532 printf("add command failed\n");
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000533 exit_value = 3;
534 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000535 i += 3;
536 } else if ((!strcmp(argv[i], "-del")) ||
537 (!strcmp(argv[i], "--del"))) {
538 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000539 if (ret < 0) {
540 fprintf(stderr, "Failed to remove entry %s\n",
541 argv[i + 1]);
542 exit_value = 1;
543 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000544 i += 1;
545 }
Daniel Veillardcda96922001-08-21 10:56:31 +0000546 }
547 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800548
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000549 } else if (shell) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000550 usershell();
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000551 } else {
552 for (i++; i < argc; i++) {
553 xmlURIPtr uri;
554 xmlChar *ans;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800555
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000556 uri = xmlParseURI(argv[i]);
557 if (uri == NULL) {
558 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
559 if (ans == NULL) {
560 printf("No entry for PUBLIC %s\n", argv[i]);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000561 exit_value = 4;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000562 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000563 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000564 xmlFree(ans);
565 }
566 } else {
567 xmlFreeURI(uri);
568 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
569 if (ans == NULL) {
570 printf("No entry for SYSTEM %s\n", argv[i]);
Daniel Veillardcccd4a02004-03-04 14:02:13 +0000571 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
572 if (ans == NULL) {
573 printf ("No entry for URI %s\n", argv[i]);
574 exit_value = 4;
575 } else {
576 printf("%s\n", (char *) ans);
577 xmlFree (ans);
578 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000579 } else {
William M. Brackc1939562003-08-05 15:52:22 +0000580 printf("%s\n", (char *) ans);
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000581 xmlFree(ans);
582 }
583 }
584 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000585 }
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000586 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
Daniel Veillardefe6c742003-12-12 14:56:03 +0000587 if (noout && filename && *filename) {
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000588 FILE *out;
589
590 out = fopen(filename, "w");
591 if (out == NULL) {
592 fprintf(stderr, "could not open %s for saving\n", filename);
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000593 exit_value = 2;
Daniel Veillarde7ead2d2001-08-22 23:44:09 +0000594 noout = 0;
595 } else {
596 xmlCatalogDump(out);
597 }
598 } else {
599 xmlCatalogDump(stdout);
600 }
601 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000602
603 /*
604 * Cleanup and check for memory leaks
605 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000606 xmlCleanupParser();
607 xmlMemoryDump();
Daniel Veillard0f5f1622002-01-20 12:42:06 +0000608 return(exit_value);
Daniel Veillard344cee72001-08-20 00:08:40 +0000609}
610#else
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000611int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
612 fprintf(stderr, "libxml was not compiled with catalog and output support\n");
Daniel Veillard344cee72001-08-20 00:08:40 +0000613 return(1);
614}
615#endif