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