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
|
#!/bin/sh
. ./mk.sh
export LC_ALL=POSIX
export LANG=POSIX
HEADER=$1
GUARD=__$(echo $HEADER | tr a-z/. A-Z__)__
mkdir -p $(dirname $HEADER)
shift
exec > $HEADER
if [ $(basename $HEADER) != assert.h ]; then
printf '#ifndef %s\n#define %s\n\n' ${GUARD} ${GUARD}
fi
cat <<EOF
/*
UNG's Not GNU
Copyright (c) 2011-2019, Jakob Kaivo <jkk@ung.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
EOF
rm -f $HEADER.*
for i in $@; do
# TODO: refs
echo $i >> $HEADER.$(classify_source $i)
done
#if [ POSIX ]; then
cat <<-EOF > /dev/null
#if (!defined _POSIX_C_SOURCE) && (defined _XOPEN_SOURCE)
# if (_XOPEN_SOURCE >= 700)
# define _POSIX_C_SOURCE 200809L
# elif (_XOPEN_SOURCE >= 600)
# define _POSIX_C_SOURCE 200112L
# elif (_XOPEN_SOURCE >= 500)
# define _POSIX_C_SOURCE 199506L
# else
# define _POSIX_C_SOURCE 2
# endif
#endif
#ifdef _POSIX_C_SOURCE
# define _POSIX_SOURCE
#endif
EOF
#fi
if [ -f $HEADER.MACRO ]; then
# FIXME: line continuation
for i in $(sort $HEADER.MACRO); do
grep -E '^#(if|def|undef|el|end)' $i
done
printf '\n'
fi
if [ -f $HEADER.TYPE ]; then
for i in $(sort $HEADER.TYPE); do
if grep -q '^#ifdef' $i; then
sed -ne '/#ifdef/,/#endif/p' $i
elif grep -q '^typedef.*;$' $i; then
grep '^typedef' $i
else
sed -ne '/^typedef/,/\}.*;$/p' $i
fi
done
printf '\n'
rm $HEADER.TYPE
fi
if [ -f $HEADER.TYPE_LONG ]; then
for i in $(sort $HEADER.TYPE_LONG); do
if grep -q '^#ifdef' $i; then
sed -ne '/#ifdef/,/#endif/p' $i
elif grep -q '^typedef.*;$' $i; then
grep '^typedef' $i
else
sed -ne '/^typedef/,/\}.*;$/p' $i
fi
done
printf '\n'
rm $HEADER.TYPE_LONG
fi
if [ -f $HEADER.STRUCT -o -f $HEADER.UNION ]; then
touch $HEADER.STRUCT $HEADER.UNION
for i in $(sort $HEADER.STRUCT $HEADER.UNION 2>/dev/null); do
if grep -q '^struct' $i; then
sed -ne '/^struct/,/\};/p' $i
else
sed -ne '/^union/,/\};/p' $i
fi
done
printf '\n'
rm -f $HEADER.STRUCT $HEADER.UNION
fi
if [ -f $HEADER.EXTERN ]; then
for i in $(sort $HEADER.EXTERN); do
printf 'extern %s' "$(grep '^[a-zA-Z_].*;$' $i)"
done
printf '\n'
rm -f $HEADER.EXTERN
fi
if [ -f $HEADER.TGFN ]; then
for i in $(sort $HEADER.TGFN); do
printf '%s;\n' "$(sed -e "/{/q" $i | tail -n2 | head -n1 | m4 '-DTGFN=$1' -DTYPE=double)"
done
printf '\n'
rm -f $HEADER.TGFN
fi
if [ -f $HEADER.FUNCTION ]; then
if grep -q restrict $(cat $HEADER.FUNCTION); then
printf '#if (!defined __STDC_VERSION__) || (__STDC_VERSION__ < 199909L)\n'
printf '#define restrict\n'
printf '#endif\n\n'
fi
if grep -q _Noreturn $(cat $HEADER.FUNCTION); then
printf '#if (!defined __STDC_VERSION__) || (__STDC_VERSION__ < 200112L)\n'
printf '#define _Noreturn\n'
printf '#endif\n\n'
fi
for i in $(sort $HEADER.FUNCTION); do
printf '%s;\n' "$(sed -e "/{/q" $i | tail -n2 | head -n1 | sed -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)\([,)]\)/__\1\2/g;s/(__\([a-zA-Z_][a-zA-Z_0-9]*\))/(\1)/g')"
done
printf '\n'
rm -f $HEADER.FUNCTION
fi
if [ -f $HEADER.MACRO ]; then
for i in $(sort $HEADER.MACRO); do
grep ' *extern.*;$' $i
done
printf '\n'
rm $HEADER.MACRO
fi
rm -f $HEADER.REFERENCE
if [ $(basename $HEADER) != assert.h ]; then
printf '\n#endif\n'
fi
|