]> git.draconx.ca Git - dxcommon.git/blob - t/bison.sh
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / t / bison.sh
1 #!/bin/sh
2 #
3 # Copyright © 2021 Nick Bowler
4 #
5 # Fake bison program for testing the bison detection macro.
6 #
7 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
8 # This is free software: you are free to do what the fuck you want to.
9 # There is NO WARRANTY, to the extent permitted by law.
10
11 : "${FAKE_BISON_VERSION=2.4.0}"
12
13 argv0=$0
14
15 infile=
16 outheader=
17 use_header=false
18
19 for arg
20 do
21   case $arg in
22   --defines) use_header=true ;;
23   --defines=*) use_header=true outheader=${arg%*=} ;;
24   *.y) infile=$arg ;;
25   esac
26 done
27
28 if test x"$infile" = x""; then
29   printf '%s: error: no input file\n' "$argv0" 1>&2
30   exit 1
31 fi
32
33 outfile=${infile%.y}.tab.c
34 if $use_header; then
35   test ${outheader:+y} || outheader=${outfile%.c}.h
36   exec 5>"$outheader"
37 fi
38 exec 3<"$infile" 4>"$outfile"
39
40 copy_header=false copy_impl=false
41 while read line <&3; do
42   case $line in
43   %require*)
44     save_IFS=$IFS IFS=$IFS\".
45     set x $FAKE_BISON_VERSION 0 0 0; shift
46     fake_major=$1 fake_minor=$2 fake_revision=$3
47     set x ${line#*\"} 0 0 0; shift
48     req_major=$1 req_minor=$2 req_revision=$3
49     IFS=$save_IFS
50     set -x
51     test $fake_major -ge $req_major || exit 1
52     if test $fake_major -eq $req_major; then
53       test $fake_minor -ge $req_minor || exit 1
54       if test $fake_minor -eq $req_minor; then
55         test $fake_revision -ge $req_revision || exit 1
56       fi
57     fi
58     set +x
59     ;;
60   %code*)
61     set x $line; shift
62     case $2 in
63     requires|provides) copy_header=true copy_impl=true ;;
64     top) copy_impl=true ;;
65     esac
66     ;;
67   [}])
68     copy_header=false copy_impl=false
69     ;;
70   %%)
71     break
72     ;;
73   *)
74     $copy_header && printf '%s\n' "$line" >&5
75     $copy_impl && printf '%s\n' "$line" >&4
76     ;;
77   esac
78 done
79
80 cat >&4 <<EOF
81 int yyparse(void)
82 {
83   return 0;
84 }
85 EOF
86
87 while read line <&3; do
88   case $line in
89   %%) break;
90   esac
91 done
92
93 cat <&3 >&4