1 | #! /bin/sh
|
---|
2 | # igawk --- like gawk but do @include processing
|
---|
3 | #
|
---|
4 | # Arnold Robbins, arnold@skeeve.com, Public Domain
|
---|
5 | # July 1993
|
---|
6 |
|
---|
7 | if [ "$1" = debug ]
|
---|
8 | then
|
---|
9 | set -x
|
---|
10 | shift
|
---|
11 | fi
|
---|
12 |
|
---|
13 | # A literal newline, so that program text is formatted correctly
|
---|
14 | n='
|
---|
15 | '
|
---|
16 |
|
---|
17 | # Initialize variables to empty
|
---|
18 | program=
|
---|
19 | opts=
|
---|
20 |
|
---|
21 | while [ $# -ne 0 ] # loop over arguments
|
---|
22 | do
|
---|
23 | case $1 in
|
---|
24 | --) shift; break;;
|
---|
25 |
|
---|
26 | -W) shift
|
---|
27 | # The ${x?'message here'} construct prints a
|
---|
28 | # diagnostic if $x is the null string
|
---|
29 | set -- -W"${@?'missing operand'}"
|
---|
30 | continue;;
|
---|
31 |
|
---|
32 | -[vF]) opts="$opts $1 '${2?'missing operand'}'"
|
---|
33 | shift;;
|
---|
34 |
|
---|
35 | -[vF]*) opts="$opts '$1'" ;;
|
---|
36 |
|
---|
37 | -f) program="$program$n@include ${2?'missing operand'}"
|
---|
38 | shift;;
|
---|
39 |
|
---|
40 | -f*) f=`expr "$1" : '-f\(.*\)'`
|
---|
41 | program="$program$n@include $f";;
|
---|
42 |
|
---|
43 | -[W-]file=*)
|
---|
44 | f=`expr "$1" : '-.file=\(.*\)'`
|
---|
45 | program="$program$n@include $f";;
|
---|
46 |
|
---|
47 | -[W-]file)
|
---|
48 | program="$program$n@include ${2?'missing operand'}"
|
---|
49 | shift;;
|
---|
50 |
|
---|
51 | -[W-]source=*)
|
---|
52 | t=`expr "$1" : '-.source=\(.*\)'`
|
---|
53 | program="$program$n$t";;
|
---|
54 |
|
---|
55 | -[W-]source)
|
---|
56 | program="$program$n${2?'missing operand'}"
|
---|
57 | shift;;
|
---|
58 |
|
---|
59 | -[W-]version)
|
---|
60 | echo igawk: version 2.0 1>&2
|
---|
61 | gawk --version
|
---|
62 | exit 0 ;;
|
---|
63 |
|
---|
64 | -[W-]*) opts="$opts '$1'" ;;
|
---|
65 |
|
---|
66 | *) break;;
|
---|
67 | esac
|
---|
68 | shift
|
---|
69 | done
|
---|
70 |
|
---|
71 | if [ -z "$program" ]
|
---|
72 | then
|
---|
73 | program=${1?'missing program'}
|
---|
74 | shift
|
---|
75 | fi
|
---|
76 |
|
---|
77 | # At this point, `program' has the program.
|
---|
78 | expand_prog='
|
---|
79 |
|
---|
80 | function pathto(file, i, t, junk)
|
---|
81 | {
|
---|
82 | if (index(file, "/") != 0)
|
---|
83 | return file
|
---|
84 |
|
---|
85 | for (i = 1; i <= ndirs; i++) {
|
---|
86 | t = (pathlist[i] "/" file)
|
---|
87 | if ((getline junk < t) > 0) {
|
---|
88 | # found it
|
---|
89 | close(t)
|
---|
90 | return t
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return ""
|
---|
94 | }
|
---|
95 | BEGIN {
|
---|
96 | path = ENVIRON["AWKPATH"]
|
---|
97 | ndirs = split(path, pathlist, ":")
|
---|
98 | for (i = 1; i <= ndirs; i++) {
|
---|
99 | if (pathlist[i] == "")
|
---|
100 | pathlist[i] = "."
|
---|
101 | }
|
---|
102 | stackptr = 0
|
---|
103 | input[stackptr] = ARGV[1] # ARGV[1] is first file
|
---|
104 |
|
---|
105 | for (; stackptr >= 0; stackptr--) {
|
---|
106 | while ((getline < input[stackptr]) > 0) {
|
---|
107 | if (tolower($1) != "@include") {
|
---|
108 | print
|
---|
109 | continue
|
---|
110 | }
|
---|
111 | fpath = pathto($2)
|
---|
112 | if (fpath == "") {
|
---|
113 | printf("igawk:%s:%d: cannot find %s\n",
|
---|
114 | input[stackptr], FNR, $2) > "/dev/stderr"
|
---|
115 | continue
|
---|
116 | }
|
---|
117 | if (! (fpath in processed)) {
|
---|
118 | processed[fpath] = input[stackptr]
|
---|
119 | input[++stackptr] = fpath # push onto stack
|
---|
120 | } else
|
---|
121 | print $2, "included in", input[stackptr],
|
---|
122 | "already included in",
|
---|
123 | processed[fpath] > "/dev/stderr"
|
---|
124 | }
|
---|
125 | close(input[stackptr])
|
---|
126 | }
|
---|
127 | }' # close quote ends `expand_prog' variable
|
---|
128 |
|
---|
129 | processed_program=`gawk -- "$expand_prog" /dev/stdin <<EOF
|
---|
130 | $program
|
---|
131 | EOF
|
---|
132 | `
|
---|
133 | eval gawk $opts -- '"$processed_program"' '"$@"'
|
---|