2 * xmlcatalog.c : a small utility program to handle XML catalogs
4 * See Copyright for the status of this software.
19 #ifdef HAVE_LIBREADLINE
20 #include <readline/readline.h>
21 #ifdef HAVE_LIBHISTORY
22 #include <readline/history.h>
26 #include <libxml/xmlmemory.h>
27 #include <libxml/uri.h>
28 #include <libxml/catalog.h>
29 #include <libxml/parser.h>
30 #include <libxml/globals.h>
32 #if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
36 static int create
= 0;
39 static int convert
= 0;
40 static int no_super_update
= 0;
41 static int verbose
= 0;
42 static char *filename
= NULL
;
45 #ifndef XML_SGML_DEFAULT_CATALOG
46 #define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
49 /************************************************************************
53 ************************************************************************/
56 * @prompt: the prompt value
60 * Returns a pointer to it or NULL on EOF the caller is expected to
61 * free the returned string.
64 xmlShellReadline(const char *prompt
) {
65 #ifdef HAVE_LIBREADLINE
68 /* Get a line from the user. */
69 line_read
= readline (prompt
);
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
);
82 fprintf(stdout
, "%s", prompt
);
83 if (!fgets(line_read
, 500, stdin
))
86 len
= strlen(line_read
);
87 ret
= (char *) malloc(len
+ 1);
89 memcpy (ret
, line_read
, len
+ 1);
95 static void usershell(void) {
96 char *cmdline
= NULL
, *cur
;
105 cmdline
= xmlShellReadline("> ");
110 * Parse the command itself
114 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
116 while ((*cur
!= ' ') && (*cur
!= '\t') &&
117 (*cur
!= '\n') && (*cur
!= '\r')) {
120 command
[i
++] = *cur
++;
129 * Parse the argument string
131 memset(arg
, 0, sizeof(arg
));
132 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
134 while ((*cur
!= '\n') && (*cur
!= '\r') && (*cur
!= 0)) {
142 * Parse the arguments
147 memset(argv
, 0, sizeof(argv
));
149 while ((*cur
== ' ') || (*cur
== '\t')) cur
++;
153 while ((*cur
!= 0) && (*cur
!= '\'')) cur
++;
160 } else if (*cur
== '"') {
163 while ((*cur
!= 0) && (*cur
!= '"')) cur
++;
172 while ((*cur
!= 0) && (*cur
!= ' ') && (*cur
!= '\t'))
182 * start interpreting the command
184 if (!strcmp(command
, "exit"))
186 if (!strcmp(command
, "quit"))
188 if (!strcmp(command
, "bye"))
190 if (!strcmp(command
, "public")) {
192 printf("public requires 1 arguments\n");
194 ans
= xmlCatalogResolvePublic((const xmlChar
*) argv
[0]);
196 printf("No entry for PUBLIC %s\n", argv
[0]);
198 printf("%s\n", (char *) ans
);
202 } else if (!strcmp(command
, "system")) {
204 printf("system requires 1 arguments\n");
206 ans
= xmlCatalogResolveSystem((const xmlChar
*) argv
[0]);
208 printf("No entry for SYSTEM %s\n", argv
[0]);
210 printf("%s\n", (char *) ans
);
214 } else if (!strcmp(command
, "add")) {
216 if ((nbargs
!= 3) && (nbargs
!= 2)) {
217 printf("add requires 2 or 3 arguments\n");
220 ret
= xmlCatalogAdd(BAD_CAST argv
[0], NULL
,
223 ret
= xmlCatalogAdd(BAD_CAST argv
[0], BAD_CAST argv
[1],
226 printf("add command failed\n");
229 if ((nbargs
!= 3) && (nbargs
!= 2)) {
230 printf("add requires 2 or 3 arguments\n");
233 ret
= xmlCatalogAdd(BAD_CAST argv
[0], NULL
,
236 ret
= xmlCatalogAdd(BAD_CAST argv
[0], BAD_CAST argv
[1],
239 printf("add command failed\n");
242 } else if (!strcmp(command
, "del")) {
244 printf("del requires 1\n");
246 ret
= xmlCatalogRemove(BAD_CAST argv
[0]);
248 printf("del command failed\n");
251 } else if (!strcmp(command
, "resolve")) {
253 printf("resolve requires 2 arguments\n");
255 ans
= xmlCatalogResolve(BAD_CAST argv
[0],
258 printf("Resolver failed to find an answer\n");
260 printf("%s\n", (char *) ans
);
264 } else if (!strcmp(command
, "dump")) {
266 printf("dump has no arguments\n");
268 xmlCatalogDump(stdout
);
270 } else if (!strcmp(command
, "debug")) {
272 printf("debug has no arguments\n");
275 xmlCatalogSetDebug(verbose
);
277 } else if (!strcmp(command
, "quiet")) {
279 printf("quiet has no arguments\n");
283 xmlCatalogSetDebug(verbose
);
286 if (strcmp(command
, "help")) {
287 printf("Unrecognized command %s\n", command
);
289 printf("Commands available:\n");
290 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
291 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
292 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
293 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
294 printf("\tdel 'values' : remove values\n");
295 printf("\tdump: print the current catalog state\n");
296 printf("\tdebug: increase the verbosity level\n");
297 printf("\tquiet: decrease the verbosity level\n");
298 printf("\texit: quit the shell\n");
300 free(cmdline
); /* not xmlFree here ! */
304 /************************************************************************
308 ************************************************************************/
309 static void usage(const char *name
) {
310 /* split into 2 printf's to avoid overly long string (gcc warning) */
312 Usage : %s [options] catalogfile entities...\n\
313 \tParse the catalog file and query it for the entities\n\
314 \t--sgml : handle SGML Super catalogs for --add and --del\n\
315 \t--shell : run a shell allowing interactive queries\n\
316 \t--create : create a new catalog\n\
317 \t--add 'type' 'orig' 'replace' : add an XML entry\n\
318 \t--add 'entry' : add an SGML entry\n", name
);
320 \t--del 'values' : remove values\n\
321 \t--noout: avoid dumping the result on stdout\n\
322 \t used with --add or --del, it saves the catalog changes\n\
323 \t and with --sgml it automatically updates the super catalog\n\
324 \t--no-super-update: do not update the SGML super catalog\n\
325 \t-v --verbose : provide debug informations\n");
327 int main(int argc
, char **argv
) {
339 for (i
= 1; i
< argc
; i
++) {
340 if (!strcmp(argv
[i
], "-"))
343 if (argv
[i
][0] != '-')
345 if ((!strcmp(argv
[i
], "-verbose")) ||
346 (!strcmp(argv
[i
], "-v")) ||
347 (!strcmp(argv
[i
], "--verbose"))) {
349 xmlCatalogSetDebug(verbose
);
350 } else if ((!strcmp(argv
[i
], "-noout")) ||
351 (!strcmp(argv
[i
], "--noout"))) {
353 } else if ((!strcmp(argv
[i
], "-shell")) ||
354 (!strcmp(argv
[i
], "--shell"))) {
357 } else if ((!strcmp(argv
[i
], "-sgml")) ||
358 (!strcmp(argv
[i
], "--sgml"))) {
360 } else if ((!strcmp(argv
[i
], "-create")) ||
361 (!strcmp(argv
[i
], "--create"))) {
363 } else if ((!strcmp(argv
[i
], "-convert")) ||
364 (!strcmp(argv
[i
], "--convert"))) {
366 } else if ((!strcmp(argv
[i
], "-no-super-update")) ||
367 (!strcmp(argv
[i
], "--no-super-update"))) {
369 } else if ((!strcmp(argv
[i
], "-add")) ||
370 (!strcmp(argv
[i
], "--add"))) {
376 } else if ((!strcmp(argv
[i
], "-del")) ||
377 (!strcmp(argv
[i
], "--del"))) {
381 fprintf(stderr
, "Unknown option %s\n", argv
[i
]);
387 for (i
= 1; i
< argc
; i
++) {
388 if ((!strcmp(argv
[i
], "-add")) ||
389 (!strcmp(argv
[i
], "--add"))) {
395 } else if ((!strcmp(argv
[i
], "-del")) ||
396 (!strcmp(argv
[i
], "--del"))) {
399 /* No catalog entry specified */
400 if (i
== argc
|| (sgml
&& i
+ 1 == argc
)) {
401 fprintf(stderr
, "No catalog entry specified to remove from\n");
407 } else if (argv
[i
][0] == '-')
410 ret
= xmlLoadCatalog(argv
[i
]);
411 if ((ret
< 0) && (create
)) {
412 xmlCatalogAdd(BAD_CAST
"catalog", BAD_CAST argv
[i
], NULL
);
418 ret
= xmlCatalogConvert();
420 if ((add
) || (del
)) {
421 for (i
= 1; i
< argc
; i
++) {
422 if (!strcmp(argv
[i
], "-"))
425 if (argv
[i
][0] != '-')
427 if (strcmp(argv
[i
], "-add") && strcmp(argv
[i
], "--add") &&
428 strcmp(argv
[i
], "-del") && strcmp(argv
[i
], "--del"))
433 * Maintenance of SGML catalogs.
435 xmlCatalogPtr catal
= NULL
;
436 xmlCatalogPtr super
= NULL
;
438 catal
= xmlLoadSGMLSuperCatalog(argv
[i
+ 1]);
440 if ((!strcmp(argv
[i
], "-add")) ||
441 (!strcmp(argv
[i
], "--add"))) {
443 catal
= xmlNewCatalog(1);
444 xmlACatalogAdd(catal
, BAD_CAST
"CATALOG",
445 BAD_CAST argv
[i
+ 2], NULL
);
447 if (!no_super_update
) {
448 super
= xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG
);
450 super
= xmlNewCatalog(1);
452 xmlACatalogAdd(super
, BAD_CAST
"CATALOG",
453 BAD_CAST argv
[i
+ 1], NULL
);
457 ret
= xmlACatalogRemove(catal
, BAD_CAST argv
[i
+ 2]);
461 fprintf(stderr
, "Failed to remove entry from %s\n",
465 if ((!no_super_update
) && (noout
) && (catal
!= NULL
) &&
466 (xmlCatalogIsEmpty(catal
))) {
467 super
= xmlLoadSGMLSuperCatalog(
468 XML_SGML_DEFAULT_CATALOG
);
470 ret
= xmlACatalogRemove(super
,
471 BAD_CAST argv
[i
+ 1]);
474 "Failed to remove entry from %s\n",
475 XML_SGML_DEFAULT_CATALOG
);
484 if (xmlCatalogIsEmpty(catal
)) {
487 out
= fopen(argv
[i
+ 1], "w");
489 fprintf(stderr
, "could not open %s for saving\n",
494 xmlACatalogDump(catal
, out
);
498 if (!no_super_update
&& super
!= NULL
) {
499 if (xmlCatalogIsEmpty(super
)) {
500 remove(XML_SGML_DEFAULT_CATALOG
);
502 out
= fopen(XML_SGML_DEFAULT_CATALOG
, "w");
505 "could not open %s for saving\n",
506 XML_SGML_DEFAULT_CATALOG
);
511 xmlACatalogDump(super
, out
);
517 xmlACatalogDump(catal
, stdout
);
521 if ((!strcmp(argv
[i
], "-add")) ||
522 (!strcmp(argv
[i
], "--add"))) {
523 if ((argv
[i
+ 3] == NULL
) || (argv
[i
+ 3][0] == 0))
524 ret
= xmlCatalogAdd(BAD_CAST argv
[i
+ 1], NULL
,
525 BAD_CAST argv
[i
+ 2]);
527 ret
= xmlCatalogAdd(BAD_CAST argv
[i
+ 1],
528 BAD_CAST argv
[i
+ 2],
529 BAD_CAST argv
[i
+ 3]);
531 printf("add command failed\n");
535 } else if ((!strcmp(argv
[i
], "-del")) ||
536 (!strcmp(argv
[i
], "--del"))) {
537 ret
= xmlCatalogRemove(BAD_CAST argv
[i
+ 1]);
539 fprintf(stderr
, "Failed to remove entry %s\n",
551 for (i
++; i
< argc
; i
++) {
555 uri
= xmlParseURI(argv
[i
]);
557 ans
= xmlCatalogResolvePublic((const xmlChar
*) argv
[i
]);
559 printf("No entry for PUBLIC %s\n", argv
[i
]);
562 printf("%s\n", (char *) ans
);
567 ans
= xmlCatalogResolveSystem((const xmlChar
*) argv
[i
]);
569 printf("No entry for SYSTEM %s\n", argv
[i
]);
570 ans
= xmlCatalogResolveURI ((const xmlChar
*) argv
[i
]);
572 printf ("No entry for URI %s\n", argv
[i
]);
575 printf("%s\n", (char *) ans
);
579 printf("%s\n", (char *) ans
);
585 if ((!sgml
) && ((add
) || (del
) || (create
) || (convert
))) {
586 if (noout
&& filename
&& *filename
) {
589 out
= fopen(filename
, "w");
591 fprintf(stderr
, "could not open %s for saving\n", filename
);
598 xmlCatalogDump(stdout
);
603 * Cleanup and check for memory leaks
610 int main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
611 fprintf(stderr
, "libxml was not compiled with catalog and output support\n");