| 1 |
/* $Id: xml_trans.c,v 1.38 2005/01/07 02:40:59 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002-2004 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 <config.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
#include "xmlstar.h" |
| 36 |
#include "trans.h" |
| 37 |
|
| 38 |
/* |
| 39 |
* TODO: |
| 40 |
* 1. proper command line arguments handling |
| 41 |
* 2. review and clean up all code (free memory) |
| 42 |
* 3. check embedded stylesheet support |
| 43 |
* 4. exit values on errors |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
* Display usage syntax |
| 48 |
*/ |
| 49 |
void |
| 50 |
trUsage(const char *argv0, exit_status status) |
| 51 |
{ |
| 52 |
extern void fprint_trans_usage(FILE* o, const char* argv0); |
| 53 |
extern const char more_info[]; |
| 54 |
extern const char libxslt_more_info[]; |
| 55 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 56 |
fprint_trans_usage(o, argv0); |
| 57 |
fprintf(o, "%s", more_info); |
| 58 |
fprintf(o, "%s", libxslt_more_info); |
| 59 |
exit(status); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Parse global command line options |
| 64 |
*/ |
| 65 |
int |
| 66 |
trParseOptions(xsltOptionsPtr ops, int argc, char **argv) |
| 67 |
{ |
| 68 |
int i; |
| 69 |
|
| 70 |
if (argc <= 2) return argc; |
| 71 |
for (i=2; i<argc; i++) |
| 72 |
{ |
| 73 |
if (argv[i][0] == '-') |
| 74 |
{ |
| 75 |
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) |
| 76 |
{ |
| 77 |
trUsage(argv[0], EXIT_SUCCESS); |
| 78 |
} |
| 79 |
else if (!strcmp(argv[i], "--show-ext")) |
| 80 |
{ |
| 81 |
ops->show_extensions = 1; |
| 82 |
} |
| 83 |
else if (!strcmp(argv[i], "--val")) |
| 84 |
{ |
| 85 |
ops->noval = 0; |
| 86 |
} |
| 87 |
else if (!strcmp(argv[i], "--net")) |
| 88 |
{ |
| 89 |
ops->nonet = 0; |
| 90 |
} |
| 91 |
else if (!strcmp(argv[i], "-E") || !strcmp(argv[i], "--embed")) |
| 92 |
{ |
| 93 |
ops->embed = 1; |
| 94 |
} |
| 95 |
else if (!strcmp(argv[i], "--omit-decl")) |
| 96 |
{ |
| 97 |
ops->omit_decl = 1; |
| 98 |
} |
| 99 |
else if (!strcmp(argv[i], "--maxdepth")) |
| 100 |
{ |
| 101 |
int value; |
| 102 |
i++; |
| 103 |
if (i >= argc) trUsage(argv[0], EXIT_BAD_ARGS); |
| 104 |
if (sscanf(argv[i], "%d", &value) == 1) |
| 105 |
if (value > 0) xsltMaxDepth = value; |
| 106 |
} |
| 107 |
#ifdef LIBXML_XINCLUDE_ENABLED |
| 108 |
else if (!strcmp(argv[i], "--xinclude")) |
| 109 |
{ |
| 110 |
ops->xinclude = 1; |
| 111 |
} |
| 112 |
#endif |
| 113 |
#ifdef LIBXML_HTML_ENABLED |
| 114 |
else if (!strcmp(argv[i], "--html")) |
| 115 |
{ |
| 116 |
ops->html = 1; |
| 117 |
} |
| 118 |
#endif |
| 119 |
} |
| 120 |
else |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return i; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Cleanup memory |
| 129 |
*/ |
| 130 |
void |
| 131 |
trCleanup() |
| 132 |
{ |
| 133 |
xsltCleanupGlobals(); |
| 134 |
xmlCleanupParser(); |
| 135 |
#if 0 |
| 136 |
xmlMemoryDump(); |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Parse command line for XSLT parameters |
| 142 |
*/ |
| 143 |
int |
| 144 |
trParseParams(const char** params, int* plen, |
| 145 |
int count, char **argv) |
| 146 |
{ |
| 147 |
int i; |
| 148 |
*plen = 0; |
| 149 |
params[0] = 0; |
| 150 |
|
| 151 |
for (i=0; i<count; i++) |
| 152 |
{ |
| 153 |
if (argv[i][0] == '-') |
| 154 |
{ |
| 155 |
if (!strcmp(argv[i], "-p")) |
| 156 |
{ |
| 157 |
int j; |
| 158 |
xmlChar *name, *value; |
| 159 |
|
| 160 |
i++; |
| 161 |
if (i >= count) trUsage(argv[0], EXIT_BAD_ARGS); |
| 162 |
|
| 163 |
for(j=0; argv[i][j] && (argv[i][j] != '='); j++); |
| 164 |
if (argv[i][j] != '=') trUsage(argv[0], EXIT_BAD_ARGS); |
| 165 |
|
| 166 |
name = xmlStrndup((const xmlChar *) argv[i], j); |
| 167 |
value = xmlStrdup((const xmlChar *) argv[i]+j+1); |
| 168 |
|
| 169 |
if (*plen >= MAX_PARAMETERS) |
| 170 |
{ |
| 171 |
fprintf(stderr, "too many params increase MAX_PARAMETERS\n"); |
| 172 |
exit(EXIT_INTERNAL_ERROR); |
| 173 |
} |
| 174 |
|
| 175 |
params[*plen] = (char *)name; |
| 176 |
(*plen)++; |
| 177 |
params[*plen] = (char *)value; |
| 178 |
(*plen)++; |
| 179 |
params[*plen] = 0; |
| 180 |
} |
| 181 |
else if (!strcmp(argv[i], "-s")) |
| 182 |
{ |
| 183 |
int j; |
| 184 |
const xmlChar *string; |
| 185 |
xmlChar *name, *value; |
| 186 |
|
| 187 |
i++; |
| 188 |
if (i >= count) trUsage(argv[0], EXIT_BAD_ARGS); |
| 189 |
|
| 190 |
for(j=0; argv[i][j] && (argv[i][j] != '='); j++); |
| 191 |
if (argv[i][j] != '=') trUsage(argv[0], EXIT_BAD_ARGS); |
| 192 |
|
| 193 |
name = xmlStrndup((const xmlChar *)argv[i], j); |
| 194 |
string = (const xmlChar *)(argv[i]+j+1); |
| 195 |
|
| 196 |
if (xmlStrchr(string, '"')) |
| 197 |
{ |
| 198 |
if (xmlStrchr(string, '\'')) |
| 199 |
{ |
| 200 |
fprintf(stderr, |
| 201 |
"string parameter contains both quote and double-quotes\n"); |
| 202 |
exit(EXIT_INTERNAL_ERROR); |
| 203 |
} |
| 204 |
value = xmlStrdup((const xmlChar *)"'"); |
| 205 |
value = xmlStrcat(value, string); |
| 206 |
value = xmlStrcat(value, (const xmlChar *)"'"); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
value = xmlStrdup((const xmlChar *)"\""); |
| 211 |
value = xmlStrcat(value, string); |
| 212 |
value = xmlStrcat(value, (const xmlChar *)"\""); |
| 213 |
} |
| 214 |
|
| 215 |
if (*plen >= MAX_PARAMETERS) |
| 216 |
{ |
| 217 |
fprintf(stderr, "too many params increase MAX_PARAMETERS\n"); |
| 218 |
exit(EXIT_INTERNAL_ERROR); |
| 219 |
} |
| 220 |
|
| 221 |
params[*plen] = (char *)name; |
| 222 |
(*plen)++; |
| 223 |
params[*plen] = (char *)value; |
| 224 |
(*plen)++; |
| 225 |
params[*plen] = 0; |
| 226 |
} |
| 227 |
} |
| 228 |
else |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
return i; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Cleanup memory allocated by XSLT parameters |
| 237 |
*/ |
| 238 |
void |
| 239 |
trCleanupParams(const char **xsltParams) |
| 240 |
{ |
| 241 |
const char **p = xsltParams; |
| 242 |
|
| 243 |
while (*p) |
| 244 |
{ |
| 245 |
xmlFree((char *)*p); |
| 246 |
p++; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* This is the main function for 'tr' option |
| 252 |
*/ |
| 253 |
int |
| 254 |
trMain(int argc, char **argv) |
| 255 |
{ |
| 256 |
static xsltOptions ops; |
| 257 |
static const char *xsltParams[2 * MAX_PARAMETERS + 1]; |
| 258 |
|
| 259 |
int errorno = 0; |
| 260 |
int start, xslt_ind; |
| 261 |
int pCount; |
| 262 |
|
| 263 |
if (argc <= 2) trUsage(argv[0], EXIT_BAD_ARGS); |
| 264 |
|
| 265 |
xsltInitOptions(&ops); |
| 266 |
start = trParseOptions(&ops, argc, argv); |
| 267 |
xslt_ind = start; |
| 268 |
xsltInitLibXml(&ops); |
| 269 |
|
| 270 |
/* set parameters */ |
| 271 |
start += trParseParams(xsltParams, &pCount, argc-start-1, argv+start+1); |
| 272 |
|
| 273 |
/* run transformation */ |
| 274 |
errorno = xsltRun(&ops, argv[xslt_ind], xsltParams, |
| 275 |
argc-start-1, argv+start+1); |
| 276 |
|
| 277 |
/* free resources */ |
| 278 |
trCleanupParams(xsltParams); |
| 279 |
trCleanup(); |
| 280 |
|
| 281 |
return errorno; |
| 282 |
} |