Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 15 | #ifdef HAVE_STDLIB_H |
| 16 | #include <stdlib.h> |
| 17 | #endif |
| 18 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 19 | #ifdef HAVE_LIBREADLINE |
| 20 | #include <readline/readline.h> |
| 21 | #ifdef HAVE_LIBHISTORY |
| 22 | #include <readline/history.h> |
| 23 | #endif |
| 24 | #endif |
| 25 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 26 | #include <libxml/xmlmemory.h> |
| 27 | #include <libxml/uri.h> |
| 28 | #include <libxml/catalog.h> |
| 29 | #include <libxml/parser.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 30 | #include <libxml/globals.h> |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 31 | |
| 32 | static int shell = 0; |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 33 | static int sgml = 0; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 34 | static int noout = 0; |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 35 | static int create = 0; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 36 | static int add = 0; |
| 37 | static int del = 0; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 38 | static int convert = 0; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 39 | static int verbose = 0; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 40 | static char *filename; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 43 | |
| 44 | #define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog" |
| 45 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 46 | /************************************************************************ |
| 47 | * * |
| 48 | * Shell Interface * |
| 49 | * * |
| 50 | ************************************************************************/ |
| 51 | /** |
| 52 | * xmlShellReadline: |
| 53 | * @prompt: the prompt value |
| 54 | * |
| 55 | * Read a string |
| 56 | * |
| 57 | * Returns a pointer to it or NULL on EOF the caller is expected to |
| 58 | * free the returned string. |
| 59 | */ |
| 60 | static char * |
| 61 | xmlShellReadline(const char *prompt) { |
| 62 | #ifdef HAVE_LIBREADLINE |
| 63 | char *line_read; |
| 64 | |
| 65 | /* Get a line from the user. */ |
| 66 | line_read = readline (prompt); |
| 67 | |
| 68 | /* If the line has any text in it, save it on the history. */ |
| 69 | if (line_read && *line_read) |
| 70 | add_history (line_read); |
| 71 | |
| 72 | return (line_read); |
| 73 | #else |
| 74 | char line_read[501]; |
Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 75 | char *ret; |
| 76 | int len; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 77 | |
| 78 | if (prompt != NULL) |
| 79 | fprintf(stdout, "%s", prompt); |
| 80 | if (!fgets(line_read, 500, stdin)) |
| 81 | return(NULL); |
| 82 | line_read[500] = 0; |
Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 83 | len = strlen(line_read); |
| 84 | ret = (char *) malloc(len + 1); |
| 85 | if (ret != NULL) { |
| 86 | memcpy (ret, line_read, len + 1); |
| 87 | } |
| 88 | return(ret); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 92 | static void usershell(void) { |
| 93 | char *cmdline = NULL, *cur; |
| 94 | int nbargs; |
| 95 | char command[100]; |
| 96 | char arg[400]; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 97 | char *argv[20]; |
| 98 | int i, ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 99 | xmlChar *ans; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 100 | |
| 101 | while (1) { |
| 102 | cmdline = xmlShellReadline("> "); |
| 103 | if (cmdline == NULL) |
| 104 | return; |
| 105 | |
| 106 | /* |
| 107 | * Parse the command itself |
| 108 | */ |
| 109 | cur = cmdline; |
| 110 | nbargs = 0; |
| 111 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 112 | i = 0; |
| 113 | while ((*cur != ' ') && (*cur != '\t') && |
| 114 | (*cur != '\n') && (*cur != '\r')) { |
| 115 | if (*cur == 0) |
| 116 | break; |
| 117 | command[i++] = *cur++; |
| 118 | } |
| 119 | command[i] = 0; |
| 120 | if (i == 0) continue; |
| 121 | nbargs++; |
| 122 | |
| 123 | /* |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 124 | * Parse the argument string |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 125 | */ |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 126 | memset(arg, 0, sizeof(arg)); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 127 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 128 | i = 0; |
| 129 | while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) { |
| 130 | if (*cur == 0) |
| 131 | break; |
| 132 | arg[i++] = *cur++; |
| 133 | } |
| 134 | arg[i] = 0; |
| 135 | if (i != 0) |
| 136 | nbargs++; |
| 137 | |
| 138 | /* |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 139 | * Parse the arguments |
| 140 | */ |
| 141 | i = 0; |
| 142 | nbargs = 0; |
| 143 | cur = arg; |
| 144 | memset(argv, 0, sizeof(argv)); |
| 145 | while (*cur != 0) { |
| 146 | while ((*cur == ' ') || (*cur == '\t')) cur++; |
| 147 | if (*cur == '\'') { |
| 148 | cur++; |
| 149 | argv[i] = cur; |
| 150 | while ((*cur != 0) && (*cur != '\'')) cur++; |
| 151 | if (*cur == '\'') { |
| 152 | *cur = 0; |
| 153 | nbargs++; |
| 154 | i++; |
| 155 | cur++; |
| 156 | } |
| 157 | } else if (*cur == '"') { |
| 158 | cur++; |
| 159 | argv[i] = cur; |
| 160 | while ((*cur != 0) && (*cur != '"')) cur++; |
| 161 | if (*cur == '"') { |
| 162 | *cur = 0; |
| 163 | nbargs++; |
| 164 | i++; |
| 165 | cur++; |
| 166 | } |
| 167 | } else { |
| 168 | argv[i] = cur; |
| 169 | while ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) |
| 170 | cur++; |
| 171 | *cur = 0; |
| 172 | nbargs++; |
| 173 | i++; |
| 174 | cur++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 179 | * start interpreting the command |
| 180 | */ |
| 181 | if (!strcmp(command, "exit")) |
| 182 | break; |
| 183 | if (!strcmp(command, "quit")) |
| 184 | break; |
| 185 | if (!strcmp(command, "bye")) |
| 186 | break; |
| 187 | if (!strcmp(command, "public")) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 188 | if (nbargs != 1) { |
| 189 | printf("public requires 1 arguments\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 190 | } else { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 191 | ans = xmlCatalogResolvePublic((const xmlChar *) argv[0]); |
| 192 | if (ans == NULL) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 193 | printf("No entry for PUBLIC %s\n", argv[0]); |
| 194 | } else { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 195 | printf("%s\n", ans); |
| 196 | xmlFree(ans); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 197 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 198 | } |
| 199 | } else if (!strcmp(command, "system")) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 200 | if (nbargs != 1) { |
| 201 | printf("system requires 1 arguments\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 202 | } else { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 203 | ans = xmlCatalogResolveSystem((const xmlChar *) argv[0]); |
| 204 | if (ans == NULL) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 205 | printf("No entry for SYSTEM %s\n", argv[0]); |
| 206 | } else { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 207 | printf("%s\n", ans); |
| 208 | xmlFree(ans); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } else if (!strcmp(command, "add")) { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 212 | if (sgml) { |
| 213 | if (nbargs != 1) { |
| 214 | printf("add requires 1 argument\n"); |
| 215 | } else { |
| 216 | ret = xmlCatalogAdd(BAD_CAST "sgmlcatalog", NULL, |
| 217 | BAD_CAST argv[0]); |
| 218 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 219 | } else { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 220 | if ((nbargs != 3) && (nbargs != 2)) { |
| 221 | printf("add requires 2 or 3 arguments\n"); |
| 222 | } else { |
| 223 | if (argv[2] == NULL) |
| 224 | ret = xmlCatalogAdd(BAD_CAST argv[0], NULL, |
| 225 | BAD_CAST argv[1]); |
| 226 | else |
| 227 | ret = xmlCatalogAdd(BAD_CAST argv[0], BAD_CAST argv[1], |
| 228 | BAD_CAST argv[2]); |
| 229 | if (ret != 0) |
| 230 | printf("add command failed\n"); |
| 231 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 232 | } |
| 233 | } else if (!strcmp(command, "del")) { |
| 234 | if (nbargs != 1) { |
| 235 | printf("del requires 1\n"); |
| 236 | } else { |
| 237 | ret = xmlCatalogRemove(BAD_CAST argv[0]); |
| 238 | if (ret <= 0) |
| 239 | printf("del command failed\n"); |
| 240 | |
| 241 | } |
| 242 | } else if (!strcmp(command, "resolve")) { |
| 243 | if (nbargs != 2) { |
| 244 | printf("resolve requires 2 arguments\n"); |
| 245 | } else { |
| 246 | ans = xmlCatalogResolve(BAD_CAST argv[0], |
| 247 | BAD_CAST argv[1]); |
| 248 | if (ans == NULL) { |
| 249 | printf("Resolver failed to find an answer\n"); |
| 250 | } else { |
| 251 | printf("%s\n", ans); |
| 252 | xmlFree(ans); |
| 253 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 254 | } |
| 255 | } else if (!strcmp(command, "dump")) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 256 | if (nbargs != 0) { |
| 257 | printf("dump has no arguments\n"); |
| 258 | } else { |
| 259 | xmlCatalogDump(stdout); |
| 260 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 261 | } else if (!strcmp(command, "debug")) { |
| 262 | if (nbargs != 0) { |
| 263 | printf("debug has no arguments\n"); |
| 264 | } else { |
| 265 | verbose++; |
| 266 | xmlCatalogSetDebug(verbose); |
| 267 | } |
| 268 | } else if (!strcmp(command, "quiet")) { |
| 269 | if (nbargs != 0) { |
| 270 | printf("quiet has no arguments\n"); |
| 271 | } else { |
| 272 | if (verbose > 0) |
| 273 | verbose--; |
| 274 | xmlCatalogSetDebug(verbose); |
| 275 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 276 | } else { |
| 277 | if (strcmp(command, "help")) { |
| 278 | printf("Unrecognized command %s\n", command); |
| 279 | } |
| 280 | printf("Commands available:\n"); |
| 281 | printf("\tpublic PublicID: make a PUBLIC identifier lookup\n"); |
| 282 | printf("\tsystem SystemID: make a SYSTEM identifier lookup\n"); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 283 | printf("\tresolve PublicID SystemID: do a full resolver lookup\n"); |
| 284 | printf("\tadd 'type' 'orig' 'replace' : add an entry\n"); |
| 285 | printf("\tdel 'values' : remove values\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 286 | printf("\tdump: print the current catalog state\n"); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 287 | printf("\tdebug: increase the verbosity level\n"); |
| 288 | printf("\tquiet: decrease the verbosity level\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 289 | printf("\texit: quit the shell\n"); |
| 290 | } |
| 291 | free(cmdline); /* not xmlFree here ! */ |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /************************************************************************ |
| 296 | * * |
| 297 | * Main * |
| 298 | * * |
| 299 | ************************************************************************/ |
| 300 | static void usage(const char *name) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 301 | printf("Usage : %s [options] catalogfile entities...\n", name); |
| 302 | printf("\tParse the catalog file and query it for the entities\n"); |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 303 | printf("\t--sgml : handle SGML Super catalogs for --add and --del\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 304 | printf("\t--shell : run a shell allowing interactive queries\n"); |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 305 | printf("\t--create : create a new catalog\n"); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 306 | printf("\t--add 'type' 'orig' 'replace' : add an entry\n"); |
| 307 | printf("\t--del 'values' : remove values\n"); |
| 308 | printf("\t--noout: avoid dumping the result on stdout\n"); |
| 309 | printf("\t used with add or del, it saves the catalog changes\n"); |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 310 | printf("\t and with --sgml it also updates the super catalog\n"); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 311 | printf("\t-v --verbose : provide debug informations\n"); |
| 312 | } |
| 313 | int main(int argc, char **argv) { |
| 314 | int i; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 315 | int ret; |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 316 | int exit_value = 0; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 317 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 318 | |
| 319 | if (argc <= 1) { |
| 320 | usage(argv[0]); |
| 321 | return(1); |
| 322 | } |
| 323 | |
| 324 | LIBXML_TEST_VERSION |
| 325 | for (i = 1; i < argc ; i++) { |
| 326 | if (!strcmp(argv[i], "-")) |
| 327 | break; |
| 328 | |
| 329 | if (argv[i][0] != '-') |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 330 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 331 | if ((!strcmp(argv[i], "-verbose")) || |
| 332 | (!strcmp(argv[i], "-v")) || |
| 333 | (!strcmp(argv[i], "--verbose"))) { |
| 334 | verbose++; |
| 335 | xmlCatalogSetDebug(verbose); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 336 | } else if ((!strcmp(argv[i], "-noout")) || |
| 337 | (!strcmp(argv[i], "--noout"))) { |
| 338 | noout = 1; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 339 | } else if ((!strcmp(argv[i], "-shell")) || |
| 340 | (!strcmp(argv[i], "--shell"))) { |
| 341 | shell++; |
| 342 | noout = 1; |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 343 | } else if ((!strcmp(argv[i], "-sgml")) || |
| 344 | (!strcmp(argv[i], "--sgml"))) { |
| 345 | sgml++; |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 346 | } else if ((!strcmp(argv[i], "-create")) || |
| 347 | (!strcmp(argv[i], "--create"))) { |
| 348 | create++; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 349 | } else if ((!strcmp(argv[i], "-convert")) || |
| 350 | (!strcmp(argv[i], "--convert"))) { |
| 351 | convert++; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 352 | } else if ((!strcmp(argv[i], "-add")) || |
| 353 | (!strcmp(argv[i], "--add"))) { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 354 | if (sgml) |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 355 | i += 2; |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 356 | else |
| 357 | i += 3; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 358 | add++; |
| 359 | } else if ((!strcmp(argv[i], "-del")) || |
| 360 | (!strcmp(argv[i], "--del"))) { |
| 361 | i += 1; |
| 362 | del++; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 363 | } else { |
| 364 | fprintf(stderr, "Unknown option %s\n", argv[i]); |
| 365 | usage(argv[0]); |
| 366 | return(1); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | for (i = 1; i < argc; i++) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 371 | if ((!strcmp(argv[i], "-add")) || |
| 372 | (!strcmp(argv[i], "--add"))) { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 373 | if (sgml) |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 374 | i += 2; |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 375 | else |
| 376 | i += 3; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 377 | continue; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 378 | } else if ((!strcmp(argv[i], "-del")) || |
| 379 | (!strcmp(argv[i], "--del"))) { |
| 380 | i += 1; |
| 381 | continue; |
| 382 | } else if (argv[i][0] == '-') |
| 383 | continue; |
| 384 | filename = argv[i]; |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 385 | if (!sgml) { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 386 | ret = xmlLoadCatalog(argv[i]); |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 387 | if ((ret < 0) && (create)) { |
| 388 | xmlCatalogAdd(BAD_CAST "catalog", BAD_CAST argv[i], NULL); |
| 389 | } |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 390 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 394 | if (convert) |
| 395 | ret = xmlCatalogConvert(); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 396 | |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 397 | if ((add) || (del)) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 398 | for (i = 1; i < argc ; i++) { |
| 399 | if (!strcmp(argv[i], "-")) |
| 400 | break; |
| 401 | |
| 402 | if (argv[i][0] != '-') |
| 403 | continue; |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 404 | if (strcmp(argv[i], "-add") && strcmp(argv[i], "--add") && |
| 405 | strcmp(argv[i], "-del") && strcmp(argv[i], "--del")) |
| 406 | continue; |
| 407 | |
| 408 | if (sgml) { |
| 409 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 410 | * Maintenance of SGML catalogs. |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 411 | */ |
| 412 | xmlCatalogPtr catal = NULL; |
| 413 | xmlCatalogPtr super = NULL; |
| 414 | |
| 415 | catal = xmlLoadSGMLSuperCatalog(argv[i + 1]); |
| 416 | |
| 417 | if ((!strcmp(argv[i], "-add")) || |
| 418 | (!strcmp(argv[i], "--add"))) { |
| 419 | if (catal == NULL) |
| 420 | catal = xmlNewCatalog(1); |
| 421 | super = xmlLoadSGMLSuperCatalog(XML_SGML_DEFAULT_CATALOG); |
| 422 | if (super == NULL) |
| 423 | super = xmlNewCatalog(1); |
| 424 | |
| 425 | xmlACatalogAdd(catal, BAD_CAST "CATALOG", |
| 426 | BAD_CAST argv[i + 2], NULL); |
| 427 | xmlACatalogAdd(super, BAD_CAST "CATALOG", |
| 428 | BAD_CAST argv[i + 1], NULL); |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 429 | } else { |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 430 | if (catal != NULL) |
| 431 | ret = xmlACatalogRemove(catal, BAD_CAST argv[i + 2]); |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 432 | else |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 433 | ret = -1; |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 434 | if (ret < 0) { |
| 435 | fprintf(stderr, "Failed to remove entry from %s\n", |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 436 | argv[i + 1]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 437 | exit_value = 1; |
| 438 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 439 | if ((noout) && (catal != NULL) && |
| 440 | (xmlCatalogIsEmpty(catal))) { |
| 441 | super = xmlLoadSGMLSuperCatalog( |
| 442 | XML_SGML_DEFAULT_CATALOG); |
| 443 | if (super != NULL) { |
| 444 | ret = xmlACatalogRemove(super, |
| 445 | BAD_CAST argv[i + 1]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 446 | if (ret < 0) { |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 447 | fprintf(stderr, |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 448 | "Failed to remove entry from %s\n", |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 449 | XML_SGML_DEFAULT_CATALOG); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 450 | exit_value = 1; |
| 451 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 454 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 455 | if (noout) { |
| 456 | FILE *out; |
| 457 | |
| 458 | if (xmlCatalogIsEmpty(catal)) { |
Daniel Veillard | 6eb1772 | 2001-11-04 22:19:27 +0000 | [diff] [blame] | 459 | remove(argv[i + 1]); |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 460 | } else { |
| 461 | out = fopen(argv[i + 1], "w"); |
| 462 | if (out == NULL) { |
| 463 | fprintf(stderr, "could not open %s for saving\n", |
| 464 | argv[i + 1]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 465 | exit_value = 2; |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 466 | noout = 0; |
| 467 | } else { |
| 468 | xmlACatalogDump(catal, out); |
| 469 | fclose(out); |
| 470 | } |
| 471 | } |
| 472 | if (super != NULL) { |
| 473 | if (xmlCatalogIsEmpty(super)) { |
Daniel Veillard | 6eb1772 | 2001-11-04 22:19:27 +0000 | [diff] [blame] | 474 | remove(XML_SGML_DEFAULT_CATALOG); |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 475 | } else { |
| 476 | out = fopen(XML_SGML_DEFAULT_CATALOG, "w"); |
| 477 | if (out == NULL) { |
| 478 | fprintf(stderr, |
| 479 | "could not open %s for saving\n", |
| 480 | XML_SGML_DEFAULT_CATALOG); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 481 | exit_value = 2; |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 482 | noout = 0; |
| 483 | } else { |
| 484 | |
| 485 | xmlACatalogDump(super, out); |
| 486 | fclose(out); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } else { |
| 491 | xmlACatalogDump(catal, stdout); |
| 492 | } |
| 493 | i += 2; |
| 494 | } else { |
| 495 | if ((!strcmp(argv[i], "-add")) || |
| 496 | (!strcmp(argv[i], "--add"))) { |
| 497 | if ((argv[i + 3] == NULL) || (argv[i + 3][0] == 0)) |
| 498 | ret = xmlCatalogAdd(BAD_CAST argv[i + 1], NULL, |
| 499 | BAD_CAST argv[i + 2]); |
| 500 | else |
| 501 | ret = xmlCatalogAdd(BAD_CAST argv[i + 1], |
| 502 | BAD_CAST argv[i + 2], |
| 503 | BAD_CAST argv[i + 3]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 504 | if (ret != 0) { |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 505 | printf("add command failed\n"); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 506 | exit_value = 3; |
| 507 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 508 | i += 3; |
| 509 | } else if ((!strcmp(argv[i], "-del")) || |
| 510 | (!strcmp(argv[i], "--del"))) { |
| 511 | ret = xmlCatalogRemove(BAD_CAST argv[i + 1]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 512 | if (ret < 0) { |
| 513 | fprintf(stderr, "Failed to remove entry %s\n", |
| 514 | argv[i + 1]); |
| 515 | exit_value = 1; |
| 516 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 517 | i += 1; |
| 518 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 522 | } else if (shell) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 523 | usershell(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 524 | } else { |
| 525 | for (i++; i < argc; i++) { |
| 526 | xmlURIPtr uri; |
| 527 | xmlChar *ans; |
| 528 | |
| 529 | uri = xmlParseURI(argv[i]); |
| 530 | if (uri == NULL) { |
| 531 | ans = xmlCatalogResolvePublic((const xmlChar *) argv[i]); |
| 532 | if (ans == NULL) { |
| 533 | printf("No entry for PUBLIC %s\n", argv[i]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 534 | exit_value = 4; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 535 | } else { |
| 536 | printf("%s\n", ans); |
| 537 | xmlFree(ans); |
| 538 | } |
| 539 | } else { |
| 540 | xmlFreeURI(uri); |
| 541 | ans = xmlCatalogResolveSystem((const xmlChar *) argv[i]); |
| 542 | if (ans == NULL) { |
| 543 | printf("No entry for SYSTEM %s\n", argv[i]); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 544 | exit_value = 4; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 545 | } else { |
| 546 | printf("%s\n", ans); |
| 547 | xmlFree(ans); |
| 548 | } |
| 549 | } |
| 550 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 551 | } |
Daniel Veillard | cd21dc7 | 2001-11-04 20:03:38 +0000 | [diff] [blame] | 552 | if ((!sgml) && ((add) || (del) || (create) || (convert))) { |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 553 | if (noout) { |
| 554 | FILE *out; |
| 555 | |
| 556 | out = fopen(filename, "w"); |
| 557 | if (out == NULL) { |
| 558 | fprintf(stderr, "could not open %s for saving\n", filename); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 559 | exit_value = 2; |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 560 | noout = 0; |
| 561 | } else { |
| 562 | xmlCatalogDump(out); |
| 563 | } |
| 564 | } else { |
| 565 | xmlCatalogDump(stdout); |
| 566 | } |
| 567 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * Cleanup and check for memory leaks |
| 571 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 572 | xmlCleanupParser(); |
| 573 | xmlMemoryDump(); |
Daniel Veillard | 0f5f162 | 2002-01-20 12:42:06 +0000 | [diff] [blame] | 574 | return(exit_value); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 575 | } |
| 576 | #else |
| 577 | int main(int argc, char **argv) { |
| 578 | fprintf(stderr, "libxml was not compiled with catalog support\n"); |
| 579 | return(1); |
| 580 | } |
| 581 | #endif |