-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCPro.sh
More file actions
258 lines (195 loc) · 5.56 KB
/
BCPro.sh
File metadata and controls
258 lines (195 loc) · 5.56 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#shellcheck disable=SC2034
# ========================================
# =============== ~BCPro~ ================
# ========================================
# ===================================
# ============== Main ===============
# ===================================
bcpro_init() {
# Globals
declare -xg G_HERE G_SYSTEM
declare -xg G_PIPEDIR # the temporary directory in which to store named pipes
declare -gA g_pipe # an array containing the path to each named pipe
declare -gA g_proc # an array containing the PIDs of each created process
declare -xg G_REL # pipe to the communication relay
# Name of the main program (might change this)
BASH_ARGV0=main
make_pipe main
# Program path
G_HERE=$(canonical_path "$0")
G_HERE=${G_HERE%/*} # dirname
# System recognition
if cmd_exists cygpath; then
G_SYSTEM=win
else
G_SYSTEM=lin
fi
# Trap to clean all pipes and subprocesses
trap 'trap_cleanup "$G_PIPEDIR" "${g_proc[@]}"' EXIT SIGINT SIGTERM
if ! G_PIPEDIR=$(mktemp -d); then
fatal "Failed to create temporary directory"
fi
spin_proc relay p_relay
G_REL="${g_pipe[relay]}"
}
spin_proc() {
local name=$1
local fn=$2
local args=$3
[[ $# -lt 2 || $# -gt 3 ]] && fatal_assert "Use <name> <fn> [args] as parameters for spin_proc"
make_pipe "$name"
bash -c '$1 "$2" "$3"' "$name" "$fn" "${g_pipe[$name]}" "$args" &
g_proc[$name]=$!
}
make_pipe() {
local name=$1
local pipepath="$G_PIPEDIR/$RANDOM.$RANDOM.$RANDOM"
if ! mkfifo "$pipepath"; then
fatal "Failed to create pipe"
fi
g_pipe[$name]=$pipepath
}
msg_head() {
# ~>sender>receiver
echo "~>$0>$1"
}
export -f spin_proc make_pipe
# ===================================
# ========== Sub-Processes ==========
# ===================================
#
# All processes take the name of their corresponding pipe as the first argument.
# This pipe is the mailing box of the process, used to talk to it.
#
# it prints stuff for now, but this will be the main communication channel
p_relay() {
local pipe=$1
[[ -z $pipe ]] && return 2 # improve UX
# main loop
local EOP=0
local line
until ((EOP)); do
while IFS= read -r line; do
noti "[relay] $line"
[[ $line == 'EOP' ]] && EOP=1
done < "$pipe"
noti "[relay] <EOF found>"
done
noti "[relay] <EOP found>"
}
export -f p_relay
# ==========
# ====================
# ==============================
# ========================================
# ================================================================================
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ================================================================================
# ========================================
# ==============================
# ====================
# ==========
cmd_exists() {
command -v "$1" &>/dev/null
return $?
}
trap_cleanup() {
local dir=$1
shift
local killtargets=("$@")
local p
for p in "${killtargets[@]}"; do
kill "$p" 2>/dev/null
done
rm -rf "$dir"
}
# ========== Constants ==========
# ANSI Colors
C_RESET=$'\e[m'
C_RED=$'\e[31m'
C_BLUE=$'\e[34m'
C_PURPLE=$'\e[35m'
C_GREEN_B=$'\e[1;32m'
C_RED_B_BL=$'\e[1;5;31m'
C_UNDERLINE=$'\e[4m'
C_NOT_UNDERLINE=$'\e[24m'
C_STRIKETHROUGH=$'\e[9m'
echo -n $'\e[m' # just for clean tracing
export C_RESET C_RED C_BLUE C_PURPLE C_GREEN_B C_RED_B_BL C_UNDERLINE C_NOT_UNDERLINE C_STRIKETHROUGH
# ========== Essential Functions ==========
noti() {
echo "$C_PURPLE#>>>$C_RESET${*}"
}
erro() {
echo "$C_RED!>>>$C_RESET${*}" >&2
return 1
}
fatal() {
erro "$*"
exit 1
}
fatal_assert() {
erro " Fatal Assertion Failure (bug):"
fatal " $*"
}
#shellcheck disable=SC1090
load_module() {
local mod="$HERE/modules/$1.sh"
source "$mod" 2>/dev/null || fatal "Could not load module: $mod"
}
canonical_path() {
local path=$1
local old_path
while true; do
old_path=$path
path=$(readlink -e -- "$path")
(($?)) && return 1
[[ $path == "$old_path" ]] && break
done
echo "$path"
}
export -f noti erro fatal fatal_assert load_module canonical_path
# ========== File Operations ==========
file_to_array() {
# file < stdin
local -n arr=$1
local line
while read -r line || [[ -n "$line" ]]; do
line=${line%$'\r'} # delete CR
[[ -z "$line" ]] && continue
arr+=("$line")
done
}
load_config() {
# configFile < stdin
local -n table=$1
local required_entries=$2
local optional_entries=$3
local entry value is_valid line
while read -r line || [[ -n "$line" ]]; do
line=${line%$'\r'} # delete CR
[[ -z "$line" || "$line" =~ ^# ]] && continue
if [[ $line != *=* ]]; then
fatal "Invalid line in config file: $line"
fi
entry=${line%%=*}
value=${line#*=}
is_valid=false
for x in $required_entries $optional_entries; do
[[ $entry == "$x" ]] && is_valid=true && break
done
if ! $is_valid; then
fatal "Invalid entry in config file: $entry"
fi
if [[ -n ${table[$entry]} ]]; then
fatal "Duplicate entry in config file: $entry"
fi
table[$entry]=$value
done
for x in $required_entries; do
if [[ -z ${table[$x]} ]]; then
fatal "Missing required entry in config file: $x"
fi
done
}
export -f file_to_array load_config