commit e49055d5ec08db674a4daa55b24d85f08da1ec74
parent 63a9dbfa21b89205b9ee0f43e4c8374f81535aae
Author: Christos Margiolis <christos@margiolis.net>
Date: Sun, 20 Mar 2022 20:11:58 +0200
fix shorthand syntax bug
Diffstat:
2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/usr.sbin/mixer/mixer.8 b/usr.sbin/mixer/mixer.8
@@ -139,6 +139,10 @@ or
the value following will be used as a relative adjustment, modifying the
current settings by the amount specified.
.Pp
+Volume can also be set using the shorthand
+.Ar dev Ns Cm =value .
+This syntax does not apply to other controls.
+.Pp
The
.Ar dev Ns Cm .mute
control (un)mutes a device.
diff --git a/usr.sbin/mixer/mixer.c b/usr.sbin/mixer/mixer.c
@@ -66,7 +66,7 @@ main(int argc, char *argv[])
mix_ctl_t *cp;
char *name = NULL, buf[NAME_MAX];
char *p, *q, *devstr, *ctlstr, *valstr = NULL;
- int dunit, i, n, pall = 1;
+ int dunit, i, n, pall = 1, shorthand = 0;
int aflag = 0, dflag = 0, oflag = 0, sflag = 0;
int ch;
@@ -137,6 +137,15 @@ parse:
while (argc > 0) {
if ((p = strdup(*argv)) == NULL)
err(1, "strdup(%s)", *argv);
+
+ /* Check if we're using the shorthand syntax for volume setting. */
+ for (q = p; *q != '\0'; q++) {
+ if (*q == '=')
+ shorthand = 1;
+ else if (*q == '.' && shorthand)
+ shorthand = 0;
+ }
+
/* Split the string into device, control and value. */
devstr = strsep(&p, ".=");
if ((m->dev = mixer_get_dev_byname(m, devstr)) == NULL) {
@@ -148,11 +157,16 @@ parse:
printdev(m, 1);
pall = 0;
goto next;
- } else {
- for (q = p; (*q >= '0' && *q <= '9') || *q == '.'; q++)
- ; /* nothing */
- /* Input: `dev=N` -> shorthand for `dev.volume=N`. */
- if (*q == '\0') {
+ } else if (shorthand) {
+ /*
+ * Input: `dev=N` -> shorthand for `dev.volume=N`.
+ *
+ * We don't care what the rest of the string contains as
+ * long as we're sure the very beginning is right,
+ * mod_volume() will take care of parsing it properly.
+ */
+ if (*p == '+' || *p == '-' || *p == '.' ||
+ (*p >= '0' && *p <= '9')) {
cp = mixer_get_ctl(m->dev, C_VOL);
cp->mod(cp->parent_dev, p);
goto next;
@@ -163,7 +177,6 @@ parse:
warnx("%s.%s: no such control", devstr, ctlstr);
goto next;
}
-
/* Input: `dev.control`. */
if (p == NULL) {
(void)cp->print(cp->parent_dev, cp->name);