wmix: rewrote parser of config file to report problems to user

The original parser would not say anything if the user had misspelled
something in the file which can be annoying. Now the parser will report
warnings for everything not understood with a message trying to be more
helpful to debug and fix.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
Christophe CURIS 2014-06-07 21:21:50 +02:00 committed by Carlos R. Mafra
parent 5b46b380e6
commit 546bc1e49b
2 changed files with 89 additions and 31 deletions

View file

@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <getopt.h> #include <getopt.h>
#include <sys/soundcard.h> #include <sys/soundcard.h>
@ -149,8 +150,8 @@ void config_read(void)
const char *filename; const char *filename;
char buffer_fname[512]; char buffer_fname[512];
FILE *fp; FILE *fp;
int line;
char buf[512]; char buf[512];
char *ptr;
if (config.file != NULL) { if (config.file != NULL) {
filename = config.file; filename = config.file;
@ -179,43 +180,98 @@ void config_read(void)
if (config.verbose) if (config.verbose)
printf("Using configuration file: %s\n", filename); printf("Using configuration file: %s\n", filename);
line = 0;
while (fgets(buf, 512, fp)) { while (fgets(buf, 512, fp)) {
if ((ptr = strstr(buf, "mousewheel="))) { char *ptr;
ptr += 11; char *keyword;
config.mousewheel = atoi(ptr); char *value;
line++;
ptr = buf;
while (isspace(*ptr))
ptr++;
if ((*ptr == '\0') || (*ptr == '#'))
continue;
/* Isolate the keyword */
keyword = ptr;
if (*ptr == '=') {
fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", no keyword before '='\n",
line, filename);
continue;
} }
if ((ptr = strstr(buf, "scrolltext="))) { value = NULL;
ptr += 11; while (*ptr) {
config.scrolltext = atoi(ptr); if (*ptr == '=') {
value = ptr + 1;
break;
} }
if ((ptr = strstr(buf, "osd="))) { if (*ptr == '#')
ptr += 4; break;
config.osd = atoi(ptr); ptr++;
} }
if ((ptr = strstr(buf, "osdcolor="))) { if (value == NULL) {
char *end; fprintf(stderr, "wmix:warning: syntax error at line %d in \"%s\", missing '='\n",
ptr += 9; line, filename);
end = strchr(ptr, '\n'); continue;
ptr[end - ptr] = '\0'; }
while (isspace(ptr[-1]))
ptr--;
*ptr = '\0';
/* Isolate the value */
while (isspace(*value))
value++;
ptr = value;
while (*ptr) {
if (*ptr == '#')
break;
ptr++;
}
while (isspace(ptr[-1]))
ptr--;
*ptr = '\0';
/* Check what keyword we have */
if (strcmp(keyword, "mousewheel") == 0) {
config.mousewheel = atoi(value);
} else if (strcmp(keyword, "osd") == 0) {
config.osd = atoi(value);
} else if (strcmp(keyword, "osdcolor") == 0) {
if (config.osd_color) if (config.osd_color)
free(config.osd_color); free(config.osd_color);
config.osd_color = strdup(ptr); config.osd_color = strdup(value);
}
if ((ptr = strstr(buf, "wheelstep="))) { } else if (strcmp(keyword, "scrolltext") == 0) {
ptr += 10; config.scrolltext = atoi(value);
/* detect old style config */
if (atoi(ptr) > 1) } else if (strcmp(keyword, "wheelbtn1") == 0) {
config.scrollstep = (float)atoi(ptr) / 100.0; config.wheel_button_up = atoi(value);
} else if (strcmp(keyword, "wheelbtn2") == 0) {
config.wheel_button_down = atoi(value);
} else if (strcmp(keyword, "wheelstep") == 0) {
double val;
val = atof(value);
if (val < 0.0 || val > 100.0)
fprintf(stderr, "wmix:error: value %f is out of range for wheelstep in %s at line %d\n",
val, filename, line);
else if (val >= 1.0)
config.scrollstep = val / 100.0;
else if (val > 0.0)
config.scrollstep = val;
else else
config.scrollstep = atof(ptr); fprintf(stderr, "wmix:error: value '%s' not understood for wheelstep in %s at line %d\n",
} value, filename, line);
if ((ptr = strstr(buf, "wheelbtn1="))) { } else {
ptr += 10; fprintf(stderr, "wmix:warning: unknow keyword '%s' at line %d of \"%s\", ignored\n",
config.wheel_button_up = atoi(ptr); keyword, line, filename);
}
if ((ptr = strstr(buf, "wheelbtn2="))) {
ptr += 10;
config.wheel_button_down = atoi(ptr);
} }
} }
fclose(fp); fclose(fp);

View file

@ -81,6 +81,8 @@ default=\fI3\fP)
.br .br
For compatibility, values between 0.0 and 1.0 will be considered as a raw percentage For compatibility, values between 0.0 and 1.0 will be considered as a raw percentage
.LP .LP
You may include comments in the file using the character # in which case
everything will be ignored from the character to the end of the line.
. .
.SH CHANNEL NAMES .SH CHANNEL NAMES
The names for the different channels are provided by ALSA/OSS as short names, for which The names for the different channels are provided by ALSA/OSS as short names, for which