svn commit: r199842 - head/usr.bin/unifdef

Tony Finch fanf at FreeBSD.org
Thu Nov 26 19:08:36 UTC 2009


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 <jrnieder at gmail.com>

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" "$@"


More information about the svn-src-all mailing list