From 77e694d96c59ec8228b69a6d3cbc7161c9993df4 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Mon, 13 Aug 2012 11:07:24 -0400 Subject: make kill take an explicit ID --- pulsemix.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pulsemix.c b/pulsemix.c index 557e4cc..c37383a 100644 --- a/pulsemix.c +++ b/pulsemix.c @@ -638,8 +638,8 @@ static void __attribute__((__noreturn__)) usage(FILE *out) fputs(" set-default NAME set default device\n", out); fputs("\nApplication Commands:\n", out); - fputs(" move NAME move application stream to device\n", out); - fputs(" kill kill an application's stream\n", out); + fputs(" move ID move application stream to device\n", out); + fputs(" kill ID kill an application's stream\n", out); exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -778,10 +778,11 @@ int main(int argc, char *argv[]) errx(EXIT_FAILURE, "unknown action: %s", argv[optind]); optind++; - if (verb == ACTION_SETVOL || - verb == ACTION_SETBAL || - verb == ACTION_INCREASE || - verb == ACTION_DECREASE) + switch (verb) { + case ACTION_SETVOL: + case ACTION_SETBAL: + case ACTION_INCREASE: + case ACTION_DECREASE: if (optind == argc) errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); else { @@ -789,16 +790,20 @@ int main(int argc, char *argv[]) if (xstrtol(argv[optind], &value) < 0) errx(EXIT_FAILURE, "invalid number: %s", argv[optind]); } - else if (verb == ACTION_SETDEFAULT) { + break; + case ACTION_SETDEFAULT: + case ACTION_KILL: if (optind == argc) errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); else id = argv[optind]; - } else if (verb == ACTION_MOVE) { + break; + case ACTION_MOVE: if (optind == argc) errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); else arg = argv[optind]; + default: } /* initialize connection */ -- cgit v1.2.3 From 7dbdac3b192f20bb6885254ed097fb18bcbc0d5e Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Mon, 13 Aug 2012 11:25:56 -0400 Subject: refactor main with a switch --- pulsemix.c | 56 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/pulsemix.c b/pulsemix.c index c37383a..0bea9d4 100644 --- a/pulsemix.c +++ b/pulsemix.c @@ -803,48 +803,54 @@ int main(int argc, char *argv[]) errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); else arg = argv[optind]; + break; default: + break; } /* initialize connection */ if (pulse_init(&pulse) != 0) return 1; - if (verb == ACTION_DEFAULTS) { + switch (verb) { + case ACTION_DEFAULTS: get_default_sink(&pulse); get_default_source(&pulse); print_all(&pulse); - } else if (verb == ACTION_LIST) { + goto done; + case ACTION_LIST: populate_sinks(&pulse, mode); populate_sources(&pulse, mode); print_all(&pulse); - } else { - /* determine sink */ - if (id && fn_get_by_name) { - if (fn_get_by_name(&pulse, id, mode) != 0) - goto done; - } else if (!mode && verb != ACTION_SETDEFAULT && fn_get_default) { - if (fn_get_default(&pulse) != 0) - goto done; - } + goto done; + default: + break; + } + + if (id && fn_get_by_name) { + if (fn_get_by_name(&pulse, id, mode) != 0) + goto done; + } else if (!mode && verb != ACTION_SETDEFAULT && fn_get_default) { + if (fn_get_default(&pulse) != 0) + goto done; + } - if (pulse.head == NULL) { - if (mode && !id) { - warnx("%s id not set, no default operations", pp_name); - rc = 1; - goto done; - } else { - warnx("%s not found: %s", pp_name, id ? id : "default"); - rc = 1; - goto done; - } + if (pulse.head == NULL) { + if (mode && !id) { + warnx("%s id not set, no default operations", pp_name); + rc = 1; + goto done; + } else { + warnx("%s not found: %s", pp_name, id ? id : "default"); + rc = 1; + goto done; } + } - if (arg && fn_get_by_name) - fn_get_by_name(&pulse, arg, MODE_DEVICE); + if (arg && fn_get_by_name) + fn_get_by_name(&pulse, arg, MODE_DEVICE); - rc = do_verb(&pulse, verb, value); - } + rc = do_verb(&pulse, verb, value); done: /* shut down */ -- cgit v1.2.3 From 38b8be02986486b0ca696b07a2fc5a888af2af28 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Mon, 13 Aug 2012 11:56:47 -0400 Subject: make move take all its arguments explictly --- pulsemix.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pulsemix.c b/pulsemix.c index 0bea9d4..ae1e8d0 100644 --- a/pulsemix.c +++ b/pulsemix.c @@ -635,10 +635,10 @@ static void __attribute__((__noreturn__)) usage(FILE *out) fputs("\nDevice Commands:\n", out); fputs(" defaults list default devices\n", out); - fputs(" set-default NAME set default device\n", out); + fputs(" set-default DEVICE set default device\n", out); fputs("\nApplication Commands:\n", out); - fputs(" move ID move application stream to device\n", out); + fputs(" move ID DEVICE move application stream to device\n", out); fputs(" kill ID kill an application's stream\n", out); exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); @@ -794,15 +794,17 @@ int main(int argc, char *argv[]) case ACTION_SETDEFAULT: case ACTION_KILL: if (optind == argc) - errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); + errx(EXIT_FAILURE, "missing arguments for action '%s'", argv[optind - 1]); else id = argv[optind]; break; case ACTION_MOVE: - if (optind == argc) - errx(EXIT_FAILURE, "missing value for action '%s'", argv[optind - 1]); - else + if (optind > argc - 2) + errx(EXIT_FAILURE, "missing arguments for action '%s'", argv[optind - 1]); + else { + id = argv[optind++]; arg = argv[optind]; + } break; default: break; -- cgit v1.2.3 From ffdf3ab3e374aa1bc7acfb6e5dd35bee71af5a13 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Mon, 13 Aug 2012 15:33:32 -0400 Subject: deduplicate some error handling --- pulsemix.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pulsemix.c b/pulsemix.c index ae1e8d0..b220ff0 100644 --- a/pulsemix.c +++ b/pulsemix.c @@ -838,15 +838,12 @@ int main(int argc, char *argv[]) } if (pulse.head == NULL) { - if (mode && !id) { + if (mode && !id) warnx("%s id not set, no default operations", pp_name); - rc = 1; - goto done; - } else { + else warnx("%s not found: %s", pp_name, id ? id : "default"); - rc = 1; - goto done; - } + rc = 1; + goto done; } if (arg && fn_get_by_name) -- cgit v1.2.3