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