aboutsummaryrefslogtreecommitdiffstats
path: root/pw.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-07 16:16:51 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-07 16:16:51 -0700
commita5bb91bf9df40b6f0f896dfaf57555651b8c2bc2 (patch)
treed481784f4c9f4210c25d581a230bd653e2996d85 /pw.c
parentb45105b94ee7d712a4d7b2a1c9054fd95e715093 (diff)
downloadpw-a5bb91bf9df40b6f0f896dfaf57555651b8c2bc2.tar.gz
pw-a5bb91bf9df40b6f0f896dfaf57555651b8c2bc2.tar.bz2
pw-a5bb91bf9df40b6f0f896dfaf57555651b8c2bc2.zip
New -e option to execute commands.
Now there is a way to set up triggers prior to execution.
Diffstat (limited to 'pw.c')
-rw-r--r--pw.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/pw.c b/pw.c
index eec33fc..3f93fde 100644
--- a/pw.c
+++ b/pw.c
@@ -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;
}
}