summaryrefslogtreecommitdiffhomepage
path: root/modules/base/src
diff options
context:
space:
mode:
Diffstat (limited to 'modules/base/src')
-rw-r--r--modules/base/src/po2lmo.c247
-rw-r--r--modules/base/src/template_lmo.c328
-rw-r--r--modules/base/src/template_lmo.h92
-rw-r--r--modules/base/src/template_lualib.c163
-rw-r--r--modules/base/src/template_lualib.h30
-rw-r--r--modules/base/src/template_parser.c386
-rw-r--r--modules/base/src/template_parser.h79
-rw-r--r--modules/base/src/template_utils.c494
-rw-r--r--modules/base/src/template_utils.h49
9 files changed, 1868 insertions, 0 deletions
diff --git a/modules/base/src/po2lmo.c b/modules/base/src/po2lmo.c
new file mode 100644
index 000000000..0da792b68
--- /dev/null
+++ b/modules/base/src/po2lmo.c
@@ -0,0 +1,247 @@
+/*
+ * lmo - Lua Machine Objects - PO to LMO conversion tool
+ *
+ * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "template_lmo.h"
+
+static void die(const char *msg)
+{
+ fprintf(stderr, "Error: %s\n", msg);
+ exit(1);
+}
+
+static void usage(const char *name)
+{
+ fprintf(stderr, "Usage: %s input.po output.lmo\n", name);
+ exit(1);
+}
+
+static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ if( fwrite(ptr, size, nmemb, stream) == 0 )
+ die("Failed to write stdout");
+}
+
+static int extract_string(const char *src, char *dest, int len)
+{
+ int pos = 0;
+ int esc = 0;
+ int off = -1;
+
+ for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ )
+ {
+ if( (off == -1) && (src[pos] == '"') )
+ {
+ off = pos + 1;
+ }
+ else if( off >= 0 )
+ {
+ if( esc == 1 )
+ {
+ switch (src[pos])
+ {
+ case '"':
+ case '\\':
+ off++;
+ break;
+ }
+ dest[pos-off] = src[pos];
+ esc = 0;
+ }
+ else if( src[pos] == '\\' )
+ {
+ dest[pos-off] = src[pos];
+ esc = 1;
+ }
+ else if( src[pos] != '"' )
+ {
+ dest[pos-off] = src[pos];
+ }
+ else
+ {
+ dest[pos-off] = '\0';
+ break;
+ }
+ }
+ }
+
+ return (off > -1) ? strlen(dest) : -1;
+}
+
+static int cmp_index(const void *a, const void *b)
+{
+ uint32_t x = ((const lmo_entry_t *)a)->key_id;
+ uint32_t y = ((const lmo_entry_t *)b)->key_id;
+
+ if (x < y)
+ return -1;
+ else if (x > y)
+ return 1;
+
+ return 0;
+}
+
+static void print_uint32(uint32_t x, FILE *out)
+{
+ uint32_t y = htonl(x);
+ print(&y, sizeof(uint32_t), 1, out);
+}
+
+static void print_index(void *array, int n, FILE *out)
+{
+ lmo_entry_t *e;
+
+ qsort(array, n, sizeof(*e), cmp_index);
+
+ for (e = array; n > 0; n--, e++)
+ {
+ print_uint32(e->key_id, out);
+ print_uint32(e->val_id, out);
+ print_uint32(e->offset, out);
+ print_uint32(e->length, out);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ char line[4096];
+ char key[4096];
+ char val[4096];
+ char tmp[4096];
+ int state = 0;
+ int offset = 0;
+ int length = 0;
+ int n_entries = 0;
+ void *array = NULL;
+ lmo_entry_t *entry = NULL;
+ uint32_t key_id, val_id;
+
+ FILE *in;
+ FILE *out;
+
+ if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) )
+ usage(argv[0]);
+
+ memset(line, 0, sizeof(key));
+ memset(key, 0, sizeof(val));
+ memset(val, 0, sizeof(val));
+
+ while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) )
+ {
+ if( state == 0 && strstr(line, "msgid \"") == line )
+ {
+ switch(extract_string(line, key, sizeof(key)))
+ {
+ case -1:
+ die("Syntax error in msgid");
+ case 0:
+ state = 1;
+ break;
+ default:
+ state = 2;
+ }
+ }
+ else if( state == 1 || state == 2 )
+ {
+ if( strstr(line, "msgstr \"") == line || state == 2 )
+ {
+ switch(extract_string(line, val, sizeof(val)))
+ {
+ case -1:
+ state = 4;
+ break;
+ default:
+ state = 3;
+ }
+ }
+ else
+ {
+ switch(extract_string(line, tmp, sizeof(tmp)))
+ {
+ case -1:
+ state = 2;
+ break;
+ default:
+ strcat(key, tmp);
+ }
+ }
+ }
+ else if( state == 3 )
+ {
+ switch(extract_string(line, tmp, sizeof(tmp)))
+ {
+ case -1:
+ state = 4;
+ break;
+ default:
+ strcat(val, tmp);
+ }
+ }
+
+ if( state == 4 )
+ {
+ if( strlen(key) > 0 && strlen(val) > 0 )
+ {
+ key_id = sfh_hash(key, strlen(key));
+ val_id = sfh_hash(val, strlen(val));
+
+ if( key_id != val_id )
+ {
+ n_entries++;
+ array = realloc(array, n_entries * sizeof(lmo_entry_t));
+ entry = (lmo_entry_t *)array + n_entries - 1;
+
+ if (!array)
+ die("Out of memory");
+
+ entry->key_id = key_id;
+ entry->val_id = val_id;
+ entry->offset = offset;
+ entry->length = strlen(val);
+
+ length = strlen(val) + ((4 - (strlen(val) % 4)) % 4);
+
+ print(val, length, 1, out);
+ offset += length;
+ }
+ }
+
+ state = 0;
+ memset(key, 0, sizeof(key));
+ memset(val, 0, sizeof(val));
+ }
+
+ memset(line, 0, sizeof(line));
+ }
+
+ print_index(array, n_entries, out);
+
+ if( offset > 0 )
+ {
+ print_uint32(offset, out);
+ fsync(fileno(out));
+ fclose(out);
+ }
+ else
+ {
+ fclose(out);
+ unlink(argv[2]);
+ }
+
+ fclose(in);
+ return(0);
+}
diff --git a/modules/base/src/template_lmo.c b/modules/base/src/template_lmo.c
new file mode 100644
index 000000000..27205a722
--- /dev/null
+++ b/modules/base/src/template_lmo.c
@@ -0,0 +1,328 @@
+/*
+ * lmo - Lua Machine Objects - Base functions
+ *
+ * Copyright (C) 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "template_lmo.h"
+
+/*
+ * Hash function from http://www.azillionmonkeys.com/qed/hash.html
+ * Copyright (C) 2004-2008 by Paul Hsieh
+ */
+
+uint32_t sfh_hash(const char *data, int len)
+{
+ uint32_t hash = len, tmp;
+ int rem;
+
+ if (len <= 0 || data == NULL) return 0;
+
+ rem = len & 3;
+ len >>= 2;
+
+ /* Main loop */
+ for (;len > 0; len--) {
+ hash += sfh_get16(data);
+ tmp = (sfh_get16(data+2) << 11) ^ hash;
+ hash = (hash << 16) ^ tmp;
+ data += 2*sizeof(uint16_t);
+ hash += hash >> 11;
+ }
+
+ /* Handle end cases */
+ switch (rem) {
+ case 3: hash += sfh_get16(data);
+ hash ^= hash << 16;
+ hash ^= data[sizeof(uint16_t)] << 18;
+ hash += hash >> 11;
+ break;
+ case 2: hash += sfh_get16(data);
+ hash ^= hash << 11;
+ hash += hash >> 17;
+ break;
+ case 1: hash += *data;
+ hash ^= hash << 10;
+ hash += hash >> 1;
+ }
+
+ /* Force "avalanching" of final 127 bits */
+ hash ^= hash << 3;
+ hash += hash >> 5;
+ hash ^= hash << 4;
+ hash += hash >> 17;
+ hash ^= hash << 25;
+ hash += hash >> 6;
+
+ return hash;
+}
+
+uint32_t lmo_canon_hash(const char *str, int len)
+{
+ char res[4096];
+ char *ptr, prev;
+ int off;
+
+ if (!str || len >= sizeof(res))
+ return 0;
+
+ for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++)
+ {
+ if (isspace(*str))
+ {
+ if (!isspace(prev))
+ *ptr++ = ' ';
+ }
+ else
+ {
+ *ptr++ = *str;
+ }
+ }
+
+ if ((ptr > res) && isspace(*(ptr-1)))
+ ptr--;
+
+ return sfh_hash(res, ptr - res);
+}
+
+lmo_archive_t * lmo_open(const char *file)
+{
+ int in = -1;
+ uint32_t idx_offset = 0;
+ struct stat s;
+
+ lmo_archive_t *ar = NULL;
+
+ if (stat(file, &s) == -1)
+ goto err;
+
+ if ((in = open(file, O_RDONLY)) == -1)
+ goto err;
+
+ if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL)
+ {
+ memset(ar, 0, sizeof(*ar));
+
+ ar->fd = in;
+ ar->size = s.st_size;
+
+ fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC);
+
+ if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED)
+ goto err;
+
+ idx_offset = ntohl(*((const uint32_t *)
+ (ar->mmap + ar->size - sizeof(uint32_t))));
+
+ if (idx_offset >= ar->size)
+ goto err;
+
+ ar->index = (lmo_entry_t *)(ar->mmap + idx_offset);
+ ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t);
+ ar->end = ar->mmap + ar->size;
+
+ return ar;
+ }
+
+err:
+ if (in > -1)
+ close(in);
+
+ if (ar != NULL)
+ {
+ if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
+ munmap(ar->mmap, ar->size);
+
+ free(ar);
+ }
+
+ return NULL;
+}
+
+void lmo_close(lmo_archive_t *ar)
+{
+ if (ar != NULL)
+ {
+ if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED))
+ munmap(ar->mmap, ar->size);
+
+ close(ar->fd);
+ free(ar);
+
+ ar = NULL;
+ }
+}
+
+
+lmo_catalog_t *_lmo_catalogs = NULL;
+lmo_catalog_t *_lmo_active_catalog = NULL;
+
+int lmo_load_catalog(const char *lang, const char *dir)
+{
+ DIR *dh = NULL;
+ char pattern[16];
+ char path[PATH_MAX];
+ struct dirent *de = NULL;
+
+ lmo_archive_t *ar = NULL;
+ lmo_catalog_t *cat = NULL;
+
+ if (!lmo_change_catalog(lang))
+ return 0;
+
+ if (!dir || !(dh = opendir(dir)))
+ goto err;
+
+ if (!(cat = malloc(sizeof(*cat))))
+ goto err;
+
+ memset(cat, 0, sizeof(*cat));
+
+ snprintf(cat->lang, sizeof(cat->lang), "%s", lang);
+ snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang);
+
+ while ((de = readdir(dh)) != NULL)
+ {
+ if (!fnmatch(pattern, de->d_name, 0))
+ {
+ snprintf(path, sizeof(path), "%s/%s", dir, de->d_name);
+ ar = lmo_open(path);
+
+ if (ar)
+ {
+ ar->next = cat->archives;
+ cat->archives = ar;
+ }
+ }
+ }
+
+ closedir(dh);
+
+ cat->next = _lmo_catalogs;
+ _lmo_catalogs = cat;
+
+ if (!_lmo_active_catalog)
+ _lmo_active_catalog = cat;
+
+ return 0;
+
+err:
+ if (dh) closedir(dh);
+ if (cat) free(cat);
+
+ return -1;
+}
+
+int lmo_change_catalog(const char *lang)
+{
+ lmo_catalog_t *cat;
+
+ for (cat = _lmo_catalogs; cat; cat = cat->next)
+ {
+ if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
+ {
+ _lmo_active_catalog = cat;
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash)
+{
+ unsigned int m, l, r;
+ uint32_t k;
+
+ l = 0;
+ r = ar->length - 1;
+
+ while (1)
+ {
+ m = l + ((r - l) / 2);
+
+ if (r < l)
+ break;
+
+ k = ntohl(ar->index[m].key_id);
+
+ if (k == hash)
+ return &ar->index[m];
+
+ if (k > hash)
+ {
+ if (!m)
+ break;
+
+ r = m - 1;
+ }
+ else
+ {
+ l = m + 1;
+ }
+ }
+
+ return NULL;
+}
+
+int lmo_translate(const char *key, int keylen, char **out, int *outlen)
+{
+ uint32_t hash;
+ lmo_entry_t *e;
+ lmo_archive_t *ar;
+
+ if (!key || !_lmo_active_catalog)
+ return -2;
+
+ hash = lmo_canon_hash(key, keylen);
+
+ for (ar = _lmo_active_catalog->archives; ar; ar = ar->next)
+ {
+ if ((e = lmo_find_entry(ar, hash)) != NULL)
+ {
+ *out = ar->mmap + ntohl(e->offset);
+ *outlen = ntohl(e->length);
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+void lmo_close_catalog(const char *lang)
+{
+ lmo_archive_t *ar, *next;
+ lmo_catalog_t *cat, *prev;
+
+ for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next)
+ {
+ if (!strncmp(cat->lang, lang, sizeof(cat->lang)))
+ {
+ if (prev)
+ prev->next = cat->next;
+ else
+ _lmo_catalogs = cat->next;
+
+ for (ar = cat->archives; ar; ar = next)
+ {
+ next = ar->next;
+ lmo_close(ar);
+ }
+
+ free(cat);
+ break;
+ }
+ }
+}
diff --git a/modules/base/src/template_lmo.h b/modules/base/src/template_lmo.h
new file mode 100644
index 000000000..57f59aa56
--- /dev/null
+++ b/modules/base/src/template_lmo.h
@@ -0,0 +1,92 @@
+/*
+ * lmo - Lua Machine Objects - General header
+ *
+ * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _TEMPLATE_LMO_H_
+#define _TEMPLATE_LMO_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fnmatch.h>
+#include <dirent.h>
+#include <ctype.h>
+#include <limits.h>
+
+#if (defined(__GNUC__) && defined(__i386__))
+#define sfh_get16(d) (*((const uint16_t *) (d)))
+#else
+#define sfh_get16(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
+ +(uint32_t)(((const uint8_t *)(d))[0]) )
+#endif
+
+
+struct lmo_entry {
+ uint32_t key_id;
+ uint32_t val_id;
+ uint32_t offset;
+ uint32_t length;
+} __attribute__((packed));
+
+typedef struct lmo_entry lmo_entry_t;
+
+
+struct lmo_archive {
+ int fd;
+ int length;
+ uint32_t size;
+ lmo_entry_t *index;
+ char *mmap;
+ char *end;
+ struct lmo_archive *next;
+};
+
+typedef struct lmo_archive lmo_archive_t;
+
+
+struct lmo_catalog {
+ char lang[6];
+ struct lmo_archive *archives;
+ struct lmo_catalog *next;
+};
+
+typedef struct lmo_catalog lmo_catalog_t;
+
+
+uint32_t sfh_hash(const char *data, int len);
+uint32_t lmo_canon_hash(const char *data, int len);
+
+lmo_archive_t * lmo_open(const char *file);
+void lmo_close(lmo_archive_t *ar);
+
+
+extern lmo_catalog_t *_lmo_catalogs;
+extern lmo_catalog_t *_lmo_active_catalog;
+
+int lmo_load_catalog(const char *lang, const char *dir);
+int lmo_change_catalog(const char *lang);
+int lmo_translate(const char *key, int keylen, char **out, int *outlen);
+void lmo_close_catalog(const char *lang);
+
+#endif
diff --git a/modules/base/src/template_lualib.c b/modules/base/src/template_lualib.c
new file mode 100644
index 000000000..0d4364104
--- /dev/null
+++ b/modules/base/src/template_lualib.c
@@ -0,0 +1,163 @@
+/*
+ * LuCI Template - Lua binding
+ *
+ * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "template_lualib.h"
+
+int template_L_parse(lua_State *L)
+{
+ const char *file = luaL_checkstring(L, 1);
+ struct template_parser *parser = template_open(file);
+ int lua_status, rv;
+
+ if (!parser)
+ {
+ lua_pushnil(L);
+ lua_pushinteger(L, errno);
+ lua_pushstring(L, strerror(errno));
+ return 3;
+ }
+
+ lua_status = lua_load(L, template_reader, parser, file);
+
+ if (lua_status == 0)
+ rv = 1;
+ else
+ rv = template_error(L, parser);
+
+ template_close(parser);
+
+ return rv;
+}
+
+int template_L_utf8(lua_State *L)
+{
+ size_t len = 0;
+ const char *str = luaL_checklstring(L, 1, &len);
+ char *res = utf8(str, len);
+
+ if (res != NULL)
+ {
+ lua_pushstring(L, res);
+ free(res);
+
+ return 1;
+ }
+
+ return 0;
+}
+
+int template_L_pcdata(lua_State *L)
+{
+ size_t len = 0;
+ const char *str = luaL_checklstring(L, 1, &len);
+ char *res = pcdata(str, len);
+
+ if (res != NULL)
+ {
+ lua_pushstring(L, res);
+ free(res);
+
+ return 1;
+ }
+
+ return 0;
+}
+
+int template_L_striptags(lua_State *L)
+{
+ size_t len = 0;
+ const char *str = luaL_checklstring(L, 1, &len);
+ char *res = striptags(str, len);
+
+ if (res != NULL)
+ {
+ lua_pushstring(L, res);
+ free(res);
+
+ return 1;
+ }
+
+ return 0;
+}
+
+static int template_L_load_catalog(lua_State *L) {
+ const char *lang = luaL_optstring(L, 1, "en");
+ const char *dir = luaL_optstring(L, 2, NULL);
+ lua_pushboolean(L, !lmo_load_catalog(lang, dir));
+ return 1;
+}
+
+static int template_L_close_catalog(lua_State *L) {
+ const char *lang = luaL_optstring(L, 1, "en");
+ lmo_close_catalog(lang);
+ return 0;
+}
+
+static int template_L_change_catalog(lua_State *L) {
+ const char *lang = luaL_optstring(L, 1, "en");
+ lua_pushboolean(L, !lmo_change_catalog(lang));
+ return 1;
+}
+
+static int template_L_translate(lua_State *L) {
+ size_t len;
+ char *tr;
+ int trlen;
+ const char *key = luaL_checklstring(L, 1, &len);
+
+ switch (lmo_translate(key, len, &tr, &trlen))
+ {
+ case 0:
+ lua_pushlstring(L, tr, trlen);
+ return 1;
+
+ case -1:
+ return 0;
+ }
+
+ lua_pushnil(L);
+ lua_pushstring(L, "no catalog loaded");
+ return 2;
+}
+
+static int template_L_hash(lua_State *L) {
+ size_t len;
+ const char *key = luaL_checklstring(L, 1, &len);
+ lua_pushinteger(L, sfh_hash(key, len));
+ return 1;
+}
+
+
+/* module table */
+static const luaL_reg R[] = {
+ { "parse", template_L_parse },
+ { "utf8", template_L_utf8 },
+ { "pcdata", template_L_pcdata },
+ { "striptags", template_L_striptags },
+ { "load_catalog", template_L_load_catalog },
+ { "close_catalog", template_L_close_catalog },
+ { "change_catalog", template_L_change_catalog },
+ { "translate", template_L_translate },
+ { "hash", template_L_hash },
+ { NULL, NULL }
+};
+
+LUALIB_API int luaopen_luci_template_parser(lua_State *L) {
+ luaL_register(L, TEMPLATE_LUALIB_META, R);
+ return 1;
+}
diff --git a/modules/base/src/template_lualib.h b/modules/base/src/template_lualib.h
new file mode 100644
index 000000000..1b659be12
--- /dev/null
+++ b/modules/base/src/template_lualib.h
@@ -0,0 +1,30 @@
+/*
+ * LuCI Template - Lua library header
+ *
+ * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _TEMPLATE_LUALIB_H_
+#define _TEMPLATE_LUALIB_H_
+
+#include "template_parser.h"
+#include "template_utils.h"
+#include "template_lmo.h"
+
+#define TEMPLATE_LUALIB_META "template.parser"
+
+LUALIB_API int luaopen_luci_template_parser(lua_State *L);
+
+#endif
diff --git a/modules/base/src/template_parser.c b/modules/base/src/template_parser.c
new file mode 100644
index 000000000..605445131
--- /dev/null
+++ b/modules/base/src/template_parser.c
@@ -0,0 +1,386 @@
+/*
+ * LuCI Template - Parser implementation
+ *
+ * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "template_parser.h"
+#include "template_utils.h"
+#include "template_lmo.h"
+
+
+/* leading and trailing code for different types */
+const char *gen_code[9][2] = {
+ { NULL, NULL },
+ { "write(\"", "\")" },
+ { NULL, NULL },
+ { "write(tostring(", " or \"\"))" },
+ { "include(\"", "\")" },
+ { "write(\"", "\")" },
+ { "write(\"", "\")" },
+ { NULL, " " },
+ { NULL, NULL },
+};
+
+/* Simple strstr() like function that takes len arguments for both haystack and needle. */
+static char *strfind(char *haystack, int hslen, const char *needle, int ndlen)
+{
+ int match = 0;
+ int i, j;
+
+ for( i = 0; i < hslen; i++ )
+ {
+ if( haystack[i] == needle[0] )
+ {
+ match = ((ndlen == 1) || ((i + ndlen) <= hslen));
+
+ for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ )
+ {
+ if( haystack[i+j] != needle[j] )
+ {
+ match = 0;
+ break;
+ }
+ }
+
+ if( match )
+ return &haystack[i];
+ }
+ }
+
+ return NULL;
+}
+
+struct template_parser * template_open(const char *file)
+{
+ struct stat s;
+ static struct template_parser *parser;
+
+ if (!(parser = malloc(sizeof(*parser))))
+ goto err;
+
+ memset(parser, 0, sizeof(*parser));
+ parser->fd = -1;
+ parser->file = file;
+
+ if (stat(file, &s))
+ goto err;
+
+ if ((parser->fd = open(file, O_RDONLY)) < 0)
+ goto err;
+
+ parser->size = s.st_size;
+ parser->mmap = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE,
+ parser->fd, 0);
+
+ if (parser->mmap != MAP_FAILED)
+ {
+ parser->off = parser->mmap;
+ parser->cur_chunk.type = T_TYPE_INIT;
+ parser->cur_chunk.s = parser->mmap;
+ parser->cur_chunk.e = parser->mmap;
+
+ return parser;
+ }
+
+err:
+ template_close(parser);
+ return NULL;
+}
+
+void template_close(struct template_parser *parser)
+{
+ if (!parser)
+ return;
+
+ if (parser->gc != NULL)
+ free(parser->gc);
+
+ if ((parser->mmap != NULL) && (parser->mmap != MAP_FAILED))
+ munmap(parser->mmap, parser->size);
+
+ if (parser->fd >= 0)
+ close(parser->fd);
+
+ free(parser);
+}
+
+void template_text(struct template_parser *parser, const char *e)
+{
+ const char *s = parser->off;
+
+ if (s < (parser->mmap + parser->size))
+ {
+ if (parser->strip_after)
+ {
+ while ((s <= e) && isspace(*s))
+ s++;
+ }
+
+ parser->cur_chunk.type = T_TYPE_TEXT;
+ }
+ else
+ {
+ parser->cur_chunk.type = T_TYPE_EOF;
+ }
+
+ parser->cur_chunk.line = parser->line;
+ parser->cur_chunk.s = s;
+ parser->cur_chunk.e = e;
+}
+
+void template_code(struct template_parser *parser, const char *e)
+{
+ const char *s = parser->off;
+
+ parser->strip_before = 0;
+ parser->strip_after = 0;
+
+ if (*s == '-')
+ {
+ parser->strip_before = 1;
+ for (s++; (s <= e) && (*s == ' ' || *s == '\t'); s++);
+ }
+
+ if (*(e-1) == '-')
+ {
+ parser->strip_after = 1;
+ for (e--; (e >= s) && (*e == ' ' || *e == '\t'); e--);
+ }
+
+ switch (*s)
+ {
+ /* comment */
+ case '#':
+ s++;
+ parser->cur_chunk.type = T_TYPE_COMMENT;
+ break;
+
+ /* include */
+ case '+':
+ s++;
+ parser->cur_chunk.type = T_TYPE_INCLUDE;
+ break;
+
+ /* translate */
+ case ':':
+ s++;
+ parser->cur_chunk.type = T_TYPE_I18N;
+ break;
+
+ /* translate raw */
+ case '_':
+ s++;
+ parser->cur_chunk.type = T_TYPE_I18N_RAW;
+ break;
+
+ /* expr */
+ case '=':
+ s++;
+ parser->cur_chunk.type = T_TYPE_EXPR;
+ break;
+
+ /* code */
+ default:
+ parser->cur_chunk.type = T_TYPE_CODE;
+ break;
+ }
+
+ parser->cur_chunk.line = parser->line;
+ parser->cur_chunk.s = s;
+ parser->cur_chunk.e = e;
+}
+
+static const char *
+template_format_chunk(struct template_parser *parser, size_t *sz)
+{
+ const char *s, *p;
+ const char *head, *tail;
+ struct template_chunk *c = &parser->prv_chunk;
+ struct template_buffer *buf;
+
+ *sz = 0;
+ s = parser->gc = NULL;
+
+ if (parser->strip_before && c->type == T_TYPE_TEXT)
+ {
+ while ((c->e > c->s) && isspace(*(c->e - 1)))
+ c->e--;
+ }
+
+ /* empty chunk */
+ if (c->s == c->e)
+ {
+ if (c->type == T_TYPE_EOF)
+ {
+ *sz = 0;
+ s = NULL;
+ }
+ else
+ {
+ *sz = 1;
+ s = " ";
+ }
+ }
+
+ /* format chunk */
+ else if ((buf = buf_init(c->e - c->s)) != NULL)
+ {
+ if ((head = gen_code[c->type][0]) != NULL)
+ buf_append(buf, head, strlen(head));
+
+ switch (c->type)
+ {
+ case T_TYPE_TEXT:
+ luastr_escape(buf, c->s, c->e - c->s, 0);
+ break;
+
+ case T_TYPE_EXPR:
+ buf_append(buf, c->s, c->e - c->s);
+ for (p = c->s; p < c->e; p++)
+ parser->line += (*p == '\n');
+ break;
+
+ case T_TYPE_INCLUDE:
+ luastr_escape(buf, c->s, c->e - c->s, 0);
+ break;
+
+ case T_TYPE_I18N:
+ luastr_translate(buf, c->s, c->e - c->s, 1);
+ break;
+
+ case T_TYPE_I18N_RAW:
+ luastr_translate(buf, c->s, c->e - c->s, 0);
+ break;
+
+ case T_TYPE_CODE:
+ buf_append(buf, c->s, c->e - c->s);
+ for (p = c->s; p < c->e; p++)
+ parser->line += (*p == '\n');
+ break;
+ }
+
+ if ((tail = gen_code[c->type][1]) != NULL)
+ buf_append(buf, tail, strlen(tail));
+
+ *sz = buf_length(buf);
+ s = parser->gc = buf_destroy(buf);
+
+ if (!*sz)
+ {
+ *sz = 1;
+ s = " ";
+ }
+ }
+
+ return s;
+}
+
+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);
+ char *tag;
+
+ parser->prv_chunk = parser->cur_chunk;
+
+ /* free previous string */
+ if (parser->gc)
+ {
+ free(parser->gc);
+ parser->gc = NULL;
+ }
+
+ /* before tag */
+ if (!parser->in_expr)
+ {
+ if ((tag = strfind(parser->off, rem, "<%", 2)) != NULL)
+ {
+ template_text(parser, tag);
+ parser->off = tag + 2;
+ parser->in_expr = 1;
+ }
+ else
+ {
+ template_text(parser, parser->mmap + parser->size);
+ parser->off = parser->mmap + parser->size;
+ }
+ }
+
+ /* inside tag */
+ else
+ {
+ if ((tag = strfind(parser->off, rem, "%>", 2)) != NULL)
+ {
+ template_code(parser, tag);
+ parser->off = tag + 2;
+ parser->in_expr = 0;
+ }
+ else
+ {
+ /* unexpected EOF */
+ template_code(parser, parser->mmap + parser->size);
+
+ *sz = 1;
+ return "\033";
+ }
+ }
+
+ return template_format_chunk(parser, sz);
+}
+
+int template_error(lua_State *L, struct template_parser *parser)
+{
+ const char *err = luaL_checkstring(L, -1);
+ const char *off = parser->prv_chunk.s;
+ const char *ptr;
+ char msg[1024];
+ int line = 0;
+ int chunkline = 0;
+
+ if ((ptr = strfind((char *)err, strlen(err), "]:", 2)) != NULL)
+ {
+ chunkline = atoi(ptr + 2) - parser->prv_chunk.line;
+
+ while (*ptr)
+ {
+ if (*ptr++ == ' ')
+ {
+ err = ptr;
+ break;
+ }
+ }
+ }
+
+ if (strfind((char *)err, strlen(err), "'char(27)'", 10) != NULL)
+ {
+ off = parser->mmap + parser->size;
+ err = "'%>' expected before end of file";
+ chunkline = 0;
+ }
+
+ for (ptr = parser->mmap; ptr < off; ptr++)
+ if (*ptr == '\n')
+ line++;
+
+ snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s",
+ parser->file, line + chunkline, err ? err : "(unknown error)");
+
+ lua_pushnil(L);
+ lua_pushinteger(L, line + chunkline);
+ lua_pushstring(L, msg);
+
+ return 3;
+}
diff --git a/modules/base/src/template_parser.h b/modules/base/src/template_parser.h
new file mode 100644
index 000000000..d1c606272
--- /dev/null
+++ b/modules/base/src/template_parser.h
@@ -0,0 +1,79 @@
+/*
+ * LuCI Template - Parser header
+ *
+ * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _TEMPLATE_PARSER_H_
+#define _TEMPLATE_PARSER_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
+
+/* code types */
+#define T_TYPE_INIT 0
+#define T_TYPE_TEXT 1
+#define T_TYPE_COMMENT 2
+#define T_TYPE_EXPR 3
+#define T_TYPE_INCLUDE 4
+#define T_TYPE_I18N 5
+#define T_TYPE_I18N_RAW 6
+#define T_TYPE_CODE 7
+#define T_TYPE_EOF 8
+
+
+struct template_chunk {
+ const char *s;
+ const char *e;
+ int type;
+ int line;
+};
+
+/* parser state */
+struct template_parser {
+ int fd;
+ uint32_t size;
+ char *mmap;
+ char *off;
+ char *gc;
+ int line;
+ int in_expr;
+ int strip_before;
+ int strip_after;
+ struct template_chunk prv_chunk;
+ struct template_chunk cur_chunk;
+ const char *file;
+};
+
+struct template_parser * template_open(const char *file);
+void template_close(struct template_parser *parser);
+
+const char *template_reader(lua_State *L, void *ud, size_t *sz);
+int template_error(lua_State *L, struct template_parser *parser);
+
+#endif
diff --git a/modules/base/src/template_utils.c b/modules/base/src/template_utils.c
new file mode 100644
index 000000000..80542bd4f
--- /dev/null
+++ b/modules/base/src/template_utils.c
@@ -0,0 +1,494 @@
+/*
+ * LuCI Template - Utility functions
+ *
+ * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "template_utils.h"
+#include "template_lmo.h"
+
+/* initialize a buffer object */
+struct template_buffer * buf_init(int size)
+{
+ struct template_buffer *buf;
+
+ if (size <= 0)
+ size = 1024;
+
+ buf = (struct template_buffer *)malloc(sizeof(struct template_buffer));
+
+ if (buf != NULL)
+ {
+ buf->fill = 0;
+ buf->size = size;
+ buf->data = malloc(buf->size);
+
+ if (buf->data != NULL)
+ {
+ buf->dptr = buf->data;
+ buf->data[0] = 0;
+
+ return buf;
+ }
+
+ free(buf);
+ }
+
+ return NULL;
+}
+
+/* grow buffer */
+int buf_grow(struct template_buffer *buf, int size)
+{
+ unsigned int off = (buf->dptr - buf->data);
+ char *data;
+
+ if (size <= 0)
+ size = 1024;
+
+ data = realloc(buf->data, buf->size + size);
+
+ if (data != NULL)
+ {
+ buf->data = data;
+ buf->dptr = data + off;
+ buf->size += size;
+
+ return buf->size;
+ }
+
+ return 0;
+}
+
+/* put one char into buffer object */
+int buf_putchar(struct template_buffer *buf, char c)
+{
+ if( ((buf->fill + 1) >= buf->size) && !buf_grow(buf, 0) )
+ return 0;
+
+ *(buf->dptr++) = c;
+ *(buf->dptr) = 0;
+
+ buf->fill++;
+ return 1;
+}
+
+/* append data to buffer */
+int buf_append(struct template_buffer *buf, const char *s, int len)
+{
+ if ((buf->fill + len + 1) >= buf->size)
+ {
+ if (!buf_grow(buf, len + 1))
+ return 0;
+ }
+
+ memcpy(buf->dptr, s, len);
+ buf->fill += len;
+ buf->dptr += len;
+
+ *(buf->dptr) = 0;
+
+ return len;
+}
+
+/* read buffer length */
+int buf_length(struct template_buffer *buf)
+{
+ return buf->fill;
+}
+
+/* destroy buffer object and return pointer to data */
+char * buf_destroy(struct template_buffer *buf)
+{
+ char *data = buf->data;
+
+ free(buf);
+ return data;
+}
+
+
+/* calculate the number of expected continuation chars */
+static inline int mb_num_chars(unsigned char c)
+{
+ if ((c & 0xE0) == 0xC0)
+ return 2;
+ else if ((c & 0xF0) == 0xE0)
+ return 3;
+ else if ((c & 0xF8) == 0xF0)
+ return 4;
+ else if ((c & 0xFC) == 0xF8)
+ return 5;
+ else if ((c & 0xFE) == 0xFC)
+ return 6;
+
+ return 1;
+}
+
+/* test whether the given byte is a valid continuation char */
+static inline int mb_is_cont(unsigned char c)
+{
+ return ((c >= 0x80) && (c <= 0xBF));
+}
+
+/* test whether the byte sequence at the given pointer with the given
+ * length is the shortest possible representation of the code point */
+static inline int mb_is_shortest(unsigned char *s, int n)
+{
+ switch (n)
+ {
+ case 2:
+ /* 1100000x (10xxxxxx) */
+ return !(((*s >> 1) == 0x60) &&
+ ((*(s+1) >> 6) == 0x02));
+
+ case 3:
+ /* 11100000 100xxxxx (10xxxxxx) */
+ return !((*s == 0xE0) &&
+ ((*(s+1) >> 5) == 0x04) &&
+ ((*(s+2) >> 6) == 0x02));
+
+ case 4:
+ /* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
+ return !((*s == 0xF0) &&
+ ((*(s+1) >> 4) == 0x08) &&
+ ((*(s+2) >> 6) == 0x02) &&
+ ((*(s+3) >> 6) == 0x02));
+
+ case 5:
+ /* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */
+ return !((*s == 0xF8) &&
+ ((*(s+1) >> 3) == 0x10) &&
+ ((*(s+2) >> 6) == 0x02) &&
+ ((*(s+3) >> 6) == 0x02) &&
+ ((*(s+4) >> 6) == 0x02));
+
+ case 6:
+ /* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */
+ return !((*s == 0xF8) &&
+ ((*(s+1) >> 2) == 0x20) &&
+ ((*(s+2) >> 6) == 0x02) &&
+ ((*(s+3) >> 6) == 0x02) &&
+ ((*(s+4) >> 6) == 0x02) &&
+ ((*(s+5) >> 6) == 0x02));
+ }
+
+ return 1;
+}
+
+/* test whether the byte sequence at the given pointer with the given
+ * length is an UTF-16 surrogate */
+static inline int mb_is_surrogate(unsigned char *s, int n)
+{
+ return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
+}
+
+/* test whether the byte sequence at the given pointer with the given
+ * length is an illegal UTF-8 code point */
+static inline int mb_is_illegal(unsigned char *s, int n)
+{
+ return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
+ (*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
+}
+
+
+/* scan given source string, validate UTF-8 sequence and store result
+ * in given buffer object */
+static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
+{
+ unsigned char *ptr = *s;
+ unsigned int o = 0, v, n;
+
+ /* ascii byte without null */
+ if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
+ {
+ if (!buf_putchar(buf, *ptr++))
+ return 0;
+
+ o = 1;
+ }
+
+ /* multi byte sequence */
+ else if ((n = mb_num_chars(*ptr)) > 1)
+ {
+ /* count valid chars */
+ for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
+
+ switch (n)
+ {
+ case 6:
+ case 5:
+ /* five and six byte sequences are always invalid */
+ if (!buf_putchar(buf, '?'))
+ return 0;
+
+ break;
+
+ default:
+ /* if the number of valid continuation bytes matches the
+ * expected number and if the sequence is legal, copy
+ * the bytes to the destination buffer */
+ if ((v == n) && mb_is_shortest(ptr, n) &&
+ !mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
+ {
+ /* copy sequence */
+ if (!buf_append(buf, (char *)ptr, n))
+ return 0;
+ }
+
+ /* the found sequence is illegal, skip it */
+ else
+ {
+ /* invalid sequence */
+ if (!buf_putchar(buf, '?'))
+ return 0;
+ }
+
+ break;
+ }
+
+ /* advance beyound the last found valid continuation char */
+ o = v;
+ ptr += v;
+ }
+
+ /* invalid byte (0x00) */
+ else
+ {
+ if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
+ return 0;
+
+ o = 1;
+ ptr++;
+ }
+
+ *s = ptr;
+ return o;
+}
+
+/* sanitize given string and replace all invalid UTF-8 sequences with "?" */
+char * utf8(const char *s, unsigned int l)
+{
+ struct template_buffer *buf = buf_init(l);
+ unsigned char *ptr = (unsigned char *)s;
+ unsigned int v, o;
+
+ if (!buf)
+ return NULL;
+
+ for (o = 0; o < l; o++)
+ {
+ /* ascii char */
+ if ((*ptr >= 0x01) && (*ptr <= 0x7F))
+ {
+ if (!buf_putchar(buf, (char)*ptr++))
+ break;
+ }
+
+ /* invalid byte or multi byte sequence */
+ else
+ {
+ if (!(v = _validate_utf8(&ptr, l - o, buf)))
+ break;
+
+ o += (v - 1);
+ }
+ }
+
+ return buf_destroy(buf);
+}
+
+/* Sanitize given string and strip all invalid XML bytes
+ * Validate UTF-8 sequences
+ * Escape XML control chars */
+char * pcdata(const char *s, unsigned int l)
+{
+ struct template_buffer *buf = buf_init(l);
+ unsigned char *ptr = (unsigned char *)s;
+ unsigned int o, v;
+ char esq[8];
+ int esl;
+
+ if (!buf)
+ return NULL;
+
+ for (o = 0; o < l; o++)
+ {
+ /* Invalid XML bytes */
+ if (((*ptr >= 0x00) && (*ptr <= 0x08)) ||
+ ((*ptr >= 0x0B) && (*ptr <= 0x0C)) ||
+ ((*ptr >= 0x0E) && (*ptr <= 0x1F)) ||
+ (*ptr == 0x7F))
+ {
+ ptr++;
+ }
+
+ /* Escapes */
+ else if ((*ptr == 0x26) ||
+ (*ptr == 0x27) ||
+ (*ptr == 0x22) ||
+ (*ptr == 0x3C) ||
+ (*ptr == 0x3E))
+ {
+ esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
+
+ if (!buf_append(buf, esq, esl))
+ break;
+
+ ptr++;
+ }
+
+ /* ascii char */
+ else if (*ptr <= 0x7F)
+ {
+ buf_putchar(buf, (char)*ptr++);
+ }
+
+ /* multi byte sequence */
+ else
+ {
+ if (!(v = _validate_utf8(&ptr, l - o, buf)))
+ break;
+
+ o += (v - 1);
+ }
+ }
+
+ return buf_destroy(buf);
+}
+
+char * striptags(const char *s, unsigned int l)
+{
+ struct template_buffer *buf = buf_init(l);
+ unsigned char *ptr = (unsigned char *)s;
+ unsigned char *end = ptr + l;
+ unsigned char *tag;
+ unsigned char prev;
+ char esq[8];
+ int esl;
+
+ for (prev = ' '; ptr < end; ptr++)
+ {
+ if ((*ptr == '<') && ((ptr + 2) < end) &&
+ ((*(ptr + 1) == '/') || isalpha(*(ptr + 1))))
+ {
+ for (tag = ptr; tag < end; tag++)
+ {
+ if (*tag == '>')
+ {
+ if (!isspace(prev))
+ buf_putchar(buf, ' ');
+
+ ptr = tag;
+ prev = ' ';
+ break;
+ }
+ }
+ }
+ else if (isspace(*ptr))
+ {
+ if (!isspace(prev))
+ buf_putchar(buf, *ptr);
+
+ prev = *ptr;
+ }
+ else
+ {
+ switch(*ptr)
+ {
+ case '"':
+ case '\'':
+ case '<':
+ case '>':
+ case '&':
+ esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
+ buf_append(buf, esq, esl);
+ break;
+
+ default:
+ buf_putchar(buf, *ptr);
+ break;
+ }
+
+ prev = *ptr;
+ }
+ }
+
+ return buf_destroy(buf);
+}
+
+void luastr_escape(struct template_buffer *out, const char *s, unsigned int l,
+ int escape_xml)
+{
+ int esl;
+ char esq[8];
+ char *ptr;
+
+ for (ptr = (char *)s; ptr < (s + l); ptr++)
+ {
+ switch (*ptr)
+ {
+ case '\\':
+ buf_append(out, "\\\\", 2);
+ break;
+
+ case '"':
+ if (escape_xml)
+ buf_append(out, "&#34;", 5);
+ else
+ buf_append(out, "\\\"", 2);
+ break;
+
+ case '\n':
+ buf_append(out, "\\n", 2);
+ break;
+
+ case '\'':
+ case '&':
+ case '<':
+ case '>':
+ if (escape_xml)
+ {
+ esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
+ buf_append(out, esq, esl);
+ break;
+ }
+
+ default:
+ buf_putchar(out, *ptr);
+ }
+ }
+}
+
+void luastr_translate(struct template_buffer *out, const char *s, unsigned int l,
+ int escape_xml)
+{
+ char *tr;
+ int trlen;
+
+ switch (lmo_translate(s, l, &tr, &trlen))
+ {
+ case 0:
+ luastr_escape(out, tr, trlen, escape_xml);
+ break;
+
+ case -1:
+ luastr_escape(out, s, l, escape_xml);
+ break;
+
+ default:
+ /* no catalog loaded */
+ break;
+ }
+}
diff --git a/modules/base/src/template_utils.h b/modules/base/src/template_utils.h
new file mode 100644
index 000000000..c54af757c
--- /dev/null
+++ b/modules/base/src/template_utils.h
@@ -0,0 +1,49 @@
+/*
+ * LuCI Template - Utility header
+ *
+ * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _TEMPLATE_UTILS_H_
+#define _TEMPLATE_UTILS_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+
+/* buffer object */
+struct template_buffer {
+ char *data;
+ char *dptr;
+ unsigned int size;
+ unsigned int fill;
+};
+
+struct template_buffer * buf_init(int size);
+int buf_grow(struct template_buffer *buf, int size);
+int buf_putchar(struct template_buffer *buf, char c);
+int buf_append(struct template_buffer *buf, const char *s, int len);
+int buf_length(struct template_buffer *buf);
+char * buf_destroy(struct template_buffer *buf);
+
+char * utf8(const char *s, unsigned int l);
+char * pcdata(const char *s, unsigned int l);
+char * striptags(const char *s, unsigned int l);
+
+void luastr_escape(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
+void luastr_translate(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
+
+#endif