Initial code commit for Toolkit 1.1.
[silc.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 (/silc_fsm_start/) print; }' | cut -d, -f2 | cut -d\) -f1`
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 h in $threads
90   do
91     echo "$i -> $h [style=dotted];"
92   done
93
94   # Draw async calls
95   for a in $asyncs
96   do
97     echo "\"$a\" [shape=plaintext];"
98     echo "$i -> \"$a\" [style=dotted];"
99   done
100 done
101
102 echo "}"