xmlcatalog.c 8.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * xmlcatalog.c : a small utility program to handle XML catalogs
 *
 * See Copyright for the status of this software.
 *
 * daniel@veillard.com
 */

#include "libxml.h"

#include <string.h>
#include <stdio.h>
#include <stdarg.h>

Bjorn Reese's avatar
Bjorn Reese committed
15
16
17
18
19
20
21
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#ifdef HAVE_LIBHISTORY
#include <readline/history.h>
#endif
#endif

22
23
24
25
26
27
28
#include <libxml/xmlmemory.h>
#include <libxml/uri.h>
#include <libxml/catalog.h>
#include <libxml/parser.h>

static int shell = 0;
static int noout = 0;
29
30
static int add = 0;
static int del = 0;
31
static int verbose = 0;
32
static char *filename;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#ifdef LIBXML_CATALOG_ENABLED
/************************************************************************
 * 									*
 * 			Shell Interface					*
 * 									*
 ************************************************************************/
/**
 * xmlShellReadline:
 * @prompt:  the prompt value
 *
 * Read a string
 * 
 * Returns a pointer to it or NULL on EOF the caller is expected to
 *     free the returned string.
 */
static char *
xmlShellReadline(const char *prompt) {
#ifdef HAVE_LIBREADLINE
    char *line_read;

    /* Get a line from the user. */
    line_read = readline (prompt);

    /* If the line has any text in it, save it on the history. */
    if (line_read && *line_read)
	add_history (line_read);

    return (line_read);
#else
    char line_read[501];

    if (prompt != NULL)
	fprintf(stdout, "%s", prompt);
    if (!fgets(line_read, 500, stdin))
        return(NULL);
    line_read[500] = 0;
    return(strdup(line_read));
#endif
}


static void usershell(void) {
    char *cmdline = NULL, *cur;
    int nbargs;
    char command[100];
    char arg[400];
80
81
    char *argv[20];
    int i, ret;
82
    const xmlChar *answer;
83
    xmlChar *ans;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

    while (1) {
	cmdline = xmlShellReadline("> ");
	if (cmdline == NULL)
	    return;

	/*
	 * Parse the command itself
	 */
	cur = cmdline;
	nbargs = 0;
	while ((*cur == ' ') || (*cur == '\t')) cur++;
	i = 0;
	while ((*cur != ' ') && (*cur != '\t') &&
	       (*cur != '\n') && (*cur != '\r')) {
	    if (*cur == 0)
		break;
	    command[i++] = *cur++;
	}
	command[i] = 0;
	if (i == 0) continue;
	nbargs++;

	/*
108
	 * Parse the argument string
109
	 */
110
	memset(arg, 0, sizeof(arg));
111
112
113
114
115
116
117
118
119
120
121
	while ((*cur == ' ') || (*cur == '\t')) cur++;
	i = 0;
	while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
	    if (*cur == 0)
		break;
	    arg[i++] = *cur++;
	}
	arg[i] = 0;
	if (i != 0) 
	    nbargs++;

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	/*
	 * Parse the arguments
	 */
	i = 0;
	nbargs = 0;
	cur = arg;
	memset(argv, 0, sizeof(argv));
	while (*cur != 0) {
	    while ((*cur == ' ') || (*cur == '\t')) cur++;
	    if (*cur == '\'') {
		cur++;
		argv[i] = cur;
		while ((*cur != 0) && (*cur != '\'')) cur++;
		if (*cur == '\'') {
		    *cur = 0;
		    nbargs++;
		    i++;
		    cur++;
		}
	    } else if (*cur == '"') { 
		cur++;
		argv[i] = cur;
		while ((*cur != 0) && (*cur != '"')) cur++;
		if (*cur == '"') {
		    *cur = 0;
		    nbargs++;
		    i++;
		    cur++;
		}
	    } else {
		argv[i] = cur;
		while ((*cur != 0) && (*cur != ' ') && (*cur != '\t'))
		    cur++;
		*cur = 0;
		nbargs++;
		i++;
		cur++;
	    }
	}

162
163
164
165
166
167
168
169
170
171
	/*
	 * start interpreting the command
	 */
        if (!strcmp(command, "exit"))
	    break;
        if (!strcmp(command, "quit"))
	    break;
        if (!strcmp(command, "bye"))
	    break;
	if (!strcmp(command, "public")) {
172
173
	    if (nbargs != 1) {
		printf("public requires 1 arguments\n");
174
	    } else {
175
176
177
178
179
180
		answer = xmlCatalogGetPublic((const xmlChar *) argv[0]);
		if (answer == NULL) {
		    printf("No entry for PUBLIC %s\n", argv[0]);
		} else {
		    printf("%s\n", answer);
		}
181
182
	    }
	} else if (!strcmp(command, "system")) {
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	    if (nbargs != 1) {
		printf("system requires 1 arguments\n");
	    } else {
		answer = xmlCatalogGetSystem((const xmlChar *) argv[0]);
		if (answer == NULL) {
		    printf("No entry for SYSTEM %s\n", argv[0]);
		} else {
		    printf("%s\n", answer);
		}
	    }
	} else if (!strcmp(command, "add")) {
	    if ((nbargs != 3) && (nbargs != 2)) {
		printf("add requires 2 or 3 arguments\n");
	    } else {
		if (argv[2] == NULL)
		    ret = xmlCatalogAdd(BAD_CAST argv[0], NULL,
			                BAD_CAST argv[1]);
		else
		    ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1],
			                BAD_CAST argv[2]);
		if (ret != 0)
		    printf("add command failed\n");
	    }
	} else if (!strcmp(command, "del")) {
	    if (nbargs != 1) {
		printf("del requires 1\n");
	    } else {
		ret = xmlCatalogRemove(BAD_CAST argv[0]);
		if (ret <= 0)
		    printf("del command failed\n");

	    }
	} else if (!strcmp(command, "resolve")) {
	    if (nbargs != 2) {
		printf("resolve requires 2 arguments\n");
218
	    } else {
219
220
221
222
223
224
225
226
		ans = xmlCatalogResolve(BAD_CAST argv[0],
			                BAD_CAST argv[1]);
		if (ans == NULL) {
		    printf("Resolver failed to find an answer\n");
		} else {
		    printf("%s\n", ans);
		    xmlFree(ans);
		}
227
228
	    }
	} else if (!strcmp(command, "dump")) {
229
230
231
232
233
	    if (nbargs != 0) {
		printf("dump has no arguments\n");
	    } else {
		xmlCatalogDump(stdout);
	    }
234
235
236
237
238
239
240
	} else {
	    if (strcmp(command, "help")) {
		printf("Unrecognized command %s\n", command);
	    }
	    printf("Commands available:\n");
	    printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
	    printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
241
242
243
	    printf("\tresolve PublicID SystemID: do a full resolver lookup\n");
	    printf("\tadd 'type' 'orig' 'replace' : add an entry\n");
	    printf("\tdel 'values' : remove values\n");
244
245
246
247
248
249
250
251
252
253
254
255
256
	    printf("\tdump: print the current catalog state\n");
	    printf("\texit:  quit the shell\n");
	} 
	free(cmdline); /* not xmlFree here ! */
    }
}

/************************************************************************
 * 									*
 * 			Main						*
 * 									*
 ************************************************************************/
static void usage(const char *name) {
257
258
    printf("Usage : %s [options] catalogfile\n", name);
    printf("\tParse the catalog file and output the result of the parsing\n");
259
    printf("\t--shell : run a shell allowing interactive queries\n");
260
261
262
263
    printf("\t--add 'type' 'orig' 'replace' : add an entry\n");
    printf("\t--del 'values' : remove values\n");
    printf("\t--noout: avoid dumping the result on stdout\n");
    printf("\t         used with add or del, it saves the catalog changes\n");
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    printf("\t-v --verbose : provide debug informations\n");
}
int main(int argc, char **argv) {
    int i;

    if (argc <= 1) {
	usage(argv[0]);
	return(1);
    }

    LIBXML_TEST_VERSION
    for (i = 1; i < argc ; i++) {
	if (!strcmp(argv[i], "-"))
	    break;

	if (argv[i][0] != '-')
	    continue;
	if ((!strcmp(argv[i], "-verbose")) ||
	    (!strcmp(argv[i], "-v")) ||
	    (!strcmp(argv[i], "--verbose"))) {
	    verbose++;
	    xmlCatalogSetDebug(verbose);
286
287
288
	} else if ((!strcmp(argv[i], "-noout")) ||
	    (!strcmp(argv[i], "--noout"))) {
            noout = 1;
289
290
291
292
	} else if ((!strcmp(argv[i], "-shell")) ||
	    (!strcmp(argv[i], "--shell"))) {
	    shell++;
            noout = 1;
293
294
295
296
297
298
299
300
	} else if ((!strcmp(argv[i], "-add")) ||
	    (!strcmp(argv[i], "--add"))) {
	    i += 3;
	    add++;
	} else if ((!strcmp(argv[i], "-del")) ||
	    (!strcmp(argv[i], "--del"))) {
	    i += 1;
	    del++;
301
302
303
304
305
306
307
308
	} else {
	    fprintf(stderr, "Unknown option %s\n", argv[i]);
	    usage(argv[0]);
	    return(1);
	}
    }

    for (i = 1; i < argc; i++) {
309
310
311
312
313
314
315
	if ((!strcmp(argv[i], "-add")) ||
	    (!strcmp(argv[i], "--add"))) {
	    i += 3;
	    continue;
	} else if ((!strcmp(argv[i], "-del")) ||
	    (!strcmp(argv[i], "--del"))) {
	    i += 1;
316
	    continue;
317
318
319
	} else if (argv[i][0] == '-')
	    continue;
	filename = argv[i];
320
	xmlLoadCatalog(argv[i]);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	break;
    }

    if ((add) || (del)) {
	int ret;

	for (i = 1; i < argc ; i++) {
	    if (!strcmp(argv[i], "-"))
		break;

	    if (argv[i][0] != '-')
		continue;
	    if ((!strcmp(argv[i], "-add")) ||
		(!strcmp(argv[i], "--add"))) {
		if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0))
		    ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL,
			                BAD_CAST argv[i + 2]);
		else
		    ret = xmlCatalogAdd(BAD_CAST argv[i + 1],
			                BAD_CAST argv[i + 2],
			                BAD_CAST argv[i + 3]);
		if (ret != 0)
		    printf("add command failed\n");
		i += 3;
	    } else if ((!strcmp(argv[i], "-del")) ||
		(!strcmp(argv[i], "--del"))) {
		ret = xmlCatalogRemove(BAD_CAST argv[i + 1]);
		i += 1;
	    }
	}
	
	if (noout) {
	    FILE *out;

	    out = fopen(filename, "w");
	    if (out == NULL) {
		fprintf(stderr, "could not open %s for saving\n", filename);
		noout = 0;
	    } else {
		xmlCatalogDump(out);
	    }
	}
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    }

    if (shell) {
	usershell();
    }
    if (!noout) {
	xmlCatalogDump(stdout);
    }

    /*
     * Cleanup and check for memory leaks
     */
    xmlCatalogCleanup();
    xmlCleanupParser();
    xmlMemoryDump();
    return(0);
}
#else
int main(int argc, char **argv) {
    fprintf(stderr, "libxml was not compiled with catalog support\n");
    return(1);
}
#endif