aboutsummaryrefslogtreecommitdiffstats
path: root/pw.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-04-27 20:06:25 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-04-27 20:06:25 -0700
commit824aca06ffbde70f86a72e474c051e0bf4474906 (patch)
treeb1e4822d16f64ab4d05e827600959981dcb478bb /pw.c
parentcee49acbf13324bce50b4b8ef9c76fcca17790a0 (diff)
downloadpw-824aca06ffbde70f86a72e474c051e0bf4474906.tar.gz
pw-824aca06ffbde70f86a72e474c051e0bf4474906.tar.bz2
pw-824aca06ffbde70f86a72e474c051e0bf4474906.zip
Regex matching for trigger mode.
Diffstat (limited to 'pw.c')
-rw-r--r--pw.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/pw.c b/pw.c
index 9e2a64a..60c94b0 100644
--- a/pw.c
+++ b/pw.c
@@ -11,6 +11,7 @@
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/time.h>
+#include <regex.h>
enum status_flags {
stat_dirty = 1, // display needs refresh
@@ -32,6 +33,7 @@ typedef struct dstr {
char **snapshot;
int snaplines;
char *trigpat;
+regex_t trigex;
static void panic(const char *fmt, ...)
{
@@ -429,7 +431,7 @@ int main(int argc, char **argv)
if ((stat & stat_eof) == 0 && pe[1].revents) {
if ((line = getln(stdin))) {
if ((stat & stat_htmode))
- if (strstr(line, trigpat))
+ if (regexec(&trigex, line, 0, NULL, 0) == 0)
stat |= stat_trgrd;
if (nlines == maxlines) {
dsdrop(circbuf[0]);
@@ -437,7 +439,7 @@ int main(int argc, char **argv)
circbuf[nlines - 1] = line;
stat |= stat_dirty;
if ((stat & stat_ttmode))
- if (strstr(circbuf[0], trigpat))
+ if (regexec(&trigex, circbuf[0], 0, NULL, 0) == 0)
stat |= stat_trgrd;
} else {
circbuf[nlines++] = line;
@@ -542,12 +544,18 @@ int main(int argc, char **argv)
goto fakecmd;
}
break;
- case kbd_colon:
case kbd_htrig:
case kbd_ttrig:
+ if (trigpat) {
+ regfree(&trigex);
+ dsdrop(trigpat);
+ trigpat = 0;
+ }
+ stat &= ~(stat_htmode | stat_ttmode);
+ // fallthrough
+ case kbd_colon:
switch (ch) {
case 27: case 13: case 3:
- kbd_state = kbd_cmd;
stat |= stat_dirty;
if (ch == 13) {
if (kbd_state == kbd_colon && cmdbuf[1]) {
@@ -556,14 +564,23 @@ int main(int argc, char **argv)
kbd_state = kbd_result;
break;
} else if (cmdbuf[1]) {
- dsdrop(trigpat);
+ int err;
trigpat = dsdup(cmdbuf + 1);
- stat &= ~(stat_htmode | stat_ttmode);
- stat |= (kbd_state == kbd_htrig) ? stat_htmode : stat_ttmode;
- } else {
- stat &= ~(stat_htmode | stat_ttmode);
+ if ((err = regcomp(&trigex, trigpat,
+ REG_EXTENDED | REG_NOSUB)))
+ {
+ regerror(err, &trigex, cmdbuf, sizeof cmdbuf);
+ if (columns < (int) sizeof cmdbuf - 1)
+ cmdbuf[columns] = 0;
+ kbd_state = kbd_result;
+ dsdrop(trigpat);
+ trigpat = 0;
+ break;
+ }
+ stat |= (kbd_state == kbd_htrig ? stat_htmode : stat_ttmode);
}
}
+ kbd_state = kbd_cmd;
curcmd = 0;
break;
case 8: case 127: