]> git.cworth.org Git - glenv/blob - glenv.c
Make the three --extensions* options mutually exclusive.
[glenv] / glenv.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <limits.h>
26 #include <getopt.h>
27
28 #include <glaze.h>
29
30 typedef struct options {
31         bool query;
32         char *vendor;
33         char *renderer;
34         char *version;
35         char *shading_language_version;
36         char *extensions;
37         char *extensions_whitelist;
38         char *extensions_blacklist;
39 } options_t;
40
41 static void
42 export_options (options_t *options)
43 {
44         if (options->query) {
45                 glaze_set_first_gl_call_callback ("glenv_query");
46         }
47
48         if (options->vendor)
49                 setenv ("GLENV_GL_VENDOR", options->vendor, 1);
50         else
51                 unsetenv ("GLENV_GL_VENDOR");
52
53         if (options->renderer)
54                 setenv ("GLENV_GL_RENDERER", options->renderer, 1);
55         else
56                 unsetenv ("GLENV_GL_RENDERER");
57
58         if (options->version)
59                 setenv ("GLENV_GL_VERSION", options->version, 1);
60         else
61                 unsetenv ("GLENV_GL_VERSION");
62
63         if (options->shading_language_version)
64                 setenv ("GLENV_GL_SHADING_LANGUAGE_VERSION",
65                         options->shading_language_version, 1);
66         else
67                 unsetenv ("GLENV_GL_SHADING_LANGUAGE_VERSION");
68
69         if (options->extensions)
70                 setenv ("GLENV_EXTENSIONS", options->extensions, 1);
71         else
72                 unsetenv ("GLENV_EXTENSIONS");
73
74         if (options->extensions_whitelist)
75                 setenv ("GLENV_EXTENSIONS_WHITELIST", options->extensions_whitelist, 1);
76         else
77                 unsetenv ("GLENV_EXTENSIONS_WHITELIST");
78
79         if (options->extensions_blacklist)
80                 setenv ("GLENV_EXTENSIONS_BLACKLIST", options->extensions_blacklist, 1);
81         else
82                 unsetenv ("GLENV_EXTENSIONS_BLACKLIST");
83 }
84
85 static void
86 usage (void)
87 {
88         printf ("Usage: glenv [OPTIONS...] <program> [program args...]\n"
89                 "\n"
90                 "Execute <program> with alternate OpenGL environment.\n"
91                 "\n"
92                 "Options:\n"
93                 "       -h, --help                      Show this help message.\n"
94                 "       -q, --query                     Query and report current environment\n"
95                 "                                       then terminate program.\n"
96                 "       --vendor=STR                    Set GL_VENDOR to STR.\n"
97                 "       --renderer=STR                  Set GL_RENDERER to STR.\n"
98                 "       --version=STR                   Set GL_VERSION to STR.\n"
99                 "       --shading-language-version=STR  Set GL_SHADING_LANGUAGE_VERSION to STR.\n"
100                 "       --extensions=STR                Set GL_EXTENSIONS to STR.\n"
101                 "       --extensions-whitelist=STR      Remove from GL_EXTENSIONS all names\n"
102                 "                                       not appearing in STR (space-separated).\n"
103                 "       --extensions-blacklist=STR      Remove from GL_EXTENSIONS any names\n"
104                 "                                       appearing in STR (space-separated).\n");
105 }
106
107 enum {
108         VENDOR_OPT = CHAR_MAX + 1,
109         RENDERER_OPT,
110         VERSION_OPT,
111         SHADING_LANGUAGE_VERSION_OPT,
112         EXTENSIONS_OPT,
113         EXTENSIONS_WHITELIST_OPT,
114         EXTENSIONS_BLACKLIST_OPT
115 };
116
117 static void
118 invalid_combination (const char *second, const char *first)
119 {
120         fprintf (stderr, "Error: Option %s cannot be specified after %s\n",
121                  second, first);
122         exit (1);
123 }
124
125 int
126 main (int argc, char *argv[])
127 {
128         int opt;
129         options_t options = {
130                 .query = false,
131                 .vendor = NULL,
132                 .renderer = NULL,
133                 .version = NULL,
134                 .shading_language_version = NULL,
135                 .extensions = NULL,
136                 .extensions_whitelist = NULL,
137                 .extensions_blacklist = NULL
138         };
139
140         const char *short_options="hq";
141         const struct option long_options[] = {
142                 {"help", no_argument, 0, 'h'},
143                 {"query", no_argument, 0, 'q'},
144                 {"vendor", required_argument, 0, VENDOR_OPT},
145                 {"renderer", required_argument, 0, RENDERER_OPT},
146                 {"version", required_argument, 0, VERSION_OPT},
147                 {"shading-language-version", required_argument, 0, SHADING_LANGUAGE_VERSION_OPT},
148                 {"extensions", required_argument, 0, EXTENSIONS_OPT},
149                 {"extensions-whitelist", required_argument, 0, EXTENSIONS_WHITELIST_OPT},
150                 {"extensions-blacklist", required_argument, 0, EXTENSIONS_BLACKLIST_OPT},
151                 {0, 0, 0, 0}
152         };
153
154         while (1)
155         {
156                 opt = getopt_long (argc, argv, short_options, long_options, NULL);
157                 if (opt == -1)
158                         break;
159
160                 switch (opt) {
161                 case 'h':
162                         usage ();
163                         return 0;
164                 case '?':
165                         exit (1);
166                         break;
167                 case 'q':
168                         options.query = true;
169                         break;
170                 case VENDOR_OPT:
171                         options.vendor = optarg;
172                         break;
173                 case RENDERER_OPT:
174                         options.renderer = optarg;
175                         break;
176                 case VERSION_OPT:
177                         options.version = optarg;
178                         break;
179                 case SHADING_LANGUAGE_VERSION_OPT:
180                         options.shading_language_version = optarg;
181                         break;
182                 case EXTENSIONS_OPT:
183                         if (options.extensions_whitelist)
184                                 invalid_combination ("--extensions", "--extensions-whitelist");
185                         if (options.extensions_blacklist)
186                                 invalid_combination ("--extensions", "--extensions-blacklist");
187                         options.extensions = optarg;
188                         break;
189                 case EXTENSIONS_WHITELIST_OPT:
190                         if (options.extensions)
191                                 invalid_combination ("--extensions-whitelist", "--extensions");
192                         if (options.extensions_blacklist)
193                                 invalid_combination ("--extensions-whitelist", "--extensions-blacklist");
194                         options.extensions_whitelist = optarg;
195                         break;
196                 case EXTENSIONS_BLACKLIST_OPT:
197                         if (options.extensions)
198                                 invalid_combination ("--extensions-blacklist", "--extensions");
199                         if (options.extensions_whitelist)
200                                 invalid_combination ("--extensions-blacklist", "--extensions-whitelist");
201                         options.extensions_blacklist = optarg;
202                         break;
203                 default:
204                         fprintf (stderr, "Internal error: "
205                                  "unexpected getopt value: %d\n", opt);
206                         exit (1);
207                 }
208         }
209
210         if (optind >= argc) {
211                 fprintf (stderr, "Error: No program name provided, "
212                          "see (glenv --help)\n");
213                 exit (1);
214         }
215
216         export_options (&options);
217
218         glaze_execute (argc - optind, &argv[optind], "libglenv.so");
219
220         /* If glaze_execute returns then something went wrong. */
221         return 1;
222 }