:: commit a5f29add002e934e591431f8bd4a5d7931745b26

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-09-02 07:35

parents: 6f0f57b697

bz3less & bz3grep

diff --git a/Makefile.am b/Makefile.am
index ae22cc9..e053070 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -29,9 +29,9 @@ bzip3_CFLAGS = $(AM_CFLAGS)
 bzip3_LDDADD = libbzip3.la
 bzip3_SOURCES = src/main.c $(libbzip3_la_SOURCES)
 
-dist_man_MANS = bzip3.1 bz3cat.1 bz3more.1
+dist_man_MANS = bzip3.1 bz3cat.1 bz3more.1 bz3less.1 bz3grep.1
 
-dist_bin_SCRIPTS = bz3cat bz3more unbzip3
+dist_bin_SCRIPTS = bz3cat bz3more bz3less bz3grep unbzip3
 
 CLEANFILES = $(bin_PROGRAMS)
 
diff --git a/bz3grep b/bz3grep
new file mode 100644
index 0000000..4c29ad7
--- /dev/null
+++ b/bz3grep
@@ -0,0 +1,136 @@
+#!/bin/sh
+#
+# Copyright (c) 2003 Thomas Klausner.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Adapted for bz3cat.
+
+grep=${GREP:-grep}
+zcat=${ZCAT:-bz3cat}
+
+endofopts=0
+pattern_found=0
+grep_args=""
+hyphen=0
+silent=0
+
+prog=${0##*/}
+
+# handle being called 'zegrep' or 'zfgrep'
+case $prog in
+    *egrep*) prog=zegrep; grep_args='-E';;
+    *fgrep*) prog=zfgrep; grep_args='-F';;
+    *)       prog=zstdgrep;;
+esac
+
+# skip all options and pass them on to grep taking care of options
+# with arguments, and if -e was supplied
+
+while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
+    case "$1" in
+    # from GNU grep-2.5.1 -- keep in sync!
+        -[ABCDXdefm])
+            if [ "$#" -lt 2 ]; then
+                printf '%s: missing argument for %s flag\n' "${prog}" "$1" >&2
+                exit 1
+            fi
+            case "$1" in
+                -e)
+                    pattern="$2"
+                    pattern_found=1
+                    shift 2
+                    break
+                    ;;
+                -f)
+                    pattern_found=2
+                    ;;
+                *)
+                    ;;
+            esac
+            grep_args="${grep_args} $1 $2"
+            shift 2
+            ;;
+        --)
+            shift
+            endofopts=1
+            ;;
+        -)
+            hyphen=1
+            shift
+            ;;
+        -h)
+            silent=1
+            shift
+            ;;
+        -*)
+            grep_args="${grep_args} $1"
+            shift
+            ;;
+        *)
+            # pattern to grep for
+            endofopts=1
+            ;;
+    esac
+done
+
+# if no -e option was found, take next argument as grep-pattern
+if [ "${pattern_found}" -lt 1 ]; then
+    if [ "$#" -ge 1 ]; then
+        pattern="$1"
+        shift
+    elif [ "${hyphen}" -gt 0 ]; then
+        pattern="-"
+    else
+        printf '%s: missing pattern\n' "${prog}" >&2
+        exit 1
+    fi
+fi
+
+EXIT_CODE=0
+# call grep ...
+if [ "$#" -lt 1 ]; then
+    # ... on stdin
+    set -f # Disable file name generation (globbing).
+    # shellcheck disable=SC2086
+    "${zcat}" - | "${grep}" ${grep_args} -- "${pattern}" -
+    EXIT_CODE=$?
+    set +f
+else
+    # ... on all files given on the command line
+    if [ "${silent}" -lt 1 ] && [ "$#" -gt 1 ]; then
+        grep_args="-H ${grep_args}"
+    fi
+    set -f
+    while [ "$#" -gt 0 ]; do
+        # shellcheck disable=SC2086
+        if [ $pattern_found -eq 2 ]; then
+            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- -
+        else
+            "${zcat}" -- "$1" | "${grep}" --label="${1}" ${grep_args} -- "${pattern}" -
+        fi
+        [ "$?" -ne 0 ] && EXIT_CODE=1
+        shift
+    done
+    set +f
+fi
+
+exit "${EXIT_CODE}"
\ No newline at end of file
diff --git a/bz3grep.1 b/bz3grep.1
new file mode 100644
index 0000000..c1b5fd9
--- /dev/null
+++ b/bz3grep.1
@@ -0,0 +1,22 @@
+.TH "BZ3GREP" "1"
+.
+.SH "NAME"
+\fBbz3grep\fR \- print lines matching a pattern in bzip3\-compressed files
+.
+.SH "SYNOPSIS"
+\fBbz3grep\fR [\fIgrep\-flags\fR] [\-\-] \fIpattern\fR [\fIfiles\fR \.\.\.]
+.
+.SH "DESCRIPTION"
+\fBbz3grep\fR runs \fBgrep (1)\fR on files, or \fBstdin\fR if no files argument is given, after decompressing them with \fBbz3cat (1)\fR\.
+.
+.P
+The grep\-flags and pattern arguments are passed on to \fBgrep (1)\fR\. If an \fB\-e\fR flag is found in the \fBgrep\-flags\fR, \fBbz3grep\fR will not look for a pattern argument\.
+.
+.SH "EXIT STATUS"
+In case of missing arguments or missing pattern, 1 will be returned, otherwise 0\.
+.
+.SH "SEE ALSO"
+\fBbzip3 (1)\fR
+.
+.SH "AUTHORS"
+Thomas Klausner \fIwiz@NetBSD\.org\fR
\ No newline at end of file
diff --git a/bz3less b/bz3less
new file mode 100644
index 0000000..e86f055
--- /dev/null
+++ b/bz3less
@@ -0,0 +1,2 @@
+#!/bin/sh
+bz3cat "$@" | less
diff --git a/bz3less.1 b/bz3less.1
new file mode 100644
index 0000000..e19d579
--- /dev/null
+++ b/bz3less.1
@@ -0,0 +1,15 @@
+.TH BZ3LESS 1
+
+.SH "NAME"
+\fBbz3less\fR \- view bzip3\-compressed files
+
+.SH "SYNOPSIS"
+\fBbz3less\fR [\fIflags\fR] [\fIfile\fR \.\.\.]
+
+.SH "DESCRIPTION"
+\fBbz3less\fR runs \fBless (1)\fR on files or stdin,
+if no files argument is given, after decompressing them
+with \fBbz3cat (1)\fR\.
+
+.SH "SEE ALSO"
+\fBbzip3 (1)\fR, \fBless (1)\fR
diff --git a/bz3more.1 b/bz3more.1
index 78268bf..afbc466 100644
--- a/bz3more.1
+++ b/bz3more.1
@@ -7,9 +7,9 @@
 \fBbz3more\fR [\fIflags\fR] [\fIfile\fR \.\.\.]
 
 .SH "DESCRIPTION"
-\fBbz3more\fR runs \fBless (1)\fR on files or stdin,
+\fBbz3more\fR runs \fBmore (1)\fR on files or stdin,
 if no files argument is given, after decompressing them
 with \fBbz3cat (1)\fR\.
 
 .SH "SEE ALSO"
-\fBbzip3 (1)\fR, \fBmore (1)\fR
\ No newline at end of file
+\fBbzip3 (1)\fR, \fBmore (1)\fR
diff --git a/bzip3.1 b/bzip3.1
index 9c68320..ba31418 100644
--- a/bzip3.1
+++ b/bzip3.1
@@ -1,6 +1,7 @@
 .TH BZIP3 1
 .SH NAME
-bzip3 \- an efficient statistical file compressor and spiritual successor to bzip2
+bzip3 \- an efficient statistical file compressor and spiritual successor
+to bzip2
 
 .SH SYNOPSIS
 .B bzip3
@@ -13,9 +14,14 @@ bzip3 \- an efficient statistical file compressor and spiritual successor to bzi
 .B bz3cat
 is equivalent to
 .B bzip3 \-dc
+.P
+.B unbzip3
+is equivalent to
+.B bzip3 \-d
 .SH DESCRIPTION
 Compress or decompress a file using run length encoding and Lempel Ziv
-prediction, followed by the Burrows-Wheeler transform and arithmetic coding.
+prediction, followed by the Burrows-Wheeler transform and arithmetic
+coding.
 .B bzip3,
 like its ancestor
 .B bzip2,
@@ -40,8 +46,8 @@ to output binary data to a terminal. The \-e flag (encode) is implied.
 .B unbzip3
 (or,
 .B bzip3 \-d
-equivalently) decompresses data from standard input to the standard output,
-refusing to read from a terminal.
+equivalently) decompresses data from standard input to the standard
+output, refusing to read from a terminal.
 
 If two files are specified, the first one is used in place of
 standard input, and the second one is used in place of standard
@@ -72,10 +78,12 @@ will decompress all
 files in the current directory.
 .TP
 .B \-b --block N
-Set the block size to N mebibytes. The minimum is 1MiB, the maximum is 511MiB.
+Set the block size to N mebibytes. The minimum is 1MiB, the maximum is
+511MiB.
 .TP
 .B \-c --stdout
-Force writing output data to the standard output if one file is specified.
+Force writing output data to the standard output if one file is
+specified.
 .TP
 .B \-d --decode
 Force decompression.
@@ -99,14 +107,15 @@ Set the amount of parallel worker threads that process one block each.
 Verify the validity of compressed blocks.
 .TP
 .B \--
-Treat all subsequent arguments as file names, even if they start with a dash.
-This is so you can handle files with names beginning with a dash.
+Treat all subsequent arguments as file names, even if they start with
+a dash. This is so you can handle files with names beginning with a dash.
 .SH FILE FORMAT
 
-Compression is performed as long as the input block is longer than 64 bytes.
-Otherwise, it's coded as a literal block. In all other cases, the compressed data
-is written to the file. The file format has constant overhead of 9 bytes per
-file and from 9 to 17 bytes per block. Random data is coded so that expansion
+Compression is performed as long as the input block is longer
+than 64 bytes. Otherwise, it's coded as a literal block. In all
+other cases, the compressed data is written to the file. The
+file format has constant overhead of 9 bytes per file and from
+9 to 17 bytes per block. Random data is coded so that expansion
 is generally under 0.8%.
 
 .B bzip3
@@ -116,8 +125,8 @@ compressed data.
 
 .SH MEMORY MANAGEMENT
 
-The \-b flag sets the block size in mebibytes (MiB). The default is 16 MiB.
-Compression and decompression memory usage can be estimated as:
+The \-b flag sets the block size in mebibytes (MiB). The default is 16
+MiB. Compression and decompression memory usage can be estimated as:
 
        6 x block size
 
@@ -133,9 +142,9 @@ Kamila Szewczyk, kspalaiologos@gmail.com.
 
 https://github.com/kspalaiologos/bzip3
 
-Thanks to: Ilya Grebnov, Chloe Strachan, Caleb Maclennan, Ilya Muravyov and everyone
-who sent patches, helped with portability problems, encouraged me to work on bzip3
-and lent me machines for performance tests.
+Thanks to: Ilya Grebnov, Chloe Strachan, Caleb Maclennan, Ilya Muravyov
+and everyone who sent patches, helped with portability problems,
+encouraged me to work on bzip3 and lent me machines for performance tests.
 
 .SH "SEE ALSO"
-\fBbzip2 (1)\fR
\ No newline at end of file
+\fBbzip2 (1)\fR, \fBbz3less (1)\fR, \fBbz3more (1)\fR, \fBbz3grep (1)\fR
\ No newline at end of file
tab: 248 wrap: offon