Logo AND Algorithmique Numérique Distribuée

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