Parse the monitoring side setting from "--monitor" (#67).

This commit is contained in:
Andrew Wood
2026-03-14 14:28:30 +00:00
parent 111b687449
commit c2bbb8eef1
2 changed files with 55 additions and 1 deletions
+10
View File
@@ -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 */
+45 -1
View File
@@ -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 '!':