-
Notifications
You must be signed in to change notification settings - Fork 0
/
esh
executable file
·204 lines (185 loc) · 4.2 KB
/
esh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh
# vim: set ts=4:
#---help---
# Usage:
# esh [options] [--] INPUT [VARIABLE ...]
# esh <-h | -V>
#
# Process and evaluate an ESH template.
#
# Arguments:
# INPUT Path of the template file or "-" to read from STDIN.
# VARIABLE Variable(s) specified as NAME=VALUE to pass into the template
# (the have higher priority than environment variables).
#
# Options:
# -d Don't evaluate template, just dump a shell script.
# -o FILE Output file or "-" for STDOUT. Defaults to "-".
# -s SHELL Command name or path of the shell to use for template
# evaluation. It must not contain spaces. Defaults to "/bin/sh".
# -h Show this help message and exit.
# -V Print version and exit.
#
# Environment Variables:
# ESH_AWK Command name of path of the awk program to use.
# It must not contain spaces. Defaults to "awk".
# ESH_SHELL Same as -s.
#
# Please report bugs at <https://github.com/jirutka/esh/issues>.
#---help---
set -eu
# Set pipefail if supported.
if ( set -o pipefail 2>/dev/null ); then
set -o pipefail
fi
readonly PROGNAME='esh'
readonly VERSION='0.1.1'
AWK_PROGRAM=$(cat <<'AWK'
function fputs(str) {
printf("%s", str)
}
function read(len, _str) {
if (len == "") {
_str = buff
buff = ""
} else if (len > 0) {
_str = substr(buff, 1, len)
buff = substr(buff, len + 1, length(buff))
}
return _str
}
function skip(len) {
buff = substr(buff, len + 1, length(buff))
}
function flush(len, _str) {
_str = read(len)
if (state == "TEXT") {
gsub("'", "'\\''", _str)
}
if (state != "COMMENT") {
fputs(_str)
}
}
BEGIN {
FS = ""
buff = ""
if (shell) {
print("#!" (shell ~ /\// ? shell : "/usr/bin/env " shell))
}
print("set -eu")
print("if ( set -o pipefail 2>/dev/null ); then set -o pipefail; fi")
print("__print() { printf '%s' \"$*\"; }")
print(vars)
fputs("\nprintf '%s' '")
state = "TEXT"
}
{
buff = $0
while (buff != "") {
print_nl = 1
if (state == "TEXT" && match(buff, /<%/)) {
flush(RSTART - 1) # print buff before "<%"
skip(2) # skip "<%"
flag = substr(buff, 1, 1)
if (flag != "%") {
fputs("'\n") # close text
}
if (flag == "%") { # <%%
skip(1)
fputs("<%")
} else if (flag == "=") { # <%=
skip(1)
fputs("__print ")
state = "TAG"
} else if (flag == "#") { # <%#
state = "COMMENT"
} else {
state = "TAG"
}
} else if (state != "TEXT" && match(buff, /%>/)) {
flag = RSTART > 1 ? substr(buff, RSTART - 1, 1) : ""
if (flag == "%") { # %%>
flush(RSTART - 2)
skip(1)
flush(2)
} else if (flag == "-") { # -%>
flush(RSTART - 2)
skip(3)
print_nl = 0
} else { # %>
flush(RSTART - 1)
skip(2)
}
if (flag != "%") {
fputs("\nprintf '%s' '")
state = "TEXT"
}
} else {
flush()
}
}
if (print_nl && state != "COMMENT") {
fputs("\n")
}
}
END {
if (state == "TEXT") {
fputs("'\n")
}
}
AWK
)
readonly AWK_PROGRAM
help() {
sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
exit ${1:-0}
}
convert() {
local input="$1"
local vars="$2"
local evaluate="${3:-yes}"
if [ "$evaluate" = yes ]; then
$ESH_AWK -v vars="$vars" -- "$AWK_PROGRAM" "$input" | $ESH_SHELL -s
else
$ESH_AWK -v vars="$vars" -v shell="$ESH_SHELL" -- "$AWK_PROGRAM" "$input"
fi
}
: ${ESH_AWK:="awk"}
: ${ESH_SHELL:="/bin/sh"}
EVALUATE='yes'
OUTPUT=''
while getopts 'dho:s:V' OPT; do
case "$OPT" in
d) EVALUATE=no;;
h) help 0;;
o) OUTPUT="$OPTARG";;
s) ESH_SHELL="$OPTARG";;
V) echo "$PROGNAME $VERSION"; exit 0;;
'?') help 1 >&2;;
esac
done
shift $(( OPTIND - 1 ))
[ $# -ge 1 ] || help 1 >&2
INPUT="$1"; shift
[ "$INPUT" != '-' ] || INPUT=''
# Validate arguments.
for arg in "$@"; do
case "$arg" in
*=*) ;;
*) echo "$PROGNAME: illegal argument: $arg" >&2; exit 1;;
esac
done
# Format variables into shell variable assignments.
vars=''; for item in "$@"; do
vars="$vars\n${item%%=*}='$(
printf %s "${item#*=}" | $ESH_AWK "{ gsub(/'/, \"'\\\\\\\''\"); print }"
)'"
done
if [ "${OUTPUT#-}" ]; then
tmpfile="$(mktemp)"
trap "rm -f '$tmpfile'" EXIT HUP INT TERM
convert "$INPUT" "$vars" "$EVALUATE" > "$tmpfile"
mv "$tmpfile" "$OUTPUT"
else
convert "$INPUT" "$vars" "$EVALUATE"
fi