Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
CleanUp unused xbt_dynar_t from callback
[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_process_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_trace_connect_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 storage
94  */
95 void STag_surfxml_storage(void)
96 {
97   XBT_DEBUG("STag_surfxml_storage");
98 }
99 void ETag_surfxml_storage(void)
100 {
101   s_sg_platf_storage_cbarg_t storage;
102   memset(&storage,0,sizeof(storage));
103
104   storage.id = A_surfxml_storage_id;
105   storage.type_id = A_surfxml_storage_typeId;
106   storage.content = A_surfxml_storage_content;
107   sg_platf_new_storage(&storage);
108 }
109 void STag_surfxml_storage_type(void)
110 {
111   XBT_DEBUG("STag_surfxml_storage_type");
112   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
113 }
114 void ETag_surfxml_storage_type(void)
115 {
116   s_sg_platf_storage_type_cbarg_t storage_type;
117   memset(&storage_type,0,sizeof(storage_type));
118
119   storage_type.content = A_surfxml_storage_type_content;
120   storage_type.id = A_surfxml_storage_type_id;
121   storage_type.model = A_surfxml_storage_type_model;
122   storage_type.properties = current_property_set;
123   storage_type.size = surf_parse_get_int(A_surfxml_storage_type_size);
124   sg_platf_new_storage_type(&storage_type);
125   current_property_set = NULL;
126 }
127 void STag_surfxml_mstorage(void)
128 {
129   XBT_DEBUG("STag_surfxml_mstorage");
130 }
131 void ETag_surfxml_mstorage(void)
132 {
133   s_sg_platf_mstorage_cbarg_t mstorage;
134   memset(&mstorage,0,sizeof(mstorage));
135
136   mstorage.name = A_surfxml_mstorage_name;
137   mstorage.type_id = A_surfxml_mstorage_typeId;
138   sg_platf_new_mstorage(&mstorage);
139 }
140 void STag_surfxml_mount(void)
141 {
142   XBT_DEBUG("STag_surfxml_mount");
143 }
144 void ETag_surfxml_mount(void)
145 {
146   s_sg_platf_mount_cbarg_t mount;
147   memset(&mount,0,sizeof(mount));
148
149   mount.name = A_surfxml_mount_name;
150   mount.id = A_surfxml_mount_id;
151   sg_platf_new_mount(&mount);
152 }
153
154 /*
155  * Stuff relative to the <include> tag
156  */
157 static xbt_dynar_t surf_input_buffer_stack = NULL;
158 static xbt_dynar_t surf_file_to_parse_stack = NULL;
159 static xbt_dynar_t surf_parsed_filename_stack = NULL;
160
161 void STag_surfxml_include(void)
162 {
163   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
164   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
165   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
166
167   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
168
169   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
170   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
171               A_surfxml_include_file);
172   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
173   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
174   surf_parse_push_buffer_state(surf_input_buffer);
175
176   fflush(NULL);
177 }
178
179 void ETag_surfxml_include(void) {
180 /* Nothing to do when done with reading the include tag.
181  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
182 }
183
184 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
185  *
186  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
187  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
188  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
189  * error message in that case.
190  *
191  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
192  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
193  * on a line. One day maybe, if the maya allow it.
194  */
195 int ETag_surfxml_include_state(void)
196 {
197   fflush(NULL);
198   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
199
200   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
201     return 0;
202
203   // Yeah, we were in an <include> Restore state and proceed.
204   fclose(surf_file_to_parse);
205   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
206   surf_parse_pop_buffer_state();
207   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
208
209   // Restore the filename for error messages
210   free(surf_parsed_filename);
211   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
212
213   return 1;
214 }
215
216
217 void surf_parse_init_callbacks(void)
218 {
219     sg_platf_init(); // FIXME: move to a proper place?
220     STag_surfxml_process_cb_list =
221         xbt_dynar_new(sizeof(void_f_void_t), NULL);
222     ETag_surfxml_process_cb_list =
223         xbt_dynar_new(sizeof(void_f_void_t), NULL);
224     STag_surfxml_argument_cb_list =
225         xbt_dynar_new(sizeof(void_f_void_t), NULL);
226     ETag_surfxml_argument_cb_list =
227         xbt_dynar_new(sizeof(void_f_void_t), NULL);
228     STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
229     ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
230     STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
231     ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
232     STag_surfxml_trace_connect_cb_list =
233         xbt_dynar_new(sizeof(void_f_void_t), NULL);
234     ETag_surfxml_trace_connect_cb_list =
235         xbt_dynar_new(sizeof(void_f_void_t), NULL);
236 }
237
238 void surf_parse_reset_callbacks(void)
239 {
240   surf_parse_free_callbacks();
241   surf_parse_init_callbacks();
242 }
243
244 void surf_parse_free_callbacks(void)
245 {
246   sg_platf_exit(); // FIXME: better place?
247
248   xbt_dynar_free(&STag_surfxml_process_cb_list);
249   xbt_dynar_free(&ETag_surfxml_process_cb_list);
250   xbt_dynar_free(&STag_surfxml_argument_cb_list);
251   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
252   xbt_dynar_free(&STag_surfxml_prop_cb_list);
253   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
254   xbt_dynar_free(&STag_surfxml_trace_cb_list);
255   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
256   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
257   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
258 }
259
260 /* Stag and Etag parse functions */
261 void ETag_surfxml_router(void)  { /* ignored -- do not add content here */ }
262
263 void STag_surfxml_platform(void) {
264   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
265
266   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
267       "You're using an ancient XML file.\n"
268       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
269       "instead of MBytes, MFlops and seconds.\n"
270
271       "Use simgrid_update_xml to update your file automatically. "
272       "This program is installed automatically with SimGrid, or "
273       "available in the tools/ directory of the source archive.\n"
274
275       "Please check also out the SURF section of the ChangeLog for "
276       "the 3.1 version for more information. \n"
277
278       "Last, do not forget to also update your values for "
279       "the calls to MSG_task_create (if any).");
280   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
281       "You're using an old XML file.\n"
282       "Use simgrid_update_xml to update your file automatically. "
283       "This program is installed automatically with SimGrid, or "
284       "available in the tools/ directory of the source archive.");
285
286   sg_platf_begin();
287 }
288 void ETag_surfxml_platform(void){
289   sg_platf_end();
290 }
291
292 void STag_surfxml_host(void){
293   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
294 }
295
296 void ETag_surfxml_host(void)    {
297   s_sg_platf_host_cbarg_t host;
298   memset(&host,0,sizeof(host));
299
300   host.properties = current_property_set;
301
302   host.id = A_surfxml_host_id;
303   host.power_peak = get_cpu_power(A_surfxml_host_power);
304   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
305   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
306   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability_file);
307   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state_file);
308   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
309         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
310   if (A_surfxml_host_state == A_surfxml_host_state_ON)
311     host.initial_state = SURF_RESOURCE_ON;
312   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
313     host.initial_state = SURF_RESOURCE_OFF;
314   host.coord = A_surfxml_host_coordinates;
315
316   sg_platf_new_host(&host);
317   current_property_set = NULL;
318 }
319
320 void STag_surfxml_host_link(void){
321   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host_link_id);
322   s_sg_platf_host_link_cbarg_t host_link;
323   memset(&host_link,0,sizeof(host_link));
324
325   host_link.id = A_surfxml_host_link_id;
326   host_link.link_up = A_surfxml_host_link_up;
327   host_link.link_down = A_surfxml_host_link_down;
328   sg_platf_new_host_link(&host_link);
329 }
330
331 void ETag_surfxml_host_link(void){
332   XBT_DEBUG("End create a Host_link for %s",A_surfxml_host_link_id);
333 }
334
335 void STag_surfxml_router(void){
336   s_sg_platf_router_cbarg_t router;
337   memset(&router, 0, sizeof(router));
338
339   router.id = A_surfxml_router_id;
340   router.coord = A_surfxml_router_coordinates;
341
342   sg_platf_new_router(&router);
343 }
344
345 void STag_surfxml_cluster(void){
346   s_sg_platf_cluster_cbarg_t cluster;
347   memset(&cluster,0,sizeof(cluster));
348   cluster.id = A_surfxml_cluster_id;
349   cluster.prefix = A_surfxml_cluster_prefix;
350   cluster.suffix = A_surfxml_cluster_suffix;
351   cluster.radical = A_surfxml_cluster_radical;
352   cluster.power= surf_parse_get_double(A_surfxml_cluster_power);
353   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
354   cluster.bw =   surf_parse_get_double(A_surfxml_cluster_bw);
355   cluster.lat =  surf_parse_get_double(A_surfxml_cluster_lat);
356   if(strcmp(A_surfxml_cluster_bb_bw,""))
357     cluster.bb_bw = surf_parse_get_double(A_surfxml_cluster_bb_bw);
358   if(strcmp(A_surfxml_cluster_bb_lat,""))
359     cluster.bb_lat = surf_parse_get_double(A_surfxml_cluster_bb_lat);
360   cluster.router_id = A_surfxml_cluster_router_id;
361
362   switch (AX_surfxml_cluster_sharing_policy) {
363   case A_surfxml_cluster_sharing_policy_SHARED:
364     cluster.sharing_policy = SURF_LINK_SHARED;
365     break;
366   case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
367     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
368     break;
369   case A_surfxml_cluster_sharing_policy_FATPIPE:
370     cluster.sharing_policy = SURF_LINK_FATPIPE;
371     break;
372   default:
373     surf_parse_error("Invalid cluster sharing policy for cluster %s",
374                      cluster.id);
375     break;
376   }
377   switch (AX_surfxml_cluster_bb_sharing_policy) {
378   case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
379     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
380     break;
381   case A_surfxml_cluster_bb_sharing_policy_SHARED:
382     cluster.bb_sharing_policy = SURF_LINK_SHARED;
383     break;
384   default:
385     surf_parse_error("Invalid bb sharing policy in cluster %s",
386                      cluster.id);
387     break;
388   }
389
390   cluster.availability_trace = A_surfxml_cluster_availability_file;
391   cluster.state_trace = A_surfxml_cluster_state_file;
392   sg_platf_new_cluster(&cluster);
393 }
394 void ETag_surfxml_cluster(void){
395   /* nothing I can think of */
396 }
397
398 void STag_surfxml_cabinet(void){
399   s_sg_platf_cabinet_cbarg_t cabinet;
400   memset(&cabinet,0,sizeof(cabinet));
401   cabinet.id = A_surfxml_cabinet_id;
402   cabinet.prefix = A_surfxml_cabinet_prefix;
403   cabinet.suffix = A_surfxml_cabinet_suffix;
404   cabinet.power = surf_parse_get_double(A_surfxml_cabinet_power);
405   cabinet.bw = surf_parse_get_double(A_surfxml_cabinet_bw);
406   cabinet.lat = surf_parse_get_double(A_surfxml_cabinet_lat);
407   cabinet.radical = A_surfxml_cabinet_radical;
408
409   sg_platf_new_cabinet(&cabinet);
410 }
411 void ETag_surfxml_cabinet(void){
412   /* nothing I can think of */
413 }
414
415 void STag_surfxml_peer(void){
416   s_sg_platf_peer_cbarg_t peer;
417   memset(&peer,0,sizeof(peer));
418   peer.id = A_surfxml_peer_id;
419   peer.power = surf_parse_get_double(A_surfxml_peer_power);
420   peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw_in);
421   peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw_out);
422   peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
423   peer.coord = A_surfxml_peer_coordinates;
424   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability_file);
425   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state_file);
426
427   sg_platf_new_peer(&peer);
428 }
429 void ETag_surfxml_peer(void){
430   /* nothing to do here */
431 }
432 void STag_surfxml_link(void){
433   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
434 }
435
436 void STag_surfxml_backbone(void){
437   /* nothing to do here */
438 }
439
440 void ETag_surfxml_link(void){
441   s_sg_platf_link_cbarg_t link;
442   memset(&link,0,sizeof(link));
443
444   link.properties = current_property_set;
445
446   link.id = A_surfxml_link_id;
447   link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
448   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth_file);
449   link.latency = surf_parse_get_double(A_surfxml_link_latency);
450   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency_file);
451
452   switch (A_surfxml_link_state) {
453   case A_surfxml_link_state_ON:
454     link.state = SURF_RESOURCE_ON;
455     break;
456   case A_surfxml_link_state_OFF:
457     link.state = SURF_RESOURCE_OFF;
458     break;
459   default:
460     surf_parse_error("invalid state for link %s", link.id);
461     break;
462   }
463   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state_file);
464
465   switch (A_surfxml_link_sharing_policy) {
466   case A_surfxml_link_sharing_policy_SHARED:
467     link.policy = SURF_LINK_SHARED;
468     break;
469   case A_surfxml_link_sharing_policy_FATPIPE:
470      link.policy = SURF_LINK_FATPIPE;
471      break;
472   case A_surfxml_link_sharing_policy_FULLDUPLEX:
473      link.policy = SURF_LINK_FULLDUPLEX;
474      break;
475   default:
476     surf_parse_error("Invalid sharing policy in link %s", link.id);
477     break;
478   }
479
480   sg_platf_new_link(&link);
481
482   current_property_set = NULL;
483 }
484
485 void STag_surfxml_link_ctn(void){
486   s_sg_platf_linkctn_cbarg_t linkctn;
487   memset(&linkctn,0,sizeof(linkctn));
488
489   linkctn.id = A_surfxml_link_ctn_id;
490
491   switch (A_surfxml_link_ctn_direction) {
492   case AU_surfxml_link_ctn_direction:
493   case A_surfxml_link_ctn_direction_NONE:
494     linkctn.direction = SURF_LINK_DIRECTION_NONE;
495     break;
496   case A_surfxml_link_ctn_direction_UP:
497     linkctn.direction = SURF_LINK_DIRECTION_UP;
498     break;
499   case A_surfxml_link_ctn_direction_DOWN:
500     linkctn.direction = SURF_LINK_DIRECTION_DOWN;
501     break;
502   }
503   sg_platf_new_linkctn(&linkctn);
504
505 }
506
507 void ETag_surfxml_link_ctn(void){
508  // NOTHING TO DO
509 }
510 void ETag_surfxml_backbone(void){
511   s_sg_platf_link_cbarg_t link;
512   memset(&link,0,sizeof(link));
513
514   link.properties = NULL;
515
516   link.id = A_surfxml_backbone_id;
517   link.bandwidth = surf_parse_get_double(A_surfxml_backbone_bandwidth);
518   link.latency = surf_parse_get_double(A_surfxml_backbone_latency);
519   link.state = SURF_RESOURCE_ON;
520   link.policy = SURF_LINK_SHARED;
521
522   sg_platf_new_link(&link);
523   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
524   current_property_set = NULL;
525 }
526
527 void STag_surfxml_route(void){
528   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
529       "Missing end-points while defining route \"%s\"->\"%s\"",
530       A_surfxml_route_src, A_surfxml_route_dst);
531 }
532 void STag_surfxml_ASroute(void){
533   xbt_assert(strlen(A_surfxml_ASroute_src) > 0 || strlen(A_surfxml_ASroute_dst) > 0
534       || strlen(A_surfxml_ASroute_gw_src) > 0 || strlen(A_surfxml_ASroute_gw_dst) > 0,
535       "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
536       A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
537       A_surfxml_ASroute_gw_src,A_surfxml_ASroute_gw_dst);
538 }
539 void STag_surfxml_bypassRoute(void){
540   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0 || strlen(A_surfxml_bypassRoute_dst) > 0,
541       "Missing end-points while defining bupass route \"%s\"->\"%s\"",
542       A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
543 }
544 void STag_surfxml_bypassASroute(void){
545   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0 || strlen(A_surfxml_bypassASroute_dst) > 0
546       || strlen(A_surfxml_bypassASroute_gw_src) > 0 || strlen(A_surfxml_bypassASroute_gw_dst) > 0,
547       "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
548       A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
549       A_surfxml_bypassASroute_gw_src,A_surfxml_bypassASroute_gw_dst);
550 }
551
552 void ETag_surfxml_route(void){
553   s_sg_platf_route_cbarg_t route;
554   memset(&route,0,sizeof(route));
555
556   route.src = A_surfxml_route_src;
557   route.dst = A_surfxml_route_dst;
558
559   switch (A_surfxml_route_symmetrical) {
560   case AU_surfxml_route_symmetrical:
561   case A_surfxml_route_symmetrical_YES:
562     route.symmetrical = TRUE;
563     break;
564   case A_surfxml_route_symmetrical_NO:
565     route.symmetrical = FALSE;;
566     break;
567   }
568
569   sg_platf_new_route(&route);
570 }
571
572 void ETag_surfxml_ASroute(void){
573   s_sg_platf_ASroute_cbarg_t ASroute;
574   memset(&ASroute,0,sizeof(ASroute));
575
576   ASroute.src = A_surfxml_ASroute_src;
577   ASroute.dst = A_surfxml_ASroute_dst;
578   ASroute.gw_src = A_surfxml_ASroute_gw_src;
579   ASroute.gw_dst = A_surfxml_ASroute_gw_dst;
580
581   switch (A_surfxml_ASroute_symmetrical) {
582   case AU_surfxml_ASroute_symmetrical:
583   case A_surfxml_ASroute_symmetrical_YES:
584     ASroute.symmetrical = TRUE;
585     break;
586   case A_surfxml_ASroute_symmetrical_NO:
587     ASroute.symmetrical = FALSE;;
588     break;
589   }
590
591   sg_platf_new_ASroute(&ASroute);
592 }
593
594 void ETag_surfxml_bypassRoute(void){
595   s_sg_platf_bypassRoute_cbarg_t route;
596   memset(&route,0,sizeof(route));
597
598   route.src = A_surfxml_bypassRoute_src;
599   route.dst = A_surfxml_bypassRoute_dst;
600
601   sg_platf_new_bypassRoute(&route);
602 }
603
604 void ETag_surfxml_bypassASroute(void){
605   s_sg_platf_bypassASroute_cbarg_t ASroute;
606   memset(&ASroute,0,sizeof(ASroute));
607
608   ASroute.src = A_surfxml_bypassASroute_src;
609   ASroute.dst = A_surfxml_bypassASroute_dst;
610   ASroute.gw_src = A_surfxml_bypassASroute_gw_src;
611   ASroute.gw_dst = A_surfxml_bypassASroute_gw_dst;
612
613   sg_platf_new_bypassASroute(&ASroute);
614 }
615
616 void STag_surfxml_process(void){
617   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
618 }
619 void STag_surfxml_argument(void){
620   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
621 }
622 void STag_surfxml_prop(void){
623   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
624 }
625 void STag_surfxml_trace(void){
626   surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
627 }
628 void STag_surfxml_trace_connect(void){
629   surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
630 }
631 void STag_surfxml_AS(void){
632   sg_platf_new_AS_begin(A_surfxml_AS_id, (int)A_surfxml_AS_routing);
633 }
634 void ETag_surfxml_AS(void){
635   sg_platf_new_AS_end();
636 }
637
638 void STag_surfxml_config(void){
639   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
640   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
641 }
642 void ETag_surfxml_config(void){
643   xbt_dict_cursor_t cursor = NULL;
644   char *key;
645   char *elem;
646   char *cfg;
647   xbt_dict_foreach(current_property_set, cursor, key, elem) {
648     cfg = bprintf("%s:%s",key,elem);
649     if(xbt_cfg_is_default_value(_surf_cfg_set, key))
650       xbt_cfg_set_parse(_surf_cfg_set, cfg);
651     else
652       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
653     free(cfg);
654   }
655   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
656   xbt_dict_free(&current_property_set);
657 }
658 void STag_surfxml_random(void){
659 //TODO
660 }
661 void ETag_surfxml_random(void){
662 //TODO
663 }
664
665 #define parse_method(type,name)                                         \
666   void type##Tag_surfxml_##name(void)                                   \
667   { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }    \
668   void type##Tag_surfxml_##name(void)
669
670 parse_method(E, process);
671 parse_method(E, argument);
672 parse_method(E, prop);
673 parse_method(E, trace);
674 parse_method(E, trace_connect);
675
676 /* Open and Close parse file */
677
678 void surf_parse_open(const char *file)
679 {
680   static int warned = 0;        /* warn only once */
681   if (!file) {
682     if (!warned) {
683       XBT_WARN
684           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
685               "If it is not what you want, go fix your code.");
686       warned = 1;
687     }
688     return;
689   }
690
691   if (!surf_input_buffer_stack)
692     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
693   if (!surf_file_to_parse_stack)
694     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
695
696   if (!surf_parsed_filename_stack)
697     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
698   surf_parsed_filename = xbt_strdup(file);
699
700   surf_file_to_parse = surf_fopen(file, "r");
701   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
702   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
703   surf_parse__switch_to_buffer(surf_input_buffer);
704   surf_parse_lineno = 1;
705 }
706
707 void surf_parse_close(void)
708 {
709   xbt_dynar_free(&surf_input_buffer_stack);
710   xbt_dynar_free(&surf_file_to_parse_stack);
711   xbt_dynar_free(&surf_parsed_filename_stack);
712
713   free(surf_parsed_filename);
714
715   if (surf_file_to_parse) {
716     surf_parse__delete_buffer(surf_input_buffer);
717     fclose(surf_file_to_parse);
718     surf_file_to_parse = NULL; //Must be reset for Bypass
719   }
720 }
721
722 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
723
724 static int _surf_parse(void) {
725   return surf_parse_lex();
726 }
727 int_f_void_t surf_parse = _surf_parse;
728
729
730 /* Aux parse functions */
731
732 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
733 {
734   xbt_dynar_push(cb_list, &function);
735 }
736
737 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
738 {
739   xbt_ex_t e;
740   unsigned int it=0;
741   void_f_void_t null_f=NULL;
742
743   TRY {
744     it = xbt_dynar_search(cb_list,&function);
745   }
746   CATCH(e) {
747     if (e.category == not_found_error) {
748       xbt_ex_free(e);
749       xbt_die("Trying to remove a callback that is not here! This should not happen");
750     }
751     RETHROW;
752   }
753
754   xbt_dynar_replace(cb_list, it,&null_f);
755 }
756
757 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
758 {
759   unsigned int iterator;
760   void_f_void_t fun;
761   xbt_dynar_foreach(cb_list, iterator, fun) {
762     if (fun) fun();
763   }
764 }
765
766
767 /* Prop tag functions */
768
769 /**
770  * With XML parser
771  */
772 void parse_properties(void)
773 {
774   if (!current_property_set)
775     current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
776
777   xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
778 }
779
780
781 /* Random tag functions */
782
783 double get_cpu_power(const char *power)
784 {
785   double power_scale = 0.0;
786   const char *p, *q;
787   char *generator;
788   random_data_t random = NULL;
789   /* randomness is inserted like this: power="$rand(my_random)" */
790   if (((p = strstr(power, "$rand(")) != NULL)
791       && ((q = strstr(power, ")")) != NULL)) {
792     if (p < q) {
793       generator = xbt_malloc(q - (p + 6) + 1);
794       memcpy(generator, p + 6, q - (p + 6));
795       generator[q - (p + 6)] = '\0';
796       random = xbt_dict_get_or_null(random_data_list, generator);
797       xbt_assert(random, "Random generator %s undefined", generator);
798       power_scale = random_generate(random);
799     }
800   } else {
801     power_scale = surf_parse_get_double(power);
802   }
803   return power_scale;
804 }
805
806 double random_min, random_max, random_mean, random_std_deviation;
807 e_random_generator_t random_generator;
808 char *random_id;
809
810 static void init_randomness(void)
811 {
812   random_id = A_surfxml_random_id;
813   random_min = surf_parse_get_double(A_surfxml_random_min);
814   random_max = surf_parse_get_double(A_surfxml_random_max);
815   random_mean = surf_parse_get_double(A_surfxml_random_mean);
816   random_std_deviation = surf_parse_get_double(A_surfxml_random_std_deviation);
817   switch (A_surfxml_random_generator) {
818   case AU_surfxml_random_generator:
819   case A_surfxml_random_generator_NONE:
820     random_generator = NONE;
821     break;
822   case A_surfxml_random_generator_DRAND48:
823     random_generator = DRAND48;
824     break;
825   case A_surfxml_random_generator_RAND:
826     random_generator = RAND;
827     break;
828   case A_surfxml_random_generator_RNGSTREAM:
829     random_generator = RNGSTREAM;
830     break;
831   default:
832     surf_parse_error("Invalid random generator");
833     break;
834   }
835 }
836
837 static void add_randomness(void)
838 {
839   /* If needed aditional properties can be added by using the prop tag */
840   random_data_t random =
841       random_new(random_generator, 0, random_min, random_max, random_mean,
842                  random_std_deviation);
843   xbt_dict_set(random_data_list, random_id, (void *) random,
844                &xbt_free_ref);
845 }
846