aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bib-strip.awk
diff options
context:
space:
mode:
authorDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2026-06-06 13:44:00 -0400
committerDouglas B. Rumbaugh <doug@douglasrumbaugh.com>2026-06-06 13:44:00 -0400
commitc102ab995f9a86a77e40b9a952b2b23c0bd7de74 (patch)
treed51b9a8f1a55f7f6e6e5afb89d524b9baa350f45 /lib/bib-strip.awk
parentb56c273d8198ae6cee69bbc9fe5a6a61da4074e4 (diff)
downloadbibutils-c102ab995f9a86a77e40b9a952b2b23c0bd7de74.tar.gz
Fuzzing with associated fixes
Diffstat (limited to 'lib/bib-strip.awk')
-rw-r--r--lib/bib-strip.awk49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/bib-strip.awk b/lib/bib-strip.awk
new file mode 100644
index 0000000..cecca3e
--- /dev/null
+++ b/lib/bib-strip.awk
@@ -0,0 +1,49 @@
+# bib-strip.awk - remove entries by key, preserving all other bytes
+#
+# Requires bib-parse.awk. Variables (set with -v):
+# keys - comma-separated list of entry keys to remove
+# keyfile - file with one key per line, for key lists too large to
+# pass on the command line; merged with keys
+#
+# Unlike bib-select.awk, which re-emits entries canonically, this
+# splices the matched entries' source spans out of the input and
+# leaves everything else - comments, formatting, @string blocks -
+# byte-for-byte intact. Used by bib-add -f so that replacing one
+# entry never rewrites the rest of the database.
+#
+# This END block runs after bib-parse.awk's (END blocks execute in
+# the order their files are given to awk), so the spans recorded by
+# the hooks below are complete by the time output happens.
+
+BEGIN {
+ bib_strip_n = split(keys, bib_strip_k, ",")
+ for (bib_strip_i = 1; bib_strip_i <= bib_strip_n; bib_strip_i++)
+ BIB_DROP[bib_strip_k[bib_strip_i]] = 1
+ if (keyfile != "") {
+ while ((getline bib_strip_line < keyfile) > 0)
+ BIB_DROP[bib_strip_line] = 1
+ close(keyfile)
+ }
+}
+
+function bib_pass(raw) { }
+
+function bib_entry(type, key) {
+ if (key in BIB_DROP) {
+ BIB_NSPAN++
+ BIB_SPAN_S[BIB_NSPAN] = BIB_START
+ BIB_SPAN_E[BIB_NSPAN] = BIB_END
+ }
+}
+
+END {
+ i = 1
+ for (j = 1; j <= BIB_NSPAN; j++) {
+ printf "%s", substr(bib_buf, i, BIB_SPAN_S[j] - i)
+ i = BIB_SPAN_E[j]
+ # swallow the whitespace that followed the removed entry
+ while (i <= length(bib_buf) && substr(bib_buf, i, 1) ~ /[ \t\r\n]/)
+ i++
+ }
+ printf "%s", substr(bib_buf, i)
+}