Index: trunk/src/modules/vterm/output.c =================================================================== --- trunk.orig/src/modules/vterm/output.c +++ trunk/src/modules/vterm/output.c @@ -151,9 +151,15 @@ void vterm_output_change(vterminal_t* ol void vterm_process_output(vterminal_t* vterm, char* data, size_t length) { vterm_output_t* out = &(vterm->output); + char cpdata[length + vterm->utf8_buffer_offset]; + int len; + + if ((len = utf8_to_cp437(vterm, data, length, cpdata)) == 0) { + return; + } // Ausgaben durch vt100-Emulation taetigen - vt100_process_output(vterm, data, length); + vt100_process_output(vterm, cpdata, len); // Anzeige aktualisieren falls es sich um aenderungen auf dem aktiven // Terminal handelt. Index: trunk/src/modules/vterm/utf8.c =================================================================== --- /dev/null +++ trunk/src/modules/vterm/utf8.c @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2007 The LOST Project. All rights reserved. + * + * This code is derived from software contributed to the LOST Project + * by Antoine Kaufmann. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the LOST Project + * and its contributors. + * 4. Neither the name of the LOST Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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 +#include +#include +#include "vterm.h" + +/** + * Zeichentabelle um Unicode-Zeichen in CP437 zu ersetzen + */ +struct { + wchar_t uc; + char cp; +} uc_to_cp [] = { + {L'ä', 0x84}, // ä + {L'ö', 0x94}, // ö + {L'ü', 0x81}, // ü + {L'Ä', 0x8E}, // Ä + {L'Ö', 0x99}, // Ö + {L'Ü', 0x9A}, // Ü + {L'ß', 0xE1}, // ß + + {L'«', 0xAE}, // « + {L'»', 0xAF}, // » +}; + +/** + * Unicode-Zeichen in der Tabelle nachschlagen und in cp437 umwandeln + * + * @param wc Unicode-Zeichen + * + * @return cp437-Zeichen oder 0 wenn keines gefunden wurde + */ +static char to_cp437(wchar_t wc) +{ + int i; + + // ASCII + if (wc <= 0x7F) { + return (char) wc; + } + + // Tabelle durchsuchen + for (i = 0; i < sizeof(uc_to_cp) / sizeof(uc_to_cp[0]); i++) { + if (wc == uc_to_cp[i].uc) { + return uc_to_cp[i].cp; + } + } + return 0x00; +} + +/** + * Wandelt das Zeichen in cp437 um oder legt es in den utf8 Buffer, falls es + * ein Teil eines Zeichens ist, das mehrere Bytes belegt + * + * @param c Das Zeichen + * @param dest Pointer auf die Speicherstelle in der das cp437-Zeichen + * abgelegt wird + * + * @return TRUE wenn das Zeichen umgewandelt wurde, FALSE sonst + */ +static bool convert_char(vterminal_t* vterm, char c, char* dest) +{ + int len = vterm->utf8_buffer_offset; + wchar_t wc; + + // Erstes Zeichen im Buffer abschneiden bei mehr als 4, danach das neue + // anhaengen + if (len >= 4) { + len = 3; + memmove(vterm->utf8_buffer, vterm->utf8_buffer + 1, len); + } + vterm->utf8_buffer[len++] = c; + + + // Versuchen ein Zeichen zu konvertieren + if (mbtowc(&wc, vterm->utf8_buffer, len) == -1) { + // Wenn das nicht klappt, wird das ganze im Buffer gelassen + vterm->utf8_buffer_offset = len; + return FALSE; + } else { + *dest = to_cp437(wc); + vterm->utf8_buffer_offset = 0; + + // Bei einem Zeichen, das nicht kopiert werden kann, wird ein ? als + // Platzhalter geschrieben + if (!*dest) { + *dest = '?'; + } + return TRUE; + } + +} + +int utf8_to_cp437(vterminal_t* vterm, const char* str, size_t len, char* buf) +{ + int l = 0; + int i; + + for (i = 0; i < len; i++) { + if (convert_char(vterm, str[i], buf + l)) { + l++; + } + } + return l; +} + + Index: trunk/src/modules/vterm/vterm.h =================================================================== --- trunk.orig/src/modules/vterm/vterm.h +++ trunk/src/modules/vterm/vterm.h @@ -151,6 +151,12 @@ typedef struct { /// Anzahl der Zeichen im vt100-Puffer size_t vt100_buffer_offset; + + /// UTF-8 Puffer + char utf8_buffer[4]; + + /// Anzahl der Zeichen im utf-8 Puffer + size_t utf8_buffer_offset; } vterminal_t; @@ -204,3 +210,6 @@ extern inline void buffer_position(vterm /// Ausgabe in vt100-Emulation verarbeiten void vt100_process_output(vterminal_t* vterm, char* data, size_t length); +/// UTF8-String in Codepage437-String verwandeln +int utf8_to_cp437(vterminal_t* vterm, const char* str, size_t len, char* buf); +