summaryrefslogtreecommitdiffstats
path: root/usr.bin/unifdef
diff options
context:
space:
mode:
authorfanf <fanf@FreeBSD.org>2009-11-26 19:08:33 +0000
committerfanf <fanf@FreeBSD.org>2009-11-26 19:08:33 +0000
commit59b0d1fa1f1bcdb38058874415f7a8fa282d29d9 (patch)
treef4a75131fd50003eeda2c35ac3dead4b58f46b5e /usr.bin/unifdef
parentac889796667ed236a1f32e354f7372c70ea0f881 (diff)
downloadFreeBSD-src-59b0d1fa1f1bcdb38058874415f7a8fa282d29d9.zip
FreeBSD-src-59b0d1fa1f1bcdb38058874415f7a8fa282d29d9.tar.gz
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@gmail.com>
Diffstat (limited to 'usr.bin/unifdef')
-rw-r--r--usr.bin/unifdef/unifdefall.sh26
1 files changed, 17 insertions, 9 deletions
diff --git a/usr.bin/unifdef/unifdefall.sh b/usr.bin/unifdef/unifdefall.sh
index 60436a0..b443054 100644
--- a/usr.bin/unifdef/unifdefall.sh
+++ b/usr.bin/unifdef/unifdefall.sh
@@ -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" "$@"
OpenPOWER on IntegriCloud