1/*
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
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#ifdef HAVE_LIBREADLINE
20#include <readline/readline.h>
21#ifdef HAVE_LIBHISTORY
22#include <readline/history.h>
23#endif
24#endif
25
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>
31
32#if defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
33static int shell = 0;
34static int sgml = 0;
35static int noout = 0;
36static int create = 0;
37static int add = 0;
38static int del = 0;
39static int convert = 0;
40static int no_super_update = 0;
41static int verbose = 0;
42static char *filename = NULL;
43
44
45#ifndef XML_SGML_DEFAULT_CATALOG
46#define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog"
47#endif
48
49/************************************************************************
50 *									*
51 *			Shell Interface					*
52 *									*
53 ************************************************************************/
54/**
55 * xmlShellReadline:
56 * @prompt:  the prompt value
57 *
58 * Read a string
59 *
60 * 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];
78    char *ret;
79    int len;
80
81    if (prompt != NULL)
82	fprintf(stdout, "%s", prompt);
83    fflush(stdout);
84    if (!fgets(line_read, 500, stdin))
85        return(NULL);
86    line_read[500] = 0;
87    len = strlen(line_read);
88    ret = (char *) malloc(len + 1);
89    if (ret != NULL) {
90	memcpy (ret, line_read, len + 1);
91    }
92    return(ret);
93#endif
94}
95
96static void usershell(void) {
97    char *cmdline = NULL, *cur;
98    int nbargs;
99    char command[100];
100    char arg[400];
101    char *argv[20];
102    int i, ret;
103    xmlChar *ans;
104
105    while (1) {
106	cmdline = xmlShellReadline("> ");
107	if (cmdline == NULL)
108	    return;
109
110	/*
111	 * Parse the command itself
112	 */
113	cur = cmdline;
114	nbargs = 0;
115	while ((*cur == ' ') || (*cur == '\t')) cur++;
116	i = 0;
117	while ((*cur != ' ') && (*cur != '\t') &&
118	       (*cur != '\n') && (*cur != '\r')) {
119	    if (*cur == 0)
120		break;
121	    command[i++] = *cur++;
122	}
123	command[i] = 0;
124	if (i == 0) {
125	    free(cmdline);
126	    continue;
127	}
128
129	/*
130	 * Parse the argument string
131	 */
132	memset(arg, 0, sizeof(arg));
133	while ((*cur == ' ') || (*cur == '\t')) cur++;
134	i = 0;
135	while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
136	    if (*cur == 0)
137		break;
138	    arg[i++] = *cur++;
139	}
140	arg[i] = 0;
141
142	/*
143	 * Parse the arguments
144	 */
145	i = 0;
146	nbargs = 0;
147	cur = arg;
148	memset(argv, 0, sizeof(argv));
149	while (*cur != 0) {
150	    while ((*cur == ' ') || (*cur == '\t')) cur++;
151	    if (*cur == '\'') {
152		cur++;
153		argv[i] = cur;
154		while ((*cur != 0) && (*cur != '\'')) cur++;
155		if (*cur == '\'') {
156		    *cur = 0;
157		    nbargs++;
158		    i++;
159		    cur++;
160		}
161	    } else if (*cur == '"') {
162		cur++;
163		argv[i] = cur;
164		while ((*cur != 0) && (*cur != '"')) cur++;
165		if (*cur == '"') {
166		    *cur = 0;
167		    nbargs++;
168		    i++;
169		    cur++;
170		}
171	    } else {
172		argv[i] = cur;
173		while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
174		    cur++;
175		*cur = 0;
176		nbargs++;
177		i++;
178		cur++;
179	    }
180	}
181
182	/*
183	 * start interpreting the command
184	 */
185	if (!strcmp(command, "exit") ||
186	    !strcmp(command, "quit") ||
187	    !strcmp(command, "bye")) {
188	    free(cmdline);
189	    break;
190	}
191
192	if (!strcmp(command, "public")) {
193	    if (nbargs != 1) {
194		printf("public requires 1 arguments\n");
195	    } else {
196		ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]);
197		if (ans == NULL) {
198		    printf("No entry for PUBLIC %s\n", argv[0]);
199		} else {
200		    printf("%s\n", (char *) ans);
201		    xmlFree(ans);
202		}
203	    }
204	} else if (!strcmp(command, "system")) {
205	    if (nbargs != 1) {
206		printf("system requires 1 arguments\n");
207	    } else {
208		ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]);
209		if (ans == NULL) {
210		    printf("No entry for SYSTEM %s\n", argv[0]);
211		} else {
212		    printf("%s\n", (char *) ans);
213		    xmlFree(ans);
214		}
215	    }
216	} else if (!strcmp(command, "add")) {
217	    if (sgml) {
218		if ((nbargs != 3) && (nbargs != 2)) {
219		    printf("add requires 2 or 3 arguments\n");
220		} else {
221		    if (argv[2] == NULL)
222			ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
223					    BAD_CAST argv[1]);
224		    else
225			ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
226					    BAD_CAST argv[2]);
227		    if (ret != 0)
228			printf("add command failed\n");
229		}
230	    } else {
231		if ((nbargs != 3) && (nbargs != 2)) {
232		    printf("add requires 2 or 3 arguments\n");
233		} else {
234		    if (argv[2] == NULL)
235			ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
236					    BAD_CAST argv[1]);
237		    else
238			ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
239					    BAD_CAST argv[2]);
240		    if (ret != 0)
241			printf("add command failed\n");
242		}
243	    }
244	} else if (!strcmp(command, "del")) {
245	    if (nbargs != 1) {
246		printf("del requires 1\n");
247	    } else {
248		ret = xmlCatalogRemove(BAD_CAST argv[0]);
249		if (ret <= 0)
250		    printf("del command failed\n");
251
252	    }
253	} else if (!strcmp(command, "resolve")) {
254	    if (nbargs != 2) {
255		printf("resolve requires 2 arguments\n");
256	    } else {
257		ans = xmlCatalogResolve(BAD_CAST argv[0],
258			                BAD_CAST argv[1]);
259		if (ans == NULL) {
260		    printf("Resolver failed to find an answer\n");
261		} else {
262		    printf("%s\n", (char *) ans);
263		    xmlFree(ans);
264		}
265	    }
266	} else if (!strcmp(command, "dump")) {
267	    if (nbargs != 0) {
268		printf("dump has no arguments\n");
269	    } else {
270		xmlCatalogDump(stdout);
271	    }
272	} else if (!strcmp(command, "debug")) {
273	    if (nbargs != 0) {
274		printf("debug has no arguments\n");
275	    } else {
276		verbose++;
277		xmlCatalogSetDebug(verbose);
278	    }
279	} else if (!strcmp(command, "quiet")) {
280	    if (nbargs != 0) {
281		printf("quiet has no arguments\n");
282	    } else {
283		if (verbose > 0)
284		    verbose--;
285		xmlCatalogSetDebug(verbose);
286	    }
287	} else {
288	    if (strcmp(command, "help")) {
289		printf("Unrecognized command %s\n", command);
290	    }
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");
301	}
302	free(cmdline); /* not xmlFree here ! */
303    }
304}
305
306/************************************************************************
307 *									*
308 *			Main						*
309 *									*
310 ************************************************************************/
311static void usage(const char *name) {
312    /* split into 2 printf's to avoid overly long string (gcc warning) */
313    printf("\
314Usage : %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);
322    printf("\
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");
329}
330int main(int argc, char **argv) {
331    int i;
332    int ret;
333    int exit_value = 0;
334
335
336    if (argc <= 1) {
337	usage(argv[0]);
338	return(1);
339    }
340
341    LIBXML_TEST_VERSION
342    for (i = 1; i < argc ; i++) {
343	if (!strcmp(argv[i], "-"))
344	    break;
345
346	if (argv[i][0] != '-')
347	    break;
348	if ((!strcmp(argv[i], "-verbose")) ||
349	    (!strcmp(argv[i], "-v")) ||
350	    (!strcmp(argv[i], "--verbose"))) {
351	    verbose++;
352	    xmlCatalogSetDebug(verbose);
353	} else if ((!strcmp(argv[i], "-noout")) ||
354	    (!strcmp(argv[i], "--noout"))) {
355            noout = 1;
356	} else if ((!strcmp(argv[i], "-shell")) ||
357	    (!strcmp(argv[i], "--shell"))) {
358	    shell++;
359            noout = 1;
360	} else if ((!strcmp(argv[i], "-sgml")) ||
361	    (!strcmp(argv[i], "--sgml"))) {
362	    sgml++;
363	} else if ((!strcmp(argv[i], "-create")) ||
364	    (!strcmp(argv[i], "--create"))) {
365	    create++;
366	} else if ((!strcmp(argv[i], "-convert")) ||
367	    (!strcmp(argv[i], "--convert"))) {
368	    convert++;
369	} else if ((!strcmp(argv[i], "-no-super-update")) ||
370	    (!strcmp(argv[i], "--no-super-update"))) {
371	    no_super_update++;
372	} else if ((!strcmp(argv[i], "-add")) ||
373	    (!strcmp(argv[i], "--add"))) {
374	    if (sgml)
375		i += 2;
376	    else
377		i += 3;
378	    add++;
379	} else if ((!strcmp(argv[i], "-del")) ||
380	    (!strcmp(argv[i], "--del"))) {
381	    i += 1;
382	    del++;
383	} else {
384	    fprintf(stderr, "Unknown option %s\n", argv[i]);
385	    usage(argv[0]);
386	    return(1);
387	}
388    }
389
390    for (i = 1; i < argc; i++) {
391	if ((!strcmp(argv[i], "-add")) ||
392	    (!strcmp(argv[i], "--add"))) {
393	    if (sgml)
394		i += 2;
395	    else
396		i += 3;
397	    continue;
398	} else if ((!strcmp(argv[i], "-del")) ||
399	    (!strcmp(argv[i], "--del"))) {
400	    i += 1;
401
402	    /* No catalog entry specified */
403	    if (i == argc || (sgml && i + 1 == argc)) {
404		fprintf(stderr, "No catalog entry specified to remove from\n");
405		usage (argv[0]);
406		return(1);
407	    }
408
409	    continue;
410	} else if (argv[i][0] == '-')
411	    continue;
412
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();
417	} else {
418	    filename = argv[i];
419	    ret = xmlLoadCatalog(argv[i]);
420	    if ((ret < 0) && (create)) {
421		xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL);
422	    }
423	}
424	break;
425    }
426
427    if (convert)
428        ret = xmlCatalogConvert();
429
430    if ((add) || (del)) {
431	for (i = 1; i < argc ; i++) {
432	    if (!strcmp(argv[i], "-"))
433		break;
434
435	    if (argv[i][0] != '-')
436		continue;
437	    if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") &&
438		strcmp(argv[i], "-del") && strcmp(argv[i], "--del"))
439		continue;
440
441	    if (sgml) {
442		/*
443		 * Maintenance of SGML catalogs.
444		 */
445		xmlCatalogPtr catal = NULL;
446		xmlCatalogPtr super = NULL;
447
448		catal = xmlLoadSGMLSuperCatalog(argv[i + 1]);
449
450		if ((!strcmp(argv[i], "-add")) ||
451		    (!strcmp(argv[i], "--add"))) {
452		    if (catal == NULL)
453			catal = xmlNewCatalog(1);
454		    xmlACatalogAdd(catal, BAD_CAST "CATALOG",
455					 BAD_CAST argv[i + 2], NULL);
456
457		    if (!no_super_update) {
458			super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG);
459			if (super == NULL)
460			    super = xmlNewCatalog(1);
461
462			xmlACatalogAdd(super, BAD_CAST "CATALOG",
463					     BAD_CAST argv[i + 1], NULL);
464		    }
465		} else {
466		    if (catal != NULL)
467			ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]);
468		    else
469			ret = -1;
470		    if (ret < 0) {
471			fprintf(stderr, "Failed to remove entry from %s\n",
472				argv[i + 1]);
473			exit_value = 1;
474		    }
475		    if ((!no_super_update) && (noout) && (catal != NULL) &&
476			(xmlCatalogIsEmpty(catal))) {
477			super = xmlLoadSGMLSuperCatalog(
478				   XML_SGML_DEFAULT_CATALOG);
479			if (super != NULL) {
480			    ret = xmlACatalogRemove(super,
481				    BAD_CAST argv[i + 1]);
482			    if (ret < 0) {
483				fprintf(stderr,
484					"Failed to remove entry from %s\n",
485					XML_SGML_DEFAULT_CATALOG);
486				exit_value = 1;
487			    }
488			}
489		    }
490		}
491		if (noout) {
492		    FILE *out;
493
494		    if (xmlCatalogIsEmpty(catal)) {
495			remove(argv[i + 1]);
496		    } else {
497			out = fopen(argv[i + 1], "w");
498			if (out == NULL) {
499			    fprintf(stderr, "could not open %s for saving\n",
500				    argv[i + 1]);
501			    exit_value = 2;
502			    noout = 0;
503			} else {
504			    xmlACatalogDump(catal, out);
505			    fclose(out);
506			}
507		    }
508		    if (!no_super_update && super != NULL) {
509			if (xmlCatalogIsEmpty(super)) {
510			    remove(XML_SGML_DEFAULT_CATALOG);
511			} else {
512			    out = fopen(XML_SGML_DEFAULT_CATALOG, "w");
513			    if (out == NULL) {
514				fprintf(stderr,
515					"could not open %s for saving\n",
516					XML_SGML_DEFAULT_CATALOG);
517				exit_value = 2;
518				noout = 0;
519			    } else {
520
521				xmlACatalogDump(super, out);
522				fclose(out);
523			    }
524			}
525		    }
526		} else {
527		    xmlACatalogDump(catal, stdout);
528		}
529		i += 2;
530	    } else {
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]);
536			else
537			    ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
538						BAD_CAST argv[i + 2],
539						BAD_CAST argv[i + 3]);
540			if (ret != 0) {
541			    printf("add command failed\n");
542			    exit_value = 3;
543			}
544			i += 3;
545		} else if ((!strcmp(argv[i], "-del")) ||
546		    (!strcmp(argv[i], "--del"))) {
547		    ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
548		    if (ret < 0) {
549			fprintf(stderr, "Failed to remove entry %s\n",
550				argv[i + 1]);
551			exit_value = 1;
552		    }
553		    i += 1;
554		}
555	    }
556	}
557
558    } else if (shell) {
559	usershell();
560    } else {
561	for (i++; i < argc; i++) {
562	    xmlURIPtr uri;
563	    xmlChar *ans;
564
565	    uri = xmlParseURI(argv[i]);
566	    if (uri == NULL) {
567		ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]);
568		if (ans == NULL) {
569		    printf("No entry for PUBLIC %s\n", argv[i]);
570		    exit_value = 4;
571		} else {
572		    printf("%s\n", (char *) ans);
573		    xmlFree(ans);
574		}
575	    } else {
576                xmlFreeURI(uri);
577		ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]);
578		if (ans == NULL) {
579		    printf("No entry for SYSTEM %s\n", argv[i]);
580		    ans = xmlCatalogResolveURI ((const xmlChar *) argv[i]);
581		    if (ans == NULL) {
582			printf ("No entry for URI %s\n", argv[i]);
583		        exit_value = 4;
584		    } else {
585		        printf("%s\n", (char *) ans);
586			xmlFree (ans);
587		    }
588		} else {
589		    printf("%s\n", (char *) ans);
590		    xmlFree(ans);
591		}
592	    }
593	}
594    }
595    if ((!sgml) && ((add) || (del) || (create) || (convert))) {
596	if (noout && filename && *filename) {
597	    FILE *out;
598
599	    out = fopen(filename, "w");
600	    if (out == NULL) {
601		fprintf(stderr, "could not open %s for saving\n", filename);
602		exit_value = 2;
603		noout = 0;
604	    } else {
605		xmlCatalogDump(out);
606	    }
607	} else {
608	    xmlCatalogDump(stdout);
609	}
610    }
611
612    /*
613     * Cleanup and check for memory leaks
614     */
615    xmlCleanupParser();
616    xmlMemoryDump();
617    return(exit_value);
618}
619#else
620int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
621    fprintf(stderr, "libxml was not compiled with catalog and output support\n");
622    return(1);
623}
624#endif
625