xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
| 12 | * are also available at https://curl.se/docs/copyright.html. |
| 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | #include "tool_setup.h" |
| 25 | |
| 26 | #define ENABLE_CURLX_PRINTF |
| 27 | /* use our own printf() functions */ |
| 28 | #include "curlx.h" |
| 29 | |
| 30 | #include "tool_cfgable.h" |
| 31 | #include "tool_msgs.h" |
| 32 | |
| 33 | #include "memdebug.h" /* keep this as LAST include */ |
| 34 | |
| 35 | #define WARN_PREFIX "Warning: " |
| 36 | #define NOTE_PREFIX "Note: " |
| 37 | #define ERROR_PREFIX "curl: " |
| 38 | |
| 39 | static void voutf(struct GlobalConfig *config, |
| 40 | const char *prefix, |
| 41 | const char *fmt, |
| 42 | va_list ap) |
| 43 | { |
| 44 | size_t width = (79 - strlen(prefix)); |
| 45 | if(!config->mute) { |
| 46 | size_t len; |
| 47 | char *ptr; |
| 48 | char *print_buffer; |
| 49 | |
| 50 | print_buffer = curlx_mvaprintf(fmt, ap); |
| 51 | if(!print_buffer) |
| 52 | return; |
| 53 | len = strlen(print_buffer); |
| 54 | |
| 55 | ptr = print_buffer; |
| 56 | while(len > 0) { |
| 57 | fputs(prefix, config->errors); |
| 58 | |
| 59 | if(len > width) { |
| 60 | size_t cut = width-1; |
| 61 | |
| 62 | while(!ISBLANK(ptr[cut]) && cut) { |
| 63 | cut--; |
| 64 | } |
| 65 | if(0 == cut) |
| 66 | /* not a single cutting position was found, just cut it at the |
| 67 | max text width then! */ |
| 68 | cut = width-1; |
| 69 | |
| 70 | (void)fwrite(ptr, cut + 1, 1, config->errors); |
| 71 | fputs("\n", config->errors); |
| 72 | ptr += cut + 1; /* skip the space too */ |
| 73 | len -= cut + 1; |
| 74 | } |
| 75 | else { |
| 76 | fputs(ptr, config->errors); |
| 77 | len = 0; |
| 78 | } |
| 79 | } |
| 80 | curl_free(print_buffer); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Emit 'note' formatted message on configured 'errors' stream, if verbose was |
| 86 | * selected. |
| 87 | */ |
| 88 | void notef(struct GlobalConfig *config, const char *fmt, ...) |
| 89 | { |
| 90 | va_list ap; |
| 91 | va_start(ap, fmt); |
| 92 | if(config->tracetype) |
| 93 | voutf(config, NOTE_PREFIX, fmt, ap); |
| 94 | va_end(ap); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Emit warning formatted message on configured 'errors' stream unless |
| 99 | * mute (--silent) was selected. |
| 100 | */ |
| 101 | |
| 102 | void warnf(struct GlobalConfig *config, const char *fmt, ...) |
| 103 | { |
| 104 | va_list ap; |
| 105 | va_start(ap, fmt); |
| 106 | voutf(config, WARN_PREFIX, fmt, ap); |
| 107 | va_end(ap); |
| 108 | } |
| 109 | /* |
| 110 | * Emit help formatted message on given stream. This is for errors with or |
| 111 | * related to command line arguments. |
| 112 | */ |
| 113 | void helpf(FILE *errors, const char *fmt, ...) |
| 114 | { |
| 115 | if(fmt) { |
| 116 | va_list ap; |
| 117 | va_start(ap, fmt); |
| 118 | fputs("curl: ", errors); /* prefix it */ |
| 119 | vfprintf(errors, fmt, ap); |
| 120 | va_end(ap); |
| 121 | } |
| 122 | fprintf(errors, "curl: try 'curl --help' " |
| 123 | #ifdef USE_MANUAL |
| 124 | "or 'curl --manual' " |
| 125 | #endif |
| 126 | "for more information\n"); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Emit error message on error stream if not muted. When errors are not tied |
| 131 | * to command line arguments, use helpf() for such errors. |
| 132 | */ |
| 133 | void errorf(struct GlobalConfig *config, const char *fmt, ...) |
| 134 | { |
| 135 | if(!config->mute) { |
| 136 | va_list ap; |
| 137 | va_start(ap, fmt); |
| 138 | voutf(config, ERROR_PREFIX, fmt, ap); |
| 139 | va_end(ap); |
| 140 | } |
| 141 | } |