Logo AND Algorithmique Numérique Distribuée

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