Blame


1 1c1e3e26 2022-09-08 op #!/usr/bin/env perl
2 1c1e3e26 2022-09-08 op #
3 1c1e3e26 2022-09-08 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 1c1e3e26 2022-09-08 op #
5 1c1e3e26 2022-09-08 op # Permission to use, copy, modify, and distribute this software for any
6 1c1e3e26 2022-09-08 op # purpose with or without fee is hereby granted, provided that the above
7 1c1e3e26 2022-09-08 op # copyright notice and this permission notice appear in all copies.
8 1c1e3e26 2022-09-08 op #
9 1c1e3e26 2022-09-08 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1c1e3e26 2022-09-08 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1c1e3e26 2022-09-08 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1c1e3e26 2022-09-08 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1c1e3e26 2022-09-08 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1c1e3e26 2022-09-08 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1c1e3e26 2022-09-08 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1c1e3e26 2022-09-08 op #
17 1c1e3e26 2022-09-08 op # subst -- filter that replaces key=values pairs
18 1c1e3e26 2022-09-08 op #
19 1c1e3e26 2022-09-08 op # usage: subst key1=val1 [... keyn=valn] [files...]
20 1c1e3e26 2022-09-08 op #
21 1c1e3e26 2022-09-08 op # Use `--' before the first file if it may contain an = in its name.
22 1c1e3e26 2022-09-08 op # If no files are given, read and substitute from standard input.
23 1c1e3e26 2022-09-08 op
24 1c1e3e26 2022-09-08 op use v5.10;
25 1c1e3e26 2022-09-08 op use strict;
26 1c1e3e26 2022-09-08 op use warnings;
27 1c1e3e26 2022-09-08 op
28 1c1e3e26 2022-09-08 op my @args = ();
29 1c1e3e26 2022-09-08 op
30 1c1e3e26 2022-09-08 op while (1) {
31 1c1e3e26 2022-09-08 op $_ = shift;
32 1c1e3e26 2022-09-08 op last if !$_;
33 1c1e3e26 2022-09-08 op
34 1c1e3e26 2022-09-08 op if ($_ eq '--') {
35 1c1e3e26 2022-09-08 op last;
36 1c1e3e26 2022-09-08 op } elsif ($_ !~ m/=/) {
37 1c1e3e26 2022-09-08 op unshift @ARGV, $_;
38 1c1e3e26 2022-09-08 op last;
39 1c1e3e26 2022-09-08 op }
40 1c1e3e26 2022-09-08 op
41 1c1e3e26 2022-09-08 op my ($match, $repl) = m/^([^\s]*)=(.*)$/;
42 1c1e3e26 2022-09-08 op push @args, "-e", "s!$match!$repl!g";
43 1c1e3e26 2022-09-08 op }
44 1c1e3e26 2022-09-08 op
45 1c1e3e26 2022-09-08 op # OK, shelling out to sed is a bit lame...
46 1c1e3e26 2022-09-08 op exec "sed", @args, @ARGV;