[tyndur-devel] [PATCH 2/3] tms: Implementierung für C
Kevin Wolf ·
+ tms: Einfache Implementierung für mehrsprachige Programme in C durch Umbiegen von Pointern auf die jeweilige Übersetzung. Eine Umsetzung von FormatNum in C fehlt noch. Signed-off-by: Kevin Wolf <kevin at tyndur.org> --- src/modules/include/tms.h | 47 +++++++++++++++++++++++++++++ src/modules/lib/tms.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 0 deletions(-) create mode 100644 src/modules/lib/tms.c diff --git a/src/modules/include/tms.h b/src/modules/include/tms.h index c7a6823..357333b 100644 --- a/src/modules/include/tms.h +++ b/src/modules/include/tms.h @@ -45,4 +45,51 @@ struct tms_lang { #define LANGUAGE(x) \ static const void* __attribute__((section("tmslang"), used)) __lang = (x); +#define tms_glue(x, y, z) x ## y ## ## z +#define tms_xglue(x, y, z) tms_glue(x, y, z) +#define tms_stringify(x) #x +#define tms_xstringify(x) tms_stringify(x) + +/** + * Das Makro TMS_MODULE kann vor dem Einbinden dieses Headers optional gesetzt + * werden und enthält dann den Namen des einbindenden Moduls. Dieser Name wird + * in die Symbolnamen der Strings eingebaut. + */ +#ifndef TMS_MODULE +#define TMS_XMODULE +#else +#define TMS_XMODULE tms_xglue(TMS_MODULE, _,) +#endif + +#define TMS_NAME(sym) tms_xglue(__tms_, TMS_XMODULE, sym) +#define TMS_SNAME(sym) tms_xstringify(TMS_NAME(sym)) + +/** + * Definiert einen übersetzbaren String. Das Makro wird an Stelle eines + * Stringliterals verwendet (z.B. printf(TMS(hello, "Hello World\n"))) + * + * @param sym Eindeutiger Bezeichner für den String. Das Makro legt eine + * Variable der Form __tms_module_sym, wobei module für TMS_MODULE steht. + * @param string Der zu übersetzende String. Wenn keine Übersetzung + * verfügbar ist, wird dieser String benutzt + */ +#define TMS(sym, string) \ + ({ \ + asm volatile( \ + ".section tmsstring,\"aw\", at progbits\n" \ + ".global " TMS_SNAME(sym) "\n" \ + TMS_SNAME(sym)":\n"\ + ".int %P0\n" \ + ".section .text" : : "i" (string)); \ + extern void* TMS_NAME(sym); \ + TMS_NAME(sym); \ + }) + + +/** + * Initialisiert ein mehrsprachiges Programm durch Aktivieren der + * Übersetzungen, die zur Sprache in der Umgebungsvariablen LANG passen. + */ +void tms_init(void); + #endif diff --git a/src/modules/lib/tms.c b/src/modules/lib/tms.c new file mode 100644 index 0000000..30401d9 --- /dev/null +++ b/src/modules/lib/tms.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <tms.h> +#include <string.h> +#include <env.h> + +extern struct tms_lang* __start_tmslang; +extern struct tms_lang* __stop_tmslang; + +/** + * Lädt die Übersetzungen einer bestimmten Sprache. + * + * @param lang Sprachcode der zu ladenden Sprache + */ +static void init_lang(const char* lang) +{ + struct tms_lang** plang; + const struct tms_strings* strings; + + plang= &__start_tmslang; + while (plang < &__stop_tmslang) { + + if (!strcmp((*plang)->lang, lang)) { + strings = (*plang)->strings; + while (strings->resstr) { + *(const char**)strings->resstr = strings->translation; + strings++; + } + break; + } + + plang++; + } +} + +/** + * Initialisiert ein mehrsprachiges Programm durch Aktivieren der + * Übersetzungen, die zur Sprache in der Umgebungsvariablen LANG passen. + */ +void tms_init(void) +{ + const char* lang = getenv("LANG"); + + if (lang != NULL) { + init_lang(lang); + } +} -- 1.6.0.2
Antworten
- [tyndur-devel] [PATCH 2/3] tms: Implementierung für C · Antoine Kaufmann