Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions amixer/amixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -1757,34 +1758,39 @@ 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[256], *args[MAX_ARGS];
char *buf = NULL, *args[MAX_ARGS];
size_t size = 0;
int err = 0;

/* quiet = 1; */
ignore_error = 1;

while (fgets(buf, sizeof(buf), stdin)) {
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"))
err = sset(narg - 1, args + 1, 0, 1);
else if (!strcmp(args[0], "cset"))
err = cset(narg - 1, args + 1, 0, 1);
if (err < 0)
return 1;
break;
}
}
return 0;

free(buf);
return err < 0 ? 1 : 0;
}


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'},
Expand All @@ -1797,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},
Expand All @@ -1806,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':
Expand Down Expand Up @@ -1863,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;
Expand All @@ -1880,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;
}

Expand Down