1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "simgrid/platf.h"
12 #include "surf/surfxml_parse.h"
13 #include "src/surf/surf_private.h"
16 #include "src/bindings/lua/simgrid_lua.h"
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(), */
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
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)
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;
34 static char *old_buff = NULL;
36 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
37 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
39 void surfxml_bufferstack_push(int new)
42 old_buff = surfxml_bufferstack;
44 xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
45 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
49 void surfxml_bufferstack_pop(int new)
52 surfxml_bufferstack = old_buff;
54 free(surfxml_bufferstack);
55 xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
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_power = NULL;
66 xbt_dict_t trace_connect_list_link_avail = NULL;
67 xbt_dict_t trace_connect_list_bandwidth = NULL;
68 xbt_dict_t trace_connect_list_latency = NULL;
70 /* ***************************************** */
72 static int after_config_done;
73 void parse_after_config() {
74 if (!after_config_done) {
77 /* Register classical callbacks */
78 storage_register_callbacks();
79 routing_register_callbacks();
81 after_config_done = 1;
85 /* This function acts as a main in the parsing area. */
86 void parse_platform_file(const char *file)
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');
93 surf_parse_init_callbacks();
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
101 lua_State* L = luaL_newstate();
104 luaL_loadfile(L, file); // This loads the file without executing it.
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");
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);
124 traces_set_list = xbt_dict_new_homogeneous(NULL);
125 trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
126 trace_connect_list_power = xbt_dict_new_homogeneous(free);
127 trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
128 trace_connect_list_bandwidth = xbt_dict_new_homogeneous(free);
129 trace_connect_list_latency = xbt_dict_new_homogeneous(free);
132 if (!surfxml_bufferstack_stack)
133 surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
135 /* Do the actual parsing */
136 parse_status = surf_parse();
139 xbt_dict_free(&trace_connect_list_host_avail);
140 xbt_dict_free(&trace_connect_list_power);
141 xbt_dict_free(&trace_connect_list_link_avail);
142 xbt_dict_free(&trace_connect_list_bandwidth);
143 xbt_dict_free(&trace_connect_list_latency);
144 xbt_dict_free(&traces_set_list);
145 xbt_dict_free(&random_data_list);
146 xbt_dynar_free(&surfxml_bufferstack_stack);
151 surf_parse_error("Parse error in %s", file);