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"
33 #include "../cl/sl_cl_parse.h"
43 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
;
53 unsigned char *outbytes
;
54 unsigned int cboutbytes
;
55 unsigned int shader_type
;
61 if (!strcmp(argv
[1], "fragment")) {
63 } else if (!strcmp(argv
[1], "vertex")) {
69 in
= fopen(argv
[2], "rb");
74 fseek(in
, 0, SEEK_END
);
76 fseek(in
, 0, SEEK_SET
);
78 out
= fopen(argv
[3], "w");
84 inbuf
= malloc(size
+ 1);
86 fprintf(out
, "$OOMERROR\n");
93 if (fread(inbuf
, 1, size
, in
) != size
) {
94 fprintf(out
, "$READERROR\n");
105 memset(&options
, 0, sizeof(options
));
107 context
= sl_pp_context_create();
109 fprintf(out
, "$CONTEXERROR\n");
116 if (sl_pp_tokenise(context
, inbuf
, &options
, &tokens
)) {
117 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
119 sl_pp_context_destroy(context
);
127 if (sl_pp_version(context
, tokens
, &version
, &tokens_eaten
)) {
128 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
130 sl_pp_context_destroy(context
);
136 if (sl_pp_process(context
, &tokens
[tokens_eaten
], &outtokens
)) {
137 fprintf(out
, "$ERROR: `%s'\n", sl_pp_context_error_message(context
));
139 sl_pp_context_destroy(context
);
147 for (i
= j
= 0; outtokens
[i
].token
!= SL_PP_EOF
; i
++) {
148 switch (outtokens
[i
].token
) {
150 case SL_PP_EXTENSION_REQUIRE
:
151 case SL_PP_EXTENSION_ENABLE
:
152 case SL_PP_EXTENSION_WARN
:
153 case SL_PP_EXTENSION_DISABLE
:
157 outtokens
[j
++] = outtokens
[i
];
160 outtokens
[j
] = outtokens
[i
];
162 if (sl_cl_compile(context
, outtokens
, shader_type
, 1, &outbytes
, &cboutbytes
, errmsg
, sizeof(errmsg
)) == 0) {
164 unsigned int line
= 0;
166 fprintf(out
, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
167 fprintf(out
, "\n/* %s */", argv
[2]);
168 fprintf(out
, "\n\n");
170 for (i
= 0; i
< cboutbytes
; i
++) {
173 if (outbytes
[i
] < 10) {
175 } else if (outbytes
[i
] < 100) {
180 if (i
< cboutbytes
- 1) {
183 if (line
+ a
>= 100) {
188 fprintf (out
, "%u", outbytes
[i
]);
189 if (i
< cboutbytes
- 1) {
196 fprintf(out
, "$SYNTAXERROR: `%s'\n", errmsg
);
200 sl_pp_context_destroy(context
);