Debian Codegraph @

src/xml_edit.c

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