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