-
Notifications
You must be signed in to change notification settings - Fork 6
/
renv.awk
53 lines (46 loc) · 927 Bytes
/
renv.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/awk -f
# Set up remote environment for rset(1)
# Print only lines that match the format [name="value" ...]
# release: ${release}
function err(s) {
print "renv: " s ": " $0 > "/dev/stderr"
exit 1
}
BEGIN {
for (i=1; i<ARGC; i++) {
if (ARGV[i] == "-q") {
ARGV[i] = ""
quiet=1
}
}
# save arguments
if (ARGV[1] ~ /[_A-Za-z0-9]+=/) {
dst = ARGV[2]
sd = ENVIRON["SD"]
if (!sd) sd = "."
if (!dst) dst = sd "/local.env"
split(ARGV[1], kv, "=")
system(sd "/renv <<EOF >> " dst "\n" kv[1] "=\"" kv[2] "\"\nEOF")
exit
}
}
/\\|\$\(|`/ {
err("subshells not permitted")
}
# empty line or comment
/^$|^#/ {
next
}
/^[_A-Za-z0-9]+="/ {
# erase quotes
gsub(/"/, "")
# collapse extra spaces and expand literals
gsub(/[ \t]+/, " ")
gsub(/\$\$/, "\\$")
sub(/ $/, "")
len=index($0, "=")
if (!quiet)
print substr($0, 0, len) "\"" substr($0, len+1) "\""
next
}
{ err("unknown pattern") }