soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / util.c
blob9972143516e4ce10ceeca5a67352a9a592bf60c1
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: util.c,v 1.71 2008/07/13 16:08:18 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: util
11 Synopsis:
12 char *unctrl(chtype c);
13 void filter(void);
14 void use_env(bool x);
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);
27 Description:
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).
54 Return Value:
55 unctrl() and wunctrl() return NULL on failure. delay_output()
56 always returns OK.
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
64 unctrl Y Y Y
65 filter Y - 3.0
66 use_env Y - 4.0
67 delay_output Y Y Y
68 getcchar Y
69 setcchar Y
70 wunctrl Y
71 PDC_mbtowc - - -
72 PDC_mbstowcs - - -
73 PDC_wcstombs - - -
75 **man-end****************************************************************/
77 #ifdef PDC_WIDE
78 # ifdef PDC_FORCE_UTF8
79 # include <string.h>
80 # else
81 # include <stdlib.h>
82 # endif
83 #endif
85 char *unctrl(chtype c)
87 static char strbuf[3] = {0, 0, 0};
89 chtype ic;
91 PDC_LOG(("unctrl() - called\n"));
93 ic = c & A_CHARTEXT;
95 if (ic >= 0x20 && ic != 0x7f) /* normal characters */
97 strbuf[0] = (char)ic;
98 strbuf[1] = '\0';
99 return strbuf;
102 strbuf[0] = '^'; /* '^' prefix */
104 if (ic == 0x7f) /* 0x7f == DEL */
105 strbuf[1] = '?';
106 else /* other control */
107 strbuf[1] = (char)(ic + '@');
109 return strbuf;
112 void filter(void)
114 PDC_LOG(("filter() - called\n"));
117 void use_env(bool x)
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));
126 return napms(ms);
129 #ifdef PDC_WIDE
130 int getcchar(const cchar_t *wcval, wchar_t *wch, attr_t *attrs,
131 short *color_pair, void *opts)
133 if (!wcval)
134 return ERR;
136 if (wch)
138 if (!attrs || !color_pair)
139 return ERR;
141 *wch = (*wcval & A_CHARTEXT);
142 *attrs = (*wcval & (A_ATTRIBUTES & ~A_COLOR));
143 *color_pair = PAIR_NUMBER(*wcval & A_COLOR);
145 if (*wch)
146 *++wch = L'\0';
148 return OK;
150 else
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)
157 if (!wcval || !wch)
158 return ERR;
160 *wcval = *wch | attrs | COLOR_PAIR(color_pair);
162 return OK;
165 wchar_t *wunctrl(cchar_t *wc)
167 static wchar_t strbuf[3] = {0, 0, 0};
169 cchar_t ic;
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;
178 strbuf[1] = L'\0';
179 return strbuf;
182 strbuf[0] = '^'; /* '^' prefix */
184 if (ic == 0x7f) /* 0x7f == DEL */
185 strbuf[1] = '?';
186 else /* other control */
187 strbuf[1] = (wchar_t)(ic + '@');
189 return strbuf;
192 int PDC_mbtowc(wchar_t *pwc, const char *s, size_t n)
194 # ifdef PDC_FORCE_UTF8
195 wchar_t key;
196 int i = -1;
197 const unsigned char *string;
199 if (!s || (n < 1))
200 return -1;
202 if (!*s)
203 return 0;
205 string = (const unsigned char *)s;
207 key = string[0];
209 /* Simplistic UTF-8 decoder -- only does the BMP, minimal validation */
211 if (key & 0x80)
213 if ((key & 0xe0) == 0xc0)
215 if (1 < n)
217 key = ((key & 0x1f) << 6) | (string[1] & 0x3f);
218 i = 2;
221 else if ((key & 0xe0) == 0xe0)
223 if (2 < n)
225 key = ((key & 0x0f) << 12) | ((string[1] & 0x3f) << 6) |
226 (string[2] & 0x3f);
227 i = 3;
231 else
232 i = 1;
234 if (i)
235 *pwc = key;
237 return i;
238 # else
239 return mbtowc(pwc, s, n);
240 # endif
243 size_t PDC_mbstowcs(wchar_t *dest, const char *src, size_t n)
245 # ifdef PDC_FORCE_UTF8
246 size_t i = 0, len;
248 if (!src || !dest)
249 return 0;
251 len = strlen(src);
253 while (*src && i < n)
255 int retval = PDC_mbtowc(dest + i, src, len);
257 if (retval < 1)
258 return -1;
260 src += retval;
261 len -= retval;
262 i++;
264 # else
265 size_t i = mbstowcs(dest, src, n);
266 # endif
267 dest[i] = 0;
268 return i;
271 size_t PDC_wcstombs(char *dest, const wchar_t *src, size_t n)
273 # ifdef PDC_FORCE_UTF8
274 size_t i = 0;
276 if (!src || !dest)
277 return 0;
279 while (*src && i < n)
281 chtype code = *src++;
283 if (code < 0x80)
285 dest[i] = code;
286 i++;
288 else
289 if (code < 0x800)
291 dest[i] = ((code & 0x07c0) >> 6) | 0xc0;
292 dest[i + 1] = (code & 0x003f) | 0x80;
293 i += 2;
295 else
297 dest[i] = ((code & 0xf000) >> 12) | 0xe0;
298 dest[i + 1] = ((code & 0x0fc0) >> 6) | 0x80;
299 dest[i + 2] = (code & 0x003f) | 0x80;
300 i += 3;
303 # else
304 size_t i = wcstombs(dest, src, n);
305 # endif
306 dest[i] = '\0';
307 return i;
309 #endif