]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unifdef/unifdef.1
Merge DTC-d75b33af.
[FreeBSD/FreeBSD.git] / usr.bin / unifdef / unifdef.1
1 .\" Copyright (c) 1985, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\" Copyright (c) 2002 - 2010 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: unifdef/unifdef.1,v 1.63 2010/02/19 16:41:15 fanf2 Exp $
34 .\" $FreeBSD$
35 .\"
36 .Dd January 19, 2010
37 .Dt UNIFDEF 1
38 .Os
39 .Sh NAME
40 .Nm unifdef , unifdefall
41 .Nd remove preprocessor conditionals from code
42 .Sh SYNOPSIS
43 .Nm
44 .Op Fl BbcdeKknst
45 .Op Fl I Ns Ar path
46 .Op Fl D Ns Ar sym Ns Op = Ns Ar val
47 .Op Fl U Ns Ar sym
48 .Op Fl iD Ns Ar sym Ns Op = Ns Ar val
49 .Op Fl iU Ns Ar sym
50 .Ar ...
51 .Op Fl o Ar outfile
52 .Op Ar infile
53 .Nm unifdefall
54 .Op Fl I Ns Ar path
55 .Ar ...
56 .Ar file
57 .Sh DESCRIPTION
58 The
59 .Nm
60 utility selectively processes conditional
61 .Xr cpp 1
62 directives.
63 It removes from a file
64 both the directives
65 and any additional text that they specify should be removed,
66 while otherwise leaving the file alone.
67 .Pp
68 The
69 .Nm
70 utility acts on
71 .Ic #if , #ifdef , #ifndef , #elif , #else ,
72 and
73 .Ic #endif
74 lines.
75 A directive is only processed
76 if the symbols specified on the command line are sufficient to allow
77 .Nm
78 to get a definite value for its control expression.
79 If the result is false,
80 the directive and the following lines under its control are removed.
81 If the result is true,
82 only the directive is removed.
83 An
84 .Ic #ifdef
85 or
86 .Ic #ifndef
87 directive is passed through unchanged
88 if its controlling symbol is not specified on the command line.
89 Any
90 .Ic #if
91 or
92 .Ic #elif
93 control expression that has an unknown value or that
94 .Nm
95 cannot parse is passed through unchanged.
96 By default,
97 .Nm
98 ignores
99 .Ic #if
100 and
101 .Ic #elif
102 lines with constant expressions;
103 it can be told to process them by specifying the
104 .Fl k
105 flag on the command line.
106 .Pp
107 It understands a commonly-used subset
108 of the expression syntax for
109 .Ic #if
110 and
111 .Ic #elif
112 lines:
113 integer constants,
114 integer values of symbols defined on the command line,
115 the
116 .Fn defined
117 operator,
118 the operators
119 .Ic \&! , < , > , <= , >= , == , != , && , || ,
120 and parenthesized expressions.
121 A kind of
122 .Dq "short circuit"
123 evaluation is used for the
124 .Ic &&
125 operator:
126 if either operand is definitely false then the result is false,
127 even if the value of the other operand is unknown.
128 Similarly,
129 if either operand of
130 .Ic ||
131 is definitely true then the result is true.
132 .Pp
133 In most cases, the
134 .Nm
135 utility does not distinguish between object-like macros
136 (without arguments) and function-like arguments (with arguments).
137 If a macro is not explicitly defined, or is defined with the
138 .Fl D
139 flag on the command-line, its arguments are ignored.
140 If a macro is explicitly undefined on the command line with the
141 .Fl U
142 flag, it may not have any arguments since this leads to a syntax error.
143 .Pp
144 The
145 .Nm
146 utility understands just enough about C
147 to know when one of the directives is inactive
148 because it is inside
149 a comment,
150 or affected by a backslash-continued line.
151 It spots unusually-formatted preprocessor directives
152 and knows when the layout is too odd for it to handle.
153 .Pp
154 A script called
155 .Nm unifdefall
156 can be used to remove all conditional
157 .Xr cpp 1
158 directives from a file.
159 It uses
160 .Nm Fl s
161 and
162 .Nm cpp Fl dM
163 to get lists of all the controlling symbols
164 and their definitions (or lack thereof),
165 then invokes
166 .Nm
167 with appropriate arguments to process the file.
168 .Sh OPTIONS
169 .Pp
170 .Bl -tag -width indent -compact
171 .It Fl D Ns Ar sym Ns = Ns Ar val
172 Specify that a symbol is defined to a given value
173 which is used when evaluating
174 .Ic #if
175 and
176 .Ic #elif
177 control expressions.
178 .Pp
179 .It Fl D Ns Ar sym
180 Specify that a symbol is defined to the value 1.
181 .Pp
182 .It Fl U Ns Ar sym
183 Specify that a symbol is undefined.
184 If the same symbol appears in more than one argument,
185 the last occurrence dominates.
186 .Pp
187 .It Fl B
188 Compress blank lines around a deleted section.
189 Mutually exclusive with the
190 .Fl b
191 option.
192 .Pp
193 .It Fl b
194 Replace removed lines with blank lines
195 instead of deleting them.
196 Mutually exclusive with the
197 .Fl B
198 option.
199 .Pp
200 .It Fl c
201 If the
202 .Fl c
203 flag is specified,
204 then the operation of
205 .Nm
206 is complemented,
207 i.e., the lines that would have been removed or blanked
208 are retained and vice versa.
209 .Pp
210 .It Fl d
211 Turn on printing of degugging messages.
212 .Pp
213 .It Fl e
214 Because
215 .Nm
216 processes its input one line at a time,
217 it cannot remove preprocessor directives that span more than one line.
218 The most common example of this is a directive with a multi-line
219 comment hanging off its right hand end.
220 By default,
221 if
222 .Nm
223 has to process such a directive,
224 it will complain that the line is too obfuscated.
225 The
226 .Fl e
227 option changes the behaviour so that,
228 where possible,
229 such lines are left unprocessed instead of reporting an error.
230 .Pp
231 .It Fl K
232 Always treat the result of
233 .Ic &&
234 and
235 .Ic ||
236 operators as unknown if either operand is unknown,
237 instead of short-circuiting when unknown operands can't affect the result.
238 This option is for compatibility with older versions of
239 .Nm .
240 .Pp
241 .It Fl k
242 Process
243 .Ic #if
244 and
245 .Ic #elif
246 lines with constant expressions.
247 By default, sections controlled by such lines are passed through unchanged
248 because they typically start
249 .Dq Li "#if 0"
250 and are used as a kind of comment to sketch out future or past development.
251 It would be rude to strip them out, just as it would be for normal comments.
252 .Pp
253 .It Fl n
254 Add
255 .Li #line
256 directives to the output following any deleted lines,
257 so that errors produced when compiling the output file correspond to
258 line numbers in the input file.
259 .Pp
260 .It Fl o Ar outfile
261 Write output to the file
262 .Ar outfile
263 instead of the standard output.
264 If
265 .Ar outfile
266 is the same as the input file,
267 the output is written to a temporary file
268 which is renamed into place when
269 .Nm
270 completes successfully.
271 .Pp
272 .It Fl s
273 Instead of processing the input file as usual,
274 this option causes
275 .Nm
276 to produce a list of symbols that appear in expressions
277 that
278 .Nm
279 understands.
280 It is useful in conjunction with the
281 .Fl dM
282 option of
283 .Xr cpp 1
284 for creating
285 .Nm
286 command lines.
287 .Pp
288 .It Fl t
289 Disables parsing for C comments
290 and line continuations,
291 which is useful
292 for plain text.
293 .Pp
294 .It Fl iD Ns Ar sym Ns Op = Ns Ar val
295 .It Fl iU Ns Ar sym
296 Ignore
297 .Ic #ifdef Ns s .
298 If your C code uses
299 .Ic #ifdef Ns s
300 to delimit non-C lines,
301 such as comments
302 or code which is under construction,
303 then you must tell
304 .Nm
305 which symbols are used for that purpose so that it will not try to parse
306 comments
307 and line continuations
308 inside those
309 .Ic #ifdef Ns s .
310 You can specify ignored symbols with
311 .Fl iD Ns Ar sym Ns Oo = Ns Ar val Oc
312 and
313 .Fl iU Ns Ar sym
314 similar to
315 .Fl D Ns Ar sym Ns Op = Ns Ar val
316 and
317 .Fl U Ns Ar sym
318 above.
319 .Pp
320 .It Fl I Ns Ar path
321 Specifies to
322 .Nm unifdefall
323 an additional place to look for
324 .Ic #include
325 files.
326 This option is ignored by
327 .Nm
328 for compatibility with
329 .Xr cpp 1
330 and to simplify the implementation of
331 .Nm unifdefall .
332 .El
333 .Pp
334 The
335 .Nm
336 utility copies its output to
337 .Em stdout
338 and will take its input from
339 .Em stdin
340 if no
341 .Ar file
342 argument is given.
343 .Pp
344 The
345 .Nm
346 utility works nicely with the
347 .Fl D Ns Ar sym
348 option of
349 .Xr diff 1 .
350 .Sh EXIT STATUS
351 The
352 .Nm
353 utility exits 0 if the output is an exact copy of the input,
354 1 if not, and 2 if in trouble.
355 .Sh DIAGNOSTICS
356 .Bl -item
357 .It
358 Too many levels of nesting.
359 .It
360 Inappropriate
361 .Ic #elif ,
362 .Ic #else
363 or
364 .Ic #endif .
365 .It
366 Obfuscated preprocessor control line.
367 .It
368 Premature
369 .Tn EOF
370 (with the line number of the most recent unterminated
371 .Ic #if ) .
372 .It
373 .Tn EOF
374 in comment.
375 .El
376 .Sh SEE ALSO
377 .Xr cpp 1 ,
378 .Xr diff 1
379 .Sh HISTORY
380 The
381 .Nm
382 command appeared in
383 .Bx 2.9 .
384 .Tn ANSI\~C
385 support was added in
386 .Fx 4.7 .
387 .Sh AUTHORS
388 The original implementation was written by
389 .An Dave Yost Aq Dave@Yost.com .
390 .An Tony Finch Aq dot@dotat.at
391 rewrote it to support
392 .Tn ANSI\~C .
393 .Sh BUGS
394 Expression evaluation is very limited.
395 .Pp
396 Preprocessor control lines split across more than one physical line
397 (because of comments or backslash-newline)
398 cannot be handled in every situation.
399 .Pp
400 Trigraphs are not recognized.
401 .Pp
402 There is no support for symbols with different definitions at
403 different points in the source file.
404 .Pp
405 The text-mode and ignore functionality does not correspond to modern
406 .Xr cpp 1
407 behaviour.