Adds readline

This commit is contained in:
Ricardo Quesada 2013-12-02 09:57:16 -08:00
parent 21afd63d82
commit eb834bce7f
1 changed files with 27 additions and 1 deletions

View File

@ -52,6 +52,31 @@ void Console::cancel()
} }
ssize_t net_readline( int fd, void *vptr, size_t maxlen )
{
ssize_t n, rc;
char c, *ptr;
ptr = (char*)vptr ;
for( n=1; n<maxlen; n++ ) {
if( (rc = read(fd, &c, 1 )) ==1 ) {
*ptr++ = c;
if( c=='\n' )
break;
} else if( rc == 0 ) {
return 0;
} else if( errno == EINTR ) {
continue;
} else {
return -1;
}
}
*ptr = 0;
return n;
}
void Console::loop() void Console::loop()
{ {
fd_set read_set; fd_set read_set;
@ -63,8 +88,8 @@ void Console::loop()
int nready = select( _fd+1, &read_set, NULL, NULL, NULL ); int nready = select( _fd+1, &read_set, NULL, NULL, NULL );
/* error ?*/
if( nready == -1 ) { if( nready == -1 ) {
/* error ?*/
if(errno != EINTR) { if(errno != EINTR) {
log("Abnormal error in select()\n"); log("Abnormal error in select()\n");
} }
@ -74,6 +99,7 @@ void Console::loop()
/* timeout ? */ /* timeout ? */
continue; continue;
} else { } else {
/* info */
} }
} }