Importing usr.bin/unifdef

No Minix-specific changes needed.

Change-Id: Icb828fb19f5f3a411a3f2e9dde35c2da30a04342
This commit is contained in:
Thomas Cort 2013-10-29 16:48:19 -04:00
parent 1e33498f8c
commit b6f0c43626
7 changed files with 1429 additions and 1 deletions

View File

@ -524,6 +524,8 @@
./usr/bin/uname minix-sys
./usr/bin/uncompress minix-sys
./usr/bin/unexpand minix-sys
./usr/bin/unifdef minix-sys
./usr/bin/unifdefall minix-sys
./usr/bin/uniq minix-sys
./usr/bin/units minix-sys
./usr/bin/unlzma minix-sys
@ -2096,6 +2098,8 @@
./usr/man/man1/uname.1 minix-sys
./usr/man/man1/uncompress.1 minix-sys
./usr/man/man1/unexpand.1 minix-sys
./usr/man/man1/unifdef.1 minix-sys
./usr/man/man1/unifdefall.1 minix-sys
./usr/man/man1/uniq.1 minix-sys
./usr/man/man1/units.1 minix-sys
./usr/man/man1/unlzma.1 minix-sys

View File

@ -219,6 +219,7 @@
2012/10/17 12:00:00,usr.bin/tr
2012/10/17 12:00:00,usr.bin/tsort
2012/10/17 12:00:00,usr.bin/tty
2012/10/17 12:00:00,usr.bin/unifdef
2010/10/06 07:59:18,usr.bin/uniq
2013/10/23 12:00:00,usr.bin/units
2012/10/17 12:00:00,usr.bin/unvis

View File

@ -26,7 +26,7 @@ SUBDIR= asa \
shuffle sed seq \
sort split stat su \
tee tic tput \
tr tsort tty unexpand \
tr tsort tty unexpand unifdef \
toproto \
uniq units unvis unzip users \
uuidgen vis \

10
usr.bin/unifdef/Makefile Normal file
View File

@ -0,0 +1,10 @@
# $NetBSD: Makefile,v 1.6 2003/08/01 11:51:14 lukem Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= unifdef
SCRIPTS=unifdefall.sh
MLINKS= unifdef.1 unifdefall.1
.include <bsd.prog.mk>

347
usr.bin/unifdef/unifdef.1 Normal file
View File

@ -0,0 +1,347 @@
.\" $NetBSD: unifdef.1,v 1.12 2009/06/11 03:16:34 ginsbach Exp $
.\"
.\" Copyright (c) 1985, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Dave Yost.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" Portions of this code (support for #if and #elif) are Copyright (c)
.\" 2002, 2003 Tony Finch.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)unifdef.1 8.2 (Berkeley) 4/1/94
.\" $dotat: things/unifdef.1,v 1.45 2003/01/20 14:37:08 fanf2 Exp $
.\" $FreeBSD: src/usr.bin/unifdef/unifdef.1,v 1.19 2003/01/20 12:41:41 fanf Exp $
.\"
.Dd June 5, 2009
.Dt UNIFDEF 1
.Os
.Sh NAME
.Nm unifdef ,
.Nm unifdefall
.Nd remove preprocessor conditionals from code
.Sh SYNOPSIS
.Nm
.Op Fl ceklst
.Op Fl I Ns Ar path
.Op Fl D Ns Ar sym Ns Op = Ns Ar val
.Op Fl U Ns Ar sym
.Op Fl iD Ns Ar sym Ns Op = Ns Ar val
.Op Fl iU Ns Ar sym
.Ar ...
.Op Fl o Ar output
.Op Ar file
.Nm unifdefall
.Op Fl I Ns Ar path
.Ar ...
.Ar file
.Sh DESCRIPTION
The
.Nm
utility selectively processes conditional
.Xr cpp 1
directives.
It removes from a file both the directives and any additional text
that they specify should be removed, while otherwise leaving the
file alone.
.Pp
The
.Nm
utility acts on
.Ic #if , #ifdef , #ifndef , #elif , #else ,
and
.Ic #endif
lines,
and it understands only the commonly-used subset
of the expression syntax for
.Ic #if
and
.Ic #elif
lines.
It handles
integer values of symbols defined on the command line,
the
.Fn defined
operator applied to symbols defined or undefined on the command line,
the operators
.Ic \&! , \*[Lt] , \*[Gt] , \*[Lt]= , \*[Gt]= , == , != , \*[Am]\*[Am] , || ,
and parenthesized expressions.
Anything that it does not understand is passed through unharmed.
It only processes
.Ic #ifdef
and
.Ic #ifndef
directives if the symbol is specified on the command line,
otherwise they are also passed through unchanged.
By default, it ignores
.Ic #if
and
.Ic #elif
lines with constant expressions,
or they may be processed by specifying the
.Fl k
flag on the command line.
.Pp
The
.Nm
utility also understands just enough about C
to know when one of the directives is inactive
because it is inside
a comment,
or affected by a backslash-continued line.
It spots unusually-formatted preprocessor directives
and knows when the layout is too odd to handle.
.Pp
A script called
.Nm unifdefall
can be used to remove all conditional
.Xr cpp 1
directives from a file.
It uses
.Nm Fl s
and
.Nm cpp Fl dM
to get lists of all the controlling symbols
and their definitions (or lack thereof),
then invokes
.Nm
with appropriate arguments to process the file.
.Pp
Available options:
.Bl -tag -width indent -compact
.It Fl D Ns Ar sym Ns Op = Ns Ar val
Specify that a symbol is defined,
and optionally specify what value to give it
for the purpose of handling
.Ic #if
and
.Ic #elif
directives.
.It Fl U Ns Ar sym
Specify that a symbol is undefined.
If the same symbol appears in more than one argument,
the last occurrence dominates.
.It Fl c
If the
.Fl c
flag is specified,
then the operation of
.Nm
is complemented,
i.e., the lines that would have been removed or blanked
are retained and vice versa.
.It Fl e
Because
.Nm
processes its input one line at a time,
it cannot remove preprocessor directives that span more than one line.
The most common example of this is a directive with a multi-line
comment hanging off its right hand end.
By default,
if
.Nm
has to process such a directive,
it will complain that the line is too obfuscated.
The
.Fl e
option changes the behaviour so that,
where possible,
such lines are left unprocessed instead of reporting an error.
.It Fl k
Process
.Ic #if
and
.Ic #elif
lines with constant expressions.
By default, sections controlled by such lines are passed through unchanged
because they typically start
.Dq Li "#if 0"
and are used as a kind of comment to sketch out future or past development.
It would be rude to strip them out, just as it would be for normal comments.
.It Fl l
Replace removed lines with blank lines
instead of deleting them.
.It Fl o Ar output
The argument given is the name of an
.Ar output
file to be used instead of the standard output.
This file can be the same as the input file.
.It Fl s
Instead of processing the input file as usual,
this option causes
.Nm
to produce a list of symbols that appear in expressions
that
.Nm
understands.
It is useful in conjunction with the
.Fl dM
option of
.Xr cpp 1
for creating
.Nm
command lines.
.It Fl t
Disables parsing for C comments
and line continuations,
which is useful
for plain text.
.It Fl iD Ns Ar sym Ns Op = Ns Ar val
.It Fl iU Ns Ar sym
Ignore
.Ic #ifdef Ns s .
If your C code uses
.Ic #ifdef Ns s
to delimit non-C lines,
such as comments
or code which is under construction,
then you must tell
.Nm
which symbols are used for that purpose so that it will not try to parse
comments
and line continuations
inside those
.Ic #ifdef Ns s .
One specifies ignored symbols with
.Fl iD Ns Ar sym Ns Oo = Ns Ar val Oc
and
.Fl iU Ns Ar sym
similar to
.Fl D Ns Ar sym Ns Op = Ns Ar val
and
.Fl U Ns Ar sym
above.
.It Fl I Ns Ar path
Specifies to
.Nm unifdefall
an additional place to look for
.Ic #include
files.
This option is ignored by
.Nm
for compatibility with
.Xr cpp 1
and to simplify the implementation of
.Nm unifdefall .
.El
.Pp
The
.Nm
utility copies its output to
.Em stdout
and will take its input from
.Em stdin
if no
.Ar file
argument is given.
.Pp
The
.Nm
utility works nicely with the
.Fl D Ns Ar sym
option of
.Xr diff 1 .
.Sh DIAGNOSTICS
.Bl -item
.It
Too many levels of nesting.
.It
Inappropriate
.Ic #elif ,
.Ic #else
or
.Ic #endif .
.It
Obfuscated preprocessor control line.
.It
Premature
.Dv EOF
(with the line number of the most recent unterminated
.Ic #if ) .
.It
.Dv EOF
in comment.
.El
.Pp
The
.Nm
utility exits 0 if the output is an exact copy of the input,
1 if not, and 2 if in trouble.
.Sh SEE ALSO
.Xr cpp 1 ,
.Xr diff 1
.Sh HISTORY
The
.Nm
command appeared in
.Bx 4.3 .
.Tn ANSI\~C
support was added in
.Fx 4.7 .
.Sh BUGS
Expression evaluation is very limited.
.Pp
Preprocessor control lines split across more than one physical line
(because of comments or backslash-newline)
cannot be handled in every situation.
.Pp
Trigraphs are not recognized.
.Pp
There is no support for symbols with different definitions at
different points in the source file.
.Pp
The text-mode and ignore functionality does not correspond to modern
.Xr cpp 1
behaviour.

1037
usr.bin/unifdef/unifdef.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
#!/bin/sh
#
# remove all the #if's from a source file
#
# $dotat: things/unifdefall.sh,v 1.10 2002/09/24 19:52:11 fanf2 Exp $
# $FreeBSD: src/usr.bin/unifdef/unifdefall.sh,v 1.2 2002/09/24 19:50:03 fanf Exp $
set -e
basename=`basename $0`
tmp=`mktemp -d -t $basename` || exit 2
unifdef -s "$@" | sort | uniq > $tmp/ctrl
cpp -dM "$@" | sort |
sed -Ee 's/^#define[ ]+(.*[^ ])[ ]*$/\1/' > $tmp/hashdefs
sed -Ee 's/^([A-Za-z0-9_]+).*$/\1/' $tmp/hashdefs > $tmp/alldef
comm -23 $tmp/ctrl $tmp/alldef > $tmp/undef
comm -12 $tmp/ctrl $tmp/alldef > $tmp/def
echo unifdef -k \\ > $tmp/cmd
sed -Ee 's/^(.*)$/-U\1 \\/' $tmp/undef >> $tmp/cmd
while read sym
do sed -Ee '/^('"$sym"')([(][^)]*[)])?([ ]+(.*))?$/!d;s//-D\1=\4/' $tmp/hashdefs
done < $tmp/def |
sed -Ee 's/\\/\\\\/g;s/"/\\"/g;s/^/"/;s/$/" \\/' >> $tmp/cmd
echo '"$@"' >> $tmp/cmd
sh $tmp/cmd "$@"
rm -r $tmp