xref: /onnv-gate/usr/src/cmd/mdb/tools/scripts/hdr2map.sh (revision 1605:9633a802c459)
10Sstevel@tonic-gate#!/bin/ksh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*1605Spetede# Common Development and Distribution License (the "License").
7*1605Spetede# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
23*1605Spetede# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
260Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate#
280Sstevel@tonic-gate
290Sstevel@tonic-gate#
300Sstevel@tonic-gate# Given a header file, extract function prototypes and global variable
310Sstevel@tonic-gate# declarations in a form that can be used in a mapfile.  The list of extracted
320Sstevel@tonic-gate# functions and variables will be combined with a user-specified template to
330Sstevel@tonic-gate# create a complete mapfile.
340Sstevel@tonic-gate#
350Sstevel@tonic-gate# Template
360Sstevel@tonic-gate# --------
370Sstevel@tonic-gate#
380Sstevel@tonic-gate# The template contains two sections - the prologue, and the epilogue.  These
390Sstevel@tonic-gate# sections are used, verbatim, as the beginning and the end of the mapfile.
400Sstevel@tonic-gate# Sections begin and end with single-line comments whose sole contents are
410Sstevel@tonic-gate# "/* BEGIN $section */" and "/* END $section */".
420Sstevel@tonic-gate#
430Sstevel@tonic-gate# Template example:
440Sstevel@tonic-gate#
450Sstevel@tonic-gate# /* BEGIN PROLOGUE */
460Sstevel@tonic-gate# [ ... prologue goes here ... ]
470Sstevel@tonic-gate# /* END PROLOGUE */
480Sstevel@tonic-gate# /* BEGIN EPILOGUE */
490Sstevel@tonic-gate# [ ... epilogue goes here ... ]
500Sstevel@tonic-gate# /* END EPILOGUE */
510Sstevel@tonic-gate#
520Sstevel@tonic-gate# Selective Exportation
530Sstevel@tonic-gate# ---------------------
540Sstevel@tonic-gate#
550Sstevel@tonic-gate# Some header files will have a public/private interface mix that is strongly
560Sstevel@tonic-gate# biased towards private interfaces.  That is, of the interfaces declared by
570Sstevel@tonic-gate# a given header file, the majority of them are private.  Only a small subset
580Sstevel@tonic-gate# of interfaces are to be exported publicly.  Using Selective Exportation, a
590Sstevel@tonic-gate# special comment is included in the header file, declaring to this script that
600Sstevel@tonic-gate# only a subset of interfaces - those with a marking declared in the comment -
610Sstevel@tonic-gate# should be included in the mapfile.  The marking is itself a special comment,
620Sstevel@tonic-gate# whose format is declared using a directive like this:
630Sstevel@tonic-gate#
640Sstevel@tonic-gate# 	MAPFILE: export "Driver OK"
650Sstevel@tonic-gate#
660Sstevel@tonic-gate# Using the above directive, only those function prototypes and variable
670Sstevel@tonic-gate# declarations with "/* Driver OK */" comments included in the mapfile.  Note
680Sstevel@tonic-gate# that the comment must be at the end of the first line.  If the declaration
690Sstevel@tonic-gate# spans multiple lines, the exportation comment must appear on the first line.
700Sstevel@tonic-gate#
710Sstevel@tonic-gate# Examples of functions selected for exportation:
720Sstevel@tonic-gate#
730Sstevel@tonic-gate# MAPFILE: export "Driver OK"
740Sstevel@tonic-gate#
750Sstevel@tonic-gate# extern int foo(int);		/* Driver OK */
760Sstevel@tonic-gate# extern void bar(int, int,	/* Driver OK */
770Sstevel@tonic-gate#     int, void *);
780Sstevel@tonic-gate#
790Sstevel@tonic-gate# Selective Exportation may not be used in the same file as Selective Exclusion.
800Sstevel@tonic-gate#
810Sstevel@tonic-gate# Selective Exclusion
820Sstevel@tonic-gate# -------------------
830Sstevel@tonic-gate#
840Sstevel@tonic-gate# Selective Exclusion is to be used in cases where the public/private interface
850Sstevel@tonic-gate# mix is reversed - where public interfaces greatly outnumber the private ones.
860Sstevel@tonic-gate# In this case, we want to be able to mark the private ones, thus telling this
870Sstevel@tonic-gate# script that the marked interfaces are to be excluded from the mapfile.
880Sstevel@tonic-gate# Marking is accomplished via a process similar to that used for Selective
890Sstevel@tonic-gate# Exportation.  A directive is included in a comment, and is formatted like
900Sstevel@tonic-gate# this:
910Sstevel@tonic-gate#
920Sstevel@tonic-gate#	MAPFILE: exclude "Internal"
930Sstevel@tonic-gate#
940Sstevel@tonic-gate# Using the above directive, function prototypes and variable declarations with
950Sstevel@tonic-gate# "/* Internal */" comments would be excluded.  Note that the comment must be at
960Sstevel@tonic-gate# the end of the first line.  If the declaration spans multiple lines, the
970Sstevel@tonic-gate# exclusion comment must appear on the first line.
980Sstevel@tonic-gate#
990Sstevel@tonic-gate# Examples of functions excluded from exportation:
1000Sstevel@tonic-gate#
1010Sstevel@tonic-gate# MAPFILE: exclude "Internal"
1020Sstevel@tonic-gate#
1030Sstevel@tonic-gate# extern int foo(int);		/* Internal */
1040Sstevel@tonic-gate# extern void bar(int, int,	/* Internal */
1050Sstevel@tonic-gate#	int, void *);
1060Sstevel@tonic-gate#
1070Sstevel@tonic-gate# Selective Exclusion may not be used in the same file as Selective Exportation.
1080Sstevel@tonic-gate#
1090Sstevel@tonic-gate
1100Sstevel@tonic-gatefunction extract_prototypes
1110Sstevel@tonic-gate{
1120Sstevel@tonic-gate	typeset header="$1"
1130Sstevel@tonic-gate	typeset prefix="$2"
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate	nawk -v prefix="$prefix" <$header '
1160Sstevel@tonic-gate		/^.*MAPFILE: export \"[^\"]*\"$/ {
1170Sstevel@tonic-gate			if (protoexclude) {
1180Sstevel@tonic-gate				print "ERROR: export after exclude\n";
1190Sstevel@tonic-gate				exit(1);
1200Sstevel@tonic-gate			}
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
1230Sstevel@tonic-gate			sub(/\"$/, "");
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate			exportmark=sprintf("/* %s */", $0);
1260Sstevel@tonic-gate			next;
1270Sstevel@tonic-gate		}
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate		/^.*MAPFILE: exclude \"[^\"]*\"$/ {
1300Sstevel@tonic-gate			if (protomatch) {
1310Sstevel@tonic-gate				print "ERROR: exclude after export";
1320Sstevel@tonic-gate				exit(1);
1330Sstevel@tonic-gate			}
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
1360Sstevel@tonic-gate			sub(/\"$/, "");
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate			excludemark=sprintf("/* %s */", $0);
1390Sstevel@tonic-gate			next;
1400Sstevel@tonic-gate		}
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate		exportmark {
1430Sstevel@tonic-gate			# Selective Exportation has been selected (exportmark is
1440Sstevel@tonic-gate			# set), so exclude this line if it does not have the
1450Sstevel@tonic-gate			# magic export mark.
1460Sstevel@tonic-gate			if (length($0) < length(exportmark) ||
1470Sstevel@tonic-gate			    substr($0, length($0) - length(exportmark) + 1) != \
1480Sstevel@tonic-gate			    exportmark)
1490Sstevel@tonic-gate				next;
1500Sstevel@tonic-gate		}
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate		excludemark {
1530Sstevel@tonic-gate			# Selective Exclusion has been selected (excludemark is
1540Sstevel@tonic-gate			# set), so exclude this line only if it has the magic
1550Sstevel@tonic-gate			# exclude mark.
1560Sstevel@tonic-gate			if (length($0) > length(excludemark) &&
1570Sstevel@tonic-gate			    substr($0, \
1580Sstevel@tonic-gate			    length($0) - length(excludemark) + 1) == \
1590Sstevel@tonic-gate			    excludemark)
1600Sstevel@tonic-gate				next;
1610Sstevel@tonic-gate		}
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate		# Functions
1640Sstevel@tonic-gate		/^extern.*\(/ {
1650Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
1660Sstevel@tonic-gate				if (sub(/\(.*$/, "", $i)) {
1670Sstevel@tonic-gate					sub(/^\*/, "", $i);
1680Sstevel@tonic-gate					if (!seenfn[$i]) {
1690Sstevel@tonic-gate						printf("%s%s;\n", prefix, $i);
1700Sstevel@tonic-gate						seenfn[$i] = 1;
1710Sstevel@tonic-gate					}
1720Sstevel@tonic-gate					break;
1730Sstevel@tonic-gate				}
1740Sstevel@tonic-gate			}
1750Sstevel@tonic-gate			next;
1760Sstevel@tonic-gate		}
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate		# Global variables
1790Sstevel@tonic-gate		/^extern[^\(\)]*;/ {
1800Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
1810Sstevel@tonic-gate				if (match($i, /;$/)) {
182*1605Spetede					printf("%s%s; /* variable */\n", prefix,
1830Sstevel@tonic-gate					    substr($i, 1, length($i) - 1));
1840Sstevel@tonic-gate					break;
1850Sstevel@tonic-gate				}
1860Sstevel@tonic-gate			}
1870Sstevel@tonic-gate			next;
1880Sstevel@tonic-gate		}
1890Sstevel@tonic-gate	' || die "Extraction failed"
1900Sstevel@tonic-gate}
1910Sstevel@tonic-gate
1920Sstevel@tonic-gatefunction extract_section
1930Sstevel@tonic-gate{
1940Sstevel@tonic-gate	typeset skel="$1"
1950Sstevel@tonic-gate	typeset secname="$2"
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate	nawk <$skel -v name=$secname -v skel=$skel '
1980Sstevel@tonic-gate	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
1990Sstevel@tonic-gate		if ($2 == "BEGIN") {
2000Sstevel@tonic-gate			printing = 1;
2010Sstevel@tonic-gate		} else {
2020Sstevel@tonic-gate			printing = 0;
2030Sstevel@tonic-gate		}
2040Sstevel@tonic-gate		next;
2050Sstevel@tonic-gate	    }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate	    printing != 0 { print; }
2080Sstevel@tonic-gate	'
2090Sstevel@tonic-gate}
2100Sstevel@tonic-gate
2110Sstevel@tonic-gatefunction die
2120Sstevel@tonic-gate{
2130Sstevel@tonic-gate	echo "$PROGNAME: $@" >&2
2140Sstevel@tonic-gate	exit 1
2150Sstevel@tonic-gate}
2160Sstevel@tonic-gate
2170Sstevel@tonic-gatefunction usage
2180Sstevel@tonic-gate{
2190Sstevel@tonic-gate	echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
2200Sstevel@tonic-gate	exit 2
2210Sstevel@tonic-gate}
2220Sstevel@tonic-gate
2230Sstevel@tonic-gatePROGNAME=$(basename "$0")
2240Sstevel@tonic-gate
2250Sstevel@tonic-gatewhile getopts t: c ; do
2260Sstevel@tonic-gate	case $c in
2270Sstevel@tonic-gate	    t)
2280Sstevel@tonic-gate		mapfile_skel=$OPTARG
2290Sstevel@tonic-gate		;;
2300Sstevel@tonic-gate	    ?)
2310Sstevel@tonic-gate		usage
2320Sstevel@tonic-gate	esac
2330Sstevel@tonic-gatedone
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate[[ -z "$mapfile_skel" ]] && usage
2360Sstevel@tonic-gate[[ ! -f $mapfile_skel ]] && die "Couldn't open template $tmplfile"
2370Sstevel@tonic-gate
2380Sstevel@tonic-gateshift $(($OPTIND - 1))
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate[[ $# -lt 1 ]] && usage
2410Sstevel@tonic-gate
2420Sstevel@tonic-gatefor file in $@ ; do
2430Sstevel@tonic-gate	[[ ! -f $file ]] && die "Can't open input file $file"
2440Sstevel@tonic-gatedone
2450Sstevel@tonic-gate
2460Sstevel@tonic-gateextract_section $mapfile_skel PROLOGUE
2470Sstevel@tonic-gate
2480Sstevel@tonic-gatefor file in $@ ; do
2490Sstevel@tonic-gate	echo "\t\t/*"
2500Sstevel@tonic-gate	echo "\t\t * Exported functions and variables from:"
2510Sstevel@tonic-gate	echo "\t\t *  $file"
2520Sstevel@tonic-gate	echo "\t\t */"
2530Sstevel@tonic-gate	extract_prototypes $file "\t\t"
2540Sstevel@tonic-gate	echo
2550Sstevel@tonic-gatedone
2560Sstevel@tonic-gate
2570Sstevel@tonic-gateextract_section $mapfile_skel EPILOGUE
258