| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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.haxx.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 |
***************************************************************************/ |
| 22 |
#include "tool_setup.h" |
| 23 |
|
| 24 |
#ifdef HAVE_SYS_IOCTL_H |
| 25 |
#include <sys/ioctl.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#define ENABLE_CURLX_PRINTF |
| 29 |
/* use our own printf() functions */ |
| 30 |
#include "curlx.h" |
| 31 |
|
| 32 |
#include "tool_cfgable.h" |
| 33 |
#include "tool_cb_prg.h" |
| 34 |
#include "tool_util.h" |
| 35 |
#include "tool_operate.h" |
| 36 |
|
| 37 |
#include "memdebug.h" /* keep this as LAST include */ |
| 38 |
|
| 39 |
#ifdef HAVE_TERMIOS_H |
| 40 |
# include <termios.h> |
| 41 |
#elif defined(HAVE_TERMIO_H) |
| 42 |
# include <termio.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
/* 200 values generated by this perl code: |
| 46 |
|
| 47 |
my $pi = 3.1415; |
| 48 |
foreach my $i (1 .. 200) { |
| 49 |
printf "%d, ", sin($i/200 * 2 * $pi) * 500000 + 500000; |
| 50 |
} |
| 51 |
*/ |
| 52 |
static const unsigned int sinus[] = { |
| 53 |
515704, 531394, 547052, 562664, 578214, 593687, 609068, 624341, 639491, |
| 54 |
654504, 669364, 684057, 698568, 712883, 726989, 740870, 754513, 767906, |
| 55 |
781034, 793885, 806445, 818704, 830647, 842265, 853545, 864476, 875047, |
| 56 |
885248, 895069, 904500, 913532, 922156, 930363, 938145, 945495, 952406, |
| 57 |
958870, 964881, 970434, 975522, 980141, 984286, 987954, 991139, 993840, |
| 58 |
996054, 997778, 999011, 999752, 999999, 999754, 999014, 997783, 996060, |
| 59 |
993848, 991148, 987964, 984298, 980154, 975536, 970449, 964898, 958888, |
| 60 |
952426, 945516, 938168, 930386, 922180, 913558, 904527, 895097, 885277, |
| 61 |
875077, 864507, 853577, 842299, 830682, 818739, 806482, 793922, 781072, |
| 62 |
767945, 754553, 740910, 727030, 712925, 698610, 684100, 669407, 654548, |
| 63 |
639536, 624386, 609113, 593733, 578260, 562710, 547098, 531440, 515751, |
| 64 |
500046, 484341, 468651, 452993, 437381, 421830, 406357, 390976, 375703, |
| 65 |
360552, 345539, 330679, 315985, 301474, 287158, 273052, 259170, 245525, |
| 66 |
232132, 219003, 206152, 193590, 181331, 169386, 157768, 146487, 135555, |
| 67 |
124983, 114781, 104959, 95526, 86493, 77868, 69660, 61876, 54525, 47613, |
| 68 |
41147, 35135, 29581, 24491, 19871, 15724, 12056, 8868, 6166, 3951, 2225, |
| 69 |
990, 248, 0, 244, 982, 2212, 3933, 6144, 8842, 12025, 15690, 19832, 24448, |
| 70 |
29534, 35084, 41092, 47554, 54462, 61809, 69589, 77794, 86415, 95445, |
| 71 |
104873, 114692, 124891, 135460, 146389, 157667, 169282, 181224, 193480, |
| 72 |
206039, 218888, 232015, 245406, 259048, 272928, 287032, 301346, 315856, |
| 73 |
330548, 345407, 360419, 375568, 390841, 406221, 421693, 437243, 452854, |
| 74 |
468513, 484202, 499907 |
| 75 |
}; |
| 76 |
|
| 77 |
static void fly(struct ProgressData *bar, bool moved) |
| 78 |
{ |
| 79 |
char buf[256]; |
| 80 |
int pos; |
| 81 |
int check = bar->width - 2; |
| 82 |
|
| 83 |
msnprintf(buf, sizeof(buf), "%*s\r", bar->width-1, " "); |
| 84 |
memcpy(&buf[bar->bar], "-=O=-", 5); |
| 85 |
|
| 86 |
pos = sinus[bar->tick%200] / (1000000 / check); |
| 87 |
buf[pos] = '#'; |
| 88 |
pos = sinus[(bar->tick + 5)%200] / (1000000 / check); |
| 89 |
buf[pos] = '#'; |
| 90 |
pos = sinus[(bar->tick + 10)%200] / (1000000 / check); |
| 91 |
buf[pos] = '#'; |
| 92 |
pos = sinus[(bar->tick + 15)%200] / (1000000 / check); |
| 93 |
buf[pos] = '#'; |
| 94 |
|
| 95 |
fputs(buf, bar->out); |
| 96 |
bar->tick += 2; |
| 97 |
if(bar->tick >= 200) |
| 98 |
bar->tick -= 200; |
| 99 |
|
| 100 |
bar->bar += (moved?bar->barmove:0); |
| 101 |
if(bar->bar >= (bar->width - 6)) { |
| 102 |
bar->barmove = -1; |
| 103 |
bar->bar = bar->width - 6; |
| 104 |
} |
| 105 |
else if(bar->bar < 0) { |
| 106 |
bar->barmove = 1; |
| 107 |
bar->bar = 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
** callback for CURLOPT_XFERINFOFUNCTION |
| 113 |
*/ |
| 114 |
|
| 115 |
#define MAX_BARLENGTH 256 |
| 116 |
|
| 117 |
#if (SIZEOF_CURL_OFF_T == 4) |
| 118 |
# define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFF) |
| 119 |
#else |
| 120 |
/* assume CURL_SIZEOF_CURL_OFF_T == 8 */ |
| 121 |
# define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF) |
| 122 |
#endif |
| 123 |
|
| 124 |
int tool_progress_cb(void *clientp, |
| 125 |
curl_off_t dltotal, curl_off_t dlnow, |
| 126 |
curl_off_t ultotal, curl_off_t ulnow) |
| 127 |
{ |
| 128 |
/* The original progress-bar source code was written for curl by Lars Aas, |
| 129 |
and this new edition inherits some of his concepts. */ |
| 130 |
|
| 131 |
struct timeval now = tvnow(); |
| 132 |
struct per_transfer *per = clientp; |
| 133 |
struct OperationConfig *config = per->config; |
| 134 |
struct ProgressData *bar = &per->progressbar; |
| 135 |
curl_off_t total; |
| 136 |
curl_off_t point; |
| 137 |
|
| 138 |
/* Calculate expected transfer size. initial_size can be less than zero |
| 139 |
when indicating that we are expecting to get the filesize from the |
| 140 |
remote */ |
| 141 |
if(bar->initial_size < 0 || |
| 142 |
((CURL_OFF_T_MAX - bar->initial_size) < (dltotal + ultotal))) |
| 143 |
total = CURL_OFF_T_MAX; |
| 144 |
else |
| 145 |
total = dltotal + ultotal + bar->initial_size; |
| 146 |
|
| 147 |
/* Calculate the current progress. initial_size can be less than zero when |
| 148 |
indicating that we are expecting to get the filesize from the remote */ |
| 149 |
if(bar->initial_size < 0 || |
| 150 |
((CURL_OFF_T_MAX - bar->initial_size) < (dlnow + ulnow))) |
| 151 |
point = CURL_OFF_T_MAX; |
| 152 |
else |
| 153 |
point = dlnow + ulnow + bar->initial_size; |
| 154 |
|
| 155 |
if(bar->calls) { |
| 156 |
/* after first call... */ |
| 157 |
if(total) { |
| 158 |
/* we know the total data to get... */ |
| 159 |
if(bar->prev == point) |
| 160 |
/* progress didn't change since last invoke */ |
| 161 |
return 0; |
| 162 |
else if((tvdiff(now, bar->prevtime) < 100L) && point < total) |
| 163 |
/* limit progress-bar updating to 10 Hz except when we're at 100% */ |
| 164 |
return 0; |
| 165 |
} |
| 166 |
else { |
| 167 |
/* total is unknown */ |
| 168 |
if(tvdiff(now, bar->prevtime) < 100L) |
| 169 |
/* limit progress-bar updating to 10 Hz */ |
| 170 |
return 0; |
| 171 |
fly(bar, point != bar->prev); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/* simply count invokes */ |
| 176 |
bar->calls++; |
| 177 |
|
| 178 |
if((total > 0) && (point != bar->prev)) { |
| 179 |
char line[MAX_BARLENGTH + 1]; |
| 180 |
char format[40]; |
| 181 |
double frac; |
| 182 |
double percent; |
| 183 |
int barwidth; |
| 184 |
int num; |
| 185 |
if(point > total) |
| 186 |
/* we have got more than the expected total! */ |
| 187 |
total = point; |
| 188 |
|
| 189 |
frac = (double)point / (double)total; |
| 190 |
percent = frac * 100.0; |
| 191 |
barwidth = bar->width - 7; |
| 192 |
num = (int) (((double)barwidth) * frac); |
| 193 |
if(num > MAX_BARLENGTH) |
| 194 |
num = MAX_BARLENGTH; |
| 195 |
memset(line, '#', num); |
| 196 |
line[num] = '\0'; |
| 197 |
msnprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth); |
| 198 |
fprintf(bar->out, format, line, percent); |
| 199 |
} |
| 200 |
fflush(bar->out); |
| 201 |
bar->prev = point; |
| 202 |
bar->prevtime = now; |
| 203 |
|
| 204 |
if(config->readbusy) { |
| 205 |
config->readbusy = FALSE; |
| 206 |
curl_easy_pause(per->curl, CURLPAUSE_CONT); |
| 207 |
} |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
void progressbarinit(struct ProgressData *bar, |
| 213 |
struct OperationConfig *config) |
| 214 |
{ |
| 215 |
char *colp; |
| 216 |
memset(bar, 0, sizeof(struct ProgressData)); |
| 217 |
|
| 218 |
/* pass this through to progress function so |
| 219 |
* it can display progress towards total file |
| 220 |
* not just the part that's left. (21-may-03, dbyron) */ |
| 221 |
if(config->use_resume) |
| 222 |
bar->initial_size = config->resume_from; |
| 223 |
|
| 224 |
colp = curlx_getenv("COLUMNS"); |
| 225 |
if(colp) { |
| 226 |
char *endptr; |
| 227 |
long num = strtol(colp, &endptr, 10); |
| 228 |
if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20) && |
| 229 |
(num < 10000)) |
| 230 |
bar->width = (int)num; |
| 231 |
curl_free(colp); |
| 232 |
} |
| 233 |
|
| 234 |
if(!bar->width) { |
| 235 |
int cols = 0; |
| 236 |
|
| 237 |
#ifdef TIOCGSIZE |
| 238 |
struct ttysize ts; |
| 239 |
if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts)) |
| 240 |
cols = ts.ts_cols; |
| 241 |
#elif defined(TIOCGWINSZ) |
| 242 |
struct winsize ts; |
| 243 |
if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts)) |
| 244 |
cols = ts.ws_col; |
| 245 |
#elif defined(WIN32) |
| 246 |
{ |
| 247 |
HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE); |
| 248 |
CONSOLE_SCREEN_BUFFER_INFO console_info; |
| 249 |
|
| 250 |
if((stderr_hnd != INVALID_HANDLE_VALUE) && |
| 251 |
GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) { |
| 252 |
/* |
| 253 |
* Do not use +1 to get the true screen-width since writing a |
| 254 |
* character at the right edge will cause a line wrap. |
| 255 |
*/ |
| 256 |
cols = (int) |
| 257 |
(console_info.srWindow.Right - console_info.srWindow.Left); |
| 258 |
} |
| 259 |
} |
| 260 |
#endif /* TIOCGSIZE */ |
| 261 |
if(cols > 20) |
| 262 |
bar->width = cols; |
| 263 |
} |
| 264 |
|
| 265 |
if(!bar->width) |
| 266 |
bar->width = 79; |
| 267 |
else if(bar->width > MAX_BARLENGTH) |
| 268 |
bar->width = MAX_BARLENGTH; |
| 269 |
|
| 270 |
bar->out = config->global->errors; |
| 271 |
bar->tick = 150; |
| 272 |
bar->barmove = 1; |
| 273 |
} |