1984263bcSMatthew Dillon#!/bin/sh 2984263bcSMatthew Dillon# 3*04eae76fSSascha Wildner# unifdefall: remove all the #if's from a source file 4984263bcSMatthew Dillon# 5*04eae76fSSascha Wildner# Copyright (c) 2002 - 2013 Tony Finch <dot@dotat.at> 6*04eae76fSSascha Wildner# Copyright (c) 2009 - 2010 Jonathan Nieder <jrnieder@gmail.com> 7*04eae76fSSascha Wildner# 8*04eae76fSSascha Wildner# Redistribution and use in source and binary forms, with or without 9*04eae76fSSascha Wildner# modification, are permitted provided that the following conditions 10*04eae76fSSascha Wildner# are met: 11*04eae76fSSascha Wildner# 1. Redistributions of source code must retain the above copyright 12*04eae76fSSascha Wildner# notice, this list of conditions and the following disclaimer. 13*04eae76fSSascha Wildner# 2. Redistributions in binary form must reproduce the above copyright 14*04eae76fSSascha Wildner# notice, this list of conditions and the following disclaimer in the 15*04eae76fSSascha Wildner# documentation and/or other materials provided with the distribution. 16*04eae76fSSascha Wildner# 17*04eae76fSSascha Wildner# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*04eae76fSSascha Wildner# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*04eae76fSSascha Wildner# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*04eae76fSSascha Wildner# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21*04eae76fSSascha Wildner# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*04eae76fSSascha Wildner# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*04eae76fSSascha Wildner# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*04eae76fSSascha Wildner# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*04eae76fSSascha Wildner# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*04eae76fSSascha Wildner# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*04eae76fSSascha Wildner# SUCH DAMAGE. 28984263bcSMatthew Dillon 29984263bcSMatthew Dillonset -e 30984263bcSMatthew Dillon 31*04eae76fSSascha Wildnerunifdef="$(dirname "$0")/unifdef" 32*04eae76fSSascha Wildnerif [ ! -e "$unifdef" ] 33*04eae76fSSascha Wildnerthen 34*04eae76fSSascha Wildner unifdef=unifdef 35*04eae76fSSascha Wildnerfi 36984263bcSMatthew Dillon 37*04eae76fSSascha Wildnercase "$@" in 38*04eae76fSSascha Wildner"-d "*) echo DEBUGGING 1>&2 39*04eae76fSSascha Wildner debug=-d 40*04eae76fSSascha Wildner shift 41*04eae76fSSascha Wildneresac 42984263bcSMatthew Dillon 43*04eae76fSSascha Wildnertmp=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXXXXXX") || exit 2 44*04eae76fSSascha Wildnertrap 'rm -r "$tmp" || exit 2' EXIT 45984263bcSMatthew Dillon 46*04eae76fSSascha Wildnerexport LC_ALL=C 47*04eae76fSSascha Wildner 48*04eae76fSSascha Wildner# list of all controlling macros; assume these are undefined 49*04eae76fSSascha Wildner"$unifdef" $debug -s "$@" | sort -u | sed 's/^/#undef /' >"$tmp/undefs" 50*04eae76fSSascha Wildner# list of all macro definitions 51*04eae76fSSascha Wildnercc -E -dM "$@" | sort >"$tmp/defs" 52*04eae76fSSascha Wildner 53*04eae76fSSascha Wildnercase $debug in 54*04eae76fSSascha Wildner-d) cat "$tmp/undefs" "$tmp/defs" 1>&2 55*04eae76fSSascha Wildneresac 56*04eae76fSSascha Wildner 57*04eae76fSSascha Wildner# order of -f arguments means definitions override undefs 58*04eae76fSSascha Wildner"$unifdef" $debug -k -f "$tmp/undefs" -f "$tmp/defs" "$@" 59