Improved catching of empty registers

This commit is contained in:
Devine Lu Linvega 2024-05-24 18:34:43 -07:00
parent 9780e2aad7
commit 6f5b5fc36c
1 changed files with 17 additions and 17 deletions

View File

@ -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
@ -217,11 +216,12 @@ apply_rule(Rule *r, char *s)
/* 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_--;
b++;
if(!write_reg(rid, reg)) {
if(*b == ' ')
b++;
else if(last == ' ')
dst_--;
}
} else
*dst_++ = last = c;