From 89f393e3492381e2f1115a6c33cf576624305ffa Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Fri, 24 May 2024 18:56:06 -0700 Subject: [PATCH] Handle empty register writes at the register level --- src/modal.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/modal.c b/src/modal.c index 0c1af6e..32ba312 100644 --- a/src/modal.c +++ b/src/modal.c @@ -123,16 +123,17 @@ file_import(char *path, char *ptr) return copy("NAF", ptr, 3); } -static int +static void write_reg(char r, char *reg) { - char *origin = dst_; switch(r) { case ':': device_write(reg); break; case '~': { unsigned char c; + char *origin = dst_; while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c; if(feof(stdin)) dst_ = copy("EOF", dst_, 3); + if(origin == dst_) dst_--; break; } case '_': { @@ -172,7 +173,6 @@ write_reg(char r, char *reg) } default: dst_ = copy(reg, dst_, walk(reg) - reg); } - return dst_ - origin; } static int @@ -193,7 +193,7 @@ static int apply_rule(Rule *r, char *s) { unsigned char rid; - char c, *a = r->a, *b = r->b, *origin = dst_, *reg, last = 0; + char c, *a = r->a, *b = r->b, *origin = dst_, *reg; /* phase: clean regs */ while(stack_ != stack) regs[(int)*(--stack_)] = 0; /* phase: match rule */ @@ -215,16 +215,10 @@ apply_rule(Rule *r, char *s) if(!spacer(c)) return 0; /* phase: write rule */ while((c = *b++)) - if(c == '?' && (rid = *b) && (reg = regs[rid])) { - b++; - if(!write_reg(rid, reg)) { - if(*b == ' ') - b++; - else if(last == ' ') - dst_--; - } - } else - *dst_++ = last = c; + if(c == '?' && (rid = *b) && (reg = regs[rid])) + write_reg(rid, reg), b++; + else + *dst_++ = c; if(dst_ == origin) { while(*s == ' ') s++; if(*s == ')' && *(dst_ - 1) == ' ') dst_--;