10 static const char *_static_root = "/";
11 static const char *_static_element = "*";
12 static unsigned int _fcount = 0;
13 static char **_filenames = 0;
14 static char *_element = 0;
15 static char *_value = 0;
16 static char *_root = 0;
17 static char *_print = 0;
18 static char *_attribute = 0;
19 static int print_filenames = 0;
21 static void free_and_exit(int i);
23 #define SHOW_NOVAL(opt) \
25 printf("option '%s' requires a value\n\n", (opt)); \
32 printf("usage: xmlgrep [options] [file ...]\n\n");
34 printf("\t-h\t\tshow this help message\n");
35 printf("\t-a <string>\tprint this attribute as the output\n");
36 printf("\t-e <id>\t\tshow sections that contain this element\n");
37 printf("\t-p <id>\t\tprint this element as the output\n");
38 printf("\t-r <path>\tspecify the XML search root\n");
39 printf("\t-v <string>\tfilter sections that contain this vale\n\n");
40 printf(" To print the contents of the 'type' element of the XML section ");
41 printf("that begins\n at '/printer/output' use the following command:\n\n");
42 printf("\txmlgrep -r /printer/output -p type sample.xml\n\n");
43 printf(" To filter 'output' elements under '/printer' that have attribute");
44 printf(" 'n' set to '1'\n use the following command:\n\n");
45 printf("\txmlgrep -r /printer -p output -a n -v 1 sample.xml\n\n");
46 printf(" To filter out sections that contain the 'driver' element with ");
47 printf("'generic' as\n it's value use the following command:");
48 printf("\n\n\txmlgrep -r /printer/output -e driver -v generic sample.xml");
54 free_and_exit(int ret)
56 if (_root && _root != _static_root) free(_root);
57 if (_element && _element != _static_element) free(_element);
58 if (_value) free(_value);
59 if (_print) free(_print);
60 if (_attribute) free(_attribute);
64 for (i=0; i < _fcount; i++)
68 if (print_filenames) printf("%s\n", _filenames[i]);
79 parse_option(char **args, int n, int max)
82 unsigned int alen = 0;
86 if (strncmp(opt, "--", 2) == 0)
89 if ((arg = strchr(opt, '=')) != NULL)
97 if (arg && arg[0] == '-')
103 if (strncmp(opt, "-help", olen) == 0)
107 else if (strncmp(opt, "-root", olen) == 0)
109 if (arg == 0) SHOW_NOVAL(opt);
110 alen = strlen(arg)+1;
111 _root = malloc(alen);
112 memcpy(_root, arg, alen);
115 else if (strncmp(opt, "-element", olen) == 0)
117 if (arg == 0) SHOW_NOVAL(opt);
118 alen = strlen(arg)+1;
119 _element = malloc(alen);
120 memcpy(_element, arg, alen);
123 else if (strncmp(opt, "-value", olen) == 0)
125 if (arg == 0) SHOW_NOVAL(opt);
126 alen = strlen(arg)+1;
127 _value = malloc(alen);
128 memcpy(_value, arg, alen);
131 else if (strncmp(opt, "-print", olen) == 0)
133 if (arg == 0) SHOW_NOVAL(opt);
134 alen = strlen(arg)+1;
135 _print = malloc(alen);
136 memcpy(_print, arg, alen);
139 else if (strncmp(opt, "-attribute", olen) == 0)
141 if (arg == 0) SHOW_NOVAL(opt);
142 alen = strlen(arg)+1;
143 _attribute = malloc(alen);
144 memcpy(_attribute, arg, alen);
147 else if (strncmp(opt, "-list-filenames", olen) == 0)
148 { /* undocumented test argument */
152 else if (opt[0] == '-')
154 printf("Unknown option %s\n", opt);
162 _filenames = (char **)malloc(sizeof(char*));
166 char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*));
169 printf("Out of memory.\n\n");
175 alen = strlen(opt)+1;
176 _filenames[pos] = malloc(alen);
177 memcpy(_filenames[pos], opt, alen);
183 void walk_the_tree(size_t num, void *xid, char *tree)
185 unsigned int i, no_elements;
187 if (!tree) /* last node from the tree */
189 void *xmid = xmlMarkId(xid);
192 no_elements = xmlNodeGetNum(xid, _print);
193 for (i=0; i<no_elements; i++)
195 if (xmlNodeGetPos(xid, xmid, _print, i) != 0)
199 xmlCopyString(xmid, (char *)&value, 1024);
200 if (_value && _attribute)
202 if (!xmlAttributeCompareString(xmid, _attribute, _value))
204 printf("%s: <%s %s=\"%s\">%s</%s>\n",
205 _filenames[num], _print, _attribute, _value,
211 printf("%s: <%s>%s</%s>\n",
212 _filenames[num], _print, value, _print);
218 else if (xmid && _value)
220 no_elements = xmlNodeGetNum(xmid, _element);
221 for (i=0; i<no_elements; i++)
223 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
227 xmlNodeCopyName(xmid, (char *)&nodename, 64);
228 if (xmlCompareString(xmid, _value) == 0)
230 printf("%s: <%s>%s</%s>\n",
231 _filenames[num], nodename, _value, nodename);
237 else if (xmid && _element)
241 xmlNodeCopyName(xid, (char *)&parentname, 64);
243 no_elements = xmlNodeGetNum(xmid, _element);
244 for (i=0; i<no_elements; i++)
246 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
250 xmlNodeCopyName(xmid, (char *)&nodename, 64);
251 if (strncasecmp((char *)&nodename, _element, 64) == 0)
254 xmlCopyString(xmid, (char *)&value, 64);
255 printf("%s: <%s> <%s>%s</%s> </%s>\n",
256 _filenames[num], parentname, nodename, value,
257 nodename, parentname);
262 else printf("Error executing xmlMarkId\n");
264 else if (xid) /* walk the rest of the tree */
270 if (*elem == '/') elem++;
272 next = strchr(elem, '/');
274 xmid = xmlMarkId(xid);
282 no_elements = xmlNodeGetNum(xid, elem);
283 for (i=0; i<no_elements; i++)
285 if (xmlNodeGetPos(xid, xmid, elem, i) != 0)
286 walk_the_tree(num, xmid, next);
296 else printf("Error executing xmlMarkId\n");
301 void grep_file(unsigned num)
305 xid = xmlOpen(_filenames[num]);
308 void *xrid = xmlMarkId(xid);
309 walk_the_tree(num, xrid, _root);
314 fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
321 main (int argc, char **argv)
330 int ret = parse_option(argv, i, argc);
334 if (_root == 0) _root = (char *)_static_root;
335 if (_element == 0) _element = (char *)_static_element;
337 for (i=0; i<_fcount; i++)