-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.m4
71 lines (56 loc) · 1.46 KB
/
macros.m4
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
divert(-1)
# ^ don't echo newlines and comments from these definitions
# name confusing strings
define(`OPENBRACE', `{')
define(`CLOSEBRACE', `}')
define(`OPENPAREN', `(')
define(`CLOSEPAREN', `)')
# switch to {{ }}
changequote(`{{', `}}')
# macros to close braces
define({{__END}}, )
# declare top level stuff
define({{DECLARE}}, {{__END}}
{{define({{__END}}, )d}}{{nl}})
# declare a go function
define({{GO}}, {{__END}}
{{define({{__END}}, CLOSEBRACE)d}}{{nl}}
{{function {{__}}GO_CMD:$1()}}OPENBRACE)
# wrap Python into a go function
define({{GOPY}}, {{__END}}
{{define({{__END}}, {{__END_PYTHON}})d}}{{nl}}
{{function {{__}}GO_CMD:$1()}}OPENBRACE
{{python <{{}}OPENPAREN{{}}cat << 'EOF'
def go(argv):}})
# end of a Python wrapper
define({{__END_PYTHON}}, {{
EOF
echo "go(\"${{}}@\".split())"
CLOSEPAREN
CLOSEBRACE}})
# blocks for running in a set -ex process
define({{BLOCK}}, {{( set -ex $1)}})
# functions for including inline files
# FILE_VAR allows bash variables
define({{FILE}}, {{cat << 'EOF' > $1 $2
EOF
}})
define({{FILE_VAR}}, {{cat << EOF > $1 $2
EOF
}})
# temporarily change shell options
define({{WITH_SHOPT}},
{{__shopts={{$}}{BASHOPTS}
shopt -s $1
$2
shopt -u $1
shopt -s {{$}}{__shopts//:/ }
unset __shopts;}})
# call a go function
define({{GO_CALL}}, {{__}}GO_CMD:$1)
# format is a common identifier, so undefine m4's format
undefine({{format}})
# close remaining braces
m4wrap({{__END}})
# end
divert(0)dnl