Blame


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