aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-07-30 20:46:50 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-07-30 20:46:50 -0700
commitcb9b4c40d87480bc794b90c2a36ed0f4c3240d8a (patch)
treea05b73ef7e5e1e58c1d85cffba1adb4aef2532c1
parent9c05f880b00065abfa6f8c9335a50522b76a4fa2 (diff)
Added -w/--word flag
-rw-r--r--bp.111
-rw-r--r--bp.1.md11
-rw-r--r--bp.c11
3 files changed, 25 insertions, 8 deletions
diff --git a/bp.1 b/bp.1
index 9a60593..8a9be7f 100644
--- a/bp.1
+++ b/bp.1
@@ -15,8 +15,13 @@ bp - Bruce\[aq]s Parsing Expression Grammar tool
custom syntax.
.SH OPTIONS
.TP
-\f[B]-v\f[R], \f[B]--verbose\f[R]
-Print debugging information.
+\f[B]-p\f[R], \f[B]--pattern\f[R] \f[I]pat\f[R]
+Give a pattern in BP syntax instead of string syntax (equivalent to
+\f[B]bp \[aq]\[rs](pat)\[aq]\f[R]
+.TP
+\f[B]-w\f[R], \f[B]--word\f[R] \f[I]word\f[R]
+Surround a string pattern with word boundaries (equivalent to
+\f[B]bp \[aq]\[rs]|word\[rs]|\[aq]\f[R])
.TP
\f[B]-e\f[R], \f[B]--explain\f[R]
Print a visual explanation of the matches.
@@ -73,7 +78,7 @@ Set the output format.
includes neither, and \f[I]auto\f[R] (the default) uses \f[I]fancy\f[R]
formatting only when the output is a TTY.
.TP
-\f[B]--help\f[R]
+\f[B]-h\f[R], \f[B]--help\f[R]
Print the usage and exit.
.TP
\f[I]pattern\f[R]
diff --git a/bp.1.md b/bp.1.md
index 8e9482d..13f98e7 100644
--- a/bp.1.md
+++ b/bp.1.md
@@ -17,8 +17,13 @@ syntax.
# OPTIONS
-`-v`, `--verbose`
-: Print debugging information.
+`-p`, `--pattern` *pat*
+: Give a pattern in BP syntax instead of string syntax (equivalent to `bp
+'\(pat)'`
+
+`-w`, `--word` *word*
+: Surround a string pattern with word boundaries (equivalent to `bp
+'\|word\|'`)
`-e`, `--explain`
: Print a visual explanation of the matches.
@@ -69,7 +74,7 @@ occur).
includes neither, and *auto* (the default) uses *fancy* formatting only when
the output is a TTY.
-`--help`
+`-h`, `--help`
: Print the usage and exit.
*pattern*
diff --git a/bp.c b/bp.c
index b36607e..c9eb009 100644
--- a/bp.c
+++ b/bp.c
@@ -48,6 +48,7 @@ static const char *usage = (
" -C --confirm ask for confirmation on each replacement\n"
" -l --list-files list filenames only\n"
" -p --pattern <pat> provide a pattern (equivalent to bp '\\(<pat>)')\n"
+ " -w --word <string-pat> find words matching the given string pattern\n"
" -r --replace <replacement> replace the input pattern with the given replacement\n"
" -s --skip <skip-pattern> skip over the given pattern when looking for matches\n"
" -c --context <context> set number of lines of context to print (all: the whole file, 0: only the match, 1: the line, N: N lines of context)\n"
@@ -559,6 +560,13 @@ int main(int argc, char *argv[])
str = after_spaces(p->end);
}
}
+ } else if (FLAG("-w") || FLAG("--word")) {
+ check_nonnegative(asprintf(&flag, "\\|%s\\|", flag), "Could not allocate memory");
+ file_t *arg_file = spoof_file(&loaded_files, "<word pattern>", flag, -1);
+ delete(&flag);
+ pat_t *p = bp_stringpattern(arg_file, arg_file->start);
+ if (!p) errx(EXIT_FAILURE, "Pattern failed to compile: %s", flag);
+ pattern = chain_together(arg_file, pattern, p);
} else if (FLAG("-s") || FLAG("--skip")) {
file_t *arg_file = spoof_file(&loaded_files, "<skip argument>", flag, -1);
pat_t *s = bp_pattern(arg_file, arg_file->start);
@@ -588,8 +596,7 @@ int main(int argc, char *argv[])
if (pattern != NULL) break;
file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", argv[0], -1);
pat_t *p = bp_stringpattern(arg_file, arg_file->start);
- if (!p)
- errx(EXIT_FAILURE, "Pattern failed to compile: %s", argv[0]);
+ if (!p) errx(EXIT_FAILURE, "Pattern failed to compile: %s", argv[0]);
pattern = chain_together(arg_file, pattern, p);
++argv;
} else {