Logo AND Algorithmique Numérique Distribuée

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