blob: 1a65450b50f4dd4db552e638fcb5443fc6212624 [file] [log] [blame]
Daniel Veillard344cee72001-08-20 00:08:40 +00001/*
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
Bjorn Reese45029602001-08-21 09:23:53 +000015#ifdef HAVE_LIBREADLINE
16#include <readline/readline.h>
17#ifdef HAVE_LIBHISTORY
18#include <readline/history.h>
19#endif
20#endif
21
Daniel Veillard344cee72001-08-20 00:08:40 +000022#include <libxml/xmlmemory.h>
23#include <libxml/uri.h>
24#include <libxml/catalog.h>
25#include <libxml/parser.h>
26
27static int shell = 0;
28static int noout = 0;
29static int verbose = 0;
30
31#ifdef LIBXML_CATALOG_ENABLED
32/************************************************************************
33 * *
34 * Shell Interface *
35 * *
36 ************************************************************************/
37/**
38 * xmlShellReadline:
39 * @prompt: the prompt value
40 *
41 * Read a string
42 *
43 * Returns a pointer to it or NULL on EOF the caller is expected to
44 * free the returned string.
45 */
46static char *
47xmlShellReadline(const char *prompt) {
48#ifdef HAVE_LIBREADLINE
49 char *line_read;
50
51 /* Get a line from the user. */
52 line_read = readline (prompt);
53
54 /* If the line has any text in it, save it on the history. */
55 if (line_read && *line_read)
56 add_history (line_read);
57
58 return (line_read);
59#else
60 char line_read[501];
61
62 if (prompt != NULL)
63 fprintf(stdout, "%s", prompt);
64 if (!fgets(line_read, 500, stdin))
65 return(NULL);
66 line_read[500] = 0;
67 return(strdup(line_read));
68#endif
69}
70
71
72static void usershell(void) {
73 char *cmdline = NULL, *cur;
74 int nbargs;
75 char command[100];
76 char arg[400];
77 int i;
78 const xmlChar *answer;
79
80 while (1) {
81 cmdline = xmlShellReadline("> ");
82 if (cmdline == NULL)
83 return;
84
85 /*
86 * Parse the command itself
87 */
88 cur = cmdline;
89 nbargs = 0;
90 while ((*cur == ' ') || (*cur == '\t')) cur++;
91 i = 0;
92 while ((*cur != ' ') && (*cur != '\t') &&
93 (*cur != '\n') && (*cur != '\r')) {
94 if (*cur == 0)
95 break;
96 command[i++] = *cur++;
97 }
98 command[i] = 0;
99 if (i == 0) continue;
100 nbargs++;
101
102 /*
103 * Parse the argument
104 */
105 while ((*cur == ' ') || (*cur == '\t')) cur++;
106 i = 0;
107 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
108 if (*cur == 0)
109 break;
110 arg[i++] = *cur++;
111 }
112 arg[i] = 0;
113 if (i != 0)
114 nbargs++;
115
116 /*
117 * start interpreting the command
118 */
119 if (!strcmp(command, "exit"))
120 break;
121 if (!strcmp(command, "quit"))
122 break;
123 if (!strcmp(command, "bye"))
124 break;
125 if (!strcmp(command, "public")) {
126 answer = xmlCatalogGetPublic((const xmlChar *) arg);
127 if (answer == NULL) {
128 printf("No entry for PUBLIC %s\n", arg);
129 } else {
130 printf("%s\n", answer);
131 }
132 } else if (!strcmp(command, "system")) {
133 answer = xmlCatalogGetSystem((const xmlChar *) arg);
134 if (answer == NULL) {
135 printf("No entry for SYSTEM %s\n", arg);
136 } else {
137 printf("%s\n", answer);
138 }
139 } else if (!strcmp(command, "dump")) {
140 xmlCatalogDump(stdout);
141 } else {
142 if (strcmp(command, "help")) {
143 printf("Unrecognized command %s\n", command);
144 }
145 printf("Commands available:\n");
146 printf("\tpublic PublicID: make a PUBLIC identifier lookup\n");
147 printf("\tsystem SystemID: make a SYSTEM identifier lookup\n");
148 printf("\tdump: print the current catalog state\n");
149 printf("\texit: quit the shell\n");
150 }
151 free(cmdline); /* not xmlFree here ! */
152 }
153}
154
155/************************************************************************
156 * *
157 * Main *
158 * *
159 ************************************************************************/
160static void usage(const char *name) {
161 printf("Usage : %s [options] catalogfile ...\n", name);
162 printf("\tParse the catalog file(s) and output the result of the parsing\n");
163 printf("\t--shell : run a shell allowing interactive queries\n");
164 printf("\t-v --verbose : provide debug informations\n");
165}
166int main(int argc, char **argv) {
167 int i;
168
169 if (argc <= 1) {
170 usage(argv[0]);
171 return(1);
172 }
173
174 LIBXML_TEST_VERSION
175 for (i = 1; i < argc ; i++) {
176 if (!strcmp(argv[i], "-"))
177 break;
178
179 if (argv[i][0] != '-')
180 continue;
181 if ((!strcmp(argv[i], "-verbose")) ||
182 (!strcmp(argv[i], "-v")) ||
183 (!strcmp(argv[i], "--verbose"))) {
184 verbose++;
185 xmlCatalogSetDebug(verbose);
186 } else if ((!strcmp(argv[i], "-shell")) ||
187 (!strcmp(argv[i], "--shell"))) {
188 shell++;
189 noout = 1;
190 } else {
191 fprintf(stderr, "Unknown option %s\n", argv[i]);
192 usage(argv[0]);
193 return(1);
194 }
195 }
196
197 for (i = 1; i < argc; i++) {
198 if (argv[i][0] == '-')
199 continue;
200 xmlLoadCatalog(argv[i]);
201 }
202
203 if (shell) {
204 usershell();
205 }
206 if (!noout) {
207 xmlCatalogDump(stdout);
208 }
209
210 /*
211 * Cleanup and check for memory leaks
212 */
213 xmlCatalogCleanup();
214 xmlCleanupParser();
215 xmlMemoryDump();
216 return(0);
217}
218#else
219int main(int argc, char **argv) {
220 fprintf(stderr, "libxml was not compiled with catalog support\n");
221 return(1);
222}
223#endif