]> git.draconx.ca Git - dxcommon.git/blob - t/flex.sh
Add flex detection macro.
[dxcommon.git] / t / flex.sh
1 #!/bin/sh
2 #
3 # Copyright © 2020 Nick Bowler
4 #
5 # Fake flex program for testing the flex 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_FLEX_VERSION=2.0.0}"
12
13 argv0=$0
14
15 infile=
16 outfile=lex.yy.c
17 for arg
18 do
19   shift
20
21   case $arg in
22   -o) outfile=$1; shift ;;
23   *) set x "$@" "$arg"; shift ;;
24   esac
25 done
26
27 for arg
28 do
29   case $arg in
30   *.l) infile=$arg ;;
31   esac
32 done
33
34 if test x"$infile" = x""; then
35   printf '%s: error: no input file\n' "$argv0" 1>&2
36   exit 1
37 fi
38
39 copyout=false
40 exec 3<"$infile" 4>"$outfile"
41
42 save_IFS=$IFS IFS=.
43 set x $FAKE_FLEX_VERSION 0 0 0; shift
44 IFS=$save_IFS
45
46 cat >&4 <<EOF
47 #define FLEX_SCANNER
48 #define YY_FLEX_MAJOR_VERSION $1
49 #define YY_FLEX_MINOR_VERSION $2
50 #define YY_FLEX_SUBMINOR_VERSION $3
51 EOF
52
53 while read line <&3; do
54   case $line in
55   %{) copyout=true ;;
56   %}) copyout=false ;;
57   %%) break ;;
58   *) $copyout && printf '%s\n' "$line" >&4 ;;
59   esac
60 done
61
62 cat >&4 <<EOF
63 int yylex(void)
64 {
65   return 0;
66 }
67 EOF
68
69 while read line <&3; do
70   case $line in
71   %%) break ;;
72   esac
73 done
74
75 cat <&3 >&4