JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Improved keybindings: support numbad stearing instead of dvorak, support 'pause'...
[vor.git] / args.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "args.h"
5 #include "config.h"
6
7 // Look and Feel
8 int opt_fullscreen;
9 int opt_sound;
10
11 int opt_autopilot;
12
13 static void
14 show_help(void)
15 {
16         puts("Dodge the rocks until you die.");
17         putchar('\n');
18         puts("  -f, --full-screen");
19         puts("  -s, --silent               No explosion sounds or music");
20         puts("  -V, --version              Print program version");
21         puts("  -?, --help                 Give this help list");
22         putchar('\n');
23         puts("Mandatory or optional arguments to long options are also mandatory or optional");
24         puts("for any corresponding short options.");
25         putchar('\n');
26         puts("Report bugs at http://jasonwoof.com/contact.html");
27 }
28
29 int
30 short_opt(char c, char *arg)
31 {
32         switch(c) {
33                 case 'f': opt_fullscreen = 1; break;
34                 case 's': opt_sound = 0; break;
35                 case 'V':
36                                   printf("Variations on Rockdodger %s\n", VERSION);
37                                   exit(0);
38                 case '?':
39                 case 'h': return 0;
40                 case 'a': opt_autopilot = 1; break;
41                 default: 
42                                   fprintf(stderr, "unknown option -%c\n\n", c);
43                                   return 0;
44         }
45         return 1;
46 }
47
48 int
49 parse_short_opts(const char *s, char *arg)
50 {
51         while(s[1]) if(!short_opt(*s++, NULL)) return 0;
52         return short_opt(*s, arg);
53 }
54
55 static char *long_opts[] = { "full-screen", "silent", "version", "help", "autopilot" };
56
57 static char short_opts[] = { 'f', 's', 'V', 'h', 'a' };
58
59 int
60 parse_long_opt(const char *s, char *arg)
61 {
62         int i;
63         for(i=0; i<sizeof(short_opts); i++) {
64                 if(strcmp(s, long_opts[i]) == 0)
65                         return short_opt(short_opts[i], arg);
66         }
67         fprintf(stderr, "unknown long option --%s\n\n", s);
68         return 0;
69 }
70
71 void
72 init_opts(void)
73 {
74         opt_fullscreen = 0;
75         opt_sound = 1;
76         opt_autopilot = 0;
77 }
78
79 int
80 parse_opts(int argc, char *argv[])
81 {
82         int i;
83         char *r;
84
85         init_opts();
86         for(i=1; i<argc; i++) {
87                 char *s, *arg;
88                 s = argv[i]; if(!*s) continue;
89                 if(*s++ != '-') {
90                         fputs("not an option\n\n", stderr);
91                         show_help();
92                         return 0;
93                 }
94
95                 arg = NULL;
96                 for(r=s; *r; r++) if(*r == '=') { *r = 0; arg = r+1; break; }
97                 if(!arg && (i+1 < argc)) arg = argv[i+1];
98
99                 if(*s == '-') {
100                         if(!parse_long_opt(s+1, arg)) { show_help(); return 0; }
101                 } else {
102                    if(!parse_short_opts(s, arg)) { show_help(); return 0; }
103                 }
104         }
105         return 1;
106 }