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") ||
185 !strcmp(command
, "quit") ||
186 !strcmp(command
, "bye")) {
191 if (!strcmp(command
, "public")) {
193 printf("public requires 1 arguments\n");
195 ans
= xmlCatalogResolvePublic((const xmlChar
*) argv
[0]);
197 printf("No entry for PUBLIC %s\n", argv
[0]);
199 printf("%s\n", (char *) ans
);
203 } else if (!strcmp(command
, "system")) {
205 printf("system requires 1 arguments\n");
207 ans
= xmlCatalogResolveSystem((const xmlChar
*) argv
[0]);
209 printf("No entry for SYSTEM %s\n", argv
[0]);
211 printf("%s\n", (char *) ans
);
215 } else if (!strcmp(command
, "add")) {
217 if ((nbargs
!= 3) && (nbargs
!= 2)) {
218 printf("add requires 2 or 3 arguments\n");
221 ret
= xmlCatalogAdd(BAD_CAST argv
[0], NULL
,
224 ret
= xmlCatalogAdd(BAD_CAST argv
[0], BAD_CAST argv
[1],
227 printf("add command failed\n");
230 if ((nbargs
!= 3) && (nbargs
!= 2)) {
231 printf("add requires 2 or 3 arguments\n");
234 ret
= xmlCatalogAdd(BAD_CAST argv
[0], NULL
,
237 ret
= xmlCatalogAdd(BAD_CAST argv
[0], BAD_CAST argv
[1],
240 printf("add command failed\n");
243 } else if (!strcmp(command
, "del")) {
245 printf("del requires 1\n");
247 ret
= xmlCatalogRemove(BAD_CAST argv
[0]);
249 printf("del command failed\n");
252 } else if (!strcmp(command
, "resolve")) {
254 printf("resolve requires 2 arguments\n");
256 ans
= xmlCatalogResolve(BAD_CAST argv
[0],
259 printf("Resolver failed to find an answer\n");
261 printf("%s\n", (char *) ans
);
265 } else if (!strcmp(command
, "dump")) {
267 printf("dump has no arguments\n");
269 xmlCatalogDump(stdout
);
271 } else if (!strcmp(command
, "debug")) {
273 printf("debug has no arguments\n");
276 xmlCatalogSetDebug(verbose
);
278 } else if (!strcmp(command
, "quiet")) {
280 printf("quiet has no arguments\n");
284 xmlCatalogSetDebug(verbose
);
287 if (strcmp(command
, "help")) {
288 printf("Unrecognized command %s\n", command
);
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");
293 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");
296 printf("\tdump: print the current catalog state\n");
297 printf("\tdebug: increase the verbosity level\n");
298 printf("\tquiet: decrease the verbosity level\n");
299 printf("\texit: quit the shell\n");
301 free(cmdline
); /* not xmlFree here ! */
305 /************************************************************************
309 ************************************************************************/
310 static void usage(const char *name
) {
311 /* split into 2 printf's to avoid overly long string (gcc warning) */
313 Usage : %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\
319 \t--add 'entry' : add an SGML entry\n", name
);
321 \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\
326 \t-v --verbose : provide debug informations\n");
328 int main(int argc
, char **argv
) {
340 for (i
= 1; i
< argc
; i
++) {
341 if (!strcmp(argv
[i
], "-"))
344 if (argv
[i
][0] != '-')
346 if ((!strcmp(argv
[i
], "-verbose")) ||
347 (!strcmp(argv
[i
], "-v")) ||
348 (!strcmp(argv
[i
], "--verbose"))) {
350 xmlCatalogSetDebug(verbose
);
351 } else if ((!strcmp(argv
[i
], "-noout")) ||
352 (!strcmp(argv
[i
], "--noout"))) {
354 } else if ((!strcmp(argv
[i
], "-shell")) ||
355 (!strcmp(argv
[i
], "--shell"))) {
358 } else if ((!strcmp(argv
[i
], "-sgml")) ||
359 (!strcmp(argv
[i
], "--sgml"))) {
361 } else if ((!strcmp(argv
[i
], "-create")) ||
362 (!strcmp(argv
[i
], "--create"))) {
364 } else if ((!strcmp(argv
[i
], "-convert")) ||
365 (!strcmp(argv
[i
], "--convert"))) {
367 } else if ((!strcmp(argv
[i
], "-no-super-update")) ||
368 (!strcmp(argv
[i
], "--no-super-update"))) {
370 } else if ((!strcmp(argv
[i
], "-add")) ||
371 (!strcmp(argv
[i
], "--add"))) {
377 } else if ((!strcmp(argv
[i
], "-del")) ||
378 (!strcmp(argv
[i
], "--del"))) {
382 fprintf(stderr
, "Unknown option %s\n", argv
[i
]);
388 for (i
= 1; i
< argc
; i
++) {
389 if ((!strcmp(argv
[i
], "-add")) ||
390 (!strcmp(argv
[i
], "--add"))) {
396 } else if ((!strcmp(argv
[i
], "-del")) ||
397 (!strcmp(argv
[i
], "--del"))) {
400 /* No catalog entry specified */
401 if (i
== argc
|| (sgml
&& i
+ 1 == argc
)) {
402 fprintf(stderr
, "No catalog entry specified to remove from\n");
408 } else if (argv
[i
][0] == '-')
411 ret
= xmlLoadCatalog(argv
[i
]);
412 if ((ret
< 0) && (create
)) {
413 xmlCatalogAdd(BAD_CAST
"catalog", BAD_CAST argv
[i
], NULL
);
419 ret
= xmlCatalogConvert();
421 if ((add
) || (del
)) {
422 for (i
= 1; i
< argc
; i
++) {
423 if (!strcmp(argv
[i
], "-"))
426 if (argv
[i
][0] != '-')
428 if (strcmp(argv
[i
], "-add") && strcmp(argv
[i
], "--add") &&
429 strcmp(argv
[i
], "-del") && strcmp(argv
[i
], "--del"))
434 * Maintenance of SGML catalogs.
436 xmlCatalogPtr catal
= NULL
;
437 xmlCatalogPtr super
= NULL
;
439 catal
= xmlLoadSGMLSuperCatalog(argv
[i
+ 1]);
441 if ((!strcmp(argv
[i
], "-add")) ||
442 (!strcmp(argv
[i
], "--add"))) {
444 catal
= xmlNewCatalog(1);
445 xmlACatalogAdd(catal
, BAD_CAST
"CATALOG",
446 BAD_CAST argv
[i
+ 2], NULL
);
448 if (!no_super_update
) {
449 super
= xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG
);
451 super
= xmlNewCatalog(1);
453 xmlACatalogAdd(super
, BAD_CAST
"CATALOG",
454 BAD_CAST argv
[i
+ 1], NULL
);
458 ret
= xmlACatalogRemove(catal
, BAD_CAST argv
[i
+ 2]);
462 fprintf(stderr
, "Failed to remove entry from %s\n",
466 if ((!no_super_update
) && (noout
) && (catal
!= NULL
) &&
467 (xmlCatalogIsEmpty(catal
))) {
468 super
= xmlLoadSGMLSuperCatalog(
469 XML_SGML_DEFAULT_CATALOG
);
471 ret
= xmlACatalogRemove(super
,
472 BAD_CAST argv
[i
+ 1]);
475 "Failed to remove entry from %s\n",
476 XML_SGML_DEFAULT_CATALOG
);
485 if (xmlCatalogIsEmpty(catal
)) {
488 out
= fopen(argv
[i
+ 1], "w");
490 fprintf(stderr
, "could not open %s for saving\n",
495 xmlACatalogDump(catal
, out
);
499 if (!no_super_update
&& super
!= NULL
) {
500 if (xmlCatalogIsEmpty(super
)) {
501 remove(XML_SGML_DEFAULT_CATALOG
);
503 out
= fopen(XML_SGML_DEFAULT_CATALOG
, "w");
506 "could not open %s for saving\n",
507 XML_SGML_DEFAULT_CATALOG
);
512 xmlACatalogDump(super
, out
);
518 xmlACatalogDump(catal
, stdout
);
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]);
528 ret
= xmlCatalogAdd(BAD_CAST argv
[i
+ 1],
529 BAD_CAST argv
[i
+ 2],
530 BAD_CAST argv
[i
+ 3]);
532 printf("add command failed\n");
536 } else if ((!strcmp(argv
[i
], "-del")) ||
537 (!strcmp(argv
[i
], "--del"))) {
538 ret
= xmlCatalogRemove(BAD_CAST argv
[i
+ 1]);
540 fprintf(stderr
, "Failed to remove entry %s\n",
552 for (i
++; i
< argc
; i
++) {
556 uri
= xmlParseURI(argv
[i
]);
558 ans
= xmlCatalogResolvePublic((const xmlChar
*) argv
[i
]);
560 printf("No entry for PUBLIC %s\n", argv
[i
]);
563 printf("%s\n", (char *) ans
);
568 ans
= xmlCatalogResolveSystem((const xmlChar
*) argv
[i
]);
570 printf("No entry for SYSTEM %s\n", argv
[i
]);
571 ans
= xmlCatalogResolveURI ((const xmlChar
*) argv
[i
]);
573 printf ("No entry for URI %s\n", argv
[i
]);
576 printf("%s\n", (char *) ans
);
580 printf("%s\n", (char *) ans
);
586 if ((!sgml
) && ((add
) || (del
) || (create
) || (convert
))) {
587 if (noout
&& filename
&& *filename
) {
590 out
= fopen(filename
, "w");
592 fprintf(stderr
, "could not open %s for saving\n", filename
);
599 xmlCatalogDump(stdout
);
604 * Cleanup and check for memory leaks
611 int main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
612 fprintf(stderr
, "libxml was not compiled with catalog and output support\n");