parse mode strings strictly

This commit is contained in:
Alexandre Ratchov 2012-12-01 13:07:01 +01:00
parent 7a76f322eb
commit b3f82753f8
1 changed files with 26 additions and 10 deletions

View File

@ -159,28 +159,44 @@ opt_onoff(void)
errx(1, "%s: on/off expected", optarg);
}
int
getword(char *word, char **str)
{
char *p = *str;
for (;;) {
if (*word == '\0')
break;
if (*word++ != *p++)
return 0;
}
if (*p == ',' || *p == '\0') {
*str = p;
return 1;
}
return 0;
}
unsigned int
opt_mode(void)
{
unsigned int mode = 0;
char *p = optarg;
size_t len;
for (p = optarg; *p != '\0'; p++) {
len = strcspn(p, ",");
if (strncmp("play", p, len) == 0) {
for (;;) {
if (getword("play", &p)) {
mode |= MODE_PLAY;
} else if (strncmp("rec", p, len) == 0) {
} else if (getword("rec", &p)) {
mode |= MODE_REC;
} else if (strncmp("mon", p, len) == 0) {
} else if (getword("mon", &p)) {
mode |= MODE_MON;
} else if (strncmp("midi", p, len) == 0) {
} else if (getword("midi", &p)) {
mode |= MODE_MIDIMASK;
} else
} else
errx(1, "%s: bad mode", optarg);
p += len;
if (*p == '\0')
break;
p++;
}
if (mode == 0)
errx(1, "empty mode");
@ -414,7 +430,7 @@ main(int argc, char **argv)
mkdev(optarg, &par, 0, bufsz, round, rate, hold, autovol);
break;
case 'M':
/* XXX: compatibility with aucat */
/* XXX: for compatibility with aucat, remove this */
break;
default:
fputs(usagestr, stderr);