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