1 
2 #line 1 "awkemu.rl"
3 /*
4  * Perform the basic line parsing of input performed by awk.
5  */
6 
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 
12 
13 #line 55 "awkemu.rl"
14 
15 
16 
17 #line 18 "awkemu.c"
18 static const int awkemu_start = 2;
19 
20 static const int awkemu_en_main = 2;
21 
22 
23 #line 58 "awkemu.rl"
24 
25 #define MAXWORDS 256
26 #define BUFSIZE 4096
27 char buf[BUFSIZE];
28 
main()29 int main()
30 {
31 	int i, nwords = 0;
32 	char *ls = 0;
33 	char *ws[MAXWORDS];
34 	char *we[MAXWORDS];
35 
36 	int cs;
37 	int have = 0;
38 
39 
40 #line 41 "awkemu.c"
41 	{
42 	cs = awkemu_start;
43 	}
44 
45 #line 74 "awkemu.rl"
46 
47 	while ( 1 ) {
48 		char *p, *pe, *data = buf + have;
49 		int len, space = BUFSIZE - have;
50 		/* fprintf( stderr, "space: %i\n", space ); */
51 
52 		if ( space == 0 ) {
53 			fprintf(stderr, "buffer out of space\n");
54 			exit(1);
55 		}
56 
57 		len = fread( data, 1, space, stdin );
58 		/* fprintf( stderr, "len: %i\n", len ); */
59 		if ( len == 0 )
60 			break;
61 
62 		/* Find the last newline by searching backwards. This is where
63 		 * we will stop processing on this iteration. */
64 		p = buf;
65 		pe = buf + have + len - 1;
66 		while ( *pe != '\n' && pe >= buf )
67 			pe--;
68 		pe += 1;
69 
70 		/* fprintf( stderr, "running on: %i\n", pe - p ); */
71 
72 
73 #line 74 "awkemu.c"
74 	{
75 	if ( p == pe )
76 		goto _test_eof;
77 	switch ( cs )
78 	{
79 tr2:
80 #line 17 "awkemu.rl"
81 	{
82 		we[nwords++] = p;
83 	}
84 #line 26 "awkemu.rl"
85 	{
86 		printf("endline(%i): ", nwords );
87 		fwrite( ls, 1, p - ls, stdout );
88 		printf("\n");
89 
90 		for ( i = 0; i < nwords; i++ ) {
91 			printf("  word: ");
92 			fwrite( ws[i], 1, we[i] - ws[i], stdout );
93 			printf("\n");
94 		}
95 	}
96 	goto st2;
97 tr5:
98 #line 26 "awkemu.rl"
99 	{
100 		printf("endline(%i): ", nwords );
101 		fwrite( ls, 1, p - ls, stdout );
102 		printf("\n");
103 
104 		for ( i = 0; i < nwords; i++ ) {
105 			printf("  word: ");
106 			fwrite( ws[i], 1, we[i] - ws[i], stdout );
107 			printf("\n");
108 		}
109 	}
110 	goto st2;
111 tr8:
112 #line 21 "awkemu.rl"
113 	{
114 		nwords = 0;
115 		ls = p;
116 	}
117 #line 26 "awkemu.rl"
118 	{
119 		printf("endline(%i): ", nwords );
120 		fwrite( ls, 1, p - ls, stdout );
121 		printf("\n");
122 
123 		for ( i = 0; i < nwords; i++ ) {
124 			printf("  word: ");
125 			fwrite( ws[i], 1, we[i] - ws[i], stdout );
126 			printf("\n");
127 		}
128 	}
129 	goto st2;
130 st2:
131 	if ( ++p == pe )
132 		goto _test_eof2;
133 case 2:
134 #line 135 "awkemu.c"
135 	switch( (*p) ) {
136 		case 9: goto tr7;
137 		case 10: goto tr8;
138 		case 32: goto tr7;
139 	}
140 	goto tr6;
141 tr3:
142 #line 13 "awkemu.rl"
143 	{
144 		ws[nwords] = p;
145 	}
146 	goto st0;
147 tr6:
148 #line 21 "awkemu.rl"
149 	{
150 		nwords = 0;
151 		ls = p;
152 	}
153 #line 13 "awkemu.rl"
154 	{
155 		ws[nwords] = p;
156 	}
157 	goto st0;
158 st0:
159 	if ( ++p == pe )
160 		goto _test_eof0;
161 case 0:
162 #line 163 "awkemu.c"
163 	switch( (*p) ) {
164 		case 9: goto tr1;
165 		case 10: goto tr2;
166 		case 32: goto tr1;
167 	}
168 	goto st0;
169 tr1:
170 #line 17 "awkemu.rl"
171 	{
172 		we[nwords++] = p;
173 	}
174 	goto st1;
175 tr7:
176 #line 21 "awkemu.rl"
177 	{
178 		nwords = 0;
179 		ls = p;
180 	}
181 	goto st1;
182 st1:
183 	if ( ++p == pe )
184 		goto _test_eof1;
185 case 1:
186 #line 187 "awkemu.c"
187 	switch( (*p) ) {
188 		case 9: goto st1;
189 		case 10: goto tr5;
190 		case 32: goto st1;
191 	}
192 	goto tr3;
193 	}
194 	_test_eof2: cs = 2; goto _test_eof;
195 	_test_eof0: cs = 0; goto _test_eof;
196 	_test_eof1: cs = 1; goto _test_eof;
197 
198 	_test_eof: {}
199 	}
200 
201 #line 101 "awkemu.rl"
202 
203 		/* How much is still in the buffer. */
204 		have = data + len - pe;
205 		if ( have > 0 )
206 			memmove( buf, pe, have );
207 
208 		/* fprintf(stderr, "have: %i\n", have ); */
209 
210 		if ( len < space )
211 			break;
212 	}
213 
214 	if ( have > 0 )
215 		fprintf(stderr, "input not newline terminated\n");
216 	return 0;
217 }
218