add rdline

This commit is contained in:
Hojun-Cho 2024-12-05 01:15:27 +09:00
parent 4580d576a8
commit dce27c5d22
2 changed files with 44 additions and 16 deletions

View File

@ -33,26 +33,55 @@ machdep(void)
static void
puts(char *s)
{
for(;*s;++s)
for(;*s; ++s)
putchar(*s);
}
static void
putcn(char *s, int n)
{
for(int i=0; i < n; ++i)
putchar(s[i]);
}
static int
rdline(char *buf, int n)
{
char *p = buf;
char *e = buf + n;
while(p < e){
int c = getchar();
switch(c){
case '\177':
case '\b':
if(p > buf){
putchar('\177');
*--p = 0;
}
break;
case '\n':
case '\r':
goto done;
default:
*p++ = c;
break;
}
}
putchar('\n');
done:
return p - buf;
}
void
boot(int bootdev)
{
char buf[8192];
machdep();
putchar('\n');
for(;;){
int i=0, c=0;
char buf[8192];
puts("\nboot >> ");
do{
c = getchar();
buf[i++] = c;
}while(c != '\n' && i < sizeof(buf) - 1);
buf[i-1] = 0;
puts(buf);
int l = rdline(buf, sizeof(buf));
putcn(buf, l);
}
}

View File

@ -3,7 +3,6 @@
#include "fn.h"
#define TABWIDTH 8
#define DEL '\177'
static void conputc(int c);
static int congetc(void);
@ -16,9 +15,9 @@ getchar(void)
int c;
c = congetc();
if( c == '\r')
if(c == '\r')
c = '\n';
if((c < ' ' && c != '\n') || c == DEL)
if((c < ' ' && c != '\n') || c == '\177')
return c;
putchar(c);
return c;
@ -28,12 +27,12 @@ void
putchar(int c)
{
switch(c){
case DEL:
case '\177':
conputc('\b');
conputc(' ');
case '\b':
conputc('\b');
if(pos)
if(pos > 0)
--pos;
break;
case '\t':