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>
35 static int create = 0;
38 static int convert = 0;
39 static int verbose = 0;
40 static char *filename;
42 #ifdef LIBXML_CATALOG_ENABLED
44 #ifndef XML_SGML_DEFAULT_CATALOG
45 #define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
48 /************************************************************************
52 ************************************************************************/
55 * @prompt: the prompt value
59 * Returns a pointer to it or NULL on EOF the caller is expected to
60 * free the returned string.
63 xmlShellReadline(const char *prompt) {
64 #ifdef HAVE_LIBREADLINE
67 /* Get a line from the user. */
68 line_read = readline (prompt);
70 /* If the line has any text in it, save it on the history. */
71 if (line_read && *line_read)
72 add_history (line_read);
81 fprintf(stdout, "%s", prompt);
82 if (!fgets(line_read, 500, stdin))
85 len = strlen(line_read);
86 ret = (char *) malloc(len + 1);
88 memcpy (ret, line_read, len + 1);
94 static void usershell(void) {
95 char *cmdline = NULL, *cur;
104 cmdline = xmlShellReadline("> ");
109 * Parse the command itself
113 while ((*cur == ' ') || (*cur == '\t')) cur++;
115 while ((*cur != ' ') && (*cur != '\t') &&
116 (*cur != '\n') && (*cur != '\r')) {
119 command[i++] = *cur++;
122 if (i == 0) continue;
126 * Parse the argument string
128 memset(arg, 0, sizeof(arg));
129 while ((*cur == ' ') || (*cur == '\t')) cur++;
131 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
141 * Parse the arguments
146 memset(argv, 0, sizeof(argv));
148 while ((*cur == ' ') || (*cur == '\t')) cur++;
152 while ((*cur != 0) && (*cur != '\'')) cur++;
159 } else if (*cur == '"') {
162 while ((*cur != 0) && (*cur != '"')) cur++;
171 while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
181 * start interpreting the command
183 if (!strcmp(command, "exit"))
185 if (!strcmp(command, "quit"))
187 if (!strcmp(command, "bye"))
189 if (!strcmp(command, "public")) {
191 printf("public requires 1 arguments\n");
193 ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
195 printf("No entry for PUBLIC %s\n", argv[0]);
197 printf("%s\n", (char *) ans);
201 } else if (!strcmp(command, "system")) {
203 printf("system requires 1 arguments\n");
205 ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
207 printf("No entry for SYSTEM %s\n", argv[0]);
209 printf("%s\n", (char *) ans);
213 } else if (!strcmp(command, "add")) {
215 if ((nbargs != 3) && (nbargs != 2)) {
216 printf("add requires 2 or 3 arguments\n");
219 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
222 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
225 printf("add command failed\n");
228 if ((nbargs != 3) && (nbargs != 2)) {
229 printf("add requires 2 or 3 arguments\n");
232 ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
235 ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
238 printf("add command failed\n");
241 } else if (!strcmp(command, "del")) {
243 printf("del requires 1\n");
245 ret = xmlCatalogRemove(BAD_CAST argv[0]);
247 printf("del command failed\n");
250 } else if (!strcmp(command, "resolve")) {
252 printf("resolve requires 2 arguments\n");
254 ans = xmlCatalogResolve(BAD_CAST argv[0],
257 printf("Resolver failed to find an answer\n");
259 printf("%s\n", (char *) ans);
263 } else if (!strcmp(command, "dump")) {
265 printf("dump has no arguments\n");
267 xmlCatalogDump(stdout);
269 } else if (!strcmp(command, "debug")) {
271 printf("debug has no arguments\n");
274 xmlCatalogSetDebug(verbose);
276 } else if (!strcmp(command, "quiet")) {
278 printf("quiet has no arguments\n");
282 xmlCatalogSetDebug(verbose);
285 if (strcmp(command, "help")) {
286 printf("Unrecognized command %s\n", command);
288 printf("Commands available:\n");
289 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
290 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
291 printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
292 printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
293 printf("\tdel 'values' : remove values\n");
294 printf("\tdump: print the current catalog state\n");
295 printf("\tdebug: increase the verbosity level\n");
296 printf("\tquiet: decrease the verbosity level\n");
297 printf("\texit: quit the shell\n");
299 free(cmdline); /* not xmlFree here ! */
303 /************************************************************************
307 ************************************************************************/
308 static void usage(const char *name) {
309 printf("Usage : %s [options] catalogfile entities...\n", name);
310 printf("\tParse the catalog file and query it for the entities\n");
311 printf("\t--sgml : handle SGML Super catalogs for --add and --del\n");
312 printf("\t--shell : run a shell allowing interactive queries\n");
313 printf("\t--create : create a new catalog\n");
314 printf("\t--add 'type' 'orig' 'replace' : add an entry\n");
315 printf("\t--del 'values' : remove values\n");
316 printf("\t--noout: avoid dumping the result on stdout\n");
317 printf("\t used with add or del, it saves the catalog changes\n");
318 printf("\t and with --sgml it also updates the super catalog\n");
319 printf("\t-v --verbose : provide debug informations\n");
321 int main(int argc, char **argv) {
333 for (i = 1; i < argc ; i++) {
334 if (!strcmp(argv[i], "-"))
337 if (argv[i][0] != '-')
339 if ((!strcmp(argv[i], "-verbose")) ||
340 (!strcmp(argv[i], "-v")) ||
341 (!strcmp(argv[i], "--verbose"))) {
343 xmlCatalogSetDebug(verbose);
344 } else if ((!strcmp(argv[i], "-noout")) ||
345 (!strcmp(argv[i], "--noout"))) {
347 } else if ((!strcmp(argv[i], "-shell")) ||
348 (!strcmp(argv[i], "--shell"))) {
351 } else if ((!strcmp(argv[i], "-sgml")) ||
352 (!strcmp(argv[i], "--sgml"))) {
354 } else if ((!strcmp(argv[i], "-create")) ||
355 (!strcmp(argv[i], "--create"))) {
357 } else if ((!strcmp(argv[i], "-convert")) ||
358 (!strcmp(argv[i], "--convert"))) {
360 } else if ((!strcmp(argv[i], "-add")) ||
361 (!strcmp(argv[i], "--add"))) {
367 } else if ((!strcmp(argv[i], "-del")) ||
368 (!strcmp(argv[i], "--del"))) {
372 fprintf(stderr, "Unknown option %s\n", argv[i]);
378 for (i = 1; i < argc; i++) {
379 if ((!strcmp(argv[i], "-add")) ||
380 (!strcmp(argv[i], "--add"))) {
386 } else if ((!strcmp(argv[i], "-del")) ||
387 (!strcmp(argv[i], "--del"))) {
390 } else if (argv[i][0] == '-')
393 ret = xmlLoadCatalog(argv[i]);
394 if ((ret < 0) && (create)) {
395 xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
401 ret = xmlCatalogConvert();
403 if ((add) || (del)) {
404 for (i = 1; i < argc ; i++) {
405 if (!strcmp(argv[i], "-"))
408 if (argv[i][0] != '-')
410 if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
411 strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
416 * Maintenance of SGML catalogs.
418 xmlCatalogPtr catal = NULL;
419 xmlCatalogPtr super = NULL;
421 catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
423 if ((!strcmp(argv[i], "-add")) ||
424 (!strcmp(argv[i], "--add"))) {
426 catal = xmlNewCatalog(1);
427 super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
429 super = xmlNewCatalog(1);
431 xmlACatalogAdd(catal, BAD_CAST "CATALOG",
432 BAD_CAST argv[i + 2], NULL);
433 xmlACatalogAdd(super, BAD_CAST "CATALOG",
434 BAD_CAST argv[i + 1], NULL);
437 ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
441 fprintf(stderr, "Failed to remove entry from %s\n",
445 if ((noout) && (catal != NULL) &&
446 (xmlCatalogIsEmpty(catal))) {
447 super = xmlLoadSGMLSuperCatalog(
448 XML_SGML_DEFAULT_CATALOG);
450 ret = xmlACatalogRemove(super,
451 BAD_CAST argv[i + 1]);
454 "Failed to remove entry from %s\n",
455 XML_SGML_DEFAULT_CATALOG);
464 if (xmlCatalogIsEmpty(catal)) {
467 out = fopen(argv[i + 1], "w");
469 fprintf(stderr, "could not open %s for saving\n",
474 xmlACatalogDump(catal, out);
479 if (xmlCatalogIsEmpty(super)) {
480 remove(XML_SGML_DEFAULT_CATALOG);
482 out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
485 "could not open %s for saving\n",
486 XML_SGML_DEFAULT_CATALOG);
491 xmlACatalogDump(super, out);
497 xmlACatalogDump(catal, stdout);
501 if ((!strcmp(argv[i], "-add")) ||
502 (!strcmp(argv[i], "--add"))) {
503 if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
504 ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
505 BAD_CAST argv[i + 2]);
507 ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
508 BAD_CAST argv[i + 2],
509 BAD_CAST argv[i + 3]);
511 printf("add command failed\n");
515 } else if ((!strcmp(argv[i], "-del")) ||
516 (!strcmp(argv[i], "--del"))) {
517 ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
519 fprintf(stderr, "Failed to remove entry %s\n",
531 for (i++; i < argc; i++) {
535 uri = xmlParseURI(argv[i]);
537 ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
539 printf("No entry for PUBLIC %s\n", argv[i]);
542 printf("%s\n", (char *) ans);
547 ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
549 printf("No entry for SYSTEM %s\n", argv[i]);
552 printf("%s\n", (char *) ans);
558 if ((!sgml) && ((add) || (del) || (create) || (convert))) {
562 out = fopen(filename, "w");
564 fprintf(stderr, "could not open %s for saving\n", filename);
571 xmlCatalogDump(stdout);
576 * Cleanup and check for memory leaks
583 int main(int argc, char **argv) {
584 fprintf(stderr, "libxml was not compiled with catalog support\n");