Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
989e211926c99cdaf3de57cf6742417cfd82b7be
[simgrid.git] / src / surf / surfxml_parseplatf.cpp
1 /* Copyright (c) 2006-2015. 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 "simgrid/platf.h"
12 #include "surf/surfxml_parse.h"
13 #include "src/surf/surf_private.h"
14
15 #ifdef HAVE_LUA
16 #include "src/bindings/lua/simgrid_lua.h"
17
18 #include <lua.h>                /* Always include this when calling Lua */
19 #include <lauxlib.h>            /* Always include this when calling Lua */
20 #include <lualib.h>             /* Prototype for luaL_openlibs(), */
21 #endif
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
24
25 /*
26  *  Allow the cluster tag to mess with the parsing buffer.
27  * (this will probably become obsolete once the cluster tag do not mess with the parsing callbacks directly)
28  */
29
30 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the cluster tag */
31 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
32 int surfxml_bufferstack_size = 2048;
33
34 static char *old_buff = NULL;
35
36 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
37 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
38
39 void surfxml_bufferstack_push(int new_one)
40 {
41   if (!new_one)
42     old_buff = surfxml_bufferstack;
43   else {
44     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
45     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
46   }
47 }
48
49 void surfxml_bufferstack_pop(int new_one)
50 {
51   if (!new_one)
52     surfxml_bufferstack = old_buff;
53   else {
54     free(surfxml_bufferstack);
55     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
56   }
57 }
58
59 /*
60  * Trace related stuff
61  */
62
63 xbt_dict_t traces_set_list = NULL;
64 xbt_dict_t trace_connect_list_host_avail = NULL;
65 xbt_dict_t trace_connect_list_host_speed = NULL;
66 xbt_dict_t trace_connect_list_link_avail = NULL;
67 xbt_dict_t trace_connect_list_link_bw = NULL;
68 xbt_dict_t trace_connect_list_link_lat = NULL;
69
70 /* ***************************************** */
71
72 static int after_config_done;
73 void parse_after_config() {
74   if (!after_config_done) {
75           TRACE_start();
76
77     /* Register classical callbacks */
78     storage_register_callbacks();
79     routing_register_callbacks();
80
81     after_config_done = 1;
82   }
83 }
84
85 /* This function acts as a main in the parsing area. */
86 void parse_platform_file(const char *file)
87 {
88 #ifdef HAVE_LUA
89   int is_lua = (file != NULL && strlen(file) > 3 && file[strlen(file)-3] == 'l' && file[strlen(file)-2] == 'u'
90         && file[strlen(file)-1] == 'a');
91 #endif
92
93   surf_parse_init_callbacks();
94
95 #ifdef HAVE_LUA
96   /* Check if file extension is "lua". If so, we will use
97    * the lua bindings to parse the platform file (since it is
98    * written in lua). If not, we will use the (old?) XML parser
99    */
100   if (is_lua) {
101     lua_State* L = luaL_newstate();
102     luaL_openlibs(L);
103
104     luaL_loadfile(L, file); // This loads the file without executing it.
105
106     /* Run the script */
107     if (lua_pcall(L, 0, 0, 0)) {
108         XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Errormessage:", lua_tostring(L, -1));
109         xbt_die("Lua call failed. See Log");
110     }
111   }
112   else
113 #endif
114   { // Use XML parser
115
116     int parse_status;
117
118     /* init the flex parser */
119     surfxml_buffer_stack_stack_ptr = 1;
120     surfxml_buffer_stack_stack[0] = 0;
121     after_config_done = 0;
122     surf_parse_open(file);
123
124     traces_set_list = xbt_dict_new_homogeneous(NULL);
125     trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
126     trace_connect_list_host_speed = xbt_dict_new_homogeneous(free);
127     trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
128     trace_connect_list_link_bw = xbt_dict_new_homogeneous(free);
129     trace_connect_list_link_lat = xbt_dict_new_homogeneous(free);
130
131     /* Init my data */
132     if (!surfxml_bufferstack_stack)
133       surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
134
135     /* Do the actual parsing */
136     parse_status = surf_parse();
137
138     /* Free my data */
139     xbt_dict_free(&trace_connect_list_host_avail);
140     xbt_dict_free(&trace_connect_list_host_speed);
141     xbt_dict_free(&trace_connect_list_link_avail);
142     xbt_dict_free(&trace_connect_list_link_bw);
143     xbt_dict_free(&trace_connect_list_link_lat);
144     xbt_dict_free(&traces_set_list);
145     xbt_dict_free(&random_data_list);
146     xbt_dynar_free(&surfxml_bufferstack_stack);
147
148     surf_parse_close();
149
150     if (parse_status)
151       surf_parse_error("Parse error in %s", file);
152
153   }
154
155
156 }