Blame


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