Skip to content

Commit 7921fe2

Browse files
Ekdohibskahrl
authored andcommittedAug 15, 2013
Fix formspec escaping, add escaping to info.txt for texture packs.
1 parent a97c085 commit 7921fe2

File tree

5 files changed

+44
-89
lines changed

5 files changed

+44
-89
lines changed
 

‎builtin/mainmenu.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ function menu.render_favorite(spec,render_details)
3939
local text = ""
4040

4141
if spec.name ~= nil then
42-
text = text .. fs_escape_string(spec.name:trim())
42+
text = text .. engine.formspec_escape(spec.name:trim())
4343

4444
-- if spec.description ~= nil and
45-
-- fs_escape_string(spec.description):trim() ~= "" then
46-
-- text = text .. " (" .. fs_escape_string(spec.description) .. ")"
45+
-- engine.formspec_escape(spec.description):trim() ~= "" then
46+
-- text = text .. " (" .. engine.formspec_escape(spec.description) .. ")"
4747
-- end
4848
else
4949
if spec.address ~= nil then
@@ -93,7 +93,7 @@ function menu.render_favorite(spec,render_details)
9393
string.format("%03d",spec.clients_max) .. " "
9494
end
9595

96-
return playercount .. fs_escape_string(details) .. text
96+
return playercount .. engine.formspec_escape(details) .. text
9797
end
9898

9999
--------------------------------------------------------------------------------
@@ -900,7 +900,7 @@ function tabbuilder.tab_multiplayer()
900900
if menu.fav_selected ~= nil and
901901
menu.favorites[menu.fav_selected].description ~= nil then
902902
retval = retval ..
903-
fs_escape_string(menu.favorites[menu.fav_selected].description,true)
903+
engine.formspec_escape(menu.favorites[menu.fav_selected].description,true)
904904
end
905905

906906
retval = retval ..
@@ -1040,7 +1040,7 @@ function tabbuilder.tab_TP()
10401040
menu.render_TP_list(TPlist) ..
10411041
";" .. index .. "]" ..
10421042
"image[0.65,0.25;4.0,3.7;"..(menu.TPscreen or no_screenshot).."]"..
1043-
"textarea[1.0,3.25;3.7,1.5;;"..(menu.TPinfo or "")..";]"
1043+
"textarea[1.0,3.25;3.7,1.5;;"..engine.formspec_escape(menu.TPinfo or "")..";]"
10441044
end
10451045

10461046
--------------------------------------------------------------------------------

‎builtin/misc.lua

-7
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,3 @@ function minetest.setting_get_pos(name)
9999
return minetest.string_to_pos(value)
100100
end
101101

102-
function minetest.formspec_escape(str)
103-
str = string.gsub(str, "\\", "\\\\")
104-
str = string.gsub(str, "%[", "\\[")
105-
str = string.gsub(str, "%]", "\\]")
106-
return str
107-
end
108-

‎builtin/misc_helpers.lua

+13-34
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ function cleanup_path(temppath)
184184
return temppath
185185
end
186186

187+
local tbl = engine or minetest
188+
function tbl.formspec_escape(text)
189+
if text ~= nil then
190+
text = string.gsub(text,"\\","\\\\")
191+
text = string.gsub(text,"%]","\\]")
192+
text = string.gsub(text,"%[","\\[")
193+
text = string.gsub(text,";","\\;")
194+
text = string.gsub(text,",","\\,")
195+
end
196+
return text
197+
end
198+
187199
--------------------------------------------------------------------------------
188200
-- mainmenu only functions
189201
--------------------------------------------------------------------------------
@@ -197,41 +209,7 @@ if engine ~= nil then
197209

198210
return nil
199211
end
200-
201-
--------------------------------------------------------------------------------
202-
function fs_escape_string(text)
203-
if text ~= nil then
204-
while (text:find("\r\n") ~= nil) do
205-
local newtext = text:sub(1,text:find("\r\n")-1)
206-
newtext = newtext .. " " .. text:sub(text:find("\r\n")+3)
207-
208-
text = newtext
209-
end
210-
211-
while (text:find("\n") ~= nil) do
212-
local newtext = text:sub(1,text:find("\n")-1)
213-
newtext = newtext .. " " .. text:sub(text:find("\n")+1)
214-
215-
text = newtext
216-
end
217-
218-
while (text:find("\r") ~= nil) do
219-
local newtext = text:sub(1,text:find("\r")-1)
220-
newtext = newtext .. " " .. text:sub(text:find("\r")+1)
221-
222-
text = newtext
223-
end
224-
225-
text = string.gsub(text,"\\","\\\\")
226-
text = string.gsub(text,"%]","\\]")
227-
text = string.gsub(text,"%[","\\[")
228-
text = string.gsub(text,";","\\;")
229-
text = string.gsub(text,",","\\,")
230-
end
231-
return text
232-
end
233212
end
234-
235213
--------------------------------------------------------------------------------
236214
-- core only fct
237215
--------------------------------------------------------------------------------
@@ -241,3 +219,4 @@ if minetest ~= nil then
241219
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
242220
end
243221
end
222+

‎builtin/modstore.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ function modstore.getmodlist(list)
233233

234234
--title + author
235235
retval = retval .."label[2.75," .. screenshot_ypos .. ";" ..
236-
fs_escape_string(details.title) .. " (" .. details.author .. ")]"
236+
engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
237237

238238
--description
239239
local descriptiony = screenshot_ypos + 0.5
240240
retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
241-
fs_escape_string(details.description) .. ";]"
241+
engine.formspec_escape(details.description) .. ";]"
242242
--rating
243243
local ratingy = screenshot_ypos + 0.6
244244
retval = retval .."label[10.1," .. ratingy .. ";Rating: " .. details.rating .."]"

‎src/guiFormSpecMenu.cpp

+23-40
Original file line numberDiff line numberDiff line change
@@ -143,51 +143,34 @@ int GUIFormSpecMenu::getListboxIndex(std::string listboxname) {
143143
return -1;
144144
}
145145

146-
std::vector<std::string> split(const std::string &s, char delim, bool escape=false) {
146+
std::vector<std::string> split(const std::string &s, char delim) {
147147
std::vector<std::string> tokens;
148148

149-
if (!escape) {
150-
int startpos = 0;
151-
size_t nextpos = s.find(delim);
152-
153-
while(nextpos != std::string::npos) {
154-
std::string toadd = s.substr(startpos,nextpos-startpos);
155-
tokens.push_back(toadd);
156-
startpos = nextpos+1;
157-
nextpos = s.find(delim,nextpos+1);
149+
std::string current = "";
150+
bool last_was_escape = false;
151+
for(unsigned int i=0; i < s.size(); i++) {
152+
if (last_was_escape) {
153+
current += '\\';
154+
current += s.c_str()[i];
155+
last_was_escape = false;
158156
}
159-
160-
//push last element
161-
tokens.push_back(s.substr(startpos));
162-
}
163-
else {
164-
std::string current = "";
165-
current += s.c_str()[0];
166-
bool last_was_escape = false;
167-
for(unsigned int i=1; i < s.size(); i++) {
168-
if (last_was_escape) {
169-
current += '\\';
170-
current += s.c_str()[i];
157+
else {
158+
if (s.c_str()[i] == delim) {
159+
tokens.push_back(current);
160+
current = "";
171161
last_was_escape = false;
172162
}
163+
else if (s.c_str()[i] == '\\'){
164+
last_was_escape = true;
165+
}
173166
else {
174-
if (s.c_str()[i] == delim) {
175-
tokens.push_back(current);
176-
current = "";
177-
last_was_escape = false;
178-
}
179-
else if (s.c_str()[i] == '\\'){
180-
last_was_escape = true;
181-
}
182-
else {
183-
current += s.c_str()[i];
184-
last_was_escape = false;
185-
}
167+
current += s.c_str()[i];
168+
last_was_escape = false;
186169
}
187170
}
188-
//push last element
189-
tokens.push_back(current);
190171
}
172+
//push last element
173+
tokens.push_back(current);
191174

192175
return tokens;
193176
}
@@ -518,7 +501,7 @@ void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
518501
std::vector<std::string> v_pos = split(parts[0],',');
519502
std::vector<std::string> v_geom = split(parts[1],',');
520503
std::string name = parts[2];
521-
std::vector<std::string> items = split(parts[3],',',true);
504+
std::vector<std::string> items = split(parts[3],',');
522505
std::string str_initial_selection = "";
523506
std::string str_transparent = "false";
524507

@@ -911,7 +894,7 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& p
911894
}
912895

913896
void GUIFormSpecMenu::parseField(parserData* data,std::string element,std::string type) {
914-
std::vector<std::string> parts = split(element,';',true);
897+
std::vector<std::string> parts = split(element,';');
915898

916899
if (parts.size() == 3) {
917900
parseSimpleField(data,parts);
@@ -1275,7 +1258,7 @@ void GUIFormSpecMenu::parseElement(parserData* data,std::string element) {
12751258
if (element == "")
12761259
return;
12771260

1278-
std::vector<std::string> parts = split(element,'[', true);
1261+
std::vector<std::string> parts = split(element,'[');
12791262

12801263
// ugly workaround to keep compatibility
12811264
if (parts.size() > 2) {
@@ -1428,7 +1411,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
14281411
m_boxes.clear();
14291412

14301413

1431-
std::vector<std::string> elements = split(m_formspec_string,']',true);
1414+
std::vector<std::string> elements = split(m_formspec_string,']');
14321415

14331416
for (unsigned int i=0;i< elements.size();i++) {
14341417
parseElement(&mydata,elements[i]);

0 commit comments

Comments
 (0)
Please sign in to comment.