diff options
Diffstat (limited to 'package/webif/src')
-rw-r--r-- | package/webif/src/bstrip.c | 75 | ||||
-rw-r--r-- | package/webif/src/webif-page.c | 289 |
2 files changed, 364 insertions, 0 deletions
diff --git a/package/webif/src/bstrip.c b/package/webif/src/bstrip.c new file mode 100644 index 000000000..1e17d4e00 --- /dev/null +++ b/package/webif/src/bstrip.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define BUF_SIZE 1024 +#define READ_LEN 14 + +static int read_len = READ_LEN; +static char rbuf[32]; +static char rbuflen = 0; + +int do_gets(char *buf) +{ + int r = 0, c = 0; + char *m; + + if (rbuflen > 0) + memcpy(buf, rbuf, rbuflen); + c += rbuflen; + + while ((c + read_len < BUF_SIZE) && ((r = read(0, &buf[c], read_len)) > 0)) { + m = NULL; + + if ((m = memchr(&buf[c], '\n', r)) != NULL) { + rbuflen = r - (m - &buf[c] + 1); + if (rbuflen > 0) + memcpy(rbuf, m + 1, rbuflen); + c += m - &buf[c] + 1; + } else { + rbuflen = 0; + c += r; + } + + if ((c > 3) && (memcmp(&buf[c - 3], "---", 3) == 0)) + read_len = 1; + + if (m != NULL) + return c; + } + + return c; +} + +int main(int argc, char **argv) +{ + char buf[BUF_SIZE]; + char buf1[BUF_SIZE]; + char *tmp; + int len, r = 0, r1 = 0; + + if (argc != 2) { + fprintf(stderr, "Syntax: %s (name|data <boundary>)\n", argv[0]); + exit(1); + } + while (tmp = strchr(argv[1], '\r')) + *tmp = 0; + + len = strlen(argv[1]); + + *buf = 0; + while ((strncmp(buf, argv[1], len) != 0) && + (strncmp(buf + 2, argv[1], len) != 0)) { + if (r > 0) { + if (r1 > 0) + write (1, buf1, r1); + r1 = r; + memcpy(buf1, buf, r); + } + if ((r = do_gets(buf)) <= 0) + exit(1); + } + + if (r1 > 2) + write(1, buf1, r1 - 2); +} diff --git a/package/webif/src/webif-page.c b/package/webif/src/webif-page.c new file mode 100644 index 000000000..13e70937e --- /dev/null +++ b/package/webif/src/webif-page.c @@ -0,0 +1,289 @@ +/* + * Webif page translator + * + * Copyright (C) 2005 Felix Fietkau <nbd@openwrt.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <glob.h> +#include <ctype.h> +#ifdef NVRAM +#include <bcmnvram.h> +#endif + +#define HASH_MAX 100 +#define LINE_BUF 1024 /* max. buffer allocated for one line */ +#define MAX_TR 32 /* max. translations done on one line */ +#define TR_START "@TR<<" +#define TR_END ">>" + +struct lstr { + char *name; + char *value; + struct lstr *next; +}; +typedef struct lstr lstr; + +static lstr *ltable[HASH_MAX]; +static char buf[LINE_BUF], buf2[LINE_BUF]; + +/* djb2 hash function */ +static inline unsigned long hash(char *str) +{ + unsigned long hash = 5381; + int c; + + while ((c = *str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +} + +static inline char *translate_lookup(char *str) +{ + char *name, *def, *p, *res = NULL; + lstr *i; + int h; + + def = name = str; + if (((p = strchr(str, '|')) != NULL) + || ((p = strchr(str, '#')) != NULL)) { + def = p + 1; + *p = 0; + } + + h = hash(name) % HASH_MAX; + i = ltable[h]; + while ((res == NULL) && (i != NULL)) { + if (strcmp(name, i->name) == 0) + res = i->value; + i = i->next; + } + + if (res == NULL) + res = def; + + return res; +} + +static inline void add_line(char *name, char *value) +{ + int h = hash(name) % HASH_MAX; + lstr *s = malloc(sizeof(lstr)); + lstr *p; + + s->name = strdup(name); + s->value = strdup(value); + s->next = NULL; + + if (ltable[h] == NULL) + ltable[h] = s; + else { + for(p = ltable[h]; p->next != NULL; p = p->next); + p->next = s; + } +} + +static char *translate_line(char *line) +{ + static char *tok[MAX_TR * 3]; + char *l, *p, *p2, *res; + int len = 0, pos = 0, i; + + l = line; + while (l != NULL) { + if ((p = strstr(l, TR_START)) == NULL) { + len += strlen((tok[pos++] = l)); + break; + } + + p2 = strstr(p, TR_END); + if (p2 == NULL) + break; + + *p = 0; + *p2 = 0; + len += strlen((tok[pos++] = l)); + len += strlen((tok[pos++] = translate_lookup(p + strlen(TR_START)))); + + l = p2; + l += strlen(TR_END); + } + len++; + + if (len > LINE_BUF) + p = malloc(len); + else + p = buf2; + + p[0] = 0; + res = p; + for (i = 0; i < pos; i++) { + strcat(p, tok[i]); + p += strlen(tok[i]); + } + + return res; +} + +/* load and parse language file */ +static void load_lang(char *file) +{ + FILE *f; + char *b, *name, *value; + + f = fopen(file, "r"); + while (!feof(f) && (fgets(buf, LINE_BUF - 1, f) != NULL)) { + b = buf; + while (isspace(*b)) + b++; /* skip leading spaces */ + if (!*b) + continue; + + name = b; + if ((b = strstr(name, "=>")) == NULL) + continue; /* separator not found */ + + value = b + 2; + if (!*value) + continue; + + *b = 0; + for (b--; isspace(*b); b--) + *b = 0; /* remove trailing spaces */ + + while (isspace(*value)) + value++; /* skip leading spaces */ + + for (b = value + strlen(value) - 1; isspace(*b); b--) + *b = 0; /* remove trailing spaces */ + + if (!*value) + continue; + + add_line(name, value); + } +} + +int main (int argc, char **argv) +{ + FILE *f; + int len, i, done; + char line[LINE_BUF], *tmp, *arg; + glob_t langfiles; + char *lang = NULL; + char *proc = "/usr/bin/haserl"; + + memset(ltable, 0, HASH_MAX * sizeof(lstr *)); +#ifdef NVRAM + if ((lang = nvram_get("language")) != NULL) { +#else + if ((f = fopen("/etc/config/webif", "r")) != NULL) { + int n, i; + + while (!feof(f) && (lang == NULL)) { + fgets(line, LINE_BUF - 1, f); + + if (strncasecmp(line, "lang", 4) != 0) + goto nomatch; + + lang = line + 4; + while (isspace(*lang)) + lang++; + + if (*lang != '=') + goto nomatch; + + lang++; + + while (isspace(*lang)) + lang++; + + for (i = 0; isalpha(lang[i]) && (i < 32); i++); + lang[i] = 0; + continue; +nomatch: + lang = NULL; + } + fclose(f); +#endif + + sprintf(buf, "/usr/lib/webif/lang/%s/*.txt", lang); + i = glob(buf, GLOB_ERR | GLOB_MARK, NULL, &langfiles); + if (i == GLOB_NOSPACE || i == GLOB_ABORTED || i == GLOB_NOMATCH) { + // no language files found + } else { + for (i = 0; i < langfiles.gl_pathc; i++) { + load_lang(langfiles.gl_pathv[i]); + } + } + } + + /* + * command line options for this parser are stored in argv[1] only. + * filename to be processed is in argv[2] + */ + done = 0; + i = 1; + while (!done) { + if (argv[1] == NULL) { + done = 1; + } else if (strncmp(argv[1], "-e", 2) == 0) { + argv[1] = strchr(argv[1], ' '); + argv[1]++; + if (argv[1] != NULL) { + arg = argv[1]; + if ((tmp = strchr(argv[1], ' ')) != NULL) { + *tmp = 0; + argv[1] = &tmp[1]; + } else { + argv[1] = NULL; + i++; + } + system(arg); + } + } else if (strncmp(argv[1], "-p", 2) == 0) { + argv[1] = strchr(argv[1], ' '); + argv[1]++; + if (argv[1] != NULL) { + arg = argv[1]; + if ((tmp = strchr(argv[1], ' ')) != NULL) { + *tmp = 0; + argv[1] = &tmp[1]; + } else { + argv[1] = NULL; + i++; + } + proc = strdup(arg); + } + } else { + done = 1; + } + } + + strcpy(buf, proc); + while (argv[i]) { + sprintf(buf + strlen(buf), " %s", argv[i++]); + } + f = popen(buf, "r"); + + while (!feof(f) && (fgets(buf, LINE_BUF - 1, f)) != NULL) { + fprintf(stdout, "%s", translate_line(buf)); + fflush(stdout); + } + + return 0; +} |