Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use the XBT_INLINE macro (defined in the portability layer header misc.h) to specify...
[simgrid.git] / src / surf / surfxml_parse.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
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. */
7
8 #include "xbt/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
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");
15
16 #undef CLEANUP
17 #include "surfxml.c"
18
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;
38
39 xbt_dict_t current_property_set = NULL;
40   
41 static xbt_dynar_t surf_input_buffer_stack = NULL;
42 static xbt_dynar_t surf_file_to_parse_stack = NULL;
43
44 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
45
46 YY_BUFFER_STATE surf_input_buffer;
47 FILE *surf_file_to_parse;
48
49 void surf_parse_free_callbacks(void)
50 {
51   xbt_dynar_free(&STag_surfxml_platform_cb_list);
52   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
53   xbt_dynar_free(&STag_surfxml_host_cb_list);
54   xbt_dynar_free(&ETag_surfxml_host_cb_list);
55   xbt_dynar_free(&STag_surfxml_router_cb_list);
56   xbt_dynar_free(&ETag_surfxml_router_cb_list);
57   xbt_dynar_free(&STag_surfxml_link_cb_list);
58   xbt_dynar_free(&ETag_surfxml_link_cb_list);
59   xbt_dynar_free(&STag_surfxml_route_cb_list);
60   xbt_dynar_free(&ETag_surfxml_route_cb_list);
61   xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
62   xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
63   xbt_dynar_free(&STag_surfxml_process_cb_list);
64   xbt_dynar_free(&ETag_surfxml_process_cb_list);
65   xbt_dynar_free(&STag_surfxml_argument_cb_list);
66   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
67   xbt_dynar_free(&STag_surfxml_prop_cb_list);
68   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
69 }
70
71 void surf_parse_reset_parser(void)
72 {
73   surf_parse_free_callbacks();
74   STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
75   ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
76   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
77   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
78   STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
79   ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
80   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
81   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
82   STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
83   ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
84   STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
85   ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
86   STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
87   ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
88   STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
89   ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
90   STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
91   ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
92
93 }
94
95 void STag_surfxml_include(void)
96 {
97   xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
98   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
99
100   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
101   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
102               A_surfxml_include_file);
103   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
104   surf_parse__switch_to_buffer(surf_input_buffer);
105   printf("STAG\n");
106   fflush(NULL);
107 }
108
109 void ETag_surfxml_include(void)
110 {
111   printf("ETAG\n");
112   fflush(NULL);
113   surf_parse__delete_buffer(surf_input_buffer);
114   fclose(surf_file_to_parse);
115   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
116   xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
117 }
118
119 void STag_surfxml_platform(void)
120 {
121   double version = 0.0;
122
123   sscanf(A_surfxml_platform_version, "%lg", &version);
124
125   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
126               "You're using an ancient XML file. "
127               "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
128               "instead of MBytes, MFlops and seconds. "
129               "A script (surfxml_update.pl) to help you convert your old "
130               "platform files "
131               "is available in the contrib/platform_generation directory "
132               "of the simgrid repository. Please check also out the "
133               "SURF section of the ChangeLog for the 3.1 version. "
134               "Last, do not forget to also update your values for "
135               "the calls to MSG_task_create (if any).");
136   xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
137               "You're using an old XML file. "
138               "A script (surfxml_update.pl) to help you convert your old "
139               "platform files "
140               "is available in the contrib/platform_generation directory "
141               "of the simgrid repository.");
142
143   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
144 }
145
146 void ETag_surfxml_platform(void)
147 {
148   surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
149 }
150
151 void STag_surfxml_host(void)
152 {
153   surfxml_call_cb_functions(STag_surfxml_host_cb_list);
154 }
155
156 void ETag_surfxml_host(void)
157 {
158   surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
159 }
160
161 void STag_surfxml_router(void)
162 {
163   surfxml_call_cb_functions(STag_surfxml_router_cb_list);
164 }
165
166 void ETag_surfxml_router(void)
167 {
168   surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
169 }
170
171 void STag_surfxml_link(void)
172 {
173   surfxml_call_cb_functions(STag_surfxml_link_cb_list);
174 }
175
176 void ETag_surfxml_link(void)
177 {
178   surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
179 }
180
181 void STag_surfxml_route(void)
182 {
183   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
184 }
185
186 void ETag_surfxml_route(void)
187 {
188   surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
189 }
190
191 void STag_surfxml_link_c_ctn(void)
192 {
193   surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
194 }
195
196 void ETag_surfxml_link_c_ctn(void)
197 {
198   surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
199 }
200
201 void STag_surfxml_process(void)
202 {
203   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
204 }
205
206 void ETag_surfxml_process(void)
207 {
208   surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
209 }
210
211 void STag_surfxml_argument(void)
212 {
213   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
214 }
215
216 void ETag_surfxml_argument(void)
217 {
218   surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
219 }
220
221 void STag_surfxml_prop(void)
222 {
223   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
224 }
225 void ETag_surfxml_prop(void)
226 {
227   surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
228 }
229
230
231 void surf_parse_open(const char *file)
232 {
233   static int warned = 0;        /* warn only once */
234   if (!file) {
235     if (!warned) {
236       WARN0
237           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
238       warned = 1;
239     }
240     return;
241   }
242   if (!surf_input_buffer_stack)
243     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
244   if (!surf_file_to_parse_stack)
245     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
246
247   surf_file_to_parse = surf_fopen(file, "r");
248   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
249   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
250   surf_parse__switch_to_buffer(surf_input_buffer);
251   surf_parse_lineno = 1;
252 }
253
254 void surf_parse_close(void)
255 {
256   if (surf_input_buffer_stack)
257     xbt_dynar_free(&surf_input_buffer_stack);
258   if (surf_file_to_parse_stack)
259     xbt_dynar_free(&surf_file_to_parse_stack);
260
261   if (surf_file_to_parse) {
262     surf_parse__delete_buffer(surf_input_buffer);
263     fclose(surf_file_to_parse);
264   }
265 }
266
267
268 static int _surf_parse(void)
269 {
270   return surf_parse_lex();
271 }
272
273 int_f_void_t surf_parse = _surf_parse;
274
275 void surf_parse_get_double(double *value, const char *string)
276 {
277   int ret = 0;
278
279   ret = sscanf(string, "%lg", value);
280   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
281               surf_parse_lineno, string);
282 }
283
284 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
285 {
286   if ((!string) || (strcmp(string, "") == 0))
287     *trace = NULL;
288   else
289     *trace = tmgr_trace_new(string);
290 }
291
292 void parse_properties(void)
293 {
294   char *value = NULL;
295
296   if(!current_property_set) current_property_set = xbt_dict_new();
297
298    value = xbt_strdup(A_surfxml_prop_value);  
299    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
300
301 }
302
303 void free_string(void *d)
304 {
305   free(*(void**)d);
306 }
307
308 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
309 {
310   xbt_dynar_push(cb_list, &function);
311 }
312
313 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
314 {
315   int iterator;
316   void_f_void_t fun;
317   xbt_dynar_foreach(cb_list, iterator, fun){
318        DEBUG2("call %p %p",fun,*fun);
319        (*fun)();
320     }
321 }