From 5c10234dc5979486b7f589b1dad162a25f47a18c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 16 May 2014 11:57:49 +0200 Subject: [PATCH] Add luci patches to support string templates --- ...-field-mmap-to-the-more-generic-data.patch | 106 +++++++++++++ ...range-static-on-variable-declaration.patch | 17 +++ ...ing-templates-to-the-template-parser.patch | 142 ++++++++++++++++++ ...ng-templates-to-luci.template-module.patch | 67 +++++++++ 4 files changed, 332 insertions(+) create mode 100644 patches/packages/luci/0001-libs-web-rename-template_parser-field-mmap-to-the-more-generic-data.patch create mode 100644 patches/packages/luci/0002-libs-web-remove-strange-static-on-variable-declaration.patch create mode 100644 patches/packages/luci/0003-libs-web-add-support-for-string-templates-to-the-template-parser.patch create mode 100644 patches/packages/luci/0004-libs-web-add-support-for-string-templates-to-luci.template-module.patch diff --git a/patches/packages/luci/0001-libs-web-rename-template_parser-field-mmap-to-the-more-generic-data.patch b/patches/packages/luci/0001-libs-web-rename-template_parser-field-mmap-to-the-more-generic-data.patch new file mode 100644 index 00000000..a771e7b8 --- /dev/null +++ b/patches/packages/luci/0001-libs-web-rename-template_parser-field-mmap-to-the-more-generic-data.patch @@ -0,0 +1,106 @@ +From: Matthias Schiffer +Date: Fri, 16 May 2014 10:57:26 +0200 +Subject: libs/web: rename template_parser field "mmap" to the more generic "data" + +diff --git a/libs/web/src/template_parser.c b/libs/web/src/template_parser.c +index 6054451..fc8607b 100644 +--- a/libs/web/src/template_parser.c ++++ b/libs/web/src/template_parser.c +@@ -82,15 +82,15 @@ struct template_parser * template_open(const char *file) + goto err; + + parser->size = s.st_size; +- parser->mmap = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE, ++ parser->data = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE, + parser->fd, 0); + +- if (parser->mmap != MAP_FAILED) ++ if (parser->data != MAP_FAILED) + { +- parser->off = parser->mmap; ++ parser->off = parser->data; + parser->cur_chunk.type = T_TYPE_INIT; +- parser->cur_chunk.s = parser->mmap; +- parser->cur_chunk.e = parser->mmap; ++ parser->cur_chunk.s = parser->data; ++ parser->cur_chunk.e = parser->data; + + return parser; + } +@@ -108,8 +108,8 @@ void template_close(struct template_parser *parser) + if (parser->gc != NULL) + free(parser->gc); + +- if ((parser->mmap != NULL) && (parser->mmap != MAP_FAILED)) +- munmap(parser->mmap, parser->size); ++ if ((parser->data != NULL) && (parser->data != MAP_FAILED)) ++ munmap(parser->data, parser->size); + + if (parser->fd >= 0) + close(parser->fd); +@@ -121,7 +121,7 @@ void template_text(struct template_parser *parser, const char *e) + { + const char *s = parser->off; + +- if (s < (parser->mmap + parser->size)) ++ if (s < (parser->data + parser->size)) + { + if (parser->strip_after) + { +@@ -291,7 +291,7 @@ template_format_chunk(struct template_parser *parser, size_t *sz) + const char *template_reader(lua_State *L, void *ud, size_t *sz) + { + struct template_parser *parser = ud; +- int rem = parser->size - (parser->off - parser->mmap); ++ int rem = parser->size - (parser->off - parser->data); + char *tag; + + parser->prv_chunk = parser->cur_chunk; +@@ -314,8 +314,8 @@ const char *template_reader(lua_State *L, void *ud, size_t *sz) + } + else + { +- template_text(parser, parser->mmap + parser->size); +- parser->off = parser->mmap + parser->size; ++ template_text(parser, parser->data + parser->size); ++ parser->off = parser->data + parser->size; + } + } + +@@ -331,7 +331,7 @@ const char *template_reader(lua_State *L, void *ud, size_t *sz) + else + { + /* unexpected EOF */ +- template_code(parser, parser->mmap + parser->size); ++ template_code(parser, parser->data + parser->size); + + *sz = 1; + return "\033"; +@@ -366,12 +366,12 @@ int template_error(lua_State *L, struct template_parser *parser) + + if (strfind((char *)err, strlen(err), "'char(27)'", 10) != NULL) + { +- off = parser->mmap + parser->size; ++ off = parser->data + parser->size; + err = "'%>' expected before end of file"; + chunkline = 0; + } + +- for (ptr = parser->mmap; ptr < off; ptr++) ++ for (ptr = parser->data; ptr < off; ptr++) + if (*ptr == '\n') + line++; + +diff --git a/libs/web/src/template_parser.h b/libs/web/src/template_parser.h +index d1c6062..ad03cbc 100644 +--- a/libs/web/src/template_parser.h ++++ b/libs/web/src/template_parser.h +@@ -58,7 +58,7 @@ struct template_chunk { + struct template_parser { + int fd; + uint32_t size; +- char *mmap; ++ char *data; + char *off; + char *gc; + int line; diff --git a/patches/packages/luci/0002-libs-web-remove-strange-static-on-variable-declaration.patch b/patches/packages/luci/0002-libs-web-remove-strange-static-on-variable-declaration.patch new file mode 100644 index 00000000..4341e267 --- /dev/null +++ b/patches/packages/luci/0002-libs-web-remove-strange-static-on-variable-declaration.patch @@ -0,0 +1,17 @@ +From: Matthias Schiffer +Date: Fri, 16 May 2014 11:29:22 +0200 +Subject: libs/web: remove strange 'static' on variable declaration + +diff --git a/libs/web/src/template_parser.c b/libs/web/src/template_parser.c +index fc8607b..1aa5131 100644 +--- a/libs/web/src/template_parser.c ++++ b/libs/web/src/template_parser.c +@@ -66,7 +66,7 @@ static char *strfind(char *haystack, int hslen, const char *needle, int ndlen) + struct template_parser * template_open(const char *file) + { + struct stat s; +- static struct template_parser *parser; ++ struct template_parser *parser; + + if (!(parser = malloc(sizeof(*parser)))) + goto err; diff --git a/patches/packages/luci/0003-libs-web-add-support-for-string-templates-to-the-template-parser.patch b/patches/packages/luci/0003-libs-web-add-support-for-string-templates-to-the-template-parser.patch new file mode 100644 index 00000000..e1efe487 --- /dev/null +++ b/patches/packages/luci/0003-libs-web-add-support-for-string-templates-to-the-template-parser.patch @@ -0,0 +1,142 @@ +From: Matthias Schiffer +Date: Fri, 16 May 2014 11:37:21 +0200 +Subject: libs/web: add support for string templates to the template parser + +diff --git a/libs/web/src/template_lualib.c b/libs/web/src/template_lualib.c +index 0d43641..1035611 100644 +--- a/libs/web/src/template_lualib.c ++++ b/libs/web/src/template_lualib.c +@@ -18,10 +18,8 @@ + + #include "template_lualib.h" + +-int template_L_parse(lua_State *L) ++static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname) + { +- const char *file = luaL_checkstring(L, 1); +- struct template_parser *parser = template_open(file); + int lua_status, rv; + + if (!parser) +@@ -32,7 +30,7 @@ int template_L_parse(lua_State *L) + return 3; + } + +- lua_status = lua_load(L, template_reader, parser, file); ++ lua_status = lua_load(L, template_reader, parser, chunkname); + + if (lua_status == 0) + rv = 1; +@@ -44,6 +42,23 @@ int template_L_parse(lua_State *L) + return rv; + } + ++int template_L_parse(lua_State *L) ++{ ++ const char *file = luaL_checkstring(L, 1); ++ struct template_parser *parser = template_open(file); ++ ++ return template_L_do_parse(L, parser, file); ++} ++ ++int template_L_parse_string(lua_State *L) ++{ ++ size_t len; ++ const char *str = luaL_checklstring(L, 1, &len); ++ struct template_parser *parser = template_string(str, len); ++ ++ return template_L_do_parse(L, parser, "[string]"); ++} ++ + int template_L_utf8(lua_State *L) + { + size_t len = 0; +@@ -146,6 +161,7 @@ static int template_L_hash(lua_State *L) { + /* module table */ + static const luaL_reg R[] = { + { "parse", template_L_parse }, ++ { "parse_string", template_L_parse_string }, + { "utf8", template_L_utf8 }, + { "pcdata", template_L_pcdata }, + { "striptags", template_L_striptags }, +diff --git a/libs/web/src/template_parser.c b/libs/web/src/template_parser.c +index 1aa5131..c263fbf 100644 +--- a/libs/web/src/template_parser.c ++++ b/libs/web/src/template_parser.c +@@ -100,6 +100,36 @@ err: + return NULL; + } + ++struct template_parser * template_string(const char *str, uint32_t len) ++{ ++ struct template_parser *parser; ++ ++ if (!str) { ++ errno = EINVAL; ++ goto err; ++ } ++ ++ if (!(parser = malloc(sizeof(*parser)))) ++ goto err; ++ ++ memset(parser, 0, sizeof(*parser)); ++ parser->fd = -1; ++ ++ parser->size = len; ++ parser->data = (char*)str; ++ ++ parser->off = parser->data; ++ parser->cur_chunk.type = T_TYPE_INIT; ++ parser->cur_chunk.s = parser->data; ++ parser->cur_chunk.e = parser->data; ++ ++ return parser; ++ ++err: ++ template_close(parser); ++ return NULL; ++} ++ + void template_close(struct template_parser *parser) + { + if (!parser) +@@ -108,11 +138,14 @@ void template_close(struct template_parser *parser) + if (parser->gc != NULL) + free(parser->gc); + +- if ((parser->data != NULL) && (parser->data != MAP_FAILED)) +- munmap(parser->data, parser->size); ++ /* if file is not set, we were parsing a string */ ++ if (parser->file) { ++ if ((parser->data != NULL) && (parser->data != MAP_FAILED)) ++ munmap(parser->data, parser->size); + +- if (parser->fd >= 0) +- close(parser->fd); ++ if (parser->fd >= 0) ++ close(parser->fd); ++ } + + free(parser); + } +@@ -376,7 +409,7 @@ int template_error(lua_State *L, struct template_parser *parser) + line++; + + snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s", +- parser->file, line + chunkline, err ? err : "(unknown error)"); ++ parser->file ? parser->file : "[string]", line + chunkline, err ? err : "(unknown error)"); + + lua_pushnil(L); + lua_pushinteger(L, line + chunkline); +diff --git a/libs/web/src/template_parser.h b/libs/web/src/template_parser.h +index ad03cbc..a3200a2 100644 +--- a/libs/web/src/template_parser.h ++++ b/libs/web/src/template_parser.h +@@ -71,6 +71,7 @@ struct template_parser { + }; + + struct template_parser * template_open(const char *file); ++struct template_parser * template_string(const char *str, uint32_t len); + void template_close(struct template_parser *parser); + + const char *template_reader(lua_State *L, void *ud, size_t *sz); diff --git a/patches/packages/luci/0004-libs-web-add-support-for-string-templates-to-luci.template-module.patch b/patches/packages/luci/0004-libs-web-add-support-for-string-templates-to-luci.template-module.patch new file mode 100644 index 00000000..e767af23 --- /dev/null +++ b/patches/packages/luci/0004-libs-web-add-support-for-string-templates-to-luci.template-module.patch @@ -0,0 +1,67 @@ +From: Matthias Schiffer +Date: Fri, 16 May 2014 11:48:42 +0200 +Subject: libs/web: add support for string templates to luci.template module + +diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua +index 72127d1..ea01d3c 100644 +--- a/libs/web/luasrc/template.lua ++++ b/libs/web/luasrc/template.lua +@@ -50,6 +50,13 @@ function render(name, scope) + return Template(name):render(scope or getfenv(2)) + end + ++--- Render a template from a string. ++-- @param template Template string ++-- @param scope Scope to assign to template (optional) ++function render_string(template, scope) ++ return Template(nil, template):render(scope or getfenv(2)) ++end ++ + + -- Template class + Template = util.class() +@@ -59,11 +66,14 @@ Template.cache = setmetatable({}, {__mode = "v"}) + + + -- Constructor - Reads and compiles the template on-demand +-function Template.__init__(self, name) ++function Template.__init__(self, name, template) ++ if name then ++ self.template = self.cache[name] ++ self.name = name ++ else ++ self.name = "[string]" ++ end + +- self.template = self.cache[name] +- self.name = name +- + -- Create a new namespace for this template + self.viewns = context.viewns + +@@ -72,16 +82,22 @@ function Template.__init__(self, name) + + -- Compile template + local err +- local sourcefile = viewdir .. "/" .. name .. ".htm" ++ local sourcefile + +- self.template, _, err = tparser.parse(sourcefile) ++ if name then ++ sourcefile = viewdir .. "/" .. name .. ".htm" ++ self.template, _, err = tparser.parse(sourcefile) ++ else ++ sourcefile = "[string]" ++ self.template, _, err = tparser.parse_string(template) ++ end + + -- If we have no valid template throw error, otherwise cache the template + if not self.template then + error("Failed to load template '" .. name .. "'.\n" .. + "Error while parsing template '" .. sourcefile .. "':\n" .. + (err or "Unknown syntax error")) +- else ++ elseif name then + self.cache[name] = self.template + end + end