1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 #include "../pp/sl_pp_public.h"
42 struct sl_pp_purify_options options
;
44 char errmsg
[100] = "";
45 unsigned int errline
= 0;
46 struct sl_pp_context
*context
;
47 struct sl_pp_token_info
*tokens
;
49 unsigned int tokens_eaten
;
50 struct sl_pp_token_info
*outtokens
;
58 in
= fopen(argv
[1], "rb");
63 fseek(in
, 0, SEEK_END
);
65 fseek(in
, 0, SEEK_SET
);
67 out
= fopen(argv
[2], "wb");
73 inbuf
= malloc(size
+ 1);
75 fprintf(out
, "$OOMERROR\n");
82 if (fread(inbuf
, 1, size
, in
) != size
) {
83 fprintf(out
, "$READERROR\n");
94 memset(&options
, 0, sizeof(options
));
96 if (sl_pp_purify(inbuf
, &options
, &outbuf
, errmsg
, sizeof(errmsg
), &errline
)) {
97 fprintf(out
, "$PURIFYERROR %s\n", errmsg
);
106 context
= sl_pp_context_create();
108 fprintf(out
, "$CONTEXERROR\n");
115 if (sl_pp_tokenise(context
, outbuf
, &tokens
)) {
116 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
118 sl_pp_context_destroy(context
);
126 if (sl_pp_version(context
, tokens
, &version
, &tokens_eaten
)) {
127 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
129 sl_pp_context_destroy(context
);
135 if (sl_pp_process(context
, &tokens
[tokens_eaten
], &outtokens
)) {
136 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
138 sl_pp_context_destroy(context
);
146 for (i
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
147 switch (outtokens
[i
].token
) {
156 case SL_PP_SEMICOLON
:
188 case SL_PP_INCREMENT
:
192 case SL_PP_ADDASSIGN
:
200 case SL_PP_DECREMENT
:
204 case SL_PP_SUBASSIGN
:
224 case SL_PP_MULASSIGN
:
232 case SL_PP_DIVASSIGN
:
240 case SL_PP_MODASSIGN
:
241 fprintf(out
, "%%= ");
248 case SL_PP_LSHIFTASSIGN
:
249 fprintf(out
, "<<= ");
256 case SL_PP_LESSEQUAL
:
264 case SL_PP_RSHIFTASSIGN
:
265 fprintf(out
, ">>= ");
272 case SL_PP_GREATEREQUAL
:
292 case SL_PP_BITANDASSIGN
:
304 case SL_PP_BITXORASSIGN
:
316 case SL_PP_BITORASSIGN
:
332 case SL_PP_IDENTIFIER
:
333 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
.identifier
));
337 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._uint
));
341 fprintf(out
, "%s ", sl_pp_context_cstr(context
, outtokens
[i
].data
._float
));
345 fprintf(out
, "%c", outtokens
[i
].data
.other
);
348 case SL_PP_PRAGMA_OPTIMIZE
:
349 fprintf(out
, "#pragma optimize(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
352 case SL_PP_PRAGMA_DEBUG
:
353 fprintf(out
, "#pragma debug(%s)", outtokens
[i
].data
.pragma
? "on" : "off");
356 case SL_PP_EXTENSION_REQUIRE
:
357 fprintf(out
, "#extension %s : require", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
360 case SL_PP_EXTENSION_ENABLE
:
361 fprintf(out
, "#extension %s : enable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
364 case SL_PP_EXTENSION_WARN
:
365 fprintf(out
, "#extension %s : warn", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
368 case SL_PP_EXTENSION_DISABLE
:
369 fprintf(out
, "#extension %s : disable", sl_pp_context_cstr(context
, outtokens
[i
].data
.extension
));
373 fprintf(out
, "#line %u %u", outtokens
[i
].data
.line
.lineno
, outtokens
[i
].data
.line
.fileno
);
381 sl_pp_context_destroy(context
);