From nobody Mon Feb 9 18:22:56 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1556383241; cv=none; d=zoho.com; s=zohoarc; b=oo9fFi2+/+mhs6AFM7FsFN691XOm99tPYp47bz5OIlVMGCQF/IFhARmEKpPc7jBHtxqqZtbqiAoIuY/IsxtgxjG//nQ58PixM9tqea0ArlXbRBCmh0JR+OUfzQhnhCBJ6nWO5kNstSR+8pdE4Q0W/qFMurJSqiKGo/1iIS2TL98= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556383241; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=7p2T2ckqniQa4Ey3BLhYOG5tXLG6wV8YDba455hMuXo=; b=VF7AY0L04BjZYxIlFmZs954tZSFPdtXl8yaceYmL8hvWTpAjuX3Y/J52Wj6co1FGg03SODN17sxppVpHbW//yn/z3S9ZYOLHVZQt0MrP02AcmTTq9e20KJAuJJ2xM+jYnu09KVtQGlLD30sQvNqr9qE/qI5WoFWXduXo3mxAJeg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1556383241075336.58170084628466; Sat, 27 Apr 2019 09:40:41 -0700 (PDT) Received: from localhost ([127.0.0.1]:34066 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQNS-00034d-1K for importer@patchew.org; Sat, 27 Apr 2019 12:40:38 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQKi-0001Ae-4Q for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:37:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hKQDb-00059r-8m for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:28 -0400 Received: from hera.aquilenet.fr ([2a0c:e300::1]:47178) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hKQDZ-0004UD-FG for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:27 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 6DC163DA4; Sat, 27 Apr 2019 18:30:13 +0200 (CEST) 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 yUhBorprDrsA; Sat, 27 Apr 2019 18:30:11 +0200 (CEST) Received: from function (105.251.129.77.rev.sfr.net [77.129.251.105]) by hera.aquilenet.fr (Postfix) with ESMTPSA id DEB6A3D9E; Sat, 27 Apr 2019 18:30:10 +0200 (CEST) Received: from samy by function with local (Exim 4.92) (envelope-from ) id 1hKQDI-0001LI-I5; Sat, 27 Apr 2019 18:30:08 +0200 X-Virus-Scanned: Debian amavisd-new at aquilenet.fr From: Samuel Thibault To: qemu-devel@nongnu.org Date: Sat, 27 Apr 2019 18:30:06 +0200 Message-Id: <20190427163007.5113-3-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> References: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a0c:e300::1 Subject: [Qemu-devel] [PATCHv3 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, kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" E.g. BSD and Solaris even use locale-specific encoding there. We thus have to go through the native multibyte representation and use mbtowc/wctomb to make a proper conversion. Signed-off-by: Samuel Thibault --- ui/curses.c | 151 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 57 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index fb63945188..395f9545e9 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -400,65 +400,102 @@ static void curses_atexit(void) endwin(); } =20 +/* + * 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_CUR_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); + char *puch, *pmbch; + size_t such, smbch; + + puch =3D (char *) &uch; + pmbch =3D (char *) mbch; + such =3D sizeof(uch); + smbch =3D sizeof(mbch); + + if (iconv(conv, &puch, &such, &pmbch, &smbch) =3D=3D (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from UCS-2 to a multibyte character: %s\n", + uch, strerror(errno)); + return; + } =20 - if (iconv(conv, &pch, &sch, &pwch, &swch) =3D=3D (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] =3D wch; + if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; } =20 /* 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_CUR_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); + char *pfch, *pmbch; + size_t sfch, smbch; + + pfch =3D (char *) &fch; + pmbch =3D (char *) &mbch; + sfch =3D sizeof(fch); + smbch =3D sizeof(mbch); + + if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) =3D=3D (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; + } =20 - if (iconv(conv, &pch, &sch, &pwch, &swch) =3D=3D (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] =3D wch; + if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; } =20 /* 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 =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); - - if (iconv(conv, &pwch, &swch, &pch, &sch) =3D=3D (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_CUR_MAX]; + uint16_t uch; + char *pmbch, *puch; + size_t smbch, such; + int ret; + + ret =3D wctomb(mbch, wch); + if (ret =3D=3D -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from wchar_t to a multibyte character: %s\n", + wch, strerror(errno)); + return 0xFFFD; + } + + pmbch =3D (char *) mbch; + puch =3D (char *) &uch; + smbch =3D ret; + such =3D sizeof(uch); + + if (iconv(conv, &pmbch, &smbch, &puch, &such) =3D=3D (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from a multibyte character to UCS-2 : %s\n", + wch, strerror(errno)); return 0xFFFD; } =20 - return ch; + return uch; } =20 /* @@ -466,6 +503,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 +547,25 @@ static void font_setup(void) 0x25bc }; =20 - iconv_t ucs_to_wchar_conv; - iconv_t wchar_to_ucs_conv; - iconv_t font_conv; - int i; - - ucs_to_wchar_conv =3D iconv_open("WCHAR_T", "UCS-2"); - if (ucs_to_wchar_conv =3D=3D (iconv_t) -1) { + ucs2_to_nativecharset =3D iconv_open(nl_langinfo(CODESET), "UCS-2"); + if (ucs2_to_nativecharset =3D=3D (iconv_t) -1) { fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", strerror(errno)); exit(1); } =20 - wchar_to_ucs_conv =3D iconv_open("UCS-2", "WCHAR_T"); - if (wchar_to_ucs_conv =3D=3D (iconv_t) -1) { - iconv_close(ucs_to_wchar_conv); + nativecharset_to_ucs2 =3D iconv_open("UCS-2", nl_langinfo(CODESET)); + if (nativecharset_to_ucs2 =3D=3D (iconv_t) -1) { + iconv_close(ucs2_to_nativecharset); fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", strerror(errno)); exit(1); } =20 - font_conv =3D iconv_open("WCHAR_T", font_charset); + font_conv =3D iconv_open(nl_langinfo(CODESET), font_charset); if (font_conv =3D=3D (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 +573,7 @@ static void font_setup(void) =20 /* Control characters */ for (i =3D 0; i <=3D 0x1F; i++) { - convert_ucs(i, control_characters[i], ucs_to_wchar_conv); + convert_ucs(i, control_characters[i], ucs2_to_nativecharset); } =20 for (i =3D 0x20; i <=3D 0xFF; i++) { @@ -544,12 +581,12 @@ static void font_setup(void) } =20 /* DEL */ - convert_ucs(0x7F, 0x2302, ucs_to_wchar_conv); + convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); =20 if (strcmp(nl_langinfo(CODESET), "UTF-8")) { /* Non-Unicode capable, use termcap equivalents for those availabl= e */ for (i =3D 0; i <=3D 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_uc= s2)) { case 0x00a3: vga_to_curses[i] =3D *WACS_STERLING; break; @@ -649,8 +686,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); } =20 --=20 2.20.1 From nobody Mon Feb 9 18:22:56 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1556383232; cv=none; d=zoho.com; s=zohoarc; b=S//AfIdig7JJVXBCl3wdTC3MQpg9GjsaLGxRxz7r7Bo6xsuEAj34bTCbYKMyOT+x942qWwAN1ZikGhMPfU2tCwaTQADjkRuofWk/zyh1rc31FTSSBbJktLXNN9LzRixF7FlmIRX7uBnbmGO3RchKmLJ5RNFlR3IcIYApo5QV3Gg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556383232; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=iedg9JmlLMz5xFI+1Sh4E11OK4kvyxxZ32V4GYF92cU=; b=FcOWhVOQOk0qAdstly+TkMOTLMeuZkQ+eyUEgoV9xEYjlRSff8XJ6S05fq+rUXImhLemfXWo4ATKjTsgR2C+god82KslxbIoQSXaW1qQklIhKCV3yBJiYqByy3uomEnN5Iz2xzOQQscdCJlLbc+CqsDNG3mcUQyHebiazIMkPDI= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155638323267045.90721750534419; Sat, 27 Apr 2019 09:40:32 -0700 (PDT) Received: from localhost ([127.0.0.1]:34064 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQNI-0002yZ-LB for importer@patchew.org; Sat, 27 Apr 2019 12:40:28 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33736) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQKi-00017g-56 for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:37:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hKQDZ-0004zV-8k for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:27 -0400 Received: from hera.aquilenet.fr ([2a0c:e300::1]:47166) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hKQDT-0004U7-IV for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:23 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 253FB3D9F; Sat, 27 Apr 2019 18:30:13 +0200 (CEST) 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 v7LqIpfBdojU; Sat, 27 Apr 2019 18:30:12 +0200 (CEST) Received: from function (105.251.129.77.rev.sfr.net [77.129.251.105]) by hera.aquilenet.fr (Postfix) with ESMTPSA id 073863DA4; Sat, 27 Apr 2019 18:30:10 +0200 (CEST) Received: from samy by function with local (Exim 4.92) (envelope-from ) id 1hKQDI-0001LK-Pc; Sat, 27 Apr 2019 18:30:08 +0200 X-Virus-Scanned: Debian amavisd-new at aquilenet.fr From: Samuel Thibault To: qemu-devel@nongnu.org Date: Sat, 27 Apr 2019 18:30:07 +0200 Message-Id: <20190427163007.5113-4-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> References: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a0c:e300::1 Subject: [Qemu-devel] [PATCHv3 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, kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The chars/attr fields are curses internals, setcchar and getcchar have to be used instead. Signed-off-by: Samuel Thibault --- ui/curses.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index 395f9545e9..bc0d77a2a8 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; =20 line =3D screen + y * width; for (h +=3D y; y < h; y ++, line +=3D width) { for (x =3D 0; x < width; x++) { chtype ch =3D line[x] & 0xff; chtype at =3D line[x] & ~0xff; - if (vga_to_curses[ch].chars[0]) { - curses_line[x] =3D vga_to_curses[ch]; - } else { - curses_line[x] =3D (cchar_t) { - .chars[0] =3D ch, - }; + ret =3D getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NUL= L); + if (ret =3D=3D ERR || wch[0] =3D=3D 0) { + wch[0] =3D ch; + wch[1] =3D 0; } - curses_line[x].attr |=3D 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_CUR_MAX]; - wchar_t wch; + wchar_t wch[2]; char *puch, *pmbch; size_t such, smbch; =20 @@ -428,20 +430,22 @@ static void convert_ucs(unsigned char fch, uint16_t u= ch, iconv_t conv) return; } =20 - if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -1) { + if (mbtowc(&wch[0], mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; + + wch[1] =3D 0; + setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); } =20 /* Setup wchar glyph for one font character */ static void convert_font(unsigned char fch, iconv_t conv) { char mbch[MB_CUR_MAX]; - wchar_t wch; + wchar_t wch[2]; char *pfch, *pmbch; size_t sfch, smbch; =20 @@ -457,13 +461,15 @@ static void convert_font(unsigned char fch, iconv_t c= onv) return; } =20 - if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -1) { + if (mbtowc(&wch[0], mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; + + wch[1] =3D 0; + setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); } =20 /* Convert one wchar to UCS-2 */ @@ -586,7 +592,16 @@ static void font_setup(void) if (strcmp(nl_langinfo(CODESET), "UTF-8")) { /* Non-Unicode capable, use termcap equivalents for those availabl= e */ for (i =3D 0; i <=3D 0xFF; i++) { - switch (get_ucs(vga_to_curses[i].chars[0], nativecharset_to_uc= s2)) { + wchar_t wch[CCHARW_MAX]; + attr_t attr; + short color; + int ret; + + ret =3D getcchar(&vga_to_curses[i], wch, &attr, &color, NULL); + if (ret =3D=3D ERR) + continue; + + switch (get_ucs(wch[0], nativecharset_to_ucs2)) { case 0x00a3: vga_to_curses[i] =3D *WACS_STERLING; break; --=20 2.20.1 From nobody Mon Feb 9 18:22:56 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1556383206; cv=none; d=zoho.com; s=zohoarc; b=MHuO3qxYZykIt8nsUNp21fgTcsbsM/6403yPg8b1veYR3RpL79nzeNNALn8uVDrDvnSfIFZzLdP2ffD5AXUKjaaUQ2SfsBsb1B2LQ8JsJlmb7a00EPk1I8TiGvnB4OzCgMn5Pi+dKZ2Kf5uHnQuweomaYbzAjwqjpBjpDjbxb5E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556383206; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=7p2T2ckqniQa4Ey3BLhYOG5tXLG6wV8YDba455hMuXo=; b=TeKq5TODQEcrzZtjfgWwU19cBeiCBV30nMXDx5b+u1vqTgwDlaSeR0hSQzyfaf3qdy3tAT7/R3fc2UeTlQuOkoYuiwORilEU2VIWbJHn8kg44T0+uCmS03jSk0y4U2jbHVot3DOKkbkADR1vZPP5tsFWbLN3Brt3rSECyh+GRP4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155638320647777.45314002561224; Sat, 27 Apr 2019 09:40:06 -0700 (PDT) Received: from localhost ([127.0.0.1]:34060 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQMs-0002cn-Ql for importer@patchew.org; Sat, 27 Apr 2019 12:40:02 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33748) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hKQKi-00017k-4j for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:37:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hKQDb-00059W-89 for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:28 -0400 Received: from hera.aquilenet.fr ([185.233.100.1]:39202) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hKQDZ-0004TJ-8w for qemu-devel@nongnu.org; Sat, 27 Apr 2019 12:30:27 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 68C683D59; Sat, 27 Apr 2019 18:30:12 +0200 (CEST) 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 MiOmbn4vIOOu; Sat, 27 Apr 2019 18:30:11 +0200 (CEST) Received: from function (105.251.129.77.rev.sfr.net [77.129.251.105]) by hera.aquilenet.fr (Postfix) with ESMTPSA id EFCC53D9F; Sat, 27 Apr 2019 18:30:10 +0200 (CEST) Received: from samy by function with local (Exim 4.92) (envelope-from ) id 1hKQDI-0001LG-FA; Sat, 27 Apr 2019 18:30:08 +0200 X-Virus-Scanned: Debian amavisd-new at aquilenet.fr From: Samuel Thibault To: qemu-devel@nongnu.org Date: Sat, 27 Apr 2019 18:30:05 +0200 Message-Id: <20190427163007.5113-2-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> References: <20190427163007.5113-1-samuel.thibault@ens-lyon.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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] [PATCH] 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, kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" E.g. BSD and Solaris even use locale-specific encoding there. We thus have to go through the native multibyte representation and use mbtowc/wctomb to make a proper conversion. Signed-off-by: Samuel Thibault --- ui/curses.c | 151 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 57 deletions(-) diff --git a/ui/curses.c b/ui/curses.c index fb63945188..395f9545e9 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -400,65 +400,102 @@ static void curses_atexit(void) endwin(); } =20 +/* + * 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_CUR_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); + char *puch, *pmbch; + size_t such, smbch; + + puch =3D (char *) &uch; + pmbch =3D (char *) mbch; + such =3D sizeof(uch); + smbch =3D sizeof(mbch); + + if (iconv(conv, &puch, &such, &pmbch, &smbch) =3D=3D (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from UCS-2 to a multibyte character: %s\n", + uch, strerror(errno)); + return; + } =20 - if (iconv(conv, &pch, &sch, &pwch, &swch) =3D=3D (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] =3D wch; + if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; } =20 /* 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_CUR_MAX]; wchar_t wch; - char *pch, *pwch; - size_t sch, swch; - - pch =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); + char *pfch, *pmbch; + size_t sfch, smbch; + + pfch =3D (char *) &fch; + pmbch =3D (char *) &mbch; + sfch =3D sizeof(fch); + smbch =3D sizeof(mbch); + + if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) =3D=3D (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; + } =20 - if (iconv(conv, &pch, &sch, &pwch, &swch) =3D=3D (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] =3D wch; + if (mbtowc(&wch, mbch, sizeof(mbch) - smbch) =3D=3D -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] =3D wch; } =20 /* 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 =3D (char *) &ch; - pwch =3D (char *) &wch; - sch =3D sizeof(ch); - swch =3D sizeof(wch); - - if (iconv(conv, &pwch, &swch, &pch, &sch) =3D=3D (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_CUR_MAX]; + uint16_t uch; + char *pmbch, *puch; + size_t smbch, such; + int ret; + + ret =3D wctomb(mbch, wch); + if (ret =3D=3D -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from wchar_t to a multibyte character: %s\n", + wch, strerror(errno)); + return 0xFFFD; + } + + pmbch =3D (char *) mbch; + puch =3D (char *) &uch; + smbch =3D ret; + such =3D sizeof(uch); + + if (iconv(conv, &pmbch, &smbch, &puch, &such) =3D=3D (size_t) -1) { + fprintf(stderr, "Could not convert 0x%04x " + "from a multibyte character to UCS-2 : %s\n", + wch, strerror(errno)); return 0xFFFD; } =20 - return ch; + return uch; } =20 /* @@ -466,6 +503,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 +547,25 @@ static void font_setup(void) 0x25bc }; =20 - iconv_t ucs_to_wchar_conv; - iconv_t wchar_to_ucs_conv; - iconv_t font_conv; - int i; - - ucs_to_wchar_conv =3D iconv_open("WCHAR_T", "UCS-2"); - if (ucs_to_wchar_conv =3D=3D (iconv_t) -1) { + ucs2_to_nativecharset =3D iconv_open(nl_langinfo(CODESET), "UCS-2"); + if (ucs2_to_nativecharset =3D=3D (iconv_t) -1) { fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", strerror(errno)); exit(1); } =20 - wchar_to_ucs_conv =3D iconv_open("UCS-2", "WCHAR_T"); - if (wchar_to_ucs_conv =3D=3D (iconv_t) -1) { - iconv_close(ucs_to_wchar_conv); + nativecharset_to_ucs2 =3D iconv_open("UCS-2", nl_langinfo(CODESET)); + if (nativecharset_to_ucs2 =3D=3D (iconv_t) -1) { + iconv_close(ucs2_to_nativecharset); fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", strerror(errno)); exit(1); } =20 - font_conv =3D iconv_open("WCHAR_T", font_charset); + font_conv =3D iconv_open(nl_langinfo(CODESET), font_charset); if (font_conv =3D=3D (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 +573,7 @@ static void font_setup(void) =20 /* Control characters */ for (i =3D 0; i <=3D 0x1F; i++) { - convert_ucs(i, control_characters[i], ucs_to_wchar_conv); + convert_ucs(i, control_characters[i], ucs2_to_nativecharset); } =20 for (i =3D 0x20; i <=3D 0xFF; i++) { @@ -544,12 +581,12 @@ static void font_setup(void) } =20 /* DEL */ - convert_ucs(0x7F, 0x2302, ucs_to_wchar_conv); + convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); =20 if (strcmp(nl_langinfo(CODESET), "UTF-8")) { /* Non-Unicode capable, use termcap equivalents for those availabl= e */ for (i =3D 0; i <=3D 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_uc= s2)) { case 0x00a3: vga_to_curses[i] =3D *WACS_STERLING; break; @@ -649,8 +686,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); } =20 --=20 2.20.1