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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#!/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#ident "%Z%%M% %I% %E% SMI"
#
#
# Given a header file, extract function prototypes and global variable
# declarations in a form that can be used in a mapfile. The list of extracted
# functions and variables will be combined with a user-specified template to
# create a complete mapfile.
#
# Template
# --------
#
# The template contains two sections - the prologue, and the epilogue. These
# sections are used, verbatim, as the beginning and the end of the mapfile.
# Sections begin and end with single-line comments whose sole contents are
# "/* BEGIN $section */" and "/* END $section */".
#
# Template example:
#
# /* BEGIN PROLOGUE */
# [ ... prologue goes here ... ]
# /* END PROLOGUE */
# /* BEGIN EPILOGUE */
# [ ... epilogue goes here ... ]
# /* END EPILOGUE */
#
# Selective Exportation
# ---------------------
#
# Some header files will have a public/private interface mix that is strongly
# biased towards private interfaces. That is, of the interfaces declared by
# a given header file, the majority of them are private. Only a small subset
# of interfaces are to be exported publicly. Using Selective Exportation, a
# special comment is included in the header file, declaring to this script that
# only a subset of interfaces - those with a marking declared in the comment -
# should be included in the mapfile. The marking is itself a special comment,
# whose format is declared using a directive like this:
#
# MAPFILE: export "Driver OK"
#
# Using the above directive, only those function prototypes and variable
# declarations with "/* Driver OK */" comments included in the mapfile. Note
# that the comment must be at the end of the first line. If the declaration
# spans multiple lines, the exportation comment must appear on the first line.
#
# Examples of functions selected for exportation:
#
# MAPFILE: export "Driver OK"
#
# extern int foo(int); /* Driver OK */
# extern void bar(int, int, /* Driver OK */
# int, void *);
#
# Selective Exportation may not be used in the same file as Selective Exclusion.
#
# Selective Exclusion
# -------------------
#
# Selective Exclusion is to be used in cases where the public/private interface
# mix is reversed - where public interfaces greatly outnumber the private ones.
# In this case, we want to be able to mark the private ones, thus telling this
# script that the marked interfaces are to be excluded from the mapfile.
# Marking is accomplished via a process similar to that used for Selective
# Exportation. A directive is included in a comment, and is formatted like
# this:
#
# MAPFILE: exclude "Internal"
#
# Using the above directive, function prototypes and variable declarations with
# "/* Internal */" comments would be excluded. Note that the comment must be at
# the end of the first line. If the declaration spans multiple lines, the
# exclusion comment must appear on the first line.
#
# Examples of functions excluded from exportation:
#
# MAPFILE: exclude "Internal"
#
# extern int foo(int); /* Internal */
# extern void bar(int, int, /* Internal */
# int, void *);
#
# Selective Exclusion may not be used in the same file as Selective Exportation.
#
function extract_prototypes
{
typeset header="$1"
typeset prefix="$2"
nawk -v prefix="$prefix" <$header '
/^.*MAPFILE: export \"[^\"]*\"$/ {
if (protoexclude) {
print "ERROR: export after exclude\n";
exit(1);
}
sub(/^[^\"]*\"/, "");
sub(/\"$/, "");
exportmark=sprintf("/* %s */", $0);
next;
}
/^.*MAPFILE: exclude \"[^\"]*\"$/ {
if (protomatch) {
print "ERROR: exclude after export";
exit(1);
}
sub(/^[^\"]*\"/, "");
sub(/\"$/, "");
excludemark=sprintf("/* %s */", $0);
next;
}
exportmark {
# Selective Exportation has been selected (exportmark is
# set), so exclude this line if it does not have the
# magic export mark.
if (length($0) < length(exportmark) ||
substr($0, length($0) - length(exportmark) + 1) != \
exportmark)
next;
}
excludemark {
# Selective Exclusion has been selected (excludemark is
# set), so exclude this line only if it has the magic
# exclude mark.
if (length($0) > length(excludemark) &&
substr($0, \
length($0) - length(excludemark) + 1) == \
excludemark)
next;
}
# Functions
/^extern.*\(/ {
for (i = 1; i <= NF; i++) {
if (sub(/\(.*$/, "", $i)) {
sub(/^\*/, "", $i);
if (!seenfn[$i]) {
printf("%s%s;\n", prefix, $i);
seenfn[$i] = 1;
}
break;
}
}
next;
}
# Global variables
/^extern[^\(\)]*;/ {
for (i = 1; i <= NF; i++) {
if (match($i, /;$/)) {
printf("%s%s; /* variable */\n", prefix,
substr($i, 1, length($i) - 1));
break;
}
}
next;
}
' || die "Extraction failed"
}
function extract_section
{
typeset skel="$1"
typeset secname="$2"
nawk <$skel -v name=$secname -v skel=$skel '
/\/\* [^ ]* [^ ]* \*\// && $3 == name {
if ($2 == "BEGIN") {
printing = 1;
} else {
printing = 0;
}
next;
}
printing != 0 { print; }
'
}
function die
{
echo "$PROGNAME: $@" >&2
exit 1
}
function usage
{
echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
exit 2
}
PROGNAME=$(basename "$0")
while getopts t: c ; do
case $c in
t)
mapfile_skel=$OPTARG
;;
?)
usage
esac
done
[[ -z "$mapfile_skel" ]] && usage
[[ ! -f $mapfile_skel ]] && die "Couldn't open template $tmplfile"
shift $(($OPTIND - 1))
[[ $# -lt 1 ]] && usage
for file in $@ ; do
[[ ! -f $file ]] && die "Can't open input file $file"
done
extract_section $mapfile_skel PROLOGUE
for file in $@ ; do
echo "\t\t/*"
echo "\t\t * Exported functions and variables from:"
echo "\t\t * $file"
echo "\t\t */"
extract_prototypes $file "\t\t"
echo
done
extract_section $mapfile_skel EPILOGUE
|