in graphics/pdcurs34/pdcurses/pdc_getstr.c [98:288]
int wgetnstr(WINDOW *win, char *str, int n)
{
#ifdef CONFIG_PDCURSES_WIDE
wchar_t wstr[MAXLINE + 1];
if (n < 0 || n > MAXLINE)
{
n = MAXLINE;
}
if (wgetn_wstr(win, (wint_t *) wstr, n) == ERR)
{
return ERR;
}
return PDC_wcstombs(str, wstr, n);
#else
int ch;
int i;
int num;
int x;
int chars;
char *p;
bool stop;
bool oldecho;
bool oldcbreak;
bool oldnodelay;
#ifdef CONFIG_PDCURSES_MULTITHREAD
FAR struct pdc_context_s *ctx = PDC_ctx();
#endif
PDC_LOG(("wgetnstr() - called\n"));
if (!win || !str)
{
return ERR;
}
chars = 0;
p = str;
stop = false;
x = win->_curx;
oldcbreak = SP->cbreak; /* Remember states */
oldecho = SP->echo;
oldnodelay = win->_nodelay;
SP->echo = false; /* We do echo ourselves */
cbreak(); /* Ensure each key is returned immediately */
win->_nodelay = false; /* Don't return -1 */
wrefresh(win);
while (!stop)
{
ch = wgetch(win);
switch (ch)
{
case '\t':
ch = ' ';
num = TABSIZE - (win->_curx - x) % TABSIZE;
for (i = 0; i < num; i++)
{
if (chars < n)
{
if (oldecho)
{
waddch(win, ch);
}
*p++ = ch;
++chars;
}
else
{
beep();
}
}
break;
case _ECHAR: /* CTRL-H -- Delete character */
if (p > str)
{
if (oldecho)
{
waddstr(win, "\b \b");
}
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
{
waddstr(win, "\b \b");
}
chars--;
}
break;
case _DLCHAR: /* CTRL-U -- Delete line */
while (p > str)
{
if (oldecho)
{
waddstr(win, "\b \b");
}
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
{
waddstr(win, "\b \b");
}
}
chars = 0;
break;
case _DWCHAR: /* CTRL-W -- Delete word */
while ((p > str) && (*(p - 1) == ' '))
{
if (oldecho)
{
waddstr(win, "\b \b");
}
--p; /* Remove space */
chars--;
}
while ((p > str) && (*(p - 1) != ' '))
{
if (oldecho)
{
waddstr(win, "\b \b");
}
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
{
waddstr(win, "\b \b");
}
chars--;
}
break;
case '\n':
case '\r':
stop = true;
if (oldecho)
{
waddch(win, '\n');
}
break;
default:
if (chars < n)
{
if (!SP->key_code && ch < 0x100)
{
*p++ = ch;
if (oldecho)
{
waddch(win, ch);
}
chars++;
}
}
else
{
beep();
}
break;
}
wrefresh(win);
}
*p = '\0';
SP->echo = oldecho; /* restore old settings */
SP->cbreak = oldcbreak;
win->_nodelay = oldnodelay;
return OK;
#endif
}