glsl/apps: Assert that ftell does not return an error.
[mesa.git] / src / glsl / apps / process.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 unsigned int version;
45 struct sl_pp_token_info *outtokens;
46 FILE *out;
47 unsigned int i;
48
49 if (argc != 3) {
50 printf("Usage: process infile outfile\n");
51 return 1;
52 }
53
54 in = fopen(argv[1], "rb");
55 if (!in) {
56 return 1;
57 }
58
59 fseek(in, 0, SEEK_END);
60 size = ftell(in);
61 assert(size != -1);
62 fseek(in, 0, SEEK_SET);
63
64 out = fopen(argv[2], "wb");
65 if (!out) {
66 fclose(in);
67 return 1;
68 }
69
70 inbuf = malloc(size + 1);
71 if (!inbuf) {
72 fprintf(out, "$OOMERROR\n");
73
74 fclose(out);
75 fclose(in);
76 return 1;
77 }
78
79 if (fread(inbuf, 1, size, in) != size) {
80 fprintf(out, "$READERROR\n");
81
82 free(inbuf);
83 fclose(out);
84 fclose(in);
85 return 1;
86 }
87 inbuf[size] = '\0';
88
89 fclose(in);
90
91 memset(&options, 0, sizeof(options));
92
93 context = sl_pp_context_create(inbuf, &options);
94 if (!context) {
95 fprintf(out, "$CONTEXERROR\n");
96
97 free(inbuf);
98 fclose(out);
99 return 1;
100 }
101
102 if (sl_pp_version(context, &version)) {
103 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
104
105 sl_pp_context_destroy(context);
106 free(inbuf);
107 fclose(out);
108 return -1;
109 }
110
111 if (sl_pp_context_add_extension(context, "GL_ARB_draw_buffers") ||
112 sl_pp_context_add_extension(context, "GL_ARB_texture_rectangle")) {
113 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
114
115 printf("Error: %s\n", sl_pp_context_error_message(context));
116 sl_pp_context_destroy(context);
117 free(inbuf);
118 fclose(out);
119 return 0;
120 }
121
122 if (sl_pp_context_add_predefined(context, "__GLSL_PP_PREDEFINED_MACRO_TEST", "1")) {
123 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
124
125 printf("Error: %s\n", sl_pp_context_error_message(context));
126 sl_pp_context_destroy(context);
127 free(inbuf);
128 fclose(out);
129 return 0;
130 }
131
132 if (sl_pp_process(context, &outtokens)) {
133 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
134
135 sl_pp_context_destroy(context);
136 free(inbuf);
137 fclose(out);
138 return -1;
139 }
140
141 free(inbuf);
142
143 for (i = 0; outtokens[i].token != SL_PP_EOF; i++) {
144 switch (outtokens[i].token) {
145 case SL_PP_NEWLINE:
146 fprintf(out, "\n");
147 break;
148
149 case SL_PP_COMMA:
150 fprintf(out, ", ");
151 break;
152
153 case SL_PP_SEMICOLON:
154 fprintf(out, "; ");
155 break;
156
157 case SL_PP_LBRACE:
158 fprintf(out, "{ ");
159 break;
160
161 case SL_PP_RBRACE:
162 fprintf(out, "} ");
163 break;
164
165 case SL_PP_LPAREN:
166 fprintf(out, "( ");
167 break;
168
169 case SL_PP_RPAREN:
170 fprintf(out, ") ");
171 break;
172
173 case SL_PP_LBRACKET:
174 fprintf(out, "[ ");
175 break;
176
177 case SL_PP_RBRACKET:
178 fprintf(out, "] ");
179 break;
180
181 case SL_PP_DOT:
182 fprintf(out, ". ");
183 break;
184
185 case SL_PP_INCREMENT:
186 fprintf(out, "++ ");
187 break;
188
189 case SL_PP_ADDASSIGN:
190 fprintf(out, "+= ");
191 break;
192
193 case SL_PP_PLUS:
194 fprintf(out, "+ ");
195 break;
196
197 case SL_PP_DECREMENT:
198 fprintf(out, "-- ");
199 break;
200
201 case SL_PP_SUBASSIGN:
202 fprintf(out, "-= ");
203 break;
204
205 case SL_PP_MINUS:
206 fprintf(out, "- ");
207 break;
208
209 case SL_PP_BITNOT:
210 fprintf(out, "~ ");
211 break;
212
213 case SL_PP_NOTEQUAL:
214 fprintf(out, "!= ");
215 break;
216
217 case SL_PP_NOT:
218 fprintf(out, "! ");
219 break;
220
221 case SL_PP_MULASSIGN:
222 fprintf(out, "*= ");
223 break;
224
225 case SL_PP_STAR:
226 fprintf(out, "* ");
227 break;
228
229 case SL_PP_DIVASSIGN:
230 fprintf(out, "/= ");
231 break;
232
233 case SL_PP_SLASH:
234 fprintf(out, "/ ");
235 break;
236
237 case SL_PP_MODASSIGN:
238 fprintf(out, "%%= ");
239 break;
240
241 case SL_PP_MODULO:
242 fprintf(out, "%% ");
243 break;
244
245 case SL_PP_LSHIFTASSIGN:
246 fprintf(out, "<<= ");
247 break;
248
249 case SL_PP_LSHIFT:
250 fprintf(out, "<< ");
251 break;
252
253 case SL_PP_LESSEQUAL:
254 fprintf(out, "<= ");
255 break;
256
257 case SL_PP_LESS:
258 fprintf(out, "< ");
259 break;
260
261 case SL_PP_RSHIFTASSIGN:
262 fprintf(out, ">>= ");
263 break;
264
265 case SL_PP_RSHIFT:
266 fprintf(out, ">> ");
267 break;
268
269 case SL_PP_GREATEREQUAL:
270 fprintf(out, ">= ");
271 break;
272
273 case SL_PP_GREATER:
274 fprintf(out, "> ");
275 break;
276
277 case SL_PP_EQUAL:
278 fprintf(out, "== ");
279 break;
280
281 case SL_PP_ASSIGN:
282 fprintf(out, "= ");
283 break;
284
285 case SL_PP_AND:
286 fprintf(out, "&& ");
287 break;
288
289 case SL_PP_BITANDASSIGN:
290 fprintf(out, "&= ");
291 break;
292
293 case SL_PP_BITAND:
294 fprintf(out, "& ");
295 break;
296
297 case SL_PP_XOR:
298 fprintf(out, "^^ ");
299 break;
300
301 case SL_PP_BITXORASSIGN:
302 fprintf(out, "^= ");
303 break;
304
305 case SL_PP_BITXOR:
306 fprintf(out, "^ ");
307 break;
308
309 case SL_PP_OR:
310 fprintf(out, "|| ");
311 break;
312
313 case SL_PP_BITORASSIGN:
314 fprintf(out, "|= ");
315 break;
316
317 case SL_PP_BITOR:
318 fprintf(out, "| ");
319 break;
320
321 case SL_PP_QUESTION:
322 fprintf(out, "? ");
323 break;
324
325 case SL_PP_COLON:
326 fprintf(out, ": ");
327 break;
328
329 case SL_PP_IDENTIFIER:
330 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data.identifier));
331 break;
332
333 case SL_PP_UINT:
334 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data._uint));
335 break;
336
337 case SL_PP_FLOAT:
338 fprintf(out, "%s ", sl_pp_context_cstr(context, outtokens[i].data._float));
339 break;
340
341 case SL_PP_OTHER:
342 fprintf(out, "%c", outtokens[i].data.other);
343 break;
344
345 case SL_PP_PRAGMA_OPTIMIZE:
346 fprintf(out, "#pragma optimize(%s)", outtokens[i].data.pragma ? "on" : "off");
347 break;
348
349 case SL_PP_PRAGMA_DEBUG:
350 fprintf(out, "#pragma debug(%s)", outtokens[i].data.pragma ? "on" : "off");
351 break;
352
353 case SL_PP_EXTENSION_REQUIRE:
354 fprintf(out, "#extension %s : require", sl_pp_context_cstr(context, outtokens[i].data.extension));
355 break;
356
357 case SL_PP_EXTENSION_ENABLE:
358 fprintf(out, "#extension %s : enable", sl_pp_context_cstr(context, outtokens[i].data.extension));
359 break;
360
361 case SL_PP_EXTENSION_WARN:
362 fprintf(out, "#extension %s : warn", sl_pp_context_cstr(context, outtokens[i].data.extension));
363 break;
364
365 case SL_PP_EXTENSION_DISABLE:
366 fprintf(out, "#extension %s : disable", sl_pp_context_cstr(context, outtokens[i].data.extension));
367 break;
368
369 case SL_PP_LINE:
370 fprintf(out, "#line %u %u", outtokens[i].data.line.lineno, outtokens[i].data.line.fileno);
371 break;
372
373 default:
374 assert(0);
375 }
376 }
377
378 sl_pp_context_destroy(context);
379 free(outtokens);
380 fclose(out);
381
382 return 0;
383 }