196670Sfanf#!/bin/sh
296670Sfanf#
3199813Sfanf# unifdefall: remove all the #if's from a source file
496670Sfanf#
5248849Sfanf# Copyright (c) 2002 - 2013 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
45248258Sobrientmp=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXXXXXX") || exit 2
46205089Sfanftrap 'rm -r "$tmp" || exit 2' EXIT
4796670Sfanf
48199813Sfanfexport LC_ALL=C
4996670Sfanf
50199842Sfanf# list of all controlling macros
51205089Sfanf"$unifdef" $debug -s "$@" | sort | uniq >"$tmp/ctrl"
52199842Sfanf# list of all macro definitions
53199813Sfanfcpp -dM "$@" | sort | sed 's/^#define //' >"$tmp/hashdefs"
54199842Sfanf# list of defined macro names
55199842Sfanfsed 's/[^A-Za-z0-9_].*$//' <"$tmp/hashdefs" >"$tmp/alldef"
56199842Sfanf# list of undefined and defined controlling macros
57199813Sfanfcomm -23 "$tmp/ctrl" "$tmp/alldef" >"$tmp/undef"
58199813Sfanfcomm -12 "$tmp/ctrl" "$tmp/alldef" >"$tmp/def"
59199842Sfanf# create a sed script that extracts the controlling macro definitions
60199842Sfanf# and converts them to unifdef command-line arguments
61199842Sfanfsed 's|.*|s/^&\\(([^)]*)\\)\\{0,1\\} /-D&=/p|' <"$tmp/def" >"$tmp/script"
62199842Sfanf# create the final unifdef command
63205089Sfanf{	echo "$unifdef" $debug -k '\'
64199842Sfanf	# convert the controlling undefined macros to -U arguments
65199842Sfanf	sed 's/.*/-U& \\/' <"$tmp/undef"
66199842Sfanf	# convert the controlling defined macros to quoted -D arguments
67199842Sfanf	sed -nf "$tmp/script" <"$tmp/hashdefs" |
68199813Sfanf		sed "s/'/'\\\\''/g;s/.*/'&' \\\\/"
69199813Sfanf	echo '"$@"'
70199842Sfanf} >"$tmp/cmd"
71205089Sfanfcase $debug in
72205089Sfanf-d)	for i in ctrl hashdefs alldef undef def script cmd
73205089Sfanf	do	echo ==== $i
74205089Sfanf		cat "$tmp/$i"
75205089Sfanf	done 1>&2
76205089Sfanfesac
77199842Sfanf# run the command we just created
78199813Sfanfsh "$tmp/cmd" "$@"
79