diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-04-27 20:06:25 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-04-27 20:06:25 -0700 |
commit | 824aca06ffbde70f86a72e474c051e0bf4474906 (patch) | |
tree | b1e4822d16f64ab4d05e827600959981dcb478bb /pw.c | |
parent | cee49acbf13324bce50b4b8ef9c76fcca17790a0 (diff) | |
download | pw-824aca06ffbde70f86a72e474c051e0bf4474906.tar.gz pw-824aca06ffbde70f86a72e474c051e0bf4474906.tar.bz2 pw-824aca06ffbde70f86a72e474c051e0bf4474906.zip |
Regex matching for trigger mode.
Diffstat (limited to 'pw.c')
-rw-r--r-- | pw.c | 35 |
1 files changed, 26 insertions, 9 deletions
@@ -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: |