From c2bbb8eef1cc6009b0fa2770ee6788f95f37fb7a Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sat, 14 Mar 2026 14:28:30 +0000 Subject: [PATCH] Parse the monitoring side setting from "--monitor" (#67). --- src/include/options.h | 10 ++++++++++ src/main/options.c | 46 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/include/options.h b/src/include/options.h index 366cc50..b1f0f3e 100644 --- a/src/include/options.h +++ b/src/include/options.h @@ -36,6 +36,15 @@ typedef enum { PV_ACTION_MONITOR /* run a process, watch its stdin/out */ } pvaction_t; +/* + * Sides of a monitored command to monitor with PV_ACTION_MONITOR. + */ +typedef enum { + PV_SIDE_IN, /* monitor only the input side */ + PV_SIDE_OUT, /* monitor only the output side */ + PV_SIDE_BOTH /* monitor both sides */ +} pvside_t; + /* * Structure describing run-time options. * @@ -71,6 +80,7 @@ struct opts_s { unsigned int watchfd_count; /* number of watchfd items */ unsigned int watchfd_length; /* allocated array size */ pvaction_t action; /* the program action to perform */ + pvside_t side; /* which side of the monitored command to monitor */ bool progress; /* progress bar flag */ bool timer; /* timer flag */ bool eta; /* ETA flag */ diff --git a/src/main/options.c b/src/main/options.c index dc77b4f..1f52d51 100644 --- a/src/main/options.c +++ b/src/main/options.c @@ -416,6 +416,27 @@ static bool opts_watchfd_parse(opts_t opts, const char *argument, /*@null@ */ co } +/* + * Return true if the first string is at least as long as the second, and + * its start matches the entirety of the second. Both strings must be + * null-terminated. + */ +static bool string_starts_with(const char *string, const char *match) +{ + size_t string_length, match_length; + + string_length = strlen(string); /* flawfinder: ignore */ + match_length = strlen(match); /* flawfinder: ignore */ + /* flawfinder - these are null-terminated strings. */ + + if (string_length < match_length) + return false; + if (0 == strncmp(string, match, match_length)) + return true; + return false; +} + + #ifdef HAVE_NFTW /* * Callback function for nftw() to add the size of the given file to the @@ -1107,7 +1128,30 @@ opts_t opts_parse(unsigned int argc, char **argv) break; case 'M': opts->action = PV_ACTION_MONITOR; - /* TODO: parse optarg to set the side */ + if (string_starts_with(optarg, "0")) { + opts->side = PV_SIDE_IN; + } else if (string_starts_with(optarg, "in")) { + opts->side = PV_SIDE_IN; + } else if (string_starts_with(optarg, "stdin")) { + opts->side = PV_SIDE_IN; + } else if (string_starts_with(optarg, "1")) { + opts->side = PV_SIDE_OUT; + } else if (string_starts_with(optarg, "out")) { + opts->side = PV_SIDE_OUT; + } else if (string_starts_with(optarg, "stdout")) { + opts->side = PV_SIDE_OUT; + } else if (string_starts_with(optarg, "2")) { + opts->side = PV_SIDE_BOTH; + } else if (string_starts_with(optarg, "both")) { + opts->side = PV_SIDE_BOTH; + } else { + /*@-mustfreefresh@ *//* see above */ + fprintf(stderr, "%s: -M: %s: %s\n", + opts->program_name, optarg, _("invalid side specification")); + opts_free(opts); + return NULL; + /*@+mustfreefresh@ */ + } break; #ifdef ENABLE_DEBUGGING case '!':