1.\" $NetBSD: unifdef.1,v 1.11 2003/11/11 13:48:50 wiz Exp $ 2.\" 3.\" Copyright (c) 1985, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Dave Yost. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.\" Portions of this code (support for #if and #elif) are Copyright (c) 34.\" 2002, 2003 Tony Finch. 35.\" 36.\" Redistribution and use in source and binary forms, with or without 37.\" modification, are permitted provided that the following conditions 38.\" are met: 39.\" 1. Redistributions of source code must retain the above copyright 40.\" notice, this list of conditions and the following disclaimer. 41.\" 2. Redistributions in binary form must reproduce the above copyright 42.\" notice, this list of conditions and the following disclaimer in the 43.\" documentation and/or other materials provided with the distribution. 44.\" 3. All advertising materials mentioning features or use of this software 45.\" must display the following acknowledgement: 46.\" This product includes software developed by the University of 47.\" California, Berkeley and its contributors. 48.\" 4. Neither the name of the University nor the names of its contributors 49.\" may be used to endorse or promote products derived from this software 50.\" without specific prior written permission. 51.\" 52.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62.\" SUCH DAMAGE. 63.\" 64.\" @(#)unifdef.1 8.2 (Berkeley) 4/1/94 65.\" $dotat: things/unifdef.1,v 1.45 2003/01/20 14:37:08 fanf2 Exp $ 66.\" $FreeBSD: src/usr.bin/unifdef/unifdef.1,v 1.19 2003/01/20 12:41:41 fanf Exp $ 67.\" 68.Dd September 24, 2002 69.Dt UNIFDEF 1 70.Os 71.Sh NAME 72.Nm unifdef , 73.Nm unifdefall 74.Nd remove preprocessor conditionals from code 75.Sh SYNOPSIS 76.Nm 77.Op Fl ceklst 78.Op Fl I Ns Ar path 79.Op Fl D Ns Ar sym Ns Op = Ns Ar val 80.Op Fl U Ns Ar sym 81.Op Fl iD Ns Ar sym Ns Op = Ns Ar val 82.Op Fl iU Ns Ar sym 83.Ar ... 84.Op Ar file 85.Nm unifdefall 86.Op Fl I Ns Ar path 87.Ar ... 88.Ar file 89.Sh DESCRIPTION 90The 91.Nm 92utility selectively processes conditional 93.Xr cpp 1 94directives. 95It removes from a file both the directives and any additional text 96that they specify should be removed, while otherwise leaving the 97file alone. 98.Pp 99The 100.Nm 101utility acts on 102.Ic #if , #ifdef , #ifndef , #elif , #else , 103and 104.Ic #endif 105lines, 106and it understands only the commonly-used subset 107of the expression syntax for 108.Ic #if 109and 110.Ic #elif 111lines. 112It handles 113integer values of symbols defined on the command line, 114the 115.Fn defined 116operator applied to symbols defined or undefined on the command line, 117the operators 118.Ic \&! , \*[Lt] , \*[Gt] , \*[Lt]= , \*[Gt]= , == , != , \*[Am]\*[Am] , || , 119and parenthesized expressions. 120Anything that it does not understand is passed through unharmed. 121It only processes 122.Ic #ifdef 123and 124.Ic #ifndef 125directives if the symbol is specified on the command line, 126otherwise they are also passed through unchanged. 127By default, it ignores 128.Ic #if 129and 130.Ic #elif 131lines with constant expressions, 132or they may be processed by specifying the 133.Fl k 134flag on the command line. 135.Pp 136The 137.Nm 138utility also understands just enough about C 139to know when one of the directives is inactive 140because it is inside 141a comment, 142or affected by a backslash-continued line. 143It spots unusually-formatted preprocessor directives 144and knows when the layout is too odd to handle. 145.Pp 146A script called 147.Nm unifdefall 148can be used to remove all conditional 149.Xr cpp 1 150directives from a file. 151It uses 152.Nm Fl s 153and 154.Nm cpp Fl dM 155to get lists of all the controlling symbols 156and their definitions (or lack thereof), 157then invokes 158.Nm 159with appropriate arguments to process the file. 160.Pp 161Available options: 162.Bl -tag -width indent -compact 163.It Fl D Ns Ar sym Ns Op = Ns Ar val 164Specify that a symbol is defined, 165and optionally specify what value to give it 166for the purpose of handling 167.Ic #if 168and 169.Ic #elif 170directives. 171.It Fl U Ns Ar sym 172Specify that a symbol is undefined. 173If the same symbol appears in more than one argument, 174the last occurrence dominates. 175.It Fl c 176If the 177.Fl c 178flag is specified, 179then the operation of 180.Nm 181is complemented, 182i.e., the lines that would have been removed or blanked 183are retained and vice versa. 184.It Fl e 185Because 186.Nm 187processes its input one line at a time, 188it cannot remove preprocessor directives that span more than one line. 189The most common example of this is a directive with a multi-line 190comment hanging off its right hand end. 191By default, 192if 193.Nm 194has to process such a directive, 195it will complain that the line is too obfuscated. 196The 197.Fl e 198option changes the behaviour so that, 199where possible, 200such lines are left unprocessed instead of reporting an error. 201.It Fl k 202Process 203.Ic #if 204and 205.Ic #elif 206lines with constant expressions. 207By default, sections controlled by such lines are passed through unchanged 208because they typically start 209.Dq Li "#if 0" 210and are used as a kind of comment to sketch out future or past development. 211It would be rude to strip them out, just as it would be for normal comments. 212.It Fl l 213Replace removed lines with blank lines 214instead of deleting them. 215.It Fl s 216Instead of processing the input file as usual, 217this option causes 218.Nm 219to produce a list of symbols that appear in expressions 220that 221.Nm 222understands. 223It is useful in conjunction with the 224.Fl dM 225option of 226.Xr cpp 1 227for creating 228.Nm 229command lines. 230.It Fl t 231Disables parsing for C comments 232and line continuations, 233which is useful 234for plain text. 235.It Fl iD Ns Ar sym Ns Op = Ns Ar val 236.It Fl iU Ns Ar sym 237Ignore 238.Ic #ifdef Ns s . 239If your C code uses 240.Ic #ifdef Ns s 241to delimit non-C lines, 242such as comments 243or code which is under construction, 244then you must tell 245.Nm 246which symbols are used for that purpose so that it will not try to parse 247comments 248and line continuations 249inside those 250.Ic #ifdef Ns s . 251One specifies ignored symbols with 252.Fl iD Ns Ar sym Ns Oo = Ns Ar val Oc 253and 254.Fl iU Ns Ar sym 255similar to 256.Fl D Ns Ar sym Ns Op = Ns Ar val 257and 258.Fl U Ns Ar sym 259above. 260.It Fl I Ns Ar path 261Specifies to 262.Nm unifdefall 263an additional place to look for 264.Ic #include 265files. 266This option is ignored by 267.Nm 268for compatibility with 269.Xr cpp 1 270and to simplify the implementation of 271.Nm unifdefall . 272.El 273.Pp 274The 275.Nm 276utility copies its output to 277.Em stdout 278and will take its input from 279.Em stdin 280if no 281.Ar file 282argument is given. 283.Pp 284The 285.Nm 286utility works nicely with the 287.Fl D Ns Ar sym 288option of 289.Xr diff 1 . 290.Sh DIAGNOSTICS 291.Bl -item 292.It 293Too many levels of nesting. 294.It 295Inappropriate 296.Ic #elif , 297.Ic #else 298or 299.Ic #endif . 300.It 301Obfuscated preprocessor control line. 302.It 303Premature 304.Dv EOF 305(with the line number of the most recent unterminated 306.Ic #if ) . 307.It 308.Dv EOF 309in comment. 310.El 311.Pp 312The 313.Nm 314utility exits 0 if the output is an exact copy of the input, 3151 if not, and 2 if in trouble. 316.Sh SEE ALSO 317.Xr cpp 1 , 318.Xr diff 1 319.Sh HISTORY 320The 321.Nm 322command appeared in 323.Bx 4.3 . 324.Tn ANSI\~C 325support was added in 326.Fx 4.7 . 327.Sh BUGS 328Expression evaluation is very limited. 329.Pp 330Preprocessor control lines split across more than one physical line 331(because of comments or backslash-newline) 332cannot be handled in every situation. 333.Pp 334Trigraphs are not recognized. 335.Pp 336There is no support for symbols with different definitions at 337different points in the source file. 338.Pp 339The text-mode and ignore functionality does not correspond to modern 340.Xr cpp 1 341behaviour. 342