glsl/apps: print usage info if insufficient args
[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 <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 struct sl_pp_context *context;
44 unsigned int version;
45 FILE *out;
46
47 if (argc != 3) {
48 printf("Usage: version infile outfile\n");
49 return 1;
50 }
51
52 in = fopen(argv[1], "rb");
53 if (!in) {
54 return 1;
55 }
56
57 fseek(in, 0, SEEK_END);
58 size = ftell(in);
59 fseek(in, 0, SEEK_SET);
60
61 out = fopen(argv[2], "wb");
62 if (!out) {
63 fclose(in);
64 return 1;
65 }
66
67 inbuf = malloc(size + 1);
68 if (!inbuf) {
69 fprintf(out, "$OOMERROR\n");
70
71 fclose(out);
72 fclose(in);
73 return 1;
74 }
75
76 if (fread(inbuf, 1, size, in) != size) {
77 fprintf(out, "$READERROR\n");
78
79 free(inbuf);
80 fclose(out);
81 fclose(in);
82 return 1;
83 }
84 inbuf[size] = '\0';
85
86 fclose(in);
87
88 memset(&options, 0, sizeof(options));
89
90 context = sl_pp_context_create(inbuf, &options);
91 if (!context) {
92 fprintf(out, "$CONTEXERROR\n");
93
94 free(inbuf);
95 fclose(out);
96 return 1;
97 }
98
99 if (sl_pp_version(context, &version)) {
100 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
101
102 sl_pp_context_destroy(context);
103 free(inbuf);
104 fclose(out);
105 return -1;
106 }
107
108 sl_pp_context_destroy(context);
109 free(inbuf);
110
111 fprintf(out, "%u\n", version);
112
113 fclose(out);
114
115 return 0;
116 }