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