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);
84 if (!fgets(line_read, 500, stdin))
87 len = strlen(line_read);
88 ret = (char *) malloc(len + 1);
90 memcpy (ret, line_read, len + 1);
96 static void usershell(void) {
97 char *cmdline = NULL, *cur;
106 cmdline = xmlShellReadline("> ");
111 * Parse the command itself
115 while ((*cur == ' ') || (*cur == '\t')) cur++;
117 while ((*cur != ' ') && (*cur != '\t') &&
118 (*cur != '\n') && (*cur != '\r')) {
121 command[i++] = *cur++;
130 * Parse the argument string
132 memset(arg, 0, sizeof(arg));
133 while ((*cur == ' ') || (*cur == '\t')) cur++;
135 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
143 * Parse the arguments
148 memset(argv, 0, sizeof(argv));
150 while ((*cur == ' ') || (*cur == '\t')) cur++;
154 while ((*cur != 0) && (*cur != '\'')) cur++;
161 } else if (*cur == '"') {
164 while ((*cur != 0) && (*cur != '"')) cur++;
173 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
183 * start interpreting the command
185 if (!strcmp(command, "exit") ||
186 !strcmp(command, "quit") ||
187 !strcmp(command, "bye")) {
192 if (!strcmp(command, "public")) {
194 printf("public requires 1 arguments\n");
196 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
198 printf("No entry for PUBLIC %s\n", argv[0]);
200 printf("%s\n", (char *) ans);
204 } else if (!strcmp(command, "system")) {
206 printf("system requires 1 arguments\n");
208 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
210 printf("No entry for SYSTEM %s\n", argv[0]);
212 printf("%s\n", (char *) ans);
216 } else if (!strcmp(command, "add")) {
218 if ((nbargs != 3) && (nbargs != 2)) {
219 printf("add requires 2 or 3 arguments\n");
222 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
225 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
228 printf("add command failed\n");
231 if ((nbargs != 3) && (nbargs != 2)) {
232 printf("add requires 2 or 3 arguments\n");
235 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
238 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
241 printf("add command failed\n");
244 } else if (!strcmp(command, "del")) {
246 printf("del requires 1\n");
248 ret = xmlCatalogRemove(BAD_CAST argv[0]);
250 printf("del command failed\n");
253 } else if (!strcmp(command, "resolve")) {
255 printf("resolve requires 2 arguments\n");
257 ans = xmlCatalogResolve(BAD_CAST argv[0],
260 printf("Resolver failed to find an answer\n");
262 printf("%s\n", (char *) ans);
266 } else if (!strcmp(command, "dump")) {
268 printf("dump has no arguments\n");
270 xmlCatalogDump(stdout);
272 } else if (!strcmp(command, "debug")) {
274 printf("debug has no arguments\n");
277 xmlCatalogSetDebug(verbose);
279 } else if (!strcmp(command, "quiet")) {
281 printf("quiet has no arguments\n");
285 xmlCatalogSetDebug(verbose);
288 if (strcmp(command, "help")) {
289 printf("Unrecognized command %s\n", command);
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");
294 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");
297 printf("\tdump: print the current catalog state\n");
298 printf("\tdebug: increase the verbosity level\n");
299 printf("\tquiet: decrease the verbosity level\n");
300 printf("\texit: quit the shell\n");
302 free(cmdline); /* not xmlFree here ! */
306 /************************************************************************
310 ************************************************************************/
311 static void usage(const char *name) {
312 /* split into 2 printf's to avoid overly long string (gcc warning) */
314 Usage : %s [options] catalogfile entities...\n\
315 \tParse the catalog file (void specification possibly expressed as \"\"\n\
316 \tappoints the default system one) and query it for the entities\n\
317 \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\
321 \t--add 'entry' : add an SGML entry\n", name);
323 \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\
328 \t-v --verbose : provide debug informations\n");
330 int main(int argc, char **argv) {
342 for (i = 1; i < argc ; i++) {
343 if (!strcmp(argv[i], "-"))
346 if (argv[i][0] != '-')
348 if ((!strcmp(argv[i], "-verbose")) ||
349 (!strcmp(argv[i], "-v")) ||
350 (!strcmp(argv[i], "--verbose"))) {
352 xmlCatalogSetDebug(verbose);
353 } else if ((!strcmp(argv[i], "-noout")) ||
354 (!strcmp(argv[i], "--noout"))) {
356 } else if ((!strcmp(argv[i], "-shell")) ||
357 (!strcmp(argv[i], "--shell"))) {
360 } else if ((!strcmp(argv[i], "-sgml")) ||
361 (!strcmp(argv[i], "--sgml"))) {
363 } else if ((!strcmp(argv[i], "-create")) ||
364 (!strcmp(argv[i], "--create"))) {
366 } else if ((!strcmp(argv[i], "-convert")) ||
367 (!strcmp(argv[i], "--convert"))) {
369 } else if ((!strcmp(argv[i], "-no-super-update")) ||
370 (!strcmp(argv[i], "--no-super-update"))) {
372 } else if ((!strcmp(argv[i], "-add")) ||
373 (!strcmp(argv[i], "--add"))) {
379 } else if ((!strcmp(argv[i], "-del")) ||
380 (!strcmp(argv[i], "--del"))) {
384 fprintf(stderr, "Unknown option %s\n", argv[i]);
390 for (i = 1; i < argc; i++) {
391 if ((!strcmp(argv[i], "-add")) ||
392 (!strcmp(argv[i], "--add"))) {
398 } else if ((!strcmp(argv[i], "-del")) ||
399 (!strcmp(argv[i], "--del"))) {
402 /* No catalog entry specified */
403 if (i == argc || (sgml && i + 1 == argc)) {
404 fprintf(stderr, "No catalog entry specified to remove from\n");
410 } else if (argv[i][0] == '-')
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();
419 ret = xmlLoadCatalog(argv[i]);
420 if ((ret < 0) && (create)) {
421 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
428 ret = xmlCatalogConvert();
430 if ((add) || (del)) {
431 for (i = 1; i < argc ; i++) {
432 if (!strcmp(argv[i], "-"))
435 if (argv[i][0] != '-')
437 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
438 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
443 * Maintenance of SGML catalogs.
445 xmlCatalogPtr catal = NULL;
446 xmlCatalogPtr super = NULL;
448 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
450 if ((!strcmp(argv[i], "-add")) ||
451 (!strcmp(argv[i], "--add"))) {
453 catal = xmlNewCatalog(1);
454 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
455 BAD_CAST argv[i + 2], NULL);
457 if (!no_super_update) {
458 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
460 super = xmlNewCatalog(1);
462 xmlACatalogAdd(super, BAD_CAST "CATALOG",
463 BAD_CAST argv[i + 1], NULL);
467 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
471 fprintf(stderr, "Failed to remove entry from %s\n",
475 if ((!no_super_update) && (noout) && (catal != NULL) &&
476 (xmlCatalogIsEmpty(catal))) {
477 super = xmlLoadSGMLSuperCatalog(
478 XML_SGML_DEFAULT_CATALOG);
480 ret = xmlACatalogRemove(super,
481 BAD_CAST argv[i + 1]);
484 "Failed to remove entry from %s\n",
485 XML_SGML_DEFAULT_CATALOG);
494 if (xmlCatalogIsEmpty(catal)) {
497 out = fopen(argv[i + 1], "w");
499 fprintf(stderr, "could not open %s for saving\n",
504 xmlACatalogDump(catal, out);
508 if (!no_super_update && super != NULL) {
509 if (xmlCatalogIsEmpty(super)) {
510 remove(XML_SGML_DEFAULT_CATALOG);
512 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
515 "could not open %s for saving\n",
516 XML_SGML_DEFAULT_CATALOG);
521 xmlACatalogDump(super, out);
527 xmlACatalogDump(catal, stdout);
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]);
537 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
538 BAD_CAST argv[i + 2],
539 BAD_CAST argv[i + 3]);
541 printf("add command failed\n");
545 } else if ((!strcmp(argv[i], "-del")) ||
546 (!strcmp(argv[i], "--del"))) {
547 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
549 fprintf(stderr, "Failed to remove entry %s\n",
561 for (i++; i < argc; i++) {
565 uri = xmlParseURI(argv[i]);
567 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
569 printf("No entry for PUBLIC %s\n", argv[i]);
572 printf("%s\n", (char *) ans);
577 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
579 printf("No entry for SYSTEM %s\n", argv[i]);
580 ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
582 printf ("No entry for URI %s\n", argv[i]);
585 printf("%s\n", (char *) ans);
589 printf("%s\n", (char *) ans);
595 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
596 if (noout && filename && *filename) {
599 out = fopen(filename, "w");
601 fprintf(stderr, "could not open %s for saving\n", filename);
608 xmlCatalogDump(stdout);
613 * Cleanup and check for memory leaks
620 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
621 fprintf(stderr, "libxml was not compiled with catalog and output support\n");