8 # include <unistd.h> /* read */
10 # define strncasecmp strnicmp
15 #include <sys/stat.h> /* fstat */
16 #include <fcntl.h> /* open */
20 static const char *_static_root = "/";
21 static const char *_static_element = "*";
22 static unsigned int _fcount = 0;
23 static char **_filenames = 0;
24 static char *_element = 0;
25 static char *_value = 0;
26 static char *_root = 0;
27 static char *_print = 0;
28 static char *_attribute = 0;
29 static int print_filenames = 0;
31 static void free_and_exit(int i);
34 #define NODE_NAME_LEN 256
35 #define STRING_LEN 2048
37 #define SHOW_NOVAL(opt) \
39 printf("option '%s' requires a value\n\n", (opt)); \
46 printf("usage: xmlgrep [options] [file ...]\n\n");
48 printf("\t-h\t\tshow this help message\n");
49 printf("\t-a <string>\tprint this attribute as the output\n");
50 printf("\t-e <id>\t\tshow sections that contain this element\n");
51 printf("\t-p <id>\t\tprint this element as the output\n");
52 printf("\t-r <path>\tspecify the XML search root\n");
53 printf("\t-v <string>\tfilter sections that contain this vale\n\n");
54 printf(" To print the contents of the 'type' element of the XML section ");
55 printf("that begins\n at '/printer/output' use the following command:\n\n");
56 printf("\txmlgrep -r /printer/output -p type sample.xml\n\n");
57 printf(" To filter 'output' elements under '/printer' that have attribute");
58 printf(" 'n' set to '1'\n use the following command:\n\n");
59 printf("\txmlgrep -r /printer -p output -a n -v 1 sample.xml\n\n");
60 printf(" To filter out sections that contain the 'driver' element with ");
61 printf("'generic' as\n it's value use the following command:");
62 printf("\n\n\txmlgrep -r /printer/output -e driver -v generic sample.xml");
68 free_and_exit(int ret)
70 if (_root && _root != _static_root) free(_root);
71 if (_element && _element != _static_element) free(_element);
72 if (_value) free(_value);
73 if (_print) free(_print);
74 if (_attribute) free(_attribute);
78 for (i=0; i < _fcount; i++)
82 if (print_filenames) printf("%s\n", _filenames[i]);
93 parse_option(char **args, int n, int max)
96 unsigned int alen = 0;
100 if (strncmp(opt, "--", 2) == 0)
103 if ((arg = strchr(opt, '=')) != NULL)
111 if (arg && arg[0] == '-')
117 if (strncmp(opt, "-help", olen) == 0)
121 else if (strncmp(opt, "-root", olen) == 0)
123 if (arg == 0) SHOW_NOVAL(opt);
124 alen = strlen(arg)+1;
125 if (_root) free(_root);
126 _root = malloc(alen);
127 memcpy(_root, arg, alen);
130 else if (strncmp(opt, "-element", olen) == 0)
132 if (arg == 0) SHOW_NOVAL(opt);
133 alen = strlen(arg)+1;
134 if (_element) free(_element);
135 _element = malloc(alen);
136 memcpy(_element, arg, alen);
139 else if (strncmp(opt, "-value", olen) == 0)
141 if (arg == 0) SHOW_NOVAL(opt);
142 alen = strlen(arg)+1;
143 if (_value) free(_value);
144 _value = malloc(alen);
145 memcpy(_value, arg, alen);
148 else if (strncmp(opt, "-print", olen) == 0)
150 if (arg == 0) SHOW_NOVAL(opt);
151 alen = strlen(arg)+1;
152 if (_print) free(_print);
153 _print = malloc(alen);
154 memcpy(_print, arg, alen);
157 else if (strncmp(opt, "-attribute", olen) == 0)
159 if (arg == 0) SHOW_NOVAL(opt);
160 alen = strlen(arg)+1;
161 if (_attribute) free(_attribute);
162 _attribute = malloc(alen);
163 memcpy(_attribute, arg, alen);
166 else if (strncmp(opt, "-list-filenames", olen) == 0)
167 { /* undocumented test argument */
171 else if (opt[0] == '-')
173 printf("Unknown option %s\n", opt);
181 _filenames = (char **)malloc(sizeof(char*));
185 char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*));
188 printf("Out of memory.\n\n");
194 alen = strlen(opt)+1;
195 _filenames[pos] = malloc(alen);
196 memcpy(_filenames[pos], opt, alen);
202 void walk_the_tree(size_t num, void *xid, char *tree)
204 unsigned int i, no_elements;
206 if (!tree) /* last node from the tree */
208 void *xmid = xmlMarkId(xid);
211 no_elements = xmlNodeGetNum(xid, _print);
212 for (i=0; i<no_elements; i++)
214 if (xmlNodeGetPos(xid, xmid, _print, i) != 0)
218 value = xmlGetString(xmid);
219 if (_value && _attribute && value)
221 if (!xmlAttributeCompareString(xmid, _attribute, _value))
223 printf("%s: <%s %s=\"%s\">%s</%s>\n",
224 _filenames[num], _print, _attribute, _value,
227 if (value) free(value);
231 printf("%s: <%s>%s</%s>\n",
232 _filenames[num], _print, value, _print);
238 else if (xmid && _value)
240 no_elements = xmlNodeGetNum(xmid, _element);
241 for (i=0; i<no_elements; i++)
243 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
245 char nodename[NODE_NAME_LEN];
247 xmlNodeCopyName(xmid, (char *)&nodename, NODE_NAME_LEN);
248 if (xmlCompareString(xmid, _value) == 0)
250 printf("%s: <%s>%s</%s>\n",
251 _filenames[num], nodename, _value, nodename);
257 else if (xmid && _element)
259 char parentname[NODE_NAME_LEN];
261 xmlNodeCopyName(xid, (char *)&parentname, NODE_NAME_LEN);
263 no_elements = xmlNodeGetNum(xmid, _element);
264 for (i=0; i<no_elements; i++)
266 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
268 char nodename[NODE_NAME_LEN];
270 xmlNodeCopyName(xmid, (char *)&nodename, NODE_NAME_LEN);
271 if (!strncasecmp((char*)&nodename, _element, NODE_NAME_LEN))
273 char value[NODE_NAME_LEN];
274 xmlCopyString(xmid, (char *)&value, NODE_NAME_LEN);
275 printf("%s: <%s> <%s>%s</%s> </%s>\n",
276 _filenames[num], parentname, nodename, value,
277 nodename, parentname);
282 else printf("Error executing xmlMarkId\n");
284 else if (xid) /* walk the rest of the tree */
290 if (*elem == '/') elem++;
292 next = strchr(elem, '/');
294 xmid = xmlMarkId(xid);
302 no_elements = xmlNodeGetNum(xid, elem);
303 for (i=0; i<no_elements; i++)
305 if (xmlNodeGetPos(xid, xmid, elem, i) != 0)
306 walk_the_tree(num, xmid, next);
316 else printf("Error executing xmlMarkId\n");
320 void grep_file(unsigned num)
324 xid = xmlOpen(_filenames[num]);
327 void *xrid = xmlMarkId(xid);
330 walk_the_tree(num, xrid, _root);
332 r = xmlErrorGetNo(xrid, 0);
335 size_t n = xmlErrorGetLineNo(xrid, 0);
336 size_t c = xmlErrorGetColumnNo(xrid, 0);
337 const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
338 printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
345 fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
352 void grep_file_buffer(unsigned num)
358 fd = open(_filenames[num], O_RDONLY);
361 printf("read error opening file '%s'\n", _filenames[num]);
366 buf = malloc(st.st_size);
369 printf("unable to allocate enough memory for reading.\n");
373 res = read(fd, buf, st.st_size);
376 printf("unable to read from file '%s'.\n", _filenames[num]);
381 xid = xmlInitBuffer(buf, st.st_size);
384 void *xrid = xmlMarkId(xid);
387 walk_the_tree(num, xrid, _root);
389 r = xmlErrorGetNo(xrid, 0);
392 size_t n = xmlErrorGetLineNo(xrid, 0);
393 size_t c = xmlErrorGetColumnNo(xrid, 0);
394 const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
395 printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
402 fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
410 main (int argc, char **argv)
420 int ret = parse_option(argv, i, argc);
424 if (_root == 0) _root = (char *)_static_root;
425 if (_element == 0) _element = (char *)_static_element;
427 for (u=0; u<_fcount; u++)