8 # define strncasecmp strnicmp
15 static const char *_static_root = "/";
16 static const char *_static_element = "*";
17 static unsigned int _fcount = 0;
18 static char **_filenames = 0;
19 static char *_element = 0;
20 static char *_value = 0;
21 static char *_root = 0;
22 static char *_print = 0;
23 static char *_attribute = 0;
24 static int print_filenames = 0;
26 static void free_and_exit(int i);
28 #define SHOW_NOVAL(opt) \
30 printf("option '%s' requires a value\n\n", (opt)); \
37 printf("usage: xmlgrep [options] [file ...]\n\n");
39 printf("\t-h\t\tshow this help message\n");
40 printf("\t-a <string>\tprint this attribute as the output\n");
41 printf("\t-e <id>\t\tshow sections that contain this element\n");
42 printf("\t-p <id>\t\tprint this element as the output\n");
43 printf("\t-r <path>\tspecify the XML search root\n");
44 printf("\t-v <string>\tfilter sections that contain this vale\n\n");
45 printf(" To print the contents of the 'type' element of the XML section ");
46 printf("that begins\n at '/printer/output' use the following command:\n\n");
47 printf("\txmlgrep -r /printer/output -p type sample.xml\n\n");
48 printf(" To filter 'output' elements under '/printer' that have attribute");
49 printf(" 'n' set to '1'\n use the following command:\n\n");
50 printf("\txmlgrep -r /printer -p output -a n -v 1 sample.xml\n\n");
51 printf(" To filter out sections that contain the 'driver' element with ");
52 printf("'generic' as\n it's value use the following command:");
53 printf("\n\n\txmlgrep -r /printer/output -e driver -v generic sample.xml");
59 free_and_exit(int ret)
61 if (_root && _root != _static_root) free(_root);
62 if (_element && _element != _static_element) free(_element);
63 if (_value) free(_value);
64 if (_print) free(_print);
65 if (_attribute) free(_attribute);
69 for (i=0; i < _fcount; i++)
73 if (print_filenames) printf("%s\n", _filenames[i]);
84 parse_option(char **args, int n, int max)
87 unsigned int alen = 0;
91 if (strncmp(opt, "--", 2) == 0)
94 if ((arg = strchr(opt, '=')) != NULL)
102 if (arg && arg[0] == '-')
108 if (strncmp(opt, "-help", olen) == 0)
112 else if (strncmp(opt, "-root", olen) == 0)
114 if (arg == 0) SHOW_NOVAL(opt);
115 alen = strlen(arg)+1;
116 _root = malloc(alen);
117 memcpy(_root, arg, alen);
120 else if (strncmp(opt, "-element", olen) == 0)
122 if (arg == 0) SHOW_NOVAL(opt);
123 alen = strlen(arg)+1;
124 _element = malloc(alen);
125 memcpy(_element, arg, alen);
128 else if (strncmp(opt, "-value", olen) == 0)
130 if (arg == 0) SHOW_NOVAL(opt);
131 alen = strlen(arg)+1;
132 _value = malloc(alen);
133 memcpy(_value, arg, alen);
136 else if (strncmp(opt, "-print", olen) == 0)
138 if (arg == 0) SHOW_NOVAL(opt);
139 alen = strlen(arg)+1;
140 _print = malloc(alen);
141 memcpy(_print, arg, alen);
144 else if (strncmp(opt, "-attribute", olen) == 0)
146 if (arg == 0) SHOW_NOVAL(opt);
147 alen = strlen(arg)+1;
148 _attribute = malloc(alen);
149 memcpy(_attribute, arg, alen);
152 else if (strncmp(opt, "-list-filenames", olen) == 0)
153 { /* undocumented test argument */
157 else if (opt[0] == '-')
159 printf("Unknown option %s\n", opt);
167 _filenames = (char **)malloc(sizeof(char*));
171 char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*));
174 printf("Out of memory.\n\n");
180 alen = strlen(opt)+1;
181 _filenames[pos] = malloc(alen);
182 memcpy(_filenames[pos], opt, alen);
188 void walk_the_tree(size_t num, void *xid, char *tree)
190 unsigned int i, no_elements;
192 if (!tree) /* last node from the tree */
194 void *xmid = xmlMarkId(xid);
197 no_elements = xmlNodeGetNum(xid, _print);
198 for (i=0; i<no_elements; i++)
200 if (xmlNodeGetPos(xid, xmid, _print, i) != 0)
204 xmlCopyString(xmid, (char *)&value, 1024);
205 if (_value && _attribute)
207 if (!xmlAttributeCompareString(xmid, _attribute, _value))
209 printf("%s: <%s %s=\"%s\">%s</%s>\n",
210 _filenames[num], _print, _attribute, _value,
216 printf("%s: <%s>%s</%s>\n",
217 _filenames[num], _print, value, _print);
223 else if (xmid && _value)
225 no_elements = xmlNodeGetNum(xmid, _element);
226 for (i=0; i<no_elements; i++)
228 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
232 xmlNodeCopyName(xmid, (char *)&nodename, 64);
233 if (xmlCompareString(xmid, _value) == 0)
235 printf("%s: <%s>%s</%s>\n",
236 _filenames[num], nodename, _value, nodename);
242 else if (xmid && _element)
246 xmlNodeCopyName(xid, (char *)&parentname, 64);
248 no_elements = xmlNodeGetNum(xmid, _element);
249 for (i=0; i<no_elements; i++)
251 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
255 xmlNodeCopyName(xmid, (char *)&nodename, 64);
256 if (strncasecmp((char *)&nodename, _element, 64) == 0)
259 xmlCopyString(xmid, (char *)&value, 64);
260 printf("%s: <%s> <%s>%s</%s> </%s>\n",
261 _filenames[num], parentname, nodename, value,
262 nodename, parentname);
267 else printf("Error executing xmlMarkId\n");
269 else if (xid) /* walk the rest of the tree */
275 if (*elem == '/') elem++;
277 next = strchr(elem, '/');
279 xmid = xmlMarkId(xid);
287 no_elements = xmlNodeGetNum(xid, elem);
288 for (i=0; i<no_elements; i++)
290 if (xmlNodeGetPos(xid, xmid, elem, i) != 0)
291 walk_the_tree(num, xmid, next);
301 else printf("Error executing xmlMarkId\n");
306 void grep_file(unsigned num)
310 xid = xmlOpen(_filenames[num]);
313 void *xrid = xmlMarkId(xid);
314 walk_the_tree(num, xrid, _root);
319 fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
326 main (int argc, char **argv)
335 int ret = parse_option(argv, i, argc);
339 if (_root == 0) _root = (char *)_static_root;
340 if (_element == 0) _element = (char *)_static_element;
342 for (i=0; i<_fcount; i++)