1193323Sed#!/bin/sh
2193323Sed#
3193323Sed# unifdefall: remove all the #if's from a source file
4193323Sed#
5193323Sed# Copyright (c) 2002 - 2013 Tony Finch <dot@dotat.at>
6193323Sed# Copyright (c) 2009 - 2010 Jonathan Nieder <jrnieder@gmail.com>
7193323Sed#
8193323Sed# Redistribution and use in source and binary forms, with or without
9193323Sed# modification, are permitted provided that the following conditions
10193323Sed# are met:
11193323Sed# 1. Redistributions of source code must retain the above copyright
12193323Sed#    notice, this list of conditions and the following disclaimer.
13193323Sed# 2. Redistributions in binary form must reproduce the above copyright
14193323Sed#    notice, this list of conditions and the following disclaimer in the
15193323Sed#    documentation and/or other materials provided with the distribution.
16193323Sed#
17193323Sed# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18193323Sed# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19193323Sed# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20193323Sed# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21193323Sed# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22193323Sed# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23193323Sed# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24193323Sed# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25193323Sed# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26193323Sed# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27193323Sed# SUCH DAMAGE.
28193323Sed#
29193323Sed# $FreeBSD$
30193323Sed
31193323Sedset -e
32193323Sed
33193323Sedunifdef="$(dirname "$0")/unifdef"
34193323Sedif [ ! -e "$unifdef" ]
35198892Srdivackythen
36193323Sed	unifdef=unifdef
37193323Sedfi
38193323Sed
39193323Sedcase "$@" in
40193323Sed"-d "*)	echo DEBUGGING 1>&2
41193323Sed	debug=-d
42193323Sed	shift
43193323Sedesac
44193323Sed
45198090Srdivackytmp=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXXXXXX") || exit 2
46201360Srdivackytrap 'rm -r "$tmp" || exit 2' EXIT
47193323Sed
48193323Sedexport LC_ALL=C
49193323Sed
50193323Sed# list of all controlling macros; assume these are undefined
51193323Sed"$unifdef" $debug -s "$@" | sort -u | sed 's/^/#undef /' >"$tmp/undefs"
52193323Sed# list of all macro definitions
53203954Srdivackycc -E -dM "$@" | sort >"$tmp/defs"
54198090Srdivacky
55193323Sedcase $debug in
56193323Sed-d)	cat "$tmp/undefs" "$tmp/defs" 1>&2
57193323Sedesac
58193323Sed
59193323Sed# order of -f arguments means definitions override undefs
60193323Sed"$unifdef" $debug -k -f "$tmp/undefs" -f "$tmp/defs" "$@"
61198090Srdivacky