Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_cb_prg.c
2065 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) 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.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
#include <curlx.h>
27
28
#include "tool_cfgable.h"
29
#include "tool_cb_prg.h"
30
#include "tool_util.h"
31
#include "tool_operate.h"
32
#include "terminal.h"
33
34
#include <memdebug.h> /* keep this as LAST include */
35
36
#define MAX_BARLENGTH 400
37
#define MIN_BARLENGTH 20
38
39
/* 200 values generated by this perl code:
40
41
my $pi = 3.1415;
42
foreach my $i (1 .. 200) {
43
printf "%d, ", sin($i/200 * 2 * $pi) * 500000 + 500000;
44
}
45
*/
46
static const int sinus[] = {
47
515704, 531394, 547052, 562664, 578214, 593687, 609068, 624341, 639491,
48
654504, 669364, 684057, 698568, 712883, 726989, 740870, 754513, 767906,
49
781034, 793885, 806445, 818704, 830647, 842265, 853545, 864476, 875047,
50
885248, 895069, 904500, 913532, 922156, 930363, 938145, 945495, 952406,
51
958870, 964881, 970434, 975522, 980141, 984286, 987954, 991139, 993840,
52
996054, 997778, 999011, 999752, 999999, 999754, 999014, 997783, 996060,
53
993848, 991148, 987964, 984298, 980154, 975536, 970449, 964898, 958888,
54
952426, 945516, 938168, 930386, 922180, 913558, 904527, 895097, 885277,
55
875077, 864507, 853577, 842299, 830682, 818739, 806482, 793922, 781072,
56
767945, 754553, 740910, 727030, 712925, 698610, 684100, 669407, 654548,
57
639536, 624386, 609113, 593733, 578260, 562710, 547098, 531440, 515751,
58
500046, 484341, 468651, 452993, 437381, 421830, 406357, 390976, 375703,
59
360552, 345539, 330679, 315985, 301474, 287158, 273052, 259170, 245525,
60
232132, 219003, 206152, 193590, 181331, 169386, 157768, 146487, 135555,
61
124983, 114781, 104959, 95526, 86493, 77868, 69660, 61876, 54525, 47613,
62
41147, 35135, 29581, 24491, 19871, 15724, 12056, 8868, 6166, 3951, 2225,
63
990, 248, 0, 244, 982, 2212, 3933, 6144, 8842, 12025, 15690, 19832, 24448,
64
29534, 35084, 41092, 47554, 54462, 61809, 69589, 77794, 86415, 95445,
65
104873, 114692, 124891, 135460, 146389, 157667, 169282, 181224, 193480,
66
206039, 218888, 232015, 245406, 259048, 272928, 287032, 301346, 315856,
67
330548, 345407, 360419, 375568, 390841, 406221, 421693, 437243, 452854,
68
468513, 484202, 499907
69
};
70
71
static void fly(struct ProgressData *bar, bool moved)
72
{
73
char buf[MAX_BARLENGTH + 2];
74
int pos;
75
int check = bar->width - 2;
76
77
/* bar->width is range checked when assigned */
78
DEBUGASSERT(bar->width <= MAX_BARLENGTH);
79
buf[0] = '\r';
80
memset(&buf[1], ' ', bar->width);
81
buf[bar->width + 1] = '\0';
82
83
memcpy(&buf[bar->bar + 1], "-=O=-", 5);
84
85
pos = sinus[bar->tick%200] / (1000000 / check) + 1;
86
buf[pos] = '#';
87
pos = sinus[(bar->tick + 5)%200] / (1000000 / check) + 1;
88
buf[pos] = '#';
89
pos = sinus[(bar->tick + 10)%200] / (1000000 / check) + 1;
90
buf[pos] = '#';
91
pos = sinus[(bar->tick + 15)%200] / (1000000 / check) + 1;
92
buf[pos] = '#';
93
94
fputs(buf, bar->out);
95
bar->tick += 2;
96
if(bar->tick >= 200)
97
bar->tick -= 200;
98
99
bar->bar += (moved ? bar->barmove : 0);
100
if(bar->bar >= (bar->width - 6)) {
101
bar->barmove = -1;
102
bar->bar = bar->width - 6;
103
}
104
else if(bar->bar < 0) {
105
bar->barmove = 1;
106
bar->bar = 0;
107
}
108
}
109
110
/*
111
** callback for CURLOPT_XFERINFOFUNCTION
112
*/
113
114
#if (SIZEOF_CURL_OFF_T < 8)
115
#error "too small curl_off_t"
116
#else
117
/* assume SIZEOF_CURL_OFF_T == 8 */
118
# define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF)
119
#endif
120
121
static void update_width(struct ProgressData *bar)
122
{
123
int cols = get_terminal_columns();
124
if(cols > MAX_BARLENGTH)
125
bar->width = MAX_BARLENGTH;
126
else if(cols > MIN_BARLENGTH)
127
bar->width = (int)cols;
128
else
129
bar->width = MIN_BARLENGTH;
130
}
131
132
int tool_progress_cb(void *clientp,
133
curl_off_t dltotal, curl_off_t dlnow,
134
curl_off_t ultotal, curl_off_t ulnow)
135
{
136
struct curltime now = curlx_now();
137
struct per_transfer *per = clientp;
138
struct OperationConfig *config = per->config;
139
struct ProgressData *bar = &per->progressbar;
140
curl_off_t total;
141
curl_off_t point;
142
143
/* Calculate expected transfer size. initial_size can be less than zero when
144
indicating that we are expecting to get the filesize from the remote */
145
if(bar->initial_size < 0) {
146
if(dltotal || ultotal)
147
total = dltotal + ultotal;
148
else
149
total = CURL_OFF_T_MAX;
150
}
151
else if((CURL_OFF_T_MAX - bar->initial_size) < (dltotal + ultotal))
152
total = CURL_OFF_T_MAX;
153
else
154
total = dltotal + ultotal + bar->initial_size;
155
156
/* Calculate the current progress. initial_size can be less than zero when
157
indicating that we are expecting to get the filesize from the remote */
158
if(bar->initial_size < 0) {
159
if(dltotal || ultotal)
160
point = dlnow + ulnow;
161
else
162
point = CURL_OFF_T_MAX;
163
}
164
else if((CURL_OFF_T_MAX - bar->initial_size) < (dlnow + ulnow))
165
point = CURL_OFF_T_MAX;
166
else
167
point = dlnow + ulnow + bar->initial_size;
168
169
if(bar->calls) {
170
/* after first call... */
171
if(total) {
172
/* we know the total data to get... */
173
if(bar->prev == point)
174
/* progress did not change since last invoke */
175
return 0;
176
else if((curlx_timediff(now, bar->prevtime) < 100L) && point < total)
177
/* limit progress-bar updating to 10 Hz except when we are at 100% */
178
return 0;
179
}
180
else {
181
/* total is unknown */
182
if(curlx_timediff(now, bar->prevtime) < 100L)
183
/* limit progress-bar updating to 10 Hz */
184
return 0;
185
update_width(bar);
186
fly(bar, point != bar->prev);
187
}
188
}
189
190
/* simply count invokes */
191
bar->calls++;
192
193
update_width(bar);
194
if((total > 0) && (point != bar->prev)) {
195
char line[MAX_BARLENGTH + 1];
196
char format[40];
197
double frac;
198
double percent;
199
int barwidth;
200
size_t num;
201
if(point > total)
202
/* we have got more than the expected total! */
203
total = point;
204
205
frac = (double)point / (double)total;
206
percent = frac * 100.0;
207
barwidth = bar->width - 7;
208
num = (size_t) (((double)barwidth) * frac);
209
if(num > MAX_BARLENGTH)
210
num = MAX_BARLENGTH;
211
memset(line, '#', num);
212
line[num] = '\0';
213
msnprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
214
#ifdef __clang__
215
#pragma clang diagnostic push
216
#pragma clang diagnostic ignored "-Wformat-nonliteral"
217
#endif
218
fprintf(bar->out, format, line, percent);
219
#ifdef __clang__
220
#pragma clang diagnostic pop
221
#endif
222
}
223
fflush(bar->out);
224
bar->prev = point;
225
bar->prevtime = now;
226
227
if(config->readbusy) {
228
config->readbusy = FALSE;
229
curl_easy_pause(per->curl, CURLPAUSE_CONT);
230
}
231
232
return 0;
233
}
234
235
void progressbarinit(struct ProgressData *bar,
236
struct OperationConfig *config)
237
{
238
memset(bar, 0, sizeof(struct ProgressData));
239
240
/* pass the resume from value through to the progress function so it can
241
* display progress towards total file not just the part that is left. */
242
if(config->use_resume)
243
bar->initial_size = config->resume_from;
244
245
update_width(bar);
246
247
bar->out = tool_stderr;
248
bar->tick = 150;
249
bar->barmove = 1;
250
}
251
252