curl: make --silent work stand-alone

- renamed the struct field to 'silent' to match the cmdline option
- make --show-error toggle independently of --silent
- make --silent independent of ->noprogress as well

By doing this, the three options --silent, --no-progress-meter and
--show-error should work independently of each other and also work with
and without '--no-' prefix as documented.

Reported-by: u20221022 on github
Fixes #10535
Closes #10536
This commit is contained in:
Daniel Stenberg 2023-02-16 16:34:36 +01:00
parent 6d860f1758
commit 6841f2ed5f
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 26 additions and 34 deletions

View File

@ -298,11 +298,9 @@ struct OperationConfig {
}; };
struct GlobalConfig { struct GlobalConfig {
int showerror; /* -1 == unset, default => show errors bool showerror; /* show errors when silent */
0 => -s is used to NOT show errors bool silent; /* don't show messages, --silent given */
1 => -S has been used to show errors */ bool noprogress; /* don't show progress bar */
bool mute; /* don't show messages, --silent given */
bool noprogress; /* don't show progress bar --silent given */
bool isatty; /* Updated internally if output is a tty */ bool isatty; /* Updated internally if output is a tty */
FILE *errors; /* Error stream, defaults to stderr */ FILE *errors; /* Error stream, defaults to stderr */
bool errors_fopened; /* Whether error stream isn't stderr */ bool errors_fopened; /* Whether error stream isn't stderr */

View File

@ -2268,21 +2268,11 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
/* use remote file's time */ /* use remote file's time */
config->remote_time = toggle; config->remote_time = toggle;
break; break;
case 's': case 's': /* --silent */
/* don't show progress meter, don't show errors : */ global->silent = toggle;
if(toggle)
global->mute = global->noprogress = TRUE;
else
global->mute = global->noprogress = FALSE;
if(global->showerror < 0)
/* if still on the default value, set showerror to the reverse of
toggle. This is to allow -S and -s to be used in an independent
order but still have the same effect. */
global->showerror = (!toggle)?TRUE:FALSE; /* toggle off */
break; break;
case 'S': case 'S': /* --show-error */
/* show errors */ global->showerror = toggle;
global->showerror = toggle?1:0; /* toggle on if used with -s */
break; break;
case 't': case 't':
/* Telnet options */ /* Telnet options */

View File

@ -155,7 +155,7 @@ static CURLcode main_init(struct GlobalConfig *config)
#endif #endif
/* Initialise the global config */ /* Initialise the global config */
config->showerror = -1; /* Will show errors */ config->showerror = FALSE; /* show errors when silent */
config->errors = stderr; /* Default errors to stderr */ config->errors = stderr; /* Default errors to stderr */
config->styled_output = TRUE; /* enable detection */ config->styled_output = TRUE; /* enable detection */
config->parallel_max = PARALLEL_DEFAULT; config->parallel_max = PARALLEL_DEFAULT;

View File

@ -42,7 +42,7 @@ static void voutf(struct GlobalConfig *config,
va_list ap) va_list ap)
{ {
size_t width = (79 - strlen(prefix)); size_t width = (79 - strlen(prefix));
if(!config->mute) { if(!config->silent) {
size_t len; size_t len;
char *ptr; char *ptr;
char *print_buffer; char *print_buffer;
@ -132,7 +132,7 @@ void helpf(FILE *errors, const char *fmt, ...)
*/ */
void errorf(struct GlobalConfig *config, const char *fmt, ...) void errorf(struct GlobalConfig *config, const char *fmt, ...)
{ {
if(!config->mute) { if(!config->silent) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
voutf(config, ERROR_PREFIX, fmt, ap); voutf(config, ERROR_PREFIX, fmt, ap);

View File

@ -396,12 +396,13 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
#ifdef __VMS #ifdef __VMS
if(is_vms_shell()) { if(is_vms_shell()) {
/* VMS DCL shell behavior */ /* VMS DCL shell behavior */
if(!global->showerror) if(global->silent && !global->showerror)
vms_show = VMSSTS_HIDE; vms_show = VMSSTS_HIDE;
} }
else else
#endif #endif
if(!config->synthetic_error && result && global->showerror) { if(!config->synthetic_error && result &&
(!global->silent || global->showerror)) {
const char *msg = per->errorbuffer; const char *msg = per->errorbuffer;
fprintf(global->errors, "curl: (%d) %s\n", result, fprintf(global->errors, "curl: (%d) %s\n", result,
(msg && msg[0]) ? msg : curl_easy_strerror(result)); (msg && msg[0]) ? msg : curl_easy_strerror(result));
@ -413,7 +414,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
long code = 0; long code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code >= 400) { if(code >= 400) {
if(global->showerror) if(!global->silent || global->showerror)
fprintf(global->errors, fprintf(global->errors,
"curl: (%d) The requested URL returned error: %ld\n", "curl: (%d) The requested URL returned error: %ld\n",
CURLE_HTTP_RETURNED_ERROR, code); CURLE_HTTP_RETURNED_ERROR, code);
@ -446,7 +447,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
if(!result && rc) { if(!result && rc) {
/* something went wrong in the writing process */ /* something went wrong in the writing process */
result = CURLE_WRITE_ERROR; result = CURLE_WRITE_ERROR;
if(global->showerror) if(!global->silent || global->showerror)
fprintf(global->errors, "curl: (%d) Failed writing body\n", result); fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
} }
} }
@ -587,7 +588,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
int rc; int rc;
/* We have written data to an output file, we truncate file /* We have written data to an output file, we truncate file
*/ */
if(!global->mute) if(!global->silent)
fprintf(global->errors, "Throwing away %" fprintf(global->errors, "Throwing away %"
CURL_FORMAT_CURL_OFF_T " bytes\n", CURL_FORMAT_CURL_OFF_T " bytes\n",
outs->bytes); outs->bytes);
@ -597,7 +598,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
if(ftruncate(fileno(outs->stream), outs->init)) { if(ftruncate(fileno(outs->stream), outs->init)) {
/* when truncate fails, we can't just append as then we'll /* when truncate fails, we can't just append as then we'll
create something strange, bail out */ create something strange, bail out */
if(global->showerror) if(!global->silent || global->showerror)
fprintf(global->errors, fprintf(global->errors,
"curl: (23) Failed to truncate file\n"); "curl: (23) Failed to truncate file\n");
return CURLE_WRITE_ERROR; return CURLE_WRITE_ERROR;
@ -613,7 +614,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
rc = fseek(outs->stream, (long)outs->init, SEEK_SET); rc = fseek(outs->stream, (long)outs->init, SEEK_SET);
#endif #endif
if(rc) { if(rc) {
if(global->showerror) if(!global->silent || global->showerror)
fprintf(global->errors, fprintf(global->errors,
"curl: (23) Failed seeking to end of file\n"); "curl: (23) Failed seeking to end of file\n");
return CURLE_WRITE_ERROR; return CURLE_WRITE_ERROR;
@ -639,7 +640,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
if(!result && rc) { if(!result && rc) {
/* something went wrong in the writing process */ /* something went wrong in the writing process */
result = CURLE_WRITE_ERROR; result = CURLE_WRITE_ERROR;
if(global->showerror) if(!global->silent || global->showerror)
fprintf(global->errors, "curl: (%d) Failed writing body\n", result); fprintf(global->errors, "curl: (%d) Failed writing body\n", result);
} }
if(result && config->rm_partial) { if(result && config->rm_partial) {
@ -799,7 +800,8 @@ static CURLcode single_transfer(struct GlobalConfig *global,
if(!config->globoff && infiles && !inglob) { if(!config->globoff && infiles && !inglob) {
/* Unless explicitly shut off */ /* Unless explicitly shut off */
result = glob_url(&inglob, infiles, &state->infilenum, result = glob_url(&inglob, infiles, &state->infilenum,
global->showerror?global->errors:NULL); (!global->silent || global->showerror)?
global->errors:NULL);
if(result) if(result)
break; break;
config->state.inglob = inglob; config->state.inglob = inglob;
@ -834,7 +836,8 @@ static CURLcode single_transfer(struct GlobalConfig *global,
/* Unless explicitly shut off, we expand '{...}' and '[...]' /* Unless explicitly shut off, we expand '{...}' and '[...]'
expressions and return total number of URLs in pattern set */ expressions and return total number of URLs in pattern set */
result = glob_url(&state->urls, urlnode->url, &state->urlnum, result = glob_url(&state->urls, urlnode->url, &state->urlnum,
global->showerror?global->errors:NULL); (!global->silent || global->showerror)?
global->errors:NULL);
if(result) if(result)
break; break;
urlnum = state->urlnum; urlnum = state->urlnum;
@ -1316,7 +1319,8 @@ static CURLcode single_transfer(struct GlobalConfig *global,
} }
my_setopt_str(curl, CURLOPT_URL, per->this_url); my_setopt_str(curl, CURLOPT_URL, per->this_url);
my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L); my_setopt(curl, CURLOPT_NOPROGRESS,
global->noprogress || global->silent?1L:0L);
if(config->no_body) if(config->no_body)
my_setopt(curl, CURLOPT_NOBODY, 1L); my_setopt(curl, CURLOPT_NOBODY, 1L);
@ -1853,7 +1857,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
progressbarinit(&per->progressbar, config); progressbarinit(&per->progressbar, config);
if((global->progressmode == CURL_PROGRESS_BAR) && if((global->progressmode == CURL_PROGRESS_BAR) &&
!global->noprogress && !global->mute) { !global->noprogress && !global->silent) {
/* we want the alternative style, then we have to implement it /* we want the alternative style, then we have to implement it
ourselves! */ ourselves! */
my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb); my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);