aboutsummaryrefslogtreecommitdiffstats
path: root/awklib/eg/prog
diff options
context:
space:
mode:
Diffstat (limited to 'awklib/eg/prog')
-rw-r--r--awklib/eg/prog/igawk.sh73
1 files changed, 38 insertions, 35 deletions
diff --git a/awklib/eg/prog/igawk.sh b/awklib/eg/prog/igawk.sh
index 7144ce50..9fce9a50 100644
--- a/awklib/eg/prog/igawk.sh
+++ b/awklib/eg/prog/igawk.sh
@@ -8,49 +8,56 @@ if [ "$1" = debug ]
then
set -x
shift
-else
- # cleanup on exit, hangup, interrupt, quit, termination
- trap 'rm -f /tmp/ig.[se].$$' 0 1 2 3 15
fi
+# A literal newline, so that program text is formmatted correctly
+n='
+'
+
+# Initialize variables to empty
+program=
+opts=
+
while [ $# -ne 0 ] # loop over arguments
do
case $1 in
--) shift; break;;
-W) shift
- set -- -W"$@"
+ # The ${x?'message here'} construct prints a
+ # diagnostic if $x is the null string
+ set -- -W"${@?'missing operand'}"
continue;;
- -[vF]) opts="$opts $1 '$2'"
+ -[vF]) opts="$opts $1 '${2?'missing operand'}'"
shift;;
-[vF]*) opts="$opts '$1'" ;;
- -f) echo @include "$2" >> /tmp/ig.s.$$
+ -f) program="$program$n@include ${2?'missing operand'}"
shift;;
- -f*) f=`echo "$1" | sed 's/-f//'`
- echo @include "$f" >> /tmp/ig.s.$$ ;;
+ -f*) f=`expr "$1" : '-f\(.*\)'`
+ program="$program$n@include $f";;
- -?file=*) # -Wfile or --file
- f=`echo "$1" | sed 's/-.file=//'`
- echo @include "$f" >> /tmp/ig.s.$$ ;;
+ -[W-]file=*)
+ f=`expr "$1" : '-.file=\(.*\)'`
+ program="$program$n@include $f";;
- -?file) # get arg, $2
- echo @include "$2" >> /tmp/ig.s.$$
+ -[W-]file)
+ program="$program$n@include ${2?'missing operand'}"
shift;;
- -?source=*) # -Wsource or --source
- t=`echo "$1" | sed 's/-.source=//'`
- echo "$t" >> /tmp/ig.s.$$ ;;
+ -[W-]source=*)
+ t=`expr "$1" : '-.source=\(.*\)'`
+ program="$program$n$t";;
- -?source) # get arg, $2
- echo "$2" >> /tmp/ig.s.$$
+ -[W-]source)
+ program="$program$n${2?'missing operand'}"
shift;;
- -?version)
- echo igawk: version 1.0 1>&2
+ -[W-]version)
+ echo igawk: version 2.0 1>&2
gawk --version
exit 0 ;;
@@ -61,21 +68,14 @@ do
shift
done
-if [ ! -s /tmp/ig.s.$$ ]
+if [ -z "$program" ]
then
- if [ -z "$1" ]
- then
- echo igawk: no program! 1>&2
- exit 1
- else
- echo "$1" > /tmp/ig.s.$$
- shift
- fi
+ program=${1?'missing program'}
+ shift
fi
-# at this point, /tmp/ig.s.$$ has the program
-gawk -- '
-# process @include directives
+# At this point, `program' has the program.
+expand_prog='
function pathto(file, i, t, junk)
{
@@ -124,7 +124,10 @@ BEGIN {
}
close(input[stackptr])
}
-}' /tmp/ig.s.$$ > /tmp/ig.e.$$
-eval gawk -f /tmp/ig.e.$$ $opts -- "$@"
+}' # close quote ends `expand_prog' variable
-exit $?
+processed_program=`gawk -- "$expand_prog" /dev/stdin <<EOF
+$program
+EOF
+`
+eval gawk $opts -- '"$processed_program"' '"$@"'