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

@ -31,32 +31,47 @@ walk(char *s)
return s; return s;
} }
static void
device_write(char *s)
{
char c, *cap = walk(s);
if(*s == '(') s++, --cap;
while(s < cap) {
c = *s++;
if(c == '\\') {
switch(*s++) {
case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break;
}
} else
putc(c, stdout);
}
}
static void
device_read(char *s)
{
char c;
while(fread(&c, 1, 1, stdin) && c >= ' ')
*dst_++ = c;
if(feof(stdin))
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
}
static void static void
write_reg(char r, char *reg) write_reg(char r, char *reg)
{ {
char c, *cap = walk(reg); char c, *cap = walk(reg);
switch(r) { switch(r) {
case ':': /* op: output */ case '>': /* op: output */
if(*reg == '(') reg++, --cap; case ':':
while(reg < cap) { device_write(reg);
c = *reg++;
if(c == '\\') {
switch(*reg++) {
case 't': putc(0x09, stdout); break;
case 'n': putc(0x0a, stdout); break;
case 's': putc(0x20, stdout); break;
}
} else
putc(c, stdout);
}
return; return;
case '~': { /* op: input */ case '<': /* op: input */
while(fread(&c, 1, 1, stdin) && c >= ' ') case '~':
*dst_++ = c; device_read(reg);
if(feof(stdin))
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
return; 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++))
@ -196,7 +211,7 @@ rewrite(void)
s += 2; s += 2;
while(*s == ' ') s++; while(*s == ' ') s++;
cap = walk(s), r = find_rule(s, cap); cap = walk(s), r = find_rule(s, cap);
if(r != NULL){ if(r != NULL) {
if(!quiet) if(!quiet)
fprintf(stderr, ">< (%s) (%s)\n", r->a, r->b); fprintf(stderr, ">< (%s) (%s)\n", r->a, r->b);
r->a = 0; r->a = 0;
@ -208,7 +223,7 @@ rewrite(void)
if(c == '<' && s[1] == '>') { if(c == '<' && s[1] == '>') {
r = rules_, r->id = rules_ - rules - 1; r = rules_, r->id = rules_ - rules - 1;
s = parse_frag(&r->b, parse_frag(&r->a, s + 2)); s = parse_frag(&r->b, parse_frag(&r->a, s + 2));
if(*r->a){ if(*r->a) {
if(!quiet) if(!quiet)
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
rules_++; rules_++;
@ -273,7 +288,7 @@ main(int argc, char **argv)
while(rewrite() && ++rw) while(rewrite() && ++rw)
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n"); if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
if(!quiet) { if(!quiet) {
while(rules_-- > rules){ while(rules_-- > rules) {
if(!rules_->refs && rules_->a) if(!rules_->refs && rules_->a)
fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b); fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
if(debug && rules_->a) if(debug && rules_->a)