xref: /dragonfly/usr.bin/unifdef/unifdef.1 (revision 1bf4b486)
1.\" Copyright (c) 1985, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\" Copyright (c) 2002 - 2005 Tony Finch <dot@dotat.at>.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" Dave Yost. It was rewritten to support ANSI C 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.51 2005/03/08 12:39:01 fanf2 Exp $
34.\" $DragonFly: src/usr.bin/unifdef/unifdef.1,v 1.3 2005/07/25 17:51:36 hmp Exp $
35.\" $FreeBSD: src/usr.bin/unifdef/unifdef.1,v 1.24 2005/05/21 09:55:09 ru Exp $
36.\"
37.Dd September 24, 2002
38.Dt UNIFDEF 1
39.Os
40.Sh NAME
41.Nm unifdef , unifdefall
42.Nd remove preprocessor conditionals from code
43.Sh SYNOPSIS
44.Nm
45.Op Fl cdeklnst
46.Op Fl I Ns Ar path
47.Op Fl D Ns Ar sym Ns Op = Ns Ar val
48.Op Fl U Ns Ar sym
49.Op Fl iD Ns Ar sym Ns Op = Ns Ar val
50.Op Fl iU Ns Ar sym
51.Ar ...
52.Op Ar file
53.Nm unifdefall
54.Op Fl I Ns Ar path
55.Ar ...
56.Ar file
57.Sh DESCRIPTION
58The
59.Nm
60utility selectively processes conditional
61.Xr cpp 1
62directives.
63It removes from a file
64both the directives
65and any additional text that they specify should be removed,
66while otherwise leaving the file alone.
67.Pp
68The
69.Nm
70utility acts on
71.Ic #if , #ifdef , #ifndef , #elif , #else ,
72and
73.Ic #endif
74lines,
75and it understands only the commonly-used subset
76of the expression syntax for
77.Ic #if
78and
79.Ic #elif
80lines.
81It handles
82integer values of symbols defined on the command line,
83the
84.Fn defined
85operator applied to symbols defined or undefined on the command line,
86the operators
87.Ic \&! , < , > , <= , >= , == , != , && , || ,
88and parenthesized expressions.
89Anything that it does not understand is passed through unharmed.
90It only processes
91.Ic #ifdef
92and
93.Ic #ifndef
94directives if the symbol is specified on the command line,
95otherwise they are also passed through unchanged.
96By default, it ignores
97.Ic #if
98and
99.Ic #elif
100lines with constant expressions,
101or they may be processed by specifying the
102.Fl k
103flag on the command line.
104.Pp
105The
106.Nm
107utility also understands just enough about C
108to know when one of the directives is inactive
109because it is inside
110a comment,
111or affected by a backslash-continued line.
112It spots unusually-formatted preprocessor directives
113and knows when the layout is too odd to handle.
114.Pp
115A script called
116.Nm unifdefall
117can be used to remove all conditional
118.Xr cpp 1
119directives from a file.
120It uses
121.Nm Fl s
122and
123.Nm cpp Fl dM
124to get lists of all the controlling symbols
125and their definitions (or lack thereof),
126then invokes
127.Nm
128with appropriate arguments to process the file.
129.Pp
130Available options:
131.Pp
132.Bl -tag -width indent -compact
133.It Fl D Ns Ar sym Ns Op = Ns Ar val
134Specify that a symbol is defined,
135and optionally specify what value to give it
136for the purpose of handling
137.Ic #if
138and
139.Ic #elif
140directives.
141.Pp
142.It Fl U Ns Ar sym
143Specify that a symbol is undefined.
144If the same symbol appears in more than one argument,
145the last occurrence dominates.
146.Pp
147.It Fl c
148If the
149.Fl c
150flag is specified,
151then the operation of
152.Nm
153is complemented,
154i.e., the lines that would have been removed or blanked
155are retained and vice versa.
156.Pp
157.It Fl d
158Turn on printing of degugging messages.
159.Pp
160.It Fl e
161Because
162.Nm
163processes its input one line at a time,
164it cannot remove preprocessor directives that span more than one line.
165The most common example of this is a directive with a multi-line
166comment hanging off its right hand end.
167By default,
168if
169.Nm
170has to process such a directive,
171it will complain that the line is too obfuscated.
172The
173.Fl e
174option changes the behaviour so that,
175where possible,
176such lines are left unprocessed instead of reporting an error.
177.Pp
178.It Fl k
179Process
180.Ic #if
181and
182.Ic #elif
183lines with constant expressions.
184By default, sections controlled by such lines are passed through unchanged
185because they typically start
186.Dq Li "#if 0"
187and are used as a kind of comment to sketch out future or past development.
188It would be rude to strip them out, just as it would be for normal comments.
189.Pp
190.It Fl l
191Replace removed lines with blank lines
192instead of deleting them.
193.Pp
194.It Fl n
195Add
196.Li #line
197directives to the output following any deleted lines,
198so that errors produced when compiling the output file correspond to
199line numbers in the input file.
200.Pp
201.It Fl s
202Instead of processing the input file as usual,
203this option causes
204.Nm
205to produce a list of symbols that appear in expressions
206that
207.Nm
208understands.
209It is useful in conjunction with the
210.Fl dM
211option of
212.Xr cpp 1
213for creating
214.Nm
215command lines.
216.Pp
217.It Fl t
218Disables parsing for C comments
219and line continuations,
220which is useful
221for plain text.
222.Pp
223.It Fl iD Ns Ar sym Ns Op = Ns Ar val
224.It Fl iU Ns Ar sym
225Ignore
226.Ic #ifdef Ns s .
227If your C code uses
228.Ic #ifdef Ns s
229to delimit non-C lines,
230such as comments
231or code which is under construction,
232then you must tell
233.Nm
234which symbols are used for that purpose so that it will not try to parse
235comments
236and line continuations
237inside those
238.Ic #ifdef Ns s .
239One specifies ignored symbols with
240.Fl iD Ns Ar sym Ns Oo = Ns Ar val Oc
241and
242.Fl iU Ns Ar sym
243similar to
244.Fl D Ns Ar sym Ns Op = Ns Ar val
245and
246.Fl U Ns Ar sym
247above.
248.Pp
249.It Fl I Ns Ar path
250Specifies to
251.Nm unifdefall
252an additional place to look for
253.Ic #include
254files.
255This option is ignored by
256.Nm
257for compatibility with
258.Xr cpp 1
259and to simplify the implementation of
260.Nm unifdefall .
261.El
262.Pp
263The
264.Nm
265utility copies its output to
266.Em stdout
267and will take its input from
268.Em stdin
269if no
270.Ar file
271argument is given.
272.Pp
273The
274.Nm
275utility works nicely with the
276.Fl D Ns Ar sym
277option of
278.Xr diff 1 .
279.Sh EXIT STATUS
280The
281.Nm
282utility exits 0 if the output is an exact copy of the input,
2831 if not, and 2 if in trouble.
284.Sh DIAGNOSTICS
285.Bl -item
286.It
287Too many levels of nesting.
288.It
289Inappropriate
290.Ic #elif ,
291.Ic #else
292or
293.Ic #endif .
294.It
295Obfuscated preprocessor control line.
296.It
297Premature
298.Tn EOF
299(with the line number of the most recent unterminated
300.Ic #if ) .
301.It
302.Tn EOF
303in comment.
304.El
305.Sh SEE ALSO
306.Xr cpp 1 ,
307.Xr diff 1
308.Sh HISTORY
309The
310.Nm
311command appeared in
312.Bx 4.3 .
313.Tn ANSI\~C
314support was added in
315.Fx 4.7 .
316.Sh BUGS
317Expression evaluation is very limited.
318.Pp
319Preprocessor control lines split across more than one physical line
320(because of comments or backslash-newline)
321cannot be handled in every situation.
322.Pp
323Trigraphs are not recognized.
324.Pp
325There is no support for symbols with different definitions at
326different points in the source file.
327.Pp
328The text-mode and ignore functionality does not correspond to modern
329.Xr cpp 1
330behaviour.
331