PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>unifdefall.sh</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">unifdefall.sh&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. <span class="enscript-reference">#!/bin/sh
  24. </span><span class="enscript-comment">#
  25. </span><span class="enscript-comment"># unifdefall: remove all the #if's from a source file
  26. </span><span class="enscript-comment">#
  27. </span><span class="enscript-comment"># Copyright (c) 2002 - 2010 Tony Finch &lt;<a href="mailto:dot@dotat.at">dot@dotat.at</a>&gt;
  28. </span><span class="enscript-comment"># Copyright (c) 2009 - 2010 Jonathan Nieder &lt;<a href="mailto:jrnieder@gmail.com">jrnieder@gmail.com</a>&gt;
  29. </span><span class="enscript-comment">#
  30. </span><span class="enscript-comment"># Redistribution and use in source and binary forms, with or without
  31. </span><span class="enscript-comment"># modification, are permitted provided that the following conditions
  32. </span><span class="enscript-comment"># are met:
  33. </span><span class="enscript-comment"># 1. Redistributions of source code must retain the above copyright
  34. </span><span class="enscript-comment"># notice, this list of conditions and the following disclaimer.
  35. </span><span class="enscript-comment"># 2. Redistributions in binary form must reproduce the above copyright
  36. </span><span class="enscript-comment"># notice, this list of conditions and the following disclaimer in the
  37. </span><span class="enscript-comment"># documentation and/or other materials provided with the distribution.
  38. </span><span class="enscript-comment">#
  39. </span><span class="enscript-comment"># THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  40. </span><span class="enscript-comment"># ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. </span><span class="enscript-comment"># IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. </span><span class="enscript-comment"># ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  43. </span><span class="enscript-comment"># FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. </span><span class="enscript-comment"># DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  45. </span><span class="enscript-comment"># OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. </span><span class="enscript-comment"># HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47. </span><span class="enscript-comment"># LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  48. </span><span class="enscript-comment"># OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. </span><span class="enscript-comment"># SUCH DAMAGE.
  50. </span><span class="enscript-comment">#
  51. </span><span class="enscript-comment"># $FreeBSD: src/usr.bin/unifdef/unifdefall.sh,v 1.7 2010/03/12 17:55:29 fanf Exp $
  52. </span>
  53. <span class="enscript-keyword">set</span> -e
  54. unifdef=<span class="enscript-string">&quot;$(dirname &quot;</span>$0<span class="enscript-string">&quot;)/unifdef&quot;</span>
  55. <span class="enscript-keyword">if</span> [ ! -e <span class="enscript-string">&quot;$unifdef&quot;</span> ]
  56. <span class="enscript-keyword">then</span>
  57. unifdef=unifdef
  58. <span class="enscript-keyword">fi</span>
  59. <span class="enscript-keyword">case</span> <span class="enscript-string">&quot;$@&quot;</span> <span class="enscript-keyword">in</span>
  60. <span class="enscript-string">&quot;-d &quot;</span>*) <span class="enscript-keyword">echo</span> DEBUGGING 1&gt;&amp;2
  61. debug=-d
  62. <span class="enscript-keyword">shift</span>
  63. <span class="enscript-keyword">esac</span>
  64. basename=$(basename <span class="enscript-string">&quot;$0&quot;</span>)
  65. tmp=$(mktemp -d <span class="enscript-string">&quot;${TMPDIR:-/tmp}/$basename.XXXXXXXXXX&quot;</span>) || <span class="enscript-keyword">exit</span> 2
  66. <span class="enscript-keyword">trap</span> 'rm -r <span class="enscript-string">&quot;$tmp&quot;</span> || <span class="enscript-keyword">exit</span> 2' EXIT
  67. <span class="enscript-keyword">export</span> LC_ALL=C
  68. <span class="enscript-comment"># list of all controlling macros
  69. </span><span class="enscript-string">&quot;$unifdef&quot;</span> $debug -s <span class="enscript-string">&quot;$@&quot;</span> | sort | uniq &gt;<span class="enscript-string">&quot;$tmp/ctrl&quot;</span>
  70. <span class="enscript-comment"># list of all macro definitions
  71. </span>cpp -dM <span class="enscript-string">&quot;$@&quot;</span> | sort | sed 's/^<span class="enscript-comment">#define //' &gt;&quot;$tmp/hashdefs&quot;
  72. </span><span class="enscript-comment"># list of defined macro names
  73. </span>sed 's/[^A-Za-z0-9_].*$//' &lt;<span class="enscript-string">&quot;$tmp/hashdefs&quot;</span> &gt;<span class="enscript-string">&quot;$tmp/alldef&quot;</span>
  74. <span class="enscript-comment"># list of undefined and defined controlling macros
  75. </span>comm -23 <span class="enscript-string">&quot;$tmp/ctrl&quot;</span> <span class="enscript-string">&quot;$tmp/alldef&quot;</span> &gt;<span class="enscript-string">&quot;$tmp/undef&quot;</span>
  76. comm -12 <span class="enscript-string">&quot;$tmp/ctrl&quot;</span> <span class="enscript-string">&quot;$tmp/alldef&quot;</span> &gt;<span class="enscript-string">&quot;$tmp/def&quot;</span>
  77. <span class="enscript-comment"># create a sed script that extracts the controlling macro definitions
  78. </span><span class="enscript-comment"># and converts them to unifdef command-line arguments
  79. </span>sed 's|.*|s/^&amp;\\(([^)]*)\\)\\{0,1\\} /-D&amp;=/p|' &lt;<span class="enscript-string">&quot;$tmp/def&quot;</span> &gt;<span class="enscript-string">&quot;$tmp/script&quot;</span>
  80. <span class="enscript-comment"># create the final unifdef command
  81. </span>{ <span class="enscript-keyword">echo</span> <span class="enscript-string">&quot;$unifdef&quot;</span> $debug -k '\'
  82. <span class="enscript-comment"># convert the controlling undefined macros to -U arguments
  83. </span> sed 's/.*/-U&amp; \\/' &lt;<span class="enscript-string">&quot;$tmp/undef&quot;</span>
  84. <span class="enscript-comment"># convert the controlling defined macros to quoted -D arguments
  85. </span> sed -nf <span class="enscript-string">&quot;$tmp/script&quot;</span> &lt;<span class="enscript-string">&quot;$tmp/hashdefs&quot;</span> |
  86. sed <span class="enscript-string">&quot;s/'/'\\\\''/g;s/.*/'&amp;' \\\\/&quot;</span>
  87. <span class="enscript-keyword">echo</span> '<span class="enscript-string">&quot;$@&quot;</span>'
  88. } &gt;<span class="enscript-string">&quot;$tmp/cmd&quot;</span>
  89. <span class="enscript-keyword">case</span> $debug <span class="enscript-keyword">in</span>
  90. -d) <span class="enscript-keyword">for</span> i <span class="enscript-keyword">in</span> ctrl hashdefs alldef undef def script cmd
  91. <span class="enscript-keyword">do</span> <span class="enscript-keyword">echo</span> ==== $i
  92. cat <span class="enscript-string">&quot;$tmp/$i&quot;</span>
  93. <span class="enscript-keyword">done</span> 1&gt;&amp;2
  94. <span class="enscript-keyword">esac</span>
  95. <span class="enscript-comment"># run the command we just created
  96. </span>sh <span class="enscript-string">&quot;$tmp/cmd&quot;</span> <span class="enscript-string">&quot;$@&quot;</span>
  97. </pre>
  98. <hr />
  99. </body></html>