-
Notifications
You must be signed in to change notification settings - Fork 0
/
click-format
executable file
·131 lines (111 loc) · 3.29 KB
/
click-format
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
#!/usr/bin/env bash
# ==============================================================================
# click-format
#
# Formatting helper script for click.
#
# Takes a Hoon command and an optional list of Clay file dependencies (as
# paths or beams) and formats them as a text representation of a Hoon noun.
# When jammed, this noun can be passed to conn.c to execute the command on a
# running ship.
#
# If a dependency file is passed as a beam, it will be interpreted as-is. If a
# dependency file is passed as a path, the default beak (i.e. [our base now])
# will be prepended to it.
#
# The structure of a conn.c thread call looks like this:
#
# [42 %fyrd [%base %hi %tape [%ship ~zod]]]
# 1 2 3 4 5 6 7
#
# 1 = Unique ID for mapping results to requests
# 2 = Command to conn.c routing the request
# 3 = Desk in which thread lives
# 4 = Name of thread
# 5 = Mark for output type
# 6 = Mark for input type
# 7 = Input to thread as a noun
#
# (1) - (6) are held constant by this client. (7) is provided by the user.
#
# ==============================================================================
# ==========================================================
# CONSTANTS
# ==========================================================
# Request ID
# A client that ran many simultaneous queries would want to somehow assign these
RID=0
# Command
# See conn.c for information about other commands and how they work
COMMAND="%fyrd"
# Desk
# Desk in which the thread lives. All threads used by click live in '%base'.
DESK="%base"
# Threads
# Name of thread to call. Only 'eval' and 'khan-eval' are used by click.
EVAL_TED="%eval"
KHAN_TED="%khan-eval"
THREAD=$EVAL_TED
# Output mark
# Output format of thread results (always noun for click)
MARK_OUT="%noun"
# Input mark
# Format of input to thread (always constant for click)
MARK_IN="%ted-eval"
# ==========================================================
# VARIABLES
# ==========================================================
STATUS=0
TED_IN=""
# ==========================================================
# FUNCTIONS
# ==========================================================
show_help() {
cat <<EOF
Usage:
$(basename "$0") [options] <hoon> [<dependency> ...]
Format a text representation of a Hoon noun for use as input to eval threads via conn.c
options:
-h Show usage info
-k Format for the 'khan-eval' thread instead
EOF
}
# ==========================================================
# MAIN
# ==========================================================
# reset getopts
OPTIND=1
# parse options
while getopts ":hk" OPT; do
case "$OPT" in
k)
THREAD=$KHAN_TED
;;
\?)
echo "Invalid option: -$OPTARG" >&2
STATUS=1
;&
h)
show_help
exit "$STATUS"
;;
esac
done
# skip over opts after parsing
shift $((OPTIND-1))
# check for mandatory input
if [[ $# -lt 1 ]]; then
echo "ERROR: no command" >&2
show_help
exit 1
fi
# setup input page
if [[ $# -eq 1 ]]; then
TED_IN="[$MARK_IN '$1']"
else
TED_IN="'$1'"
shift
TED_IN="[$MARK_IN [$TED_IN [$* ~]]]"
fi
# format command
echo "[$RID $COMMAND [$DESK $THREAD $MARK_OUT $TED_IN]]"