added serial spaceball support in the dos version. can be used for
[dosdemo] / src / cfgopt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "cfgopt.h"
6
7 struct options opt;
8
9 int parse_args(int argc, char **argv)
10 {
11         int i;
12         char *scrname = 0;
13
14         for(i=1; i<argc; i++) {
15                 if(argv[i][0] == '-') {
16                         if(strcmp(argv[i], "-music") == 0) {
17                                 opt.music = 1;
18                         } else if(strcmp(argv[i], "-nomusic") == 0) {
19                                 opt.music = 0;
20                         } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
21                                 scrname = argv[++i];
22                         } else if(strcmp(argv[i], "-sball") == 0) {
23                                 opt.sball = !opt.sball;
24                         } else {
25                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
26                                 return -1;
27                         }
28                 } else {
29                         if(scrname) {
30                                 fprintf(stderr, "unexpected option: %s\n", argv[i]);
31                                 return -1;
32                         }
33                         scrname = argv[i];
34                 }
35         }
36
37         if(scrname) {
38                 opt.start_scr = scrname;
39         }
40         return 0;
41 }
42
43 static char *strip_space(char *s)
44 {
45         int len;
46         char *end;
47
48         while(*s && isspace(*s)) ++s;
49         if(!*s) return 0;
50
51         if((end = strrchr(s, '#'))) {
52                 --end;
53         } else {
54                 len = strlen(s);
55                 end = s + len - 1;
56         }
57
58         while(end > s && isspace(*end)) *end-- = 0;
59         return end > s ? s : 0;
60 }
61
62 static int bool_value(char *s)
63 {
64         char *ptr = s;
65         while(*ptr) {
66                 *ptr = tolower(*ptr);
67                 ++ptr;
68         }
69
70         return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
71 }
72
73 int load_config(const char *fname)
74 {
75         FILE *fp;
76         char buf[256];
77         int nline = 0;
78
79         if(!(fp = fopen(fname, "rb"))) {
80                 return 0;       /* just ignore missing config files */
81         }
82
83         while(fgets(buf, sizeof buf, fp)) {
84                 char *line, *key, *value;
85
86                 ++nline;
87                 if(!(line = strip_space(buf))) {
88                         continue;
89                 }
90
91                 if(!(value = strchr(line, '='))) {
92                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
93                         return -1;
94                 }
95                 *value++ = 0;
96
97                 if(!(key = strip_space(line)) || !(value = strip_space(value))) {
98                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
99                         return -1;
100                 }
101
102                 if(strcmp(line, "music") == 0) {
103                         opt.music = bool_value(value);
104                 } else if(strcmp(line, "screen") == 0) {
105                         opt.start_scr = strdup(value);
106                 } else if(strcmp(line, "sball") == 0) {
107                         opt.sball = bool_value(value);
108                 } else {
109                         fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);
110                         return -1;
111                 }
112         }
113         return 0;
114 }