]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/unifdef/unifdef.1
trim(8): Fix a few issues reported by mandoc
[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 - 2015 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 December 3, 2015
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 (unary),
127 .Ic * , / , % , + , - ,
128 .Ic < , <= , > , >= , == , != , & , ^ , \&| ,
129 .Ic && , || ,
130 and parenthesized expressions.
131 Division by zero is treated as an unknown value.
132 A kind of
133 .Dq "short circuit"
134 evaluation is used for the
135 .Ic &&
136 operator:
137 if either operand is definitely false then the result is false,
138 even if the value of the other operand is unknown.
139 Similarly,
140 if either operand of
141 .Ic ||
142 is definitely true then the result is true.
143 .Pp
144 When evaluating an expression,
145 .Nm
146 does not expand macros first.
147 The value of a macro must be a simple number,
148 not an expression.
149 A limited form of indirection is allowed,
150 where one macro's value is the name of another.
151 .Pp
152 In most cases,
153 .Nm
154 does not distinguish between object-like macros
155 (without arguments) and function-like macros (with arguments).
156 A function-like macro invocation can appear in
157 .Ic #if
158 and
159 .Ic #elif
160 control expressions.
161 If the macro is not explicitly defined,
162 or is defined with the
163 .Fl D
164 flag on the command-line,
165 or with
166 .Ic #define
167 in a
168 .Fl f
169 definitions file,
170 its arguments are ignored.
171 If a macro is explicitly undefined on the command line with the
172 .Fl U
173 flag,
174 or with
175 .Ic #undef
176 in a
177 .Fl f
178 definitions file,
179 it may not have any arguments since this leads to a syntax error.
180 .Pp
181 The
182 .Nm
183 utility understands just enough about C
184 to know when one of the directives is inactive
185 because it is inside
186 a comment,
187 or affected by a backslash-continued line.
188 It spots unusually-formatted preprocessor directives
189 and knows when the layout is too odd for it to handle.
190 .Pp
191 A script called
192 .Nm unifdefall
193 can be used to remove all conditional
194 .Xr cpp 1
195 directives from a file.
196 It uses
197 .Nm Fl s
198 and
199 .Nm cpp Fl dM
200 to get lists of all the controlling macros
201 and their definitions (or lack thereof),
202 then invokes
203 .Nm
204 with appropriate arguments to process the file.
205 .Sh OPTIONS
206 .Bl -tag -width indent -compact
207 .It Fl D Ns Ar sym Ns = Ns Ar val
208 Specify that a macro is defined to a given value.
209 .Pp
210 .It Fl D Ns Ar sym
211 Specify that a macro is defined to the value 1.
212 .Pp
213 .It Fl U Ns Ar sym
214 Specify that a macro is undefined.
215 .Pp
216 If the same macro appears in more than one argument,
217 the last occurrence dominates.
218 .Pp
219 .It Fl iD Ns Ar sym Ns Op = Ns Ar val
220 .It Fl iU Ns Ar sym
221 C strings, comments,
222 and line continuations
223 are ignored within
224 .Ic #ifdef
225 and
226 .Ic #ifndef
227 blocks
228 controlled by macros
229 specified with these options.
230 .Pp
231 .It Fl f Ar defile
232 The file
233 .Ar defile
234 contains
235 .Ic #define
236 and
237 .Ic #undef
238 preprocessor directives,
239 which have the same effect as the corresponding
240 .Fl D
241 and
242 .Fl U
243 command-line arguments.
244 You can have multiple
245 .Fl f
246 arguments and mix them with
247 .Fl D
248 and
249 .Fl U
250 arguments;
251 later options override earlier ones.
252 .Pp
253 Each directive must be on a single line.
254 Object-like macro definitions (without arguments)
255 are set to the given value.
256 Function-like macro definitions (with arguments)
257 are treated as if they are set to 1.
258 .Pp
259 .Em Warning:
260 string literals and character constants are not parsed correctly in
261 .Fl f
262 files.
263 .Pp
264 .It Fl b
265 Replace removed lines with blank lines
266 instead of deleting them.
267 Mutually exclusive with the
268 .Fl B
269 option.
270 .Pp
271 .It Fl B
272 Compress blank lines around a deleted section.
273 Mutually exclusive with the
274 .Fl b
275 option.
276 .Pp
277 .It Fl c
278 Complement,
279 i.e., lines that would have been removed or blanked
280 are retained and vice versa.
281 .Pp
282 .It Fl d
283 Turn on printing of debugging messages.
284 .Pp
285 .It Fl e
286 By default,
287 .Nm
288 will report an error if it needs to remove
289 a preprocessor directive that spans more than one line,
290 for example, if it has a multi-line
291 comment hanging off its right hand end.
292 The
293 .Fl e
294 flag makes it ignore the line instead.
295 .Pp
296 .It Fl h
297 Print help.
298 .Pp
299 .It Fl I Ns Ar path
300 Specifies to
301 .Nm unifdefall
302 an additional place to look for
303 .Ic #include
304 files.
305 This option is ignored by
306 .Nm
307 for compatibility with
308 .Xr cpp 1
309 and to simplify the implementation of
310 .Nm unifdefall .
311 .Pp
312 .It Fl K
313 Always treat the result of
314 .Ic &&
315 and
316 .Ic ||
317 operators as unknown if either operand is unknown,
318 instead of short-circuiting when unknown operands can't affect the result.
319 This option is for compatibility with older versions of
320 .Nm .
321 .Pp
322 .It Fl k
323 Process
324 .Ic #if
325 and
326 .Ic #elif
327 lines with constant expressions.
328 By default, sections controlled by such lines are passed through unchanged
329 because they typically start
330 .Dq Li "#if 0"
331 and are used as a kind of comment to sketch out future or past development.
332 It would be rude to strip them out, just as it would be for normal comments.
333 .Pp
334 .It Fl m
335 Modify one or more input files in place.
336 If an input file is not modified,
337 the original is preserved instead of being overwritten with an identical copy.
338 .Pp
339 .It Fl M Ar backext
340 Modify input files in place, and keep backups of the original files by
341 appending the
342 .Ar backext
343 to the input filenames.
344 A zero length
345 .Ar backext
346 behaves the same as the
347 .Fl m
348 option.
349 .Pp
350 .It Fl n
351 Add
352 .Li #line
353 directives to the output following any deleted lines,
354 so that errors produced when compiling the output file correspond to
355 line numbers in the input file.
356 .Pp
357 .It Fl o Ar outfile
358 Write output to the file
359 .Ar outfile
360 instead of the standard output when processing a single file.
361 .Pp
362 .It Fl s
363 Instead of processing an input file as usual,
364 this option causes
365 .Nm
366 to produce a list of macros that are used in
367 preprocessor directive controlling expressions.
368 .Pp
369 .It Fl S
370 Like the
371 .Fl s
372 option, but the nesting depth of each macro is also printed.
373 This is useful for working out the number of possible combinations
374 of interdependent defined/undefined macros.
375 .Pp
376 .It Fl t
377 Disables parsing for C strings, comments,
378 and line continuations,
379 which is useful
380 for plain text.
381 This is a blanket version of the
382 .Fl iD
383 and
384 .Fl iU
385 flags.
386 .Pp
387 .It Fl V
388 Print version details.
389 .Pp
390 .It Fl x Bro Ar 012 Brc
391 Set exit status mode to zero, one, or two.
392 See the
393 .Sx EXIT STATUS
394 section below for details.
395 .El
396 .Pp
397 The
398 .Nm
399 utility takes its input from
400 .Em stdin
401 if there are no
402 .Ar file
403 arguments.
404 You must use the
405 .Fl m
406 or
407 .Fl M
408 options if there are multiple input files.
409 You can specify inut from stdin or output to stdout with
410 .Ql - .
411 .Pp
412 The
413 .Nm
414 utility works nicely with the
415 .Fl D Ns Ar sym
416 option of
417 .Xr diff 1 .
418 .Sh EXIT STATUS
419 In normal usage the
420 .Nm
421 utility's exit status depends on the mode set using the
422 .Fl x
423 option.
424 .Pp
425 If the exit mode is zero (the default) then
426 .Nm
427 exits with status 0 if the output is an exact copy of the input,
428 or with status 1 if the output differs.
429 .Pp
430 If the exit mode is one,
431 .Nm
432 exits with status 1 if the output is unmodified
433 or 0 if it differs.
434 .Pp
435 If the exit mode is two,
436 .Nm
437 exits with status zero in both cases.
438 .Pp
439 In all exit modes,
440 .Nm
441 exits with status 2 if there is an error.
442 .Pp
443 The exit status is 0 if the
444 .Fl h
445 or
446 .Fl V
447 command line options are given.
448 .Sh DIAGNOSTICS
449 .Bl -item
450 .It
451 .Tn EOF
452 in comment
453 .It
454 Inappropriate
455 .Ic #elif ,
456 .Ic #else
457 or
458 .Ic #endif
459 .It
460 Missing macro name in #define or #undef
461 .It
462 Obfuscated preprocessor control line
463 .It
464 Premature
465 .Tn EOF
466 (with the line number of the most recent unterminated
467 .Ic #if )
468 .It
469 Too many levels of nesting
470 .It
471 Unrecognized preprocessor directive
472 .It
473 Unterminated char or string literal
474 .El
475 .Sh SEE ALSO
476 .Xr cpp 1 ,
477 .Xr diff 1
478 .Pp
479 The unifdef home page is
480 .Pa https://dotat.at/prog/unifdef
481 .Sh HISTORY
482 The
483 .Nm
484 command appeared in
485 .Bx 2.9 .
486 .Tn ANSI\~C
487 support was added in
488 .Fx 4.7 .
489 .Sh AUTHORS
490 .An -nosplit
491 The original implementation was written by
492 .An Dave Yost Aq Mt Dave@Yost.com .
493 .An Tony Finch Aq Mt dot@dotat.at
494 rewrote it to support
495 .Tn ANSI\~C .
496 .Sh BUGS
497 Expression evaluation is very limited.
498 .Pp
499 Character constants are not evaluated.
500 String literals and character constants in
501 .Fl f
502 definition files are ignored rather than parsed as
503 part of a macro's replacement tokens.
504 .Pp
505 Handling one line at a time means
506 preprocessor directives split across more than one physical line
507 (because of comments or backslash-newline)
508 cannot be handled in every situation.
509 .Pp
510 Trigraphs are not recognized.
511 .Pp
512 There is no support for macros with different definitions at
513 different points in the source file.
514 .Pp
515 The text-mode and ignore functionality does not correspond to modern
516 .Xr cpp 1
517 behaviour.