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