Simple fixes to get navit compiling with MSVC
[profile/ivi/navit.git] / navit / navit / xmlconfig.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2009 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 /* see http://library.gnome.org/devel/glib/stable/glib-Simple-XML-Subset-Parser.html
21  * for details on how the xml file parser works.
22  */
23
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <glib/gprintf.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include "debug.h"
30 #include "config.h"
31 #include "file.h"
32 #include "coord.h"
33 #include "layout.h"
34 #include "mapset.h"
35 #include "projection.h"
36 #include "map.h"
37 #include "navigation.h"
38 #include "navit.h"
39 #include "plugin.h"
40 #include "route.h"
41 #include "speech.h"
42 #include "track.h"
43 #include "vehicle.h"
44 #include "point.h"
45 #include "graphics.h"
46 #include "gui.h"
47 #include "osd.h"
48 #include "log.h"
49 #include "announcement.h"
50 #include "vehicleprofile.h"
51 #include "roadprofile.h"
52 #include "config_.h"
53 #include "xmlconfig.h"
54
55 #if (defined __MINGW32__) || (defined _MSC_VER)
56 /* This only works if a is a string constant, i.e. "name" */
57 #define unsetenv(a) putenv(a "=")
58 #endif
59
60 #ifdef HAVE_GLIB
61 #define ATTR_DISTANCE 1
62 const int xml_attr_distance=1;
63 #else
64 #include "ezxml.h"
65 const int xml_attr_distance=2;
66 #define ATTR_DISTANCE 2
67 #define G_MARKUP_ERROR 0
68 #define G_MARKUP_ERROR_INVALID_CONTENT 0
69 #define G_MARKUP_ERROR_PARSE 0
70 #define G_MARKUP_ERROR_UNKNOWN_ELEMENT 0
71 typedef void * GMarkupParseContext;
72 #endif
73
74 struct xistate {
75         struct xistate *parent;
76         struct xistate *child;
77         const gchar *element;
78         const gchar **attribute_names;
79         const gchar **attribute_values;
80 };
81
82 struct xmldocument {
83         const gchar *href;
84         const gchar *xpointer;
85         gpointer user_data;
86         struct xistate *first;
87         struct xistate *last;
88         int active;
89         int level;
90 };
91
92
93 struct xmlstate {
94         const gchar **attribute_names;
95         const gchar **attribute_values;
96         struct xmlstate *parent;
97         struct attr element_attr;
98         const gchar *element;
99         xmlerror **error;
100         struct element_func *func;
101         struct object_func *object_func;
102         struct xmldocument *document;
103 };
104
105
106 struct attr_fixme {
107         char *element;
108         char **attr_fixme;
109 };
110
111 static struct attr ** convert_to_attrs(struct xmlstate *state, struct attr_fixme *fixme)
112 {
113         const gchar **attribute_name=state->attribute_names;
114         const gchar **attribute_value=state->attribute_values;
115         const gchar *name;
116         int count=0;
117         struct attr **ret;
118         static int fixme_count;
119
120         while (*attribute_name) {
121                 count++;
122                 attribute_name++;
123         }
124         ret=g_new(struct attr *, count+1);
125         attribute_name=state->attribute_names;
126         count=0;
127         while (*attribute_name) {
128                 name=*attribute_name;
129                 if (fixme) {
130                         char **attr_fixme=fixme->attr_fixme;
131                         while (attr_fixme[0]) {
132                                 if (! strcmp(name, attr_fixme[0])) {
133                                         name=attr_fixme[1];
134                                         if (fixme_count++ < 10)
135                                                 dbg(0,"Please change attribute '%s' to '%s' in <%s />\n", attr_fixme[0], attr_fixme[1], fixme->element);
136                                         break;
137                                 }
138                                 attr_fixme+=2;
139                         }
140                 }
141                 ret[count]=attr_new_from_text(name,*attribute_value);
142                 if (ret[count])
143                         count++;
144                 else if (strcmp(*attribute_name,"enabled") && strcmp(*attribute_name,"xmlns:xi"))
145                         dbg(0,"failed to create attribute '%s' with value '%s'\n", *attribute_name,*attribute_value);
146                 attribute_name++;
147                 attribute_value++;
148         }
149         ret[count]=NULL;
150         dbg(1,"ret=%p\n", ret);
151         return ret;
152 }
153
154
155 static const char * find_attribute(struct xmlstate *state, const char *attribute, int required)
156 {
157         const gchar **attribute_name=state->attribute_names;
158         const gchar **attribute_value=state->attribute_values;
159         while(*attribute_name) {
160                 if(! g_ascii_strcasecmp(attribute,*attribute_name))
161                         return *attribute_value;
162                 attribute_name++;
163                 attribute_value++;
164         }
165         if (required)
166                 g_set_error(state->error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "element '%s' is missing attribute '%s'", state->element, attribute);
167         return NULL;
168 }
169
170 static int
171 find_boolean(struct xmlstate *state, const char *attribute, int deflt, int required)
172 {
173         const char *value;
174
175         value=find_attribute(state, attribute, required);
176         if (! value)
177                 return deflt;
178         if (g_ascii_strcasecmp(value,"no") && g_ascii_strcasecmp(value,"0") && g_ascii_strcasecmp(value,"false"))
179                 return 1;
180         return 0;
181 }
182
183 /**
184  * * Convert a string number to int
185  * *
186  * * @param val the string value to convert
187  * * @returns int value of converted string
188  * */
189 static int
190 convert_number(const char *val)
191 {
192         if (val)
193                 return g_ascii_strtoull(val,NULL,0);
194         else
195                 return 0;
196 }
197
198 static int
199 xmlconfig_announce(struct xmlstate *state)
200 {
201         const char *type,*value;
202         char key[32];
203         int level[3];
204         int i;
205         enum item_type itype;
206         char *tok, *type_str, *str;
207
208         type=find_attribute(state, "type", 1);
209         if (! type)
210                 return 0;
211         for (i = 0 ; i < 3 ; i++) {
212                 sprintf(key,"level%d", i);
213                 value=find_attribute(state, key, 0);
214                 if (value)
215                         level[i]=convert_number(value);
216                 else
217                         level[i]=-1;
218         }
219         type_str=g_strdup(type);
220         str=type_str;
221         while ((tok=strtok(str, ","))) {
222                 itype=item_from_name(tok);
223                 navigation_set_announce(state->parent->element_attr.u.data, itype, level);
224                 str=NULL;
225         }
226         g_free(type_str);
227         return 1;
228 }
229 /**
230  * * Define the elements in our config
231  * *
232  * */
233
234 #define NEW(x) (void *(*)(struct attr *, struct attr **))(x)
235 #define GET(x) (int (*)(void *, enum attr_type type, struct attr *attr, struct attr_iter *iter))(x)
236 #define ITERN(x) (struct attr_iter * (*)(void *))(x)
237 #define ITERD(x) (void (*)(struct attr_iter *iter))(x)
238 #define SET(x) (int (*)(void *, struct attr *attr))(x)
239 #define ADD(x) (int (*)(void *, struct attr *attr))(x)
240 #define REMOVE(x) (int (*)(void *, struct attr *attr))(x)
241 #define INIT(x) (int (*)(void *))(x)
242 #define DESTROY(x) (void (*)(void *))(x)
243
244 static struct object_func object_funcs[] = {
245         { attr_announcement,NEW(announcement_new),  GET(announcement_get_attr), NULL, NULL, SET(announcement_set_attr), ADD(announcement_add_attr) },
246         { attr_arrows,     NEW(arrows_new)},
247         { attr_circle,     NEW(circle_new),   NULL, NULL, NULL, NULL, ADD(element_add_attr)},
248         { attr_config,     NEW(config_new), GET(config_get_attr), ITERN(config_attr_iter_new), ITERD(config_attr_iter_destroy), SET(config_set_attr), ADD(config_add_attr), REMOVE(config_remove_attr), NULL, DESTROY(config_destroy)},
249         { attr_coord,      NEW(coord_new_from_attrs)},
250         { attr_cursor,     NEW(cursor_new),   NULL, NULL, NULL, NULL, ADD(cursor_add_attr)},
251         { attr_debug,      NEW(debug_new)},
252         { attr_graphics,   NEW(graphics_new)},
253         { attr_gui,        NEW(gui_new), GET(gui_get_attr), NULL, NULL, SET(gui_set_attr), ADD(gui_add_attr)},
254         { attr_icon,       NEW(icon_new),     NULL, NULL, NULL, NULL, ADD(element_add_attr)},
255         { attr_image,      NEW(image_new)},
256         { attr_itemgra,    NEW(itemgra_new),  NULL, NULL, NULL, NULL, ADD(itemgra_add_attr)},
257         { attr_layer,      NEW(layer_new),    NULL, NULL, NULL, NULL, ADD(layer_add_attr)},
258         { attr_layout,     NEW(layout_new),   NULL, NULL, NULL, NULL, ADD(layout_add_attr)},
259         { attr_log,        NEW(log_new)},
260         { attr_navigation, NEW(navigation_new), GET(navigation_get_attr)},
261         { attr_osd,        NEW(osd_new),  GET(osd_get_attr), NULL, NULL, SET(osd_set_attr) },
262         { attr_plugins,    NEW(plugins_new),  NULL, NULL, NULL, NULL, NULL, NULL, INIT(plugins_init)},
263         { attr_plugin,     NEW(plugin_new)},
264         { attr_polygon,    NEW(polygon_new),  NULL, NULL, NULL, NULL, ADD(element_add_attr)},
265         { attr_polyline,   NEW(polyline_new), NULL, NULL, NULL, NULL, ADD(element_add_attr)},
266         { attr_roadprofile,NEW(roadprofile_new),  GET(roadprofile_get_attr), NULL, NULL, SET(roadprofile_set_attr), ADD(roadprofile_add_attr) },
267         { attr_route,      NEW(route_new), GET(route_get_attr), NULL, NULL, SET(route_set_attr), ADD(route_add_attr), REMOVE(route_remove_attr)},
268         { attr_speech,     NEW(speech_new), GET(speech_get_attr), NULL, NULL, SET(speech_set_attr)},
269         { attr_text,       NEW(text_new)},
270         { attr_vehicleprofile, NEW(vehicleprofile_new),  GET(vehicleprofile_get_attr), NULL, NULL, SET(vehicleprofile_set_attr), ADD(vehicleprofile_add_attr) },
271 };
272
273 struct object_func *
274 object_func_lookup(enum attr_type type)
275 {
276         int i;
277         switch (type) {
278         case attr_map:
279                 return &map_func;
280         case attr_maps:
281                 return &maps_func;
282         case attr_mapset:
283                 return &mapset_func;
284         case attr_navit:
285                 return &navit_func;
286         case attr_trackingo:
287                 return &tracking_func;
288         case attr_vehicle:
289                 return &vehicle_func;
290         default:
291                 for (i = 0 ; i < sizeof(object_funcs)/sizeof(struct object_func); i++) {
292                         if (object_funcs[i].type == type)
293                                 return &object_funcs[i];
294                 }
295                 return NULL;
296         }
297 }
298
299 struct element_func {
300         char *name;
301         char *parent;
302         int (*func)(struct xmlstate *state);
303         enum attr_type type;
304 };
305 struct element_func *elements;
306
307 static char *attr_fixme_itemgra[]={
308         "type","item_types",
309         NULL,NULL,
310 };
311
312 static char *attr_fixme_text[]={
313         "label_size","text_size",
314         NULL,NULL,
315 };
316
317 static char *attr_fixme_circle[]={
318         "label_size","text_size",
319         NULL,NULL,
320 };
321
322 static struct attr_fixme attr_fixmes[]={
323         {"item",attr_fixme_itemgra},
324         {"itemgra",attr_fixme_itemgra},
325         {"text",attr_fixme_text},
326         {"label",attr_fixme_text},
327         {"circle",attr_fixme_circle},
328         {NULL,NULL},
329 };
330
331
332 static char *element_fixmes[]={
333         "item","itemgra",
334         "label","text",
335         NULL,NULL,
336 };
337
338 static void initStatic(void) {
339         elements=g_new0(struct element_func,40); //39 is a number of elements + ending NULL element
340
341         elements[0].name="config";
342         elements[0].parent=NULL;
343         elements[0].func=NULL;
344         elements[0].type=attr_config;
345
346         elements[1].name="announce";
347         elements[1].parent="navigation";
348         elements[1].func=xmlconfig_announce;
349
350         elements[2].name="speech";
351         elements[2].parent="navit";
352         elements[2].func=NULL;
353         elements[2].type=attr_speech;
354
355         elements[3].name="tracking";
356         elements[3].parent="navit";
357         elements[3].func=NULL;
358         elements[3].type=attr_trackingo;
359
360         elements[4].name="route";
361         elements[4].parent="navit";
362         elements[4].func=NULL;
363         elements[4].type=attr_route;
364
365         elements[5].name="mapset";
366         elements[5].parent="navit";
367         elements[5].func=NULL;
368         elements[5].type=attr_mapset;
369
370         elements[6].name="map";
371         elements[6].parent="mapset";
372         elements[6].func=NULL;
373         elements[6].type=attr_map;
374
375         elements[7].name="debug";
376         elements[7].parent="config";
377         elements[7].func=NULL;
378         elements[7].type=attr_debug;
379
380         elements[8].name="osd";
381         elements[8].parent="navit";
382         elements[8].func=NULL;
383         elements[8].type=attr_osd;
384
385         elements[9].name="navigation";
386         elements[9].parent="navit";
387         elements[9].func=NULL;
388         elements[9].type=attr_navigation;
389
390         elements[10].name="navit";
391         elements[10].parent="config";
392         elements[10].func=NULL;
393         elements[10].type=attr_navit;
394
395         elements[11].name="graphics";
396         elements[11].parent="navit";
397         elements[11].func=NULL;
398         elements[11].type=attr_graphics;
399
400         elements[12].name="gui";
401         elements[12].parent="navit";
402         elements[12].func=NULL;
403         elements[12].type=attr_gui;
404
405         elements[13].name="layout";
406         elements[13].parent="navit";
407         elements[13].func=NULL;
408         elements[13].type=attr_layout;
409
410         elements[14].name="cursor";
411         elements[14].parent="layout";
412         elements[14].func=NULL;
413         elements[14].type=attr_cursor;
414
415         elements[15].name="layer";
416         elements[15].parent="layout";
417         elements[15].func=NULL;
418         elements[15].type=attr_layer;
419
420         elements[16].name="itemgra";
421         elements[16].parent="layer";
422         elements[16].func=NULL;
423         elements[16].type=attr_itemgra;
424
425         elements[17].name="circle";
426         elements[17].parent="itemgra";
427         elements[17].func=NULL;
428         elements[17].type=attr_circle;
429
430         elements[18].name="coord";
431         elements[18].parent="circle";
432         elements[18].func=NULL;
433         elements[18].type=attr_coord;
434
435         elements[19].name="icon";
436         elements[19].parent="itemgra";
437         elements[19].func=NULL;
438         elements[19].type=attr_icon;
439
440         elements[20].name="coord";
441         elements[20].parent="icon";
442         elements[20].func=NULL;
443         elements[20].type=attr_coord;
444
445         elements[21].name="image";
446         elements[21].parent="itemgra";
447         elements[21].func=NULL;
448         elements[21].type=attr_image;
449
450         elements[22].name="text";
451         elements[22].parent="itemgra";
452         elements[22].func=NULL;
453         elements[22].type=attr_text;
454
455         elements[23].name="polygon";
456         elements[23].parent="itemgra";
457         elements[23].func=NULL;
458         elements[23].type=attr_polygon;
459
460         elements[24].name="coord";
461         elements[24].parent="polygon";
462         elements[24].func=NULL;
463         elements[24].type=attr_coord;
464
465         elements[25].name="polyline";
466         elements[25].parent="itemgra";
467         elements[25].func=NULL;
468         elements[25].type=attr_polyline;
469
470         elements[26].name="coord";
471         elements[26].parent="polyline";
472         elements[26].func=NULL;
473         elements[26].type=attr_coord;
474
475         elements[27].name="arrows";
476         elements[27].parent="itemgra";
477         elements[27].func=NULL;
478         elements[27].type=attr_arrows;
479
480         elements[28].name="vehicle";
481         elements[28].parent="navit";
482         elements[28].func=NULL;
483         elements[28].type=attr_vehicle;
484
485         elements[29].name="vehicleprofile";
486         elements[29].parent="navit";
487         elements[29].func=NULL;
488         elements[29].type=attr_vehicleprofile;
489
490         elements[30].name="roadprofile";
491         elements[30].parent="vehicleprofile";
492         elements[30].func=NULL;
493         elements[30].type=attr_roadprofile;
494
495         elements[31].name="announcement";
496         elements[31].parent="roadprofile";
497         elements[31].func=NULL;
498         elements[31].type=attr_announcement;
499
500         elements[32].name="cursor";
501         elements[32].parent="vehicle";
502         elements[32].func=NULL;
503         elements[32].type=attr_cursor;
504
505         elements[33].name="itemgra";
506         elements[33].parent="cursor";
507         elements[33].func=NULL;
508         elements[33].type=attr_itemgra;
509
510         elements[34].name="log";
511         elements[34].parent="vehicle";
512         elements[34].func=NULL;
513         elements[34].type=attr_log;
514
515         elements[35].name="log";
516         elements[35].parent="navit";
517         elements[35].func=NULL;
518         elements[35].type=attr_log;
519
520         elements[36].name="plugins";
521         elements[36].parent="config";
522         elements[36].func=NULL;
523         elements[36].type=attr_plugins;
524
525         elements[37].name="plugin";
526         elements[37].parent="plugins";
527         elements[37].func=NULL;
528         elements[37].type=attr_plugin;
529
530         elements[38].name="maps";
531         elements[38].parent="mapset";
532         elements[38].func=NULL;
533         elements[38].type=attr_maps;
534 }
535
536 /**
537  * * Parse the opening tag of a config element
538  * *
539  * * @param context document parse context
540  * * @param element_name the current tag name
541  * * @param attribute_names ptr to return the set of attribute names
542  * * @param attribute_values ptr return the set of attribute values
543  * * @param user_data ptr to xmlstate structure
544  * * @param error ptr return error context
545  * * @returns nothing
546  * */
547
548 static void
549 start_element(GMarkupParseContext *context,
550                 const gchar         *element_name,
551                 const gchar        **attribute_names,
552                 const gchar        **attribute_values,
553                 gpointer             user_data,
554                 xmlerror             **error)
555 {
556         struct xmlstate *new=NULL, **parent = user_data;
557         struct element_func *e=elements,*func=NULL;
558         struct attr_fixme *attr_fixme=attr_fixmes;
559         char **element_fixme=element_fixmes;
560         int found=0;
561         static int fixme_count;
562         const char *parent_name=NULL;
563         char *s,*sep="",*possible_parents;
564         struct attr *parent_attr;
565         dbg(2,"name='%s' parent='%s'\n", element_name, *parent ? (*parent)->element:NULL);
566
567         if (!strcmp(element_name,"xml"))
568                 return;
569         /* determine if we have to fix any attributes */
570         while (attr_fixme[0].element) {
571                 if (!strcmp(element_name,attr_fixme[0].element))
572                         break;
573                 attr_fixme++;
574         }
575         if (!attr_fixme[0].element)
576                 attr_fixme=NULL;
577
578         /* tell user to fix  deprecated element names */
579         while (element_fixme[0]) {
580                 if (!strcmp(element_name,element_fixme[0])) {
581                         element_name=element_fixme[1];
582                         if (fixme_count++ < 10)
583                                 dbg(0,"Please change <%s /> to <%s /> in config file\n", element_fixme[0], element_fixme[1]);
584                 }
585                 element_fixme+=2;
586         }
587         /* validate that this element is valid
588          * and that the element has a valid parent */
589         possible_parents=g_strdup("");
590         if (*parent)
591                 parent_name=(*parent)->element;
592         while (e->name) {
593                 if (!g_ascii_strcasecmp(element_name, e->name)) {
594                         found=1;
595                         s=g_strconcat(possible_parents,sep,e->parent,NULL);
596                         g_free(possible_parents);
597                         possible_parents=s;
598                         sep=",";
599                         if ((parent_name && e->parent && !g_ascii_strcasecmp(parent_name, e->parent)) ||
600                             (!parent_name && !e->parent))
601                                 func=e;
602                 }
603                 e++;
604         }
605         if (! found) {
606                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_UNKNOWN_ELEMENT,
607                                 "Unknown element '%s'", element_name);
608                 g_free(possible_parents);
609                 return;
610         }
611         if (! func) {
612                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT,
613                                 "Element '%s' within unexpected context '%s'. Expected '%s'%s",
614                                 element_name, parent_name, possible_parents, ! strcmp(possible_parents, "config") ? "\nPlease add <config> </config> tags at the beginning/end of your navit.xml": "");
615                 g_free(possible_parents);
616                 return;
617         }
618         g_free(possible_parents);
619
620         new=g_new(struct xmlstate, 1);
621         new->attribute_names=attribute_names;
622         new->attribute_values=attribute_values;
623         new->parent=*parent;
624         new->element_attr.u.data=NULL;
625         new->element=element_name;
626         new->error=error;
627         new->func=func;
628         new->object_func=NULL;
629         *parent=new;
630         if (!find_boolean(new, "enabled", 1, 0))
631                 return;
632         if (new->parent && !new->parent->element_attr.u.data)
633                 return;
634         if (func->func) {
635                 if (!func->func(new)) {
636                         return;
637                 }
638         } else {
639                 struct attr **attrs;
640
641                 new->object_func=object_func_lookup(func->type);
642                 if (! new->object_func)
643                         return;
644                 attrs=convert_to_attrs(new,attr_fixme);
645                 new->element_attr.type=attr_none;
646                 if (!new->parent || new->parent->element_attr.type == attr_none)
647                         parent_attr=NULL;
648                 else
649                         parent_attr=&new->parent->element_attr;
650                 new->element_attr.u.data = new->object_func->create(parent_attr, attrs);
651                 if (! new->element_attr.u.data)
652                         return;
653                 new->element_attr.type=attr_from_name(element_name);
654                 if (new->element_attr.type == attr_none)
655                         dbg(0,"failed to create object of type '%s'\n", element_name);
656                 if (new->element_attr.type == attr_tracking)
657                         new->element_attr.type=attr_trackingo;
658                 if (new->parent && new->parent->object_func && new->parent->object_func->add_attr)
659                         new->parent->object_func->add_attr(new->parent->element_attr.u.data, &new->element_attr);
660         }
661         return;
662 }
663
664
665 /* Called for close tags </foo> */
666 static void
667 end_element (GMarkupParseContext *context,
668                 const gchar         *element_name,
669                 gpointer             user_data,
670                 xmlerror             **error)
671 {
672         struct xmlstate *curr, **state = user_data;
673
674         if (!strcmp(element_name,"xml"))
675                 return;
676         dbg(2,"name='%s'\n", element_name);
677         curr=*state;
678         if (curr->object_func && curr->object_func->init)
679                 curr->object_func->init(curr->element_attr.u.data);
680         if (curr->object_func && curr->object_func->unref) 
681                 curr->object_func->unref(curr->element_attr.u.data);
682         *state=curr->parent;
683         g_free(curr);
684 }
685
686 static gboolean parse_file(struct xmldocument *document, xmlerror **error);
687
688 static void
689 xinclude(GMarkupParseContext *context, const gchar **attribute_names, const gchar **attribute_values, struct xmldocument *doc_old, xmlerror **error)
690 {
691         struct xmldocument doc_new;
692         struct file_wordexp *we;
693         int i,count;
694         const char *href=NULL;
695         char **we_files;
696
697         if (doc_old->level >= 16) {
698                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "xi:include recursion too deep");
699                 return;
700         }
701         memset(&doc_new, 0, sizeof(doc_new));
702         i=0;
703         while (attribute_names[i]) {
704                 if(!g_ascii_strcasecmp("href", attribute_names[i])) {
705                         if (!href)
706                                 href=attribute_values[i];
707                         else {
708                                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one href");
709                                 return;
710                         }
711                 } else if(!g_ascii_strcasecmp("xpointer", attribute_names[i])) {
712                         if (!doc_new.xpointer)
713                                 doc_new.xpointer=attribute_values[i];
714                         else {
715                                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one xpointer");
716                                 return;
717                         }
718                 } else {
719                         g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has invalid attributes");
720                         return;
721                 }
722                 i++;
723         }
724         if (!doc_new.xpointer && !href) {
725                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has neither href nor xpointer");
726                 return;
727         }
728         doc_new.level=doc_old->level+1;
729         doc_new.user_data=doc_old->user_data;
730         if (! href) {
731                 dbg(1,"no href, using '%s'\n", doc_old->href);
732                 doc_new.href=doc_old->href;
733                 if (file_exists(doc_new.href)) {
734                     parse_file(&doc_new, error);
735                 } else {
736                     dbg(0,"Unable to include %s\n",doc_new.href);
737                 }
738         } else {
739                 dbg(1,"expanding '%s'\n", href);
740                 we=file_wordexp_new(href);
741                 we_files=file_wordexp_get_array(we);
742                 count=file_wordexp_get_count(we);
743                 dbg(1,"%d results\n", count);
744                 if (file_exists(we_files[0])) {
745                         for (i = 0 ; i < count ; i++) {
746                                 dbg(1,"result[%d]='%s'\n", i, we_files[i]);
747                                 doc_new.href=we_files[i];
748                                 parse_file(&doc_new, error);
749                         }
750                 } else {
751                         dbg(0,"Unable to include %s\n",we_files[0]);
752                 }
753                 file_wordexp_destroy(we);
754
755         }
756
757 }
758 static int
759 strncmp_len(const char *s1, int s1len, const char *s2)
760 {
761         int ret;
762 #if 0
763         char c[s1len+1];
764         strncpy(c, s1, s1len);
765         c[s1len]='\0';
766         dbg(0,"'%s' vs '%s'\n", c, s2);
767 #endif
768
769         ret=strncmp(s1, s2, s1len);
770         if (ret)
771                 return ret;
772         return strlen(s2)-s1len;
773 }
774
775 static int
776 xpointer_value(const char *test, int len, struct xistate *elem, const char **out, int out_len)
777 {
778         int i,ret=0;
779         if (len <= 0 || out_len <= 0) {
780                 return 0;
781         }
782         if (!(strncmp_len(test,len,"name(.)"))) {
783                 out[0]=elem->element;
784                 return 1;
785         }
786         if (test[0] == '@') {
787                 i=0;
788                 while (elem->attribute_names[i] && out_len > 0) {
789                         if (!strncmp_len(test+1,len-1,elem->attribute_names[i])) {
790                                 out[ret++]=elem->attribute_values[i];
791                                 out_len--;
792                         }
793                         i++;
794                 }
795                 return ret;
796         }
797         return 0;
798 }
799
800 static int
801 xpointer_test(const char *test, int len, struct xistate *elem)
802 {
803         int eq,i,count,vlen,cond_req=1,cond=0;
804         char c;
805         const char *tmp[16];
806 #if 0
807         char test2[len+1];
808
809         strncpy(test2, test, len);
810         test2[len]='\0';
811         dbg(0,"%s\n", test2);
812 #endif
813         if (!len)
814                 return 0;
815         c=test[len-1];
816         if (c != '\'' && c != '"')
817                 return 0;
818         eq=strcspn(test, "=");
819         if (eq >= len || test[eq+1] != c)
820                 return 0;
821         vlen=eq;
822         if (eq > 0 && test[eq-1] == '!') {
823                 cond_req=0;
824                 vlen--;
825         }
826         count=xpointer_value(test,vlen,elem,tmp,16);
827         for (i = 0 ; i < count ; i++) {
828                 if (!strncmp_len(test+eq+2,len-eq-3, tmp[i]))
829                         cond=1;
830         }
831         if (cond == cond_req)
832                 return 1;
833         return 0;
834 }
835
836 static int
837 xpointer_element_match(const char *xpointer, int len, struct xistate *elem)
838 {
839         int start,tlen,tlen2;
840 #if 0
841         char test2[len+1];
842
843         strncpy(test2, xpointer, len);
844         test2[len]='\0';
845         dbg(0,"%s\n", test2);
846 #endif
847         start=strcspn(xpointer, "[");
848         if (start > len)
849                 start=len;
850         if (strncmp_len(xpointer, start, elem->element) && (start != 1 || xpointer[0] != '*'))
851                 return 0;
852         if (start == len)
853                 return 1;
854         if (xpointer[len-1] != ']')
855                 return 0;
856         tlen=len-start-2;
857         for (;;) {
858                 start++;
859                 tlen2=strcspn(xpointer+start,"]");
860                 if (start + tlen2 > len)
861                         return 1;
862                 if (!xpointer_test(xpointer+start, tlen2, elem))
863                         return 0;
864                 start+=tlen2+1;
865         }
866 }
867
868 static int
869 xpointer_xpointer_match(const char *xpointer, int len, struct xistate *first)
870 {
871         const char *c;
872         int s;
873         dbg(2,"%s\n", xpointer);
874         if (xpointer[0] != '/')
875                 return 0;
876         c=xpointer+1;
877         len--;
878         do {
879                 s=strcspn(c, "/");
880                 if (s > len)
881                         s=len;
882                 if (! xpointer_element_match(c, s, first))
883                         return 0;
884                 first=first->child;
885                 c+=s+1;
886                 len-=s+1;
887         } while (len > 0 && first);
888         if (len > 0)
889                 return 0;
890         return 1;
891 }
892
893 static int
894 xpointer_match(const char *xpointer, struct xistate *first)
895 {
896         char *prefix="xpointer(";
897         int len;
898         if (! xpointer)
899                 return 1;
900         len=strlen(xpointer);
901         if (strncmp(xpointer,prefix,strlen(prefix)))
902                 return 0;
903         if (xpointer[len-1] != ')')
904                 return 0;
905         return xpointer_xpointer_match(xpointer+strlen(prefix), len-strlen(prefix)-1, first);
906
907 }
908
909 static void
910 xi_start_element(GMarkupParseContext *context,
911                 const gchar         *element_name,
912                 const gchar        **attribute_names,
913                 const gchar        **attribute_values,
914                 gpointer             user_data,
915                 xmlerror             **error)
916 {
917         struct xmldocument *doc=user_data;
918         struct xistate *xistate;
919         int i,count=0;
920         while (attribute_names[count++*ATTR_DISTANCE]);
921         xistate=g_new0(struct xistate, 1);
922         xistate->element=element_name;
923         xistate->attribute_names=g_new0(const char *, count);
924         xistate->attribute_values=g_new0(const char *, count);
925         for (i = 0 ; i < count ; i++) {
926                 if (attribute_names[i*ATTR_DISTANCE] && attribute_values[i*ATTR_DISTANCE]) {
927                         xistate->attribute_names[i]=g_strdup(attribute_names[i*ATTR_DISTANCE]);
928                         xistate->attribute_values[i]=g_strdup(attribute_values[i*ATTR_DISTANCE]);
929                 }
930         }
931         xistate->parent=doc->last;
932
933         if (doc->last) {
934                 doc->last->child=xistate;
935         } else
936                 doc->first=xistate;
937         doc->last=xistate;
938         if (doc->active > 0 || xpointer_match(doc->xpointer, doc->first)) {
939                 if(!g_ascii_strcasecmp("xi:include", element_name)) {
940                         xinclude(context, xistate->attribute_names, xistate->attribute_values, doc, error);
941                         return;
942                 }
943                 start_element(context, element_name, xistate->attribute_names, xistate->attribute_values, doc->user_data, error);
944                 doc->active++;
945         }
946
947 }
948 /**
949  * * Reached closing tag of a config element
950  * *
951  * * @param context
952  * * @param element name
953  * * @param user_data ptr to xmldocument
954  * * @param error ptr to struct for error information
955  * * @returns nothing
956  * */
957
958 static void
959 xi_end_element (GMarkupParseContext *context,
960                 const gchar         *element_name,
961                 gpointer             user_data,
962                 xmlerror             **error)
963 {
964         struct xmldocument *doc=user_data;
965         struct xistate *xistate=doc->last;
966         int i=0;
967         doc->last=doc->last->parent;
968         if (! doc->last)
969                 doc->first=NULL;
970         else
971                 doc->last->child=NULL;
972         if (doc->active > 0) {
973                 if(!g_ascii_strcasecmp("xi:include", element_name)) {
974                         return;
975                 }
976                 end_element(context, element_name, doc->user_data, error);
977                 doc->active--;
978         }
979         while (xistate->attribute_names[i]) {
980                 g_free((char *)(xistate->attribute_names[i]));
981                 g_free((char *)(xistate->attribute_values[i]));
982                 i++;
983         }
984         g_free(xistate->attribute_names);
985         g_free(xistate->attribute_values);
986         g_free(xistate);
987 }
988
989 /* Called for character data */
990 /* text is not nul-terminated */
991 static void
992 xi_text (GMarkupParseContext *context,
993                 const gchar            *text,
994                 gsize                   text_len,
995                 gpointer                user_data,
996                 xmlerror               **error)
997 {
998         struct xmldocument *doc=user_data;
999         int i;
1000         if (doc->active) {
1001                 for (i = 0 ; i < text_len ; i++) {
1002                         if (!isspace(text[i])) {
1003                                 struct xmldocument *doc=user_data;
1004                                 struct xmlstate *curr, **state = doc->user_data;
1005                                 struct attr attr;
1006                                 char *text_dup = malloc(text_len+1);
1007
1008                                 curr=*state;
1009                                 strncpy(text_dup, text, text_len);
1010                                 text_dup[text_len]='\0';
1011                                 attr.type=attr_xml_text;
1012                                 attr.u.str=text_dup;
1013                                 if (curr->object_func && curr->object_func->add_attr && curr->element_attr.u.data)
1014                                         curr->object_func->add_attr(curr->element_attr.u.data, &attr);
1015                                 free(text_dup);
1016                                 return;
1017                         }
1018                 }
1019         }
1020 }
1021
1022 #ifndef HAVE_GLIB
1023 static void
1024 parse_node_text(ezxml_t node, void *data, void (*start)(void *, const char *, const char **, const char **, void *, void *),
1025                                           void (*end)(void *, const char *, void *, void *),
1026                                           void (*text)(void *, const char *, int, void *, void *))
1027 {
1028         while (node) {
1029                 if (start)
1030                         start(NULL, node->name, (const char **)node->attr, (const char **)(node->attr+1), data, NULL);
1031                 if (text && node->txt)
1032                         text(NULL, node->txt, strlen(node->txt), data, NULL);
1033                 if (node->child)
1034                         parse_node_text(node->child, data, start, end, text);
1035                 if (end)
1036                         end(NULL, node->name, data, NULL);
1037                 node=node->ordered;
1038         }
1039 }
1040 #endif
1041
1042 void
1043 xml_parse_text(const char *document, void *data, void (*start)(void *, const char *, const char **, const char **, void *, void *),
1044                                            void (*end)(void *, const char *, void *, void *),
1045                                            void (*text)(void *, const char *, int, void *, void *))
1046 {
1047 #ifdef HAVE_GLIB
1048         GMarkupParser parser = { start, end, text, NULL, NULL};
1049         GMarkupParseContext *context;
1050         gboolean result;
1051
1052         context = g_markup_parse_context_new (&parser, 0, data, NULL);
1053         result = g_markup_parse_context_parse (context, document, strlen(document), NULL);
1054         g_markup_parse_context_free (context);
1055 #else
1056         char *str=g_strdup(document);
1057         ezxml_t root = ezxml_parse_str(str, strlen(str));
1058         if (!root)
1059                 return;
1060         parse_node_text(root, data, start, end, text);
1061         ezxml_free(root);
1062         g_free(str);
1063 #endif
1064 }
1065
1066
1067 #ifdef HAVE_GLIB
1068
1069 static const GMarkupParser parser = {
1070         xi_start_element,
1071         xi_end_element,
1072         xi_text,
1073         NULL,
1074         NULL
1075 };
1076 /**
1077  * * Parse the contents of the configuration file
1078  * *
1079  * * @param document struct holding info  about the config file
1080  * * @param error info on any errors detected
1081  * * @returns boolean TRUE or FALSE
1082  * */
1083
1084 static gboolean
1085 parse_file(struct xmldocument *document, xmlerror **error)
1086 {
1087         GMarkupParseContext *context;
1088         gchar *contents, *message;
1089         gsize len;
1090         gint line, chr;
1091         gboolean result;
1092         char *xmldir,*newxmldir,*xmlfile,*newxmlfile,*sep;
1093
1094         dbg(1,"enter filename='%s'\n", document->href);
1095 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 12
1096 #define G_MARKUP_TREAT_CDATA_AS_TEXT 0
1097 #endif
1098         context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, document, NULL);
1099
1100         if (!g_file_get_contents (document->href, &contents, &len, error)) {
1101                 g_markup_parse_context_free (context);
1102                 return FALSE;
1103         }
1104         xmldir=getenv("XMLDIR");
1105         xmlfile=getenv("XMLFILE");
1106         newxmlfile=g_strdup(document->href);
1107         newxmldir=g_strdup(document->href);
1108         if ((sep=strrchr(newxmldir,'/'))) 
1109                 *sep='\0';
1110         else {
1111                 g_free(newxmldir);
1112                 newxmldir=g_strdup(".");
1113         }
1114         setenv("XMLDIR",newxmldir,1);
1115         setenv("XMLFILE",newxmlfile,1);
1116         document->active=document->xpointer ? 0:1;
1117         document->first=NULL;
1118         document->last=NULL;
1119         result = g_markup_parse_context_parse (context, contents, len, error);
1120         if (!result && error && *error) {
1121                 g_markup_parse_context_get_position(context, &line, &chr);
1122                 message=g_strdup_printf("%s at line %d, char %d\n", (*error)->message, line, chr);
1123                 g_free((*error)->message);
1124                 (*error)->message=message;
1125         }
1126         g_markup_parse_context_free (context);
1127         g_free (contents);
1128         if (xmldir)
1129                 setenv("XMLDIR",xmldir,1);      
1130         else
1131                 unsetenv("XMLDIR");
1132         if (xmlfile)
1133                 setenv("XMLFILE",xmlfile,1);
1134         else
1135                 unsetenv("XMLFILE");
1136         g_free(newxmldir);
1137         g_free(newxmlfile);
1138         dbg(1,"return %d\n", result);
1139
1140         return result;
1141 }
1142 #else
1143 static void
1144 parse_node(struct xmldocument *document, ezxml_t node)
1145 {
1146         while (node) {
1147                 xi_start_element(NULL,node->name, node->attr, node->attr+1, document, NULL);
1148                 if (node->txt)
1149                         xi_text(NULL,node->txt,strlen(node->txt),document,NULL);
1150                 if (node->child)
1151                         parse_node(document, node->child);
1152                 xi_end_element (NULL,node->name,document,NULL);
1153                 node=node->ordered;
1154         }
1155 }
1156
1157 static gboolean
1158 parse_file(struct xmldocument *document, xmlerror **error)
1159 {
1160         FILE *f;
1161         ezxml_t root;
1162
1163         f=fopen(document->href,"rb");
1164         if (!f)
1165                 return FALSE;
1166         root = ezxml_parse_fp(f);
1167         fclose(f);
1168         if (!root)
1169                 return FALSE;
1170         document->active=document->xpointer ? 0:1;
1171         document->first=NULL;
1172         document->last=NULL;
1173
1174         parse_node(document, root);
1175
1176         return TRUE;
1177 }
1178 #endif
1179
1180 /**
1181  * * Load and parse the master config file
1182  * *
1183  * * @param filename FQFN of the file
1184  * * @param error ptr to error details, if any
1185  * * @returns boolean TRUE or FALSE (if error detected)
1186  * */
1187
1188 gboolean config_load(const char *filename, xmlerror **error)
1189 {
1190         struct xmldocument document;
1191         struct xmlstate *curr=NULL;
1192         gboolean result;
1193
1194         attr_create_hash();
1195         item_create_hash();
1196         initStatic();
1197
1198         dbg(1,"enter filename='%s'\n", filename);
1199         memset(&document, 0, sizeof(document));
1200         document.href=filename;
1201         document.user_data=&curr;
1202         result=parse_file(&document, error);
1203         if (result && curr) {
1204                 g_set_error(error,G_MARKUP_ERROR,G_MARKUP_ERROR_PARSE, "element '%s' not closed", curr->element);
1205                 result=FALSE;
1206         }
1207         attr_destroy_hash();
1208         item_destroy_hash();
1209         dbg(1,"return %d\n", result);
1210         return result;
1211 }
1212