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 <sys/types.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24 #include <langinfo.h>
26 #include "got_error.h"
27 #include "got_utf8.h"
29 const struct got_error *
30 got_mbsavis(char** outp, int *widthp, const char *mbs)
31 {
32 const char *src; /* Iterate mbs. */
33 char *dst; /* Iterate *outp. */
34 wchar_t wc;
35 int total_width; /* Display width of the whole string. */
36 int width; /* Display width of a single Unicode char. */
37 int len; /* Length in bytes of UTF-8 encoded string. */
39 len = strlen(mbs);
40 if ((*outp = malloc(len + 1)) == NULL)
41 return got_error_from_errno("malloc");
43 if (MB_CUR_MAX == 1) {
44 memcpy(*outp, mbs, len + 1);
45 *widthp = len;
46 return NULL;
47 }
49 src = mbs;
50 dst = *outp;
51 total_width = 0;
52 while (*src != '\0') {
53 if ((len = mbtowc(&wc, src, MB_CUR_MAX)) == -1) {
54 total_width++;
55 *dst++ = '?';
56 src++;
57 } else if ((width = wcwidth(wc)) == -1) {
58 total_width++;
59 *dst++ = '?';
60 src += len;
61 } else {
62 total_width += width;
63 while (len-- > 0)
64 *dst++ = *src++;
65 }
66 }
67 *dst = '\0';
68 *widthp = total_width;
69 return NULL;
70 }
72 int
73 got_locale_is_utf8(void)
74 {
75 char *codeset = nl_langinfo(CODESET);
76 return (strcmp(codeset, "UTF-8") == 0);
77 }