unifdef - remove ifdefed lines


unifdef [-clt] [-D sym -U sym -iD sym -iU sym] ...


The unifdef(1) command is useful for removing ifdefed lines from a file, otherwise leaving the file alone.

The unifdef(1) command acts on #ifdef,#ifndef, #else, and #endif lines; it knows only enough about C to discern when one of these is inactive because it is inside a comment, or a single or double quote. Its process of parsing for quotes is very simple: when it finds an open quote, it ignores everything (except escaped quotes) until it finds a close quote; it will not complain if it gets to the end of a line and finds no backslash for continuation.

This version of unifdef(1) has been extended to strip out specified tagged comments. See the -C option for more details

Available options:

If the -c flag is specified, the operation of unifdef(1) is complemented; that is, the lines that would have been removed or blanked are retained and vice versa.
Strip out comments beginning with /* or // and immediately followed by string. For example, the command-line option -C@home will cause all comments beginning with /*@home or //@home to be stripped, including (for /*) multiline comments.
Specify which symbols to define or undefine. The lines inside those ifdefs will be copied to the output or removed as appropriate. The ifdef, ifndef, else, and endif lines associated with sym will also be removed. Ifdefs involving symbols you do not specify and "#if" control lines are untouched and copied out along with their associated ifdef, else, and endif lines. If an ifdef X occurs nested inside another ifdef X, the inside ifdef is treated as if it were an unrecognized symbol. If the same symbol appears in more than one argument, the last occurrence dominates.
Ignore the ifdefs associated with sym. If your C code uses ifdefs to delimit non-C lines, such as comments or code that is under construction, you must tell unifdef(1) which symbols are used for that purpose so that it will not try to parse for quotes and comments inside those ifdefs. Specify ignored ifdefs with -iD sym and -iU sym, similar to -D sym and -U sym above.
Replace removed lines with blank lines instead of deleting them.
Disables parsing for C comments and quotes, which is useful for plain text.

The unifdef(1) command copies its output to stdout and will take its input from stdin if no file argument is given.

The unifdef(1) command works nicely with the -D sym option added to diff(1) as of the 4.1 Berkeley Software Distribution (BSD).


Inappropriate else or endif.

Premature EOF with line numbers of the unterminated #ifdefs.

Exit status is 0 if output is exact copy of input, 1 if not, 2 if trouble.


Should try to deal with "#if" lines.

Does not work correctly if input contains null characters.