6 #include <simgear/props/props.hxx>
7 #include <simgear/props/props_io.hxx>
8 #include <simgear/structure/exception.hxx>
11 unsigned int _fcount = 0;
12 char **_filenames = 0;
18 int print_filenames = 0;
22 void free_and_exit(int i);
25 #define SHOW_NOVAL(opt) \
27 printf("option '%s' requires a value\n\n", (opt)); \
33 printf("usage: xmlgrep [options] [file ...]\n\n");
35 printf("\t-h\t\tshow this help message\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>\tshow sections where on of the elements has this value \n");
41 printf(" To print the contents of the 'type' element of the XML section ");
42 printf("that begins\n at '/printer/output' one would use the following ");
43 printf("syntax:\n\n\txmlgrep -r /printer/output -p type sample.xml\n\n");
44 printf(" To filter out sections that contain the 'driver' element with ");
45 printf("'generic' as\n it's value one would issue the following command:\n");
46 printf("\n\txmlgrep -r /printer/output -e driver -v generic -p type ");
47 printf("sample.xml\n\n");
51 void free_and_exit(int i)
53 if (_root) free(_root);
54 if (_value) free(_value);
55 if (_element) free(_element);
58 for (i=0; i < _fcount; i++) {
60 if (print_filenames) printf("%s\n", _filenames[i]);
70 int parse_option(char **args, int n, int max) {
75 if (opt[0] == '-' && opt[1] == '-')
78 if ((arg = strchr(opt, '=')) != NULL)
84 if (arg && arg[0] == '-')
89 fprintf(stderr, "processing '%s'='%s'\n", opt, arg ? arg : "NULL");
93 if (strncmp(opt, "-help", sz) == 0) {
97 else if (strncmp(opt, "-root", sz) == 0) {
98 if (arg == 0) SHOW_NOVAL(opt);
101 fprintf(stderr, "\troot=%s\n", _root);
106 else if (strncmp(opt, "-element", sz) == 0) {
107 if (arg == 0) SHOW_NOVAL(opt);
108 _element = strdup(arg);
110 fprintf(stderr, "\telement=%s\n", _element);
115 else if (strncmp(opt, "-value", sz) == 0) {
116 if (arg == 0) SHOW_NOVAL(opt);
117 _value = strdup(arg);
119 fprintf(stderr, "\tvalue=%s\n", _value);
124 else if (strncmp(opt, "-print", sz) == 0) {
125 if (arg == 0) SHOW_NOVAL(opt);
126 _print = strdup(arg);
128 fprintf(stderr, "\tprint=%s\n", _print);
134 /* undocumented test argument */
135 else if (strncmp(opt, "-list-filenames", sz) == 0) {
140 else if (opt[0] == '-') {
141 printf("Unknown option %s\n", opt);
148 _filenames = (char **)malloc(sizeof(char*));
150 char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*));
152 printf("Out of memory.\n\n");
158 _filenames[pos] = strdup(opt);
160 fprintf(stderr, "\tadding filenames[%i]='%s'\n", pos, _filenames[pos]);
167 void grep_file(unsigned num)
169 SGPropertyNode root, *path;
172 fprintf(stderr, "Reading filenames[%i]: %s ... ", num, _filenames[num]);
175 readProperties(_filenames[num], &root);
176 } catch (const sg_exception &e) {
177 fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
178 // free_and_exit(-1);
182 fprintf(stderr, "done.\n");
185 if ((path = root.getNode(_root, false)) != NULL)
187 SGPropertyNode *elem;
189 if (_element && _value)
191 if ((elem = path->getNode(_element, false)) != NULL)
193 if (strcmp(elem->getStringValue(), _value) == NULL)
195 SGPropertyNode *print = path->getNode(_print, false);
198 printf("%s: <%s>%s</%s>\n", _filenames[num],
199 _print, print->getStringValue(), _print);
213 fprintf(stderr," No root node specified.\n");
217 inline void grep_files()
220 fprintf(stderr, "Reading files ...\n");
222 for (int i=0; i<_fcount; i++)
227 main (int argc, char **argv)
236 int ret = parse_option(argv, i, argc);
239 fprintf(stderr, "%i arguments processed.\n", ret);