| 1 |
/* $Id: xml_depyx.c,v 1.8 2005/03/12 03:24:23 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002 Mikhail Grushinskiy. All Rights Reserved. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in |
| 17 |
all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
THE SOFTWARE. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <string.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
|
| 33 |
#include <libxml/xmlmemory.h> |
| 34 |
|
| 35 |
#include "xmlstar.h" |
| 36 |
#include "escape.h" |
| 37 |
|
| 38 |
#define INSZ 4*1024 |
| 39 |
|
| 40 |
static void |
| 41 |
depyxUsage(int argc, char **argv, exit_status status) |
| 42 |
{ |
| 43 |
extern void fprint_depyx_usage(FILE* o, const char* argv0); |
| 44 |
extern const char more_info[]; |
| 45 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 46 |
fprint_depyx_usage(o, argv[0]); |
| 47 |
fprintf(o, "%s", more_info); |
| 48 |
exit(status); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Decode PYX string |
| 53 |
* |
| 54 |
*/ |
| 55 |
void |
| 56 |
pyxDecode(char *str, xml_C14NNormalizationMode mode) |
| 57 |
{ |
| 58 |
while (*str) |
| 59 |
{ |
| 60 |
if ((*str == '\\') && (*(str+1) == 'n')) |
| 61 |
{ |
| 62 |
printf("\n"); |
| 63 |
str++; |
| 64 |
} |
| 65 |
else if ((*str == '\\') && (*(str+1) == 't')) |
| 66 |
{ |
| 67 |
printf("\t"); |
| 68 |
str++; |
| 69 |
} |
| 70 |
else if ((*str == '\\') && (*(str+1) == '\\')) |
| 71 |
{ |
| 72 |
printf("\\"); |
| 73 |
str++; |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
if ((*str == '<') && ((mode == XML_C14N_NORMALIZE_ATTR) || |
| 78 |
(mode == XML_C14N_NORMALIZE_TEXT))) { |
| 79 |
printf("<"); |
| 80 |
} |
| 81 |
else if ((*str == '>') && (mode == XML_C14N_NORMALIZE_TEXT)) { |
| 82 |
printf(">"); |
| 83 |
} |
| 84 |
else if ((*str == '&') && ((mode == XML_C14N_NORMALIZE_ATTR) || |
| 85 |
(mode == XML_C14N_NORMALIZE_TEXT))) { |
| 86 |
printf("&"); |
| 87 |
} |
| 88 |
else if ((*str == '"') && (mode == XML_C14N_NORMALIZE_ATTR)) { |
| 89 |
printf("""); |
| 90 |
} |
| 91 |
else { |
| 92 |
printf("%c", *str); |
| 93 |
} |
| 94 |
} |
| 95 |
str++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Decode PYX file |
| 101 |
* |
| 102 |
*/ |
| 103 |
int |
| 104 |
pyxDePyx(char *file) |
| 105 |
{ |
| 106 |
static char line[INSZ]; |
| 107 |
FILE *in = stdin; |
| 108 |
|
| 109 |
if (strcmp(file, "-")) |
| 110 |
{ |
| 111 |
in = fopen(file, "r"); |
| 112 |
if (in == NULL) |
| 113 |
{ |
| 114 |
fprintf(stderr, "error: could not open: %s\n", file); |
| 115 |
exit(EXIT_BAD_FILE); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
while (!feof(in)) |
| 120 |
{ |
| 121 |
if (fgets(line, INSZ - 1, in)) |
| 122 |
{ |
| 123 |
if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; |
| 124 |
|
| 125 |
while (line[0] == '(') |
| 126 |
{ |
| 127 |
printf("<%s", line+1); |
| 128 |
if (!feof(in)) |
| 129 |
{ |
| 130 |
if (fgets(line, INSZ - 1, in)) |
| 131 |
{ |
| 132 |
if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; |
| 133 |
|
| 134 |
while(line[0] == 'A') /* attribute */ |
| 135 |
{ |
| 136 |
char *value; |
| 137 |
|
| 138 |
printf(" "); |
| 139 |
value = line+1; |
| 140 |
while(*value && (*value != ' ')) |
| 141 |
{ |
| 142 |
printf("%c", *value); |
| 143 |
value++; |
| 144 |
} |
| 145 |
if (*value == ' ') |
| 146 |
{ |
| 147 |
value++; |
| 148 |
printf("=\""); |
| 149 |
pyxDecode(value, XML_C14N_NORMALIZE_ATTR); /* attribute value */ |
| 150 |
printf("\""); |
| 151 |
} |
| 152 |
if (!feof(in)) |
| 153 |
{ |
| 154 |
if (fgets(line, INSZ - 1, in)) |
| 155 |
{ |
| 156 |
if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
printf(">"); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (line[0] == '-') |
| 166 |
{ |
| 167 |
/* text */ |
| 168 |
pyxDecode(line+1, XML_C14N_NORMALIZE_TEXT); |
| 169 |
} |
| 170 |
else if (line[0] == '?') |
| 171 |
{ |
| 172 |
/* processing instruction */ |
| 173 |
printf("<?"); |
| 174 |
pyxDecode(line+1, XML_C14N_NORMALIZE_TEXT); |
| 175 |
printf("?>"); |
| 176 |
printf("\n"); /* is this correct? */ |
| 177 |
} |
| 178 |
else if (line[0] == 'D') |
| 179 |
{ |
| 180 |
/* processing instruction */ |
| 181 |
printf("<!DOCTYPE"); |
| 182 |
pyxDecode(line+1, XML_C14N_NORMALIZE_TEXT); |
| 183 |
printf(">"); |
| 184 |
printf("\n"); /* is this correct? */ |
| 185 |
} |
| 186 |
else if (line[0] == 'C') |
| 187 |
{ |
| 188 |
/* comment */ |
| 189 |
printf("<!--"); |
| 190 |
pyxDecode(line+1, XML_C14N_NORMALIZE_TEXT); |
| 191 |
printf("-->"); |
| 192 |
printf("\n"); /* is this correct? */ |
| 193 |
} |
| 194 |
else if (line[0] == '[') |
| 195 |
{ |
| 196 |
/* CDATA */ |
| 197 |
printf("<![CDATA["); |
| 198 |
pyxDecode(line+1, XML_C14N_NORMALIZE_NOTHING); |
| 199 |
printf("]]>"); |
| 200 |
printf("\n"); /* is this correct? */ |
| 201 |
} |
| 202 |
else if (line[0] == ')') |
| 203 |
{ |
| 204 |
printf("</%s>", line+1); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return EXIT_SUCCESS; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Main function for 'de-PYX' |
| 214 |
* |
| 215 |
*/ |
| 216 |
int |
| 217 |
depyxMain(int argc, char **argv) |
| 218 |
{ |
| 219 |
int ret = EXIT_SUCCESS; |
| 220 |
|
| 221 |
if ((argc >= 3) && (!strcmp(argv[2], "-h") || !strcmp(argv[2], "--help"))) |
| 222 |
{ |
| 223 |
depyxUsage(argc, argv, EXIT_SUCCESS); |
| 224 |
} |
| 225 |
else if (argc == 3) |
| 226 |
{ |
| 227 |
ret = pyxDePyx(argv[2]); |
| 228 |
} |
| 229 |
else if (argc == 2) |
| 230 |
{ |
| 231 |
ret = pyxDePyx("-"); |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
depyxUsage(argc, argv, EXIT_BAD_ARGS); |
| 236 |
} |
| 237 |
|
| 238 |
printf("\n"); |
| 239 |
|
| 240 |
return ret; |
| 241 |
} |
| 242 |
|