From patchwork Sat Apr 27 18:33:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 10920471 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8EDD01395 for ; Sat, 27 Apr 2019 18:34:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7D42A27FA8 for ; Sat, 27 Apr 2019 18:34:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 71D7328420; Sat, 27 Apr 2019 18:34:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C597D27FA8 for ; Sat, 27 Apr 2019 18:34:52 +0000 (UTC) Received: from localhost ([127.0.0.1]:35177 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKSA0-00018O-5y for patchwork-qemu-devel@patchwork.kernel.org; Sat, 27 Apr 2019 14:34:52 -0400 Received: from eggs.gnu.org ([209.51.188.92]:53175) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKS8e-0000H0-K9 for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hKS8d-0005AA-2h for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:28 -0400 Received: from hera.aquilenet.fr ([185.233.100.1]:40806) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hKS8c-00059W-PC for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:27 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id A7E199E68; Sat, 27 Apr 2019 20:33:25 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ur0SmhYzuMQk; Sat, 27 Apr 2019 20:33:24 +0200 (CEST) Received: from function (105.251.129.77.rev.sfr.net [77.129.251.105]) by hera.aquilenet.fr (Postfix) with ESMTPSA id 6F4573633; Sat, 27 Apr 2019 20:33:23 +0200 (CEST) Received: from samy by function with local (Exim 4.92) (envelope-from ) id 1hKS8M-0003LA-BZ; Sat, 27 Apr 2019 20:33:10 +0200 From: Samuel Thibault To: qemu-devel@nongnu.org Date: Sat, 27 Apr 2019 20:33:06 +0200 Message-Id: <20190427183307.12796-2-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190427183307.12796-1-samuel.thibault@ens-lyon.org> References: <20190427183307.12796-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 185.233.100.1 Subject: [Qemu-devel] [PATCHv4 1/2] ui/curses: do not assume wchar_t contains unicode X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Thibault , kamil@netbsd.org, Kamil Rytarowski , kraxel@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP E.g. BSD and Solaris even use locale-specific encoding there. We thus have to go through the native multibyte representation and use mbrtowc/wcrtomb to make a proper conversion. Signed-off-by: Samuel Thibault Tested-by: Kamil Rytarowski --- ui/curses.c | 157 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 57 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index fb63945188..81d419879e 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -400,65 +400,108 @@ static void curses_atexit(void) endwin(); } +/* + * In the following: + * - fch is the font glyph number + * - uch is the unicode value + * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris) + * - mbch is the native local-dependent multibyte representation + */ + /* Setup wchar glyph for one UCS-2 char */ -static void convert_ucs(int glyph, uint16_t ch, iconv_t conv) +static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) { + char mbch[MB_LEN_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch = (char *) &ch; - pwch = (char *) &wch; - sch = sizeof(ch); - swch = sizeof(wch); + char *puch, *pmbch; + size_t such, smbch; + mbstate_t ps; + + puch = (char *) &uch; + pmbch = (char *) mbch; + such = sizeof(uch); + smbch = sizeof(mbch); + + if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from UCS-2 to a multibyte character: %s\n", + uch, strerror(errno)); + return; + } - if (iconv(conv, &pch, &sch, &pwch, &swch) == (size_t) -1) { - fprintf(stderr, "Could not convert 0x%04x from UCS-2 to WCHAR_T: %s\n", - ch, strerror(errno)); - } else { - vga_to_curses[glyph].chars[0] = wch; + memset(&ps, 0, sizeof(ps)); + if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from a multibyte character to wchar_t: %s\n", + uch, strerror(errno)); + return; } + vga_to_curses[fch].chars[0] = wch; } /* Setup wchar glyph for one font character */ -static void convert_font(unsigned char ch, iconv_t conv) +static void convert_font(unsigned char fch, iconv_t conv) { + char mbch[MB_LEN_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch = (char *) &ch; - pwch = (char *) &wch; - sch = sizeof(ch); - swch = sizeof(wch); + char *pfch, *pmbch; + size_t sfch, smbch; + mbstate_t ps; + + pfch = (char *) &fch; + pmbch = (char *) &mbch; + sfch = sizeof(fch); + smbch = sizeof(mbch); + + if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) { + fprintf(stderr, "Could not convert font glyph 0x%02x " + "from %s to a multibyte character: %s\n", + fch, font_charset, strerror(errno)); + return; + } - if (iconv(conv, &pch, &sch, &pwch, &swch) == (size_t) -1) { - fprintf(stderr, "Could not convert 0x%02x from %s to WCHAR_T: %s\n", - ch, font_charset, strerror(errno)); - } else { - vga_to_curses[ch].chars[0] = wch; + memset(&ps, 0, sizeof(ps)); + if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { + fprintf(stderr, "Could not convert font glyph 0x%02x " + "from a multibyte character to wchar_t: %s\n", + fch, strerror(errno)); + return; } + vga_to_curses[fch].chars[0] = wch; } /* Convert one wchar to UCS-2 */ static uint16_t get_ucs(wchar_t wch, iconv_t conv) { - uint16_t ch; - char *pch, *pwch; - size_t sch, swch; - - pch = (char *) &ch; - pwch = (char *) &wch; - sch = sizeof(ch); - swch = sizeof(wch); - - if (iconv(conv, &pwch, &swch, &pch, &sch) == (size_t) -1) { - fprintf(stderr, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n", - (unsigned long)wch, strerror(errno)); + char mbch[MB_LEN_MAX]; + uint16_t uch; + char *pmbch, *puch; + size_t smbch, such; + mbstate_t ps; + int ret; + + memset(&ps, 0, sizeof(ps)); + ret = wcrtomb(mbch, wch, &ps); + if (ret == -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from wchar_t to a multibyte character: %s\n", + wch, strerror(errno)); + return 0xFFFD; + } + + pmbch = (char *) mbch; + puch = (char *) &uch; + smbch = ret; + such = sizeof(uch); + + if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from a multibyte character to UCS-2 : %s\n", + wch, strerror(errno)); return 0xFFFD; } - return ch; + return uch; } /* @@ -466,6 +509,11 @@ static uint16_t get_ucs(wchar_t wch, iconv_t conv) */ static void font_setup(void) { + iconv_t ucs2_to_nativecharset; + iconv_t nativecharset_to_ucs2; + iconv_t font_conv; + int i; + /* * Control characters are normally non-printable, but VGA does have * well-known glyphs for them. @@ -505,30 +553,25 @@ static void font_setup(void) 0x25bc }; - iconv_t ucs_to_wchar_conv; - iconv_t wchar_to_ucs_conv; - iconv_t font_conv; - int i; - - ucs_to_wchar_conv = iconv_open("WCHAR_T", "UCS-2"); - if (ucs_to_wchar_conv == (iconv_t) -1) { + ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2"); + if (ucs2_to_nativecharset == (iconv_t) -1) { fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", strerror(errno)); exit(1); } - wchar_to_ucs_conv = iconv_open("UCS-2", "WCHAR_T"); - if (wchar_to_ucs_conv == (iconv_t) -1) { - iconv_close(ucs_to_wchar_conv); + nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET)); + if (nativecharset_to_ucs2 == (iconv_t) -1) { + iconv_close(ucs2_to_nativecharset); fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", strerror(errno)); exit(1); } - font_conv = iconv_open("WCHAR_T", font_charset); + font_conv = iconv_open(nl_langinfo(CODESET), font_charset); if (font_conv == (iconv_t) -1) { - iconv_close(ucs_to_wchar_conv); - iconv_close(wchar_to_ucs_conv); + iconv_close(ucs2_to_nativecharset); + iconv_close(nativecharset_to_ucs2); fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n", font_charset, strerror(errno)); exit(1); @@ -536,7 +579,7 @@ static void font_setup(void) /* Control characters */ for (i = 0; i <= 0x1F; i++) { - convert_ucs(i, control_characters[i], ucs_to_wchar_conv); + convert_ucs(i, control_characters[i], ucs2_to_nativecharset); } for (i = 0x20; i <= 0xFF; i++) { @@ -544,12 +587,12 @@ static void font_setup(void) } /* DEL */ - convert_ucs(0x7F, 0x2302, ucs_to_wchar_conv); + convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); if (strcmp(nl_langinfo(CODESET), "UTF-8")) { /* Non-Unicode capable, use termcap equivalents for those available */ for (i = 0; i <= 0xFF; i++) { - switch (get_ucs(vga_to_curses[i].chars[0], wchar_to_ucs_conv)) { + switch (get_ucs(vga_to_curses[i].chars[0], nativecharset_to_ucs2)) { case 0x00a3: vga_to_curses[i] = *WACS_STERLING; break; @@ -649,8 +692,8 @@ static void font_setup(void) } } } - iconv_close(ucs_to_wchar_conv); - iconv_close(wchar_to_ucs_conv); + iconv_close(ucs2_to_nativecharset); + iconv_close(nativecharset_to_ucs2); iconv_close(font_conv); } From patchwork Sat Apr 27 18:33:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 10920469 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 18F661395 for ; Sat, 27 Apr 2019 18:34:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 05F4627FA8 for ; Sat, 27 Apr 2019 18:34:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC6D628420; Sat, 27 Apr 2019 18:34:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 793E727FA8 for ; Sat, 27 Apr 2019 18:34:51 +0000 (UTC) Received: from localhost ([127.0.0.1]:35175 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKS9y-00017q-Nf for patchwork-qemu-devel@patchwork.kernel.org; Sat, 27 Apr 2019 14:34:50 -0400 Received: from eggs.gnu.org ([209.51.188.92]:53172) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKS8e-0000Gv-8r for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hKS8d-0005A4-1R for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:28 -0400 Received: from hera.aquilenet.fr ([185.233.100.1]:40794) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hKS8c-00058x-M8 for qemu-devel@nongnu.org; Sat, 27 Apr 2019 14:33:26 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 56C1FA488; Sat, 27 Apr 2019 20:33:25 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at aquilenet.fr Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 95xXsz479cxW; Sat, 27 Apr 2019 20:33:24 +0200 (CEST) Received: from function (105.251.129.77.rev.sfr.net [77.129.251.105]) by hera.aquilenet.fr (Postfix) with ESMTPSA id D82DF9E68; Sat, 27 Apr 2019 20:33:23 +0200 (CEST) Received: from samy by function with local (Exim 4.92) (envelope-from ) id 1hKS8M-0003LC-V2; Sat, 27 Apr 2019 20:33:10 +0200 From: Samuel Thibault To: qemu-devel@nongnu.org Date: Sat, 27 Apr 2019 20:33:07 +0200 Message-Id: <20190427183307.12796-3-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190427183307.12796-1-samuel.thibault@ens-lyon.org> References: <20190427183307.12796-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 185.233.100.1 Subject: [Qemu-devel] [PATCHv4 2/2] ui/curses: manipulate cchar_t with standard curses functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Thibault , kamil@netbsd.org, Kamil Rytarowski , kraxel@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The chars/attr fields are curses internals, setcchar and getcchar have to be used instead. Signed-off-by: Samuel Thibault Tested-by: Kamil Rytarowski --- ui/curses.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index 81d419879e..1f3fcabb00 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -66,20 +66,22 @@ static void curses_update(DisplayChangeListener *dcl, { console_ch_t *line; cchar_t curses_line[width]; + wchar_t wch[CCHARW_MAX]; + attr_t attrs; + short colors; + int ret; line = screen + y * width; for (h += y; y < h; y ++, line += width) { for (x = 0; x < width; x++) { chtype ch = line[x] & 0xff; chtype at = line[x] & ~0xff; - if (vga_to_curses[ch].chars[0]) { - curses_line[x] = vga_to_curses[ch]; - } else { - curses_line[x] = (cchar_t) { - .chars[0] = ch, - }; + ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL); + if (ret == ERR || wch[0] == 0) { + wch[0] = ch; + wch[1] = 0; } - curses_line[x].attr |= at; + setcchar(&curses_line[x], wch, at, 0, NULL); } mvwadd_wchnstr(screenpad, y, 0, curses_line, width); } @@ -412,7 +414,7 @@ static void curses_atexit(void) static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) { char mbch[MB_LEN_MAX]; - wchar_t wch; + wchar_t wch[2]; char *puch, *pmbch; size_t such, smbch; mbstate_t ps; @@ -430,20 +432,22 @@ static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) } memset(&ps, 0, sizeof(ps)); - if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { + if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { fprintf(stderr, "Could not convert 0x%04x " "from a multibyte character to wchar_t: %s\n", uch, strerror(errno)); return; } - vga_to_curses[fch].chars[0] = wch; + + wch[1] = 0; + setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); } /* Setup wchar glyph for one font character */ static void convert_font(unsigned char fch, iconv_t conv) { char mbch[MB_LEN_MAX]; - wchar_t wch; + wchar_t wch[2]; char *pfch, *pmbch; size_t sfch, smbch; mbstate_t ps; @@ -461,13 +465,15 @@ static void convert_font(unsigned char fch, iconv_t conv) } memset(&ps, 0, sizeof(ps)); - if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { + if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { fprintf(stderr, "Could not convert font glyph 0x%02x " "from a multibyte character to wchar_t: %s\n", fch, strerror(errno)); return; } - vga_to_curses[fch].chars[0] = wch; + + wch[1] = 0; + setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); } /* Convert one wchar to UCS-2 */ @@ -592,7 +598,16 @@ static void font_setup(void) if (strcmp(nl_langinfo(CODESET), "UTF-8")) { /* Non-Unicode capable, use termcap equivalents for those available */ for (i = 0; i <= 0xFF; i++) { - switch (get_ucs(vga_to_curses[i].chars[0], nativecharset_to_ucs2)) { + wchar_t wch[CCHARW_MAX]; + attr_t attr; + short color; + int ret; + + ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL); + if (ret == ERR) + continue; + + switch (get_ucs(wch[0], nativecharset_to_ucs2)) { case 0x00a3: vga_to_curses[i] = *WACS_STERLING; break;