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