]> git.mxchange.org Git - flightgear.git/blob - utils/xmlgrep/xmlgrep.c
688fb66818eeb95691fabbc91092b7c75f717e22
[flightgear.git] / utils / xmlgrep / xmlgrep.c
1 #include <stdio.h>
2
3 #define _GNU_SOURCE
4 #include <stdlib.h>
5 #include <string.h>
6 #ifndef _MSC_VER
7 # include <strings.h>
8 #else
9 # define strncasecmp strnicmp
10 # include <stdlib.h>
11 #endif
12 #include <assert.h>
13 #include <unistd.h>     /* read */
14 #include <sys/stat.h>   /* fstat */
15 #include <fcntl.h>      /* open */
16
17 #include "xml.h"
18
19 static const char *_static_root = "/";
20 static const char *_static_element = "*";
21 static unsigned int _fcount = 0;
22 static char **_filenames = 0;
23 static char *_element = 0;
24 static char *_value = 0;
25 static char *_root = 0;
26 static char *_print = 0;
27 static char *_attribute = 0;
28 static int print_filenames = 0;
29
30 static void free_and_exit(int i);
31
32 #define USE_BUFFER              0
33 #define NODE_NAME_LEN           256
34 #define STRING_LEN              2048
35
36 #define SHOW_NOVAL(opt) \
37 { \
38     printf("option '%s' requires a value\n\n", (opt)); \
39     free_and_exit(-1); \
40 }
41
42 void
43 show_help ()
44 {
45     printf("usage: xmlgrep [options] [file ...]\n\n");
46     printf("Options:\n");
47     printf("\t-h\t\tshow this help message\n");
48     printf("\t-a <string>\tprint this attribute as the output\n");
49     printf("\t-e <id>\t\tshow sections that contain this element\n");
50     printf("\t-p <id>\t\tprint this element as the output\n");
51     printf("\t-r <path>\tspecify the XML search root\n");
52     printf("\t-v <string>\tfilter sections that contain this vale\n\n");
53     printf(" To print the contents of the 'type' element of the XML section ");
54     printf("that begins\n at '/printer/output' use the following command:\n\n");
55     printf("\txmlgrep -r /printer/output -p type sample.xml\n\n");
56     printf(" To filter 'output' elements under '/printer' that have attribute");
57     printf(" 'n' set to '1'\n use the following command:\n\n");
58     printf("\txmlgrep -r /printer -p output -a n -v 1 sample.xml\n\n");
59     printf(" To filter out sections that contain the 'driver' element with ");
60     printf("'generic' as\n it's value use the following command:");
61     printf("\n\n\txmlgrep -r /printer/output -e driver -v generic sample.xml");
62     printf("\n\n");
63     free_and_exit(0);
64 }
65
66 void
67 free_and_exit(int ret)
68 {
69     if (_root && _root != _static_root) free(_root);
70     if (_element && _element != _static_element) free(_element);
71     if (_value) free(_value);
72     if (_print) free(_print);
73     if (_attribute) free(_attribute);
74     if (_filenames)
75     {
76         unsigned int i;
77         for (i=0; i < _fcount; i++)
78         {
79             if (_filenames[i])
80             {
81                 if (print_filenames) printf("%s\n", _filenames[i]);
82                 free(_filenames[i]);
83             }
84         }
85         free(_filenames);
86     }
87  
88     exit(ret);
89 }
90
91 int
92 parse_option(char **args, int n, int max)
93 {
94     char *opt, *arg = 0;
95     unsigned int alen = 0;
96     unsigned int olen;
97
98     opt = args[n];
99     if (strncmp(opt, "--", 2) == 0)
100         opt++;
101
102     if ((arg = strchr(opt, '=')) != NULL)
103     {
104         *arg++ = 0;
105     }
106     else if (++n < max)
107     {
108         arg = args[n];
109 #if 0
110         if (arg && arg[0] == '-')
111             arg = 0;
112 #endif
113     }
114
115     olen = strlen(opt);
116     if (strncmp(opt, "-help", olen) == 0)
117     {
118         show_help();
119     }
120     else if (strncmp(opt, "-root", olen) == 0)
121     {
122         if (arg == 0) SHOW_NOVAL(opt);
123         alen = strlen(arg)+1;
124         _root = malloc(alen);
125         memcpy(_root, arg, alen);
126         return 2;
127     }
128     else if (strncmp(opt, "-element", olen) == 0)
129     {
130         if (arg == 0) SHOW_NOVAL(opt);
131         alen = strlen(arg)+1;
132         _element = malloc(alen);
133         memcpy(_element, arg, alen);
134         return 2;
135     }
136     else if (strncmp(opt, "-value", olen) == 0)
137     {
138         if (arg == 0) SHOW_NOVAL(opt);
139         alen = strlen(arg)+1;
140         _value = malloc(alen);
141         memcpy(_value, arg, alen);
142         return 2;
143     }
144     else if (strncmp(opt, "-print", olen) == 0)
145     {
146         if (arg == 0) SHOW_NOVAL(opt);
147         alen = strlen(arg)+1;
148         _print = malloc(alen);
149         memcpy(_print, arg, alen);
150         return 2;
151     }
152     else if (strncmp(opt, "-attribute", olen) == 0)
153     {
154         if (arg == 0) SHOW_NOVAL(opt);
155         alen = strlen(arg)+1;
156         _attribute = malloc(alen);
157         memcpy(_attribute, arg, alen);
158         return 2;
159     }
160     else if (strncmp(opt, "-list-filenames", olen) == 0)
161     { /* undocumented test argument */
162         print_filenames = 1;
163         return 1;
164     }
165     else if (opt[0] == '-')
166     {
167         printf("Unknown option %s\n", opt);
168         free_and_exit(-1);
169     }
170     else
171     {
172         int pos = _fcount++;
173         if (_filenames == 0)
174         {
175             _filenames = (char **)malloc(sizeof(char*));
176         }
177         else
178         {
179             char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*));
180             if (ptr == 0)
181             {
182                 printf("Out of memory.\n\n");
183                 free_and_exit(-1);
184             }
185            _filenames = ptr;
186         }
187
188         alen = strlen(opt)+1;
189         _filenames[pos] = malloc(alen);
190         memcpy(_filenames[pos], opt, alen);
191     }
192
193     return 1;
194 }
195
196 void walk_the_tree(size_t num, void *xid, char *tree)
197 {
198     unsigned int i, no_elements;
199
200     if (!tree)                                  /* last node from the tree */
201     {
202         void *xmid = xmlMarkId(xid);
203         if (xmid && _print)
204         {
205             no_elements = xmlNodeGetNum(xid, _print);
206             for (i=0; i<no_elements; i++)
207             {
208                 if (xmlNodeGetPos(xid, xmid, _print, i) != 0)
209                 {
210                     char value[STRING_LEN];
211
212                     xmlCopyString(xmid, (char *)&value, STRING_LEN);
213                     if (_value && _attribute)
214                     {
215                        if (!xmlAttributeCompareString(xmid, _attribute, _value))
216                        {
217                           printf("%s: <%s %s=\"%s\">%s</%s>\n",
218                                  _filenames[num], _print, _attribute, _value,
219                                                   value, _print);
220                        }
221                     }
222                     else
223                     {
224                        printf("%s: <%s>%s</%s>\n",
225                               _filenames[num], _print, value, _print);
226                     }
227                 }
228             }
229             free(xmid);
230         }
231         else if (xmid && _value)
232         {
233             no_elements = xmlNodeGetNum(xmid, _element);
234             for (i=0; i<no_elements; i++)
235             {
236                 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
237                 {
238                     char nodename[NODE_NAME_LEN];
239
240                     xmlNodeCopyName(xmid, (char *)&nodename, NODE_NAME_LEN);
241                     if (xmlCompareString(xmid, _value) == 0)
242                     {
243                         printf("%s: <%s>%s</%s>\n",
244                                _filenames[num], nodename, _value, nodename);
245                     }
246                 }
247             }
248             free(xmid);
249         }
250         else if (xmid && _element)
251         {
252             char parentname[NODE_NAME_LEN];
253
254             xmlNodeCopyName(xid, (char *)&parentname, NODE_NAME_LEN);
255
256             no_elements = xmlNodeGetNum(xmid, _element);
257             for (i=0; i<no_elements; i++)
258             {
259                 if (xmlNodeGetPos(xid, xmid, _element, i) != 0)
260                 {
261                     char nodename[NODE_NAME_LEN];
262
263                     xmlNodeCopyName(xmid, (char *)&nodename, NODE_NAME_LEN);
264                     if (!strncasecmp((char*)&nodename, _element, NODE_NAME_LEN))
265                     {
266                         char value[NODE_NAME_LEN];
267                         xmlCopyString(xmid, (char *)&value, NODE_NAME_LEN);
268                         printf("%s: <%s> <%s>%s</%s> </%s>\n",
269                                 _filenames[num], parentname, nodename, value,
270                                                  nodename, parentname);
271                     }
272                 }
273             }
274         }
275         else printf("Error executing xmlMarkId\n");
276     }
277     else if (xid)                        /* walk the rest of the tree */
278     {
279         char *elem, *next;
280         void *xmid;
281
282         elem = tree;
283         if (*elem == '/') elem++;
284
285         next = strchr(elem, '/');
286         
287         xmid = xmlMarkId(xid);
288         if (xmid)
289         {
290             if (next)
291             {
292                *next++ = 0;
293             }
294
295             no_elements = xmlNodeGetNum(xid, elem);
296             for (i=0; i<no_elements; i++)
297             {
298                 if (xmlNodeGetPos(xid, xmid, elem, i) != 0)
299                     walk_the_tree(num, xmid, next);
300             }
301
302             if (next)
303             {
304                *--next = '/';
305             }
306
307             free(xmid);
308         }
309         else printf("Error executing xmlMarkId\n");
310     }
311 }
312
313 void grep_file(unsigned num)
314 {
315     void *xid;
316
317     xid = xmlOpen(_filenames[num]);
318     if (xid)
319     {
320        void *xrid = xmlMarkId(xid);
321        int r = 0;
322
323        walk_the_tree(num, xrid, _root);
324
325        r = xmlErrorGetNo(xrid, 0);
326        if (r)
327        {
328             size_t n = xmlErrorGetLineNo(xrid, 0);
329             size_t c = xmlErrorGetColumnNo(xrid, 0);
330             const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
331             printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
332        }
333
334        free(xrid);
335     }
336     else
337     {
338         fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
339     }
340
341     xmlClose(xid);
342 }
343
344
345 void grep_file_buffer(unsigned num)
346 {
347     struct stat st;
348     void *xid, *buf;
349     int fd, res;
350
351     fd = open(_filenames[num], O_RDONLY);
352     if (fd == -1)
353     {
354         printf("read error opening file '%s'\n", _filenames[num]);
355         return;
356     }
357     
358     fstat(fd, &st);
359     buf = malloc(st.st_size);
360     if (!buf)
361     {
362         printf("unable to allocate enough memory for reading.\n");
363         return;
364     }
365
366     res = read(fd, buf, st.st_size);
367     if (res == -1)
368     {
369         printf("unable to read from file '%s'.\n", _filenames[num]);
370         return;
371     }
372     close(fd);
373
374     xid = xmlInitBuffer(buf, st.st_size);
375     if (xid)
376     {
377        void *xrid = xmlMarkId(xid);
378        int r = 0;
379
380        walk_the_tree(num, xrid, _root);
381
382        r = xmlErrorGetNo(xrid, 0);
383        if (r)
384        {
385             size_t n = xmlErrorGetLineNo(xrid, 0);
386             size_t c = xmlErrorGetColumnNo(xrid, 0);
387             const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
388             printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
389        }
390
391        free(xrid);
392     }
393     else
394     {
395         fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
396     }
397
398     xmlClose(xid);
399     free(buf);
400 }
401
402 int
403 main (int argc, char **argv)
404 {
405     unsigned int u;
406     int i;
407
408     if (argc == 1)
409         show_help();
410
411     for (i=1; i<argc;)
412     {
413         int ret = parse_option(argv, i, argc);
414         i += ret;
415     }
416
417     if (_root == 0) _root = (char *)_static_root;
418     if (_element == 0) _element = (char *)_static_element;
419
420     for (u=0; u<_fcount; u++)
421 #if USE_BUFFER
422         grep_file_buffer(u);
423 #else
424         grep_file(u);
425 #endif
426
427     free_and_exit(0);
428
429     return 0;
430 }