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