Module Name:    othersrc
Committed By:   agc
Date:           Mon Jan 21 07:30:48 UTC 2019

Added Files:
        othersrc/external/bsd/file2c: Makefile file2c.1 file2c.sh

Log Message:
Add a new utility called file2c(1) to othersrc, which converts its
input stream into individual bytes, suitable for inclusion as a C
source file.  This kind of utility is occasionally useful for
embedding data in C-derived libraries and executables.  The utility is
similar in function, and arguments, to the utility written in C, and
included in FreeBSD.  This version of the utility just uses awk(1),
and so is written as a simple shell script.

Example usage:

        % echo 'Hello world' | file2c -sx 'static const char *text = {\n' '};\n'
        static const char *text = {
                0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
                0x64, 0xa
        };
        %


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 othersrc/external/bsd/file2c/Makefile \
    othersrc/external/bsd/file2c/file2c.1 \
    othersrc/external/bsd/file2c/file2c.sh

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: othersrc/external/bsd/file2c/Makefile
diff -u /dev/null othersrc/external/bsd/file2c/Makefile:1.1
--- /dev/null	Mon Jan 21 07:30:48 2019
+++ othersrc/external/bsd/file2c/Makefile	Mon Jan 21 07:30:48 2019
@@ -0,0 +1,23 @@
+# $NetBSD: Makefile,v 1.1 2019/01/21 07:30:48 agc Exp $
+
+PROG=		file2c
+SRCS+=		file2c.sh
+MAN=		file2c.1
+
+SCRIPTSDIR=	/usr/sbin
+
+.include <bsd.prog.mk>
+
+t: ${PROG}
+	@echo "1. basics"
+	./${PROG} -sx 'static const char *text = {\n' '};\n' < 1.in > 1.out
+	diff 1.expected 1.out
+	rm -f 1.out
+	@echo "2. decimal"
+	./${PROG} -s 'static const char *text = {\n' '};\n' < 1.in > 2.out
+	diff 2.expected 2.out
+	rm -f 2.out
+	@echo "3. no space, decimal"
+	./${PROG} 'static const char *text = {\n' '};\n' < 1.in > 3.out
+	diff 3.expected 3.out
+	rm -f 3.out
Index: othersrc/external/bsd/file2c/file2c.1
diff -u /dev/null othersrc/external/bsd/file2c/file2c.1:1.1
--- /dev/null	Mon Jan 21 07:30:48 2019
+++ othersrc/external/bsd/file2c/file2c.1	Mon Jan 21 07:30:48 2019
@@ -0,0 +1,88 @@
+.\" $NetBSD: file2c.1,v 1.1 2019/01/21 07:30:48 agc Exp $
+.\"
+.\" Copyright (c) 2019 Alistair Crooks <a...@netbsd.org>
+.\" All rights reserved.
+.\"
+.\" 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.
+.\"
+.Dd January 20, 2019
+.Dt FILE2C 1
+.Os
+.Sh NAME
+.Nm file2c
+.Nd script to convert input to a C language character array
+.Sh SYNOPSIS
+.Nm
+.Op Fl sxv
+.Op Header\-text
+.Op Footer\-text
+.Sh DESCRIPTION
+The
+.Nm
+script converts its standard input into a form suitable for compilation
+as part of a C language source file.
+The output format can have leading spaces.
+The output format can be displayed as hexadecimal or decimal numbers.
+If a header is desired, it should be given as the first argument
+after any options.
+Similarily, if a footer is desired, it should be given after its corresponding
+header argument.
+.Sh OPTIONS
+The following options are available:
+.Bl -tag -width inits
+.It Fl s
+Prefix the output lines with a leading tab character.
+.It Fl x
+Display the output characters as hexadecimal byte constants.
+.It Fl v
+Perform the operations in a verbose manner.
+.El
+.Pp
+Internally, the
+.Nm
+utility uses the
+.Xr awk 1
+utility to transform the input into output.
+.Sh RETURN VALUES
+The
+.Nm
+utility will return 0 for success,
+and non-zero for failure.
+.Sh EXAMPLES
+.Bd -literal
+% echo 'Hello world' | file2c -sx 'static const char *text = {\en' '};\en'
+static const char *text = {
+        0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
+        0x64, 0xa
+};
+.Ed
+.Sh SEE ALSO
+.Xr awk 1
+.Sh HISTORY
+The
+.Nm
+program was first seen in
+.Nx 9 .
+.Sh AUTHORS
+The
+.Nm
+script was written by
+.An Alistair Crooks Aq Mt a...@netbsd.org .
Index: othersrc/external/bsd/file2c/file2c.sh
diff -u /dev/null othersrc/external/bsd/file2c/file2c.sh:1.1
--- /dev/null	Mon Jan 21 07:30:48 2019
+++ othersrc/external/bsd/file2c/file2c.sh	Mon Jan 21 07:30:48 2019
@@ -0,0 +1,102 @@
+#! /bin/sh
+
+# $NetBSD: file2c.sh,v 1.1 2019/01/21 07:30:48 agc Exp $
+
+# Copyright (c) 2019 Alistair Crooks <a...@netbsd.org>
+# All rights reserved.
+#
+# 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.
+#
+
+# small awk script to do same as file2c(1)
+# input is taken from stdin
+# can have hex or decimal characters in the array
+# can have preamble and postamble via command line args
+
+pre=""
+post=""
+space=0
+hex=0
+while getopts "sxv" arg; do
+	case "${arg}" in
+	s)	space=1 ;;
+	x)	hex=1 ;;
+	v)	set -x ;;
+	\?)	printf"Usage: $0 [-s] [-x] [-v] [header] [footer]" >&2
+		exit 1 ;;
+	esac
+done
+shift $(( OPTIND - 1 ))
+
+if [ $# -gt 0 ]; then
+	pre="$1"
+	shift
+fi
+if [ $# -gt 0 ]; then
+	post="$1"
+	shift
+fi
+
+awk -vpre="${pre}" -vpost="${post}" -vspace=${space} -vhex=${hex} '
+BEGIN {
+	if (pre) {
+		printf("%s", pre);
+	}
+	off = 0;
+	for (n = 0;n < 256 ; n++) {
+		ord[sprintf("%c", n)] = n
+	}
+	if (hex) {
+		format = "0x%x"
+		linelen = 10
+	} else {
+		format = "%d"
+		linelen = 14
+	}
+	if (space) {
+		printf("\t");
+	}
+}
+{
+	sub("$", "\n");
+	len = length($0);
+	for (i = 1 ; i <= len ; i++) {
+		if (off > 0) {
+			printf(", ");
+			if (off % linelen == 0) {
+				printf("\n");
+				if (space) {
+					printf("\t");
+				}
+			}
+		}
+		printf(format, ord[substr($0, i, 1)]);
+		off += 1;
+	}
+}
+END {
+	printf("\n");
+	if (post) {
+		printf("%s", post);
+	}
+}
+'
+exit 0

Reply via email to