]> git.alsa-project.org Git - alsa-utils.git/commitdiff
amixer: support --file option
authorMikhail Ivashinenko <miuivashinenko@sberdevices.ru>
Wed, 28 Jan 2026 17:59:22 +0000 (20:59 +0300)
committerJaroslav Kysela <perex@perex.cz>
Mon, 25 May 2026 09:34:09 +0000 (11:34 +0200)
Allows to read commands directly from file.
Refactor exec_stdin() to exec_file() function to support generic files.

Closes: https://github.com/alsa-project/alsa-utils/pull/321
Signed-off-by: Mikhail Ivashinenko <miuivashinenko@salutedevices.com>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
amixer/amixer.c

index 9eb7f73c9b3a8922bb7c57db8ac6da448a2f88fc..5ca81ff1f58ae34b0902f0a02ab0b7fc145a6693 100644 (file)
@@ -71,6 +71,7 @@ static int help(void)
        printf("  -i,--inactive   show also inactive controls\n");
        printf("  -a,--abstract L select abstraction level (none or basic)\n");
        printf("  -s,--stdin      Read and execute commands from stdin sequentially\n");
+       printf("  -f,--file FILE  Read and execute commands from file sequentially\n");
        printf("  -R,--raw-volume Use the raw value (default)\n");
        printf("  -M,--mapped-volume Use the mapped volume\n");
        printf("\nAvailable commands:\n");
@@ -1757,7 +1758,7 @@ static int split_line(char *buf, char **token, int max_token)
 
 #define MAX_ARGS       32
 
-static int exec_stdin(void)
+static int exec_file(FILE *file)
 {
        int narg;
        char *buf = NULL, *args[MAX_ARGS];
@@ -1767,7 +1768,7 @@ static int exec_stdin(void)
        /* quiet = 1; */
        ignore_error = 1;
 
-       while (getline(&buf, &size, stdin) > 0) {
+       while (getline(&buf, &size, file) > 0) {
                narg = split_line(buf, args, MAX_ARGS);
                if (narg > 0) {
                        if (!strcmp(args[0], "sset") || !strcmp(args[0], "set"))
@@ -1788,6 +1789,8 @@ int main(int argc, char *argv[])
 {
        int badopt, retval, level = 0;
        int read_stdin = 0;
+       int read_file = 0;
+       char filename[256] = { 0 };
        static const struct option long_option[] =
        {
                {"help", 0, NULL, 'h'},
@@ -1800,6 +1803,7 @@ int main(int argc, char *argv[])
                {"version", 0, NULL, 'v'},
                {"abstract", 1, NULL, 'a'},
                {"stdin", 0, NULL, 's'},
+               {"file", 1, NULL, 'f'},
                {"raw-volume", 0, NULL, 'R'},
                {"mapped-volume", 0, NULL, 'M'},
                {NULL, 0, NULL, 0},
@@ -1809,7 +1813,7 @@ int main(int argc, char *argv[])
        while (1) {
                int c;
 
-               if ((c = getopt_long(argc, argv, "hc:D:qidnva:sRM", long_option, NULL)) < 0)
+               if ((c = getopt_long(argc, argv, "hc:D:qidnva:sf:RM", long_option, NULL)) < 0)
                        break;
                switch (c) {
                case 'h':
@@ -1866,6 +1870,11 @@ int main(int argc, char *argv[])
                case 's':
                        read_stdin = 1;
                        break;
+               case 'f':
+                       read_file = 1;
+                       strncpy(filename, optarg, sizeof(filename)-1);
+                       filename[sizeof(filename)-1] = '\0';
+                       break;
                case 'R':
                        std_vol_type = VOL_RAW;
                        break;
@@ -1883,7 +1892,21 @@ int main(int argc, char *argv[])
        smixer_options.device = card;
 
        if (read_stdin) {
-               retval = exec_stdin();
+               retval = exec_file(stdin);
+               goto finish;
+       }
+
+       if (read_file) {
+               FILE *file;
+               file = fopen(filename, "r");
+               if (!file) {
+                       retval = errno;
+                       perror("fopen");
+                       goto finish;
+               }
+
+               retval = exec_file(file);
+               fclose(file);
                goto finish;
        }