From owner-svn-src-all@FreeBSD.ORG Thu Nov 26 19:08:36 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3FE3210656A4; Thu, 26 Nov 2009 19:08:36 +0000 (UTC) (envelope-from fanf@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 140248FC0A; Thu, 26 Nov 2009 19:08:36 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id nAQJ8Zfc090627; Thu, 26 Nov 2009 19:08:35 GMT (envelope-from fanf@svn.freebsd.org) Received: (from fanf@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id nAQJ8ZRP090626; Thu, 26 Nov 2009 19:08:35 GMT (envelope-from fanf@svn.freebsd.org) Message-Id: <200911261908.nAQJ8ZRP090626@svn.freebsd.org> From: Tony Finch Date: Thu, 26 Nov 2009 19:08:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r199842 - head/usr.bin/unifdef X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Nov 2009 19:08:36 -0000 Author: fanf Date: Thu Nov 26 19:08:33 2009 New Revision: 199842 URL: http://svn.freebsd.org/changeset/base/199842 Log: unifdefall: optimise the loop that builds the unifdef command. The old code used a shell loop to convert each controlling macro definition into a command-line argument, reading the macro definitions file each time. The new code converts the list of controlling macros into a sed script which can run through the list of macro definitions in one go. Add some explanatory comments, since the code is quite meta. Use {} instead of () for redirecting a group of commands. Submitted by: Jonathan Nieder Modified: head/usr.bin/unifdef/unifdefall.sh Modified: head/usr.bin/unifdef/unifdefall.sh ============================================================================== --- head/usr.bin/unifdef/unifdefall.sh Thu Nov 26 19:01:19 2009 (r199841) +++ head/usr.bin/unifdef/unifdefall.sh Thu Nov 26 19:08:33 2009 (r199842) @@ -25,7 +25,7 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # -# $dotat: unifdef/unifdefall.sh,v 1.21 2009/11/25 19:54:34 fanf2 Exp $ +# $dotat: unifdef/unifdefall.sh,v 1.24 2009/11/26 12:54:39 fanf2 Exp $ # $FreeBSD$ set -e @@ -36,18 +36,26 @@ trap 'rm -r "$tmp" || exit 1' EXIT export LC_ALL=C +# list of all controlling macros unifdef -s "$@" | sort | uniq >"$tmp/ctrl" +# list of all macro definitions cpp -dM "$@" | sort | sed 's/^#define //' >"$tmp/hashdefs" -sed 's/[^A-Za-z0-9_].*$//' "$tmp/hashdefs" >"$tmp/alldef" +# list of defined macro names +sed 's/[^A-Za-z0-9_].*$//' <"$tmp/hashdefs" >"$tmp/alldef" +# list of undefined and defined controlling macros comm -23 "$tmp/ctrl" "$tmp/alldef" >"$tmp/undef" comm -12 "$tmp/ctrl" "$tmp/alldef" >"$tmp/def" -( - echo unifdef -k \\ - sed 's/.*/-U& \\/' "$tmp/undef" - while read sym - do sed -n 's/^'$sym'\(([^)]*)\)\{0,1\} /-D'$sym'=/p' "$tmp/hashdefs" - done <"$tmp/def" | +# create a sed script that extracts the controlling macro definitions +# and converts them to unifdef command-line arguments +sed 's|.*|s/^&\\(([^)]*)\\)\\{0,1\\} /-D&=/p|' <"$tmp/def" >"$tmp/script" +# create the final unifdef command +{ echo unifdef -k \\ + # convert the controlling undefined macros to -U arguments + sed 's/.*/-U& \\/' <"$tmp/undef" + # convert the controlling defined macros to quoted -D arguments + sed -nf "$tmp/script" <"$tmp/hashdefs" | sed "s/'/'\\\\''/g;s/.*/'&' \\\\/" echo '"$@"' -) >"$tmp/cmd" +} >"$tmp/cmd" +# run the command we just created sh "$tmp/cmd" "$@"