From nobody Thu Sep 07 14:51:05 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RhMcq2BvSz4snkD; Thu, 7 Sep 2023 14:51:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RhMcp0lrmz4l5R; Thu, 7 Sep 2023 14:51:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694098266; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=x1egAy6q5VZ6x3u6fGUmL8FGpsBtu94W7GaCUmq24qc=; b=K8PikLkAiwFtXXRYeuBbOwOhZc4qgxQprR/QzWOQV+0WLvG0JJ8258z6IT+79HPm9jHYIU gsJRFuMfM15BqMiSo+PJR2VKASUmJY5A9HrhE7o2rKOW7G6q7VVXV+VrJETJieK4/tnWtf rRZS9lMzTNKVBM2p+nqaZ0PXVK6a6Hl4TamAgnhMqYSwHpVnDAgNXlPbP7LhIOkZ/pGgxZ KRw48YQGsfzzZTBMiR2J/SEP9RI2Zp5LBQbaFdl8+Q5mlvhLFSIQMT9LCIAOCubtk9Lm3N cON2WdM5jEyeDimpE+U3+I/k1vNz14hqnftT4w5E3zhzNO8+SU+Rs6goQw5rew== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694098266; a=rsa-sha256; cv=none; b=CuACZ3jC2UV7i2agazFvefptpXC/UWX9CctlkW5gsMGXxIPMv+QxSX/3FRLzp0k69dhg7f Vp/t0l0vceFaTH5Ja3XgLX/byUexrz/4dzdfOCLrJGB13clV+Jn3mPlEZRWwi73JRm+ypc 2Dv1G6sm6FOP9B73jkaVhuXwKbsLcJ6wbw1RPq2yl0aGAjQWZMafVLzRLpJcksTwKtQSZ3 k9cMptGYOHd+Lt01WVh8iO6ys9rhEZ45629GKmcwj1Vd1ERDbeG0qVOkhqnYpRgzhjuhiw yeDvjKMoUdGueGxwHLkx5MyRcS+sTkn43AjeMa2ZMsYAdhUQdW9i4Gt/BD76MA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694098266; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=x1egAy6q5VZ6x3u6fGUmL8FGpsBtu94W7GaCUmq24qc=; b=xZtZ8tL869HM91fSwHZyU0SzMidb4eWaFKBPbwIcThpHND9V+qgAf02CopRJKNaCzSOKbu cjem+hLwenxuaUld5WCNZi37V+kDTdYA1Gh5L6J8OvhfQwALqcVxNMgFyNPYLL7Q8q3Jzw vqIjl2EAHA0mmv0BBXgD5Y+g1fvTmnDpAV5rxNlIzzg2+LMgHkO7ph+UspcROHOs8moMWJ ZHMQB4vasLfSLuk8w6W/xxHC9FFytpS6phFFKho5mCP9yJiWSHDpJQZx44zf6ldrYVZxoP D5Yh1eHjVaWYsPx3N0uTTN7OIW9kASli0ko2KR0x2Pk4R4v2MDvbwyPMW+oUWg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RhMcn6t2qz13tB; Thu, 7 Sep 2023 14:51:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 387Ep5LR013104; Thu, 7 Sep 2023 14:51:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 387Ep5GL013101; Thu, 7 Sep 2023 14:51:05 GMT (envelope-from git) Date: Thu, 7 Sep 2023 14:51:05 GMT Message-Id: <202309071451.387Ep5GL013101@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: d194776d7df1 - stable/13 - unifdef: Vendorize. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d194776d7df15b4932881f8327cd8d8b45c84d8f Auto-Submitted: auto-generated The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=d194776d7df15b4932881f8327cd8d8b45c84d8f commit d194776d7df15b4932881f8327cd8d8b45c84d8f Author: Dag-Erling Smørgrav AuthorDate: 2023-08-21 17:49:23 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-09-07 08:59:37 +0000 unifdef: Vendorize. (cherry picked from commit 7addfafe73e077891f003b7cc7a62cfb434f92ec) unifdef: Handle redefined symbols correctly. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D41758 (cherry picked from commit aacbe7384221d2eafa326864bbbe2f22a10063ce) --- contrib/unifdef/unifdef.c | 6 +- usr.bin/unifdef/Makefile | 6 +- usr.bin/unifdef/tests/unifdef_test.sh | 21 + usr.bin/unifdef/unifdef.1 | 515 ---------- usr.bin/unifdef/unifdef.c | 1719 --------------------------------- usr.bin/unifdef/unifdefall.sh | 61 -- 6 files changed, 29 insertions(+), 2299 deletions(-) diff --git a/contrib/unifdef/unifdef.c b/contrib/unifdef/unifdef.c index 82f6acd66038..3dd4ace7b81e 100644 --- a/contrib/unifdef/unifdef.c +++ b/contrib/unifdef/unifdef.c @@ -1550,8 +1550,12 @@ addsym2(bool ignorethis, const char *symname, const char *val) sym->value = val; r = RB_INSERT(MACROMAP, ¯o_tree, sym); assert(r == NULL); + debugsym("addsym", sym); + } else { + sym->ignore = ignorethis; + sym->value = val; + debugsym("updsym", sym); } - debugsym("addsym", sym); } static void diff --git a/usr.bin/unifdef/Makefile b/usr.bin/unifdef/Makefile index 9c2e081b3ba9..82b0c5839f8e 100644 --- a/usr.bin/unifdef/Makefile +++ b/usr.bin/unifdef/Makefile @@ -1,11 +1,11 @@ -# @(#)Makefile 8.1 (Berkeley) 6/6/93 - -.include +.PATH: ${SRCTOP}/contrib/unifdef PROG= unifdef SCRIPTS=unifdefall.sh MLINKS= unifdef.1 unifdefall.1 +.include + HAS_TESTS= SUBDIR.${MK_TESTS}+= tests diff --git a/usr.bin/unifdef/tests/unifdef_test.sh b/usr.bin/unifdef/tests/unifdef_test.sh index 98adef3adfdc..dfb08c187724 100644 --- a/usr.bin/unifdef/tests/unifdef_test.sh +++ b/usr.bin/unifdef/tests/unifdef_test.sh @@ -17,6 +17,27 @@ EOF atf_check -o file:f unifdef file <. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Dave Yost. It was rewritten to support ANSI C by 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. 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. -.\" -.Dd December 3, 2015 -.Dt UNIFDEF 1 PRM -.Os " " -.Sh NAME -.Nm unifdef , unifdefall -.Nd remove preprocessor conditionals from code -.Sh SYNOPSIS -.Nm -.Op Fl bBcdehKkmnsStV -.Op Fl I Ns Ar path -.Op Fl [i]D Ns Ar sym Ns Op = Ns Ar val -.Op Fl [i]U Ns Ar sym -.Ar ... -.Op Fl f Ar defile -.Op Fl x Bro Ar 012 Brc -.Op Fl M Ar backext -.Op Fl o Ar outfile -.Op Ar infile ... -.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 , -.Ic #elif , #else , -and -.Ic #endif -lines, -using macros specified in -.Fl D -and -.Fl U -command line options or in -.Fl f -definitions files. -A directive is processed -if the macro specifications are sufficient to provide -a definite value for its control expression. -If the result is false, -the directive and the following lines under its control are removed. -If the result is true, -only the directive is removed. -An -.Ic #ifdef -or -.Ic #ifndef -directive is passed through unchanged -if its controlling macro is not specified. -Any -.Ic #if -or -.Ic #elif -control expression that has an unknown value or that -.Nm -cannot parse is passed through unchanged. -By default, -.Nm -ignores -.Ic #if -and -.Ic #elif -lines with constant expressions; -it can be told to process them by specifying the -.Fl k -flag on the command line. -.Pp -It understands a commonly-used subset -of the expression syntax for -.Ic #if -and -.Ic #elif -lines: -integer constants, -integer values of macros defined on the command line, -the -.Fn defined -operator, -the operators -.Ic \&! , ~ , - -(unary), -.Ic * , / , % , + , - , -.Ic < , <= , > , >= , == , != , & , ^ , \&| , -.Ic && , || , -and parenthesized expressions. -Division by zero is treated as an unknown value. -A kind of -.Dq "short circuit" -evaluation is used for the -.Ic && -operator: -if either operand is definitely false then the result is false, -even if the value of the other operand is unknown. -Similarly, -if either operand of -.Ic || -is definitely true then the result is true. -.Pp -When evaluating an expression, -.Nm -does not expand macros first. -The value of a macro must be a simple number, -not an expression. -A limited form of indirection is allowed, -where one macro's value is the name of another. -.Pp -In most cases, -.Nm -does not distinguish between object-like macros -(without arguments) and function-like macros (with arguments). -A function-like macro invocation can appear in -.Ic #if -and -.Ic #elif -control expressions. -If the macro is not explicitly defined, -or is defined with the -.Fl D -flag on the command-line, -or with -.Ic #define -in a -.Fl f -definitions file, -its arguments are ignored. -If a macro is explicitly undefined on the command line with the -.Fl U -flag, -or with -.Ic #undef -in a -.Fl f -definitions file, -it may not have any arguments since this leads to a syntax error. -.Pp -The -.Nm -utility 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 for it 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 macros -and their definitions (or lack thereof), -then invokes -.Nm -with appropriate arguments to process the file. -.Sh OPTIONS -.Bl -tag -width indent -compact -.It Fl D Ns Ar sym Ns = Ns Ar val -Specify that a macro is defined to a given value. -.Pp -.It Fl D Ns Ar sym -Specify that a macro is defined to the value 1. -.Pp -.It Fl U Ns Ar sym -Specify that a macro is undefined. -.Pp -If the same macro appears in more than one argument, -the last occurrence dominates. -.Pp -.It Fl iD Ns Ar sym Ns Op = Ns Ar val -.It Fl iU Ns Ar sym -C strings, comments, -and line continuations -are ignored within -.Ic #ifdef -and -.Ic #ifndef -blocks -controlled by macros -specified with these options. -.Pp -.It Fl f Ar defile -The file -.Ar defile -contains -.Ic #define -and -.Ic #undef -preprocessor directives, -which have the same effect as the corresponding -.Fl D -and -.Fl U -command-line arguments. -You can have multiple -.Fl f -arguments and mix them with -.Fl D -and -.Fl U -arguments; -later options override earlier ones. -.Pp -Each directive must be on a single line. -Object-like macro definitions (without arguments) -are set to the given value. -Function-like macro definitions (with arguments) -are treated as if they are set to 1. -.Pp -.Em Warning: -string literals and character constants are not parsed correctly in -.Fl f -files. -.Pp -.It Fl b -Replace removed lines with blank lines -instead of deleting them. -Mutually exclusive with the -.Fl B -option. -.Pp -.It Fl B -Compress blank lines around a deleted section. -Mutually exclusive with the -.Fl b -option. -.Pp -.It Fl c -Complement, -i.e., lines that would have been removed or blanked -are retained and vice versa. -.Pp -.It Fl d -Turn on printing of debugging messages. -.Pp -.It Fl e -By default, -.Nm -will report an error if it needs to remove -a preprocessor directive that spans more than one line, -for example, if it has a multi-line -comment hanging off its right hand end. -The -.Fl e -flag makes it ignore the line instead. -.Pp -.It Fl h -Print help. -.Pp -.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 . -.Pp -.It Fl K -Always treat the result of -.Ic && -and -.Ic || -operators as unknown if either operand is unknown, -instead of short-circuiting when unknown operands can't affect the result. -This option is for compatibility with older versions of -.Nm . -.Pp -.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. -.Pp -.It Fl m -Modify one or more input files in place. -If an input file is not modified, -the original is preserved instead of being overwritten with an identical copy. -.Pp -.It Fl M Ar backext -Modify input files in place, and keep backups of the original files by -appending the -.Ar backext -to the input filenames. -A zero length -.Ar backext -behaves the same as the -.Fl m -option. -.Pp -.It Fl n -Add -.Li #line -directives to the output following any deleted lines, -so that errors produced when compiling the output file correspond to -line numbers in the input file. -.Pp -.It Fl o Ar outfile -Write output to the file -.Ar outfile -instead of the standard output when processing a single file. -.Pp -.It Fl s -Instead of processing an input file as usual, -this option causes -.Nm -to produce a list of macros that are used in -preprocessor directive controlling expressions. -.Pp -.It Fl S -Like the -.Fl s -option, but the nesting depth of each macro is also printed. -This is useful for working out the number of possible combinations -of interdependent defined/undefined macros. -.Pp -.It Fl t -Disables parsing for C strings, comments, -and line continuations, -which is useful -for plain text. -This is a blanket version of the -.Fl iD -and -.Fl iU -flags. -.Pp -.It Fl V -Print version details. -.Pp -.It Fl x Bro Ar 012 Brc -Set exit status mode to zero, one, or two. -See the -.Sx EXIT STATUS -section below for details. -.El -.Pp -The -.Nm -utility takes its input from -.Em stdin -if there are no -.Ar file -arguments. -You must use the -.Fl m -or -.Fl M -options if there are multiple input files. -You can specify inut from stdin or output to stdout with -.Ql - . -.Pp -The -.Nm -utility works nicely with the -.Fl D Ns Ar sym -option of -.Xr diff 1 . -.Sh EXIT STATUS -In normal usage the -.Nm -utility's exit status depends on the mode set using the -.Fl x -option. -.Pp -If the exit mode is zero (the default) then -.Nm -exits with status 0 if the output is an exact copy of the input, -or with status 1 if the output differs. -.Pp -If the exit mode is one, -.Nm -exits with status 1 if the output is unmodified -or 0 if it differs. -.Pp -If the exit mode is two, -.Nm -exits with status zero in both cases. -.Pp -In all exit modes, -.Nm -exits with status 2 if there is an error. -.Pp -The exit status is 0 if the -.Fl h -or -.Fl V -command line options are given. -.Sh DIAGNOSTICS -.Bl -item -.It -.Tn EOF -in comment -.It -Inappropriate -.Ic #elif , -.Ic #else -or -.Ic #endif -.It -Missing macro name in #define or #undef -.It -Obfuscated preprocessor control line -.It -Premature -.Tn EOF -(with the line number of the most recent unterminated -.Ic #if ) -.It -Too many levels of nesting -.It -Unrecognized preprocessor directive -.It -Unterminated char or string literal -.El -.Sh SEE ALSO -.Xr cpp 1 , -.Xr diff 1 -.Pp -The unifdef home page is -.Pa https://dotat.at/prog/unifdef -.Sh HISTORY -The -.Nm -command appeared in -.Bx 2.9 . -.Tn ANSI\~C -support was added in -.Fx 4.7 . -.Sh AUTHORS -.An -nosplit -The original implementation was written by -.An Dave Yost Aq Mt Dave@Yost.com . -.An Tony Finch Aq Mt dot@dotat.at -rewrote it to support -.Tn ANSI\~C . -.Sh BUGS -Expression evaluation is very limited. -.Pp -Character constants are not evaluated. -String literals and character constants in -.Fl f -definition files are ignored rather than parsed as -part of a macro's replacement tokens. -.Pp -Handling one line at a time means -preprocessor directives 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 macros 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. diff --git a/usr.bin/unifdef/unifdef.c b/usr.bin/unifdef/unifdef.c deleted file mode 100644 index 598c66e3ab06..000000000000 --- a/usr.bin/unifdef/unifdef.c +++ /dev/null @@ -1,1719 +0,0 @@ -/*- - * SPDX-License-Identifier: BSD-2-Clause - * - * Copyright (c) 2002 - 2015 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. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 - remove ifdef'ed lines - * - * This code was derived from software contributed to Berkeley by Dave Yost. - * It was rewritten to support ANSI C by Tony Finch. The original version - * of unifdef carried the 4-clause BSD copyright licence. None of its code - * remains in this version (though some of the names remain) so it now - * carries a more liberal licence. - * - * Wishlist: - * provide an option which will append the name of the - * appropriate symbol after #else's and #endif's - * provide an option which will check symbols after - * #else's and #endif's to see that they match their - * corresponding #ifdef or #ifndef - * - * These require better buffer handling, which would also make - * it possible to handle all "dodgy" directives correctly. - */ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static const char copyright[] = - "@(#) $Version: unifdef-2.11 $\n" - "@(#) $FreeBSD$\n" - "@(#) $Author: Tony Finch (dot@dotat.at) $\n" - "@(#) $URL: https://dotat.at/prog/unifdef $\n" -; - -/* types of input lines: */ -typedef enum { - LT_TRUEI, /* a true #if with ignore flag */ - LT_FALSEI, /* a false #if with ignore flag */ - LT_IF, /* an unknown #if */ - LT_TRUE, /* a true #if */ - LT_FALSE, /* a false #if */ - LT_ELIF, /* an unknown #elif */ - LT_ELTRUE, /* a true #elif */ - LT_ELFALSE, /* a false #elif */ - LT_ELSE, /* #else */ - LT_ENDIF, /* #endif */ - LT_DODGY, /* flag: directive is not on one line */ - LT_DODGY_LAST = LT_DODGY + LT_ENDIF, - LT_PLAIN, /* ordinary line */ - LT_EOF, /* end of file */ - LT_ERROR, /* unevaluable #if */ - LT_COUNT -} Linetype; - -static char const * const linetype_name[] = { - "TRUEI", "FALSEI", "IF", "TRUE", "FALSE", - "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF", - "DODGY TRUEI", "DODGY FALSEI", - "DODGY IF", "DODGY TRUE", "DODGY FALSE", - "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE", - "DODGY ELSE", "DODGY ENDIF", - "PLAIN", "EOF", "ERROR" -}; - -#define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF)) -#define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY)) - -/* state of #if processing */ -typedef enum { - IS_OUTSIDE, - IS_FALSE_PREFIX, /* false #if followed by false #elifs */ - IS_TRUE_PREFIX, /* first non-false #(el)if is true */ - IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */ - IS_FALSE_MIDDLE, /* a false #elif after a pass state */ - IS_TRUE_MIDDLE, /* a true #elif after a pass state */ - IS_PASS_ELSE, /* an else after a pass state */ - IS_FALSE_ELSE, /* an else after a true state */ - IS_TRUE_ELSE, /* an else after only false states */ - IS_FALSE_TRAILER, /* #elifs after a true are false */ - IS_COUNT -} Ifstate; - -static char const * const ifstate_name[] = { - "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX", - "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE", - "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE", - "FALSE_TRAILER" -}; - -/* state of comment parser */ -typedef enum { - NO_COMMENT = false, /* outside a comment */ - C_COMMENT, /* in a comment like this one */ - CXX_COMMENT, /* between // and end of line */ - STARTING_COMMENT, /* just after slash-backslash-newline */ - FINISHING_COMMENT, /* star-backslash-newline in a C comment */ - CHAR_LITERAL, /* inside '' */ - STRING_LITERAL /* inside "" */ -} Comment_state; - -static char const * const comment_name[] = { - "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING" -}; - -/* state of preprocessor line parser */ -typedef enum { - LS_START, /* only space and comments on this line */ - LS_HASH, /* only space, comments, and a hash */ - LS_DIRTY /* this line can't be a preprocessor line */ -} Line_state; - -static char const * const linestate_name[] = { - "START", "HASH", "DIRTY" -}; - -/* - * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1 - */ -#define MAXDEPTH 64 /* maximum #if nesting */ -#define MAXLINE 4096 /* maximum length of line */ - -/* - * Sometimes when editing a keyword the replacement text is longer, so - * we leave some space at the end of the tline buffer to accommodate this. - */ -#define EDITSLOP 10 - -/* - * C17/18 allow 63 characters per macro name, but up to 127 arbitrarily large - * parameters. - */ -struct macro { - RB_ENTRY(macro) entry; - const char *name; - const char *value; - bool ignore; /* -iDsym or -iUsym */ -}; - -static int -macro_cmp(struct macro *a, struct macro *b) -{ - return (strcmp(a->name, b->name)); -} - -static RB_HEAD(MACROMAP, macro) macro_tree = RB_INITIALIZER(¯o_tree); -RB_GENERATE_STATIC(MACROMAP, macro, entry, macro_cmp); - -/* - * Globals. - */ - -static bool compblank; /* -B: compress blank lines */ -static bool lnblank; /* -b: blank deleted lines */ -static bool complement; /* -c: do the complement */ -static bool debugging; /* -d: debugging reports */ -static bool inplace; /* -m: modify in place */ -static bool iocccok; /* -e: fewer IOCCC errors */ -static bool strictlogic; /* -K: keep ambiguous #ifs */ -static bool killconsts; /* -k: eval constant #ifs */ -static bool lnnum; /* -n: add #line directives */ -static bool symlist; /* -s: output symbol list */ -static bool symdepth; /* -S: output symbol depth */ -static bool text; /* -t: this is a text file */ - -static FILE *input; /* input file pointer */ -static const char *filename; /* input file name */ -static int linenum; /* current line number */ -static const char *linefile; /* file name for #line */ -static FILE *output; /* output file pointer */ -static const char *ofilename; /* output file name */ -static const char *backext; /* backup extension */ -static char *tempname; /* avoid splatting input */ - -static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */ -static char *keyword; /* used for editing #elif's */ - -/* - * When processing a file, the output's newline style will match the - * input's, and unifdef correctly handles CRLF or LF endings whatever - * the platform's native style. The stdio streams are opened in binary - * mode to accommodate platforms whose native newline style is CRLF. - * When the output isn't a processed input file (when it is error / - * debug / diagnostic messages) then unifdef uses native line endings. - */ - -static const char *newline; /* input file format */ -static const char newline_unix[] = "\n"; -static const char newline_crlf[] = "\r\n"; - -static Comment_state incomment; /* comment parser state */ -static Line_state linestate; /* #if line parser state */ -static Ifstate ifstate[MAXDEPTH]; /* #if processor state */ -static bool ignoring[MAXDEPTH]; /* ignore comments state */ -static int stifline[MAXDEPTH]; /* start of current #if */ -static int depth; /* current #if nesting */ -static int delcount; /* count of deleted lines */ -static unsigned blankcount; /* count of blank lines */ -static unsigned blankmax; /* maximum recent blankcount */ -static bool constexpr; /* constant #if expression */ -static bool zerosyms; /* to format symdepth output */ -static bool firstsym; /* ditto */ - -static int exitmode; /* exit status mode */ -static int exitstat; /* program exit status */ -static bool altered; /* was this file modified? */ - -static void addsym1(bool, bool, char *); -static void addsym2(bool, const char *, const char *); -static char *astrcat(const char *, const char *); -static void cleantemp(void); -static void closeio(void); -static void debug(const char *, ...); -static void debugsym(const char *, const struct macro *); -static bool defundef(void); -static void defundefile(const char *); -static void done(void); -static void error(const char *); -static struct macro *findsym(const char **); -static void flushline(bool); -static void hashline(void); -static void help(void); -static Linetype ifeval(const char **); -static void ignoreoff(void); -static void ignoreon(void); -static void indirectsym(void); -static void keywordedit(const char *); -static const char *matchsym(const char *, const char *); -static void nest(void); -static Linetype parseline(void); -static void process(void); -static void processinout(const char *, const char *); -static const char *skipargs(const char *); -static const char *skipcomment(const char *); -static const char *skiphash(void); -static const char *skipline(const char *); -static const char *skipsym(const char *); -static void state(Ifstate); -static void unnest(void); -static void usage(void); -static void version(void); -static const char *xstrdup(const char *, const char *); - -#define endsym(c) (!isalnum((unsigned char)c) && c != '_') - -static FILE * -mktempmode(char *tmp, int mode) -{ - int rc, fd; - - mode &= (S_IRWXU|S_IRWXG|S_IRWXO); - fd = mkstemp(tmp); - if (fd < 0) - err(2, "can't create %s", tmp); - rc = fchmod(fd, mode); - if (rc < 0) - err(2, "can't fchmod %s mode=0o%o", tmp, mode); - return (fdopen(fd, "wb")); -} - -/* - * The main program. - */ -int -main(int argc, char *argv[]) -{ - int opt; - - while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1) - switch (opt) { - case 'i': /* treat stuff controlled by these symbols as text */ - /* - * For strict backwards-compatibility the U or D - * should be immediately after the -i but it doesn't - * matter much if we relax that requirement. - */ - opt = *optarg++; - if (opt == 'D') - addsym1(true, true, optarg); - else if (opt == 'U') - addsym1(true, false, optarg); - else - usage(); - break; - case 'D': /* define a symbol */ - addsym1(false, true, optarg); - break; - case 'U': /* undef a symbol */ - addsym1(false, false, optarg); - break; - case 'I': /* no-op for compatibility with cpp */ - break; - case 'b': /* blank deleted lines instead of omitting them */ - case 'l': /* backwards compatibility */ - lnblank = true; - break; - case 'B': /* compress blank lines around removed section */ - compblank = true; - break; - case 'c': /* treat -D as -U and vice versa */ - complement = true; - break; - case 'd': - debugging = true; - break; - case 'e': /* fewer errors from dodgy lines */ - iocccok = true; - break; - case 'f': /* definitions file */ - defundefile(optarg); - break; - case 'h': - help(); - break; - case 'K': /* keep ambiguous #ifs */ - strictlogic = true; - break; - case 'k': /* process constant #ifs */ - killconsts = true; - break; - case 'm': /* modify in place */ - inplace = true; - break; - case 'M': /* modify in place and keep backup */ - inplace = true; - if (strlen(optarg) > 0) - backext = optarg; *** 1426 LINES SKIPPED ***