Blame


1 00dfcb92 2018-06-11 stsp /*
2 00dfcb92 2018-06-11 stsp * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
3 00dfcb92 2018-06-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 00dfcb92 2018-06-11 stsp *
5 00dfcb92 2018-06-11 stsp * Permission to use, copy, modify, and distribute this software for any
6 00dfcb92 2018-06-11 stsp * purpose with or without fee is hereby granted, provided that the above
7 00dfcb92 2018-06-11 stsp * copyright notice and this permission notice appear in all copies.
8 00dfcb92 2018-06-11 stsp *
9 00dfcb92 2018-06-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 00dfcb92 2018-06-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 00dfcb92 2018-06-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 00dfcb92 2018-06-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 00dfcb92 2018-06-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 00dfcb92 2018-06-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 00dfcb92 2018-06-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 00dfcb92 2018-06-11 stsp */
17 00dfcb92 2018-06-11 stsp
18 ebc5bf64 2020-09-19 naddy #include <sys/types.h>
19 ebc5bf64 2020-09-19 naddy
20 00dfcb92 2018-06-11 stsp #include <err.h>
21 00dfcb92 2018-06-11 stsp #include <stdlib.h>
22 00dfcb92 2018-06-11 stsp #include <string.h>
23 00dfcb92 2018-06-11 stsp #include <wchar.h>
24 84717ec7 2018-09-02 stsp #include <langinfo.h>
25 00dfcb92 2018-06-11 stsp
26 00dfcb92 2018-06-11 stsp #include "got_error.h"
27 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
28 00dfcb92 2018-06-11 stsp
29 00dfcb92 2018-06-11 stsp const struct got_error *
30 00dfcb92 2018-06-11 stsp got_mbsavis(char** outp, int *widthp, const char *mbs)
31 00dfcb92 2018-06-11 stsp {
32 00dfcb92 2018-06-11 stsp const char *src; /* Iterate mbs. */
33 00dfcb92 2018-06-11 stsp char *dst; /* Iterate *outp. */
34 00dfcb92 2018-06-11 stsp wchar_t wc;
35 00dfcb92 2018-06-11 stsp int total_width; /* Display width of the whole string. */
36 00dfcb92 2018-06-11 stsp int width; /* Display width of a single Unicode char. */
37 00dfcb92 2018-06-11 stsp int len; /* Length in bytes of UTF-8 encoded string. */
38 00dfcb92 2018-06-11 stsp
39 00dfcb92 2018-06-11 stsp len = strlen(mbs);
40 00dfcb92 2018-06-11 stsp if ((*outp = malloc(len + 1)) == NULL)
41 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
42 00dfcb92 2018-06-11 stsp
43 00dfcb92 2018-06-11 stsp if (MB_CUR_MAX == 1) {
44 00dfcb92 2018-06-11 stsp memcpy(*outp, mbs, len + 1);
45 00dfcb92 2018-06-11 stsp *widthp = len;
46 00dfcb92 2018-06-11 stsp return NULL;
47 00dfcb92 2018-06-11 stsp }
48 00dfcb92 2018-06-11 stsp
49 00dfcb92 2018-06-11 stsp src = mbs;
50 00dfcb92 2018-06-11 stsp dst = *outp;
51 00dfcb92 2018-06-11 stsp total_width = 0;
52 00dfcb92 2018-06-11 stsp while (*src != '\0') {
53 00dfcb92 2018-06-11 stsp if ((len = mbtowc(&wc, src, MB_CUR_MAX)) == -1) {
54 00dfcb92 2018-06-11 stsp total_width++;
55 00dfcb92 2018-06-11 stsp *dst++ = '?';
56 00dfcb92 2018-06-11 stsp src++;
57 00dfcb92 2018-06-11 stsp } else if ((width = wcwidth(wc)) == -1) {
58 00dfcb92 2018-06-11 stsp total_width++;
59 00dfcb92 2018-06-11 stsp *dst++ = '?';
60 00dfcb92 2018-06-11 stsp src += len;
61 00dfcb92 2018-06-11 stsp } else {
62 00dfcb92 2018-06-11 stsp total_width += width;
63 00dfcb92 2018-06-11 stsp while (len-- > 0)
64 00dfcb92 2018-06-11 stsp *dst++ = *src++;
65 00dfcb92 2018-06-11 stsp }
66 00dfcb92 2018-06-11 stsp }
67 00dfcb92 2018-06-11 stsp *dst = '\0';
68 00dfcb92 2018-06-11 stsp *widthp = total_width;
69 00dfcb92 2018-06-11 stsp return NULL;
70 00dfcb92 2018-06-11 stsp }
71 84717ec7 2018-09-02 stsp
72 84717ec7 2018-09-02 stsp int
73 84717ec7 2018-09-02 stsp got_locale_is_utf8(void)
74 84717ec7 2018-09-02 stsp {
75 84717ec7 2018-09-02 stsp char *codeset = nl_langinfo(CODESET);
76 84717ec7 2018-09-02 stsp return (strcmp(codeset, "UTF-8") == 0);
77 84717ec7 2018-09-02 stsp }