From 3a13f268eb32f4d6d2c27562dc885eb05431f41b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 13 May 2022 23:05:26 -0700 Subject: Tighten the command syntax. Commands are now separated from their argument by exactly one space, which may only be omitted for an argument which does not begin with a letter. Thus :gfoo no longer pushes a regular expression foo onto the grep stack, but is the unrecognized command gfoo. The command :g foo specifies the regular expression " foo". --- pw.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'pw.c') diff --git a/pw.c b/pw.c index aaaeea0..1a1e5ea 100644 --- a/pw.c +++ b/pw.c @@ -801,9 +801,15 @@ static execode execute(pwstate *pw, char *cmd, char *resbuf, size_t size, int count) { execode res = exec_failed; - char *arg = cmd + 2 + strspn(cmd + 2, " \t"); + char cmdbuf[16]; + int cmdlen = 0; + int ntok = sscanf(cmd + 1, "%15[a-z]%n", cmdbuf, &cmdlen); + char *arg = cmd + 1 + cmdlen; - if (cmd[0] == ':') switch (cmd[1]) { + if (*arg == ' ') + arg++; + + if (cmd[0] == ':' && ntok == 1 && cmdlen == 1) switch (cmd[1]) { case 'w': case 'a': if (arg[0] == 0) { snprintf(resbuf, size, "file name required!"); @@ -989,7 +995,7 @@ static execode execute(pwstate *pw, char *cmd, char *resbuf, rflg = gr->flags; fputs(((gr->flags & REG_EXTENDED)) ? ":E\n" : ":B\n", f); } - fputs(gr->inv ? ":v" : ":g", f); + fputs(gr->inv ? ":v " : ":g ", f); fputs(gr->pat, f); putc('\n', f); } @@ -1027,9 +1033,9 @@ static execode execute(pwstate *pw, char *cmd, char *resbuf, res = exec_ok; break; default: - snprintf(resbuf, size, "bad command"); + goto badcmd; break; - } else { + } else if (cmd[0] == '?' || cmd[0] == '/') { int trig = count > 0 ? count - 1 : count; if (trig < pw->maxlines && trig < maxtrig) @@ -1082,6 +1088,9 @@ static execode execute(pwstate *pw, char *cmd, char *resbuf, } } } + } else { +badcmd: + snprintf(resbuf, size, "unrecognized command"); } return res; -- cgit v1.2.3