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