Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[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   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
257 }
258
259 void ETag_surfxml_host(void)    {
260   s_sg_platf_host_cbarg_t host;
261   memset(&host,0,sizeof(host));
262
263   host.properties = current_property_set;
264
265   host.id = A_surfxml_host_id;
266   host.power_peak = get_cpu_power(A_surfxml_host_power);
267   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
268   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
269   host.power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
270   host.state_trace = tmgr_trace_new(A_surfxml_host_state_file);
271   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
272         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
273   if (A_surfxml_host_state == A_surfxml_host_state_ON)
274     host.initial_state = SURF_RESOURCE_ON;
275   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
276     host.initial_state = SURF_RESOURCE_OFF;
277   host.coord = A_surfxml_host_coordinates;
278
279   sg_platf_new_host(&host);
280   current_property_set = NULL;
281 }
282
283 void STag_surfxml_router(void){
284   s_sg_platf_router_cbarg_t router;
285   memset(&router, 0, sizeof(router));
286
287         router.id = A_surfxml_router_id;
288         router.coord = A_surfxml_router_coordinates;
289
290         sg_platf_new_router(&router);
291 }
292
293 void STag_surfxml_cluster(void){
294   s_sg_platf_cluster_cbarg_t cluster;
295   memset(&cluster,0,sizeof(cluster));
296         cluster.id = A_surfxml_cluster_id;
297         cluster.prefix = A_surfxml_cluster_prefix;
298         cluster.suffix = A_surfxml_cluster_suffix;
299         cluster.radical = A_surfxml_cluster_radical;
300         cluster.power= surf_parse_get_double(A_surfxml_cluster_power);
301         cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
302         cluster.bw =   surf_parse_get_double(A_surfxml_cluster_bw);
303         cluster.lat =  surf_parse_get_double(A_surfxml_cluster_lat);
304         if(strcmp(A_surfxml_cluster_bb_bw,""))
305           cluster.bb_bw = surf_parse_get_double(A_surfxml_cluster_bb_bw);
306         if(strcmp(A_surfxml_cluster_bb_lat,""))
307           cluster.bb_lat = surf_parse_get_double(A_surfxml_cluster_bb_lat);
308         cluster.router_id = A_surfxml_cluster_router_id;
309
310   switch (AX_surfxml_cluster_sharing_policy) {
311   case A_surfxml_cluster_sharing_policy_SHARED:
312     cluster.sharing_policy = SURF_LINK_SHARED;
313     break;
314   case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
315     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
316     break;
317   case A_surfxml_cluster_sharing_policy_FATPIPE:
318     cluster.sharing_policy = SURF_LINK_FATPIPE;
319     break;
320   default:
321     surf_parse_error(bprintf
322                      ("Invalid cluster sharing policy for cluster %s",
323                       cluster.id));
324     break;
325   }
326   switch (AX_surfxml_cluster_bb_sharing_policy) {
327   case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
328     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
329     break;
330   case A_surfxml_cluster_bb_sharing_policy_SHARED:
331     cluster.bb_sharing_policy = SURF_LINK_SHARED;
332     break;
333   default:
334     surf_parse_error(bprintf
335                      ("Invalid bb sharing policy in cluster %s",
336                       cluster.id));
337     break;
338   }
339
340         cluster.availability_trace = A_surfxml_cluster_availability_file;
341         cluster.state_trace = A_surfxml_cluster_state_file;
342         sg_platf_new_cluster(&cluster);
343 }
344 void ETag_surfxml_cluster(void){
345   /* nothing I can think of */
346 }
347
348 void STag_surfxml_peer(void){
349   s_sg_platf_peer_cbarg_t peer;
350   memset(&peer,0,sizeof(peer));
351         peer.id = A_surfxml_peer_id;
352         peer.power = surf_parse_get_double(A_surfxml_peer_power);
353         peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw_in);
354         peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw_out);
355         peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
356         peer.coord = A_surfxml_peer_coordinates;
357         peer.availability_trace = tmgr_trace_new(A_surfxml_peer_availability_file);
358         peer.state_trace = tmgr_trace_new(A_surfxml_peer_state_file);
359
360         surfxml_call_cb_functions(STag_surfxml_peer_cb_list);
361         sg_platf_new_peer(&peer);
362 }
363 void ETag_surfxml_peer(void){
364   /* nothing to do here */
365 }
366 void STag_surfxml_link(void){
367   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
368 }
369 void ETag_surfxml_link(void){
370   s_sg_platf_link_cbarg_t link;
371   memset(&link,0,sizeof(link));
372
373   link.properties = current_property_set;
374
375   link.id = A_surfxml_link_id;
376   link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
377   link.bandwidth_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
378   link.latency = surf_parse_get_double(A_surfxml_link_latency);
379   link.latency_trace = tmgr_trace_new(A_surfxml_link_latency_file);
380
381   switch (A_surfxml_link_state) {
382   case A_surfxml_link_state_ON:
383     link.state = SURF_RESOURCE_ON;
384     break;
385   case A_surfxml_link_state_OFF:
386     link.state = SURF_RESOURCE_OFF;
387     break;
388   default:
389     surf_parse_error(bprintf("invalid state for link %s",link.id));
390     break;
391   }
392   link.state_trace = tmgr_trace_new(A_surfxml_link_state_file);
393
394   switch (A_surfxml_link_sharing_policy) {
395   case A_surfxml_link_sharing_policy_SHARED:
396     link.policy = SURF_LINK_SHARED;
397     break;
398   case A_surfxml_link_sharing_policy_FATPIPE:
399      link.policy = SURF_LINK_FATPIPE;
400      break;
401   case A_surfxml_link_sharing_policy_FULLDUPLEX:
402      link.policy = SURF_LINK_FULLDUPLEX;
403      break;
404   default:
405     surf_parse_error(bprintf("Invalid sharing policy in link %s",link.id));
406     break;
407   }
408
409   sg_platf_new_link(&link);
410
411   current_property_set = NULL;
412 }
413
414 void STag_surfxml_route(void){
415         surfxml_call_cb_functions(STag_surfxml_route_cb_list);
416 }
417 void STag_surfxml_link_ctn(void){
418         surfxml_call_cb_functions(STag_surfxml_link_ctn_cb_list);
419 }
420 void STag_surfxml_process(void){
421         surfxml_call_cb_functions(STag_surfxml_process_cb_list);
422 }
423 void STag_surfxml_argument(void){
424         surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
425 }
426 void STag_surfxml_prop(void){
427         surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
428 }
429 void STag_surfxml_trace(void){
430         surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
431 }
432 void STag_surfxml_trace_connect(void){
433         surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
434 }
435 void STag_surfxml_AS(void){
436   sg_platf_new_AS_begin(A_surfxml_AS_id,A_surfxml_AS_routing);
437 }
438 void ETag_surfxml_AS(void){
439   sg_platf_new_AS_end();
440 }
441 void STag_surfxml_ASroute(void){
442         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
443 }
444 void STag_surfxml_bypassRoute(void){
445         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
446 }
447 void STag_surfxml_config(void){
448   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
449   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
450 }
451 void ETag_surfxml_config(void){
452   xbt_dict_cursor_t cursor = NULL;
453   char *key;
454   char *elem;
455   char *cfg;
456   xbt_dict_foreach(current_property_set, cursor, key, elem) {
457     cfg = bprintf("%s:%s",key,elem);
458     if(xbt_cfg_is_default_value(_surf_cfg_set, key))
459       xbt_cfg_set_parse(_surf_cfg_set, cfg);
460     else
461       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
462     free(cfg);
463   }
464   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
465   xbt_dict_free(&current_property_set);
466 }
467 void STag_surfxml_random(void){
468         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
469 }
470
471 #define parse_method(type,name) \
472 void type##Tag_surfxml_##name(void) \
473 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
474 parse_method(E, route);
475 parse_method(E, link_ctn);
476 parse_method(E, process);
477 parse_method(E, argument);
478 parse_method(E, prop);
479 parse_method(E, trace);
480 parse_method(E, trace_connect);
481 parse_method(E, random);
482 parse_method(E, ASroute);
483 parse_method(E, bypassRoute);
484
485 /* Open and Close parse file */
486
487 void surf_parse_open(const char *file)
488 {
489   static int warned = 0;        /* warn only once */
490   if (!file) {
491     if (!warned) {
492       XBT_WARN
493           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
494               "If it is not what you want, go fix your code.");
495       warned = 1;
496     }
497     return;
498   }
499
500   if (!surf_input_buffer_stack)
501     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
502   if (!surf_file_to_parse_stack)
503     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
504
505   surf_file_to_parse = surf_fopen(file, "r");
506   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
507   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
508   surf_parse__switch_to_buffer(surf_input_buffer);
509   surf_parse_lineno = 1;
510 }
511
512 void surf_parse_close(void)
513 {
514   xbt_dynar_free(&surf_input_buffer_stack);
515   xbt_dynar_free(&surf_file_to_parse_stack);
516
517   if (surf_file_to_parse) {
518     surf_parse__delete_buffer(surf_input_buffer);
519     fclose(surf_file_to_parse);
520     surf_file_to_parse = NULL; //Must be reset for Bypass
521   }
522 }
523
524 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
525
526 static int _surf_parse(void) {
527   return surf_parse_lex();
528 }
529 int_f_void_t surf_parse = _surf_parse;
530
531
532 /* Aux parse functions */
533
534 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
535 {
536   xbt_dynar_push(cb_list, &function);
537 }
538
539 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
540 {
541   xbt_ex_t e;
542   unsigned int it=0;
543   void_f_void_t null_f=NULL;
544
545   TRY {
546     it = xbt_dynar_search(cb_list,&function);
547   }
548   CATCH(e) {
549     if (e.category == not_found_error) {
550       xbt_ex_free(e);
551       xbt_die("Trying to remove a callback that is not here! This should not happen");
552     }
553     RETHROW;
554   }
555
556   xbt_dynar_replace(cb_list, it,&null_f);
557 }
558
559 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
560 {
561   unsigned int iterator;
562   void_f_void_t fun;
563   xbt_dynar_foreach(cb_list, iterator, fun) {
564     if (fun) fun();
565   }
566 }
567
568
569 /* Prop tag functions */
570
571 /**
572  * With XML parser
573  */
574 void parse_properties(void)
575 {
576   if (!current_property_set)
577     current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
578
579   xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
580 }
581
582
583 /* Random tag functions */
584
585 double get_cpu_power(const char *power)
586 {
587   double power_scale = 0.0;
588   const char *p, *q;
589   char *generator;
590   random_data_t random = NULL;
591   /* randomness is inserted like this: power="$rand(my_random)" */
592   if (((p = strstr(power, "$rand(")) != NULL)
593       && ((q = strstr(power, ")")) != NULL)) {
594     if (p < q) {
595       generator = xbt_malloc(q - (p + 6) + 1);
596       memcpy(generator, p + 6, q - (p + 6));
597       generator[q - (p + 6)] = '\0';
598       random = xbt_dict_get_or_null(random_data_list, generator);
599       xbt_assert(random, "Random generator %s undefined", generator);
600       power_scale = random_generate(random);
601     }
602   } else {
603     power_scale = surf_parse_get_double(power);
604   }
605   return power_scale;
606 }
607
608 double random_min, random_max, random_mean, random_std_deviation,
609     random_generator;
610 char *random_id;
611
612 static void init_randomness(void)
613 {
614   random_id = A_surfxml_random_id;
615   random_min = surf_parse_get_double(A_surfxml_random_min);
616   random_max = surf_parse_get_double(A_surfxml_random_max);
617   random_mean = surf_parse_get_double(A_surfxml_random_mean);
618   random_std_deviation = surf_parse_get_double(A_surfxml_random_std_deviation);
619   random_generator = A_surfxml_random_generator;
620 }
621
622 static void add_randomness(void)
623 {
624   /* If needed aditional properties can be added by using the prop tag */
625   random_data_t random =
626       random_new(random_generator, 0, random_min, random_max, random_mean,
627                  random_std_deviation);
628   xbt_dict_set(random_data_list, random_id, (void *) random,
629                &xbt_free_ref);
630 }
631