Mac OS X >= 10.7 support
[runtime.git] / scripts / fsmgraph
1 #!/bin/sh
2 #
3 #  Author: Pekka Riikonen <priikone@silcnet.org>
4 #
5 #  Copyright (C) 2005 Pekka Riikonen
6 #
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; version 2 of the License.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  Usage: ./fsmgraph file | dot -Tps -o outfile.ps
17 #
18 #  Graphviz dot is required to create the graphs.
19
20 ##############################################################################
21
22 # Get the starts
23 starts=`awk '/^SILC_FSM_STATE\(/,/^}/ { next } { print }' $@ | grep "silc_fsm_start" | cut -d, -f2 | cut -d\) -f1`
24
25 # Get all states
26 states=`grep "^SILC_FSM_STATE(" $@ | grep -v ");" | cut -d\( -f2 | cut -d\) -f1`
27
28 # Output the graph
29 echo "digraph G {"
30
31 # Draw starts
32 for i in $starts
33 do
34   echo "\"start $i\" [shape=plaintext];"
35   echo "\"start $i\" -> $i;"
36 done
37
38 # Draw states and transitions
39 for i in $states
40 do
41   echo "$i;"
42
43   # This weird line gets us all state transitions and their optioanl
44   # comment lines which be put as labels
45   tr=`cat $@ | grep -v "^SILC_FSM_STATE($i);" | awk '/^SILC_FSM_STATE\('$i'\)/,/^}/ { if (/\/\*\* /) print; if (/silc_fsm_next/) print; }' | sed 's/^[  ]*//; s/\\/\\*\\* /L:/; s/\\*\\///; s/silc_fsm_next/T:silc_fsm_next/' | sed '/L:/s/ /\\\\/g; /T:/s/ /\\\\/g; s/T:/T: /; s/L:/L: /'`
46
47   # Get thread starts
48   threads=`cat $@ | grep -v "^SILC_FSM_STATE($i);" | awk '/^SILC_FSM_STATE\('$i'\)/,/^}/ { if (/\/\*\*\* /) print; if (/silc_fsm_start/) print; }' | sed 's/^[ ]*//; s/\\/\\*\\*\\* /L:/; s/\\*\\///; s/silc_fsm_start/T:silc_fsm_start/' | sed '/L:/s/ /\\\\/g; /T:/s/ /\\\\/g; s/T:/T: /; s/L:/L: /'`
49
50   # Get async calls
51   asyncs=`cat $@ | grep -v "^SILC_FSM_STATE($i);" | awk '/^SILC_FSM_STATE\('$i'\)/,/^}/ { if (/SILC_FSM_CALL\(/) print; }' | sed 's/SILC_FSM_CALL(//' | cut -d= -f2 | cut -d\( -f1`
52
53   trname=""
54   label=""
55
56   # Draw transitions
57   for t in $tr
58   do
59     if test "$t" = "L:"; then
60       label="$t"
61       continue
62     fi
63     if test "$t" = "T:"; then
64       trname="$t"
65       continue
66     fi
67     if test "$label" = "L:"; then
68       label="$t"
69       continue
70     fi
71     if test "$trname" = "T:"; then
72       trname="$t"
73     fi
74
75     # Unescape
76     if test "$label"; then
77       label=`echo $label | sed 's/\\\\/ /g'`
78     fi
79     trname=`echo $trname | sed 's/\\\\/ /g'`
80     trname=`echo $trname | cut -d, -f2 | cut -d\) -f1`
81
82     echo "$i -> $trname [label=\" $label \"];"
83
84     trname=""
85     label=""
86   done
87
88   # Draw thread transitions
89   for t in $threads
90   do
91     if test "$t" = "L:"; then
92       label="$t"
93       continue
94     fi
95     if test "$t" = "T:"; then
96       trname="$t"
97       continue
98     fi
99     if test "$label" = "L:"; then
100       label="$t"
101       continue
102     fi
103     if test "$trname" = "T:"; then
104       trname="$t"
105     fi
106
107     # Unescape
108     if test "$label"; then
109       label=`echo $label | sed 's/\\\\/ /g'`
110     fi
111     trname=`echo $trname | sed 's/\\\\/ /g'`
112     trname=`echo $trname | cut -d, -f2 | cut -d\) -f1`
113
114     echo "$i -> $trname [label=\" $label \"] [style=dotted];"
115
116     trname=""
117     label=""
118   done
119
120   # Draw async calls
121   for a in $asyncs
122   do
123     echo "\"$a\" [shape=plaintext];"
124     echo "$i -> \"$a\" [style=dotted];"
125   done
126 done
127
128 echo "}"