1 /* Public Domain Curses */
5 RCSID("$Id: getstr.c,v 1.51 2008/07/14 04:24:51 wmcbrine Exp $")
7 /*man-start**************************************************************
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);
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.
44 This functions return ERR on failure or any other value on
47 Portability X/Open BSD SYS V
65 **man-end****************************************************************/
69 int wgetnstr(WINDOW
*win
, char *str
, int n
)
72 wchar_t wstr
[MAXLINE
+ 1];
74 if (n
< 0 || n
> MAXLINE
)
77 if (wgetn_wstr(win
, (wint_t *)wstr
, n
) == ERR
)
80 return PDC_wcstombs(str
, wstr
, n
);
82 int ch
, i
, num
, x
, chars
;
84 bool stop
, oldecho
, oldcbreak
, oldnodelay
;
86 PDC_LOG(("wgetnstr() - called\n"));
97 oldcbreak
= SP
->cbreak
; /* remember states */
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 */
116 num
= TABSIZE
- (win
->_curx
- x
) % TABSIZE
;
117 for (i
= 0; i
< num
; i
++)
131 case _ECHAR
: /* CTRL-H -- Delete character */
135 waddstr(win
, "\b \b");
136 ch
= (unsigned char)(*--p
);
137 if ((ch
< ' ') && (oldecho
))
138 waddstr(win
, "\b \b");
143 case _DLCHAR
: /* CTRL-U -- Delete line */
147 waddstr(win
, "\b \b");
148 ch
= (unsigned char)(*--p
);
149 if ((ch
< ' ') && (oldecho
))
150 waddstr(win
, "\b \b");
155 case _DWCHAR
: /* CTRL-W -- Delete word */
157 while ((p
> str
) && (*(p
- 1) == ' '))
160 waddstr(win
, "\b \b");
162 --p
; /* remove space */
165 while ((p
> str
) && (*(p
- 1) != ' '))
168 waddstr(win
, "\b \b");
170 ch
= (unsigned char)(*--p
);
171 if ((ch
< ' ') && (oldecho
))
172 waddstr(win
, "\b \b");
187 if (!SP
->key_code
&& ch
< 0x100)
207 SP
->echo
= oldecho
; /* restore old settings */
208 SP
->cbreak
= oldcbreak
;
209 win
->_nodelay
= oldnodelay
;
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
)
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
)
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
)
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
)
273 return wgetnstr(win
, str
, n
);
277 int wgetn_wstr(WINDOW
*win
, wint_t *wstr
, int n
)
279 int ch
, i
, num
, x
, chars
;
281 bool stop
, oldecho
, oldcbreak
, oldnodelay
;
283 PDC_LOG(("wgetn_wstr() - called\n"));
294 oldcbreak
= SP
->cbreak
; /* remember states */
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 */
313 num
= TABSIZE
- (win
->_curx
- x
) % TABSIZE
;
314 for (i
= 0; i
< num
; i
++)
328 case _ECHAR
: /* CTRL-H -- Delete character */
332 waddstr(win
, "\b \b");
334 if ((ch
< ' ') && (oldecho
))
335 waddstr(win
, "\b \b");
340 case _DLCHAR
: /* CTRL-U -- Delete line */
344 waddstr(win
, "\b \b");
346 if ((ch
< ' ') && (oldecho
))
347 waddstr(win
, "\b \b");
352 case _DWCHAR
: /* CTRL-W -- Delete word */
354 while ((p
> wstr
) && (*(p
- 1) == ' '))
357 waddstr(win
, "\b \b");
359 --p
; /* remove space */
362 while ((p
> wstr
) && (*(p
- 1) != ' '))
365 waddstr(win
, "\b \b");
368 if ((ch
< ' ') && (oldecho
))
369 waddstr(win
, "\b \b");
404 SP
->echo
= oldecho
; /* restore old settings */
405 SP
->cbreak
= oldcbreak
;
406 win
->_nodelay
= oldnodelay
;
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
)
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
)
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
)
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
)
469 return wgetn_wstr(win
, wstr
, n
);