196670Sfanf#!/bin/sh
296670Sfanf#
3199813Sfanf# unifdefall: remove all the #if's from a source file
496670Sfanf#
5202636Sfanf# Copyright (c) 2002 - 2010 Tony Finch <dot@dotat.at>
6202636Sfanf# Copyright (c) 2009 - 2010 Jonathan Nieder <jrnieder@gmail.com>
7199813Sfanf#
8199813Sfanf# Redistribution and use in source and binary forms, with or without
9199813Sfanf# modification, are permitted provided that the following conditions
10199813Sfanf# are met:
11199813Sfanf# 1. Redistributions of source code must retain the above copyright
12199813Sfanf#    notice, this list of conditions and the following disclaimer.
13199813Sfanf# 2. Redistributions in binary form must reproduce the above copyright
14199813Sfanf#    notice, this list of conditions and the following disclaimer in the
15199813Sfanf#    documentation and/or other materials provided with the distribution.
16199813Sfanf#
17199813Sfanf# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18199813Sfanf# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19199813Sfanf# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20199813Sfanf# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21199813Sfanf# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22199813Sfanf# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199813Sfanf# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24199813Sfanf# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25199813Sfanf# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26199813Sfanf# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27199813Sfanf# SUCH DAMAGE.
28199813Sfanf#
2996670Sfanf# $FreeBSD$
3096670Sfanf
3196670Sfanfset -e
3296670Sfanf
33202636Sfanfunifdef="$(dirname "$0")/unifdef"
34202636Sfanfif [ ! -e "$unifdef" ]
35202636Sfanfthen
36202636Sfanf	unifdef=unifdef
37202636Sfanffi
38202636Sfanf
39205089Sfanfcase "$@" in
40205089Sfanf"-d "*)	echo DEBUGGING 1>&2
41205089Sfanf	debug=-d
42205089Sfanf	shift
43205089Sfanfesac
44205089Sfanf
45202636Sfanfbasename=$(basename "$0")
46199813Sfanftmp=$(mktemp -d "${TMPDIR:-/tmp}/$basename.XXXXXXXXXX") || exit 2
47205089Sfanftrap 'rm -r "$tmp" || exit 2' EXIT
4896670Sfanf
49199813Sfanfexport LC_ALL=C
5096670Sfanf
51199842Sfanf# list of all controlling macros
52205089Sfanf"$unifdef" $debug -s "$@" | sort | uniq >"$tmp/ctrl"
53199842Sfanf# list of all macro definitions
54199813Sfanfcpp -dM "$@" | sort | sed 's/^#define //' >"$tmp/hashdefs"
55199842Sfanf# list of defined macro names
56199842Sfanfsed 's/[^A-Za-z0-9_].*$//' <"$tmp/hashdefs" >"$tmp/alldef"
57199842Sfanf# list of undefined and defined controlling macros
58199813Sfanfcomm -23 "$tmp/ctrl" "$tmp/alldef" >"$tmp/undef"
59199813Sfanfcomm -12 "$tmp/ctrl" "$tmp/alldef" >"$tmp/def"
60199842Sfanf# create a sed script that extracts the controlling macro definitions
61199842Sfanf# and converts them to unifdef command-line arguments
62199842Sfanfsed 's|.*|s/^&\\(([^)]*)\\)\\{0,1\\} /-D&=/p|' <"$tmp/def" >"$tmp/script"
63199842Sfanf# create the final unifdef command
64205089Sfanf{	echo "$unifdef" $debug -k '\'
65199842Sfanf	# convert the controlling undefined macros to -U arguments
66199842Sfanf	sed 's/.*/-U& \\/' <"$tmp/undef"
67199842Sfanf	# convert the controlling defined macros to quoted -D arguments
68199842Sfanf	sed -nf "$tmp/script" <"$tmp/hashdefs" |
69199813Sfanf		sed "s/'/'\\\\''/g;s/.*/'&' \\\\/"
70199813Sfanf	echo '"$@"'
71199842Sfanf} >"$tmp/cmd"
72205089Sfanfcase $debug in
73205089Sfanf-d)	for i in ctrl hashdefs alldef undef def script cmd
74205089Sfanf	do	echo ==== $i
75205089Sfanf		cat "$tmp/$i"
76205089Sfanf	done 1>&2
77205089Sfanfesac
78199842Sfanf# run the command we just created
79199813Sfanfsh "$tmp/cmd" "$@"
80