diff options
Diffstat (limited to 'modules/luci-base/src')
-rw-r--r-- | modules/luci-base/src/Makefile | 30 | ||||
-rw-r--r-- | modules/luci-base/src/lib/lmo.c (renamed from modules/luci-base/src/template_lmo.c) | 9 | ||||
-rw-r--r-- | modules/luci-base/src/lib/lmo.h (renamed from modules/luci-base/src/template_lmo.h) | 44 | ||||
-rw-r--r-- | modules/luci-base/src/lib/luci.c | 383 | ||||
-rw-r--r-- | modules/luci-base/src/lib/plural_formula.y (renamed from modules/luci-base/src/plural_formula.y) | 0 | ||||
-rwxr-xr-x | modules/luci-base/src/mkversion.sh | 24 | ||||
-rw-r--r-- | modules/luci-base/src/po2lmo.c | 9 | ||||
-rw-r--r-- | modules/luci-base/src/template_lualib.c | 224 | ||||
-rw-r--r-- | modules/luci-base/src/template_lualib.h | 30 | ||||
-rw-r--r-- | modules/luci-base/src/template_parser.c | 419 | ||||
-rw-r--r-- | modules/luci-base/src/template_parser.h | 80 | ||||
-rw-r--r-- | modules/luci-base/src/template_utils.c | 500 | ||||
-rw-r--r-- | modules/luci-base/src/template_utils.h | 49 |
13 files changed, 433 insertions, 1368 deletions
diff --git a/modules/luci-base/src/Makefile b/modules/luci-base/src/Makefile index 2a425d5ab7..ad309e5c6b 100644 --- a/modules/luci-base/src/Makefile +++ b/modules/luci-base/src/Makefile @@ -4,29 +4,31 @@ contrib/lemon: contrib/lemon.c contrib/lempar.c cc -o contrib/lemon $< -plural_formula.c: plural_formula.y contrib/lemon +lib/plural_formula.c: lib/plural_formula.y contrib/lemon ./contrib/lemon -q $< -template_lmo.c: plural_formula.c +lib/lmo.c: lib/plural_formula.c + +core.so: lib/luci.o lib/lmo.o lib/plural_formula.o + $(CC) $(LDFLAGS) -shared -lcrypt -o $@ $^ + +version.uc: + echo "export const revision = '$(LUCI_VERSION)', branch = '$(LUCI_GITBRANCH)';" > $@ clean: - rm -f contrib/lemon po2lmo parser.so version.lua plural_formula.c plural_formula.h *.o + rm -f contrib/lemon lib/*.o lib/plural_formula.c lib/plural_formula.h core.so version.uc jsmin: jsmin.o $(CC) $(LDFLAGS) -o $@ $^ -po2lmo: po2lmo.o template_lmo.o plural_formula.o +po2lmo: po2lmo.o lib/lmo.o lib/plural_formula.o $(CC) $(LDFLAGS) -o $@ $^ -parser.so: template_parser.o template_utils.o template_lmo.o template_lualib.o plural_formula.o - $(CC) $(LDFLAGS) -shared -o $@ $^ - -version.lua: - ./mkversion.sh $@ $(LUCI_VERSION) "$(LUCI_GITBRANCH)" - -compile: parser.so version.lua +compile: core.so version.uc install: compile - mkdir -p $(DESTDIR)/usr/lib/lua/luci/template - cp parser.so $(DESTDIR)/usr/lib/lua/luci/template/parser.so - cp version.lua $(DESTDIR)/usr/lib/lua/luci/version.lua + mkdir -p $(DESTDIR)/usr/lib/ucode/luci + cp core.so $(DESTDIR)/usr/lib/ucode/luci/core.so + + mkdir -p $(DESTDIR)/usr/share/ucode/luci + cp version.uc $(DESTDIR)/usr/share/ucode/luci/version.uc diff --git a/modules/luci-base/src/template_lmo.c b/modules/luci-base/src/lib/lmo.c index 8634bc4bf3..da521bc98b 100644 --- a/modules/luci-base/src/template_lmo.c +++ b/modules/luci-base/src/lib/lmo.c @@ -16,7 +16,7 @@ * limitations under the License. */ -#include "template_lmo.h" +#include "lmo.h" #include "plural_formula.h" /* @@ -24,9 +24,9 @@ * Copyright (C) 2004-2008 by Paul Hsieh */ -uint32_t sfh_hash(const char *data, int len) +uint32_t sfh_hash(const char *data, size_t len, uint32_t init) { - uint32_t hash = len, tmp; + uint32_t hash = init, tmp; int rem; if (len <= 0 || data == NULL) return 0; @@ -137,7 +137,7 @@ uint32_t lmo_canon_hash(const char *str, int len, ptr += snprintf(ptr, 3, "\2%d", plural); } - return sfh_hash(res, ptr - res); + return sfh_hash(res, ptr - res, ptr - res); } lmo_archive_t * lmo_open(const char *file) @@ -550,7 +550,6 @@ int lmo_translate_plural_ctxt(int n, const char *skey, int skeylen, uint32_t hash; lmo_entry_t *e; lmo_archive_t *ar; - const char *plural_formula; if (!skey || !pkey || !_lmo_active_catalog) return -2; diff --git a/modules/luci-base/src/template_lmo.h b/modules/luci-base/src/lib/lmo.h index d6cba7bf49..744209f62c 100644 --- a/modules/luci-base/src/template_lmo.h +++ b/modules/luci-base/src/lib/lmo.h @@ -41,6 +41,10 @@ +(uint32_t)(((const uint8_t *)(d))[0]) ) #endif +#ifndef __hidden +#define __hidden __attribute__((visibility("hidden"))) +#endif + struct lmo_entry { uint32_t key_id; @@ -75,30 +79,30 @@ typedef struct lmo_catalog lmo_catalog_t; typedef void (*lmo_iterate_cb_t)(uint32_t, const char *, int, void *); -uint32_t sfh_hash(const char *data, int len); -uint32_t lmo_canon_hash(const char *data, int len, - const char *ctx, int ctxlen, int plural); +__hidden uint32_t sfh_hash(const char *data, size_t len, uint32_t init); +__hidden uint32_t lmo_canon_hash(const char *data, int len, + const char *ctx, int ctxlen, int plural); -lmo_archive_t * lmo_open(const char *file); -void lmo_close(lmo_archive_t *ar); +__hidden lmo_archive_t * lmo_open(const char *file); +__hidden void lmo_close(lmo_archive_t *ar); -extern lmo_catalog_t *_lmo_catalogs; -extern lmo_catalog_t *_lmo_active_catalog; +__hidden extern lmo_catalog_t *_lmo_catalogs; +__hidden 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); -int lmo_translate_ctxt(const char *key, int keylen, - const char *ctx, int ctxlen, char **out, int *outlen); -int lmo_translate_plural(int n, const char *skey, int skeylen, - const char *pkey, int pkeylen, - char **out, int *outlen); -int lmo_translate_plural_ctxt(int n, const char *skey, int skeylen, - const char *pkey, int pkeylen, - const char *ctx, int ctxlen, +__hidden int lmo_load_catalog(const char *lang, const char *dir); +__hidden int lmo_change_catalog(const char *lang); +__hidden int lmo_translate(const char *key, int keylen, char **out, int *outlen); +__hidden int lmo_translate_ctxt(const char *key, int keylen, + const char *ctx, int ctxlen, char **out, int *outlen); +__hidden int lmo_translate_plural(int n, const char *skey, int skeylen, + const char *pkey, int pkeylen, + char **out, int *outlen); +__hidden int lmo_translate_plural_ctxt(int n, const char *skey, int skeylen, + const char *pkey, int pkeylen, + const char *ctx, int ctxlen, char **out, int *outlen); -void lmo_iterate(lmo_iterate_cb_t cb, void *priv); -void lmo_close_catalog(const char *lang); +__hidden void lmo_iterate(lmo_iterate_cb_t cb, void *priv); +__hidden void lmo_close_catalog(const char *lang); #endif diff --git a/modules/luci-base/src/lib/luci.c b/modules/luci-base/src/lib/luci.c new file mode 100644 index 0000000000..e6860e727d --- /dev/null +++ b/modules/luci-base/src/lib/luci.c @@ -0,0 +1,383 @@ +/* + * LuCI low level routines - ucode binding + * + * Copyright (C) 2009-2022 Jo-Philipp Wich <jo@mein.io> + * + * 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 "lmo.h" + +#include <pwd.h> +#include <crypt.h> +#include <shadow.h> +#include <unistd.h> +#include <signal.h> +#include <errno.h> + +#include <sys/types.h> +#include <sys/utsname.h> +#include <sys/sysinfo.h> +#include <sys/statvfs.h> + +#include <ucode/module.h> + +/* translation catalog functions */ + +static uc_value_t * +uc_luci_load_catalog(uc_vm_t *vm, size_t nargs) { + uc_value_t *lang = uc_fn_arg(0); + uc_value_t *dir = uc_fn_arg(1); + + if (lang && ucv_type(lang) != UC_STRING) + return NULL; + + if (dir && ucv_type(dir) != UC_STRING) + return NULL; + + return ucv_boolean_new(lmo_load_catalog( + lang ? ucv_string_get(lang) : "en", + ucv_string_get(dir)) == 0); +} + +static uc_value_t * +uc_luci_close_catalog(uc_vm_t *vm, size_t nargs) { + uc_value_t *lang = uc_fn_arg(0); + + if (lang && ucv_type(lang) != UC_STRING) + return NULL; + + lmo_close_catalog(lang ? ucv_string_get(lang) : "en"); + + return ucv_boolean_new(true); +} + +static uc_value_t * +uc_luci_change_catalog(uc_vm_t *vm, size_t nargs) { + uc_value_t *lang = uc_fn_arg(0); + + if (lang && ucv_type(lang) != UC_STRING) + return NULL; + + return ucv_boolean_new(lmo_change_catalog( + lang ? ucv_string_get(lang) : "en") == 0); +} + +static void +uc_luci_get_translations_cb(uint32_t key, const char *val, int len, void *priv) { + uc_vm_t *vm = priv; + + uc_vm_stack_push(vm, ucv_get(uc_vm_stack_peek(vm, 0))); + uc_vm_stack_push(vm, ucv_uint64_new(key)); + uc_vm_stack_push(vm, ucv_string_new_length(val, (size_t)len)); + + if (uc_vm_call(vm, false, 2) == EXCEPTION_NONE) + ucv_put(uc_vm_stack_pop(vm)); +} + +static uc_value_t * +uc_luci_get_translations(uc_vm_t *vm, size_t nargs) { + lmo_iterate(uc_luci_get_translations_cb, vm); + + return ucv_boolean_new(true); +} + +static uc_value_t * +uc_luci_translate(uc_vm_t *vm, size_t nargs) { + uc_value_t *key = uc_fn_arg(0); + uc_value_t *ctx = uc_fn_arg(1); + int trlen; + char *tr; + + if (ucv_type(key) != UC_STRING) + return NULL; + + if (ctx && ucv_type(ctx) != UC_STRING) + return NULL; + + if (lmo_translate_ctxt(ucv_string_get(key), ucv_string_length(key), + ucv_string_get(ctx), ucv_string_length(ctx), + &tr, &trlen) != 0) + return NULL; + + return ucv_string_new_length(tr, (size_t)trlen); +} + +static uc_value_t * +uc_luci_ntranslate(uc_vm_t *vm, size_t nargs) { + uc_value_t *cnt = uc_fn_arg(0); + uc_value_t *skey = uc_fn_arg(1); + uc_value_t *pkey = uc_fn_arg(2); + uc_value_t *ctx = uc_fn_arg(3); + int trlen; + char *tr; + + if (ucv_type(skey) != UC_STRING || ucv_type(pkey) != UC_STRING) + return NULL; + + if (ctx && ucv_type(ctx) != UC_STRING) + return NULL; + + if (lmo_translate_plural_ctxt(ucv_int64_get(cnt), + ucv_string_get(skey), ucv_string_length(skey), + ucv_string_get(pkey), ucv_string_length(pkey), + ucv_string_get(ctx), ucv_string_length(ctx), + &tr, &trlen) != 0) + return NULL; + + return ucv_string_new_length(tr, (size_t)trlen); +} + +static uc_value_t * +uc_luci_hash(uc_vm_t *vm, size_t nargs) { + uc_value_t *key = uc_fn_arg(0); + uc_value_t *init = uc_fn_arg(1); + + if (ucv_type(key) != UC_STRING) + return NULL; + + if (init && ucv_type(init) != UC_INTEGER) + return NULL; + + return ucv_uint64_new(sfh_hash(ucv_string_get(key), ucv_string_length(key), + init ? ucv_uint64_get(init) : ucv_string_length(key))); +} + + +/* user functions */ + +static uc_value_t * +uc_luci_getspnam(uc_vm_t *vm, size_t nargs) { + uc_value_t *name = uc_fn_arg(0), *rv; + struct spwd *s; + + if (ucv_type(name) != UC_STRING) + return NULL; + + s = getspnam(ucv_string_get(name)); + + if (!s) + return NULL; + + rv = ucv_object_new(vm); + + ucv_object_add(rv, "namp", ucv_string_new(s->sp_namp)); + ucv_object_add(rv, "pwdp", ucv_string_new(s->sp_pwdp)); + ucv_object_add(rv, "lstchg", ucv_int64_new(s->sp_lstchg)); + ucv_object_add(rv, "min", ucv_int64_new(s->sp_min)); + ucv_object_add(rv, "max", ucv_int64_new(s->sp_max)); + ucv_object_add(rv, "warn", ucv_int64_new(s->sp_warn)); + ucv_object_add(rv, "inact", ucv_int64_new(s->sp_inact)); + ucv_object_add(rv, "expire", ucv_int64_new(s->sp_expire)); + + return rv; +} + +static uc_value_t * +uc_luci_getpwnam(uc_vm_t *vm, size_t nargs) { + uc_value_t *name = uc_fn_arg(0), *rv; + struct passwd *p; + + if (ucv_type(name) != UC_STRING) + return NULL; + + p = getpwnam(ucv_string_get(name)); + + if (!p) + return NULL; + + rv = ucv_object_new(vm); + + ucv_object_add(rv, "name", ucv_string_new(p->pw_name)); + ucv_object_add(rv, "passwd", ucv_string_new(p->pw_passwd)); + ucv_object_add(rv, "uid", ucv_int64_new(p->pw_uid)); + ucv_object_add(rv, "gid", ucv_int64_new(p->pw_gid)); + ucv_object_add(rv, "gecos", ucv_string_new(p->pw_gecos)); + ucv_object_add(rv, "dir", ucv_string_new(p->pw_dir)); + ucv_object_add(rv, "shell", ucv_string_new(p->pw_shell)); + + return rv; +} + +static uc_value_t * +uc_luci_crypt(uc_vm_t *vm, size_t nargs) { + uc_value_t *phrase = uc_fn_arg(0); + uc_value_t *setting = uc_fn_arg(1); + char *hash; + + if (ucv_type(phrase) != UC_STRING || ucv_type(setting) != UC_STRING) + return NULL; + + errno = 0; + hash = crypt(ucv_string_get(phrase), ucv_string_get(setting)); + + if (hash == NULL || errno != 0) + return NULL; + + return ucv_string_new(hash); +} + +static uc_value_t * +uc_luci_getuid(uc_vm_t *vm, size_t nargs) { + return ucv_int64_new(getuid()); +} + +static uc_value_t * +uc_luci_getgid(uc_vm_t *vm, size_t nargs) { + return ucv_int64_new(getgid()); +} + +static uc_value_t * +uc_luci_setuid(uc_vm_t *vm, size_t nargs) { + uc_value_t *uid = uc_fn_arg(0); + + if (ucv_type(uid) != UC_INTEGER) + return NULL; + + return ucv_boolean_new(setuid(ucv_int64_get(uid)) == 0); +} + +static uc_value_t * +uc_luci_setgid(uc_vm_t *vm, size_t nargs) { + uc_value_t *gid = uc_fn_arg(0); + + if (ucv_type(gid) != UC_INTEGER) + return NULL; + + return ucv_boolean_new(setgid(ucv_int64_get(gid)) == 0); +} + + +/* misc functions */ + +static uc_value_t * +uc_luci_kill(uc_vm_t *vm, size_t nargs) { + uc_value_t *pid = uc_fn_arg(0); + uc_value_t *sig = uc_fn_arg(1); + + if (ucv_type(pid) != UC_INTEGER || ucv_type(sig) != UC_INTEGER) + return NULL; + + return ucv_boolean_new(kill(ucv_int64_get(pid), ucv_int64_get(sig)) == 0); +} + +static uc_value_t * +uc_luci_uname(uc_vm_t *vm, size_t nargs) { + struct utsname u; + uc_value_t *rv; + + if (uname(&u) == -1) + return NULL; + + rv = ucv_object_new(vm); + + ucv_object_add(rv, "sysname", ucv_string_new(u.sysname)); + ucv_object_add(rv, "nodename", ucv_string_new(u.nodename)); + ucv_object_add(rv, "release", ucv_string_new(u.release)); + ucv_object_add(rv, "version", ucv_string_new(u.version)); + ucv_object_add(rv, "machine", ucv_string_new(u.machine)); + + return rv; +} + +static uc_value_t * +uc_luci_sysinfo(uc_vm_t *vm, size_t nargs) { + uc_value_t *rv, *loads; + struct sysinfo i; + + if (sysinfo(&i) == -1) + return NULL; + + rv = ucv_object_new(vm); + loads = ucv_array_new_length(vm, 3); + + ucv_array_push(loads, ucv_uint64_new(i.loads[0])); + ucv_array_push(loads, ucv_uint64_new(i.loads[1])); + ucv_array_push(loads, ucv_uint64_new(i.loads[2])); + + ucv_object_add(rv, "uptime", ucv_int64_new(i.uptime)); + ucv_object_add(rv, "loads", loads); + ucv_object_add(rv, "totalram", ucv_uint64_new(i.totalram)); + ucv_object_add(rv, "freeram", ucv_uint64_new(i.freeram)); + ucv_object_add(rv, "sharedram", ucv_uint64_new(i.sharedram)); + ucv_object_add(rv, "bufferram", ucv_uint64_new(i.bufferram)); + ucv_object_add(rv, "totalswap", ucv_uint64_new(i.totalswap)); + ucv_object_add(rv, "freeswap", ucv_uint64_new(i.freeswap)); + ucv_object_add(rv, "procs", ucv_uint64_new(i.procs)); + ucv_object_add(rv, "totalhigh", ucv_uint64_new(i.totalhigh)); + ucv_object_add(rv, "freehigh", ucv_uint64_new(i.freehigh)); + ucv_object_add(rv, "mem_unit", ucv_uint64_new(i.mem_unit)); + + return rv; +} + +static uc_value_t * +uc_luci_statvfs(uc_vm_t *vm, size_t nargs) { + uc_value_t *path = uc_fn_arg(0), *rv; + struct statvfs s; + + if (ucv_type(path) != UC_STRING) + return NULL; + + if (statvfs(ucv_string_get(path), &s) == -1) + return NULL; + + rv = ucv_object_new(vm); + + ucv_object_add(rv, "bsize", ucv_uint64_new(s.f_bsize)); + ucv_object_add(rv, "frsize", ucv_uint64_new(s.f_frsize)); + + ucv_object_add(rv, "blocks", ucv_uint64_new(s.f_blocks)); + ucv_object_add(rv, "bfree", ucv_uint64_new(s.f_bfree)); + ucv_object_add(rv, "bavail", ucv_uint64_new(s.f_bavail)); + + ucv_object_add(rv, "files", ucv_uint64_new(s.f_files)); + ucv_object_add(rv, "ffree", ucv_uint64_new(s.f_ffree)); + ucv_object_add(rv, "favail", ucv_uint64_new(s.f_favail)); + + ucv_object_add(rv, "fsid", ucv_uint64_new(s.f_fsid)); + ucv_object_add(rv, "flag", ucv_uint64_new(s.f_flag)); + ucv_object_add(rv, "namemax", ucv_uint64_new(s.f_namemax)); + + return rv; +} + + +static const uc_function_list_t luci_fns[] = { + { "load_catalog", uc_luci_load_catalog }, + { "close_catalog", uc_luci_close_catalog }, + { "change_catalog", uc_luci_change_catalog }, + { "get_translations", uc_luci_get_translations }, + { "translate", uc_luci_translate }, + { "ntranslate", uc_luci_ntranslate }, + { "hash", uc_luci_hash }, + + { "getspnam", uc_luci_getspnam }, + { "getpwnam", uc_luci_getpwnam }, + { "crypt", uc_luci_crypt }, + { "getuid", uc_luci_getuid }, + { "setuid", uc_luci_setuid }, + { "getgid", uc_luci_getgid }, + { "setgid", uc_luci_setgid }, + + { "kill", uc_luci_kill }, + { "uname", uc_luci_uname }, + { "sysinfo", uc_luci_sysinfo }, + { "statvfs", uc_luci_statvfs }, +}; + + +void uc_module_init(uc_vm_t *vm, uc_value_t *scope) +{ + uc_function_list_register(scope, luci_fns); +} diff --git a/modules/luci-base/src/plural_formula.y b/modules/luci-base/src/lib/plural_formula.y index 1623f8b282..1623f8b282 100644 --- a/modules/luci-base/src/plural_formula.y +++ b/modules/luci-base/src/lib/plural_formula.y diff --git a/modules/luci-base/src/mkversion.sh b/modules/luci-base/src/mkversion.sh deleted file mode 100755 index e2d02c1c74..0000000000 --- a/modules/luci-base/src/mkversion.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh - -cat <<EOF > $1 -local pcall, dofile, _G = pcall, dofile, _G - -module "luci.version" - -if pcall(dofile, "/etc/openwrt_release") and _G.DISTRIB_DESCRIPTION then - distname = "" - distversion = _G.DISTRIB_DESCRIPTION - if _G.DISTRIB_REVISION then - distrevision = _G.DISTRIB_REVISION - if not distversion:find(distrevision,1,true) then - distversion = distversion .. " " .. distrevision - end - end -else - distname = "OpenWrt" - distversion = "Development Snapshot" -end - -luciname = "${3:-LuCI}" -luciversion = "${2:-Git}" -EOF diff --git a/modules/luci-base/src/po2lmo.c b/modules/luci-base/src/po2lmo.c index 5f398c266e..0a04e9ba17 100644 --- a/modules/luci-base/src/po2lmo.c +++ b/modules/luci-base/src/po2lmo.c @@ -16,7 +16,7 @@ * limitations under the License. */ -#include "template_lmo.h" +#include "lib/lmo.h" static void die(const char *msg) { @@ -169,8 +169,11 @@ static void print_msg(struct msg *msg, FILE *out) else snprintf(key, sizeof(key), "%s", msg->id); - key_id = sfh_hash(key, strlen(key)); - val_id = sfh_hash(msg->val[i], strlen(msg->val[i])); + len = strlen(key); + key_id = sfh_hash(key, len, len); + + len = strlen(msg->val[i]); + val_id = sfh_hash(msg->val[i], len, len); if (key_id != val_id) { n_entries++; diff --git a/modules/luci-base/src/template_lualib.c b/modules/luci-base/src/template_lualib.c deleted file mode 100644 index 4efd9f1de6..0000000000 --- a/modules/luci-base/src/template_lualib.c +++ /dev/null @@ -1,224 +0,0 @@ -/* - * LuCI Template - Lua binding - * - * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.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" - -static int template_L_do_parse(lua_State *L, struct template_parser *parser, const char *chunkname) -{ - 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, chunkname); - - if (lua_status == 0) - rv = 1; - else - rv = template_error(L, parser); - - template_close(parser); - - 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; - 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 void template_L_get_translations_cb(uint32_t key, const char *val, int len, void *priv) { - lua_State *L = priv; - char hex[9]; - - luaL_checktype(L, 1, LUA_TFUNCTION); - snprintf(hex, sizeof(hex), "%08x", key); - - lua_pushvalue(L, 1); - lua_pushstring(L, hex); - lua_pushlstring(L, val, len); - lua_call(L, 2, 0); -} - -static int template_L_get_translations(lua_State *L) { - lmo_iterate(template_L_get_translations_cb, L); - return 0; -} - -static int template_L_translate(lua_State *L) { - size_t len, ctxlen = 0; - char *tr; - int trlen; - const char *key = luaL_checklstring(L, 1, &len); - const char *ctx = luaL_optlstring(L, 2, NULL, &ctxlen); - - switch (lmo_translate_ctxt(key, len, ctx, ctxlen, &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_ntranslate(lua_State *L) { - size_t slen, plen, ctxlen = 0; - char *tr; - int trlen; - int n = luaL_checkinteger(L, 1); - const char *skey = luaL_checklstring(L, 2, &slen); - const char *pkey = luaL_checklstring(L, 3, &plen); - const char *ctx = luaL_optlstring(L, 4, NULL, &ctxlen); - - switch (lmo_translate_plural_ctxt(n, skey, slen, pkey, plen, ctx, ctxlen, &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 }, - { "parse_string", template_L_parse_string }, - { "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 }, - { "get_translations", template_L_get_translations }, - { "translate", template_L_translate }, - { "ntranslate", template_L_ntranslate }, - { "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/luci-base/src/template_lualib.h b/modules/luci-base/src/template_lualib.h deleted file mode 100644 index ff7746d158..0000000000 --- a/modules/luci-base/src/template_lualib.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * LuCI Template - Lua library header - * - * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.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/luci-base/src/template_parser.c b/modules/luci-base/src/template_parser.c deleted file mode 100644 index 0ef08c63d2..0000000000 --- a/modules/luci-base/src/template_parser.c +++ /dev/null @@ -1,419 +0,0 @@ -/* - * LuCI Template - Parser implementation - * - * Copyright (C) 2009-2012 Jo-Philipp Wich <jow@openwrt.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; - 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->data = mmap(NULL, parser->size, PROT_READ, MAP_PRIVATE, - parser->fd, 0); - - if (parser->data != MAP_FAILED) - { - 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; -} - -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) - return; - - if (parser->gc != NULL) - free(parser->gc); - - /* 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); - } - - free(parser); -} - -void template_text(struct template_parser *parser, const char *e) -{ - const char *s = parser->off; - - if (s < (parser->data + 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->data); - 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->data + parser->size); - parser->off = parser->data + 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->data + 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->data + parser->size; - err = "'%>' expected before end of file"; - chunkline = 0; - } - - for (ptr = parser->data; ptr < off; ptr++) - if (*ptr == '\n') - line++; - - snprintf(msg, sizeof(msg), "Syntax error in %s:%d: %s", - parser->file ? parser->file : "[string]", line + chunkline, err ? err : "(unknown error)"); - - lua_pushnil(L); - lua_pushinteger(L, line + chunkline); - lua_pushstring(L, msg); - - return 3; -} diff --git a/modules/luci-base/src/template_parser.h b/modules/luci-base/src/template_parser.h deleted file mode 100644 index 2415e87079..0000000000 --- a/modules/luci-base/src/template_parser.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * LuCI Template - Parser header - * - * Copyright (C) 2009 Jo-Philipp Wich <jow@openwrt.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 *data; - 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); -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); -int template_error(lua_State *L, struct template_parser *parser); - -#endif diff --git a/modules/luci-base/src/template_utils.c b/modules/luci-base/src/template_utils.c deleted file mode 100644 index 8580405e32..0000000000 --- a/modules/luci-base/src/template_utils.c +++ /dev/null @@ -1,500 +0,0 @@ -/* - * LuCI Template - Utility functions - * - * Copyright (C) 2010 Jo-Philipp Wich <jow@openwrt.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 beyond 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, """, 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) -{ - int trlen, idlen = l, ctxtlen = 0, esc = 0; - const char *p, *msgid = s, *msgctxt = NULL; - char *tr; - - for (p = s; p < s + l; p++) { - if (esc) { - esc = 0; - } - else if (*p == '\\') { - esc = 1; - } - else if (*p == '|') { - idlen = p - s; - msgctxt = p + 1; - ctxtlen = s + l - msgctxt; - break; - } - } - - if (!lmo_translate_ctxt(msgid, idlen, msgctxt, ctxtlen, &tr, &trlen)) - luastr_escape(out, tr, trlen, escape_xml); - else - luastr_escape(out, s, l, escape_xml); -} diff --git a/modules/luci-base/src/template_utils.h b/modules/luci-base/src/template_utils.h deleted file mode 100644 index 32a79f93bc..0000000000 --- a/modules/luci-base/src/template_utils.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * LuCI Template - Utility header - * - * Copyright (C) 2010-2012 Jo-Philipp Wich <jow@openwrt.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 |