1#!/bin/sh
2
3hxtoh()
4{
5    flag=1
6    while read -r str; do
7        case $str in
8            HXCOMM*)
9            ;;
10            STEXI*|ETEXI*|SQMP*|EQMP*) flag=$(($flag^1))
11            ;;
12            *)
13            test $flag -eq 1 && printf "%s\n" "$str"
14            ;;
15        esac
16    done
17}
18
19hxtotexi()
20{
21    flag=0
22    line=1
23    while read -r str; do
24        case "$str" in
25            HXCOMM*)
26            ;;
27            STEXI*)
28            if test $flag -eq 1 ; then
29                echo "line $line: syntax error: expected ETEXI, found $str" >&2
30                exit 1
31            fi
32            flag=1
33            ;;
34            ETEXI*)
35            if test $flag -ne 1 ; then
36                echo "line $line: syntax error: expected STEXI, found $str" >&2
37                exit 1
38            fi
39            flag=0
40            ;;
41            SQMP*|EQMP*)
42            if test $flag -eq 1 ; then
43                echo "line $line: syntax error: expected ETEXI, found $str" >&2
44                exit 1
45            fi
46            ;;
47            DEFHEADING*)
48            echo "$(expr "$str" : "DEFHEADING(\(.*\))")"
49            ;;
50            *)
51            test $flag -eq 1 && echo "$str"
52            ;;
53        esac
54        line=$((line+1))
55    done
56}
57
58hxtoqmp()
59{
60    IFS=
61    flag=0
62    line=1
63    while read -r str; do
64        case "$str" in
65            HXCOMM*)
66            ;;
67            SQMP*)
68            if test $flag -eq 1 ; then
69                echo "line $line: syntax error: expected EQMP, found $str" >&2
70                exit 1
71            fi
72            flag=1
73            ;;
74            EQMP*)
75            if test $flag -ne 1 ; then
76                echo "line $line: syntax error: expected SQMP, found $str" >&2
77                exit 1
78            fi
79            flag=0
80            ;;
81            STEXI*|ETEXI*)
82            if test $flag -eq 1 ; then
83                echo "line $line: syntax error: expected EQMP, found $str" >&2
84                exit 1
85            fi
86            ;;
87            *)
88            test $flag -eq 1 && echo "$str"
89            ;;
90        esac
91        line=$((line+1))
92    done
93}
94
95case "$1" in
96"-h") hxtoh ;;
97"-t") hxtotexi ;;
98"-q") hxtoqmp ;;
99*) exit 1 ;;
100esac
101
102exit 0
103