introduce cngets, a method for kernel to read a string from console

This is intended as a replacement for libkern's gets and mostly borrows
its implementation.  It uses cngrab/cnungrab to delimit kernel's access
to console input.

Note: libkern's gets obviously doesn't share any bits of implementation
iwth libc's gets.  They also have different APIs and the former doesn't
have the overflow problems of the latter.

Inspired by:	bde
MFC after:	2 months
This commit is contained in:
Andriy Gapon 2011-12-17 15:16:54 +00:00
parent bf8696b408
commit 8e62854265
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=228633
2 changed files with 50 additions and 0 deletions

View File

@ -407,6 +407,55 @@ cncheckc(void)
return (-1);
}
void
cngets(char *cp, size_t size, int visible)
{
char *lp, *end;
int c;
cngrab();
lp = cp;
end = cp + size - 1;
for (;;) {
c = cngetc() & 0177;
switch (c) {
case '\n':
case '\r':
cnputc(c);
*lp = '\0';
cnungrab();
return;
case '\b':
case '\177':
if (lp > cp) {
if (visible) {
cnputc(c);
cnputs(" \b");
}
lp--;
}
continue;
case '\0':
continue;
default:
if (lp < end) {
switch (visible) {
case GETS_NOECHO:
break;
case GETS_ECHOPASS:
cnputc('*');
break;
default:
cnputc(c);
break;
}
*lp++ = c;
}
}
}
}
void
cnputc(int c)
{

View File

@ -121,6 +121,7 @@ void cngrab(void);
void cnungrab(void);
int cncheckc(void);
int cngetc(void);
void cngets(char *, size_t, int);
void cnputc(int);
void cnputs(char *);
int cnunavailable(void);