-
Notifications
You must be signed in to change notification settings - Fork 189
/
ncurses-interface.c
1335 lines (1041 loc) · 37 KB
/
ncurses-interface.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
/* FDUPES Copyright (c) 2018-2022 Adrian Lopez
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#ifdef HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#else
#include <curses.h>
#endif
#include "ncurses-interface.h"
#include "ncurses-getcommand.h"
#include "ncurses-commands.h"
#include "ncurses-prompt.h"
#include "ncurses-status.h"
#include "ncurses-print.h"
#include "fileaction.h"
#include "mbstowcs_escape_invalid.h"
#include "positive_wcwidth.h"
#include "commandidentifier.h"
#include "filegroup.h"
#include "errormsg.h"
#include "log.h"
#include "sigint.h"
#include "flags.h"
char *fmttime(time_t t);
enum linestyle
{
linestyle_groupheader = 0,
linestyle_groupheaderspacing,
linestyle_filename,
linestyle_groupfooterspacing
};
enum linestyle getlinestyle(struct filegroup *group, int line)
{
if (line <= group->startline)
return linestyle_groupheader;
else if (line == group->startline + 1)
return linestyle_groupheaderspacing;
else if (line >= group->endline)
return linestyle_groupfooterspacing;
else
return linestyle_filename;
}
#define FILENAME_INDENT_EXTRA 5
#define FILE_INDEX_MIN_WIDTH 3
int filerowcount(file_t *file, const int columns, int group_file_count)
{
int lines;
int line_remaining;
size_t x = 0;
size_t read;
size_t filename_bytes;
wchar_t ch;
mbstate_t mbstate;
int index_width;
int timestamp_width;
size_t needed;
wchar_t *wcfilename;
memset(&mbstate, 0, sizeof(mbstate));
needed = mbstowcs_escape_invalid(0, file->d_name, 0);
wcfilename = (wchar_t*)malloc(sizeof(wchar_t) * needed);
if (wcfilename == 0)
return 0;
mbstowcs_escape_invalid(wcfilename, file->d_name, needed);
index_width = get_num_digits(group_file_count);
if (index_width < FILE_INDEX_MIN_WIDTH)
index_width = FILE_INDEX_MIN_WIDTH;
timestamp_width = ISFLAG(flags, F_SHOWTIME) ? 19 : 0;
lines = (index_width + timestamp_width + FILENAME_INDENT_EXTRA) / columns + 1;
line_remaining = columns - (index_width + timestamp_width + FILENAME_INDENT_EXTRA) % columns;
while (wcfilename[x] != L'\0')
{
if (positive_wcwidth(wcfilename[x]) <= line_remaining)
{
line_remaining -= positive_wcwidth(wcfilename[x]);
}
else
{
line_remaining = columns - positive_wcwidth(wcfilename[x]);
++lines;
}
++x;
}
free(wcfilename);
return lines;
}
int getgroupindex(struct filegroup *groups, int group_count, int group_hint, int line)
{
int group = group_hint;
while (group > 0 && line < groups[group].startline)
--group;
while (group < group_count && line > groups[group].endline)
++group;
return group;
}
int getgroupfileindex(int *row, struct filegroup *group, int line, int columns)
{
int l;
int f = 0;
int rowcount;
l = group->startline + 2;
while (f < group->filecount)
{
rowcount = filerowcount(group->files[f].file, columns, group->filecount);
if (line <= l + rowcount - 1)
{
*row = line - l;
return f;
}
l += rowcount;
++f;
}
return -1;
}
int getgroupfileline(struct filegroup *group, int fileindex, int columns)
{
int l;
int f = 0;
int rowcount;
l = group->startline + 2;
while (f < fileindex && f < group->filecount)
{
rowcount = filerowcount(group->files[f].file, columns, group->filecount);
l += rowcount;
++f;
}
return l;
}
void set_file_action(struct groupfile *file, int new_action, size_t *deletion_tally)
{
switch (file->action)
{
case FILEACTION_DELETE:
if (new_action != FILEACTION_DELETE)
--*deletion_tally;
break;
default:
if (new_action == FILEACTION_DELETE)
++*deletion_tally;
break;
}
file->action = new_action;
}
void scroll_to_group(int *topline, int group, int tail, struct filegroup *groups, WINDOW *filewin)
{
if (*topline < groups[group].startline)
{
if (groups[group].endline >= *topline + getmaxy(filewin))
{
if (groups[group].endline - groups[group].startline < getmaxy(filewin))
*topline = groups[group].endline - getmaxy(filewin) + 1;
else
*topline = groups[group].startline;
}
}
else
{
if (groups[group].endline - groups[group].startline < getmaxy(filewin) || !tail)
*topline = groups[group].startline;
else
*topline = groups[group].endline - getmaxy(filewin);
}
}
void move_to_next_group(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, WINDOW *filewin)
{
*cursorgroup += 1;
*cursorfile = 0;
scroll_to_group(topline, *cursorgroup, 0, groups, filewin);
}
int move_to_next_selected_group(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, int totalgroups, WINDOW *filewin)
{
size_t g;
for (g = *cursorgroup + 1; g < totalgroups; ++g)
{
if (groups[g].selected)
{
*cursorgroup = g;
*cursorfile = 0;
scroll_to_group(topline, *cursorgroup, 0, groups, filewin);
return 1;
}
}
return 0;
}
void move_to_next_file(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, WINDOW *filewin)
{
*cursorfile += 1;
if (getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS) >= *topline + getmaxy(filewin))
{
if (groups[*cursorgroup].endline - getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS) < getmaxy(filewin))
*topline = groups[*cursorgroup].endline - getmaxy(filewin) + 1;
else
*topline = getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS);
}
}
void move_to_previous_group(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, WINDOW *filewin)
{
*cursorgroup -= 1;
*cursorfile = groups[*cursorgroup].filecount - 1;
scroll_to_group(topline, *cursorgroup, 1, groups, filewin);
}
int move_to_previous_selected_group(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, int totalgroups, WINDOW *filewin)
{
size_t g;
for (g = *cursorgroup; g > 0; --g)
{
if (groups[g - 1].selected)
{
*cursorgroup = g - 1;
*cursorfile = 0;
scroll_to_group(topline, *cursorgroup, 0, groups, filewin);
return 1;
}
}
return 0;
}
void move_to_previous_file(int *topline, int *cursorgroup, int *cursorfile, struct filegroup *groups, WINDOW *filewin)
{
*cursorfile -= 1;
if (getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS) < *topline)
{
if (getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS) - groups[*cursorgroup].startline < getmaxy(filewin))
*topline -= getgroupfileline(&groups[*cursorgroup], *cursorfile, COLS) - groups[*cursorgroup].startline + 1;
else
*topline -= getmaxy(filewin);
}
}
#define FILE_LIST_OK 1
#define FILE_LIST_ERROR_INDEX_OUT_OF_RANGE -1
#define FILE_LIST_ERROR_LIST_CONTAINS_INVALID_INDEX -2
#define FILE_LIST_ERROR_UNKNOWN_COMMAND -3
#define FILE_LIST_ERROR_OUT_OF_MEMORY -4
int validate_file_list(struct filegroup *currentgroup, wchar_t *commandbuffer_in)
{
wchar_t *commandbuffer;
wchar_t *token;
wchar_t *wcsptr;
wchar_t *wcstolcheck;
long int number;
int parts = 0;
int parse_error = 0;
int out_of_bounds_error = 0;
commandbuffer = malloc(sizeof(wchar_t) * (wcslen(commandbuffer_in)+1));
if (commandbuffer == 0)
return FILE_LIST_ERROR_OUT_OF_MEMORY;
wcscpy(commandbuffer, commandbuffer_in);
token = wcstok(commandbuffer, L",", &wcsptr);
while (token != NULL)
{
++parts;
number = wcstol(token, &wcstolcheck, 10);
if (wcstolcheck == token || *wcstolcheck != '\0')
parse_error = 1;
if (number > currentgroup->filecount || number < 1)
out_of_bounds_error = 1;
token = wcstok(NULL, L",", &wcsptr);
}
free(commandbuffer);
if (parts == 1 && parse_error)
return FILE_LIST_ERROR_UNKNOWN_COMMAND;
else if (parse_error)
return FILE_LIST_ERROR_LIST_CONTAINS_INVALID_INDEX;
else if (out_of_bounds_error)
return FILE_LIST_ERROR_INDEX_OUT_OF_RANGE;
return FILE_LIST_OK;
}
void deletefiles_ncurses(file_t *files, char *logfile)
{
WINDOW *filewin;
WINDOW *promptwin;
WINDOW *statuswin;
file_t *curfile;
file_t *dupefile;
struct filegroup *groups;
struct filegroup *reallocgroups;
size_t groupfilecount;
int topline = 0;
int cursorgroup = 0;
int cursorfile = 0;
int cursor_x;
int cursor_y;
int groupfirstline = 0;
int totallines = 0;
int allocatedgroups = 0;
int totalgroups = 0;
size_t groupindex = 0;
enum linestyle linestyle;
int preservecount;
int deletecount;
int unresolvedcount;
size_t globaldeletiontally = 0;
int row;
int x;
int g;
wint_t wch;
int keyresult;
int cy;
int f;
wchar_t *commandbuffer;
size_t commandbuffersize;
wchar_t *commandarguments;
struct command_identifier_node *commandidentifier;
struct command_identifier_node *confirmationkeywordidentifier;
int doprune;
wchar_t *token;
wchar_t *wcsptr;
wchar_t *wcstolcheck;
long int number;
struct status_text *status;
struct prompt_info *prompt;
int dupesfound;
int intresult;
int resumecommandinput = 0;
int index_width;
int timestamp_width;
noecho();
cbreak();
halfdelay(5);
filewin = newwin(LINES - 2, COLS, 0, 0);
statuswin = newwin(1, COLS, LINES - 1, 0);
promptwin = newwin(1, COLS, LINES - 2, 0);
scrollok(filewin, FALSE);
scrollok(statuswin, FALSE);
scrollok(promptwin, FALSE);
wattron(statuswin, A_REVERSE);
keypad(promptwin, 1);
commandbuffersize = 80;
commandbuffer = malloc(commandbuffersize * sizeof(wchar_t));
if (commandbuffer == 0)
{
endwin();
errormsg("out of memory\n");
exit(1);
}
allocatedgroups = 1024;
groups = malloc(sizeof(struct filegroup) * allocatedgroups);
if (groups == 0)
{
free(commandbuffer);
endwin();
errormsg("out of memory\n");
exit(1);
}
commandidentifier = build_command_identifier_tree(command_list);
if (commandidentifier == 0)
{
free(groups);
free(commandbuffer);
endwin();
errormsg("out of memory\n");
exit(1);
}
confirmationkeywordidentifier = build_command_identifier_tree(confirmation_keyword_list);
if (confirmationkeywordidentifier == 0)
{
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
}
curfile = files;
while (curfile)
{
if (!curfile->hasdupes)
{
curfile = curfile->next;
continue;
}
if (totalgroups + 1 > allocatedgroups)
{
allocatedgroups *= 2;
reallocgroups = realloc(groups, sizeof(struct filegroup) * allocatedgroups);
if (reallocgroups == 0)
{
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
free_command_identifier_tree(confirmationkeywordidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
}
groups = reallocgroups;
}
groups[totalgroups].startline = groupfirstline;
groups[totalgroups].endline = groupfirstline + 2;
groups[totalgroups].selected = 0;
groupfilecount = 0;
dupefile = curfile;
do
{
++groupfilecount;
dupefile = dupefile->duplicates;
} while(dupefile);
dupefile = curfile;
do
{
groups[totalgroups].endline += filerowcount(dupefile, COLS, groupfilecount);
dupefile = dupefile->duplicates;
} while (dupefile);
groups[totalgroups].files = malloc(sizeof(struct groupfile) * groupfilecount);
if (groups[totalgroups].files == 0)
{
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
free_command_identifier_tree(confirmationkeywordidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
}
groupfilecount = 0;
dupefile = curfile;
do
{
groups[totalgroups].files[groupfilecount].file = dupefile;
groups[totalgroups].files[groupfilecount].action = FILEACTION_UNRESOLVED;
groups[totalgroups].files[groupfilecount].selected = 0;
++groupfilecount;
dupefile = dupefile->duplicates;
} while (dupefile);
groups[totalgroups].filecount = groupfilecount;
groupfirstline = groups[totalgroups].endline + 1;
++totalgroups;
curfile = curfile->next;
}
dupesfound = totalgroups > 0;
status = status_text_alloc(0, COLS);
if (status == 0)
{
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
free_command_identifier_tree(confirmationkeywordidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
}
format_status_left(status, L"Ready");
prompt = prompt_info_alloc(80);
if (prompt == 0)
{
free_status_text(status);
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
free_command_identifier_tree(confirmationkeywordidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
}
doprune = 1;
do
{
wmove(filewin, 0, 0);
werase(filewin);
if (totalgroups > 0)
totallines = groups[totalgroups-1].endline;
else
totallines = 0;
for (x = topline; x < topline + getmaxy(filewin); ++x)
{
if (x >= totallines)
{
wclrtoeol(filewin);
continue;
}
groupindex = getgroupindex(groups, totalgroups, groupindex, x);
index_width = get_num_digits(groups[groupindex].filecount);
if (index_width < FILE_INDEX_MIN_WIDTH)
index_width = FILE_INDEX_MIN_WIDTH;
timestamp_width = ISFLAG(flags, F_SHOWTIME) ? 19 : 0;
linestyle = getlinestyle(groups + groupindex, x);
if (linestyle == linestyle_groupheader)
{
wattron(filewin, A_BOLD);
if (groups[groupindex].selected)
wattron(filewin, A_REVERSE);
wprintw(filewin, "Set %d of %d:\n", groupindex + 1, totalgroups);
if (groups[groupindex].selected)
wattroff(filewin, A_REVERSE);
wattroff(filewin, A_BOLD);
}
else if (linestyle == linestyle_groupheaderspacing)
{
wprintw(filewin, "\n");
}
else if (linestyle == linestyle_filename)
{
f = getgroupfileindex(&row, groups + groupindex, x, COLS);
if (cursorgroup != groupindex)
{
if (row == 0)
{
print_spaces(filewin, index_width);
wprintw(filewin, " [%c] ", getfileactionchar(groups[groupindex].files[f].action));
if (ISFLAG(flags, F_SHOWTIME))
wprintw(filewin, "[%s] ", fmttime(groups[groupindex].files[f].file->mtime));
}
cy = getcury(filewin);
if (groups[groupindex].files[f].selected)
wattron(filewin, A_REVERSE);
putline(filewin, groups[groupindex].files[f].file->d_name, row, COLS, index_width + timestamp_width + FILENAME_INDENT_EXTRA);
if (groups[groupindex].files[f].selected)
wattroff(filewin, A_REVERSE);
wclrtoeol(filewin);
wmove(filewin, cy+1, 0);
}
else
{
if (row == 0)
{
print_right_justified_int(filewin, f+1, index_width);
wprintw(filewin, " ");
if (cursorgroup == groupindex && cursorfile == f)
wattron(filewin, A_REVERSE);
wprintw(filewin, "[%c]", getfileactionchar(groups[groupindex].files[f].action));
if (cursorgroup == groupindex && cursorfile == f)
wattroff(filewin, A_REVERSE);
wprintw(filewin, " ");
if (ISFLAG(flags, F_SHOWTIME))
wprintw(filewin, "[%s] ", fmttime(groups[groupindex].files[f].file->mtime));
}
cy = getcury(filewin);
if (groups[groupindex].files[f].selected)
wattron(filewin, A_REVERSE);
putline(filewin, groups[groupindex].files[f].file->d_name, row, COLS, index_width + timestamp_width + FILENAME_INDENT_EXTRA);
if (groups[groupindex].files[f].selected)
wattroff(filewin, A_REVERSE);
wclrtoeol(filewin);
wmove(filewin, cy+1, 0);
}
}
else if (linestyle == linestyle_groupfooterspacing)
{
wprintw(filewin, "\n");
}
}
if (totalgroups > 0)
format_status_right(status, L"Set %d of %d", cursorgroup+1, totalgroups);
else
format_status_right(status, L"Finished");
print_status(statuswin, status);
if (totalgroups > 0)
format_prompt(prompt, L"( Preserve files [1 - %d, all, help] )", groups[cursorgroup].filecount);
else if (dupesfound)
format_prompt(prompt, L"( No duplicates remaining; type 'exit' to exit program )");
else
format_prompt(prompt, L"( No duplicates found; type 'exit' to exit program )");
print_prompt(promptwin, prompt, L"");
/* refresh windows (using wrefresh instead of wnoutrefresh to avoid bug in gnome-terminal) */
wrefresh(filewin);
wrefresh(statuswin);
wrefresh(promptwin);
/* wait for user input */
if (!resumecommandinput)
{
do
{
keyresult = wget_wch(promptwin, &wch);
if (got_sigint)
{
getyx(promptwin, cursor_y, cursor_x);
format_status_left(status, L"Type 'exit' to exit fdupes.");
print_status(statuswin, status);
wmove(promptwin, cursor_y, cursor_x);
got_sigint = 0;
wrefresh(statuswin);
}
} while (keyresult == ERR);
if (keyresult == OK && iswprint(wch))
{
commandbuffer[0] = wch;
commandbuffer[1] = '\0';
}
else
{
commandbuffer[0] = '\0';
}
}
if (resumecommandinput || (keyresult == OK && iswprint(wch) && ((wch != '\t' && wch != '\n' && wch != '?'))))
{
resumecommandinput = 0;
switch (get_command_text(&commandbuffer, &commandbuffersize, promptwin, prompt, 1, 1))
{
case GET_COMMAND_OK:
format_status_left(status, L"Ready");
get_command_arguments(&commandarguments, commandbuffer);
switch (identify_command(commandidentifier, commandbuffer, 0))
{
case COMMAND_SELECT_CONTAINING:
cmd_select_containing(groups, totalgroups, commandarguments, status);
break;
case COMMAND_SELECT_BEGINNING:
cmd_select_beginning(groups, totalgroups, commandarguments, status);
break;
case COMMAND_SELECT_ENDING:
cmd_select_ending(groups, totalgroups, commandarguments, status);
break;
case COMMAND_SELECT_MATCHING:
cmd_select_matching(groups, totalgroups, commandarguments, status);
break;
case COMMAND_SELECT_REGEX:
cmd_select_regex(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_SELECTIONS_CONTAINING:
cmd_clear_selections_containing(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_SELECTIONS_BEGINNING:
cmd_clear_selections_beginning(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_SELECTIONS_ENDING:
cmd_clear_selections_ending(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_SELECTIONS_MATCHING:
cmd_clear_selections_matching(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_SELECTIONS_REGEX:
cmd_clear_selections_regex(groups, totalgroups, commandarguments, status);
break;
case COMMAND_CLEAR_ALL_SELECTIONS:
cmd_clear_all_selections(groups, totalgroups, commandarguments, status);
break;
case COMMAND_INVERT_GROUP_SELECTIONS:
cmd_invert_group_selections(groups, totalgroups, commandarguments, status);
break;
case COMMAND_KEEP_SELECTED:
cmd_keep_selected(groups, totalgroups, commandarguments, &globaldeletiontally, status);
break;
case COMMAND_DELETE_SELECTED:
cmd_delete_selected(groups, totalgroups, commandarguments, &globaldeletiontally, status);
break;
case COMMAND_RESET_SELECTED:
cmd_reset_selected(groups, totalgroups, commandarguments, &globaldeletiontally, status);
break;
case COMMAND_RESET_GROUP:
for (x = 0; x < groups[cursorgroup].filecount; ++x)
set_file_action(&groups[cursorgroup].files[x], FILEACTION_UNRESOLVED, &globaldeletiontally);
format_status_left(status, L"Reset all files in current group.");
break;
case COMMAND_PRESERVE_ALL:
/* mark all files for preservation */
for (x = 0; x < groups[cursorgroup].filecount; ++x)
set_file_action(&groups[cursorgroup].files[x], FILEACTION_KEEP, &globaldeletiontally);
format_status_left(status, L"%d files marked for preservation", groups[cursorgroup].filecount);
if (cursorgroup < totalgroups - 1)
move_to_next_group(&topline, &cursorgroup, &cursorfile, groups, filewin);
break;
case COMMAND_GOTO_SET:
number = wcstol(commandarguments, &wcstolcheck, 10);
if (wcstolcheck != commandarguments && *wcstolcheck == '\0')
{
if (number >= 1 && number <= totalgroups)
{
scroll_to_group(&topline, number - 1, 0, groups, filewin);
cursorgroup = number - 1;
cursorfile = 0;
}
else
{
format_status_left(status, L"Group index out of range.");
}
}
else
{
format_status_left(status, L"Invalid group index.");
}
break;
case COMMAND_HELP:
endwin();
if (system(HELP_COMMAND_STRING) == -1)
format_status_left(status, L"Could not display help text.");
refresh();
break;
case COMMAND_PRUNE:
cmd_prune(groups, totalgroups, commandarguments, &globaldeletiontally, &totalgroups, &cursorgroup, &cursorfile, &topline, logfile, filewin, statuswin, status);
break;
case COMMAND_EXIT: /* exit program */
if (totalgroups == 0)
{
doprune = 0;
continue;
}
else
{
if (globaldeletiontally != 0)
format_prompt(prompt, L"( There are files marked for deletion. Exit without deleting? )");
else
format_prompt(prompt, L"( There are duplicates remaining. Exit anyway? )");
print_prompt(promptwin, prompt, L"");
wrefresh(promptwin);
switch (get_command_text(&commandbuffer, &commandbuffersize, promptwin, prompt, 0, 0))
{
case GET_COMMAND_OK:
switch (identify_command(confirmationkeywordidentifier, commandbuffer, 0))
{
case COMMAND_YES:
doprune = 0;
continue;
case COMMAND_NO:
case COMMAND_UNDEFINED:
commandbuffer[0] = '\0';
continue;
}
break;
case GET_COMMAND_CANCELED:
commandbuffer[0] = '\0';
continue;
case GET_COMMAND_RESIZE_REQUESTED:
/* resize windows */
wresize(filewin, LINES - 2, COLS);
wresize(statuswin, 1, COLS);
wresize(promptwin, 1, COLS);
mvwin(statuswin, LINES - 1, 0);
mvwin(promptwin, LINES - 2, 0);
status_text_alloc(status, COLS);
/* recalculate line boundaries */
groupfirstline = 0;
for (g = 0; g < totalgroups; ++g)
{
groups[g].startline = groupfirstline;
groups[g].endline = groupfirstline + 2;
for (f = 0; f < groups[g].filecount; ++f)
groups[g].endline += filerowcount(groups[g].files[f].file, COLS, groups[g].filecount);
groupfirstline = groups[g].endline + 1;
}
commandbuffer[0] = '\0';
break;
case GET_COMMAND_ERROR_OUT_OF_MEMORY:
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
free_command_identifier_tree(confirmationkeywordidentifier);
endwin();
errormsg("out of memory\n");
exit(1);
break;
}
}
break;
default: /* parse list of files to preserve and mark for preservation */
intresult = validate_file_list(groups + cursorgroup, commandbuffer);
if (intresult != FILE_LIST_OK)
{
if (intresult == FILE_LIST_ERROR_UNKNOWN_COMMAND)
{
format_status_left(status, L"Unrecognized command");
break;
}
else if (intresult == FILE_LIST_ERROR_INDEX_OUT_OF_RANGE)
{
format_status_left(status, L"Index out of range (1 - %d).", groups[cursorgroup].filecount);
break;
}
else if (intresult == FILE_LIST_ERROR_LIST_CONTAINS_INVALID_INDEX)
{
format_status_left(status, L"Invalid index");
break;
}
else if (intresult == FILE_LIST_ERROR_OUT_OF_MEMORY)
{
free(commandbuffer);
free_command_identifier_tree(commandidentifier);
for (g = 0; g < totalgroups; ++g)
free(groups[g].files);
free(groups);
endwin();