"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Christian Weisgerber <naddy@mips.inka.de>
Subject:
Re: regress: Replace sed -i with ed for portability
To:
Omar Polo <op@omarpolo.com>
Cc:
gameoftrees@openbsd.org
Date:
Sun, 5 Mar 2023 18:15:05 +0100

Download raw body.

Thread
Omar Polo:

> @@ -1589,10 +1589,8 @@ test_commit_prepared_logmsg() {
>  
>  	echo 'test commit_prepared_logmsg' > $testroot/logmsg
>  
> -	cat > $testroot/editor.sh <<EOF
> -#!/bin/sh
> -sed -i 's/foo/bar/' "\$1"
> -EOF
> +	# a no-op editor script
> +	> $testroot/editor.sh
>  	chmod +x $testroot/editor.sh

Yes, please.

> There are a few places in your diff where we run ed multiple times on
> the same file, [...]
> these could be folded in a single ed invocation; however this is not
> an issue.

Revised script attached that takes care of the folding.

$ cd got/regress/cmdline
$ perl -i ~/sed2ed *.sh

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de
#!/usr/bin/perl -w

while(<>) {
	chomp;
	if (/^\tsed -i( -e)? (['"])([^'"]+)['"]\s(.*)/) {
		my ($quote, $file) = ($2, $4);
		if ($quote eq "'") {
			print "\ted -s $file <<-\\EOF\n";
		} else {
			print "\ted -s $file <<-EOF\n";
		}
		do {
			my $expr = $3;
			if ($expr =~ /^\//) {
				print "\tg$expr\n";
			} else {
				print "\t,$expr\n";
			}
			chomp($_ = <>);
		} while (/^\tsed -i( -e)? ($quote)([^'"]+)$quote\s\Q$file\E/);
		print "\tw\n";
		print "\tEOF\n";
		redo;
	} elsif (/^sed -i( -e)? (['"])([^'"]+)['"]\s(.*)/) {
		my ($quote, $expr, $file) = ($2, $3, $4);
		print "ed -s $file <<-EOF\n";
		if ($expr =~ /^\//) {
			print "\tg$expr\n";
		} else {
			print "\t,$expr\n";
		}
		print "\tw\n";
		print "\tEOF\n";
	} else {
		print $_, "\n";
	}
}