Logo AND Algorithmique Numérique Distribuée

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