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