3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
14 "Logging specific to the SURF parsing module");
19 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
20 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
21 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
22 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
23 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
24 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
25 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
26 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
27 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
28 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
29 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
30 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
31 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
32 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
33 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
34 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
35 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
36 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
37 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
39 xbt_dict_t current_property_set = NULL;
41 xbt_dict_t route_table = NULL;
42 xbt_dynar_t route_link_list = NULL;
43 xbt_dynar_t links = NULL;
44 xbt_dynar_t keys = NULL;
46 static xbt_dynar_t surf_input_buffer_stack = NULL;
47 static xbt_dynar_t surf_file_to_parse_stack = NULL;
49 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
51 YY_BUFFER_STATE surf_input_buffer;
52 FILE *surf_file_to_parse = NULL;
54 void make_route_table(void)
56 xbt_dict_cursor_t cursor = NULL;
58 const char *sep = "##";
60 xbt_dict_foreach(route_table, cursor, key, data) {
61 links = (xbt_dynar_t)data;
62 keys = xbt_str_split_str(key, sep);
63 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
66 xbt_dict_free(&route_table);
67 xbt_dynar_free(&links);
71 void surf_parse_free_callbacks(void)
73 xbt_dynar_free(&STag_surfxml_platform_cb_list);
74 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
75 xbt_dynar_free(&STag_surfxml_host_cb_list);
76 xbt_dynar_free(&ETag_surfxml_host_cb_list);
77 xbt_dynar_free(&STag_surfxml_router_cb_list);
78 xbt_dynar_free(&ETag_surfxml_router_cb_list);
79 xbt_dynar_free(&STag_surfxml_link_cb_list);
80 xbt_dynar_free(&ETag_surfxml_link_cb_list);
81 xbt_dynar_free(&STag_surfxml_route_cb_list);
82 xbt_dynar_free(&ETag_surfxml_route_cb_list);
83 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
84 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
85 xbt_dynar_free(&STag_surfxml_process_cb_list);
86 xbt_dynar_free(&ETag_surfxml_process_cb_list);
87 xbt_dynar_free(&STag_surfxml_argument_cb_list);
88 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
89 xbt_dynar_free(&STag_surfxml_prop_cb_list);
90 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
93 void surf_parse_reset_parser(void)
95 surf_parse_free_callbacks();
96 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
97 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
98 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
99 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
100 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
101 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
102 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
103 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
104 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
105 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
106 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
107 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
108 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
109 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
110 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
111 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
112 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
113 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
117 void STag_surfxml_include(void)
119 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
120 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
122 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
123 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
124 A_surfxml_include_file);
125 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
126 surf_parse__switch_to_buffer(surf_input_buffer);
131 void ETag_surfxml_include(void)
135 surf_parse__delete_buffer(surf_input_buffer);
136 fclose(surf_file_to_parse);
137 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
138 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
141 void STag_surfxml_platform(void)
143 double version = 0.0;
145 sscanf(A_surfxml_platform_version, "%lg", &version);
147 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
148 "You're using an ancient XML file. "
149 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
150 "instead of MBytes, MFlops and seconds. "
151 "A script (surfxml_update.pl) to help you convert your old "
153 "is available in the contrib/platform_generation directory "
154 "of the simgrid repository. Please check also out the "
155 "SURF section of the ChangeLog for the 3.1 version. "
156 "Last, do not forget to also update your values for "
157 "the calls to MSG_task_create (if any).");
158 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
159 "You're using an old XML file. "
160 "A script (surfxml_update.pl) to help you convert your old "
162 "is available in the contrib/platform_generation directory "
163 "of the simgrid repository.");
165 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
168 void ETag_surfxml_platform(void)
170 if (route_table != NULL) make_route_table();
173 void STag_surfxml_host(void)
175 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
178 void ETag_surfxml_host(void)
180 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
183 void STag_surfxml_router(void)
185 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
188 void ETag_surfxml_router(void)
190 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
193 void STag_surfxml_link(void)
195 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
198 void ETag_surfxml_link(void)
200 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
203 void STag_surfxml_route(void)
205 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
208 void ETag_surfxml_route(void)
210 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
213 void STag_surfxml_link_c_ctn(void)
215 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
218 void ETag_surfxml_link_c_ctn(void)
220 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
223 void STag_surfxml_process(void)
225 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
228 void ETag_surfxml_process(void)
230 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
233 void STag_surfxml_argument(void)
235 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
238 void ETag_surfxml_argument(void)
240 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
243 void STag_surfxml_prop(void)
245 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
247 void ETag_surfxml_prop(void)
249 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
253 void surf_parse_open(const char *file)
255 static int warned = 0; /* warn only once */
259 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
264 if (!surf_input_buffer_stack)
265 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
266 if (!surf_file_to_parse_stack)
267 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
269 surf_file_to_parse = surf_fopen(file, "r");
270 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
271 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
272 surf_parse__switch_to_buffer(surf_input_buffer);
273 surf_parse_lineno = 1;
276 void surf_parse_close(void)
278 if (surf_input_buffer_stack)
279 xbt_dynar_free(&surf_input_buffer_stack);
280 if (surf_file_to_parse_stack)
281 xbt_dynar_free(&surf_file_to_parse_stack);
283 if (surf_file_to_parse) {
284 surf_parse__delete_buffer(surf_input_buffer);
285 fclose(surf_file_to_parse);
290 static int _surf_parse(void)
292 return surf_parse_lex();
295 int_f_void_t surf_parse = _surf_parse;
297 void surf_parse_get_double(double *value, const char *string)
301 ret = sscanf(string, "%lg", value);
302 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
303 surf_parse_lineno, string);
306 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
308 if ((!string) || (strcmp(string, "") == 0))
311 *trace = tmgr_trace_new(string);
314 void parse_properties(void)
318 if(!current_property_set) current_property_set = xbt_dict_new();
320 value = xbt_strdup(A_surfxml_prop_value);
321 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
325 void free_string(void *d)
330 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
332 xbt_dynar_push(cb_list, &function);
335 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
337 unsigned int iterator;
339 xbt_dynar_foreach(cb_list, iterator, fun){
340 DEBUG2("call %p %p",fun,*fun);
345 void init_route_table(void)
347 xbt_dict_free(&route_table);
348 xbt_dynar_free(&route_link_list);
349 route_table = xbt_dict_new();
352 void parse_route_elem(void)
355 val = xbt_strdup(A_surfxml_link_c_ctn_id);
356 xbt_dynar_push(route_link_list, &val);
359 void parse_platform_file(const char* file)
361 surf_parse_open(file);
362 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);