summaryrefslogtreecommitdiffstats
path: root/gxpd.c
blob: d6a2601a4f30ed7cf47616c2c43e9fee97e0534f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * gxpd.c  -- Simple XPath Delete Util
 *	
 * Copyright © 2006-2008  Red Hat, Inc. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions of the
 * GNU General Public License v.2.  This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
 * including the implied warranties of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
 * trademarks that are incorporated in the source code or documentation are not
 * subject to the GNU General Public License and may only be used or replicated
 * with the express permission of Red Hat, Inc.
 *
 * Red Hat Author(s): Nathan Straz <nstraz@redhat.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <ctype.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xinclude.h>

#include "gxp-int.h"

static void usage(const char *pname);
static int gxpd(char *fn, const char *xpath_query, const char *prefix);

int 
main(int argc, char **argv) 
{
	char *xpath_query = NULL;
	char *prefix = NULL;
	int c;

	while ((c = getopt(argc, argv, "+p:h")) != -1) {
		switch (c) {
			case 'p':
				prefix = strdup(optarg);
				break;
			case 'h':
			case '?':
				usage(argv[0]);
				exit(2);
				break;
		}
	}
	xpath_query = argv[optind];
	if (!xpath_query) {
		fprintf(stderr, "Missing XPath Query\n");
		usage(argv[0]);
		exit(2);
	}

	optind++;
	if (!argv[optind]) {
		/* No files given, filter stdin to stdout */
		gxpd("-", xpath_query, prefix);
	} else {
		/* Rest of the args are the files to change */
#if 0
		if (((argc - optind) > 1) 
		    && (!gxpp_opts.omitfileprefix)) gxpp_opts.addfileprefix = 1;
#endif
		for (c = optind; c < argc; c++) {
			/* Make sure we can write to the file */
			gxpd(argv[c], xpath_query, prefix);
		}
	}
	return 0;
}
	
static int
gxpd(char *fn, const char *xpath_query, const char *prefix)
{
	xmlDocPtr doc;
	xmlXPathContextPtr ctx;
	xmlXPathObjectPtr obj;
	int i;

	if (!(doc = xmlParseFile(fn))) {
		fprintf(stderr, "Failed to parse XML file\n");
		exit(1);
	}
	/* xmlXIncludeProcess(doc); */
	ctx = generate_context(doc, prefix);
	obj = xmlXPathEvalExpression((xmlChar *)xpath_query, ctx);
	if (!obj) {
		fprintf(stderr, "XPath expression '%s' did not match in '%s'\n",
				xpath_query, fn);
		exit(1);
	}
	/* return early if there were no nodes to remove */
	if (obj->type != XPATH_NODESET) return 1;
       	if (obj->nodesetval == NULL) return 1;

	for (i = 0; i < obj->nodesetval->nodeNr; i++) {
		xmlUnlinkNode(obj->nodesetval->nodeTab[i]);
		xmlFreeNode(obj->nodesetval->nodeTab[i]);
	}
	printf("%d nodes removed from %s\n", i, fn);

	xmlXPathFreeObject(obj);
	xmlSaveFile(fn, doc);
	
	xmlXPathFreeContext(ctx);
	xmlFreeDoc(doc);

	return 0;
}


static void 
usage(const char *pname)
{
	fprintf(stderr,"%s [OPTION]... XPATH [FILE]...\n", pname);
	fprintf(stderr,"Options:\n"
	               "-p prefix   Set prefix for default namespace\n");
	return;
}