Isolated device read/write functions

This commit is contained in:
Devine Lu Linvega 2024-04-28 18:55:03 -07:00
parent 1ddc66e1dd
commit 53b89bcf65
1 changed files with 37 additions and 22 deletions

View File

@ -32,16 +32,14 @@ walk(char *s)
} }
static void static void
write_reg(char r, char *reg) device_write(char *s)
{ {
char c, *cap = walk(reg); char c, *cap = walk(s);
switch(r) { if(*s == '(') s++, --cap;
case ':': /* op: output */ while(s < cap) {
if(*reg == '(') reg++, --cap; c = *s++;
while(reg < cap) {
c = *reg++;
if(c == '\\') { if(c == '\\') {
switch(*reg++) { switch(*s++) {
case 't': putc(0x09, stdout); break; case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break; case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break; case 's': putc(0x20, stdout); break;
@ -49,14 +47,31 @@ write_reg(char r, char *reg)
} else } else
putc(c, stdout); putc(c, stdout);
} }
return; }
case '~': { /* op: input */
static void
device_read(char *s)
{
char c;
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c; *dst_++ = c;
if(feof(stdin)) if(feof(stdin))
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F'; *dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
return;
} }
static void
write_reg(char r, char *reg)
{
char c, *cap = walk(reg);
switch(r) {
case '>': /* op: output */
case ':':
device_write(reg);
return;
case '<': /* op: input */
case '~':
device_read(reg);
return;
case '^': /* op: join */ case '^': /* op: join */
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
while(reg < cap && (c = *reg++)) while(reg < cap && (c = *reg++))