aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-03-17 20:03:23 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-03-17 20:03:23 -0700
commit4171b13bd8659819badea73d338ab194c1a08d1f (patch)
tree3405a6de27fd126d2d1885ee06ad5cd67c7158df
parent519ae83f84e62c4b341e8e1dc20161481aae2e60 (diff)
downloadcppawk-4171b13bd8659819badea73d338ab194c1a08d1f.tar.gz
cppawk-4171b13bd8659819badea73d338ab194c1a08d1f.tar.bz2
cppawk-4171b13bd8659819badea73d338ab194c1a08d1f.zip
First working version.
-rwxr-xr-xcppawk118
1 files changed, 118 insertions, 0 deletions
diff --git a/cppawk b/cppawk
new file mode 100755
index 0000000..cdff59e
--- /dev/null
+++ b/cppawk
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+# cppawk: C preprocessor wrapper around awk
+# Kaz Kylheku <kaz@kylheku.com>
+#
+# BSD-2 License
+#
+# 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+
+# site configuration
+prepro=/usr/bin/cpp
+incopt=-iquote # GNU cpp feature: use -I if unavailable
+awk=/usr/bin/gawk
+
+# globals
+awk_file=
+awk_opts=
+prepro_opts=
+tmp_file=
+
+# functions
+shell_escape()
+{
+ case $1 in
+ *"'"* )
+ case $1 in
+ *'"'* | *'$'* )
+ printf "%s" "'$(printf "%s" "$1" | sed -e "s/'/'\\\\''/g")'"
+ ;;
+ * )
+ printf "%s" "\"$1\""
+ ;;
+ esac
+ ;;
+ *'"'* | *['$*?[(){};&|<>#']* | '~'* )
+ printf "%s" "'$1'"
+ ;;
+ *' '* | *' '* )
+ printf "%s" "\"$1\""
+ ;;
+ * )
+ printf "%s" "$1"
+ ;;
+ esac
+}
+
+die()
+{
+ fmt="$0: $1\n" ; shift
+ printf "$fmt" "$@"
+ exit 1
+}
+
+while [ $# -gt 0 ] ; do
+ case $1 in
+ -M* )
+ die "-M family of cpp options not supported"
+ ;;
+ -U* | -D* | -iquote* )
+ prepro_opts="$prepro_opts $(shell_escape "$1")"
+ ;;
+ -f )
+ [ $# -gt 1 ] || die "-f requires argument"
+ [ -z "$awk_file" ] || die "-f can be only given once"
+ awk_file=$2
+ shift
+ ;;
+ -F | -v | -E | -i | -l | -L )
+ [ $# -gt 1 ] || die "%s requires argument" $1
+ awk_opts="$awk_opts $1 $(shell_escape "$2")"
+ shift
+ ;;
+ -- )
+ break
+ ;;
+ -* )
+ awk_opts="$awk_opts $(shell_escape "$1")"
+ ;;
+ * )
+ break
+ ;;
+ esac
+ shift
+done
+
+trap 'rm -f $tmp_file' EXIT INT TERM
+
+if [ -n "$awk_file" ] ; then
+ tmp_file=$(mktemp)
+ $prepro $prepro_opts "$awk_file" > $tmp_file
+ $awk $awk_opts -f $tmp_file "$@"
+elif [ $# -gt 0 ] ; then
+ tmp_file=$(mktemp)
+ printf "%s" "$1" | $prepro $incopt"$(pwd)" $prepro_opts - > $tmp_file; shift
+ $awk $awk_opts -f $tmp_file "$@"
+else
+ die "awk code must be specified"
+fi