1#!/bin/bash
2
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16CONTENTS=$(egrep '^#.*$' < $1 \
17           | sed -n -e '1,/^### Begin of instructions/d' \
18                    -e '1,/^### End of instructions/p')
19
20SUBSECTIONS=$(echo "$CONTENTS" | egrep '^## .*' \
21              | sed -e 's/## \(.*\)/\1/')
22
23# Menu of subsections.
24
25echo "@menu"
26while read -r sub
27do
28    echo "* $sub::"
29done <<< "$SUBSECTIONS"
30echo "@end menu"
31
32# Process each subsection.
33
34while read -r sub
35do
36    sed_cmd='/^## '$sub'/,/^## /p'
37    SUBCONTENTS=$(echo "$CONTENTS" \
38                  | sed -n -e "$sed_cmd" \
39                  | head --line=-1)
40
41    echo ""
42    echo "@node $sub"
43    echo "@subsection $sub"
44    echo ""
45
46    # Instructions menu
47    echo "@menu"
48    echo "$SUBCONTENTS" \
49        | egrep '^# Instruction: ' \
50        | sed -e 's/# Instruction: \([^ ][^ ]*\)\(.*\)/* Instruction \1::/g'
51    echo "@end menu"
52
53    # Instruction subsections
54    echo "$SUBCONTENTS" \
55        | sed -e 's/## .*//' \
56              -e 's/# Stack: \(.*\)/\nStack: @code{\1}/' \
57              -e 's/# Exceptions Stack: \(.*\)/\nException Stack: @code{\1}/' \
58              -e 's/# Exceptions: \(.*\)/\nExceptions: @code{\1}/' \
59              -e 's/# Instruction: \([^ ][^ ]*\)\(.*\)/\n@node Instruction \1\n@subsubsection Instruction \1\n\nSynopsys:\n\n@example\n\1\2\n@end example\n\n/' \
60              -e 's/^# //' -e 's/^#//' \
61        | uniq
62done <<< "$SUBSECTIONS"
63