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