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