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