Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * testURI.c : a small tester program for XML input. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 9 | #include "libxml.h" |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 10 | |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdarg.h> |
| 14 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 15 | #include <libxml/xmlmemory.h> |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 16 | #include <libxml/uri.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 17 | #include <libxml/globals.h> |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 18 | |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 19 | static const char *base = NULL; |
| 20 | static int escape = 0; |
Daniel Veillard | 1bc8d85 | 2007-10-16 12:18:18 +0000 | [diff] [blame] | 21 | static int debug = 0; |
Elliott Hughes | 7fbecab | 2019-01-10 16:42:03 -0800 | [diff] [blame] | 22 | static int relative = 0; |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 23 | |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 24 | static void handleURI(const char *str) { |
| 25 | int ret; |
| 26 | xmlURIPtr uri; |
| 27 | xmlChar *res = NULL, *parsed = NULL; |
| 28 | |
| 29 | uri = xmlCreateURI(); |
| 30 | |
| 31 | if (base == NULL) { |
| 32 | ret = xmlParseURIReference(uri, str); |
| 33 | if (ret != 0) |
| 34 | printf("%s : error %d\n", str, ret); |
| 35 | else { |
Daniel Veillard | 1bc8d85 | 2007-10-16 12:18:18 +0000 | [diff] [blame] | 36 | if (debug) { |
| 37 | if (uri->scheme) printf("scheme: %s\n", uri->scheme); |
| 38 | if (uri->opaque) printf("opaque: %s\n", uri->opaque); |
| 39 | if (uri->authority) printf("authority: %s\n", uri->authority); |
| 40 | if (uri->server) printf("server: %s\n", uri->server); |
| 41 | if (uri->user) printf("user: %s\n", uri->user); |
| 42 | if (uri->port != 0) printf("port: %d\n", uri->port); |
| 43 | if (uri->path) printf("path: %s\n", uri->path); |
| 44 | if (uri->query) printf("query: %s\n", uri->query); |
| 45 | if (uri->fragment) printf("fragment: %s\n", uri->fragment); |
| 46 | if (uri->query_raw) printf("query_raw: %s\n", uri->query_raw); |
| 47 | if (uri->cleanup != 0) printf("cleanup\n"); |
| 48 | } |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 49 | xmlNormalizeURIPath(uri->path); |
| 50 | if (escape != 0) { |
| 51 | parsed = xmlSaveUri(uri); |
| 52 | res = xmlURIEscape(parsed); |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 53 | printf("%s\n", (char *) res); |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 54 | |
| 55 | } else { |
| 56 | xmlPrintURI(stdout, uri); |
| 57 | printf("\n"); |
| 58 | } |
| 59 | } |
| 60 | } else { |
Elliott Hughes | 7fbecab | 2019-01-10 16:42:03 -0800 | [diff] [blame] | 61 | if (relative) { |
| 62 | res = xmlBuildRelativeURI((xmlChar *)str, (xmlChar *) base); |
| 63 | } else { |
| 64 | res = xmlBuildURI((xmlChar *)str, (xmlChar *) base); |
| 65 | } |
| 66 | |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 67 | if (res != NULL) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 68 | printf("%s\n", (char *) res); |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 69 | } |
| 70 | else |
| 71 | printf("::ERROR::\n"); |
| 72 | } |
| 73 | if (res != NULL) |
| 74 | xmlFree(res); |
| 75 | if (parsed != NULL) |
| 76 | xmlFree(parsed); |
| 77 | xmlFreeURI(uri); |
| 78 | } |
| 79 | |
| 80 | int main(int argc, char **argv) { |
| 81 | int i, arg = 1; |
| 82 | |
| 83 | if ((argc > arg) && (argv[arg] != NULL) && |
Elliott Hughes | 7fbecab | 2019-01-10 16:42:03 -0800 | [diff] [blame] | 84 | (!strcmp(argv[arg], "--relative"))) { |
| 85 | arg++; |
| 86 | relative++; |
| 87 | } |
| 88 | if ((argc > arg) && (argv[arg] != NULL) && |
Daniel Veillard | b6e7fdb | 2001-02-02 17:07:32 +0000 | [diff] [blame] | 89 | ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base")))) { |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 90 | arg++; |
| 91 | base = argv[arg]; |
| 92 | if (base != NULL) |
| 93 | arg++; |
| 94 | } |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 95 | if ((argc > arg) && (argv[arg] != NULL) && |
| 96 | ((!strcmp(argv[arg], "-escape")) || (!strcmp(argv[arg], "--escape")))) { |
| 97 | arg++; |
| 98 | escape++; |
| 99 | } |
Daniel Veillard | 1bc8d85 | 2007-10-16 12:18:18 +0000 | [diff] [blame] | 100 | if ((argc > arg) && (argv[arg] != NULL) && |
| 101 | ((!strcmp(argv[arg], "-debug")) || (!strcmp(argv[arg], "--debug")))) { |
| 102 | arg++; |
| 103 | debug++; |
| 104 | } |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 105 | if (argv[arg] == NULL) { |
| 106 | char str[1024]; |
| 107 | |
| 108 | while (1) { |
| 109 | /* |
| 110 | * read one line in string buffer. |
| 111 | */ |
| 112 | if (fgets (&str[0], sizeof (str) - 1, stdin) == NULL) |
| 113 | break; |
| 114 | |
| 115 | /* |
| 116 | * remove the ending spaces |
| 117 | */ |
| 118 | i = strlen(str); |
| 119 | while ((i > 0) && |
| 120 | ((str[i - 1] == '\n') || (str[i - 1] == '\r') || |
| 121 | (str[i - 1] == ' ') || (str[i - 1] == '\t'))) { |
| 122 | i--; |
| 123 | str[i] = 0; |
| 124 | } |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 125 | handleURI(str); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 126 | } |
| 127 | } else { |
| 128 | while (argv[arg] != NULL) { |
Daniel Veillard | 220346d | 2001-12-07 11:33:54 +0000 | [diff] [blame] | 129 | handleURI(argv[arg]); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 130 | arg++; |
| 131 | } |
| 132 | } |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 133 | xmlMemoryDump(); |
Daniel Veillard | b6e7fdb | 2001-02-02 17:07:32 +0000 | [diff] [blame] | 134 | return(0); |
Daniel Veillard | e77a918 | 2000-04-05 19:12:29 +0000 | [diff] [blame] | 135 | } |