xref: /onnv-gate/usr/src/cmd/mdb/tools/scripts/hdr2map.sh (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/bin/ksh
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate# Use is subject to license terms.
26*0Sstevel@tonic-gate#
27*0Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate#
31*0Sstevel@tonic-gate# Given a header file, extract function prototypes and global variable
32*0Sstevel@tonic-gate# declarations in a form that can be used in a mapfile.  The list of extracted
33*0Sstevel@tonic-gate# functions and variables will be combined with a user-specified template to
34*0Sstevel@tonic-gate# create a complete mapfile.
35*0Sstevel@tonic-gate#
36*0Sstevel@tonic-gate# Template
37*0Sstevel@tonic-gate# --------
38*0Sstevel@tonic-gate#
39*0Sstevel@tonic-gate# The template contains two sections - the prologue, and the epilogue.  These
40*0Sstevel@tonic-gate# sections are used, verbatim, as the beginning and the end of the mapfile.
41*0Sstevel@tonic-gate# Sections begin and end with single-line comments whose sole contents are
42*0Sstevel@tonic-gate# "/* BEGIN $section */" and "/* END $section */".
43*0Sstevel@tonic-gate#
44*0Sstevel@tonic-gate# Template example:
45*0Sstevel@tonic-gate#
46*0Sstevel@tonic-gate# /* BEGIN PROLOGUE */
47*0Sstevel@tonic-gate# [ ... prologue goes here ... ]
48*0Sstevel@tonic-gate# /* END PROLOGUE */
49*0Sstevel@tonic-gate# /* BEGIN EPILOGUE */
50*0Sstevel@tonic-gate# [ ... epilogue goes here ... ]
51*0Sstevel@tonic-gate# /* END EPILOGUE */
52*0Sstevel@tonic-gate#
53*0Sstevel@tonic-gate# Selective Exportation
54*0Sstevel@tonic-gate# ---------------------
55*0Sstevel@tonic-gate#
56*0Sstevel@tonic-gate# Some header files will have a public/private interface mix that is strongly
57*0Sstevel@tonic-gate# biased towards private interfaces.  That is, of the interfaces declared by
58*0Sstevel@tonic-gate# a given header file, the majority of them are private.  Only a small subset
59*0Sstevel@tonic-gate# of interfaces are to be exported publicly.  Using Selective Exportation, a
60*0Sstevel@tonic-gate# special comment is included in the header file, declaring to this script that
61*0Sstevel@tonic-gate# only a subset of interfaces - those with a marking declared in the comment -
62*0Sstevel@tonic-gate# should be included in the mapfile.  The marking is itself a special comment,
63*0Sstevel@tonic-gate# whose format is declared using a directive like this:
64*0Sstevel@tonic-gate#
65*0Sstevel@tonic-gate# 	MAPFILE: export "Driver OK"
66*0Sstevel@tonic-gate#
67*0Sstevel@tonic-gate# Using the above directive, only those function prototypes and variable
68*0Sstevel@tonic-gate# declarations with "/* Driver OK */" comments included in the mapfile.  Note
69*0Sstevel@tonic-gate# that the comment must be at the end of the first line.  If the declaration
70*0Sstevel@tonic-gate# spans multiple lines, the exportation comment must appear on the first line.
71*0Sstevel@tonic-gate#
72*0Sstevel@tonic-gate# Examples of functions selected for exportation:
73*0Sstevel@tonic-gate#
74*0Sstevel@tonic-gate# MAPFILE: export "Driver OK"
75*0Sstevel@tonic-gate#
76*0Sstevel@tonic-gate# extern int foo(int);		/* Driver OK */
77*0Sstevel@tonic-gate# extern void bar(int, int,	/* Driver OK */
78*0Sstevel@tonic-gate#     int, void *);
79*0Sstevel@tonic-gate#
80*0Sstevel@tonic-gate# Selective Exportation may not be used in the same file as Selective Exclusion.
81*0Sstevel@tonic-gate#
82*0Sstevel@tonic-gate# Selective Exclusion
83*0Sstevel@tonic-gate# -------------------
84*0Sstevel@tonic-gate#
85*0Sstevel@tonic-gate# Selective Exclusion is to be used in cases where the public/private interface
86*0Sstevel@tonic-gate# mix is reversed - where public interfaces greatly outnumber the private ones.
87*0Sstevel@tonic-gate# In this case, we want to be able to mark the private ones, thus telling this
88*0Sstevel@tonic-gate# script that the marked interfaces are to be excluded from the mapfile.
89*0Sstevel@tonic-gate# Marking is accomplished via a process similar to that used for Selective
90*0Sstevel@tonic-gate# Exportation.  A directive is included in a comment, and is formatted like
91*0Sstevel@tonic-gate# this:
92*0Sstevel@tonic-gate#
93*0Sstevel@tonic-gate#	MAPFILE: exclude "Internal"
94*0Sstevel@tonic-gate#
95*0Sstevel@tonic-gate# Using the above directive, function prototypes and variable declarations with
96*0Sstevel@tonic-gate# "/* Internal */" comments would be excluded.  Note that the comment must be at
97*0Sstevel@tonic-gate# the end of the first line.  If the declaration spans multiple lines, the
98*0Sstevel@tonic-gate# exclusion comment must appear on the first line.
99*0Sstevel@tonic-gate#
100*0Sstevel@tonic-gate# Examples of functions excluded from exportation:
101*0Sstevel@tonic-gate#
102*0Sstevel@tonic-gate# MAPFILE: exclude "Internal"
103*0Sstevel@tonic-gate#
104*0Sstevel@tonic-gate# extern int foo(int);		/* Internal */
105*0Sstevel@tonic-gate# extern void bar(int, int,	/* Internal */
106*0Sstevel@tonic-gate#	int, void *);
107*0Sstevel@tonic-gate#
108*0Sstevel@tonic-gate# Selective Exclusion may not be used in the same file as Selective Exportation.
109*0Sstevel@tonic-gate#
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gatefunction extract_prototypes
112*0Sstevel@tonic-gate{
113*0Sstevel@tonic-gate	typeset header="$1"
114*0Sstevel@tonic-gate	typeset prefix="$2"
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate	nawk -v prefix="$prefix" <$header '
117*0Sstevel@tonic-gate		/^.*MAPFILE: export \"[^\"]*\"$/ {
118*0Sstevel@tonic-gate			if (protoexclude) {
119*0Sstevel@tonic-gate				print "ERROR: export after exclude\n";
120*0Sstevel@tonic-gate				exit(1);
121*0Sstevel@tonic-gate			}
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
124*0Sstevel@tonic-gate			sub(/\"$/, "");
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate			exportmark=sprintf("/* %s */", $0);
127*0Sstevel@tonic-gate			next;
128*0Sstevel@tonic-gate		}
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate		/^.*MAPFILE: exclude \"[^\"]*\"$/ {
131*0Sstevel@tonic-gate			if (protomatch) {
132*0Sstevel@tonic-gate				print "ERROR: exclude after export";
133*0Sstevel@tonic-gate				exit(1);
134*0Sstevel@tonic-gate			}
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
137*0Sstevel@tonic-gate			sub(/\"$/, "");
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate			excludemark=sprintf("/* %s */", $0);
140*0Sstevel@tonic-gate			next;
141*0Sstevel@tonic-gate		}
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate		exportmark {
144*0Sstevel@tonic-gate			# Selective Exportation has been selected (exportmark is
145*0Sstevel@tonic-gate			# set), so exclude this line if it does not have the
146*0Sstevel@tonic-gate			# magic export mark.
147*0Sstevel@tonic-gate			if (length($0) < length(exportmark) ||
148*0Sstevel@tonic-gate			    substr($0, length($0) - length(exportmark) + 1) != \
149*0Sstevel@tonic-gate			    exportmark)
150*0Sstevel@tonic-gate				next;
151*0Sstevel@tonic-gate		}
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate		excludemark {
154*0Sstevel@tonic-gate			# Selective Exclusion has been selected (excludemark is
155*0Sstevel@tonic-gate			# set), so exclude this line only if it has the magic
156*0Sstevel@tonic-gate			# exclude mark.
157*0Sstevel@tonic-gate			if (length($0) > length(excludemark) &&
158*0Sstevel@tonic-gate			    substr($0, \
159*0Sstevel@tonic-gate			    length($0) - length(excludemark) + 1) == \
160*0Sstevel@tonic-gate			    excludemark)
161*0Sstevel@tonic-gate				next;
162*0Sstevel@tonic-gate		}
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate		# Functions
165*0Sstevel@tonic-gate		/^extern.*\(/ {
166*0Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
167*0Sstevel@tonic-gate				if (sub(/\(.*$/, "", $i)) {
168*0Sstevel@tonic-gate					sub(/^\*/, "", $i);
169*0Sstevel@tonic-gate					if (!seenfn[$i]) {
170*0Sstevel@tonic-gate						printf("%s%s;\n", prefix, $i);
171*0Sstevel@tonic-gate						seenfn[$i] = 1;
172*0Sstevel@tonic-gate					}
173*0Sstevel@tonic-gate					break;
174*0Sstevel@tonic-gate				}
175*0Sstevel@tonic-gate			}
176*0Sstevel@tonic-gate			next;
177*0Sstevel@tonic-gate		}
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate		# Global variables
180*0Sstevel@tonic-gate		/^extern[^\(\)]*;/ {
181*0Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
182*0Sstevel@tonic-gate				if (match($i, /;$/)) {
183*0Sstevel@tonic-gate					printf("%s%s;\n", prefix,
184*0Sstevel@tonic-gate					    substr($i, 1, length($i) - 1));
185*0Sstevel@tonic-gate					break;
186*0Sstevel@tonic-gate				}
187*0Sstevel@tonic-gate			}
188*0Sstevel@tonic-gate			next;
189*0Sstevel@tonic-gate		}
190*0Sstevel@tonic-gate	' || die "Extraction failed"
191*0Sstevel@tonic-gate}
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gatefunction extract_section
194*0Sstevel@tonic-gate{
195*0Sstevel@tonic-gate	typeset skel="$1"
196*0Sstevel@tonic-gate	typeset secname="$2"
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate	nawk <$skel -v name=$secname -v skel=$skel '
199*0Sstevel@tonic-gate	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
200*0Sstevel@tonic-gate		if ($2 == "BEGIN") {
201*0Sstevel@tonic-gate			printing = 1;
202*0Sstevel@tonic-gate		} else {
203*0Sstevel@tonic-gate			printing = 0;
204*0Sstevel@tonic-gate		}
205*0Sstevel@tonic-gate		next;
206*0Sstevel@tonic-gate	    }
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate	    printing != 0 { print; }
209*0Sstevel@tonic-gate	'
210*0Sstevel@tonic-gate}
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gatefunction die
213*0Sstevel@tonic-gate{
214*0Sstevel@tonic-gate	echo "$PROGNAME: $@" >&2
215*0Sstevel@tonic-gate	exit 1
216*0Sstevel@tonic-gate}
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gatefunction usage
219*0Sstevel@tonic-gate{
220*0Sstevel@tonic-gate	echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
221*0Sstevel@tonic-gate	exit 2
222*0Sstevel@tonic-gate}
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gatePROGNAME=$(basename "$0")
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gatewhile getopts t: c ; do
227*0Sstevel@tonic-gate	case $c in
228*0Sstevel@tonic-gate	    t)
229*0Sstevel@tonic-gate		mapfile_skel=$OPTARG
230*0Sstevel@tonic-gate		;;
231*0Sstevel@tonic-gate	    ?)
232*0Sstevel@tonic-gate		usage
233*0Sstevel@tonic-gate	esac
234*0Sstevel@tonic-gatedone
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate[[ -z "$mapfile_skel" ]] && usage
237*0Sstevel@tonic-gate[[ ! -f $mapfile_skel ]] && die "Couldn't open template $tmplfile"
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gateshift $(($OPTIND - 1))
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate[[ $# -lt 1 ]] && usage
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gatefor file in $@ ; do
244*0Sstevel@tonic-gate	[[ ! -f $file ]] && die "Can't open input file $file"
245*0Sstevel@tonic-gatedone
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gateextract_section $mapfile_skel PROLOGUE
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gatefor file in $@ ; do
250*0Sstevel@tonic-gate	echo "\t\t/*"
251*0Sstevel@tonic-gate	echo "\t\t * Exported functions and variables from:"
252*0Sstevel@tonic-gate	echo "\t\t *  $file"
253*0Sstevel@tonic-gate	echo "\t\t */"
254*0Sstevel@tonic-gate	extract_prototypes $file "\t\t"
255*0Sstevel@tonic-gate	echo
256*0Sstevel@tonic-gatedone
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gateextract_section $mapfile_skel EPILOGUE
259