From 09e54d93c10f54b3c17af0c9dff9b8077cca06d6 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 22 Oct 2013 22:50:15 +0200 Subject: [PATCH] lib.sh: Add simple_cpp() to replace "defines". Also add perl_line_filter() which is used by simple_cpp(). --- lib.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib.sh b/lib.sh index 7204024..7bb1d5c 100644 --- a/lib.sh +++ b/lib.sh @@ -73,6 +73,37 @@ sed_i() { cmd_i sed "$@" } +# Usage: perl_line_filter ... +# +# Run the perl command cmd on each line before printing it. +perl_line_filter() { + cmd="$1" + shift + + # Can't use -pe because it uses <> which treats the arguments as files. + perl -e "use strict; use warnings; while () { $cmd; print; }" "$@" +} + +# Usage: simple_cpp .. -- ... +# +# Replaces each FIRST (on word boundaries) with like a +# simple cpp replacement. +simple_cpp() { + cmd='my $i = 0;' + + for x; do + shift + + if test x"$x" = x--; then + break + fi + + cmd="$cmd s/\b$x\b/\$ARGV[\$i]/g; \$i++;" + done + + perl_line_filter "$cmd" -- "$@" +} + # Print the current OS. The following OS are supported at the moment: # - Debian (debian) # - Gentoo (gentoo) -- 2.45.2