-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1504 lines (1287 loc) · 45.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <curl/curl.h>
#include <curses.h>
#include <event.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
typedef struct DownloadItem {
int mode;
char *url;
char *escape_url;
char *effective_url;
char *primary_ip;
long primary_port;
long rcode;
long protocol;
double contentlength;
double download_size;
FILE *outputfile;
char *outputfilename;
char *contenttype;
double progress;
double uprogress;
int speed;
long eta;
curl_off_t max_speed;
long int downloaded;
long int start_time;
long int end_time;
CURL *handle;
struct DownloadItem *next;
struct DownloadItem *prev;
} DownloadItem;
typedef struct SockInfo {
curl_socket_t sockfd;
CURL *easy;
struct event *ev;
long timeout;
int action;
int evset;
} SockInfo;
#define MODE_ALL 0
#define MODE_INACTIVE 1
#define MODE_PAUSED 2
#define MODE_ACTIVE 3
#define MODE_FINISHED 4
#define NB_MODES 5
int current_mode = MODE_ALL;
#define ENTERING_URL 1
#define ENTERING_REFERER 2
#define ENTERING_SEARCH 3
#define PARAM_REFERER 1
#define PARAM_OUTPUT 2
#define PARAM_MAXTCONN 3
#define PARAM_OUTPUTOVER 4
#define PARAM_INPUTFILE 5
#define PARAM_MAXSPEED 6
#define PARAM_MAXHCONN 7
#define PARAM_AUTOSTART 8
#define PARAM_AUTOEXIT 9
#define MAX_STRING_LEN 16384
char *last_search = NULL;
char *string = NULL;
CURLM *mhandle = NULL;
struct event_base *eventbase = NULL;
struct event *timerevent = NULL;
DownloadItem *items = NULL;
DownloadItem *items_tail = NULL;
DownloadItem *sitem[NB_MODES] = { NULL };
long int start_time = INT_MIN;
int nb_ditems = 0;
int nb_logs = 0;
int string_pos = 0;
int current_page = 0;
int still_running = 0;
int help_active = 0;
int info_active = 0;
int log_active = 0;
int downloading = 0;
int inactive_downloads = 0;
int paused_downloads = 0;
int active_downloads = 0;
int finished_downloads = 0;
int auto_start = 0;
int auto_exit = 0;
pthread_t curses_thread;
pthread_t curl_thread;
WINDOW *downloads = NULL;
WINDOW *helpwin = NULL;
WINDOW *infowin = NULL;
WINDOW *logwin = NULL;
WINDOW *openwin = NULL;
WINDOW *statuswin = NULL;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
static char *clonestring(const char *string, size_t string_len)
{
char *clone = malloc((string_len + 1) * sizeof(*string));
if (clone) {
memcpy(clone, string, (string_len + 1) * sizeof(*string));
clone[string_len] = '\0';
}
return clone;
}
static void write_log(int color, const char *fmt, ...)
{
int y, y0, x;
va_list vl;
getyx(logwin, y0, x);
wattrset(logwin, color);
va_start(vl, fmt);
vwprintw(logwin, fmt, vl);
va_end(vl);
getyx(logwin, y, x);
nb_logs += y - y0;
(void)x;
}
static void check_erc(const char *where, CURLcode code)
{
if (code == CURLE_OK)
return;
write_log(COLOR_PAIR(1), "%s returns %s\n", where, curl_easy_strerror(code));
}
static void check_mrc(const char *where, CURLMcode code)
{
if (code == CURLM_OK)
return;
write_log(COLOR_PAIR(1), "%s returns %s\n", where, curl_multi_strerror(code));
}
static DownloadItem* delete_ditem(DownloadItem *ditem)
{
int i;
for (i = 0; i < NB_MODES; i++) {
if (ditem == sitem[i]) {
sitem[i] = NULL;
}
}
if (ditem->handle) {
if (ditem->mode == MODE_ACTIVE) {
CURLMcode rc;
rc = curl_multi_remove_handle(mhandle, ditem->handle);
check_mrc("delete:", rc);
}
curl_easy_cleanup(ditem->handle);
}
if (ditem->url)
free(ditem->url);
if (ditem->escape_url)
free(ditem->escape_url);
if (ditem->outputfile)
fclose(ditem->outputfile);
if (ditem->outputfilename)
free(ditem->outputfilename);
nb_ditems--;
if (ditem->mode == MODE_INACTIVE) {
inactive_downloads--;
} else if (ditem->mode == MODE_PAUSED) {
paused_downloads--;
} else if (ditem->mode == MODE_ACTIVE) {
active_downloads--;
} else if (ditem->mode == MODE_FINISHED) {
finished_downloads--;
}
if (ditem->prev && ditem->next) {
DownloadItem *old = ditem;
ditem = old->prev;
ditem->next = old->next;
old->next->prev = ditem;
ditem = ditem->next;
if (current_mode) {
DownloadItem *temp = ditem;
for (; ditem; ditem = ditem->next) {
if (ditem->mode == current_mode)
break;
}
if (!ditem) {
ditem = temp;
for (; ditem; ditem = ditem->prev) {
if (ditem->mode == current_mode)
break;
}
}
}
free(old);
} else if (ditem->next) {
DownloadItem *old;
old = ditem;
items = ditem = ditem->next;
ditem->prev = NULL;
if (current_mode) {
for (; ditem; ditem = ditem->next) {
if (ditem->mode == current_mode)
break;
}
}
free(old);
} else if (ditem->prev) {
DownloadItem *old;
old = ditem;
ditem = items_tail = ditem->prev;
ditem->next = NULL;
if (current_mode) {
for (; ditem; ditem = ditem->prev) {
if (ditem->mode == current_mode)
break;
}
}
free(old);
} else {
free(ditem);
items = ditem = items_tail = NULL;
}
return ditem;
}
static void write_status(int color, const char *fmt, ...)
{
va_list vl;
werase(statuswin);
wattrset(statuswin, color);
va_start(vl, fmt);
vwprintw(statuswin, fmt, vl);
va_end(vl);
wnoutrefresh(statuswin);
}
static void write_helpwin()
{
int i = 0;
wattrset(helpwin, COLOR_PAIR(5));
mvwaddstr(helpwin, i++, 0, " 1 - show all downloads ");
mvwaddstr(helpwin, i++, 0, " 2 - show only inactive downloads ");
mvwaddstr(helpwin, i++, 0, " 3 - show only paused downloads ");
mvwaddstr(helpwin, i++, 0, " 4 - show only active downloads ");
mvwaddstr(helpwin, i++, 0, " 5 - show only finished downloads ");
mvwaddstr(helpwin, i++, 0, " a - add new download URL, downloading from last position ");
mvwaddstr(helpwin, i++, 0, " A - add new download URL, downloading from beginning ");
mvwaddstr(helpwin, i++, 0, " S - start/stop all downloads ");
mvwaddstr(helpwin, i++, 0, " H - halt selected download ");
mvwaddstr(helpwin, i++, 0, " h - unhalt selected download ");
mvwaddstr(helpwin, i++, 0, " p - pause/unpause selected download ");
mvwaddstr(helpwin, i++, 0, " D - delete selected download from the list ");
mvwaddstr(helpwin, i++, 0, " R - set referer for the selected download ");
mvwaddstr(helpwin, i++, 0, " i - show extra info for selected download ");
mvwaddstr(helpwin, i++, 0, " l - show log of all downloads ");
mvwaddstr(helpwin, i++, 0, " / - search for download ");
mvwaddstr(helpwin, i++, 0, " n - repeat last search ");
mvwaddstr(helpwin, i++, 0, " N - repeat last search backward ");
mvwaddstr(helpwin, i++, 0, " UP/DOWN - select download ");
mvwaddstr(helpwin, i++, 0, " LEFT/RIGHT - decrease/increase download speed ");
mvwaddstr(helpwin, i++, 0, " Q - quit ");
wnoutrefresh(helpwin);
}
static void write_infowin(DownloadItem *sitem)
{
int i = 0;
if (!sitem)
return;
curl_easy_getinfo(sitem->handle, CURLINFO_EFFECTIVE_URL, &sitem->effective_url);
curl_easy_getinfo(sitem->handle, CURLINFO_RESPONSE_CODE, &sitem->rcode);
curl_easy_getinfo(sitem->handle, CURLINFO_PROTOCOL, &sitem->protocol);
curl_easy_getinfo(sitem->handle, CURLINFO_CONTENT_TYPE, &sitem->contenttype);
curl_easy_getinfo(sitem->handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &sitem->contentlength);
curl_easy_getinfo(sitem->handle, CURLINFO_SIZE_DOWNLOAD, &sitem->download_size);
curl_easy_getinfo(sitem->handle, CURLINFO_PRIMARY_IP, &sitem->primary_ip);
curl_easy_getinfo(sitem->handle, CURLINFO_PRIMARY_PORT, &sitem->primary_port);
wattrset(infowin, COLOR_PAIR(7));
mvwprintw(infowin, i++, 0, " Filename: %.*s ", COLS, sitem->outputfilename);
mvwprintw(infowin, i++, 0, " URL: %.*s ", COLS, sitem->url);
mvwprintw(infowin, i++, 0, " Effective URL: %.*s ", COLS, sitem->effective_url);
mvwprintw(infowin, i++, 0, " Current download speed: %ldB/s ", sitem->speed);
mvwprintw(infowin, i++, 0, " Max download speed: %ldB/s ", sitem->max_speed);
mvwprintw(infowin, i++, 0, " Response code: %ld ", sitem->rcode);
mvwprintw(infowin, i++, 0, " Content-type: %s ", sitem->contenttype);
mvwprintw(infowin, i++, 0, " Content-length: %.0f ", sitem->contentlength);
mvwprintw(infowin, i++, 0, " Download size: %.0f ", sitem->download_size);
mvwprintw(infowin, i++, 0, " Download time: %ld ", sitem->start_time ? ((sitem->end_time ? sitem->end_time : time(NULL)) - sitem->start_time) : 0);
mvwprintw(infowin, i++, 0, " ETA: %ld ", sitem->eta);
mvwprintw(infowin, i++, 0, " Primary IP: %s ", sitem->primary_ip);
mvwprintw(infowin, i++, 0, " Primary port: %ld ", sitem->primary_port);
mvwprintw(infowin, i++, 0, " Used Protocol: ");
switch (sitem->protocol) {
case CURLPROTO_HTTP: waddstr(infowin, "HTTP"); break;
case CURLPROTO_HTTPS: waddstr(infowin, "HTTPS"); break;
case CURLPROTO_FTP: waddstr(infowin, "FTP"); break;
case CURLPROTO_FTPS: waddstr(infowin, "FTPS"); break;
case CURLPROTO_SCP: waddstr(infowin, "SCP"); break;
case CURLPROTO_SFTP: waddstr(infowin, "SFTP"); break;
case CURLPROTO_FILE: waddstr(infowin, "FILE"); break;
case CURLPROTO_TFTP: waddstr(infowin, "TFTP"); break;
case CURLPROTO_TELNET: waddstr(infowin, "TELNET"); break;
default: waddstr(infowin, "unknown");
}
wnoutrefresh(infowin);
}
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *ourptr)
{
FILE *outfile = ourptr;
return fwrite(ptr, size, nmemb, outfile);
}
static int progressf(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
DownloadItem *item = clientp;
long int stime = item->start_time ? item->start_time : start_time;
long int curr_time = time(NULL);
long int tdiff = curr_time - stime;
if (dltotal)
item->progress = 100. * (dlnow + item->downloaded)/(dltotal + item->downloaded);
else
item->progress = 0;
if (ultotal)
item->uprogress = 100. * ulnow/ultotal;
else
item->uprogress = 0;
if (tdiff != 0)
item->speed = dlnow / tdiff;
else
item->speed = 0;
if (item->speed)
item->eta = (dltotal - dlnow) / item->speed;
else
item->eta = -1;
return 0;
}
static int get_fg(DownloadItem *ditem)
{
int bold = 0;
if (ditem->mode == MODE_ACTIVE)
bold = A_BOLD;
if (ditem->mode == MODE_INACTIVE) {
return ditem == sitem[current_mode] ? bold | A_REVERSE | COLOR_PAIR(5) : bold | A_REVERSE | COLOR_PAIR(4);
} else {
return ditem == sitem[current_mode] ? bold | A_REVERSE | COLOR_PAIR(3) : bold | A_REVERSE | COLOR_PAIR(2);
}
}
static int get_bg(DownloadItem *ditem)
{
int bold = 0;
if (ditem->mode == MODE_ACTIVE)
bold = A_BOLD;
if (ditem->mode == MODE_INACTIVE) {
return ditem == sitem[current_mode] ? bold | COLOR_PAIR(5) : bold | COLOR_PAIR(4);
} else {
return ditem == sitem[current_mode] ? bold | COLOR_PAIR(3) : bold | COLOR_PAIR(2);
}
}
static void uninit()
{
DownloadItem *item = items_tail;
pthread_cancel(curl_thread);
pthread_cancel(curses_thread);
delwin(logwin);
delwin(openwin);
delwin(infowin);
delwin(helpwin);
delwin(statuswin);
delwin(downloads);
endwin();
for (;item;)
item = delete_ditem(item);
curl_multi_cleanup(mhandle);
mhandle = NULL;
curl_global_cleanup();
items = NULL;
items_tail = NULL;
free(last_search);
last_search = NULL;
free(string);
string = NULL;
event_free(timerevent);
timerevent = NULL;
event_base_free(eventbase);
eventbase = NULL;
}
static void error(int sig, const char *error_msg)
{
uninit();
fprintf(stderr, "%s", error_msg);
exit(sig);
}
static void finish(int sig)
{
uninit();
exit(sig);
}
static int create_handle(int overwritefile, const char *newurl,
const char *referer, const char *outname,
curl_off_t speed)
{
DownloadItem *item;
FILE *outputfile = NULL;
const char *ofilename;
const char *lpath;
char *unescape;
CURL *handle;
CURLcode rc;
int urllen, escape_url_size, i;
string_pos = 0;
urllen = strlen(newurl);
if (urllen <= 1) {
werase(openwin);
return 1;
}
for (item = items; item; item = item->next) {
if (item->url && !strcmp(newurl, item->url)) {
write_status(A_REVERSE | COLOR_PAIR(1), "URL already in use");
return 1;
}
}
if (items == NULL) {
items = calloc(1, sizeof(DownloadItem));
if (!items) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to allocate DownloadItem");
return 1;
}
item = items_tail = items;
} else {
DownloadItem *prev;
item = items_tail;
item->next = calloc(1, sizeof(DownloadItem));
if (!item->next) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to allocate DownloadItem");
return 1;
}
prev = item;
item = items_tail = item->next;
item->prev = prev;
}
item->url = clonestring(newurl, urllen);
if (!item->url) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to copy URL");
delete_ditem(item);
return 1;
}
item->handle = handle = curl_easy_init();
if (!handle) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to create curl easy handle");
delete_ditem(item);
return 1;
}
unescape = curl_easy_unescape(handle, newurl, urllen, NULL);
if (!unescape) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to unescape URL");
delete_ditem(item);
return 1;
}
lpath = strrchr(unescape, '/');
if (!lpath) {
curl_free(unescape);
write_status(A_REVERSE | COLOR_PAIR(1), "Invalid URL");
delete_ditem(item);
return 1;
}
lpath += 1;
if (!strcmp(unescape, newurl)) {
char *escape = curl_easy_escape(handle, lpath, 0);
if (!escape) {
curl_free(unescape);
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to escape URL");
delete_ditem(item);
return 1;
}
escape_url_size = strlen(escape) + strlen(newurl);
item->escape_url = calloc(escape_url_size, sizeof(*item->escape_url));
if (!item->escape_url) {
curl_free(unescape);
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to allocate escape URL");
delete_ditem(item);
return 1;
}
for (i = urllen - 1; i >= 0; i--) {
if (newurl[i] == '/')
break;
}
snprintf(item->escape_url, escape_url_size, "%.*s/%s", i, newurl, escape);
curl_free(escape);
} else {
item->escape_url = clonestring(newurl, urllen);
if (!item->escape_url) {
curl_free(unescape);
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to duplicate url");
delete_ditem(item);
return 1;
}
}
ofilename = outname ? outname : lpath;
item->outputfilename = clonestring(ofilename, strlen(ofilename));
item->max_speed = speed;
curl_free(unescape);
if (!item->outputfilename) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to duplicate output filename");
delete_ditem(item);
return 1;
}
if (!overwritefile)
item->outputfile = outputfile = fopen(item->outputfilename, "rb+");
if (!outputfile)
item->outputfile = outputfile = fopen(item->outputfilename, "wb");
if (!outputfile) {
write_status(A_REVERSE | COLOR_PAIR(1), "Failed to open file: %s", item->outputfilename);
delete_ditem(item);
return 1;
}
rc = curl_easy_setopt(handle, CURLOPT_URL, item->escape_url);
check_erc("url:", rc);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(handle, CURLOPT_PRIVATE, item);
curl_easy_setopt(handle, CURLOPT_XFERINFODATA, item);
curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, progressf);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, outputfile);
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
curl_easy_setopt(handle, CURLOPT_MAX_RECV_SPEED_LARGE, item->max_speed);
if (referer) {
rc = curl_easy_setopt(handle, CURLOPT_REFERER, referer);
check_erc("referer:", rc);
}
if (!overwritefile) {
curl_off_t from;
fseek(outputfile, 0, SEEK_END);
from = item->downloaded = ftell(outputfile);
rc = curl_easy_setopt(handle, CURLOPT_RESUME_FROM_LARGE, from);
check_erc("resume:", rc);
}
item->mode = MODE_PAUSED;
paused_downloads++;
nb_ditems++;
return 0;
}
static void write_downloads()
{
DownloadItem *item = items;
int line, cline = -1, offset;
wmove(downloads, 0, 0);
item = items;
for (line = 0; item; item = item->next) {
double progress = item->progress;
char speedstr[128] = { 0 };
char progstr[8] = { 0 };
int speed = item->speed;
char *namestr = item->outputfilename;
int j, pos = progress * COLS / 100;
int fg = get_fg(item);
int bg = get_bg(item);
int namestrlen, k, l;
int speedstrlen;
int progstrlen;
if (current_mode && item->mode != current_mode) {
continue;
}
if (item == sitem[current_mode]) {
cline = line;
}
snprintf((char *)&progstr, sizeof(progstr), "%3.2f%%", progress);
snprintf((char *)&speedstr, sizeof(speedstr), "%dKB/s", speed / 1024);
namestrlen = strlen(namestr);
speedstrlen = strlen(speedstr);
progstrlen = strlen(progstr);
for (j = 0, k = 0, l = 0; j < COLS; j++) {
if (j < pos)
wattrset(downloads, fg);
else
wattrset(downloads, bg);
if (j < namestrlen)
mvwaddch(downloads, line, j, namestr[j]);
else if (j >= COLS/2 && l < speedstrlen)
mvwaddch(downloads, line, j, speedstr[l++]);
else if (j >= COLS - progstrlen && k < progstrlen)
mvwaddch(downloads, line, j, progstr[k++]);
else
mvwaddch(downloads, line, j, ' ');
}
line++;
}
if (cline >= 0) {
offset = MAX(cline - (LINES - 2), 0);
current_page = cline / (LINES - 1);
} else {
offset = current_page * (LINES - 1);
}
wclrtobot(downloads);
pnoutrefresh(downloads, offset, 0, 0, 0, LINES-1, COLS);
}
static void write_logwin()
{
pnoutrefresh(logwin, MAX(nb_logs - LINES, 0), 0, 0, 0, LINES - 1, COLS);
}
static void write_statuswin(int downloading)
{
wattrset(statuswin, COLOR_PAIR(7));
mvwprintw(statuswin, 0, 0, "[");
wattrset(statuswin, A_BOLD | COLOR_PAIR(4));
waddstr(statuswin, "I");
wattrset(statuswin, COLOR_PAIR(7));
wprintw(statuswin, ":%d ", inactive_downloads);
wattrset(statuswin, COLOR_PAIR(2));
waddstr(statuswin, "P");
wattrset(statuswin, COLOR_PAIR(7));
wprintw(statuswin, ":%d ", paused_downloads);
wattrset(statuswin, A_BOLD | COLOR_PAIR(2));
waddstr(statuswin, "A");
wattrset(statuswin, COLOR_PAIR(7));
wprintw(statuswin, ":%d ", active_downloads);
wattrset(statuswin, A_REVERSE | COLOR_PAIR(2));
waddstr(statuswin, "F");
wattrset(statuswin, COLOR_PAIR(7));
wprintw(statuswin, ":%d ", finished_downloads);
wprintw(statuswin, "N:%d", nb_ditems);
if (start_time != INT_MIN && downloading)
wprintw(statuswin, " T:%ld", time(NULL) - start_time);
wprintw(statuswin, "] ");
wclrtoeol(statuswin);
mvwprintw(statuswin, 0, COLS-12, " Help (F1) ");
wnoutrefresh(statuswin);
}
static void add_handle(DownloadItem *ditem)
{
CURLMcode rc;
curl_off_t from;
int still_running;
fseek(ditem->outputfile, 0, SEEK_END);
from = ditem->downloaded = ftell(ditem->outputfile);
curl_easy_setopt(ditem->handle, CURLOPT_RESUME_FROM_LARGE, from);
ditem->start_time = time(NULL);
ditem->end_time = 0;
rc = curl_multi_add_handle(mhandle, ditem->handle);
check_mrc("add:", rc);
curl_multi_socket_action(mhandle, CURL_SOCKET_TIMEOUT, 0, &still_running);
}
static void remove_handle(DownloadItem *ditem)
{
CURLMcode rc;
rc = curl_multi_remove_handle(mhandle, ditem->handle);
check_mrc("remove:", rc);
ditem->end_time = time(NULL);
}
static void set_timeouts(int check)
{
if (check) {
wtimeout(downloads, 100);
wtimeout(openwin, 100);
} else {
wtimeout(downloads, -1);
wtimeout(openwin, -1);
}
}
static void init_windows()
{
downloads = newpad(4096, COLS);
if (!downloads) {
error(-1, "Failed to create downloads window.\n");
}
logwin = newpad(4096, COLS);
if (!logwin) {
error(-1, "Failed to create log window.\n");
}
statuswin = newwin(1, COLS, LINES-1, 0);
if (!statuswin) {
error(-1, "Failed to create status window.\n");
}
helpwin = newwin(LINES-1, COLS, 0, 0);
if (!statuswin) {
error(-1, "Failed to create help window.\n");
}
infowin = newwin(LINES-1, COLS, 0, 0);
if (!infowin) {
error(-1, "Failed to create info window.\n");
}
openwin = newwin(1, COLS, LINES/2, 0);
if (!openwin) {
error(-1, "Failed to create open window.\n");
}
keypad(downloads, TRUE);
keypad(openwin, TRUE);
leaveok(downloads, TRUE);
leaveok(helpwin, TRUE);
leaveok(infowin, TRUE);
leaveok(logwin, TRUE);
leaveok(openwin, TRUE);
leaveok(statuswin, TRUE);
}
static void auto_startall()
{
DownloadItem *item;
if (!auto_start)
return;
for (item = items; item; item = item->next) {
item->mode = MODE_ACTIVE;
add_handle(item);
active_downloads++;
}
}
static int parse_file(char *filename)
{
FILE *file = fopen(filename, "r");
if (!file)
return -1;
while (fgets(string, MAX_STRING_LEN, file) != NULL) {
size_t len = strlen(string);
if (len > 0 && string[len-1] == '\n')
string[len-1] = '\0';
create_handle(0, string, NULL, NULL, 0);
}
return 0;
}
static int parse_parameters(int argc, char *argv[],
long *max_total_connections,
long *max_host_connections)
{
const char *referer = NULL;
const char *output = NULL;
long max = 0, maxh = 0, speed = 0;
int overwritefile = 0;
int i, param = 0;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-R")) {
param = PARAM_REFERER;
} else if (!strcmp(argv[i], "-o")) {
param = PARAM_OUTPUT;
overwritefile = 0;
} else if (!strcmp(argv[i], "-M")) {
param = PARAM_MAXTCONN;
} else if (!strcmp(argv[i], "-O")) {
param = PARAM_OUTPUTOVER;
overwritefile = 1;
} else if (!strcmp(argv[i], "-i")) {
param = PARAM_INPUTFILE;
} else if (!strcmp(argv[i], "-s")) {
param = PARAM_MAXSPEED;
} else if (!strcmp(argv[i], "-H")) {
param = PARAM_MAXHCONN;
} else if (!strcmp(argv[i], "-X")) {
param = PARAM_AUTOEXIT;
} else if (!strcmp(argv[i], "-x")) {
param = PARAM_AUTOSTART;
} else {
if (param == PARAM_REFERER) {
referer = argv[i];
} else if (param == PARAM_OUTPUT || param == PARAM_OUTPUTOVER) {
output = argv[i];
} else if (param == PARAM_MAXTCONN) {
max = atol(argv[i]);
} else if (param == PARAM_MAXHCONN) {
maxh = atol(argv[i]);
} else if (param == PARAM_INPUTFILE) {
parse_file(argv[i]);
} else if (param == PARAM_MAXSPEED) {
speed = atol(argv[i]);
} else if (param == PARAM_AUTOEXIT) {
auto_exit = !!atol(argv[i]);
} else if (param == PARAM_AUTOSTART) {
auto_start = !!atol(argv[i]);
} else {
create_handle(overwritefile, argv[i], referer, output, speed);
referer = output = NULL;
overwritefile = 0;
speed = 0;
}
param = 0;
}
}
*max_total_connections = max;
*max_host_connections = maxh;
return i;
}
static void check_multi_info()
{
char *eff_url;
CURLMsg *msg;
int msgs_left;
DownloadItem *ditem;
CURL *easy;
while ((msg = curl_multi_info_read(mhandle, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
easy = msg->easy_handle;
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &ditem);
curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
remove_handle(ditem);
ditem->mode = MODE_FINISHED;
ditem->progress = 100.;
finished_downloads++;
active_downloads--;
write_log(COLOR_PAIR(7), "Finished downloading %s.\n", ditem->outputfilename);
}
}
if (auto_exit && (finished_downloads > 0) && (finished_downloads == nb_ditems))
finish(0);
}
static void timer_cb(int fd, short kind, void *userp)
{
CURLMcode rc;
(void)fd;
(void)kind;
(void)userp;
rc = curl_multi_socket_action(mhandle, CURL_SOCKET_TIMEOUT, 0, &still_running);
check_mrc("timer_cb:", rc);
check_multi_info();
downloading = still_running > 0;
}
static int multi_timer_cb(CURLM *multi, long timeout_ms, void *unused)
{
(void)multi;
(void)unused;
if (timeout_ms > 0) {
struct timeval timeout;
timeout.tv_sec = timeout_ms/1000;
timeout.tv_usec = (timeout_ms%1000)*1000;
evtimer_add(timerevent, &timeout);
} else if (timeout_ms == 0) {
timer_cb(0, 0, NULL);
}
return 0;
}
static void remove_sock(SockInfo *f)
{
if (f) {
if (f->evset)
event_free(f->ev);
free(f);
}
}
static void event_cb(int fd, short kind, void *userp)
{
CURLMcode rc;
(void)userp;
int action = (kind & EV_READ ? CURL_CSELECT_IN : 0) | (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
rc = curl_multi_socket_action(mhandle, fd, action, &still_running);
check_mrc("event_cb:", rc);
check_multi_info();
downloading = still_running > 0;
if (still_running <= 0) {
if (evtimer_pending(timerevent, NULL)) {
evtimer_del(timerevent);
}
}
}
static void set_sock(SockInfo *f, curl_socket_t s, CURL *e, int act)
{
int kind = (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
f->sockfd = s;
f->action = act;
f->easy = e;
if (f->evset)
event_free(f->ev);