Removes ifdef lines from a file.
The unifdef command is useful for removing ifdef lines from a file while otherwise leaving the file alone. The unifdef command recognizes nested ifdefs, comments, and single and double quotes of C syntax in order to function correctly, but does not include files or interpret macros. The unifdef command recognizes but does not remove comments.
The unifdef command takes its input from standard input if no File is specified and copies its output to standard output.
Once a Symbol is specified, the lines inside those ifdefs are copied to the output or removed, as appropriate. The ifdef, ifndef, else, elif, and endif lines associated with the symbol are also removed. Ifdefs that involve unspecified symbols are untouched and copied out along with their associated ifdef, else, elif, and endif lines. If the same symbol appears in more than one argument, only the first occurrence is significant. For instance, if an ifdef X occurs nested inside another ifdef X, the inside ifdef is considered an unrecognized symbol.
When using ifdefs to delimit non-C lines such as comments or unfinished code, it is necessary to specify which symbols are to be used for that purpose. Otherwise, the unifdef command will try to parse for quotes and comments in those ifdef lines.
The unifdef command cannot process cpp constructs such as:
#if defined(X) || defined(Y)
OR
#elif X
OR
#elif defined(X) || defined(Y)
Keywords
The following keywords are recognized by the unifdef command:
Item | Description |
---|---|
-c | Complements the operation of the unifdef command. That is, the lines which would have been removed are retained and vice versa. |
-D Symbol | Specifies the symbol to be defined. |
File | Specifies the input source. |
-id Symbol | The unifdef command will not try to recognize comments, single quotes, or double quotes inside specified ifdefs, but these lines will be copied out. |
-iu Symbol | The unifdef command will not try to recognize comments, single quotes, or double quotes inside specified ifdefs. These lines will not be copied out. |
-l | Causes removed lines to be replaced with blank lines instead of being deleted. |
-t | Allows the unifdef command to be used for plain text (instead of C code): the unifdef command will not try to recognize comments, single quotes and double quotes. |
-U Symbol | Specifies the symbol to be undefined. |
This command returns the following exit values:
Item | Description |
---|---|
0 | The output is an exact copy of the input. |
1 | The output is not an exact copy of the input. |
2 | The command failed due to a premature EOF, or to an inappropriate else, elif, or endif. |
unifdef -DA original.c > modified.c
causes
the unifdef command to read the file original.c, and
remove the #ifdef A lines. It then removes everything following an
#elif/#else associated with the #ifdef A, down to the #endif. The
output is placed in the modified.c file.unifdef -UA original.c > modified.c
causes
the unifdef command to read the file original.c, and
remove the #ifdef A down to either its associated #elif//#else, or
its associated #endif. In the case of the #elif, the #elif is replaced
with #if. In the case of #else, the #else is deleted along with its
associated #endif. The output is placed in the modified.c file.Item | Description |
---|---|
/usr/bin/unifdef | Contains the unifdef command. |