17c478bd9Sstevel@tonic-gate#!/bin/ksh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*b272bda5Spetede# Common Development and Distribution License (the "License").
7*b272bda5Spetede# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23*b272bda5Spetede# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate#
307c478bd9Sstevel@tonic-gate# Given a header file, extract function prototypes and global variable
317c478bd9Sstevel@tonic-gate# declarations in a form that can be used in a mapfile.  The list of extracted
327c478bd9Sstevel@tonic-gate# functions and variables will be combined with a user-specified template to
337c478bd9Sstevel@tonic-gate# create a complete mapfile.
347c478bd9Sstevel@tonic-gate#
357c478bd9Sstevel@tonic-gate# Template
367c478bd9Sstevel@tonic-gate# --------
377c478bd9Sstevel@tonic-gate#
387c478bd9Sstevel@tonic-gate# The template contains two sections - the prologue, and the epilogue.  These
397c478bd9Sstevel@tonic-gate# sections are used, verbatim, as the beginning and the end of the mapfile.
407c478bd9Sstevel@tonic-gate# Sections begin and end with single-line comments whose sole contents are
417c478bd9Sstevel@tonic-gate# "/* BEGIN $section */" and "/* END $section */".
427c478bd9Sstevel@tonic-gate#
437c478bd9Sstevel@tonic-gate# Template example:
447c478bd9Sstevel@tonic-gate#
457c478bd9Sstevel@tonic-gate# /* BEGIN PROLOGUE */
467c478bd9Sstevel@tonic-gate# [ ... prologue goes here ... ]
477c478bd9Sstevel@tonic-gate# /* END PROLOGUE */
487c478bd9Sstevel@tonic-gate# /* BEGIN EPILOGUE */
497c478bd9Sstevel@tonic-gate# [ ... epilogue goes here ... ]
507c478bd9Sstevel@tonic-gate# /* END EPILOGUE */
517c478bd9Sstevel@tonic-gate#
527c478bd9Sstevel@tonic-gate# Selective Exportation
537c478bd9Sstevel@tonic-gate# ---------------------
547c478bd9Sstevel@tonic-gate#
557c478bd9Sstevel@tonic-gate# Some header files will have a public/private interface mix that is strongly
567c478bd9Sstevel@tonic-gate# biased towards private interfaces.  That is, of the interfaces declared by
577c478bd9Sstevel@tonic-gate# a given header file, the majority of them are private.  Only a small subset
587c478bd9Sstevel@tonic-gate# of interfaces are to be exported publicly.  Using Selective Exportation, a
597c478bd9Sstevel@tonic-gate# special comment is included in the header file, declaring to this script that
607c478bd9Sstevel@tonic-gate# only a subset of interfaces - those with a marking declared in the comment -
617c478bd9Sstevel@tonic-gate# should be included in the mapfile.  The marking is itself a special comment,
627c478bd9Sstevel@tonic-gate# whose format is declared using a directive like this:
637c478bd9Sstevel@tonic-gate#
647c478bd9Sstevel@tonic-gate# 	MAPFILE: export "Driver OK"
657c478bd9Sstevel@tonic-gate#
667c478bd9Sstevel@tonic-gate# Using the above directive, only those function prototypes and variable
677c478bd9Sstevel@tonic-gate# declarations with "/* Driver OK */" comments included in the mapfile.  Note
687c478bd9Sstevel@tonic-gate# that the comment must be at the end of the first line.  If the declaration
697c478bd9Sstevel@tonic-gate# spans multiple lines, the exportation comment must appear on the first line.
707c478bd9Sstevel@tonic-gate#
717c478bd9Sstevel@tonic-gate# Examples of functions selected for exportation:
727c478bd9Sstevel@tonic-gate#
737c478bd9Sstevel@tonic-gate# MAPFILE: export "Driver OK"
747c478bd9Sstevel@tonic-gate#
757c478bd9Sstevel@tonic-gate# extern int foo(int);		/* Driver OK */
767c478bd9Sstevel@tonic-gate# extern void bar(int, int,	/* Driver OK */
777c478bd9Sstevel@tonic-gate#     int, void *);
787c478bd9Sstevel@tonic-gate#
797c478bd9Sstevel@tonic-gate# Selective Exportation may not be used in the same file as Selective Exclusion.
807c478bd9Sstevel@tonic-gate#
817c478bd9Sstevel@tonic-gate# Selective Exclusion
827c478bd9Sstevel@tonic-gate# -------------------
837c478bd9Sstevel@tonic-gate#
847c478bd9Sstevel@tonic-gate# Selective Exclusion is to be used in cases where the public/private interface
857c478bd9Sstevel@tonic-gate# mix is reversed - where public interfaces greatly outnumber the private ones.
867c478bd9Sstevel@tonic-gate# In this case, we want to be able to mark the private ones, thus telling this
877c478bd9Sstevel@tonic-gate# script that the marked interfaces are to be excluded from the mapfile.
887c478bd9Sstevel@tonic-gate# Marking is accomplished via a process similar to that used for Selective
897c478bd9Sstevel@tonic-gate# Exportation.  A directive is included in a comment, and is formatted like
907c478bd9Sstevel@tonic-gate# this:
917c478bd9Sstevel@tonic-gate#
927c478bd9Sstevel@tonic-gate#	MAPFILE: exclude "Internal"
937c478bd9Sstevel@tonic-gate#
947c478bd9Sstevel@tonic-gate# Using the above directive, function prototypes and variable declarations with
957c478bd9Sstevel@tonic-gate# "/* Internal */" comments would be excluded.  Note that the comment must be at
967c478bd9Sstevel@tonic-gate# the end of the first line.  If the declaration spans multiple lines, the
977c478bd9Sstevel@tonic-gate# exclusion comment must appear on the first line.
987c478bd9Sstevel@tonic-gate#
997c478bd9Sstevel@tonic-gate# Examples of functions excluded from exportation:
1007c478bd9Sstevel@tonic-gate#
1017c478bd9Sstevel@tonic-gate# MAPFILE: exclude "Internal"
1027c478bd9Sstevel@tonic-gate#
1037c478bd9Sstevel@tonic-gate# extern int foo(int);		/* Internal */
1047c478bd9Sstevel@tonic-gate# extern void bar(int, int,	/* Internal */
1057c478bd9Sstevel@tonic-gate#	int, void *);
1067c478bd9Sstevel@tonic-gate#
1077c478bd9Sstevel@tonic-gate# Selective Exclusion may not be used in the same file as Selective Exportation.
1087c478bd9Sstevel@tonic-gate#
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gatefunction extract_prototypes
1117c478bd9Sstevel@tonic-gate{
1127c478bd9Sstevel@tonic-gate	typeset header="$1"
1137c478bd9Sstevel@tonic-gate	typeset prefix="$2"
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate	nawk -v prefix="$prefix" <$header '
1167c478bd9Sstevel@tonic-gate		/^.*MAPFILE: export \"[^\"]*\"$/ {
1177c478bd9Sstevel@tonic-gate			if (protoexclude) {
1187c478bd9Sstevel@tonic-gate				print "ERROR: export after exclude\n";
1197c478bd9Sstevel@tonic-gate				exit(1);
1207c478bd9Sstevel@tonic-gate			}
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
1237c478bd9Sstevel@tonic-gate			sub(/\"$/, "");
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate			exportmark=sprintf("/* %s */", $0);
1267c478bd9Sstevel@tonic-gate			next;
1277c478bd9Sstevel@tonic-gate		}
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate		/^.*MAPFILE: exclude \"[^\"]*\"$/ {
1307c478bd9Sstevel@tonic-gate			if (protomatch) {
1317c478bd9Sstevel@tonic-gate				print "ERROR: exclude after export";
1327c478bd9Sstevel@tonic-gate				exit(1);
1337c478bd9Sstevel@tonic-gate			}
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate			sub(/^[^\"]*\"/, "");
1367c478bd9Sstevel@tonic-gate			sub(/\"$/, "");
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate			excludemark=sprintf("/* %s */", $0);
1397c478bd9Sstevel@tonic-gate			next;
1407c478bd9Sstevel@tonic-gate		}
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate		exportmark {
1437c478bd9Sstevel@tonic-gate			# Selective Exportation has been selected (exportmark is
1447c478bd9Sstevel@tonic-gate			# set), so exclude this line if it does not have the
1457c478bd9Sstevel@tonic-gate			# magic export mark.
1467c478bd9Sstevel@tonic-gate			if (length($0) < length(exportmark) ||
1477c478bd9Sstevel@tonic-gate			    substr($0, length($0) - length(exportmark) + 1) != \
1487c478bd9Sstevel@tonic-gate			    exportmark)
1497c478bd9Sstevel@tonic-gate				next;
1507c478bd9Sstevel@tonic-gate		}
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate		excludemark {
1537c478bd9Sstevel@tonic-gate			# Selective Exclusion has been selected (excludemark is
1547c478bd9Sstevel@tonic-gate			# set), so exclude this line only if it has the magic
1557c478bd9Sstevel@tonic-gate			# exclude mark.
1567c478bd9Sstevel@tonic-gate			if (length($0) > length(excludemark) &&
1577c478bd9Sstevel@tonic-gate			    substr($0, \
1587c478bd9Sstevel@tonic-gate			    length($0) - length(excludemark) + 1) == \
1597c478bd9Sstevel@tonic-gate			    excludemark)
1607c478bd9Sstevel@tonic-gate				next;
1617c478bd9Sstevel@tonic-gate		}
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate		# Functions
1647c478bd9Sstevel@tonic-gate		/^extern.*\(/ {
1657c478bd9Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
1667c478bd9Sstevel@tonic-gate				if (sub(/\(.*$/, "", $i)) {
1677c478bd9Sstevel@tonic-gate					sub(/^\*/, "", $i);
1687c478bd9Sstevel@tonic-gate					if (!seenfn[$i]) {
1697c478bd9Sstevel@tonic-gate						printf("%s%s;\n", prefix, $i);
1707c478bd9Sstevel@tonic-gate						seenfn[$i] = 1;
1717c478bd9Sstevel@tonic-gate					}
1727c478bd9Sstevel@tonic-gate					break;
1737c478bd9Sstevel@tonic-gate				}
1747c478bd9Sstevel@tonic-gate			}
1757c478bd9Sstevel@tonic-gate			next;
1767c478bd9Sstevel@tonic-gate		}
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate		# Global variables
1797c478bd9Sstevel@tonic-gate		/^extern[^\(\)]*;/ {
1807c478bd9Sstevel@tonic-gate			for (i = 1; i <= NF; i++) {
1817c478bd9Sstevel@tonic-gate				if (match($i, /;$/)) {
182*b272bda5Spetede					printf("%s%s; /* variable */\n", prefix,
1837c478bd9Sstevel@tonic-gate					    substr($i, 1, length($i) - 1));
1847c478bd9Sstevel@tonic-gate					break;
1857c478bd9Sstevel@tonic-gate				}
1867c478bd9Sstevel@tonic-gate			}
1877c478bd9Sstevel@tonic-gate			next;
1887c478bd9Sstevel@tonic-gate		}
1897c478bd9Sstevel@tonic-gate	' || die "Extraction failed"
1907c478bd9Sstevel@tonic-gate}
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gatefunction extract_section
1937c478bd9Sstevel@tonic-gate{
1947c478bd9Sstevel@tonic-gate	typeset skel="$1"
1957c478bd9Sstevel@tonic-gate	typeset secname="$2"
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate	nawk <$skel -v name=$secname -v skel=$skel '
1987c478bd9Sstevel@tonic-gate	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
1997c478bd9Sstevel@tonic-gate		if ($2 == "BEGIN") {
2007c478bd9Sstevel@tonic-gate			printing = 1;
2017c478bd9Sstevel@tonic-gate		} else {
2027c478bd9Sstevel@tonic-gate			printing = 0;
2037c478bd9Sstevel@tonic-gate		}
2047c478bd9Sstevel@tonic-gate		next;
2057c478bd9Sstevel@tonic-gate	    }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate	    printing != 0 { print; }
2087c478bd9Sstevel@tonic-gate	'
2097c478bd9Sstevel@tonic-gate}
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gatefunction die
2127c478bd9Sstevel@tonic-gate{
2137c478bd9Sstevel@tonic-gate	echo "$PROGNAME: $@" >&2
2147c478bd9Sstevel@tonic-gate	exit 1
2157c478bd9Sstevel@tonic-gate}
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gatefunction usage
2187c478bd9Sstevel@tonic-gate{
2197c478bd9Sstevel@tonic-gate	echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
2207c478bd9Sstevel@tonic-gate	exit 2
2217c478bd9Sstevel@tonic-gate}
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gatePROGNAME=$(basename "$0")
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gatewhile getopts t: c ; do
2267c478bd9Sstevel@tonic-gate	case $c in
2277c478bd9Sstevel@tonic-gate	    t)
2287c478bd9Sstevel@tonic-gate		mapfile_skel=$OPTARG
2297c478bd9Sstevel@tonic-gate		;;
2307c478bd9Sstevel@tonic-gate	    ?)
2317c478bd9Sstevel@tonic-gate		usage
2327c478bd9Sstevel@tonic-gate	esac
2337c478bd9Sstevel@tonic-gatedone
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate[[ -z "$mapfile_skel" ]] && usage
2367c478bd9Sstevel@tonic-gate[[ ! -f $mapfile_skel ]] && die "Couldn't open template $tmplfile"
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gateshift $(($OPTIND - 1))
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate[[ $# -lt 1 ]] && usage
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gatefor file in $@ ; do
2437c478bd9Sstevel@tonic-gate	[[ ! -f $file ]] && die "Can't open input file $file"
2447c478bd9Sstevel@tonic-gatedone
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gateextract_section $mapfile_skel PROLOGUE
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gatefor file in $@ ; do
2497c478bd9Sstevel@tonic-gate	echo "\t\t/*"
2507c478bd9Sstevel@tonic-gate	echo "\t\t * Exported functions and variables from:"
2517c478bd9Sstevel@tonic-gate	echo "\t\t *  $file"
2527c478bd9Sstevel@tonic-gate	echo "\t\t */"
2537c478bd9Sstevel@tonic-gate	extract_prototypes $file "\t\t"
2547c478bd9Sstevel@tonic-gate	echo
2557c478bd9Sstevel@tonic-gatedone
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gateextract_section $mapfile_skel EPILOGUE
258