From 30e7238ad917c038105aecf4a1bd3bdefe0e33f7 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 24 Mar 2022 04:15:10 -0700 Subject: __FILE__ in main program resolves to original name. --- cppawk | 19 ++++++++++++++----- testcases | 5 +++++ testdir/name.cwk | 1 + 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 testdir/name.cwk diff --git a/cppawk b/cppawk index 876a5bb..3eab7e8 100755 --- a/cppawk +++ b/cppawk @@ -37,7 +37,6 @@ prepro_opts="-D__gawk__=1 -D__cppawk_ver=20220324" awk_file= awk_opts= tmp_file= -delhashbang="sed -e 1s/^#!.*/#/ --" prepro_only= selfdir=$(dirname "$0") @@ -70,6 +69,16 @@ die() exit 1 } +delhashbang_setline() +{ + sed -e "1s/^#!.*/#/" -e "1i#line 1 \"$1\"" "$1" +} + +delhashbang() +{ + sed -e "1s/^#!.*/#/" +} + # Eliminate tracer lines in cpp output which continue a previous line, and # merge the continued lines into one. Awk needs this because spurious newlines # break its syntax. @@ -160,24 +169,24 @@ prepro="command $(quote "$prepro")" if [ -n "$awk_file" ] ; then awk_file_dir=$(dirname "$awk_file") if [ $prepro_only ] ; then - $delhashbang "$awk_file" \ + delhashbang_setline "$awk_file" \ | eval "$prepro $incopt\"$awk_file_dir\" $prepro_opts - | collapse" else trap 'rm -f $tmp_file' EXIT INT TERM tmp_file=$(mktemp) - $delhashbang "$awk_file" \ + delhashbang_setline "$awk_file" \ | eval "$prepro $incopt\"$awk_file_dir\" $prepro_opts - | collapse" \ > $tmp_file eval "$awk $awk_opts -f $tmp_file -- \"\$@\"" fi elif [ $# -gt 0 ] ; then if [ $prepro_only ] ; then - printf "%s" "$1" | $delhashbang \ + printf "%s" "$1" | delhashbang \ | eval "$prepro $incopt"$(pwd)" $prepro_opts - | collapse" else trap 'rm -f $tmp_file' EXIT INT TERM tmp_file=$(mktemp) - printf "%s" "$1" | $delhashbang \ + printf "%s" "$1" | delhashbang \ | eval "$prepro $incopt"$(pwd)" $prepro_opts - | collapse" > $tmp_file shift eval "$awk $awk_opts -f $tmp_file -- \"\$@\"" diff --git a/testcases b/testcases index 39519a0..40933af 100644 --- a/testcases +++ b/testcases @@ -232,3 +232,8 @@ BEGIN { print __LINE__ : 2 4 +-- +41: +./cppawk -f testdir/name.cwk +: +testdir/name.cwk diff --git a/testdir/name.cwk b/testdir/name.cwk new file mode 100644 index 0000000..76613c7 --- /dev/null +++ b/testdir/name.cwk @@ -0,0 +1 @@ +BEGIN { print __FILE__ } -- cgit v1.2.3