-
Notifications
You must be signed in to change notification settings - Fork 4
/
includer
executable file
·174 lines (160 loc) · 2.59 KB
/
includer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
PROGNAME="$0"
usage() {
cat <<EOF
NAME
`basename $PROGNAME` - CPP replacement
SYNOPSIS
`basename $PROGNAME` [options] file
DESCRIPTION
CPP replacement.
Handles:
#include "file"
#ifdef symbol
#ifndef symbol
#endif
OPTIONS
-t type Type is sh, html, man
-v DEFn=symbol Symbol is defined, n = 1, ... 9.
i.e. -v DEF1=groff is equivalent to:
#define groff 1
-D lvl Debug level
EOF
exit 1
}
#
# Report an error and exit
#
error() {
echo "`basename $PROGNAME`: $1" >&2
exit 1
}
debug() {
if [ $DEBUG -ge $1 ]; then
echo "`basename $PROGNAME`: $2" >&2
fi
}
#
# Process the options
#
DEBUG=0
VARS=""
TYPE=man
while getopts "t:v:D:h?" opt
do
case $opt in
t) TYPE="$OPTARG";;
v) VARS="$VARS -v $OPTARG";;
D) DEBUG="$OPTARG";;
h|\?) usage;;
esac
done
shift `expr $OPTIND - 1`
#
# Main Program
#
if [ -x /opt/sfw/bin/gawk ]; then
AWK=/opt/sfw/bin/gawk
else
AWK=awk
fi
case "$TYPE" in
sh)
;;
man)
;;
html)
;;
*)
error "Wrong type '$TYPE'; should be sh, man, or html"
;;
esac
$AWK -v TYPE="$TYPE" $VARS '
function do1(file, i) {
if (nfiles++ != 0) nrec = nr+1
else nrec = 0
while ((getline < file) > 0)
{
++nrec
if (nrec == nr)
{
print comment_begin
if (!comment_blank) print comment
print comment " DO NOT EDIT! This file is generated from " file
print comment_end
}
split($0, a)
if (a[1] == "#include")
{
gsub(/"/, "", a[2])
if (comment_blank) print ""
do1(a[2])
}
else if (a[1] == "#define")
{
def[a[2]] = 1
print comment
}
else if (a[1] == "#ifdef")
{
if (!def[a[2]])
skip = 1
print comment
}
else if (a[1] == "#ifndef")
{
if (def[a[2]])
skip = 1
print comment
}
else if (a[1] == "#endif")
{
skip = 0
print comment
}
else if (skip)
print comment
else
print $0
}
close(file)
}
BEGIN {
if (TYPE == "sh")
{
nr = 2
comment_begin = "#"
comment = "#"
comment_end = "#"
comment_blank = 0
}
else if (TYPE == "man")
{
nr = 1
comment_begin = "'"'"'\\\" t"
comment = "'"'"'\\\""
comment_end = "'"'"'\\\""
comment_blank = 0
}
else if (TYPE == "html")
{
nr = 1
comment_begin = "<!--"
comment = ""
comment_end = "-->"
comment_blank = 1
}
if (DEF1) def[DEF1] = 1;
if (DEF2) def[DEF2] = 1;
if (DEF3) def[DEF3] = 1;
if (DEF4) def[DEF4] = 1;
if (DEF5) def[DEF5] = 1;
if (DEF6) def[DEF6] = 1;
if (DEF7) def[DEF7] = 1;
if (DEF8) def[DEF8] = 1;
if (DEF9) def[DEF9] = 1;
#for (i in def)
#print i, def[i]
do1(ARGV[1])
exit
}' $*