Logo AND Algorithmique Numérique Distribuée

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