14 static const char *usagefmt = "Usage: %s [options]\n"
16 " -music/-nomusic toggle music playback\n"
17 " -scr,-screen <name> ignore demoscript, run specific screen\n"
18 " -fs/-win run fullscreen/windowed\n"
19 " -vsync/-novsync toggle vsync\n"
20 " -h,-help print usage and exit\n";
22 int parse_args(int argc, char **argv)
27 for(i=1; i<argc; i++) {
28 if(argv[i][0] == '-') {
29 if(strcmp(argv[i], "-music") == 0) {
31 } else if(strcmp(argv[i], "-nomusic") == 0) {
33 } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
35 } else if(strcmp(argv[i], "-vsync") == 0) {
37 } else if(strcmp(argv[i], "-novsync") == 0) {
39 } else if(strcmp(argv[i], "-fs") == 0) {
41 } else if(strcmp(argv[i], "-win") == 0) {
43 } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
44 printf(usagefmt, argv[0]);
47 fprintf(stderr, "invalid option: %s\n", argv[i]);
48 fprintf(stderr, usagefmt, argv[0]);
53 fprintf(stderr, "unexpected option: %s\n", argv[i]);
54 fprintf(stderr, usagefmt, argv[0]);
62 opt.scrname = scrname;
68 static char *strip_space(char *s)
73 while(*s && isspace(*s)) ++s;
76 if((end = strrchr(s, '#'))) {
83 while(end > s && isspace(*end)) *end-- = 0;
84 return end > s ? s : 0;
87 static int bool_value(char *s)
95 return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
98 int load_config(const char *fname)
104 if(!(fp = fopen(fname, "rb"))) {
105 return 0; /* just ignore missing config files */
108 while(fgets(buf, sizeof buf, fp)) {
109 char *line, *key, *value;
112 if(!(line = strip_space(buf))) {
116 if(!(value = strchr(line, '='))) {
117 fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
122 if(!(key = strip_space(line)) || !(value = strip_space(value))) {
123 fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
127 if(strcmp(line, "music") == 0) {
128 opt.music = bool_value(value);
129 } else if(strcmp(line, "screen") == 0) {
130 opt.scrname = strdup(value);
131 } else if(strcmp(line, "vsync") == 0) {
132 opt.vsync = bool_value(value);
133 } else if(strcmp(line, "fullscreen") == 0) {
134 opt.fullscreen = bool_value(value);
136 fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);