ongrep

A cleaned up fork of ngrep for OpenBSD
git clone git://git.sgregoratto.me/ongrep
Log | Files | Refs | README | LICENSE

commit 627fe9ae40619adf9b025523384a5770b9b79bc9
parent d0c17df8f9a7f017023aef80702ac41e33ade245
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed, 24 Jun 2020 21:42:25 +1000

Re-enable data highlighting for regexes

Also note in the manpage that highlighting only happens for the *first*
occurence of expression.

Diffstat:
Mngrep.8 | 3+++
Mngrep.c | 29++++++++---------------------
2 files changed, 11 insertions(+), 21 deletions(-)

diff --git a/ngrep.8 b/ngrep.8 @@ -40,6 +40,9 @@ Dump .Ar num packets of trailing context after matching a packet. .It Fl C +Highlight the first occurence of +.Ar expression +in bright red. Enable highlighting of matched data. .It Fl c Ar cols Explicitly set the console width to diff --git a/ngrep.c b/ngrep.c @@ -867,33 +867,20 @@ int8_t re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, uint16_t *msize) { - static int sub[2]; + static int matchpos[2] = {0}; int did_match = pcre_exec(pattern, 0, (const char *)data, (int)len, - 0, 0, 0, 0); + 0, 0, matchpos, 2); if (did_match < 0) { - switch (did_match) { - case PCRE_ERROR_NULL: - case PCRE_ERROR_BADOPTION: - case PCRE_ERROR_BADMAGIC: - case PCRE_ERROR_UNKNOWN_NODE: - case PCRE_ERROR_NOMEMORY: - warnx("pcre_exec: fatal error"); - clean_exit(-1); - case PCRE_ERROR_NOMATCH: + if (did_match == PCRE_ERROR_NOMATCH) { return 0; + } else { + warnx("pcre_exec: returned %d", did_match); + clean_exit(-1); } } - /* - * XXX: This doesn't actually do anything! - * We need to save the pattern offset/length for the first match in the - * pcre output vector. - * - * Pos = ovec[0] - * Len = ovec[1] - ovec[0] - */ - *mindex = sub[0]; - *msize = sub[1] - sub[0]; + *mindex = matchpos[0]; + *msize = matchpos[1] - matchpos[0]; matches++; if (match_after && keep_matching != match_after)