Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move some XML specific code from surf_routing.c to surfxml_parse.c
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15                                 "Logging specific to the SURF parsing module");
16 #undef CLEANUP
17 int ETag_surfxml_include_state(void);
18
19 #include "simgrid_dtd.c"
20
21 /*
22  * Helping functions
23  */
24 void surf_parse_error(const char *msg) {
25   xbt_die("Parse error on line %d: %s\n", surf_parse_lineno, msg);
26 }
27
28 double surf_parse_get_double(const char *string) {
29   double res;
30   int ret = sscanf(string, "%lg", &res);
31   if (ret != 1)
32     surf_parse_error(bprintf("%s is not a double", string));
33   return res;
34 }
35
36 int surf_parse_get_int(const char *string) {
37   int res;
38   int ret = sscanf(string, "%d", &res);
39   if (ret != 1)
40     surf_parse_error(bprintf("%s is not an integer", string));
41   return res;
42 }
43
44
45 /*
46  * All the callback lists that can be overiden anywhere.
47  * (this list should probably be reduced to the bare minimum to allow the models to work)
48  */
49
50 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
51 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_link_ctn_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_peer_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_include_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_include_cb_list = NULL;
77
78 /* The default current property receiver. Setup in the corresponding opening callbacks. */
79 xbt_dict_t current_property_set = NULL;
80 /* dictionary of random generator data */
81 xbt_dict_t random_data_list = NULL;
82
83 /* Call all the callbacks of a specific SAX event */
84 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
85
86 YY_BUFFER_STATE surf_input_buffer;
87 FILE *surf_file_to_parse = NULL;
88
89 static void init_randomness(void);
90 static void add_randomness(void);
91
92 /*
93  * Stuff relative to the <include> tag
94  */
95
96 static xbt_dynar_t surf_input_buffer_stack = NULL;
97 static xbt_dynar_t surf_file_to_parse_stack = NULL;
98
99 void STag_surfxml_include(void)
100 {
101   XBT_INFO("STag_surfxml_include '%s'",A_surfxml_include_file);
102   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old filename
103
104   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new filename
105   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
106               A_surfxml_include_file);
107   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
108   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
109   surf_parse_push_buffer_state(surf_input_buffer);
110   fflush(NULL);
111 }
112
113 void ETag_surfxml_include(void) {
114 /* Nothing to do when done with reading the include tag.
115  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
116 }
117
118 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
119  *
120  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
121  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
122  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
123  * error message in that case.
124  *
125  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
126  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
127  * on a line.
128  */
129 int ETag_surfxml_include_state(void)
130 {
131   fflush(NULL);
132   XBT_INFO("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
133   if(xbt_dynar_length(surf_input_buffer_stack)!= 0)
134           return 1;
135   fclose(surf_file_to_parse);
136   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
137   surf_parse_pop_buffer_state();
138   xbt_dynar_pop(surf_input_buffer_stack,surf_input_buffer);
139   return 0;
140 }
141
142
143 void surf_parse_init_callbacks(void)
144 {
145           sg_platf_init(); // FIXME: move to a proper place?
146
147           STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
148           ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
149           STag_surfxml_link_ctn_cb_list =
150               xbt_dynar_new(sizeof(void_f_void_t), NULL);
151           ETag_surfxml_link_ctn_cb_list =
152               xbt_dynar_new(sizeof(void_f_void_t), NULL);
153           STag_surfxml_process_cb_list =
154               xbt_dynar_new(sizeof(void_f_void_t), NULL);
155           ETag_surfxml_process_cb_list =
156               xbt_dynar_new(sizeof(void_f_void_t), NULL);
157           STag_surfxml_argument_cb_list =
158               xbt_dynar_new(sizeof(void_f_void_t), NULL);
159           ETag_surfxml_argument_cb_list =
160               xbt_dynar_new(sizeof(void_f_void_t), NULL);
161           STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
162           ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
163           STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
164           ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
165           STag_surfxml_trace_connect_cb_list =
166               xbt_dynar_new(sizeof(void_f_void_t), NULL);
167           ETag_surfxml_trace_connect_cb_list =
168               xbt_dynar_new(sizeof(void_f_void_t), NULL);
169           STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
170           ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
171           STag_surfxml_ASroute_cb_list =
172               xbt_dynar_new(sizeof(void_f_void_t), NULL);
173           ETag_surfxml_ASroute_cb_list =
174               xbt_dynar_new(sizeof(void_f_void_t), NULL);
175           STag_surfxml_bypassRoute_cb_list =
176               xbt_dynar_new(sizeof(void_f_void_t), NULL);
177           ETag_surfxml_bypassRoute_cb_list =
178               xbt_dynar_new(sizeof(void_f_void_t), NULL);
179           STag_surfxml_cluster_cb_list =
180               xbt_dynar_new(sizeof(void_f_void_t), NULL);
181           ETag_surfxml_cluster_cb_list =
182               xbt_dynar_new(sizeof(void_f_void_t), NULL);
183           STag_surfxml_peer_cb_list =
184               xbt_dynar_new(sizeof(void_f_void_t), NULL);
185           ETag_surfxml_peer_cb_list =
186               xbt_dynar_new(sizeof(void_f_void_t), NULL);
187           STag_surfxml_include_cb_list =
188                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
189           ETag_surfxml_include_cb_list =
190                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
191 }
192
193 void surf_parse_reset_callbacks(void)
194 {
195         surf_parse_free_callbacks();
196         surf_parse_init_callbacks();
197 }
198
199 void surf_parse_free_callbacks(void)
200 {
201   sg_platf_exit(); // FIXME: better place?
202
203   xbt_dynar_free(&STag_surfxml_route_cb_list);
204   xbt_dynar_free(&ETag_surfxml_route_cb_list);
205   xbt_dynar_free(&STag_surfxml_link_ctn_cb_list);
206   xbt_dynar_free(&ETag_surfxml_link_ctn_cb_list);
207   xbt_dynar_free(&STag_surfxml_process_cb_list);
208   xbt_dynar_free(&ETag_surfxml_process_cb_list);
209   xbt_dynar_free(&STag_surfxml_argument_cb_list);
210   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
211   xbt_dynar_free(&STag_surfxml_prop_cb_list);
212   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
213   xbt_dynar_free(&STag_surfxml_trace_cb_list);
214   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
215   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
216   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
217   xbt_dynar_free(&STag_surfxml_random_cb_list);
218   xbt_dynar_free(&ETag_surfxml_random_cb_list);
219   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
220   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
221   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
222   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
223   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
224   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
225   xbt_dynar_free(&STag_surfxml_peer_cb_list);
226   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
227   xbt_dynar_free(&STag_surfxml_include_cb_list);
228   xbt_dynar_free(&ETag_surfxml_include_cb_list);
229 }
230
231 /* Stag and Etag parse functions */
232 void ETag_surfxml_router(void)  { /* ignored -- do not add content here */ }
233
234 void STag_surfxml_platform(void) {
235   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
236
237   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
238       "You're using an ancient XML file.\n"
239       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
240       "instead of MBytes, MFlops and seconds.\n"
241
242       "Use simgrid_update_xml to update your file automatically. "
243       "This program is installed automatically with SimGrid, or "
244       "available in the tools/ directory of the source archive.\n"
245
246       "Please check also out the SURF section of the ChangeLog for "
247       "the 3.1 version for more information. \n"
248
249       "Last, do not forget to also update your values for "
250       "the calls to MSG_task_create (if any).");
251   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
252       "You're using an old XML file.\n"
253       "Use simgrid_update_xml to update your file automatically. "
254       "This program is installed automatically with SimGrid, or "
255       "available in the tools/ directory of the source archive.");
256
257   sg_platf_begin();
258 }
259 void ETag_surfxml_platform(void){
260   sg_platf_end();
261 }
262
263 void STag_surfxml_host(void){
264   s_sg_platf_host_cbarg_t host;
265   memset(&host,0,sizeof(host));
266
267   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
268   host.properties = current_property_set = xbt_dict_new();
269
270         host.id = A_surfxml_host_id;
271         host.power_peak = get_cpu_power(A_surfxml_host_power);
272         host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
273         host.core_amount = surf_parse_get_int(A_surfxml_host_core);
274         host.power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
275         host.state_trace = tmgr_trace_new(A_surfxml_host_state_file);
276         xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
277                           (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
278         if (A_surfxml_host_state == A_surfxml_host_state_ON)
279                 host.initial_state = SURF_RESOURCE_ON;
280         if (A_surfxml_host_state == A_surfxml_host_state_OFF)
281                 host.initial_state = SURF_RESOURCE_OFF;
282         host.coord = A_surfxml_host_coordinates;
283
284         sg_platf_new_host(&host);
285 }
286 void ETag_surfxml_host(void)    {
287   current_property_set = NULL;
288 }
289
290 void STag_surfxml_router(void){
291   s_sg_platf_router_cbarg_t router;
292   memset(&router, 0, sizeof(router));
293
294         router.id = A_surfxml_router_id;
295         router.coord = A_surfxml_router_coordinates;
296
297         sg_platf_new_router(&router);
298 }
299
300 void STag_surfxml_cluster(void){
301         struct_cluster = xbt_new0(s_surf_parsing_cluster_arg_t, 1);
302         struct_cluster->id = A_surfxml_cluster_id;
303         struct_cluster->prefix = A_surfxml_cluster_prefix;
304         struct_cluster->suffix = A_surfxml_cluster_suffix;
305         struct_cluster->radical = A_surfxml_cluster_radical;
306         struct_cluster->power= surf_parse_get_double(A_surfxml_cluster_power);
307         struct_cluster->core_amount = surf_parse_get_int(A_surfxml_cluster_core);
308         struct_cluster->bw =   surf_parse_get_double(A_surfxml_cluster_bw);
309         struct_cluster->lat =  surf_parse_get_double(A_surfxml_cluster_lat);
310         if(strcmp(A_surfxml_cluster_bb_bw,""))
311           struct_cluster->bb_bw = surf_parse_get_double(A_surfxml_cluster_bb_bw);
312         if(strcmp(A_surfxml_cluster_bb_lat,""))
313           struct_cluster->bb_lat = surf_parse_get_double(A_surfxml_cluster_bb_lat);
314         struct_cluster->router_id = A_surfxml_cluster_router_id;
315
316   switch (AX_surfxml_cluster_sharing_policy) {
317   case A_surfxml_cluster_sharing_policy_SHARED:
318     struct_cluster->sharing_policy = SURF_LINK_SHARED;
319     break;
320   case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
321     struct_cluster->sharing_policy = SURF_LINK_FULLDUPLEX;
322     break;
323   case A_surfxml_cluster_sharing_policy_FATPIPE:
324     struct_cluster->sharing_policy = SURF_LINK_FATPIPE;
325     break;
326   default:
327     surf_parse_error(bprintf
328                      ("Invalid cluster sharing policy for cluster %s",
329                       struct_cluster->id));
330     break;
331   }
332   switch (AX_surfxml_cluster_bb_sharing_policy) {
333   case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
334     struct_cluster->bb_sharing_policy = SURF_LINK_FATPIPE;
335     break;
336   case A_surfxml_cluster_bb_sharing_policy_SHARED:
337     struct_cluster->bb_sharing_policy = SURF_LINK_SHARED;
338     break;
339   default:
340     surf_parse_error(bprintf
341                      ("Invalid bb sharing policy in cluster %s",
342                       struct_cluster->id));
343     break;
344   }
345
346         struct_cluster->availability_trace = A_surfxml_cluster_availability_file;
347         struct_cluster->state_trace = A_surfxml_cluster_state_file;
348
349         surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
350 }
351 void ETag_surfxml_cluster(void){
352         surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
353         xbt_free(struct_cluster);
354 }
355
356 void STag_surfxml_peer(void){
357   s_sg_platf_peer_cbarg_t peer;
358   memset(&peer,0,sizeof(peer));
359         peer.id = A_surfxml_peer_id;
360         peer.power = surf_parse_get_double(A_surfxml_peer_power);
361         peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw_in);
362         peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw_out);
363         peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
364         peer.coord = A_surfxml_peer_coordinates;
365         peer.availability_trace = tmgr_trace_new(A_surfxml_peer_availability_file);
366         peer.state_trace = tmgr_trace_new(A_surfxml_peer_state_file);
367
368         surfxml_call_cb_functions(STag_surfxml_peer_cb_list);
369         sg_platf_new_peer(&peer);
370 }
371 void ETag_surfxml_peer(void){
372   /* nothing to do here */
373 }
374 void STag_surfxml_link(void){
375   s_sg_platf_link_cbarg_t link;
376   memset(&link,0,sizeof(link));
377
378   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
379   link.properties = current_property_set = xbt_dict_new();
380
381         link.id = A_surfxml_link_id;
382         link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
383         link.bandwidth_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
384         link.latency = surf_parse_get_double(A_surfxml_link_latency);
385         link.latency_trace = tmgr_trace_new(A_surfxml_link_latency_file);
386
387         switch (A_surfxml_link_state) {
388         case A_surfxml_link_state_ON:
389                 link.state = SURF_RESOURCE_ON;
390                 break;
391         case A_surfxml_link_state_OFF:
392                 link.state = SURF_RESOURCE_OFF;
393                 break;
394         default:
395           surf_parse_error(bprintf("invalid state for link %s",link.id));
396         }
397         link.state_trace = tmgr_trace_new(A_surfxml_link_state_file);
398
399         switch (A_surfxml_link_sharing_policy) {
400         case A_surfxml_link_sharing_policy_SHARED:
401                 link.policy = SURF_LINK_SHARED;
402                 break;
403         case A_surfxml_link_sharing_policy_FATPIPE:
404                  link.policy = SURF_LINK_FATPIPE;
405                  break;
406         case A_surfxml_link_sharing_policy_FULLDUPLEX:
407                  link.policy = SURF_LINK_FULLDUPLEX;
408                  break;
409         default:
410           surf_parse_error(bprintf("Invalid sharing policy in link %s",link.id));
411         }
412
413         sg_platf_new_link(&link);
414 }
415 void ETag_surfxml_link(void){
416   current_property_set = NULL;
417 }
418
419 void STag_surfxml_route(void){
420         surfxml_call_cb_functions(STag_surfxml_route_cb_list);
421 }
422 void STag_surfxml_link_ctn(void){
423         surfxml_call_cb_functions(STag_surfxml_link_ctn_cb_list);
424 }
425 void STag_surfxml_process(void){
426         surfxml_call_cb_functions(STag_surfxml_process_cb_list);
427 }
428 void STag_surfxml_argument(void){
429         surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
430 }
431 void STag_surfxml_prop(void){
432         surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
433 }
434 void STag_surfxml_trace(void){
435         surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
436 }
437 void STag_surfxml_trace_connect(void){
438         surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
439 }
440 void STag_surfxml_AS(void){
441   sg_platf_new_AS_begin(A_surfxml_AS_id,A_surfxml_AS_routing);
442 }
443 void ETag_surfxml_AS(void){
444   sg_platf_new_AS_end();
445 }
446 void STag_surfxml_ASroute(void){
447         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
448 }
449 void STag_surfxml_bypassRoute(void){
450         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
451 }
452 void STag_surfxml_config(void){
453   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
454   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
455   current_property_set = xbt_dict_new();
456
457 }
458 void ETag_surfxml_config(void){
459   xbt_dict_cursor_t cursor = NULL;
460   char *key;
461   char *elem;
462   char *cfg;
463   xbt_dict_foreach(current_property_set, cursor, key, elem) {
464     cfg = bprintf("%s:%s",key,elem);
465     if(xbt_cfg_is_default_value(_surf_cfg_set, key))
466       xbt_cfg_set_parse(_surf_cfg_set, cfg);
467     else
468       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
469     free(cfg);
470   }
471   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
472   xbt_dict_free(&current_property_set);
473 }
474 void STag_surfxml_random(void){
475         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
476 }
477
478 #define parse_method(type,name) \
479 void type##Tag_surfxml_##name(void) \
480 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
481 parse_method(E, route);
482 parse_method(E, link_ctn);
483 parse_method(E, process);
484 parse_method(E, argument);
485 parse_method(E, prop);
486 parse_method(E, trace);
487 parse_method(E, trace_connect);
488 parse_method(E, random);
489 parse_method(E, ASroute);
490 parse_method(E, bypassRoute);
491
492 /* Open and Close parse file */
493
494 void surf_parse_open(const char *file)
495 {
496   static int warned = 0;        /* warn only once */
497   if (!file) {
498     if (!warned) {
499       XBT_WARN
500           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
501               "If it is not what you want, go fix your code.");
502       warned = 1;
503     }
504     return;
505   }
506
507   if (!surf_input_buffer_stack)
508     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
509   if (!surf_file_to_parse_stack)
510     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
511
512   surf_file_to_parse = surf_fopen(file, "r");
513   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
514   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
515   surf_parse__switch_to_buffer(surf_input_buffer);
516   surf_parse_lineno = 1;
517 }
518
519 void surf_parse_close(void)
520 {
521   xbt_dynar_free(&surf_input_buffer_stack);
522   xbt_dynar_free(&surf_file_to_parse_stack);
523
524   if (surf_file_to_parse) {
525     surf_parse__delete_buffer(surf_input_buffer);
526     fclose(surf_file_to_parse);
527     surf_file_to_parse = NULL; //Must be reset for Bypass
528   }
529 }
530
531 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
532
533 static int _surf_parse(void) {
534   return surf_parse_lex();
535 }
536 int_f_void_t surf_parse = _surf_parse;
537
538
539 /* Aux parse functions */
540
541 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
542 {
543   xbt_dynar_push(cb_list, &function);
544 }
545
546 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
547 {
548   xbt_ex_t e;
549   unsigned int it=0;
550   void_f_void_t null_f=NULL;
551
552   TRY {
553     it = xbt_dynar_search(cb_list,&function);
554   }
555   CATCH(e) {
556     if (e.category == not_found_error) {
557       xbt_ex_free(e);
558       xbt_die("Trying to remove a callback that is not here! This should not happen");
559     }
560     RETHROW;
561   }
562
563   xbt_dynar_replace(cb_list, it,&null_f);
564 }
565
566 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
567 {
568   unsigned int iterator;
569   void_f_void_t fun;
570   xbt_dynar_foreach(cb_list, iterator, fun) {
571     if (fun) fun();
572   }
573 }
574
575
576 /* Prop tag functions */
577
578 void parse_properties(const char* prop_id, const char* prop_value)
579 {
580         if (!current_property_set)
581             current_property_set = xbt_dict_new();      // Maybe, it should raise an error
582         if(!strcmp(prop_id,"coordinates")){
583                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
584                   {
585                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
586                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
587                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
588                   }
589                 if(strcmp(A_surfxml_prop_value,"yes"))
590                           xbt_die("Setting XML prop coordinates must be \"yes\"");
591           }
592         else{
593                   xbt_dict_set(current_property_set, prop_id, xbt_strdup(prop_value), free);
594          }
595 }
596
597 /**
598  * With XML parser
599  */
600 void parse_properties_XML(void)
601 {
602   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
603 }
604
605
606 /* Random tag functions */
607
608 double get_cpu_power(const char *power)
609 {
610   double power_scale = 0.0;
611   const char *p, *q;
612   char *generator;
613   random_data_t random = NULL;
614   /* randomness is inserted like this: power="$rand(my_random)" */
615   if (((p = strstr(power, "$rand(")) != NULL)
616       && ((q = strstr(power, ")")) != NULL)) {
617     if (p < q) {
618       generator = xbt_malloc(q - (p + 6) + 1);
619       memcpy(generator, p + 6, q - (p + 6));
620       generator[q - (p + 6)] = '\0';
621       random = xbt_dict_get_or_null(random_data_list, generator);
622       xbt_assert(random, "Random generator %s undefined", generator);
623       power_scale = random_generate(random);
624     }
625   } else {
626     power_scale = surf_parse_get_double(power);
627   }
628   return power_scale;
629 }
630
631 double random_min, random_max, random_mean, random_std_deviation,
632     random_generator;
633 char *random_id;
634
635 static void init_randomness(void)
636 {
637   random_id = A_surfxml_random_id;
638   random_min = surf_parse_get_double(A_surfxml_random_min);
639   random_max = surf_parse_get_double(A_surfxml_random_max);
640   random_mean = surf_parse_get_double(A_surfxml_random_mean);
641   random_std_deviation = surf_parse_get_double(A_surfxml_random_std_deviation);
642   random_generator = A_surfxml_random_generator;
643 }
644
645 static void add_randomness(void)
646 {
647   /* If needed aditional properties can be added by using the prop tag */
648   random_data_t random =
649       random_new(random_generator, 0, random_min, random_max, random_mean,
650                  random_std_deviation);
651   xbt_dict_set(random_data_list, random_id, (void *) random,
652                &xbt_free_ref);
653 }
654