[PATCH ucspi-vsock] Factor out set_verbosity()
This will save some cognitive load when reading main functions. --- log.c | 20 +++++++++++++++++++- log.h | 5 +++++ vsockclient.c | 6 +----- vsockserver.c | 6 +----- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/log.c b/log.c index 385d39f..cdfacd6 100644 --- a/log.c +++ b/log.c @@ -1,15 +1,33 @@ // SPDX-License-Identifier: GPL-2.0-or-later -// SPDX-FileCopyrightText: 2020 Alyssa Ross <hi@alyssa.is> +// SPDX-FileCopyrightText: 2020-2021 Alyssa Ross <hi@alyssa.is> #define _GNU_SOURCE #include "log.h" #include <err.h> +#include <stdbool.h> #include <stdlib.h> enum verbosity verbosity = errors; +bool set_verbosity(int opt) +{ + switch (opt) { + case 'q': + verbosity = nothing; + return true; + case 'Q': + verbosity = errors; + return true; + case 'v': + verbosity = all; + return true; + } + + return false; +} + void veloge(const char *fmt, va_list args) { if (verbosity) diff --git a/log.h b/log.h index ea9e9cf..7b40e48 100644 --- a/log.h +++ b/log.h @@ -11,6 +11,11 @@ enum verbosity { extern enum verbosity verbosity; +// If opt is a character that matches a standard UCSPI command line +// verbosity option, sets the verbosity appropriately and returns +// true. Otherwise, returns false. +_Bool set_verbosity(int opt); + // Log an error message, followed by strerrno(errno), then exit with // status eval. _Noreturn void diee(int eval, const char *fmt, ...); diff --git a/vsockclient.c b/vsockclient.c index 91e1320..d25ab13 100644 --- a/vsockclient.c +++ b/vsockclient.c @@ -35,13 +35,9 @@ int main(int argc, char *argv[]) while ((opt = getopt(argc, argv, "+qQv")) != -1) { switch (opt) { case 'q': - verbosity = nothing; - break; case 'Q': - verbosity = errors; - break; case 'v': - verbosity = all; + set_verbosity(opt); break; default: ex_usage(); diff --git a/vsockserver.c b/vsockserver.c index 03d9838..4c251b0 100644 --- a/vsockserver.c +++ b/vsockserver.c @@ -43,13 +43,9 @@ int main(int argc, char *argv[]) notify = true; break; case 'q': - verbosity = nothing; - break; case 'Q': - verbosity = errors; - break; case 'v': - verbosity = all; + set_verbosity(opt); break; default: ex_usage(); -- 2.30.0
On Wed Mar 17, 2021 at 5:35 PM PDT, Alyssa Ross wrote:
This will save some cognitive load when reading main functions. --- log.c | 20 +++++++++++++++++++- log.h | 5 +++++ vsockclient.c | 6 +----- vsockserver.c | 6 +----- 4 files changed, 26 insertions(+), 11 deletions(-)
Yay, refactoring! Reviewed-by: Cole Helbling <cole.e.helbling@outlook.com>
This will save some cognitive load when reading main functions. --- log.c | 20 +++++++++++++++++++- log.h | 5 +++++ vsockclient.c | 6 +----- vsockserver.c | 6 +----- 4 files changed, 26 insertions(+), 11 deletions(-)
Yay, refactoring!
Reviewed-by: Cole Helbling <cole.e.helbling@outlook.com>
Committed as c2990a9.
participants (2)
-
Alyssa Ross -
Cole Helbling