/opensource.apple.com/source/developer_cmds/developer_cmds-58/unifdef/unifdefall.sh
Shell | 104 lines | 99 code | 5 blank | 0 comment | 1 complexity | 6aa7577122f11665c947c65c579ccad7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <title>unifdefall.sh</title>
- <style type="text/css">
- .enscript-comment { font-style: italic; color: rgb(178,34,34); }
- .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
- .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
- .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
- .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
- .enscript-string { font-weight: bold; color: rgb(188,143,143); }
- .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
- .enscript-type { font-weight: bold; color: rgb(34,139,34); }
- .enscript-highlight { text-decoration: underline; color: 0; }
- </style>
- </head>
- <body id="top">
- <h1 style="margin:8px;" id="f1">unifdefall.sh <span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
- <hr/>
- <div></div>
- <pre>
- <span class="enscript-reference">#!/bin/sh
- </span><span class="enscript-comment">#
- </span><span class="enscript-comment"># unifdefall: remove all the #if's from a source file
- </span><span class="enscript-comment">#
- </span><span class="enscript-comment"># Copyright (c) 2002 - 2010 Tony Finch <<a href="mailto:dot@dotat.at">dot@dotat.at</a>>
- </span><span class="enscript-comment"># Copyright (c) 2009 - 2010 Jonathan Nieder <<a href="mailto:jrnieder@gmail.com">jrnieder@gmail.com</a>>
- </span><span class="enscript-comment">#
- </span><span class="enscript-comment"># Redistribution and use in source and binary forms, with or without
- </span><span class="enscript-comment"># modification, are permitted provided that the following conditions
- </span><span class="enscript-comment"># are met:
- </span><span class="enscript-comment"># 1. Redistributions of source code must retain the above copyright
- </span><span class="enscript-comment"># notice, this list of conditions and the following disclaimer.
- </span><span class="enscript-comment"># 2. Redistributions in binary form must reproduce the above copyright
- </span><span class="enscript-comment"># notice, this list of conditions and the following disclaimer in the
- </span><span class="enscript-comment"># documentation and/or other materials provided with the distribution.
- </span><span class="enscript-comment">#
- </span><span class="enscript-comment"># THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- </span><span class="enscript-comment"># ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- </span><span class="enscript-comment"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- </span><span class="enscript-comment"># ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- </span><span class="enscript-comment"># FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- </span><span class="enscript-comment"># DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- </span><span class="enscript-comment"># OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- </span><span class="enscript-comment"># HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- </span><span class="enscript-comment"># LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- </span><span class="enscript-comment"># OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- </span><span class="enscript-comment"># SUCH DAMAGE.
- </span><span class="enscript-comment">#
- </span><span class="enscript-comment"># $FreeBSD: src/usr.bin/unifdef/unifdefall.sh,v 1.7 2010/03/12 17:55:29 fanf Exp $
- </span>
- <span class="enscript-keyword">set</span> -e
- unifdef=<span class="enscript-string">"$(dirname "</span>$0<span class="enscript-string">")/unifdef"</span>
- <span class="enscript-keyword">if</span> [ ! -e <span class="enscript-string">"$unifdef"</span> ]
- <span class="enscript-keyword">then</span>
- unifdef=unifdef
- <span class="enscript-keyword">fi</span>
- <span class="enscript-keyword">case</span> <span class="enscript-string">"$@"</span> <span class="enscript-keyword">in</span>
- <span class="enscript-string">"-d "</span>*) <span class="enscript-keyword">echo</span> DEBUGGING 1>&2
- debug=-d
- <span class="enscript-keyword">shift</span>
- <span class="enscript-keyword">esac</span>
- basename=$(basename <span class="enscript-string">"$0"</span>)
- tmp=$(mktemp -d <span class="enscript-string">"${TMPDIR:-/tmp}/$basename.XXXXXXXXXX"</span>) || <span class="enscript-keyword">exit</span> 2
- <span class="enscript-keyword">trap</span> 'rm -r <span class="enscript-string">"$tmp"</span> || <span class="enscript-keyword">exit</span> 2' EXIT
- <span class="enscript-keyword">export</span> LC_ALL=C
- <span class="enscript-comment"># list of all controlling macros
- </span><span class="enscript-string">"$unifdef"</span> $debug -s <span class="enscript-string">"$@"</span> | sort | uniq ><span class="enscript-string">"$tmp/ctrl"</span>
- <span class="enscript-comment"># list of all macro definitions
- </span>cpp -dM <span class="enscript-string">"$@"</span> | sort | sed 's/^<span class="enscript-comment">#define //' >"$tmp/hashdefs"
- </span><span class="enscript-comment"># list of defined macro names
- </span>sed 's/[^A-Za-z0-9_].*$//' <<span class="enscript-string">"$tmp/hashdefs"</span> ><span class="enscript-string">"$tmp/alldef"</span>
- <span class="enscript-comment"># list of undefined and defined controlling macros
- </span>comm -23 <span class="enscript-string">"$tmp/ctrl"</span> <span class="enscript-string">"$tmp/alldef"</span> ><span class="enscript-string">"$tmp/undef"</span>
- comm -12 <span class="enscript-string">"$tmp/ctrl"</span> <span class="enscript-string">"$tmp/alldef"</span> ><span class="enscript-string">"$tmp/def"</span>
- <span class="enscript-comment"># create a sed script that extracts the controlling macro definitions
- </span><span class="enscript-comment"># and converts them to unifdef command-line arguments
- </span>sed 's|.*|s/^&\\(([^)]*)\\)\\{0,1\\} /-D&=/p|' <<span class="enscript-string">"$tmp/def"</span> ><span class="enscript-string">"$tmp/script"</span>
- <span class="enscript-comment"># create the final unifdef command
- </span>{ <span class="enscript-keyword">echo</span> <span class="enscript-string">"$unifdef"</span> $debug -k '\'
- <span class="enscript-comment"># convert the controlling undefined macros to -U arguments
- </span> sed 's/.*/-U& \\/' <<span class="enscript-string">"$tmp/undef"</span>
- <span class="enscript-comment"># convert the controlling defined macros to quoted -D arguments
- </span> sed -nf <span class="enscript-string">"$tmp/script"</span> <<span class="enscript-string">"$tmp/hashdefs"</span> |
- sed <span class="enscript-string">"s/'/'\\\\''/g;s/.*/'&' \\\\/"</span>
- <span class="enscript-keyword">echo</span> '<span class="enscript-string">"$@"</span>'
- } ><span class="enscript-string">"$tmp/cmd"</span>
- <span class="enscript-keyword">case</span> $debug <span class="enscript-keyword">in</span>
- -d) <span class="enscript-keyword">for</span> i <span class="enscript-keyword">in</span> ctrl hashdefs alldef undef def script cmd
- <span class="enscript-keyword">do</span> <span class="enscript-keyword">echo</span> ==== $i
- cat <span class="enscript-string">"$tmp/$i"</span>
- <span class="enscript-keyword">done</span> 1>&2
- <span class="enscript-keyword">esac</span>
- <span class="enscript-comment"># run the command we just created
- </span>sh <span class="enscript-string">"$tmp/cmd"</span> <span class="enscript-string">"$@"</span>
- </pre>
- <hr />
- </body></html>