diff --git a/src/modal.c b/src/modal.c index 2dcd110..661a36b 100644 --- a/src/modal.c +++ b/src/modal.c @@ -123,35 +123,36 @@ file_import(char *path, char *ptr) return copy("NAF", ptr, 3); } -static void +static int write_reg(char r, char *reg) { + char *origin = dst_; switch(r) { - case ':': device_write(reg); return; + case ':': device_write(reg); break; case '~': { unsigned char c; while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c; if(feof(stdin)) dst_ = copy("EOF", dst_, 3); - return; + break; } case '_': { char filepath[0x80]; copy(reg, filepath, walk(reg) - reg); dst_ = file_import(filepath, dst_); - return; + break; } case '^': { /* op: join */ char c, *cap = walk(reg); if(*reg == '(') reg++, --cap; while(reg < cap && (c = *reg++)) if(!spacer(c)) *dst_++ = c; - return; + break; } case '.': { /* op: unwrap */ char *cap = walk(reg); if(*reg == '(') reg++, --cap; dst_ = copy(reg, dst_, cap - reg); - return; + break; } case '*': { /* op: explode */ int i, depth = 0; @@ -167,13 +168,11 @@ write_reg(char r, char *reg) while((c = *reg++) && !spacer(c)) *dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++; for(i = 0; i < depth; i++) *dst_++ = ')'; - return; - } - default: { - dst_ = copy(reg, dst_, walk(reg) - reg); - return; + break; } + default: dst_ = copy(reg, dst_, walk(reg) - reg); } + return dst_ - origin; } static int @@ -216,12 +215,13 @@ apply_rule(Rule *r, char *s) if(!spacer(c)) return 0; /* phase: write rule */ while((c = *b++)) - if(c == '?' && (rid = *b) && (reg = regs[rid])){ - char *ori = dst_; - write_reg(rid, reg), b++; - if(dst_ == ori){ - if(b[0] == ' ') b++; - else if(last == ' ') dst_--; + if(c == '?' && (rid = *b) && (reg = regs[rid])) { + b++; + if(!write_reg(rid, reg)) { + if(*b == ' ') + b++; + else if(last == ' ') + dst_--; } } else *dst_++ = last = c;