1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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.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
36 #include "memdebug.h" /* keep this as LAST include */
37
38 #ifdef HAVE_TERMIOS_H
39 # include <termios.h>
40 #elif defined(HAVE_TERMIO_H)
41 # include <termio.h>
42 #endif
43
44 /* 200 values generated by this perl code:
45
46 my $pi = 3.1415;
47 foreach my $i (1 .. 200) {
48 printf "%d, ", sin($i/200 * 2 * $pi) * 5000 + 5000;
49 }
50 */
51 static const unsigned int sinus[] = {
52 5157, 5313, 5470, 5626, 5782, 5936, 6090, 6243, 6394, 6545, 6693, 6840, 6985,
53 7128, 7269, 7408, 7545, 7679, 7810, 7938, 8064, 8187, 8306, 8422, 8535, 8644,
54 8750, 8852, 8950, 9045, 9135, 9221, 9303, 9381, 9454, 9524, 9588, 9648, 9704,
55 9755, 9801, 9842, 9879, 9911, 9938, 9960, 9977, 9990, 9997, 9999, 9997, 9990,
56 9977, 9960, 9938, 9911, 9879, 9842, 9801, 9755, 9704, 9648, 9588, 9524, 9455,
57 9381, 9303, 9221, 9135, 9045, 8950, 8852, 8750, 8645, 8535, 8422, 8306, 8187,
58 8064, 7939, 7810, 7679, 7545, 7409, 7270, 7129, 6986, 6841, 6694, 6545, 6395,
59 6243, 6091, 5937, 5782, 5627, 5470, 5314, 5157, 5000, 4843, 4686, 4529, 4373,
60 4218, 4063, 3909, 3757, 3605, 3455, 3306, 3159, 3014, 2871, 2730, 2591, 2455,
61 2321, 2190, 2061, 1935, 1813, 1693, 1577, 1464, 1355, 1249, 1147, 1049, 955,
62 864, 778, 696, 618, 545, 476, 411, 351, 295, 244, 198, 157, 120, 88, 61, 39,
63 22, 9, 2, 0, 2, 9, 22, 39, 61, 88, 120, 156, 198, 244, 295, 350, 410, 475,
64 544, 618, 695, 777, 864, 954, 1048, 1146, 1248, 1354, 1463, 1576, 1692, 1812,
65 1934, 2060, 2188, 2320, 2454, 2590, 2729, 2870, 3013, 3158, 3305, 3454, 3604,
66 3755, 3908, 4062, 4216, 4372, 4528, 4685, 4842, 4999
67 };
68
fly(struct ProgressData * bar,bool moved)69 static void fly(struct ProgressData *bar, bool moved)
70 {
71 char buf[256];
72 int pos;
73 int check = bar->width - 2;
74
75 msnprintf(buf, sizeof(buf), "%*s\r", bar->width-1, " ");
76 memcpy(&buf[bar->bar], "-=O=-", 5);
77
78 pos = sinus[bar->tick%200] / (10000 / check);
79 buf[pos] = '#';
80 pos = sinus[(bar->tick + 5)%200] / (10000 / check);
81 buf[pos] = '#';
82 pos = sinus[(bar->tick + 10)%200] / (10000 / check);
83 buf[pos] = '#';
84 pos = sinus[(bar->tick + 15)%200] / (10000 / check);
85 buf[pos] = '#';
86
87 fputs(buf, bar->out);
88 bar->tick += 2;
89 if(bar->tick >= 200)
90 bar->tick -= 200;
91
92 bar->bar += (moved?bar->barmove:0);
93 if(bar->bar >= (bar->width - 6)) {
94 bar->barmove = -1;
95 bar->bar = bar->width - 6;
96 }
97 else if(bar->bar < 0) {
98 bar->barmove = 1;
99 bar->bar = 0;
100 }
101 }
102
103 /*
104 ** callback for CURLOPT_XFERINFOFUNCTION
105 */
106
107 #define MAX_BARLENGTH 256
108
109 #if (SIZEOF_CURL_OFF_T == 4)
110 # define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFF)
111 #else
112 /* assume CURL_SIZEOF_CURL_OFF_T == 8 */
113 # define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF)
114 #endif
115
tool_progress_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)116 int tool_progress_cb(void *clientp,
117 curl_off_t dltotal, curl_off_t dlnow,
118 curl_off_t ultotal, curl_off_t ulnow)
119 {
120 /* The original progress-bar source code was written for curl by Lars Aas,
121 and this new edition inherits some of his concepts. */
122
123 struct timeval now = tvnow();
124 struct ProgressData *bar = (struct ProgressData *)clientp;
125 curl_off_t total;
126 curl_off_t point;
127
128 /* Calculate expected transfer size. initial_size can be less than zero
129 when indicating that we are expecting to get the filesize from the
130 remote */
131 if(bar->initial_size < 0 ||
132 ((CURL_OFF_T_MAX - bar->initial_size) < (dltotal + ultotal)))
133 total = CURL_OFF_T_MAX;
134 else
135 total = dltotal + ultotal + bar->initial_size;
136
137 /* Calculate the current progress. initial_size can be less than zero when
138 indicating that we are expecting to get the filesize from the remote */
139 if(bar->initial_size < 0 ||
140 ((CURL_OFF_T_MAX - bar->initial_size) < (dlnow + ulnow)))
141 point = CURL_OFF_T_MAX;
142 else
143 point = dlnow + ulnow + bar->initial_size;
144
145 if(bar->calls) {
146 /* after first call... */
147 if(total) {
148 /* we know the total data to get... */
149 if(bar->prev == point)
150 /* progress didn't change since last invoke */
151 return 0;
152 else if((tvdiff(now, bar->prevtime) < 100L) && point < total)
153 /* limit progress-bar updating to 10 Hz except when we're at 100% */
154 return 0;
155 }
156 else {
157 /* total is unknown */
158 if(tvdiff(now, bar->prevtime) < 100L)
159 /* limit progress-bar updating to 10 Hz */
160 return 0;
161 fly(bar, point != bar->prev);
162 }
163 }
164
165 /* simply count invokes */
166 bar->calls++;
167
168 if((total > 0) && (point != bar->prev)) {
169 char line[MAX_BARLENGTH + 1];
170 char format[40];
171 double frac;
172 double percent;
173 int barwidth;
174 int num;
175 if(point > total)
176 /* we have got more than the expected total! */
177 total = point;
178
179 frac = (double)point / (double)total;
180 percent = frac * 100.0;
181 barwidth = bar->width - 7;
182 num = (int) (((double)barwidth) * frac);
183 if(num > MAX_BARLENGTH)
184 num = MAX_BARLENGTH;
185 memset(line, '#', num);
186 line[num] = '\0';
187 msnprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
188 fprintf(bar->out, format, line, percent);
189 }
190 fflush(bar->out);
191 bar->prev = point;
192 bar->prevtime = now;
193
194 return 0;
195 }
196
progressbarinit(struct ProgressData * bar,struct OperationConfig * config)197 void progressbarinit(struct ProgressData *bar,
198 struct OperationConfig *config)
199 {
200 char *colp;
201 memset(bar, 0, sizeof(struct ProgressData));
202
203 /* pass this through to progress function so
204 * it can display progress towards total file
205 * not just the part that's left. (21-may-03, dbyron) */
206 if(config->use_resume)
207 bar->initial_size = config->resume_from;
208
209 colp = curlx_getenv("COLUMNS");
210 if(colp) {
211 char *endptr;
212 long num = strtol(colp, &endptr, 10);
213 if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20) &&
214 (num < 10000))
215 bar->width = (int)num;
216 curl_free(colp);
217 }
218
219 if(!bar->width) {
220 int cols = 0;
221
222 #ifdef TIOCGSIZE
223 struct ttysize ts;
224 if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))
225 cols = ts.ts_cols;
226 #elif defined(TIOCGWINSZ)
227 struct winsize ts;
228 if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
229 cols = ts.ws_col;
230 #elif defined(WIN32)
231 {
232 HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
233 CONSOLE_SCREEN_BUFFER_INFO console_info;
234
235 if((stderr_hnd != INVALID_HANDLE_VALUE) &&
236 GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {
237 /*
238 * Do not use +1 to get the true screen-width since writing a
239 * character at the right edge will cause a line wrap.
240 */
241 cols = (int)
242 (console_info.srWindow.Right - console_info.srWindow.Left);
243 }
244 }
245 #endif /* TIOCGSIZE */
246 bar->width = cols;
247 }
248
249 if(!bar->width)
250 bar->width = 79;
251 else if(bar->width > MAX_BARLENGTH)
252 bar->width = MAX_BARLENGTH;
253
254 bar->out = config->global->errors;
255 bar->tick = 150;
256 bar->barmove = 1;
257 }
258