glsl/apps: Assert that ftell does not return an error.
[mesa.git] / src / glsl / apps / tokenise.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33
34
35 int
36 main(int argc,
37 char *argv[])
38 {
39 FILE *in;
40 long size;
41 char *inbuf;
42 struct sl_pp_purify_options options;
43 struct sl_pp_context *context;
44 struct sl_pp_token_info *tokens;
45 FILE *out;
46 unsigned int i;
47
48 if (argc != 3) {
49 printf("Usage: tokenize infile outfile\n");
50 return 1;
51 }
52
53 in = fopen(argv[1], "rb");
54 if (!in) {
55 return 1;
56 }
57
58 fseek(in, 0, SEEK_END);
59 size = ftell(in);
60 assert(size != -1);
61 fseek(in, 0, SEEK_SET);
62
63 out = fopen(argv[2], "wb");
64 if (!out) {
65 fclose(in);
66 return 1;
67 }
68
69 inbuf = malloc(size + 1);
70 if (!inbuf) {
71 fprintf(out, "$OOMERROR\n");
72
73 fclose(out);
74 fclose(in);
75 return 1;
76 }
77
78 if (fread(inbuf, 1, size, in) != size) {
79 fprintf(out, "$READERROR\n");
80
81 free(inbuf);
82 fclose(out);
83 fclose(in);
84 return 1;
85 }
86 inbuf[size] = '\0';
87
88 fclose(in);
89
90 memset(&options, 0, sizeof(options));
91
92 context = sl_pp_context_create(inbuf, &options);
93 if (!context) {
94 fprintf(out, "$CONTEXERROR\n");
95
96 free(inbuf);
97 fclose(out);
98 return 1;
99 }
100
101 if (sl_pp_tokenise(context, &tokens)) {
102 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
103
104 sl_pp_context_destroy(context);
105 free(inbuf);
106 fclose(out);
107 return 1;
108 }
109
110 free(inbuf);
111
112 for (i = 0; tokens[i].token != SL_PP_EOF; i++) {
113 switch (tokens[i].token) {
114 case SL_PP_WHITESPACE:
115 break;
116
117 case SL_PP_NEWLINE:
118 fprintf(out, "\n");
119 break;
120
121 case SL_PP_HASH:
122 fprintf(out, "# ");
123 break;
124
125 case SL_PP_COMMA:
126 fprintf(out, ", ");
127 break;
128
129 case SL_PP_SEMICOLON:
130 fprintf(out, "; ");
131 break;
132
133 case SL_PP_LBRACE:
134 fprintf(out, "{ ");
135 break;
136
137 case SL_PP_RBRACE:
138 fprintf(out, "} ");
139 break;
140
141 case SL_PP_LPAREN:
142 fprintf(out, "( ");
143 break;
144
145 case SL_PP_RPAREN:
146 fprintf(out, ") ");
147 break;
148
149 case SL_PP_LBRACKET:
150 fprintf(out, "[ ");
151 break;
152
153 case SL_PP_RBRACKET:
154 fprintf(out, "] ");
155 break;
156
157 case SL_PP_DOT:
158 fprintf(out, ". ");
159 break;
160
161 case SL_PP_INCREMENT:
162 fprintf(out, "++ ");
163 break;
164
165 case SL_PP_ADDASSIGN:
166 fprintf(out, "+= ");
167 break;
168
169 case SL_PP_PLUS:
170 fprintf(out, "+ ");
171 break;
172
173 case SL_PP_DECREMENT:
174 fprintf(out, "-- ");
175 break;
176
177 case SL_PP_SUBASSIGN:
178 fprintf(out, "-= ");
179 break;
180
181 case SL_PP_MINUS:
182 fprintf(out, "- ");
183 break;
184
185 case SL_PP_BITNOT:
186 fprintf(out, "~ ");
187 break;
188
189 case SL_PP_NOTEQUAL:
190 fprintf(out, "!= ");
191 break;
192
193 case SL_PP_NOT:
194 fprintf(out, "! ");
195 break;
196
197 case SL_PP_MULASSIGN:
198 fprintf(out, "*= ");
199 break;
200
201 case SL_PP_STAR:
202 fprintf(out, "* ");
203 break;
204
205 case SL_PP_DIVASSIGN:
206 fprintf(out, "/= ");
207 break;
208
209 case SL_PP_SLASH:
210 fprintf(out, "/ ");
211 break;
212
213 case SL_PP_MODASSIGN:
214 fprintf(out, "%%= ");
215 break;
216
217 case SL_PP_MODULO:
218 fprintf(out, "%% ");
219 break;
220
221 case SL_PP_LSHIFTASSIGN:
222 fprintf(out, "<<= ");
223 break;
224
225 case SL_PP_LSHIFT:
226 fprintf(out, "<< ");
227 break;
228
229 case SL_PP_LESSEQUAL:
230 fprintf(out, "<= ");
231 break;
232
233 case SL_PP_LESS:
234 fprintf(out, "< ");
235 break;
236
237 case SL_PP_RSHIFTASSIGN:
238 fprintf(out, ">>= ");
239 break;
240
241 case SL_PP_RSHIFT:
242 fprintf(out, ">> ");
243 break;
244
245 case SL_PP_GREATEREQUAL:
246 fprintf(out, ">= ");
247 break;
248
249 case SL_PP_GREATER:
250 fprintf(out, "> ");
251 break;
252
253 case SL_PP_EQUAL:
254 fprintf(out, "== ");
255 break;
256
257 case SL_PP_ASSIGN:
258 fprintf(out, "= ");
259 break;
260
261 case SL_PP_AND:
262 fprintf(out, "&& ");
263 break;
264
265 case SL_PP_BITANDASSIGN:
266 fprintf(out, "&= ");
267 break;
268
269 case SL_PP_BITAND:
270 fprintf(out, "& ");
271 break;
272
273 case SL_PP_XOR:
274 fprintf(out, "^^ ");
275 break;
276
277 case SL_PP_BITXORASSIGN:
278 fprintf(out, "^= ");
279 break;
280
281 case SL_PP_BITXOR:
282 fprintf(out, "^ ");
283 break;
284
285 case SL_PP_OR:
286 fprintf(out, "|| ");
287 break;
288
289 case SL_PP_BITORASSIGN:
290 fprintf(out, "|= ");
291 break;
292
293 case SL_PP_BITOR:
294 fprintf(out, "| ");
295 break;
296
297 case SL_PP_QUESTION:
298 fprintf(out, "? ");
299 break;
300
301 case SL_PP_COLON:
302 fprintf(out, ": ");
303 break;
304
305 case SL_PP_IDENTIFIER:
306 fprintf(out, "%s ", sl_pp_context_cstr(context, tokens[i].data.identifier));
307 break;
308
309 case SL_PP_UINT:
310 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._uint));
311 break;
312
313 case SL_PP_FLOAT:
314 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._float));
315 break;
316
317 case SL_PP_OTHER:
318 if (tokens[i].data.other == '\'') {
319 fprintf(out, "'\\'' ");
320 } else {
321 fprintf(out, "'%c' ", tokens[i].data.other);
322 }
323 break;
324
325 default:
326 assert(0);
327 }
328 }
329
330 sl_pp_context_destroy(context);
331 free(tokens);
332 fclose(out);
333
334 return 0;
335 }