glsl/apps: Include missing header, properly escape format strings.
[mesa.git] / src / glsl / apps / tokenise.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 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 if (sl_pp_purify(inbuf, &options, &outbuf)) {
92 fprintf(out, "$PURIFYERROR\n");
93
94 free(inbuf);
95 fclose(out);
96 return 1;
97 }
98
99 free(inbuf);
100
101 context = sl_pp_context_create();
102 if (!context) {
103 fprintf(out, "$CONTEXERROR\n");
104
105 free(outbuf);
106 fclose(out);
107 return 1;
108 }
109
110 if (sl_pp_tokenise(context, outbuf, &tokens)) {
111 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
112
113 sl_pp_context_destroy(context);
114 free(outbuf);
115 fclose(out);
116 return 1;
117 }
118
119 free(outbuf);
120
121 for (i = 0; tokens[i].token != SL_PP_EOF; i++) {
122 switch (tokens[i].token) {
123 case SL_PP_WHITESPACE:
124 break;
125
126 case SL_PP_NEWLINE:
127 fprintf(out, "\n");
128 break;
129
130 case SL_PP_HASH:
131 fprintf(out, "# ");
132 break;
133
134 case SL_PP_COMMA:
135 fprintf(out, ", ");
136 break;
137
138 case SL_PP_SEMICOLON:
139 fprintf(out, "; ");
140 break;
141
142 case SL_PP_LBRACE:
143 fprintf(out, "{ ");
144 break;
145
146 case SL_PP_RBRACE:
147 fprintf(out, "} ");
148 break;
149
150 case SL_PP_LPAREN:
151 fprintf(out, "( ");
152 break;
153
154 case SL_PP_RPAREN:
155 fprintf(out, ") ");
156 break;
157
158 case SL_PP_LBRACKET:
159 fprintf(out, "[ ");
160 break;
161
162 case SL_PP_RBRACKET:
163 fprintf(out, "] ");
164 break;
165
166 case SL_PP_DOT:
167 fprintf(out, ". ");
168 break;
169
170 case SL_PP_INCREMENT:
171 fprintf(out, "++ ");
172 break;
173
174 case SL_PP_ADDASSIGN:
175 fprintf(out, "+= ");
176 break;
177
178 case SL_PP_PLUS:
179 fprintf(out, "+ ");
180 break;
181
182 case SL_PP_DECREMENT:
183 fprintf(out, "-- ");
184 break;
185
186 case SL_PP_SUBASSIGN:
187 fprintf(out, "-= ");
188 break;
189
190 case SL_PP_MINUS:
191 fprintf(out, "- ");
192 break;
193
194 case SL_PP_BITNOT:
195 fprintf(out, "~ ");
196 break;
197
198 case SL_PP_NOTEQUAL:
199 fprintf(out, "!= ");
200 break;
201
202 case SL_PP_NOT:
203 fprintf(out, "! ");
204 break;
205
206 case SL_PP_MULASSIGN:
207 fprintf(out, "*= ");
208 break;
209
210 case SL_PP_STAR:
211 fprintf(out, "* ");
212 break;
213
214 case SL_PP_DIVASSIGN:
215 fprintf(out, "/= ");
216 break;
217
218 case SL_PP_SLASH:
219 fprintf(out, "/ ");
220 break;
221
222 case SL_PP_MODASSIGN:
223 fprintf(out, "%%= ");
224 break;
225
226 case SL_PP_MODULO:
227 fprintf(out, "%% ");
228 break;
229
230 case SL_PP_LSHIFTASSIGN:
231 fprintf(out, "<<= ");
232 break;
233
234 case SL_PP_LSHIFT:
235 fprintf(out, "<< ");
236 break;
237
238 case SL_PP_LESSEQUAL:
239 fprintf(out, "<= ");
240 break;
241
242 case SL_PP_LESS:
243 fprintf(out, "< ");
244 break;
245
246 case SL_PP_RSHIFTASSIGN:
247 fprintf(out, ">>= ");
248 break;
249
250 case SL_PP_RSHIFT:
251 fprintf(out, ">> ");
252 break;
253
254 case SL_PP_GREATEREQUAL:
255 fprintf(out, ">= ");
256 break;
257
258 case SL_PP_GREATER:
259 fprintf(out, "> ");
260 break;
261
262 case SL_PP_EQUAL:
263 fprintf(out, "== ");
264 break;
265
266 case SL_PP_ASSIGN:
267 fprintf(out, "= ");
268 break;
269
270 case SL_PP_AND:
271 fprintf(out, "&& ");
272 break;
273
274 case SL_PP_BITANDASSIGN:
275 fprintf(out, "&= ");
276 break;
277
278 case SL_PP_BITAND:
279 fprintf(out, "& ");
280 break;
281
282 case SL_PP_XOR:
283 fprintf(out, "^^ ");
284 break;
285
286 case SL_PP_BITXORASSIGN:
287 fprintf(out, "^= ");
288 break;
289
290 case SL_PP_BITXOR:
291 fprintf(out, "^ ");
292 break;
293
294 case SL_PP_OR:
295 fprintf(out, "|| ");
296 break;
297
298 case SL_PP_BITORASSIGN:
299 fprintf(out, "|= ");
300 break;
301
302 case SL_PP_BITOR:
303 fprintf(out, "| ");
304 break;
305
306 case SL_PP_QUESTION:
307 fprintf(out, "? ");
308 break;
309
310 case SL_PP_COLON:
311 fprintf(out, ": ");
312 break;
313
314 case SL_PP_IDENTIFIER:
315 fprintf(out, "%s ", sl_pp_context_cstr(context, tokens[i].data.identifier));
316 break;
317
318 case SL_PP_UINT:
319 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._uint));
320 break;
321
322 case SL_PP_FLOAT:
323 fprintf(out, "(%s) ", sl_pp_context_cstr(context, tokens[i].data._float));
324 break;
325
326 case SL_PP_OTHER:
327 if (tokens[i].data.other == '\'') {
328 fprintf(out, "'\\'' ");
329 } else {
330 fprintf(out, "'%c' ", tokens[i].data.other);
331 }
332 break;
333
334 default:
335 assert(0);
336 }
337 }
338
339 sl_pp_context_destroy(context);
340 free(tokens);
341 fclose(out);
342
343 return 0;
344 }