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