diff options
Diffstat (limited to 'pw.c')
-rw-r--r-- | pw.c | 39 |
1 files changed, 33 insertions, 6 deletions
@@ -279,8 +279,11 @@ static void usage(void) "-d do not quit on end-of-input\n" "-q integer Require this many repetitions of q to quit\n" "-E treat regular expressions as extended\n" - "-B treat regular expressions as basic (default)\n\n" - "-g [!]pattern add pattern to grep stack; ! inverts.\n\n" + "-B treat regular expressions as basic (default)\n" + "-g [!]pattern add pattern to grep stack; ! inverts\n" + "-m integer specify maixmum line length\n" + "-p values set display parameters\n" + "-e command execute :, / or ? command\n\n" "<command> represents an arbitrary command that generates the\n" "output to be monitored by %s.\n\n" "Standard input must be redirected; it cannot be the same device\n" @@ -820,7 +823,7 @@ static execode execute(pwstate *pw, const char *cmd, char *resbuf, snprintf(resbuf, size, "bad command"); break; } else { - int trig = count; + int trig = count > 0 ? count - 1 : count; if (trig < pw->maxlines && trig < maxtrig) { @@ -857,6 +860,9 @@ static execode execute(pwstate *pw, const char *cmd, char *resbuf, res = exec_ok; } dsdrop(pat); + } else { + snprintf(resbuf, size, "trigger position out of range"); + res = exec_failed; } if (res == exec_ok) { @@ -968,7 +974,7 @@ int main(int argc, char **argv) } } - while ((opt = getopt(argc, argv, "n:i:l:dEBg:q:m:p:")) != -1) { + while ((opt = getopt(argc, argv, "n:i:l:dEBg:q:m:p:e:")) != -1) { switch (opt) { case 'n': { @@ -1089,6 +1095,27 @@ int main(int argc, char **argv) } } break; + case 'e': + { + size_t ndigits = strspn(optarg, "0123456789"); + int count = 0; + const char *cmd = optarg; + + if (ndigits > 3) { + error("-%c option %s: command count out of 0-999 range\n", + opt, optarg); + break; + } else if (ndigits) { + (void) sscanf(cmd, "%3d", &count); + cmd += ndigits; + } + + if (execute(&pw, cmd, pw.cmdbuf, cmdsize, count) == exec_failed) { + error("-%c option %s: %s\n", opt, optarg, pw.cmdbuf); + return EXIT_FAILURE; + } + } + break; default: usage(); } @@ -1606,8 +1633,7 @@ int main(int argc, char **argv) break; case CR: case ctrl('c'): if (ch == CR) { - int count = (cmdcount == INT_MAX || cmdcount == 0 - ? 0 : cmdcount - 1); + int count = (cmdcount == INT_MAX) ? 0 : cmdcount; if (pw.cmdbuf[1]) { int *pnhist = (kbd_state == kbd_lcmd ? &ncmdhist : &npathist); int nhist = *pnhist; @@ -1629,6 +1655,7 @@ int main(int argc, char **argv) if (pw.columns < cmdsize) pw.cmdbuf[pw.columns] = 0; kbd_state = kbd_result; + cmdcount = INT_MAX; break; } } |