1 /*
2 options.c
3
4 command line options handling
5
6 Copyright (C) 2002 Colin Thompson
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17 See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to:
21
22 Free Software Foundation, Inc.
23 59 Temple Place - Suite 330
24 Boston, MA 02111-1307, USA
25
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif
37 #include <getopt.h>
38 #include <errno.h>
39 #include <stdlib.h>
40
41 #include "QF/dstring.h"
42 #include "QF/mathlib.h"
43 #include "QF/qtypes.h"
44
45 #include "compat.h"
46
47 #include "entities.h"
48 #include "options.h"
49 #include "properties.h"
50
51 const char *this_program;
52
53 static struct option const long_options[] = {
54 {"quiet", no_argument, 0, 'q'},
55 {"verbose", no_argument, 0, 'v'},
56 {"help", no_argument, 0, 'h'},
57 {"version", no_argument, 0, 'V'},
58 {"threads", required_argument, 0, 't'},
59 {"properties", required_argument, 0, 'P'},
60 {"attenuation", required_argument, 0, 'a'},
61 {"noise", required_argument, 0, 'n'},
62 {"cutoff", required_argument, 0, 'c'},
63 {"extra", required_argument, 0, 'e'},
64 {"novis", no_argument, 0, 'N'},
65 {"distance", required_argument, 0, 'd'},
66 {"range", required_argument, 0, 'r'},
67 {"file", required_argument, 0, 'f'},
68 {NULL, 0, NULL, 0}
69 };
70
71 static const char *short_options =
72 "q" // quiet
73 "v" // verbose
74 "h" // help
75 "V" // version
76 "N" // novis
77 "t:" // threads
78 "a:" // attenuation
79 "n:" // noise
80 "c:" // cutoff
81 "e:" // extra sampling
82 "d:" // scale distance
83 "r:" // scale range
84 "P:" // properties file
85 "f:"
86 ;
87
88 void
usage(int status)89 usage (int status)
90 {
91 printf ("%s - QuakeForge light tool\n", this_program);
92 printf ("Usage: %s [options] bspfile\n", this_program);
93 printf (
94 "Options:\n"
95 " -q, --quiet Inhibit usual output\n"
96 " -v, --verbose Display more output than usual\n"
97 " -h, --help Display this help and exit\n"
98 " -V, --version Output version information and exit\n"
99 " -N, --novis Don't use vis data\n"
100 " -t, --threads [num] Number of threads to use\n"
101 " -e, --extra [num] Apply extra sampling\n"
102 " -d, --distance [scale] Scale distance\n"
103 " -r, --range [scale] Scale range\n"
104 " -a, --attenuation [type] linear,radius,inverse,realistic,none,\n"
105 " havoc\n"
106 " -n, --noise [factor] Scale noise. 0 (default) to disable\n"
107 " -c, --cutoff [scale] Scale cutoff. 0 to disable\n"
108 " -P, --properties [file] Properties file\n"
109 " -f, --file [bspfile] BSP file\n\n");
110 exit (status);
111 }
112
113 int
DecodeArgs(int argc,char ** argv)114 DecodeArgs (int argc, char **argv)
115 {
116 int c;
117 char *eptr;
118
119 memset (&options, 0, sizeof (options));
120 options.verbosity = 0;
121 options.threads = 1;
122 options.distance = 1.0;
123 options.range = 0.5;
124 options.globallightscale = 1.0;
125 options.cutoff = 1.0;
126 options.attenuation = LIGHT_LINEAR;
127 bspfile = NULL;
128
129 while ((c = getopt_long (argc, argv, short_options, long_options, 0))
130 != EOF) {
131 switch (c) {
132 case 'q': // quiet
133 options.verbosity -= 1;
134 break;
135 case 'v': // verbose
136 options.verbosity += 1;
137 break;
138 case 'h': // help
139 usage (0);
140 break;
141 case 'N': // novis
142 options.novis = 1;
143 break;
144 case 'V': // version
145 printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
146 exit (0);
147 break;
148 case 't': // threads
149 options.threads = strtol (optarg, &eptr, 10);
150 if (eptr == optarg || *eptr)
151 usage (1);
152 break;
153 case 'a':
154 if ((options.attenuation = parse_attenuation (optarg)) == -1)
155 usage (1);
156 break;
157 case 'n': // noise
158 options.noise = strtod (optarg, &eptr);
159 if (eptr == optarg || *eptr)
160 usage (1);
161 options.noise = bound (0, options.extrabit, 2);
162 break;
163 case 'c': // cutoff
164 options.cutoff = strtod (optarg, &eptr);
165 if (eptr == optarg || *eptr)
166 usage (1);
167 options.cutoff = bound (0, options.extrabit, 2);
168 break;
169 case 'e': // extra
170 options.extrabit = strtol (optarg, &eptr, 10);
171 if (eptr == optarg || *eptr)
172 usage (1);
173 options.extrabit = bound (0, options.extrabit, 2);
174 break;
175 case 'd': // scale distance
176 options.distance = strtod (optarg, &eptr);
177 if (eptr == optarg || *eptr)
178 usage (1);
179 break;
180 case 'r': // scale range
181 options.range = strtod (optarg, &eptr);
182 if (eptr == optarg || *eptr)
183 usage (1);
184 break;
185 case 'f':
186 bspfile = dstring_strdup (optarg);
187 break;
188 case 'P':
189 options.properties_filename = strdup (optarg);
190 break;
191 default:
192 usage (1);
193 }
194 }
195 options.extrascale = 1.0 / (1 << (options.extrabit * 2));
196 if ((!bspfile) && argv[optind] && *(argv[optind]))
197 bspfile = dstring_strdup (argv[optind++]);
198
199 return optind;
200 }
201