Initialize Tizen 2.3
[external/ragel.git] / test / langtrans_c.sh
1 #!/bin/bash
2 #
3
4 file=$1
5
6 [ -f $file ] || exit 1
7
8 # Get the machine name.
9 machine=`sed -n 's/^[\t ]*machine[\t ]*\([a-zA-Z_0-9]*\)[\t ]*;[\t ]*$/\1/p' $file`
10
11 # Make a temporary version of the test case using the C language translations.
12 sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_c.txl > $file.pr
13
14 needs_eof=`sed '/@NEEDS_EOF/s/^.*$/yes/p;d' $file`
15 if [ "$needs_eof" != 'yes' ]; then
16         needs_eof=`sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin checkeofact.txl`
17 fi
18
19 # Begin writing out the test case.
20 cat << EOF
21 /*
22  * @LANG: c
23  * @GENERATED: yes
24 EOF
25
26 grep '@ALLOW_GENFLAGS:' $file
27 grep '@ALLOW_MINFLAGS:' $file
28
29 cat << EOF
30  */
31 #include <string.h>
32 #include <stdio.h>
33 EOF
34
35 # Write the data declarations
36 sed -n '/^%%$/q;p' $file.pr
37
38 # Write out the machine specification.
39 sed -n '/^%%{$/,/^}%%/p' $file.pr
40
41 # Write out the init and execute routines.
42 cat << EOF
43 int cs;
44 %% write data;
45 void init()
46 {
47 EOF
48
49 sed -n '0,/^%%$/d; /^%%{$/q; {s/^/\t/;p}' $file.pr
50
51 cat << EOF
52         %% write init;
53 }
54
55 void exec( char *data, int len )
56 {
57         char *p = data;
58         char *pe = data + len;
59 EOF
60
61 [ "$needs_eof" = "yes" ] && echo "char *eof = pe;"
62
63 cat << EOF
64         %% write exec;
65 }
66
67 void finish( )
68 {
69         if ( cs >= ${machine}_first_final )
70                 printf( "ACCEPT\\n" );
71         else
72                 printf( "FAIL\\n" );
73 }
74 EOF
75
76 # Write out the test data.
77 sed -n '0,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
78 BEGIN {
79         print "char *inp[] = {"
80 }
81 {
82         print " " $0 ","
83 }
84 END {
85         print "};"
86         print ""
87         print "int inplen = " NR ";"
88 }'
89
90 # Write out the main routine.
91 cat << EOF
92
93 int main( )
94 {
95         int i;
96         for ( i = 0; i < inplen; i++ ) {
97                 init();
98                 exec( inp[i], strlen(inp[i]) );
99                 finish();
100         }
101         return 0;
102 }
103 #ifdef _____OUTPUT_____
104 EOF
105
106 # Write out the expected output.
107 sed -n '0,/\/\* _____OUTPUT_____/d; /_____OUTPUT_____ \*\//q; p;' $file
108 echo "#endif"
109
110 # Don't need this language-specific file anymore.
111 rm $file.pr