File: optfile.c

package info (click to toggle)
awesfx 0.5.2-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 780 kB
  • sloc: ansic: 6,532; sh: 56; makefile: 48
file content (248 lines) | stat: -rw-r--r-- 5,748 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*----------------------------------------------------------------
 * parse options
 *
 * Copyright (C) 1996-1999 Takashi Iwai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "util.h"
#include "sfopts.h"
#include "config.h"


#define SYSTEM_RCFILE		"/etc/sfxloadrc"
#define RCFILE			".sfxloadrc"

#define DEFAULT_ID	"default"

typedef struct OptionFile {
	int argc;
	char **argv;
	struct OptionFile *next;
} OptionFile;

static OptionFile *optlist = NULL;

#define MAX_ARGC	100

static void read_option_file(void)
{
	char *p, rcfile[256];
	char line[256];
	FILE *fp;
	OptionFile *rec;

	*rcfile = 0;
	if ((p = getenv("HOME")) != NULL && *p) {
		snprintf(rcfile, sizeof(rcfile), "%s/%s", p, RCFILE);
		if (access(rcfile, R_OK) != 0)
			rcfile[0] = 0;
	}
	if (! *rcfile) {
#ifdef SYSTEM_RCFILE
		strcpy(rcfile, SYSTEM_RCFILE);
		if (access(rcfile, R_OK) != 0)
			return;
#else
		return;
#endif
	}

	optlist = NULL;

	if ((fp = fopen(rcfile, "r")) == NULL)
		return;

	while (fgets(line, sizeof(line), fp)) {
		char *argv[MAX_ARGC];
		int i, argc;
		if ((argv[0] = strtok(line, " \t\n")) == NULL) continue;
		if (*argv[0] == '#') continue; /* skip comments */
		for (argc = 1; argc < MAX_ARGC; argc++) {
			argv[argc] = strtok(NULL,  " \t\n");
			if (argv[argc] == NULL)
				break;
		}
		rec = (OptionFile*)safe_malloc(sizeof(OptionFile));
		rec->argv = (char**)safe_malloc(sizeof(char*) * argc);
		rec->argc = argc;
		for (i = 0; i < argc; i++)
			rec->argv[i] = safe_strdup(argv[i]);
		rec->next = optlist;
		optlist = rec;
	}

	fclose (fp);
}

static void parse_named_option(char *fname)
{
	OptionFile *p;
	for (p = optlist; p; p = p->next) {
		int optind_save = optind;
		optind = 0;
		if (strcmp(p->argv[0], fname) == 0) {
			while (awe_parse_options(p->argc, p->argv, 0, 0, 0) == 0)
				;
		}
		optind = optind_save;
	}
}

void awe_read_option_file(char *fname)
{
	char tmp[256], *base, *ep;

	if (optlist == NULL) {
		read_option_file();
		if (optlist == NULL)
			return;
	}

	parse_named_option(DEFAULT_ID);
	if (fname) {
		strncpy(tmp, fname, sizeof(tmp));
		fname[sizeof(tmp) - 1] = 0;
		if ((base = strrchr(tmp, '/')) == NULL)
			base = tmp; /* no directory path is attached */
		else
			base++; /* next of slash letter */
		/* remove extension */
		if ((ep = strchr(base, '.')) != NULL)
			*ep = 0;
		if (strcmp(base, DEFAULT_ID) != 0)
			parse_named_option(base);
	}
}

#define DEFAULT_OPTION_NUM	10

static struct option long_options[40] = {
	{"addblank", 2, 0, 'B'},
	{"bank", 2, 0, 'b'},
	{"chorus", 1, 0, 'c'},
	{"reverb", 1, 0, 'r'},
	{"path", 1, 0, 'P'},
	{"sense", 1, 0, 'A'},
	{"atten", 1, 0, 'a'},
	{"decay", 1, 0, 'd'},
	{"volume", 1, 0, 'V'},
	{"compat", 2, 0, 'C'},
};
#define OPTION_FLAGS	"b:c:r:P:A:a:d:V:BC"

#define set_bool()	(optarg ? bool_val(optarg) : TRUE)

int awe_parse_options(int argc, char **argv, char *optflags,
		      struct option *long_opts, int *optidx)
{
	int c;
	int ival;
	double dval;
	static char options[100];

	if (optflags) {
		if (strlen(OPTION_FLAGS) + strlen(optflags) >= sizeof(options))
			return -1;
	}
	strcpy(options, OPTION_FLAGS);
	if (optflags)
		strcat(options, optflags);
	c = DEFAULT_OPTION_NUM;
	if (long_opts) {
		struct option *p;
		for (p = long_opts; p->name; p++) {
			if (c >= numberof(long_options))
				return -1;
			long_options[c++] = *p;
		}
	}
	long_options[c].name = 0;

	if ((c = getopt_long(argc, argv, options, long_options, optidx)) == -1)
		return -1;

	switch (c) {
	case 'b':
		ival = atoi(optarg);
		if (ival > 127)
			fprintf(stderr, "awe: illegal bank number %d\n", ival);
		else
			awe_option.default_bank = ival;
		break;
	case 'B':
		awe_option.auto_add_blank = set_bool();
		break;
	case 'C':
		awe_option.compatible = set_bool();
		break;
	case 'V':
		ival = atoi(optarg);
		if (ival < 0 || ival > 100)
			fprintf(stderr, "awe: illegal default volume value %d\n", ival);
		else
			awe_option.default_volume = ival;
		break;
	case 'c':
		ival = atoi(optarg);
		if (ival < 0 || ival > 100)
			fprintf(stderr, "awe: illegal default chorus value %d\n", ival);
		else
			awe_option.default_chorus = ival;
		break;
	case 'r':
		ival = atoi(optarg);
		if (ival < 0 || ival > 100)
			fprintf(stderr, "awe: illegal default reverb value %d\n", ival);
		else
			awe_option.default_reverb = ival;
		break;
	case 'P':
		if (awe_option.search_path)
			free(awe_option.search_path);

		awe_option.search_path = safe_strdup(optarg);
		break;
	case 'A':
		dval = atof(optarg);
		if (dval <= 0)
			fprintf(stderr, "awe: illegal atten sense parameter %g\n", dval);
		else {
			awe_option.atten_sense = dval;
			awe_option.default_atten = awe_calc_def_atten(dval);
		}
		break;
	case 'a':
		ival = atoi(optarg);
		if (ival < 0 || ival > 255)
			fprintf(stderr, "awe: illegal attenuation parameter %d\n", ival);
		else
			awe_option.default_atten = ival;
		break;
	case 'd':
		awe_option.decay_sense = atof(optarg);
		break;

	default:
		return c;
	}

	return 0;
}