drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / getstr.c
blob744e5e65fe715125491b2b9c41f39f70b6ebd1ba
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: getstr.c,v 1.51 2008/07/14 04:24:51 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: getstr
11 Synopsis:
12 int getstr(char *str);
13 int wgetstr(WINDOW *win, char *str);
14 int mvgetstr(int y, int x, char *str);
15 int mvwgetstr(WINDOW *win, int y, int x, char *str);
16 int getnstr(char *str, int n);
17 int wgetnstr(WINDOW *win, char *str, int n);
18 int mvgetnstr(int y, int x, char *str, int n);
19 int mvwgetnstr(WINDOW *win, int y, int x, char *str, int n);
21 int get_wstr(wint_t *wstr);
22 int wget_wstr(WINDOW *win, wint_t *wstr);
23 int mvget_wstr(int y, int x, wint_t *wstr);
24 int mvwget_wstr(WINDOW *win, int, int, wint_t *wstr);
25 int getn_wstr(wint_t *wstr, int n);
26 int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
27 int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
28 int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
30 Description:
31 These routines call wgetch() repeatedly to build a string,
32 interpreting erase and kill characters along the way, until a
33 newline or carriage return is received. When PDCurses is built
34 with wide-character support enabled, the narrow-character
35 functions convert the wgetch()'d values into a multibyte string
36 in the current locale before returning it. The resulting string
37 is placed in the area pointed to by *str. The routines with n as
38 the last argument read at most n characters.
40 Note that there's no way to know how long the buffer passed to
41 wgetstr() is, so use wgetnstr() to avoid buffer overflows.
43 Return Value:
44 This functions return ERR on failure or any other value on
45 success.
47 Portability X/Open BSD SYS V
48 getstr Y Y Y
49 wgetstr Y Y Y
50 mvgetstr Y Y Y
51 mvwgetstr Y Y Y
52 getnstr Y - 4.0
53 wgetnstr Y - 4.0
54 mvgetnstr Y - -
55 mvwgetnstr Y - -
56 get_wstr Y
57 wget_wstr Y
58 mvget_wstr Y
59 mvwget_wstr Y
60 getn_wstr Y
61 wgetn_wstr Y
62 mvgetn_wstr Y
63 mvwgetn_wstr Y
65 **man-end****************************************************************/
67 #define MAXLINE 255
69 int wgetnstr(WINDOW *win, char *str, int n)
71 #ifdef PDC_WIDE
72 wchar_t wstr[MAXLINE + 1];
74 if (n < 0 || n > MAXLINE)
75 n = MAXLINE;
77 if (wgetn_wstr(win, (wint_t *)wstr, n) == ERR)
78 return ERR;
80 return PDC_wcstombs(str, wstr, n);
81 #else
82 int ch, i, num, x, chars;
83 char *p;
84 bool stop, oldecho, oldcbreak, oldnodelay;
86 PDC_LOG(("wgetnstr() - called\n"));
88 if (!win || !str)
89 return ERR;
91 chars = 0;
92 p = str;
93 stop = FALSE;
95 x = win->_curx;
97 oldcbreak = SP->cbreak; /* remember states */
98 oldecho = SP->echo;
99 oldnodelay = win->_nodelay;
101 SP->echo = FALSE; /* we do echo ourselves */
102 cbreak(); /* ensure each key is returned immediately */
103 win->_nodelay = FALSE; /* don't return -1 */
105 wrefresh(win);
107 while (!stop)
109 ch = wgetch(win);
111 switch (ch)
114 case '\t':
115 ch = ' ';
116 num = TABSIZE - (win->_curx - x) % TABSIZE;
117 for (i = 0; i < num; i++)
119 if (chars < n)
121 if (oldecho)
122 waddch(win, ch);
123 *p++ = ch;
124 ++chars;
126 else
127 beep();
129 break;
131 case _ECHAR: /* CTRL-H -- Delete character */
132 if (p > str)
134 if (oldecho)
135 waddstr(win, "\b \b");
136 ch = (unsigned char)(*--p);
137 if ((ch < ' ') && (oldecho))
138 waddstr(win, "\b \b");
139 chars--;
141 break;
143 case _DLCHAR: /* CTRL-U -- Delete line */
144 while (p > str)
146 if (oldecho)
147 waddstr(win, "\b \b");
148 ch = (unsigned char)(*--p);
149 if ((ch < ' ') && (oldecho))
150 waddstr(win, "\b \b");
152 chars = 0;
153 break;
155 case _DWCHAR: /* CTRL-W -- Delete word */
157 while ((p > str) && (*(p - 1) == ' '))
159 if (oldecho)
160 waddstr(win, "\b \b");
162 --p; /* remove space */
163 chars--;
165 while ((p > str) && (*(p - 1) != ' '))
167 if (oldecho)
168 waddstr(win, "\b \b");
170 ch = (unsigned char)(*--p);
171 if ((ch < ' ') && (oldecho))
172 waddstr(win, "\b \b");
173 chars--;
175 break;
177 case '\n':
178 case '\r':
179 stop = TRUE;
180 if (oldecho)
181 waddch(win, '\n');
182 break;
184 default:
185 if (chars < n)
187 if (!SP->key_code && ch < 0x100)
189 *p++ = ch;
190 if (oldecho)
191 waddch(win, ch);
192 chars++;
195 else
196 beep();
198 break;
202 wrefresh(win);
205 *p = '\0';
207 SP->echo = oldecho; /* restore old settings */
208 SP->cbreak = oldcbreak;
209 win->_nodelay = oldnodelay;
211 return OK;
212 #endif
215 int getstr(char *str)
217 PDC_LOG(("getstr() - called\n"));
219 return wgetnstr(stdscr, str, MAXLINE);
222 int wgetstr(WINDOW *win, char *str)
224 PDC_LOG(("wgetstr() - called\n"));
226 return wgetnstr(win, str, MAXLINE);
229 int mvgetstr(int y, int x, char *str)
231 PDC_LOG(("mvgetstr() - called\n"));
233 if (move(y, x) == ERR)
234 return ERR;
236 return wgetnstr(stdscr, str, MAXLINE);
239 int mvwgetstr(WINDOW *win, int y, int x, char *str)
241 PDC_LOG(("mvwgetstr() - called\n"));
243 if (wmove(win, y, x) == ERR)
244 return ERR;
246 return wgetnstr(win, str, MAXLINE);
249 int getnstr(char *str, int n)
251 PDC_LOG(("getnstr() - called\n"));
253 return wgetnstr(stdscr, str, n);
256 int mvgetnstr(int y, int x, char *str, int n)
258 PDC_LOG(("mvgetnstr() - called\n"));
260 if (move(y, x) == ERR)
261 return ERR;
263 return wgetnstr(stdscr, str, n);
266 int mvwgetnstr(WINDOW *win, int y, int x, char *str, int n)
268 PDC_LOG(("mvwgetnstr() - called\n"));
270 if (wmove(win, y, x) == ERR)
271 return ERR;
273 return wgetnstr(win, str, n);
276 #ifdef PDC_WIDE
277 int wgetn_wstr(WINDOW *win, wint_t *wstr, int n)
279 int ch, i, num, x, chars;
280 wint_t *p;
281 bool stop, oldecho, oldcbreak, oldnodelay;
283 PDC_LOG(("wgetn_wstr() - called\n"));
285 if (!win || !wstr)
286 return ERR;
288 chars = 0;
289 p = wstr;
290 stop = FALSE;
292 x = win->_curx;
294 oldcbreak = SP->cbreak; /* remember states */
295 oldecho = SP->echo;
296 oldnodelay = win->_nodelay;
298 SP->echo = FALSE; /* we do echo ourselves */
299 cbreak(); /* ensure each key is returned immediately */
300 win->_nodelay = FALSE; /* don't return -1 */
302 wrefresh(win);
304 while (!stop)
306 ch = wgetch(win);
308 switch (ch)
311 case '\t':
312 ch = ' ';
313 num = TABSIZE - (win->_curx - x) % TABSIZE;
314 for (i = 0; i < num; i++)
316 if (chars < n)
318 if (oldecho)
319 waddch(win, ch);
320 *p++ = ch;
321 ++chars;
323 else
324 beep();
326 break;
328 case _ECHAR: /* CTRL-H -- Delete character */
329 if (p > wstr)
331 if (oldecho)
332 waddstr(win, "\b \b");
333 ch = *--p;
334 if ((ch < ' ') && (oldecho))
335 waddstr(win, "\b \b");
336 chars--;
338 break;
340 case _DLCHAR: /* CTRL-U -- Delete line */
341 while (p > wstr)
343 if (oldecho)
344 waddstr(win, "\b \b");
345 ch = *--p;
346 if ((ch < ' ') && (oldecho))
347 waddstr(win, "\b \b");
349 chars = 0;
350 break;
352 case _DWCHAR: /* CTRL-W -- Delete word */
354 while ((p > wstr) && (*(p - 1) == ' '))
356 if (oldecho)
357 waddstr(win, "\b \b");
359 --p; /* remove space */
360 chars--;
362 while ((p > wstr) && (*(p - 1) != ' '))
364 if (oldecho)
365 waddstr(win, "\b \b");
367 ch = *--p;
368 if ((ch < ' ') && (oldecho))
369 waddstr(win, "\b \b");
370 chars--;
372 break;
374 case '\n':
375 case '\r':
376 stop = TRUE;
377 if (oldecho)
378 waddch(win, '\n');
379 break;
381 default:
382 if (chars < n)
384 if (!SP->key_code)
386 *p++ = ch;
387 if (oldecho)
388 waddch(win, ch);
389 chars++;
392 else
393 beep();
395 break;
399 wrefresh(win);
402 *p = '\0';
404 SP->echo = oldecho; /* restore old settings */
405 SP->cbreak = oldcbreak;
406 win->_nodelay = oldnodelay;
408 return OK;
411 int get_wstr(wint_t *wstr)
413 PDC_LOG(("get_wstr() - called\n"));
415 return wgetn_wstr(stdscr, wstr, MAXLINE);
418 int wget_wstr(WINDOW *win, wint_t *wstr)
420 PDC_LOG(("wget_wstr() - called\n"));
422 return wgetn_wstr(win, wstr, MAXLINE);
425 int mvget_wstr(int y, int x, wint_t *wstr)
427 PDC_LOG(("mvget_wstr() - called\n"));
429 if (move(y, x) == ERR)
430 return ERR;
432 return wgetn_wstr(stdscr, wstr, MAXLINE);
435 int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr)
437 PDC_LOG(("mvwget_wstr() - called\n"));
439 if (wmove(win, y, x) == ERR)
440 return ERR;
442 return wgetn_wstr(win, wstr, MAXLINE);
445 int getn_wstr(wint_t *wstr, int n)
447 PDC_LOG(("getn_wstr() - called\n"));
449 return wgetn_wstr(stdscr, wstr, n);
452 int mvgetn_wstr(int y, int x, wint_t *wstr, int n)
454 PDC_LOG(("mvgetn_wstr() - called\n"));
456 if (move(y, x) == ERR)
457 return ERR;
459 return wgetn_wstr(stdscr, wstr, n);
462 int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n)
464 PDC_LOG(("mvwgetn_wstr() - called\n"));
466 if (wmove(win, y, x) == ERR)
467 return ERR;
469 return wgetn_wstr(win, wstr, n);
471 #endif