Blob


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