Debian Codegraph @

awklib/eg/prog/igawk.sh

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