11 int opt_joystick_enabled;
12 int opt_joystick_number;
13 int opt_joystick_x_axis;
14 int opt_joystick_y_axis;
21 puts("Dodge the rocks until you die.");
23 puts(" -f, --full-screen");
24 puts(" -s, --silent No explosion sounds or music");
25 puts(" -j x,y, --joystick=x,y set the axis numbers. defaults to 0,1");
26 puts(" press a joystick button to activate that joystick");
27 puts(" -V, --version Print program version");
28 puts(" -?, --help Give this help list");
30 puts("Report bugs at http://jasonwoof.com/contact.html");
33 // Advances buf while reading a positive int, terminated by [^0-9]
34 // buf is left pointing at first [^0-9]
35 // If digits are found, the parsed number is written to *out and 0 is returned.
36 // Otherwise *buf and *out are left unchanged, and non-zero is returned.
38 parse_next_int(char **buf, int* out) {
43 // make sure *buf starts -?[0-9]
46 if((*buf)[1] < '0' || (*buf)[1] > '9') {
51 if(**buf < '0' || **buf > '9') {
55 while(**buf >= '0' && **buf <= '9') {
65 // returns 1 on success. return 0 causes usage message
67 parse_joystick_opts(char *arg) {
69 static char* bad_arg = "Error: invalid argument to -j/--joystick.";
71 // argument is required
78 if(parse_next_int(&arg, &opt_joystick_x_axis)) {
91 if(parse_next_int(&arg, &opt_joystick_y_axis)) {
96 // optionally joystick number
98 arg += 1; // skip comma
99 if(parse_next_int(&arg, &opt_joystick_number)) {
103 opt_joystick_enabled = 1;
106 // end with a comma or end of string
107 if(*arg != 0 && *arg != ',') {
112 // mark arg as consumed (so it won't be parsed as a commandline switch)
119 short_opt(char c, char *arg)
122 case 'f': opt_fullscreen = 1; break;
123 case 's': opt_sound = 0; break;
125 return parse_joystick_opts(arg);
127 printf("Variations on Rockdodger %s\n", PACKAGE_VERSION);
131 case 'a': opt_autopilot = 1; break;
133 fprintf(stderr, "unknown option -%c\n\n", c);
140 parse_short_opts(const char *s, char *arg)
142 while(s[1]) if(!short_opt(*s++, NULL)) return 0;
143 return short_opt(*s, arg);
146 static char *long_opts[] = { "full-screen", "silent", "joystick", "version", "help", "autopilot" };
148 static char short_opts[] = { 'f', 's', 'j', 'V', 'h', 'a' };
151 parse_long_opt(const char *s, char *arg)
154 for(i=0; i<sizeof(short_opts); i++) {
155 if(strcmp(s, long_opts[i]) == 0)
156 return short_opt(short_opts[i], arg);
158 fprintf(stderr, "unknown long option --%s\n\n", s);
167 opt_joystick_enabled = 0; // can also be enabled by pressing one of its buttons
168 opt_joystick_number = 0;
169 opt_joystick_x_axis = 0;
170 opt_joystick_y_axis = 1;
175 parse_opts(int argc, char *argv[])
181 for(i=1; i<argc; i++) {
183 // args that are consumed as arguments to an option (such as the zero
184 // in: -j 0) have their first byte set to null. So skip them:
185 s = argv[i]; if(!*s) continue;
187 fputs("not an option\n\n", stderr);
193 for(r=s; *r; r++) if(*r == '=') { *r = 0; arg = r+1; break; }
195 if(arg == NULL && i + 1 < argc) {
197 // if this is used, it's first byte will be set to null, and
198 // it'll be skipped. See above.
202 if(!parse_long_opt(s+1, arg)) { show_help(); return 0; }
204 if(!parse_short_opts(s, arg)) { show_help(); return 0; }