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