-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
389 lines (356 loc) · 6.98 KB
/
Copy pathfunctions
File metadata and controls
389 lines (356 loc) · 6.98 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# This file is part of newrun, copyright © M. Kristall
#
# This program is free software. You can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License (GPL) as published by
# the Free Software Foundation.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GPL
# for more details.
#
# You should have received a copy of the GPL with this distribution. If not,
# see <http://www.gnu.org/licenses/>.
preserve () {
echo "$*"
}
upper () {
echo "${*^^}"
}
lower () {
echo "${*,,}"
}
warn () {
echo "$@" >&2
}
[ -z "$DEBUG" ] && DEBUG=0
D_COMMAND=1
D_INCLUDE=2
D_SEND=4
D_NOISY=64
debug () {
local l=$1
shift 1
(( DEBUG & ${!l} )) && warn "$@"
}
die () {
local r=$1
shift 1
warn "$@"
exit $r
}
include () {
debug D_INCLUDE "include $1"
. "$1" 2> /dev/null || die 3 "$_NAME: could not exec '$1'"
}
tryrun () {
local func="$1"
shift 1
declare -f "$func" > /dev/null || return 127
debug D_COMMAND "try $func $@"
"$func" "$@"
return $?
}
runcmd () {
local cmd="$1" r
shift 1
debug D_COMMAND "$(shellify run "$cmd" "$@")"
"$cmd" "$@"
r=$?
(( r )) && debug D_COMMAND " $cmd fails $r"
return $r
}
readconfig () {
local config="$1"
include "$_CONFIGPATH/$config"
if [[ "$RUNAS" != "" && "$RUNAS" != "$USER" ]]; then
warn "RUNAS may not work right"
runcmd exec su "$RUNAS" -c "$_PATH/$_NAME"
fi
include "$_DEFPATH/$TYPE"
local svname="$(basename "$config")"
PIDFILE="$_PIDPATH/$svname.pid"
debug D_NOISY "PID file will be saved to $PIDFILE"
if [[ "$SERVER" == "" ]]; then
SERVER="$_SERVER"
fi
if [ ! -x "$SERVER" ]; then
die 4 "$_NAME: SERVER cannot be used: $SERVER"
fi
if [[ "$EXEC" == "" ]]; then
EXEC=null
fi
include "$_EXECPATH/$EXEC"
if [[ "$COMM" == "" ]]; then
COMM="$EXEC"
fi
if [[ "$COMM" != "$EXEC" ]]; then
include "$_EXECPATH/$COMM"
fi
debug D_NOISY "using COMM=$COMM, EXEC=$EXEC"
case "$CASE" in
lower|upper|preserve)
;;
*)
CASE=preserve
;;
esac
if [[ "$ARGDELIM" != "" && ! "$UNSAFE" =~ "$ARGDELIM" ]]; then
UNSAFE="$UNSAFE$ARGDELIM"
fi
case "$QUOTE" in
escape)
if [[ "$ESCAPE" == "" ]]; then
ESCAPE=\\
fi
if [[ ! "$UNSAFE" =~ "$ESCAPE" ]]; then
UNSAFE="$UNSAFE$ESCAPE"
fi
;;
single)
QUOTE="'"
;;
double|*)
QUOTE='"'
;;
esac
init
}
getpid () {
PID=
if [ -f "$PIDFILE" ]; then
read PID < "$PIDFILE"
debug D_NOISY "getpid=$PID"
else
debug D_NOISY "getpid failed"
return 1
fi
}
savepid () {
if [[ "$PIDFILE" != "" ]]; then
echo "$PID" > "$PIDFILE"
runcmd chmod 600 "$PIDFILE"
fi
}
removepid () {
runcmd rm -f "${DEFAULT_PIDFILE[@]}" "$PIDFILE"
}
pidfilehack () {
local pf
PID=
for pf in "${DEFAULT_PIDFILE[@]}"; do
if [[ -f "$pf" ]]; then
read PID < "$pf"
removepid
break 1
fi
done
if [[ "$PID" == "" ]]; then
PID=($(pidof "$SERVER"))
if (( ${#PID[@]} == 1 )); then
PID="${PID[0]}"
elif (( ${#PID[@]} > 1 )); then
PID=-${#PID[@]}
else
PID=
fi
fi
debug D_NOISY "pidfilehack=$PID"
}
isrunning () {
if [[ "$PID" == "" ]]; then
getpid
fi
if [[ "$PID" == "" ]]; then
return 1
fi
kill -0 $PID &> /dev/null
return $?
}
forcequit () {
if isrunning; then
sleep 1
if ! isrunning; then
break
fi
runcmd kill -TERM $PID
sleep 1
if isrunning; then
warn "$_NAME: server doesn't want to quit"
runcmd kill -KILL $PID
fi
fi
removepid
}
shellify () {
local i args
for (( i = 1; i <= $#; i++ )); do
args+="$(printf %q "${!i}") "
done
echo "$args"
}
stringify () {
local str append arg i j
for (( i = 1; i <= $#; i++ )); do
arg="${!i}"
if [[ "$QUOTE" == escape ]]; then
for (( j = 0; j < ${#arg}; j++ )); do
if [[ "${arg:j:1}" =~ ["$UNSAFE"] ]]; then
append+="$ESCAPE${arg:j:1}"
else
append+="${arg:j:1}"
fi
done
else
if [[ "$arg" =~ ["$UNSAFE"] ]]; then
append="$QUOTE$arg$QUOTE"
else
append="$arg"
fi
fi
if [[ "$str" == "" ]]; then
str="$append"
else
str="$str$ARGDELIM$append"
fi
done
echo "$str"
}
## flags
F_SKIP=1 # skip
F_NOARG=2 # no arguments
F_NOPRE=4 # no PREFIX
F_NOVAL=8 # no value
F_NODEF=16 # no default value
init () {
local flags typ varname val arg def opt i
for (( i = 0; i < ${#ARGUMENTS[@]}; )); do
opt=()
flags=0
def=
typ="${ARGUMENTS[i++]}"
varname="${ARGUMENTS[i++]}"
val="${!varname}"
if [[ "$val" == "" ]]; then
(( flags |= F_SKIP ))
fi
case "$typ" in
req)
(( flags |= ( F_NOPRE | F_NODEF ) ))
;;
bool)
(( flags |= ( F_NOVAL | F_NODEF ) ))
if [[ "$val" == "0" ]]; then
(( flags |= F_NOARG | F_SKIP ))
fi
;;
int)
val="$(echo "$val" | tr -d '[\0-/:-\xff]')"
if [[ "$val" == "" ]]; then
(( flags |= F_SKIP ))
fi
;;
file)
if (( !( flags & F_SKIP ) )) && [ ! -f "$val" ];
then
echo "$varname=$val not a file" >&2
# exit 4
fi
;;
dir)
if (( !( flags & F_SKIP ) )) && [ ! -d "$val" ];
then
echo "$varname=$val not a directory" >&2
# exit 4
fi
;;
str|*)
;;
esac
# default value
if (( !( flags & ( F_NOVAL | F_NODEF ) ) )); then
def="${ARGUMENTS[i++]}"
if [[ "$val" == "" ]]; then
declare -g "$varname"="$def"
val="$def"
fi
if [[ "$val" == "$def" ]]; then
# don't process args/value
(( flags |= ( F_SKIP | F_NOARG ) ))
fi
fi
# args
if (( !( flags & F_NOARG ) )); then
arg="${ARGUMENTS[i++]}"
if (( !( flags & F_NOPRE ) )); then
arg="$PREFIX$arg"
fi
opt+=("$arg")
for (( ; i < ${#ARGUMENTS[@]}; i++ )); do
arg="${ARGUMENTS[i]}"
if [[ "$arg" == + ]]; then
break 1
fi
opt+=("$arg")
done
# value
if (( !( flags & F_NOVAL ) )); then
if [[ "$EQUALS" != "" ]]; then
(( arg=${#opt[@]} - 1 ))
opt[arg]="${opt[arg]}$EQUALS$(stringify "$val")"
else
opt+=("$val")
fi
fi
fi
for (( ; i < ${#ARGUMENTS[@]}; i++ )); do
if [[ "${ARGUMENTS[i]}" == + ]]; then
(( i++ ))
break
fi
done
# need to add value here
if (( !( flags & F_SKIP ) )); then
run+=("${opt[@]}")
fi
done
}
Cmd () {
local args="$(stringify "$@")"
tryrun "$COMM"Cmd "$args" || \
tryrun "$EXEC"Cmd "$args" || \
die 3 "$_NAME: $COMM does not specify a way to communicate"
}
Start () {
STARTCMD=()
tryrun "$TYPE"StartCmd
tryrun "$EXEC"StartCmd
if [[ "$COMM" != "$EXEC" ]]; then
tryrun "$COMM"StartCmd
fi
if ! tryrun "$EXEC"Start "${run[@]}" "${STARTCMD[@]}" "$@"; then
debug D_COMMAND "run $SERVER ${run[@]} ${STARTCMD[@]}"
"$SERVER" "${run[@]}" "${STARTCMD[@]}" "$@" < /dev/null &
PID=$!
disown
savepid
fi
}
Stop () {
local args="$(stringify "$@")"
if ! tryrun "$EXEC"Stop "$args"; then
isrunning || die 125 "$_NAME: server may not be running"
Cmd $QUIT "$args"
forcequit
removepid
fi
}
Restart () {
if ! tryrun "$EXEC"Restart "$@"; then
isrunning || die 125 "$_NAME: server may not be running"
Stop Please reconnect in a few seconds
Start "$@"
fi
}
Reload () {
tryrun "$TYPE"Reload || Restart
}