| 1 |
/* $Id: xml_edit.c,v 1.45 2005/01/08 00:07:03 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 <config.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
#include <libxml/xmlmemory.h> |
| 36 |
#include <libxml/debugXML.h> |
| 37 |
#include <libxml/xmlsave.h> |
| 38 |
#include <libxml/HTMLtree.h> |
| 39 |
#include <libxml/xinclude.h> |
| 40 |
#include <libxml/xpath.h> |
| 41 |
#include <libxml/xpathInternals.h> |
| 42 |
#include <libxml/xpointer.h> |
| 43 |
#include <libxml/parserInternals.h> |
| 44 |
#include <libxml/uri.h> |
| 45 |
#include <libexslt/exslt.h> |
| 46 |
|
| 47 |
#include "xmlstar.h" |
| 48 |
|
| 49 |
/* |
| 50 |
TODO: |
| 51 |
1. Should this be allowed ? |
| 52 |
./xml ed -m /xml /xml/table/rec/object ../examples/xml/tab-obj.xml |
| 53 |
*/ |
| 54 |
|
| 55 |
typedef struct _edOptions { /* Global 'edit' options */ |
| 56 |
int noblanks; /* Remove insignificant spaces from XML tree */ |
| 57 |
int preserveFormat; /* Preserve original XML formatting */ |
| 58 |
int omit_decl; /* Omit XML declaration line <?xml version="1.0"?> */ |
| 59 |
int inplace; /* Edit file inplace (no output on stdout) */ |
| 60 |
int nonet; /* Disallow network access */ |
| 61 |
} edOptions; |
| 62 |
|
| 63 |
typedef edOptions *edOptionsPtr; |
| 64 |
|
| 65 |
typedef enum _XmlEdOp { |
| 66 |
XML_ED_DELETE, |
| 67 |
XML_ED_VAR, |
| 68 |
XML_ED_INSERT, |
| 69 |
XML_ED_APPEND, |
| 70 |
XML_ED_UPDATE, |
| 71 |
XML_ED_RENAME, |
| 72 |
XML_ED_MOVE, |
| 73 |
XML_ED_SUBNODE |
| 74 |
} XmlEdOp; |
| 75 |
|
| 76 |
/* TODO ??? */ |
| 77 |
typedef enum _XmlNodeType { |
| 78 |
XML_UNDEFINED, |
| 79 |
XML_ATTR, |
| 80 |
XML_ELEM, |
| 81 |
XML_TEXT, |
| 82 |
XML_COMT, |
| 83 |
XML_CDATA, |
| 84 |
XML_EXPR |
| 85 |
} XmlNodeType; |
| 86 |
|
| 87 |
typedef struct { |
| 88 |
char shortOpt; |
| 89 |
const char* longOpt; /* include "--" */ |
| 90 |
XmlNodeType type; |
| 91 |
} OptionSpec; |
| 92 |
|
| 93 |
static const OptionSpec |
| 94 |
OPT_VAL_OR_EXP[] = { |
| 95 |
{'x', "--expr", XML_EXPR}, |
| 96 |
{'v', "--value", XML_TEXT} |
| 97 |
}, |
| 98 |
OPT_JUST_VAL[] = { |
| 99 |
{'v', "--value", XML_TEXT} |
| 100 |
}, |
| 101 |
OPT_JUST_TYPE[] = { |
| 102 |
{'t', "--type"} |
| 103 |
}, |
| 104 |
OPT_NODE_TYPE[] = { |
| 105 |
{0, "elem", XML_ELEM}, |
| 106 |
{0, "attr", XML_ATTR}, |
| 107 |
{0, "text", XML_TEXT} |
| 108 |
}, |
| 109 |
OPT_JUST_NAME[] = { |
| 110 |
{'n', "--name"} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
typedef const char* XmlEdArg; |
| 115 |
|
| 116 |
typedef struct _XmlEdAction { |
| 117 |
XmlEdOp op; |
| 118 |
XmlEdArg arg1; |
| 119 |
XmlEdArg arg2; |
| 120 |
XmlEdArg arg3; |
| 121 |
XmlNodeType type; |
| 122 |
} XmlEdAction; |
| 123 |
|
| 124 |
/** |
| 125 |
* display short help message |
| 126 |
*/ |
| 127 |
static void |
| 128 |
edUsage(const char *argv0, exit_status status) |
| 129 |
{ |
| 130 |
extern void fprint_edit_usage(FILE* o, const char* argv0); |
| 131 |
extern const char more_info[]; |
| 132 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 133 |
fprint_edit_usage(o, argv0); |
| 134 |
fprintf(o, "%s", more_info); |
| 135 |
exit(status); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Initialize global command line options |
| 140 |
*/ |
| 141 |
static void |
| 142 |
edInitOptions(edOptionsPtr ops) |
| 143 |
{ |
| 144 |
ops->noblanks = 1; |
| 145 |
ops->omit_decl = 0; |
| 146 |
ops->preserveFormat = 0; |
| 147 |
ops->inplace = 0; |
| 148 |
ops->nonet = 1; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Parse global command line options |
| 153 |
*/ |
| 154 |
static int |
| 155 |
edParseOptions(edOptionsPtr ops, int argc, char **argv) |
| 156 |
{ |
| 157 |
int i; |
| 158 |
|
| 159 |
i = 2; |
| 160 |
while((i < argc) && (argv[i][0] == '-')) |
| 161 |
{ |
| 162 |
if (!strcmp(argv[i], "-S") || !strcmp(argv[i], "--ps")) |
| 163 |
{ |
| 164 |
ops->noblanks = 0; /* preserve spaces */ |
| 165 |
} |
| 166 |
else if (!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pf")) |
| 167 |
{ |
| 168 |
ops->preserveFormat = 1; /* preserve format */ |
| 169 |
} |
| 170 |
else if (!strcmp(argv[i], "-O") || !strcmp(argv[i], "--omit-decl")) |
| 171 |
{ |
| 172 |
ops->omit_decl = 1; |
| 173 |
} |
| 174 |
else if (!strcmp(argv[i], "-L") || !strcmp(argv[i], "--inplace")) |
| 175 |
{ |
| 176 |
ops->inplace = 1; |
| 177 |
} |
| 178 |
else if (!strcmp(argv[i], "--net")) |
| 179 |
{ |
| 180 |
ops->nonet = 0; |
| 181 |
} |
| 182 |
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h") || |
| 183 |
!strcmp(argv[i], "-?") || !strcmp(argv[i], "-Z")) |
| 184 |
{ |
| 185 |
edUsage(argv[0], EXIT_SUCCESS); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
break; |
| 190 |
} |
| 191 |
i++; |
| 192 |
} |
| 193 |
|
| 194 |
return i; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* register the namespace from @ns_arr to @ctxt |
| 199 |
*/ |
| 200 |
static void |
| 201 |
nsarr_xpath_register(xmlXPathContextPtr ctxt) |
| 202 |
{ |
| 203 |
int ns; |
| 204 |
for (ns = 0; ns_arr[ns]; ns += 2) { |
| 205 |
xmlXPathRegisterNs(ctxt, ns_arr[ns], ns_arr[ns+1]); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* register top-level namespace definitions from @doc to @ctxt |
| 211 |
*/ |
| 212 |
static void |
| 213 |
extract_ns_defs(xmlDocPtr doc, xmlXPathContextPtr ctxt) |
| 214 |
{ |
| 215 |
xmlNsPtr nsDef; |
| 216 |
xmlNodePtr root = xmlDocGetRootElement(doc); |
| 217 |
if (!root) return; |
| 218 |
|
| 219 |
for (nsDef = root->nsDef; nsDef; nsDef = nsDef->next) { |
| 220 |
if (nsDef->prefix != NULL) { /* can only register ns with prefix */ |
| 221 |
xmlXPathRegisterNs(ctxt, nsDef->prefix, nsDef->href); |
| 222 |
} else { |
| 223 |
default_ns = nsDef->href; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (default_ns) { |
| 228 |
xmlXPathRegisterNs(ctxt, BAD_CAST "_", default_ns); |
| 229 |
xmlXPathRegisterNs(ctxt, BAD_CAST "DEFAULT", default_ns); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
update_string(xmlDocPtr doc, xmlNodePtr dest, const xmlChar* newstr) |
| 235 |
{ |
| 236 |
/* TODO: do we need xmlEncodeEntitiesReentrant() too/instead? */ |
| 237 |
xmlChar* string = xmlEncodeSpecialChars(doc, newstr); |
| 238 |
xmlNodeSetContent(dest, string); |
| 239 |
xmlFree(string); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* 'update' operation |
| 244 |
*/ |
| 245 |
static void |
| 246 |
edUpdate(xmlDocPtr doc, xmlNodeSetPtr nodes, const char *val, |
| 247 |
XmlNodeType type, xmlXPathContextPtr ctxt) |
| 248 |
{ |
| 249 |
int i; |
| 250 |
xmlXPathCompExprPtr xpath = NULL; |
| 251 |
|
| 252 |
if (type == XML_EXPR) { |
| 253 |
xpath = xmlXPathCompile((const xmlChar*) val); |
| 254 |
if (!xpath) return; |
| 255 |
} |
| 256 |
|
| 257 |
for (i = 0; i < nodes->nodeNr; i++) |
| 258 |
{ |
| 259 |
/* update node */ |
| 260 |
if (type == XML_EXPR) { |
| 261 |
xmlXPathObjectPtr res; |
| 262 |
|
| 263 |
ctxt->node = nodes->nodeTab[i]; |
| 264 |
res = xmlXPathCompiledEval(xpath, ctxt); |
| 265 |
if (res->type == XPATH_NODESET || res->type == XPATH_XSLT_TREE) { |
| 266 |
int j; |
| 267 |
xmlNodePtr oldChild; |
| 268 |
xmlNodeSetPtr oldChildren = xmlXPathNodeSetCreate(NULL); |
| 269 |
/* NOTE: newChildren can be NULL for empty result set */ |
| 270 |
xmlNodeSetPtr newChildren = res->nodesetval; |
| 271 |
|
| 272 |
/* NOTE: nodes can be both oldChildren and newChildren */ |
| 273 |
|
| 274 |
/* unlink the old children */ |
| 275 |
for (oldChild = nodes->nodeTab[i]->children; oldChild; oldChild = oldChild->next) { |
| 276 |
xmlUnlinkNode(oldChild); |
| 277 |
/* we can't free it now because an oldChild can also be |
| 278 |
newChild! just put it in the list */ |
| 279 |
xmlXPathNodeSetAdd(oldChildren, oldChild); |
| 280 |
} |
| 281 |
|
| 282 |
/* add the new children */ |
| 283 |
for (j = 0; newChildren && j < newChildren->nodeNr; j++) { |
| 284 |
xmlNodePtr node = newChildren->nodeTab[j]; |
| 285 |
xmlAddChild(nodes->nodeTab[i], |
| 286 |
/* if node is linked to this doc we need to copy */ |
| 287 |
(node->doc == doc)? xmlDocCopyNode(node, doc, 1) : node); |
| 288 |
newChildren->nodeTab[j] = NULL; |
| 289 |
} |
| 290 |
newChildren->nodeNr = 0; |
| 291 |
|
| 292 |
/* NOTE: if any oldChildren were newChildren, they've been |
| 293 |
copied so we can free them all now */ |
| 294 |
for (j = 0; j < oldChildren->nodeNr; j++) { |
| 295 |
xmlFreeNode(oldChildren->nodeTab[j]); |
| 296 |
oldChildren->nodeTab[j] = NULL; |
| 297 |
} |
| 298 |
oldChildren->nodeNr = 0; |
| 299 |
xmlXPathFreeNodeSet(oldChildren); |
| 300 |
} else { |
| 301 |
res = xmlXPathConvertString(res); |
| 302 |
update_string(doc, nodes->nodeTab[i], res->stringval); |
| 303 |
} |
| 304 |
xmlXPathFreeObject(res); |
| 305 |
} else { |
| 306 |
update_string(doc, nodes->nodeTab[i], (const xmlChar*) val); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
xmlXPathFreeCompExpr(xpath); |
| 311 |
} |
| 312 |
|
| 313 |
/* holds the node that was last inserted */ |
| 314 |
static xmlNodeSetPtr previous_insertion; |
| 315 |
|
| 316 |
/** |
| 317 |
* We must not keep free'd nodes in @previous_insertion. |
| 318 |
* This is a callback from xmlFreeNode() |
| 319 |
*/ |
| 320 |
static void |
| 321 |
removeNodeFromPrev(xmlNodePtr node) |
| 322 |
{ |
| 323 |
xmlXPathNodeSetDel(previous_insertion, node); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* 'insert' operation |
| 328 |
*/ |
| 329 |
static void |
| 330 |
edInsert(xmlDocPtr doc, xmlNodeSetPtr nodes, const char *val, const char *name, |
| 331 |
XmlNodeType type, int mode) |
| 332 |
{ |
| 333 |
int i; |
| 334 |
|
| 335 |
xmlXPathEmptyNodeSet(previous_insertion); |
| 336 |
|
| 337 |
for (i = 0; i < nodes->nodeNr; i++) |
| 338 |
{ |
| 339 |
xmlNodePtr node; |
| 340 |
|
| 341 |
if (nodes->nodeTab[i] == (void*) doc && mode != 0) { |
| 342 |
fprintf(stderr, "%s", "The document node cannot have siblings.\n"); |
| 343 |
exit(EXIT_INTERNAL_ERROR); |
| 344 |
} |
| 345 |
|
| 346 |
/* update node */ |
| 347 |
if (type == XML_ATTR) |
| 348 |
{ |
| 349 |
node = (xmlNodePtr) xmlNewProp(nodes->nodeTab[i], BAD_CAST name, BAD_CAST val); |
| 350 |
} |
| 351 |
else if (type == XML_ELEM) |
| 352 |
{ |
| 353 |
node = xmlNewDocNode(doc, NULL /* TODO: NS */, BAD_CAST name, BAD_CAST val); |
| 354 |
if (mode > 0) |
| 355 |
xmlAddNextSibling(nodes->nodeTab[i], node); |
| 356 |
else if (mode < 0) |
| 357 |
xmlAddPrevSibling(nodes->nodeTab[i], node); |
| 358 |
else |
| 359 |
xmlAddChild(nodes->nodeTab[i], node); |
| 360 |
} |
| 361 |
else if (type == XML_TEXT) |
| 362 |
{ |
| 363 |
node = xmlNewDocText(doc, BAD_CAST val); |
| 364 |
if (mode > 0) |
| 365 |
xmlAddNextSibling(nodes->nodeTab[i], node); |
| 366 |
else if (mode < 0) |
| 367 |
xmlAddPrevSibling(nodes->nodeTab[i], node); |
| 368 |
else |
| 369 |
xmlAddChild(nodes->nodeTab[i], node); |
| 370 |
} |
| 371 |
xmlXPathNodeSetAdd(previous_insertion, node); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* 'rename' operation |
| 377 |
*/ |
| 378 |
static void |
| 379 |
edRename(xmlDocPtr doc, xmlNodeSetPtr nodes, const char *val, XmlNodeType type) |
| 380 |
{ |
| 381 |
int i; |
| 382 |
for (i = 0; i < nodes->nodeNr; i++) |
| 383 |
{ |
| 384 |
if (nodes->nodeTab[i] == (void*) doc) { |
| 385 |
fprintf(stderr, "%s", "The document node cannot be renamed.\n"); |
| 386 |
exit(EXIT_INTERNAL_ERROR); |
| 387 |
} |
| 388 |
xmlNodeSetName(nodes->nodeTab[i], BAD_CAST val); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* 'delete' operation |
| 394 |
*/ |
| 395 |
static void |
| 396 |
edDelete(xmlDocPtr doc, xmlNodeSetPtr nodes) |
| 397 |
{ |
| 398 |
int i; |
| 399 |
for (i = nodes->nodeNr - 1; i >= 0; i--) |
| 400 |
{ |
| 401 |
if (nodes->nodeTab[i] == (void*) doc) { |
| 402 |
fprintf(stderr, "%s", "The document node cannot be deleted.\n"); |
| 403 |
exit(EXIT_INTERNAL_ERROR); |
| 404 |
} |
| 405 |
|
| 406 |
if (nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 407 |
fprintf(stderr, "%s", "FIXME: can't delete namespace nodes\n"); |
| 408 |
exit(EXIT_INTERNAL_ERROR); |
| 409 |
} |
| 410 |
/* delete node */ |
| 411 |
xmlUnlinkNode(nodes->nodeTab[i]); |
| 412 |
|
| 413 |
/* Free node and children */ |
| 414 |
xmlFreeNode(nodes->nodeTab[i]); |
| 415 |
nodes->nodeTab[i] = NULL; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* 'move' operation |
| 421 |
*/ |
| 422 |
static void |
| 423 |
edMove(xmlDocPtr doc, xmlNodeSetPtr nodes, xmlNodePtr to) |
| 424 |
{ |
| 425 |
int i; |
| 426 |
for (i = 0; i < nodes->nodeNr; i++) |
| 427 |
{ |
| 428 |
if (nodes->nodeTab[i] == (void*) doc) { |
| 429 |
fprintf(stderr, "%s", "The document node cannot be moved.\n"); |
| 430 |
exit(EXIT_INTERNAL_ERROR); |
| 431 |
} |
| 432 |
|
| 433 |
if (nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 434 |
fprintf(stderr, "%s", "FIXME: can't move namespace nodes\n"); |
| 435 |
exit(EXIT_INTERNAL_ERROR); |
| 436 |
} |
| 437 |
/* move node */ |
| 438 |
xmlUnlinkNode(nodes->nodeTab[i]); |
| 439 |
xmlAddChild(to, nodes->nodeTab[i]); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Loop through array of operations and perform them |
| 445 |
*/ |
| 446 |
static void |
| 447 |
edProcess(xmlDocPtr doc, const XmlEdAction* ops, int ops_count) |
| 448 |
{ |
| 449 |
int k; |
| 450 |
xmlXPathContextPtr ctxt = xmlXPathNewContext(doc); |
| 451 |
/* NOTE: later registrations override earlier ones */ |
| 452 |
registerXstarNs(ctxt); |
| 453 |
|
| 454 |
/* variables */ |
| 455 |
previous_insertion = xmlXPathNodeSetCreate(NULL); |
| 456 |
registerXstarVariable(ctxt, "prev", |
| 457 |
xmlXPathWrapNodeSet(previous_insertion)); |
| 458 |
xmlDeregisterNodeDefault(&removeNodeFromPrev); |
| 459 |
|
| 460 |
#if HAVE_EXSLT_XPATH_REGISTER |
| 461 |
/* register extension functions */ |
| 462 |
exsltDateXpathCtxtRegister(ctxt, BAD_CAST "date"); |
| 463 |
exsltMathXpathCtxtRegister(ctxt, BAD_CAST "math"); |
| 464 |
exsltSetsXpathCtxtRegister(ctxt, BAD_CAST "set"); |
| 465 |
exsltStrXpathCtxtRegister(ctxt, BAD_CAST "str"); |
| 466 |
#endif |
| 467 |
/* namespaces from doc */ |
| 468 |
if (globalOptions.doc_namespace) |
| 469 |
extract_ns_defs(doc, ctxt); |
| 470 |
/* namespaces from command line */ |
| 471 |
nsarr_xpath_register(ctxt); |
| 472 |
|
| 473 |
for (k = 0; k < ops_count; k++) |
| 474 |
{ |
| 475 |
xmlXPathObjectPtr res; |
| 476 |
xmlNodeSetPtr nodes; |
| 477 |
|
| 478 |
/* NOTE: to make relative paths match as if from "/", set context to |
| 479 |
document; setting to root would match as if from "/node()/" */ |
| 480 |
ctxt->node = (xmlNodePtr) doc; |
| 481 |
|
| 482 |
if (ops[k].op == XML_ED_VAR) { |
| 483 |
res = xmlXPathEvalExpression(BAD_CAST ops[k].arg2, ctxt); |
| 484 |
xmlXPathRegisterVariable(ctxt, BAD_CAST ops[k].arg1, res); |
| 485 |
continue; |
| 486 |
} |
| 487 |
|
| 488 |
res = xmlXPathEvalExpression(BAD_CAST ops[k].arg1, ctxt); |
| 489 |
if (!res || res->type != XPATH_NODESET || !res->nodesetval) continue; |
| 490 |
nodes = res->nodesetval; |
| 491 |
|
| 492 |
switch (ops[k].op) |
| 493 |
{ |
| 494 |
case XML_ED_DELETE: |
| 495 |
edDelete(doc, nodes); |
| 496 |
break; |
| 497 |
case XML_ED_MOVE: { |
| 498 |
xmlXPathObjectPtr res_to; |
| 499 |
ctxt->node = (xmlNodePtr) doc; |
| 500 |
res_to = xmlXPathEvalExpression(BAD_CAST ops[k].arg2, ctxt); |
| 501 |
if (!res_to |
| 502 |
|| res_to->type != XPATH_NODESET |
| 503 |
|| res_to->nodesetval->nodeNr != 1) { |
| 504 |
fprintf(stderr, "%s", "move destination is not a single node\n"); |
| 505 |
continue; |
| 506 |
} |
| 507 |
edMove(doc, nodes, res_to->nodesetval->nodeTab[0]); |
| 508 |
xmlXPathFreeObject(res_to); |
| 509 |
break; |
| 510 |
} |
| 511 |
case XML_ED_UPDATE: |
| 512 |
edUpdate(doc, nodes, ops[k].arg2, ops[k].type, ctxt); |
| 513 |
break; |
| 514 |
case XML_ED_RENAME: |
| 515 |
edRename(doc, nodes, ops[k].arg2, ops[k].type); |
| 516 |
break; |
| 517 |
case XML_ED_INSERT: |
| 518 |
edInsert(doc, nodes, ops[k].arg2, ops[k].arg3, ops[k].type, -1); |
| 519 |
break; |
| 520 |
case XML_ED_APPEND: |
| 521 |
edInsert(doc, nodes, ops[k].arg2, ops[k].arg3, ops[k].type, 1); |
| 522 |
break; |
| 523 |
case XML_ED_SUBNODE: |
| 524 |
edInsert(doc, nodes, ops[k].arg2, ops[k].arg3, ops[k].type, 0); |
| 525 |
break; |
| 526 |
default: |
| 527 |
break; |
| 528 |
} |
| 529 |
xmlXPathFreeObject(res); |
| 530 |
} |
| 531 |
/* NOTE: free()ing ctxt also free()s previous_insertion */ |
| 532 |
previous_insertion = NULL; |
| 533 |
xmlDeregisterNodeDefault(NULL); |
| 534 |
|
| 535 |
xmlXPathFreeContext(ctxt); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Output document |
| 540 |
*/ |
| 541 |
static void |
| 542 |
edOutput(const char* filename, const XmlEdAction* ops, int ops_count, |
| 543 |
const edOptions* g_ops) |
| 544 |
{ |
| 545 |
xmlDocPtr doc; |
| 546 |
int save_options = |
| 547 |
#if LIBXML_VERSION >= 20708 |
| 548 |
(g_ops->noblanks? 0 : XML_SAVE_WSNONSIG) | |
| 549 |
#endif |
| 550 |
(g_ops->preserveFormat? 0 : XML_SAVE_FORMAT) | |
| 551 |
(g_ops->omit_decl? XML_SAVE_NO_DECL : 0); |
| 552 |
int read_options = |
| 553 |
(g_ops->nonet? XML_PARSE_NONET : 0); |
| 554 |
xmlSaveCtxtPtr save; |
| 555 |
|
| 556 |
doc = xmlReadFile(filename, NULL, read_options); |
| 557 |
if (!doc) |
| 558 |
{ |
| 559 |
cleanupNSArr(ns_arr); |
| 560 |
xmlCleanupParser(); |
| 561 |
xmlCleanupGlobals(); |
| 562 |
exit(EXIT_BAD_FILE); |
| 563 |
} |
| 564 |
|
| 565 |
edProcess(doc, ops, ops_count); |
| 566 |
|
| 567 |
/* avoid getting ASCII CRs in UTF-16/UCS-(2,4) text */ |
| 568 |
if ((xmlStrcasestr(doc->encoding, BAD_CAST "UTF") == 0 |
| 569 |
&& xmlStrcasestr(doc->encoding, BAD_CAST "16") == 0) |
| 570 |
|| |
| 571 |
(xmlStrcasestr(doc->encoding, BAD_CAST "UCS") == 0 |
| 572 |
&& (xmlStrcasestr(doc->encoding, BAD_CAST "2") == 0 |
| 573 |
|| |
| 574 |
xmlStrcasestr(doc->encoding, BAD_CAST "4") == 0))) |
| 575 |
{ |
| 576 |
set_stdout_binary(); |
| 577 |
} |
| 578 |
|
| 579 |
save = xmlSaveToFilename(g_ops->inplace? filename : "-", NULL, save_options); |
| 580 |
xmlSaveDoc(save, doc); |
| 581 |
xmlSaveClose(save); |
| 582 |
xmlFreeDoc(doc); |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* get next command line arg, or print error exit and exit if there isn't one |
| 587 |
* @returns pointer to the arg |
| 588 |
* @argi is incremented |
| 589 |
*/ |
| 590 |
static const char* |
| 591 |
nextArg(char *const*const argv, int *argi) |
| 592 |
{ |
| 593 |
const char *arg = argv[*argi]; |
| 594 |
if (arg == NULL) |
| 595 |
{ |
| 596 |
edUsage(argv[0], EXIT_BAD_ARGS); |
| 597 |
} |
| 598 |
*argi += 1; |
| 599 |
return arg; |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* like nextArg(), but additionally look for next arg in @choices |
| 604 |
*/ |
| 605 |
static XmlNodeType |
| 606 |
parseNextArg(char *const*const argv, int *argi, |
| 607 |
const OptionSpec choices[], int choices_count) |
| 608 |
{ |
| 609 |
const char* arg = nextArg(argv, argi); |
| 610 |
int i; |
| 611 |
for (i = 0; i < choices_count; i++) { |
| 612 |
if ((arg[0] == '-' && arg[1] == choices[i].shortOpt) || |
| 613 |
(strcmp(arg, choices[i].longOpt) == 0)) |
| 614 |
return choices[i].type; |
| 615 |
} |
| 616 |
edUsage(argv[0], EXIT_BAD_ARGS); |
| 617 |
return 0; /* never reach here */ |
| 618 |
} |
| 619 |
#define parseNextArg(argv, argi, choices) \ |
| 620 |
parseNextArg(argv, argi, choices, COUNT_OF(choices)) |
| 621 |
|
| 622 |
|
| 623 |
/** --insert, --append, and --subnode all take the same arguments */ |
| 624 |
static void |
| 625 |
parseInsertionArgs(XmlEdOp op_type, XmlEdAction* op, |
| 626 |
char *const*const argv, int *argi) |
| 627 |
{ |
| 628 |
XmlEdArg arg; |
| 629 |
op->op = op_type; |
| 630 |
op->arg1 = nextArg(argv, argi); |
| 631 |
parseNextArg(argv, argi, OPT_JUST_TYPE); |
| 632 |
op->type = parseNextArg(argv, argi, OPT_NODE_TYPE); |
| 633 |
parseNextArg(argv, argi, OPT_JUST_NAME); |
| 634 |
op->arg3 = nextArg(argv, argi); |
| 635 |
/* test if value is given */ |
| 636 |
op->arg2 = 0; |
| 637 |
arg = argv[*argi]; |
| 638 |
if (!arg) return; |
| 639 |
if (!strcmp(arg, "-v") || !strcmp(arg, "--value")) { |
| 640 |
parseNextArg(argv, argi, OPT_JUST_VAL); |
| 641 |
op->arg2 = nextArg(argv, argi); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* This is the main function for 'edit' option |
| 647 |
*/ |
| 648 |
int |
| 649 |
edMain(int argc, char **argv) |
| 650 |
{ |
| 651 |
int i, ops_count, max_ops_count = 8, n, start = 0; |
| 652 |
XmlEdAction* ops = xmlMalloc(sizeof(XmlEdAction) * max_ops_count); |
| 653 |
static edOptions g_ops; |
| 654 |
int nCount = 0; |
| 655 |
|
| 656 |
if (argc < 3) edUsage(argv[0], EXIT_BAD_ARGS); |
| 657 |
|
| 658 |
edInitOptions(&g_ops); |
| 659 |
start = edParseOptions(&g_ops, argc, argv); |
| 660 |
|
| 661 |
parseNSArr(ns_arr, &nCount, argc-start, argv+start); |
| 662 |
|
| 663 |
/* |
| 664 |
* Parse command line and fill array of operations |
| 665 |
*/ |
| 666 |
ops_count = 0; |
| 667 |
i = start + nCount; |
| 668 |
|
| 669 |
while (i < argc) |
| 670 |
{ |
| 671 |
const char *arg = nextArg(argv, &i); |
| 672 |
if (arg[0] == '-') |
| 673 |
{ |
| 674 |
if (ops_count >= max_ops_count) |
| 675 |
{ |
| 676 |
max_ops_count *= 2; |
| 677 |
ops = xmlRealloc(ops, sizeof(XmlEdAction) * max_ops_count); |
| 678 |
} |
| 679 |
ops[ops_count].type = XML_UNDEFINED; |
| 680 |
|
| 681 |
if (!strcmp(arg, "-d") || !strcmp(arg, "--delete")) |
| 682 |
{ |
| 683 |
ops[ops_count].op = XML_ED_DELETE; |
| 684 |
ops[ops_count].arg1 = nextArg(argv, &i); |
| 685 |
ops[ops_count].arg2 = 0; |
| 686 |
} |
| 687 |
else if (!strcmp(arg, "--var")) |
| 688 |
{ |
| 689 |
ops[ops_count].op = XML_ED_VAR; |
| 690 |
ops[ops_count].arg1 = nextArg(argv, &i); |
| 691 |
ops[ops_count].arg2 = nextArg(argv, &i); |
| 692 |
} |
| 693 |
else if (!strcmp(arg, "-m") || !strcmp(arg, "--move")) |
| 694 |
{ |
| 695 |
ops[ops_count].op = XML_ED_MOVE; |
| 696 |
ops[ops_count].arg1 = nextArg(argv, &i); |
| 697 |
ops[ops_count].arg2 = nextArg(argv, &i); |
| 698 |
} |
| 699 |
else if (!strcmp(arg, "-u") || !strcmp(arg, "--update")) |
| 700 |
{ |
| 701 |
ops[ops_count].op = XML_ED_UPDATE; |
| 702 |
ops[ops_count].arg1 = nextArg(argv, &i); |
| 703 |
ops[ops_count].type = parseNextArg(argv, &i, OPT_VAL_OR_EXP); |
| 704 |
ops[ops_count].arg2 = nextArg(argv, &i); |
| 705 |
} |
| 706 |
else if (!strcmp(arg, "-r") || !strcmp(arg, "--rename")) |
| 707 |
{ |
| 708 |
ops[ops_count].op = XML_ED_RENAME; |
| 709 |
ops[ops_count].arg1 = nextArg(argv, &i); |
| 710 |
ops[ops_count].type = parseNextArg(argv, &i, OPT_JUST_VAL); |
| 711 |
ops[ops_count].arg2 = nextArg(argv, &i); |
| 712 |
} |
| 713 |
else if (!strcmp(arg, "-i") || !strcmp(arg, "--insert")) |
| 714 |
{ |
| 715 |
parseInsertionArgs(XML_ED_INSERT, &ops[ops_count], argv, &i); |
| 716 |
} |
| 717 |
else if (!strcmp(arg, "-a") || !strcmp(arg, "--append")) |
| 718 |
{ |
| 719 |
parseInsertionArgs(XML_ED_APPEND, &ops[ops_count], argv, &i); |
| 720 |
} |
| 721 |
else if (!strcmp(arg, "-s") || !strcmp(arg, "--subnode")) |
| 722 |
{ |
| 723 |
parseInsertionArgs(XML_ED_SUBNODE, &ops[ops_count], argv, &i); |
| 724 |
} |
| 725 |
else |
| 726 |
{ |
| 727 |
fprintf(stderr, "Warning: unrecognized option '%s'\n", arg); |
| 728 |
} |
| 729 |
ops_count++; |
| 730 |
} |
| 731 |
else |
| 732 |
{ |
| 733 |
i--; /* it was a filename, we didn't use it */ |
| 734 |
break; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
xmlKeepBlanksDefault(0); |
| 739 |
|
| 740 |
if ((!g_ops.noblanks) || g_ops.preserveFormat) xmlKeepBlanksDefault(1); |
| 741 |
|
| 742 |
if (i >= argc) |
| 743 |
{ |
| 744 |
edOutput("-", ops, ops_count, &g_ops); |
| 745 |
} |
| 746 |
|
| 747 |
for (n=i; n<argc; n++) |
| 748 |
{ |
| 749 |
edOutput(argv[n], ops, ops_count, &g_ops); |
| 750 |
} |
| 751 |
|
| 752 |
xmlFree(ops); |
| 753 |
cleanupNSArr(ns_arr); |
| 754 |
xmlCleanupParser(); |
| 755 |
xmlCleanupGlobals(); |
| 756 |
return 0; |
| 757 |
} |