1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
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
fly(struct ProgressData * bar,bool moved)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 SIZEOF_CURL_OFF_T == 8 */
121 # define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF)
122 #endif
123
tool_progress_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)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 struct timeval now = tvnow();
129 struct per_transfer *per = clientp;
130 struct OperationConfig *config = per->config;
131 struct ProgressData *bar = &per->progressbar;
132 curl_off_t total;
133 curl_off_t point;
134
135 /* Calculate expected transfer size. initial_size can be less than zero when
136 indicating that we are expecting to get the filesize from the remote */
137 if(bar->initial_size < 0) {
138 if(dltotal || ultotal)
139 total = dltotal + ultotal;
140 else
141 total = CURL_OFF_T_MAX;
142 }
143 else if((CURL_OFF_T_MAX - bar->initial_size) < (dltotal + ultotal))
144 total = CURL_OFF_T_MAX;
145 else
146 total = dltotal + ultotal + bar->initial_size;
147
148 /* Calculate the current progress. initial_size can be less than zero when
149 indicating that we are expecting to get the filesize from the remote */
150 if(bar->initial_size < 0) {
151 if(dltotal || ultotal)
152 point = dlnow + ulnow;
153 else
154 point = CURL_OFF_T_MAX;
155 }
156 else if((CURL_OFF_T_MAX - bar->initial_size) < (dlnow + ulnow))
157 point = CURL_OFF_T_MAX;
158 else
159 point = dlnow + ulnow + bar->initial_size;
160
161 if(bar->calls) {
162 /* after first call... */
163 if(total) {
164 /* we know the total data to get... */
165 if(bar->prev == point)
166 /* progress didn't change since last invoke */
167 return 0;
168 else if((tvdiff(now, bar->prevtime) < 100L) && point < total)
169 /* limit progress-bar updating to 10 Hz except when we're at 100% */
170 return 0;
171 }
172 else {
173 /* total is unknown */
174 if(tvdiff(now, bar->prevtime) < 100L)
175 /* limit progress-bar updating to 10 Hz */
176 return 0;
177 fly(bar, point != bar->prev);
178 }
179 }
180
181 /* simply count invokes */
182 bar->calls++;
183
184 if((total > 0) && (point != bar->prev)) {
185 char line[MAX_BARLENGTH + 1];
186 char format[40];
187 double frac;
188 double percent;
189 int barwidth;
190 int num;
191 if(point > total)
192 /* we have got more than the expected total! */
193 total = point;
194
195 frac = (double)point / (double)total;
196 percent = frac * 100.0;
197 barwidth = bar->width - 7;
198 num = (int) (((double)barwidth) * frac);
199 if(num > MAX_BARLENGTH)
200 num = MAX_BARLENGTH;
201 memset(line, '#', num);
202 line[num] = '\0';
203 msnprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
204 fprintf(bar->out, format, line, percent);
205 }
206 fflush(bar->out);
207 bar->prev = point;
208 bar->prevtime = now;
209
210 if(config->readbusy) {
211 config->readbusy = FALSE;
212 curl_easy_pause(per->curl, CURLPAUSE_CONT);
213 }
214
215 return 0;
216 }
217
progressbarinit(struct ProgressData * bar,struct OperationConfig * config)218 void progressbarinit(struct ProgressData *bar,
219 struct OperationConfig *config)
220 {
221 char *colp;
222 memset(bar, 0, sizeof(struct ProgressData));
223
224 /* pass the resume from value through to the progress function so it can
225 * display progress towards total file not just the part that's left. */
226 if(config->use_resume)
227 bar->initial_size = config->resume_from;
228
229 colp = curlx_getenv("COLUMNS");
230 if(colp) {
231 char *endptr;
232 long num = strtol(colp, &endptr, 10);
233 if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20) &&
234 (num < 10000))
235 bar->width = (int)num;
236 curl_free(colp);
237 }
238
239 if(!bar->width) {
240 int cols = 0;
241
242 #ifdef TIOCGSIZE
243 struct ttysize ts;
244 if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))
245 cols = ts.ts_cols;
246 #elif defined(TIOCGWINSZ)
247 struct winsize ts;
248 if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
249 cols = ts.ws_col;
250 #elif defined(WIN32)
251 {
252 HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
253 CONSOLE_SCREEN_BUFFER_INFO console_info;
254
255 if((stderr_hnd != INVALID_HANDLE_VALUE) &&
256 GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {
257 /*
258 * Do not use +1 to get the true screen-width since writing a
259 * character at the right edge will cause a line wrap.
260 */
261 cols = (int)
262 (console_info.srWindow.Right - console_info.srWindow.Left);
263 }
264 }
265 #endif /* TIOCGSIZE */
266 if(cols > 20)
267 bar->width = cols;
268 }
269
270 if(!bar->width)
271 bar->width = 79;
272 else if(bar->width > MAX_BARLENGTH)
273 bar->width = MAX_BARLENGTH;
274
275 bar->out = config->global->errors;
276 bar->tick = 150;
277 bar->barmove = 1;
278 }
279