Skip to content

Commit 8132bd2

Browse files
Mikhail IvashinenkoMikhail Ivashinenko
authored andcommitted
amixer: support --file option
Allows to read commands directly from file. Refactor exec_stdin() to exec_file() function to support generic files. Signed-off-by: Mikhail Ivashinenko <miuivashinenko@salutedevices.com>
1 parent d003075 commit 8132bd2

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

amixer/amixer.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static int help(void)
7171
printf(" -i,--inactive show also inactive controls\n");
7272
printf(" -a,--abstract L select abstraction level (none or basic)\n");
7373
printf(" -s,--stdin Read and execute commands from stdin sequentially\n");
74+
printf(" -f,--file FILE Read and execute commands from file sequentially\n");
7475
printf(" -R,--raw-volume Use the raw value (default)\n");
7576
printf(" -M,--mapped-volume Use the mapped volume\n");
7677
printf("\nAvailable commands:\n");
@@ -1757,7 +1758,7 @@ static int split_line(char *buf, char **token, int max_token)
17571758

17581759
#define MAX_ARGS 32
17591760

1760-
static int exec_stdin(void)
1761+
static int exec_file(FILE *file)
17611762
{
17621763
int narg;
17631764
char *buf = NULL, *args[MAX_ARGS];
@@ -1767,7 +1768,7 @@ static int exec_stdin(void)
17671768
/* quiet = 1; */
17681769
ignore_error = 1;
17691770

1770-
while (getline(&buf, &size, stdin) > 0) {
1771+
while (getline(&buf, &size, file) > 0) {
17711772
narg = split_line(buf, args, MAX_ARGS);
17721773
if (narg > 0) {
17731774
if (!strcmp(args[0], "sset") || !strcmp(args[0], "set"))
@@ -1790,6 +1791,8 @@ int main(int argc, char *argv[])
17901791
{
17911792
int badopt, retval, level = 0;
17921793
int read_stdin = 0;
1794+
int read_file = 0;
1795+
char filename[256] = { 0 };
17931796
static const struct option long_option[] =
17941797
{
17951798
{"help", 0, NULL, 'h'},
@@ -1802,6 +1805,7 @@ int main(int argc, char *argv[])
18021805
{"version", 0, NULL, 'v'},
18031806
{"abstract", 1, NULL, 'a'},
18041807
{"stdin", 0, NULL, 's'},
1808+
{"file", 1, NULL, 'f'},
18051809
{"raw-volume", 0, NULL, 'R'},
18061810
{"mapped-volume", 0, NULL, 'M'},
18071811
{NULL, 0, NULL, 0},
@@ -1811,7 +1815,7 @@ int main(int argc, char *argv[])
18111815
while (1) {
18121816
int c;
18131817

1814-
if ((c = getopt_long(argc, argv, "hc:D:qidnva:sRM", long_option, NULL)) < 0)
1818+
if ((c = getopt_long(argc, argv, "hc:D:qidnva:sf:RM", long_option, NULL)) < 0)
18151819
break;
18161820
switch (c) {
18171821
case 'h':
@@ -1868,6 +1872,11 @@ int main(int argc, char *argv[])
18681872
case 's':
18691873
read_stdin = 1;
18701874
break;
1875+
case 'f':
1876+
read_file = 1;
1877+
strncpy(filename, optarg, sizeof(filename)-1);
1878+
filename[sizeof(card)-1] = '\0';
1879+
break;
18711880
case 'R':
18721881
std_vol_type = VOL_RAW;
18731882
break;
@@ -1885,7 +1894,21 @@ int main(int argc, char *argv[])
18851894
smixer_options.device = card;
18861895

18871896
if (read_stdin) {
1888-
retval = exec_stdin();
1897+
retval = exec_file(stdin);
1898+
goto finish;
1899+
}
1900+
1901+
if (read_file) {
1902+
FILE *file;
1903+
file = fopen(filename, "r");
1904+
if (!file) {
1905+
retval = errno;
1906+
perror("fopen");
1907+
goto finish;
1908+
}
1909+
1910+
retval = exec_file(file);
1911+
fclose(file);
18891912
goto finish;
18901913
}
18911914

0 commit comments

Comments
 (0)