blob: 177d0c7e7cc10f020e6afb27fad477f47bfa449d (
plain)
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
|
#!/bin/sh -e
# paths to state file and definition dir
PState=/var/fsm/$2
PDef=/etc/fsm/$2
watch () {
SO=$(cat $PState)
SN=$(callOne $PDef/watch $SO "xxx" \
"./$SO
./default
/bin/false") || fail "watch script failed or missing"
[ -n "$SN" ] || fail "watch script $S0 returned empty state name"
change $SN
}
change () {
SO=$(cat $PState)
SN=$1
if [ "$SO" != "$SN" ]; then
if [ -x $PDef/trans/$SO-$SN.trans ]; then
# one script to handle whole transition
callOne $PDef/trans $SO $SN \
"./$SO-$SN.trans" \
|| fail "state transition script failed"
else
# seperate scripts for leaving and entering states
callOne $PDef/trans $SO $SN \
"./$SO.leave
./default.leave
/bin/true" \
|| fail "state leave script failed"
callOne $PDef/trans $SO $SN \
"./$SN.enter
./default.enter
/bin/true" \
|| fail "state enter script failed"
fi
echo $SN > $PState
fi
}
callOne () { # args: cwd arg1 arg2 cmdlist
echo "$4" | sed 's/ *//' | (
set -e
cd $1
while read cmd; do
if [ -x "$cmd" ]; then
$cmd $2 $3 666<&-
exit $?
fi
done )
}
lockState () {
# lock state file (neccessary also for watch, as it calls scripts
# that assume a certain state as active during their whole
# execution time)
exec 666<$1
flock -x 666
}
fail() {
echo "$1" 1>&2
exit 1
}
printArgs () {
fail "Usage:
$0 get fsm
$0 watch fsm
$0 change fsm state"
}
[ $# -lt 2 ] && printArgs
# check for minimal FSM definition
if [ -f $PDef/initial_state \
-a -d $PDef/watch \
-a -d $PDef/trans ]; then
if [ ! -f $PState ]; then
mkdir -p $(dirname $PState)
S=$(cat $PDef/initial_state)
[ -n "$S" ] || fail "initial state must not be empty"
echo $S >$PState
fi
else
fail "FSM $2 does not exist"
fi
case "$1" in
get)
cat $PState
;;
watch)
lockState $PState
watch
;;
change)
[ -n "$3" ] || printArgs
lockState $PState
change $3
;;
*)
printArgs
esac
|