glsl/apps: Allow builds on all platforms.
[mesa.git] / src / glsl / apps / version.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_public.h"
32
33
34 int
35 main(int argc,
36 char *argv[])
37 {
38 FILE *in;
39 long size;
40 char *inbuf;
41 struct sl_pp_purify_options options;
42 char *outbuf;
43 struct sl_pp_context *context;
44 struct sl_pp_token_info *tokens;
45 unsigned int version;
46 unsigned int tokens_eaten;
47 FILE *out;
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 if (sl_pp_version(context, tokens, &version, &tokens_eaten)) {
122 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
123
124 sl_pp_context_destroy(context);
125 free(tokens);
126 fclose(out);
127 return -1;
128 }
129
130 sl_pp_context_destroy(context);
131 free(tokens);
132
133 fprintf(out,
134 "%u\n%u\n",
135 version,
136 tokens_eaten);
137
138 fclose(out);
139
140 return 0;
141 }