Logo AND Algorithmique Numérique Distribuée

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