1 /* Public Domain Curses */
5 RCSID("$Id: util.c,v 1.71 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
12 char *unctrl(chtype c);
15 int delay_output(int ms);
17 int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
18 short *color_pair, void *opts);
19 int setcchar(cchar_t *wcval, const wchar_t *wch, const attr_t attrs,
20 short color_pair, const void *opts);
21 wchar_t *wunctrl(cchar_t *wc);
23 int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n);
24 size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n);
25 size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n);
28 unctrl() expands the text portion of the chtype c into a
29 printable string. Control characters are changed to the "^X"
30 notation; others are passed through. wunctrl() is the wide-
31 character version of the function.
33 filter() and use_env() are no-ops in PDCurses.
35 delay_output() inserts an ms millisecond pause in output.
37 getcchar() works in two modes: When wch is not NULL, it reads
38 the cchar_t pointed to by wcval and stores the attributes in
39 attrs, the color pair in color_pair, and the text in the
40 wide-character string wch. When wch is NULL, getcchar() merely
41 returns the number of wide characters in wcval. In either mode,
42 the opts argument is unused.
44 setcchar constructs a cchar_t at wcval from the wide-character
45 text at wch, the attributes in attr and the color pair in
46 color_pair. The opts argument is unused.
48 Currently, the length returned by getcchar() is always 1 or 0.
49 Similarly, setcchar() will only take the first wide character
50 from wch, and ignore any others that it "should" take (i.e.,
51 combining characters). Nor will it correctly handle any
52 character outside the basic multilingual plane (UCS-2).
55 unctrl() and wunctrl() return NULL on failure. delay_output()
58 getcchar() returns the number of wide characters wcval points to
59 when wch is NULL; when it's not, getcchar() returns OK or ERR.
61 setcchar() returns OK or ERR.
63 Portability X/Open BSD SYS V
75 **man-end****************************************************************/
78 # ifdef PDC_FORCE_UTF8
85 char *unctrl(chtype c
)
87 static char strbuf
[3] = {0, 0, 0};
91 PDC_LOG(("unctrl() - called\n"));
95 if (ic
>= 0x20 && ic
!= 0x7f) /* normal characters */
102 strbuf
[0] = '^'; /* '^' prefix */
104 if (ic
== 0x7f) /* 0x7f == DEL */
106 else /* other control */
107 strbuf
[1] = (char)(ic
+ '@');
114 PDC_LOG(("filter() - called\n"));
119 PDC_LOG(("use_env() - called: x %d\n", x
));
122 int delay_output(int ms
)
124 PDC_LOG(("delay_output() - called: ms %d\n", ms
));
130 int getcchar(const cchar_t
*wcval
, wchar_t *wch
, attr_t
*attrs
,
131 short *color_pair
, void *opts
)
138 if (!attrs
|| !color_pair
)
141 *wch
= (*wcval
& A_CHARTEXT
);
142 *attrs
= (*wcval
& (A_ATTRIBUTES
& ~A_COLOR
));
143 *color_pair
= PAIR_NUMBER(*wcval
& A_COLOR
);
151 return ((*wcval
& A_CHARTEXT
) != L
'\0');
154 int setcchar(cchar_t
*wcval
, const wchar_t *wch
, const attr_t attrs
,
155 short color_pair
, const void *opts
)
160 *wcval
= *wch
| attrs
| COLOR_PAIR(color_pair
);
165 wchar_t *wunctrl(cchar_t
*wc
)
167 static wchar_t strbuf
[3] = {0, 0, 0};
171 PDC_LOG(("wunctrl() - called\n"));
173 ic
= *wc
& A_CHARTEXT
;
175 if (ic
>= 0x20 && ic
!= 0x7f) /* normal characters */
177 strbuf
[0] = (wchar_t)ic
;
182 strbuf
[0] = '^'; /* '^' prefix */
184 if (ic
== 0x7f) /* 0x7f == DEL */
186 else /* other control */
187 strbuf
[1] = (wchar_t)(ic
+ '@');
192 int PDC_mbtowc(wchar_t *pwc
, const char *s
, size_t n
)
194 # ifdef PDC_FORCE_UTF8
197 const unsigned char *string
;
205 string
= (const unsigned char *)s
;
209 /* Simplistic UTF-8 decoder -- only does the BMP, minimal validation */
213 if ((key
& 0xe0) == 0xc0)
217 key
= ((key
& 0x1f) << 6) | (string
[1] & 0x3f);
221 else if ((key
& 0xe0) == 0xe0)
225 key
= ((key
& 0x0f) << 12) | ((string
[1] & 0x3f) << 6) |
239 return mbtowc(pwc
, s
, n
);
243 size_t PDC_mbstowcs(wchar_t *dest
, const char *src
, size_t n
)
245 # ifdef PDC_FORCE_UTF8
253 while (*src
&& i
< n
)
255 int retval
= PDC_mbtowc(dest
+ i
, src
, len
);
265 size_t i
= mbstowcs(dest
, src
, n
);
271 size_t PDC_wcstombs(char *dest
, const wchar_t *src
, size_t n
)
273 # ifdef PDC_FORCE_UTF8
279 while (*src
&& i
< n
)
281 chtype code
= *src
++;
291 dest
[i
] = ((code
& 0x07c0) >> 6) | 0xc0;
292 dest
[i
+ 1] = (code
& 0x003f) | 0x80;
297 dest
[i
] = ((code
& 0xf000) >> 12) | 0xe0;
298 dest
[i
+ 1] = ((code
& 0x0fc0) >> 6) | 0x80;
299 dest
[i
+ 2] = (code
& 0x003f) | 0x80;
304 size_t i
= wcstombs(dest
, src
, n
);