git: 4cd4841a2773 - main - Modularize uuencode and uudecode by wrapping them in bintrans.c

From: Piotr Pawel Stefaniak <pstef_at_FreeBSD.org>
Date: Mon, 18 Apr 2022 08:54:09 UTC
The branch main has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=4cd4841a2773d3674151f4acef0a1bf05212c87d

commit 4cd4841a2773d3674151f4acef0a1bf05212c87d
Author:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
AuthorDate: 2022-04-05 19:21:16 +0000
Commit:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
CommitDate: 2022-04-18 08:46:44 +0000

    Modularize uuencode and uudecode by wrapping them in bintrans.c
    
    The program will be installed as bintrans, uuencode, uudecode,
    b64encode, and b64decode and will be responsible for running the coders
    according to their historical behavior.
    
    Additionally, bintrans will be able to take a parameter designating
    the coder and accept all its options in this form:
    bintrans <coder> [options]
    and the behavior should be the same as if
    <coder> [options]
    was invoked.
    This has the advantage that adding coders won't require installing them
    as binaries.
    
    Move uudecode files to uuencode since the latter is the one that
    provides the manual page.
    
    Reviewed by:    delphij (previous version)
    Differential Revision:  https://reviews.freebsd.org/D32943
---
 Makefile.inc1                                      |   2 +-
 etc/mtree/BSD.tests.dist                           |   2 -
 targets/pseudo/tests/Makefile.depend               |   1 -
 targets/pseudo/userland/Makefile.depend            |   1 -
 usr.bin/Makefile                                   |   1 -
 usr.bin/uudecode/Makefile                          |  13 ---
 usr.bin/uudecode/Makefile.depend                   |  18 -----
 usr.bin/uudecode/tests/Makefile                    |  13 ---
 usr.bin/uudecode/tests/Makefile.depend             |  11 ---
 usr.bin/uudecode/tests/legacy_test.sh              |   6 --
 usr.bin/uudecode/tests/regress.sh                  |  13 ---
 usr.bin/uuencode/Makefile                          |  15 ++--
 usr.bin/uuencode/{uuencode.1 => bintrans.1}        |  30 ++++---
 usr.bin/uuencode/bintrans.c                        |  88 +++++++++++++++++++++
 usr.bin/uuencode/tests/Makefile                    |   7 +-
 .../{uudecode => uuencode}/tests/regress.153276.in |   0
 .../tests/regress.153276.out                       |   0
 .../{uudecode => uuencode}/tests/regress.base64.in |   0
 usr.bin/{uudecode => uuencode}/tests/regress.out   | Bin
 usr.bin/uuencode/tests/regress.sh                  |   4 +-
 .../tests/regress.traditional.in                   |   0
 usr.bin/{uudecode => uuencode}/uudecode.c          |   4 +-
 usr.bin/uuencode/uuencode.c                        |   4 +-
 23 files changed, 131 insertions(+), 102 deletions(-)

diff --git a/Makefile.inc1 b/Makefile.inc1
index 1f29e89881c0..303dd62a3bed 100644
--- a/Makefile.inc1
+++ b/Makefile.inc1
@@ -2393,7 +2393,7 @@ _basic_bootstrap_tools_multilink+=usr.bin/awk awk,nawk
 # file2c is required for building usr.sbin/config:
 _basic_bootstrap_tools+=usr.bin/file2c
 # uuencode/uudecode required for share/tabset
-_basic_bootstrap_tools+=usr.bin/uuencode usr.bin/uudecode
+_basic_bootstrap_tools_multilink+=usr.bin/uuencode uuencode,uudecode
 # xargs is required by mkioctls
 _basic_bootstrap_tools+=usr.bin/xargs
 # cap_mkdb is required for share/termcap:
diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index bbe6d3fc93a5..55fcd2796d43 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1088,8 +1088,6 @@
         ..
         units
         ..
-        uudecode
-        ..
         uuencode
         ..
         unifdef
diff --git a/targets/pseudo/tests/Makefile.depend b/targets/pseudo/tests/Makefile.depend
index 937b3d79f149..1f146cab5851 100644
--- a/targets/pseudo/tests/Makefile.depend
+++ b/targets/pseudo/tests/Makefile.depend
@@ -358,7 +358,6 @@ DIRDEPS= \
       usr.bin/truncate/tests \
       usr.bin/uniq/tests \
       usr.bin/units/tests \
-      usr.bin/uudecode/tests \
       usr.bin/uuencode/tests \
       usr.bin/xargs/tests \
       usr.bin/xinstall/tests \
diff --git a/targets/pseudo/userland/Makefile.depend b/targets/pseudo/userland/Makefile.depend
index 42b196559040..3ac9c23ec67e 100644
--- a/targets/pseudo/userland/Makefile.depend
+++ b/targets/pseudo/userland/Makefile.depend
@@ -365,7 +365,6 @@ DIRDEPS+= \
 	usr.bin/usbhidaction \
 	usr.bin/usbhidctl \
 	usr.bin/users \
-	usr.bin/uudecode \
 	usr.bin/uuencode \
 	usr.bin/vacation \
 	usr.bin/vgrind \
diff --git a/usr.bin/Makefile b/usr.bin/Makefile
index 433b076210f9..63a660ce0c18 100644
--- a/usr.bin/Makefile
+++ b/usr.bin/Makefile
@@ -169,7 +169,6 @@ SUBDIR=	alias \
 	unzip \
 	units \
 	unvis \
-	uudecode \
 	uuencode \
 	vis \
 	vmstat \
diff --git a/usr.bin/uudecode/Makefile b/usr.bin/uudecode/Makefile
deleted file mode 100644
index dbcbe8d93b8a..000000000000
--- a/usr.bin/uudecode/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-#	@(#)Makefile	8.1 (Berkeley) 6/6/93
-# $FreeBSD$
-
-.include <src.opts.mk>
-
-PROG=	uudecode
-LINKS=	${BINDIR}/uudecode ${BINDIR}/b64decode
-MAN=
-
-HAS_TESTS=
-SUBDIR.${MK_TESTS}+= tests
-
-.include <bsd.prog.mk>
diff --git a/usr.bin/uudecode/Makefile.depend b/usr.bin/uudecode/Makefile.depend
deleted file mode 100644
index d5ed563ecfc8..000000000000
--- a/usr.bin/uudecode/Makefile.depend
+++ /dev/null
@@ -1,18 +0,0 @@
-# $FreeBSD$
-# Autogenerated - do NOT edit!
-
-DIRDEPS = \
-	gnu/lib/csu \
-	include \
-	include/arpa \
-	include/xlocale \
-	lib/${CSU_DIR} \
-	lib/libc \
-	lib/libcompiler_rt \
-
-
-.include <dirdeps.mk>
-
-.if ${DEP_RELDIR} == ${_DEP_RELDIR}
-# local dependencies - needed for -jN in clean tree
-.endif
diff --git a/usr.bin/uudecode/tests/Makefile b/usr.bin/uudecode/tests/Makefile
deleted file mode 100644
index 204f25b18c9e..000000000000
--- a/usr.bin/uudecode/tests/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-# $FreeBSD$
-
-PACKAGE=	tests
-
-TAP_TESTS_SH=	legacy_test
-
-${PACKAGE}FILES+=		regress.base64.in
-${PACKAGE}FILES+=		regress.out
-${PACKAGE}FILES+=		regress.sh
-${PACKAGE}FILES+=		regress.traditional.in
-${PACKAGE}FILES+=		regress.153276.in regress.153276.out
-
-.include <bsd.test.mk>
diff --git a/usr.bin/uudecode/tests/Makefile.depend b/usr.bin/uudecode/tests/Makefile.depend
deleted file mode 100644
index f80275d86ab1..000000000000
--- a/usr.bin/uudecode/tests/Makefile.depend
+++ /dev/null
@@ -1,11 +0,0 @@
-# $FreeBSD$
-# Autogenerated - do NOT edit!
-
-DIRDEPS = \
-
-
-.include <dirdeps.mk>
-
-.if ${DEP_RELDIR} == ${_DEP_RELDIR}
-# local dependencies - needed for -jN in clean tree
-.endif
diff --git a/usr.bin/uudecode/tests/legacy_test.sh b/usr.bin/uudecode/tests/legacy_test.sh
deleted file mode 100644
index 1b6b806c5980..000000000000
--- a/usr.bin/uudecode/tests/legacy_test.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/sh
-# $FreeBSD$
-
-SRCDIR="$(dirname "${0}")"; export SRCDIR
-
-m4 "${SRCDIR}/../regress.m4" "${SRCDIR}/regress.sh" | sh
diff --git a/usr.bin/uudecode/tests/regress.sh b/usr.bin/uudecode/tests/regress.sh
deleted file mode 100644
index dca89804bfec..000000000000
--- a/usr.bin/uudecode/tests/regress.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-# $FreeBSD$
-
-echo 1..3
-
-REGRESSION_START($1)
-
-REGRESSION_TEST_ONE(`uudecode -p <${SRCDIR}/regress.traditional.in', `traditional')
-REGRESSION_TEST_ONE(`uudecode -p <${SRCDIR}/regress.base64.in', `base64')
-
-# was uudecode: stdin: /dev/null: character out of range: [33-96]
-REGRESSION_TEST(`153276', `uudecode -o /dev/null <${SRCDIR}/regress.153276.in 2>&1')
-
-REGRESSION_END()
diff --git a/usr.bin/uuencode/Makefile b/usr.bin/uuencode/Makefile
index 5897d89dc057..ef9686aa290f 100644
--- a/usr.bin/uuencode/Makefile
+++ b/usr.bin/uuencode/Makefile
@@ -3,12 +3,17 @@
 
 .include <src.opts.mk>
 
-PROG=	uuencode
-MAN=	uuencode.1 uuencode.format.5
-LINKS=	${BINDIR}/uuencode ${BINDIR}/b64encode
-MLINKS=	uuencode.1 uudecode.1 \
+PROG=	bintrans
+SRCS=	bintrans.c uuencode.c uudecode.c
+MAN=	bintrans.1 uuencode.format.5
+LINKS+=	${BINDIR}/bintrans ${BINDIR}/uuencode
+LINKS+=	${BINDIR}/bintrans ${BINDIR}/b64encode
+LINKS+=	${BINDIR}/bintrans ${BINDIR}/uudecode
+LINKS+=	${BINDIR}/bintrans ${BINDIR}/b64decode
+MLINKS=	bintrans.1 uudecode.1 \
+	bintrans.1 uuencode.1 \
 	uuencode.format.5 uuencode.5 \
-	uuencode.1 b64encode.1 \
+	bintrans.1 b64encode.1 \
 	b64encode.1 b64decode.1
 
 HAS_TESTS=
diff --git a/usr.bin/uuencode/uuencode.1 b/usr.bin/uuencode/bintrans.1
similarity index 94%
rename from usr.bin/uuencode/uuencode.1
rename to usr.bin/uuencode/bintrans.1
index 92064578c664..e3ad5813f4cd 100644
--- a/usr.bin/uuencode/uuencode.1
+++ b/usr.bin/uuencode/bintrans.1
@@ -25,13 +25,14 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\"     @(#)uuencode.1	8.1 (Berkeley) 6/6/93
+.\"     From: @(#)uuencode.1	8.1 (Berkeley) 6/6/93
 .\" $FreeBSD$
 .\"
-.Dd January 27, 2002
-.Dt UUENCODE 1
+.Dd April 18, 2022
+.Dt BINTRANS 1
 .Os
 .Sh NAME
+.Nm bintrans ,
 .Nm uuencode ,
 .Nm uudecode ,
 .Nm b64encode ,
@@ -39,6 +40,9 @@
 .Nd encode/decode a binary file
 .Sh SYNOPSIS
 .Nm
+.Op algorithm
+.Op ...
+.Nm uuencode
 .Op Fl m
 .Op Fl r
 .Op Fl o Ar output_file
@@ -64,7 +68,7 @@
 .Op Ar file
 .Sh DESCRIPTION
 The
-.Nm
+.Nm uuencode
 and
 .Nm uudecode
 utilities are used to transmit binary files over transmission mediums
@@ -74,7 +78,7 @@ data.
 The
 .Nm b64encode
 utility is synonymous with
-.Nm
+.Nm uuencode
 with the
 .Fl m
 flag specified.
@@ -87,7 +91,7 @@ with the
 flag specified.
 .Pp
 The
-.Nm
+.Nm uuencode
 utility reads
 .Ar file
 (or by default the standard input) and writes an encoded version
@@ -119,11 +123,11 @@ The
 utility ignores any leading and trailing lines.
 .Pp
 The following options are available for
-.Nm :
+.Nm uuencode :
 .Bl -tag -width ident
 .It Fl m
 Use the Base64 method of encoding, rather than the traditional
-.Nm
+.Nm uuencode
 algorithm.
 .It Fl r
 Produce raw output by excluding the initial and final framing lines.
@@ -146,7 +150,7 @@ Do not overwrite files.
 When used with the
 .Fl r
 flag, decode Base64 input instead of traditional
-.Nm
+.Nm uuencode
 input.
 Without
 .Fl r
@@ -163,7 +167,7 @@ and write output to standard output.
 Decode raw (or broken) input, which is missing the initial and
 possibly the final framing lines.
 The input is assumed to be in the traditional
-.Nm
+.Nm uuencode
 encoding, but if the
 .Fl m
 flag is used, or if the utility is invoked as
@@ -176,6 +180,10 @@ By default
 deletes any prefix ending with the last slash '/' for security
 reasons.
 .El
+.Pp
+.Nm
+is a generic utility that can run
+any of the aforementioned encoders and decoders.
 .Sh EXAMPLES
 The following example packages up a source tree, compresses it,
 uuencodes it and mails it to a user on another system.
@@ -213,7 +221,7 @@ uudecode -o /dev/stdout < $MAIL | zcat | tar xfv -
 The
 .Nm uudecode
 and
-.Nm
+.Nm uuencode
 utilities appeared in
 .Bx 4.0 .
 .Sh BUGS
diff --git a/usr.bin/uuencode/bintrans.c b/usr.bin/uuencode/bintrans.c
new file mode 100644
index 000000000000..e158dd2c1b5c
--- /dev/null
+++ b/usr.bin/uuencode/bintrans.c
@@ -0,0 +1,88 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2022 The FreeBSD Foundation
+ *
+ * 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 AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
+ */
+
+#include <sys/param.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+
+extern int	main_decode(int, char *[]);
+extern int	main_encode(int, char *[]);
+
+static int	search(const char *const);
+
+enum coders {
+	uuencode, uudecode, b64encode, b64decode
+};
+
+int
+main(int argc, char *argv[])
+{
+	const char *const progname = getprogname();
+	int coder = search(progname);
+
+	if (coder == -1 && argc > 1) {
+		argc--;
+		argv++;
+		coder = search(argv[0]);
+	}
+	switch (coder) {
+	case uuencode:
+	case b64encode:
+		main_encode(argc, argv);
+		break;
+	case uudecode:
+	case b64decode:
+		main_decode(argc, argv);
+		break;
+	default:
+		(void)fprintf(stderr,
+		    "usage: %s <uuencode | uudecode> ...\n"
+		    "       %s <b64encode | b64decode> ...\n",
+		    progname, progname);
+		exit(EX_USAGE);
+	}
+}
+
+static int
+search(const char *const progname)
+{
+#define DESIGNATE(item) [item] = #item
+	const char *const known[] = {
+		DESIGNATE(uuencode),
+		DESIGNATE(uudecode),
+		DESIGNATE(b64encode),
+		DESIGNATE(b64decode)
+	};
+
+	for (size_t i = 0; i < nitems(known); i++)
+		if (strcmp(progname, known[i]) == 0)
+			return ((int)i);
+	return (-1);
+}
diff --git a/usr.bin/uuencode/tests/Makefile b/usr.bin/uuencode/tests/Makefile
index 2c7f18c36328..d89babfaa16a 100644
--- a/usr.bin/uuencode/tests/Makefile
+++ b/usr.bin/uuencode/tests/Makefile
@@ -4,9 +4,10 @@ PACKAGE=	tests
 
 TAP_TESTS_SH=	legacy_test
 
-${PACKAGE}FILES+=		regress.base64.out
-${PACKAGE}FILES+=		regress.in
+${PACKAGE}FILES+=		regress.base64.in regress.base64.out
+${PACKAGE}FILES+=		regress.in regress.out
 ${PACKAGE}FILES+=		regress.sh
-${PACKAGE}FILES+=		regress.traditional.out
+${PACKAGE}FILES+=		regress.traditional.in regress.traditional.out
+${PACKAGE}FILES+=		regress.153276.in regress.153276.out
 
 .include <bsd.test.mk>
diff --git a/usr.bin/uudecode/tests/regress.153276.in b/usr.bin/uuencode/tests/regress.153276.in
similarity index 100%
rename from usr.bin/uudecode/tests/regress.153276.in
rename to usr.bin/uuencode/tests/regress.153276.in
diff --git a/usr.bin/uudecode/tests/regress.153276.out b/usr.bin/uuencode/tests/regress.153276.out
similarity index 100%
rename from usr.bin/uudecode/tests/regress.153276.out
rename to usr.bin/uuencode/tests/regress.153276.out
diff --git a/usr.bin/uudecode/tests/regress.base64.in b/usr.bin/uuencode/tests/regress.base64.in
similarity index 100%
rename from usr.bin/uudecode/tests/regress.base64.in
rename to usr.bin/uuencode/tests/regress.base64.in
diff --git a/usr.bin/uudecode/tests/regress.out b/usr.bin/uuencode/tests/regress.out
similarity index 100%
rename from usr.bin/uudecode/tests/regress.out
rename to usr.bin/uuencode/tests/regress.out
diff --git a/usr.bin/uuencode/tests/regress.sh b/usr.bin/uuencode/tests/regress.sh
index 6be0ef3cc9b0..ee6c2d606df4 100644
--- a/usr.bin/uuencode/tests/regress.sh
+++ b/usr.bin/uuencode/tests/regress.sh
@@ -1,10 +1,12 @@
 # $FreeBSD$
 
-echo 1..2
+echo 1..5
 
 REGRESSION_START($1)
 
 REGRESSION_TEST(`traditional', `uuencode regress.in <${SRCDIR}/regress.in')
 REGRESSION_TEST(`base64', `uuencode -m regress.in <${SRCDIR}/regress.in')
+REGRESSION_TEST_ONE(`uudecode -p <${SRCDIR}/regress.traditional.in', `traditional')
+REGRESSION_TEST_ONE(`uudecode -p <${SRCDIR}/regress.base64.in', `base64')
 
 REGRESSION_END()
diff --git a/usr.bin/uudecode/tests/regress.traditional.in b/usr.bin/uuencode/tests/regress.traditional.in
similarity index 100%
rename from usr.bin/uudecode/tests/regress.traditional.in
rename to usr.bin/uuencode/tests/regress.traditional.in
diff --git a/usr.bin/uudecode/uudecode.c b/usr.bin/uuencode/uudecode.c
similarity index 99%
rename from usr.bin/uudecode/uudecode.c
rename to usr.bin/uuencode/uudecode.c
index 707ba27df62e..cd97f7406cc2 100644
--- a/usr.bin/uudecode/uudecode.c
+++ b/usr.bin/uuencode/uudecode.c
@@ -68,6 +68,8 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 
+extern int main_decode(int, char *[]);
+
 static const char *infile, *outfile;
 static FILE *infp, *outfp;
 static bool base64, cflag, iflag, oflag, pflag, rflag, sflag;
@@ -79,7 +81,7 @@ static int	uu_decode(void);
 static int	base64_decode(void);
 
 int
-main(int argc, char *argv[])
+main_decode(int argc, char *argv[])
 {
 	int rval, ch;
 
diff --git a/usr.bin/uuencode/uuencode.c b/usr.bin/uuencode/uuencode.c
index b5eff44f1651..f2d4b5b2b498 100644
--- a/usr.bin/uuencode/uuencode.c
+++ b/usr.bin/uuencode/uuencode.c
@@ -63,6 +63,8 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 
+extern int main_encode(int, char *[]);
+
 static void encode(void);
 static void base64_encode(void);
 static void usage(void);
@@ -73,7 +75,7 @@ static bool raw;
 static char **av;
 
 int
-main(int argc, char *argv[])
+main_encode(int argc, char *argv[])
 {
 	struct stat sb;
 	bool base64;