]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.bin/unifdef/unifdef.1
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.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 - 2013 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 January 7, 2014
35 .Dt UNIFDEF 1 PRM
36 .Os " "
37 .Sh NAME
38 .Nm unifdef , unifdefall
39 .Nd remove preprocessor conditionals from code
40 .Sh SYNOPSIS
41 .Nm
42 .Op Fl bBcdehKkmnsStV
43 .Op Fl I Ns Ar path
44 .Op Fl [i]D Ns Ar sym Ns Op = Ns Ar val
45 .Op Fl [i]U Ns Ar sym
46 .Ar ...
47 .Op Fl f Ar defile
48 .Op Fl x Bro Ar 012 Brc
49 .Op Fl M Ar backext
50 .Op Fl o Ar outfile
51 .Op Ar infile ...
52 .Nm unifdefall
53 .Op Fl I Ns Ar path
54 .Ar ...
55 .Ar file
56 .Sh DESCRIPTION
57 The
58 .Nm
59 utility selectively processes conditional
60 .Xr cpp 1
61 directives.
62 It removes from a file
63 both the directives
64 and any additional text that they specify should be removed,
65 while otherwise leaving the file alone.
66 .Pp
67 The
68 .Nm
69 utility acts on
70 .Ic #if , #ifdef , #ifndef ,
71 .Ic #elif , #else ,
72 and
73 .Ic #endif
74 lines,
75 using macros specified in
76 .Fl D
77 and
78 .Fl U
79 command line options or in
80 .Fl f
81 definitions files.
82 A directive is processed
83 if the macro specifications are sufficient to provide
84 a definite value for its control expression.
85 If the result is false,
86 the directive and the following lines under its control are removed.
87 If the result is true,
88 only the directive is removed.
89 An
90 .Ic #ifdef
91 or
92 .Ic #ifndef
93 directive is passed through unchanged
94 if its controlling macro is not specified.
95 Any
96 .Ic #if
97 or
98 .Ic #elif
99 control expression that has an unknown value or that
100 .Nm
101 cannot parse is passed through unchanged.
102 By default,
103 .Nm
104 ignores
105 .Ic #if
106 and
107 .Ic #elif
108 lines with constant expressions;
109 it can be told to process them by specifying the
110 .Fl k
111 flag on the command line.
112 .Pp
113 It understands a commonly-used subset
114 of the expression syntax for
115 .Ic #if
116 and
117 .Ic #elif
118 lines:
119 integer constants,
120 integer values of macros defined on the command line,
121 the
122 .Fn defined
123 operator,
124 the operators
125 .Ic \&! , < , > ,
126 .Ic <= , >= , == , != ,
127 .Ic && , || ,
128 and parenthesized expressions.
129 A kind of
130 .Dq "short circuit"
131 evaluation is used for the
132 .Ic &&
133 operator:
134 if either operand is definitely false then the result is false,
135 even if the value of the other operand is unknown.
136 Similarly,
137 if either operand of
138 .Ic ||
139 is definitely true then the result is true.
140 .Pp
141 When evaluating an expression,
142 .Nm
143 does not expand macros first.
144 The value of a macro must be a simple number,
145 not an expression.
146 A limited form of indirection is allowed,
147 where one macro's value is the name of another.
148 .Pp
149 In most cases,
150 .Nm
151 does not distinguish between object-like macros
152 (without arguments) and function-like macros (with arguments).
153 A function-like macro invocation can appear in
154 .Ic #if
155 and
156 .Ic #elif
157 control expressions.
158 If the macro is not explicitly defined,
159 or is defined with the
160 .Fl D
161 flag on the command-line,
162 or with
163 .Ic #define
164 in a
165 .Fl f
166 definitions file,
167 its arguments are ignored.
168 If a macro is explicitly undefined on the command line with the
169 .Fl U
170 flag,
171 or with
172 .Ic #undef
173 in a
174 .Fl f
175 definitions file,
176 it may not have any arguments since this leads to a syntax error.
177 .Pp
178 The
179 .Nm
180 utility understands just enough about C
181 to know when one of the directives is inactive
182 because it is inside
183 a comment,
184 or affected by a backslash-continued line.
185 It spots unusually-formatted preprocessor directives
186 and knows when the layout is too odd for it to handle.
187 .Pp
188 A script called
189 .Nm unifdefall
190 can be used to remove all conditional
191 .Xr cpp 1
192 directives from a file.
193 It uses
194 .Nm Fl s
195 and
196 .Nm cpp Fl dM
197 to get lists of all the controlling macros
198 and their definitions (or lack thereof),
199 then invokes
200 .Nm
201 with appropriate arguments to process the file.
202 .Sh OPTIONS
203 .Bl -tag -width indent -compact
204 .It Fl D Ns Ar sym Ns = Ns Ar val
205 Specify that a macro is defined to a given value.
206 .Pp
207 .It Fl D Ns Ar sym
208 Specify that a macro is defined to the value 1.
209 .Pp
210 .It Fl U Ns Ar sym
211 Specify that a macro is undefined.
212 .Pp
213 If the same macro appears in more than one argument,
214 the last occurrence dominates.
215 .Pp
216 .It Fl iD Ns Ar sym Ns Op = Ns Ar val
217 .It Fl iU Ns Ar sym
218 C strings, comments,
219 and line continuations
220 are ignored within
221 .Ic #ifdef
222 and
223 .Ic #ifndef
224 blocks
225 controlled by macros
226 specified with these options.
227 .Pp
228 .It Fl f Ar defile
229 The file
230 .Ar defile
231 contains
232 .Ic #define
233 and
234 .Ic #undef
235 preprocessor directives,
236 which have the same effect as the corresponding
237 .Fl D
238 and
239 .Fl U
240 command-line arguments.
241 You can have multiple
242 .Fl f
243 arguments and mix them with
244 .Fl D
245 and
246 .Fl U
247 arguments;
248 later options override earlier ones.
249 .Pp
250 Each directive must be on a single line.
251 Object-like macro definitions (without arguments)
252 are set to the given value.
253 Function-like macro definitions (with arguments)
254 are treated as if they are set to 1.
255 .Pp
256 .It Fl b
257 Replace removed lines with blank lines
258 instead of deleting them.
259 Mutually exclusive with the
260 .Fl B
261 option.
262 .Pp
263 .It Fl B
264 Compress blank lines around a deleted section.
265 Mutually exclusive with the
266 .Fl b
267 option.
268 .Pp
269 .It Fl c
270 Complement,
271 i.e., lines that would have been removed or blanked
272 are retained and vice versa.
273 .Pp
274 .It Fl d
275 Turn on printing of debugging messages.
276 .Pp
277 .It Fl e
278 By default,
279 .Nm
280 will report an error if it needs to remove
281 a preprocessor directive that spans more than one line,
282 for example, if it has a multi-line
283 comment hanging off its right hand end.
284 The
285 .Fl e
286 flag makes it ignore the line instead.
287 .Pp
288 .It Fl h
289 Print help.
290 .Pp
291 .It Fl I Ns Ar path
292 Specifies to
293 .Nm unifdefall
294 an additional place to look for
295 .Ic #include
296 files.
297 This option is ignored by
298 .Nm
299 for compatibility with
300 .Xr cpp 1
301 and to simplify the implementation of
302 .Nm unifdefall .
303 .Pp
304 .It Fl K
305 Always treat the result of
306 .Ic &&
307 and
308 .Ic ||
309 operators as unknown if either operand is unknown,
310 instead of short-circuiting when unknown operands can't affect the result.
311 This option is for compatibility with older versions of
312 .Nm .
313 .Pp
314 .It Fl k
315 Process
316 .Ic #if
317 and
318 .Ic #elif
319 lines with constant expressions.
320 By default, sections controlled by such lines are passed through unchanged
321 because they typically start
322 .Dq Li "#if 0"
323 and are used as a kind of comment to sketch out future or past development.
324 It would be rude to strip them out, just as it would be for normal comments.
325 .Pp
326 .It Fl m
327 Modify one or more input files in place.
328 .Pp
329 .It Fl M Ar backext
330 Modify input files in place, and keep backups of the original files by
331 appending the
332 .Ar backext
333 to the input filenames.
334 .Pp
335 .It Fl n
336 Add
337 .Li #line
338 directives to the output following any deleted lines,
339 so that errors produced when compiling the output file correspond to
340 line numbers in the input file.
341 .Pp
342 .It Fl o Ar outfile
343 Write output to the file
344 .Ar outfile
345 instead of the standard output when processing a single file.
346 .Pp
347 .It Fl s
348 Instead of processing an input file as usual,
349 this option causes
350 .Nm
351 to produce a list of macros that are used in
352 preprocessor directive controlling expressions.
353 .Pp
354 .It Fl S
355 Like the
356 .Fl s
357 option, but the nesting depth of each macro is also printed.
358 This is useful for working out the number of possible combinations
359 of interdependent defined/undefined macros.
360 .Pp
361 .It Fl t
362 Disables parsing for C strings, comments,
363 and line continuations,
364 which is useful
365 for plain text.
366 This is a blanket version of the
367 .Fl iD
368 and
369 .Fl iU
370 flags.
371 .Pp
372 .It Fl V
373 Print version details.
374 .Pp
375 .It Fl x Bro Ar 012 Brc
376 Set exit status mode to zero, one, or two.
377 See the
378 .Sx EXIT STATUS
379 section below for details.
380 .El
381 .Pp
382 The
383 .Nm
384 utility takes its input from
385 .Em stdin
386 if there are no
387 .Ar file
388 arguments.
389 You must use the
390 .Fl m
391 or
392 .Fl M
393 options if there are multiple input files.
394 You can specify inut from stdin or output to stdout with
395 .Ql - .
396 .Pp
397 The
398 .Nm
399 utility works nicely with the
400 .Fl D Ns Ar sym
401 option of
402 .Xr diff 1 .
403 .Sh EXIT STATUS
404 In normal usage the
405 .Nm
406 utility's exit status depends on the mode set using the
407 .Fl x
408 option.
409 .Pp
410 If the exit mode is zero (the default) then
411 .Nm
412 exits with status 0 if the output is an exact copy of the input,
413 or with status 1 if the output differs.
414 .Pp
415 If the exit mode is one,
416 .Nm
417 exits with status 1 if the output is unmodified
418 or 0 if it differs.
419 .Pp
420 If the exit mode is two,
421 .Nm
422 exits with status zero in both cases.
423 .Pp
424 In all exit modes,
425 .Nm
426 exits with status 2 if there is an error.
427 .Pp
428 The exit status is 0 if the
429 .Fl h
430 or
431 .Fl V
432 command line options are given.
433 .Sh DIAGNOSTICS
434 .Bl -item
435 .It
436 Too many levels of nesting.
437 .It
438 Inappropriate
439 .Ic #elif ,
440 .Ic #else
441 or
442 .Ic #endif .
443 .It
444 Obfuscated preprocessor control line.
445 .It
446 Premature
447 .Tn EOF
448 (with the line number of the most recent unterminated
449 .Ic #if ) .
450 .It
451 .Tn EOF
452 in comment.
453 .El
454 .Sh SEE ALSO
455 .Xr cpp 1 ,
456 .Xr diff 1
457 .Pp
458 The unifdef home page is
459 .Pa http://dotat.at/prog/unifdef
460 .Sh HISTORY
461 The
462 .Nm
463 command appeared in
464 .Bx 2.9 .
465 .Tn ANSI\~C
466 support was added in
467 .Fx 4.7 .
468 .Sh AUTHORS
469 The original implementation was written by
470 .An Dave Yost Aq Dave@Yost.com .
471 .An Tony Finch Aq dot@dotat.at
472 rewrote it to support
473 .Tn ANSI\~C .
474 .Sh BUGS
475 Expression evaluation is very limited.
476 .Pp
477 Handling one line at a time means
478 preprocessor directives split across more than one physical line
479 (because of comments or backslash-newline)
480 cannot be handled in every situation.
481 .Pp
482 Trigraphs are not recognized.
483 .Pp
484 There is no support for macros with different definitions at
485 different points in the source file.
486 .Pp
487 The text-mode and ignore functionality does not correspond to modern
488 .Xr cpp 1
489 behaviour.