-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrover.c
1502 lines (1418 loc) · 45.5 KB
/
rover.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
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#define _XOPEN_SOURCE_EXTENDED
#define _FILE_OFFSET_BITS 64
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include <string.h>
#include <sys/types.h> /* pid_t, ... */
#include <stdio.h>
#include <limits.h> /* PATH_MAX */
#include <locale.h> /* setlocale(), LC_ALL */
#include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
#include <dirent.h> /* DIR, struct dirent, opendir(), ... */
#include <libgen.h>
#include <sys/stat.h>
#include <fcntl.h> /* open() */
#include <sys/wait.h> /* waitpid() */
#include <signal.h> /* struct sigaction, sigaction() */
#include <errno.h>
#include <stdarg.h>
#include <curses.h>
#include "config.h"
/* This signal is not defined by POSIX, but should be
present on all systems that have resizable terminals. */
#ifndef SIGWINCH
#define SIGWINCH 28
#endif
/* String buffers. */
#define BUFLEN PATH_MAX
static char BUF1[BUFLEN];
static char BUF2[BUFLEN];
static char INPUT[BUFLEN];
static char CLIPBOARD[BUFLEN];
static wchar_t WBUF[BUFLEN];
/* Paths to external programs. */
static char *user_shell;
static char *user_pager;
static char *user_editor;
static char *user_open;
/* Listing view parameters. */
#define HEIGHT (LINES-4)
#define STATUSPOS (COLS-16)
/* Listing view flags. */
#define SHOW_FILES 0x01u
#define SHOW_DIRS 0x02u
#define SHOW_HIDDEN 0x04u
/* Marks parameters. */
#define BULK_INIT 5
#define BULK_THRESH 256
/* Information associated to each entry in listing. */
typedef struct Row {
char *name;
off_t size;
mode_t mode;
int islink;
int marked;
} Row;
/* Dynamic array of marked entries. */
typedef struct Marks {
char dirpath[PATH_MAX];
int bulk;
int nentries;
char **entries;
} Marks;
/* Line editing state. */
typedef struct Edit {
wchar_t buffer[BUFLEN+1];
int left, right;
} Edit;
/* Each tab only stores the following information. */
typedef struct Tab {
int scroll;
int esel;
uint8_t flags;
char cwd[PATH_MAX];
} Tab;
typedef struct Prog {
off_t partial;
off_t total;
const char *msg;
} Prog;
/* Global state. */
static struct Rover {
int tab;
int nfiles;
Row *rows;
WINDOW *window;
Marks marks;
Edit edit;
int edit_scroll;
volatile sig_atomic_t pending_usr1;
volatile sig_atomic_t pending_winch;
Prog prog;
Tab tabs[10];
} rover;
/* Macros for accessing global state. */
#define ENAME(I) rover.rows[I].name
#define ESIZE(I) rover.rows[I].size
#define EMODE(I) rover.rows[I].mode
#define ISLINK(I) rover.rows[I].islink
#define MARKED(I) rover.rows[I].marked
#define SCROLL rover.tabs[rover.tab].scroll
#define ESEL rover.tabs[rover.tab].esel
#define FLAGS rover.tabs[rover.tab].flags
#define CWD rover.tabs[rover.tab].cwd
/* Helpers. */
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define ISDIR(E) (strchr((E), '/') != NULL)
/* Line Editing Macros. */
#define EDIT_FULL(E) ((E).left == (E).right)
#define EDIT_CAN_LEFT(E) ((E).left)
#define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
#define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
#define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
#define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
#define EDIT_BACKSPACE(E) (E).left--
#define EDIT_DELETE(E) (E).right++
#define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
typedef enum EditStat {CONTINUE, CONFIRM, CANCEL} EditStat;
typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK} Color;
typedef int (*PROCESS)(const char *path);
static void
init_marks(Marks *marks)
{
strcpy(marks->dirpath, "");
marks->bulk = BULK_INIT;
marks->nentries = 0;
marks->entries = calloc(marks->bulk, sizeof *marks->entries);
}
/* Unmark all entries. */
static void
mark_none(Marks *marks)
{
int i;
strcpy(marks->dirpath, "");
for (i = 0; i < marks->bulk && marks->nentries; i++)
if (marks->entries[i]) {
free(marks->entries[i]);
marks->entries[i] = NULL;
marks->nentries--;
}
if (marks->bulk > BULK_THRESH) {
/* Reset bulk to free some memory. */
free(marks->entries);
marks->bulk = BULK_INIT;
marks->entries = calloc(marks->bulk, sizeof *marks->entries);
}
}
static void
add_mark(Marks *marks, char *dirpath, char *entry)
{
int i;
if (!strcmp(marks->dirpath, dirpath)) {
/* Append mark to directory. */
if (marks->nentries == marks->bulk) {
/* Expand bulk to accomodate new entry. */
int extra = marks->bulk / 2;
marks->bulk += extra; /* bulk *= 1.5; */
marks->entries = realloc(marks->entries,
marks->bulk * sizeof *marks->entries);
memset(&marks->entries[marks->nentries], 0,
extra * sizeof *marks->entries);
i = marks->nentries;
} else {
/* Search for empty slot (there must be one). */
for (i = 0; i < marks->bulk; i++)
if (!marks->entries[i])
break;
}
} else {
/* Directory changed. Discard old marks. */
mark_none(marks);
strcpy(marks->dirpath, dirpath);
i = 0;
}
marks->entries[i] = malloc(strlen(entry) + 1);
strcpy(marks->entries[i], entry);
marks->nentries++;
}
static void
del_mark(Marks *marks, char *entry)
{
int i;
if (marks->nentries > 1) {
for (i = 0; i < marks->bulk; i++)
if (marks->entries[i] && !strcmp(marks->entries[i], entry))
break;
free(marks->entries[i]);
marks->entries[i] = NULL;
marks->nentries--;
} else
mark_none(marks);
}
static void
free_marks(Marks *marks)
{
int i;
for (i = 0; i < marks->bulk && marks->nentries; i++)
if (marks->entries[i]) {
free(marks->entries[i]);
marks->nentries--;
}
free(marks->entries);
}
static void
handle_usr1(int sig)
{
rover.pending_usr1 = 1;
}
static void
handle_winch(int sig)
{
rover.pending_winch = 1;
}
static void
enable_handlers()
{
struct sigaction sa;
memset(&sa, 0, sizeof (struct sigaction));
sa.sa_handler = handle_usr1;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = handle_winch;
sigaction(SIGWINCH, &sa, NULL);
}
static void
disable_handlers()
{
struct sigaction sa;
memset(&sa, 0, sizeof (struct sigaction));
sa.sa_handler = SIG_DFL;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGWINCH, &sa, NULL);
}
static void reload();
static void update_view();
/* Handle any signals received since last call. */
static void
sync_signals()
{
if (rover.pending_usr1) {
/* SIGUSR1 received: refresh directory listing. */
reload();
rover.pending_usr1 = 0;
}
if (rover.pending_winch) {
/* SIGWINCH received: resize application accordingly. */
delwin(rover.window);
endwin();
refresh();
clear();
rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
if (HEIGHT < rover.nfiles && SCROLL + HEIGHT > rover.nfiles)
SCROLL = ESEL - HEIGHT;
update_view();
rover.pending_winch = 0;
}
}
/* This function must be used in place of getch().
It handles signals while waiting for user input. */
static int
rover_getch()
{
int ch;
while ((ch = getch()) == ERR)
sync_signals();
return ch;
}
/* This function must be used in place of get_wch().
It handles signals while waiting for user input. */
static int
rover_get_wch(wint_t *wch)
{
wint_t ret;
while ((ret = get_wch(wch)) == (wint_t) ERR)
sync_signals();
return ret;
}
/* Get user programs from the environment. */
#define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
dst = getenv(#src);
static void
get_user_programs()
{
ROVER_ENV(user_shell, SHELL)
ROVER_ENV(user_pager, PAGER)
ROVER_ENV(user_editor, VISUAL)
if (!user_editor)
ROVER_ENV(user_editor, EDITOR)
ROVER_ENV(user_open, OPEN)
}
/* Do a fork-exec to external program (e.g. $EDITOR). */
static void
spawn(char **args)
{
pid_t pid;
int status;
setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
pid = fork();
if (pid > 0) {
/* fork() succeeded. */
disable_handlers();
endwin();
waitpid(pid, &status, 0);
enable_handlers();
kill(getpid(), SIGWINCH);
} else if (pid == 0) {
/* Child process. */
execvp(args[0], args);
}
}
static void
shell_escaped_cat(char *buf, char *str, size_t n)
{
char *p = buf + strlen(buf);
*p++ = '\'';
for (n--; n; n--, str++) {
switch (*str) {
case '\'':
if (n < 4)
goto done;
strcpy(p, "'\\''");
n -= 4;
p += 4;
break;
case '\0':
goto done;
default:
*p = *str;
p++;
}
}
done:
strncat(p, "'", n);
}
static int
open_with_env(char *program, char *path)
{
if (program) {
#ifdef RV_SHELL
strncpy(BUF1, program, BUFLEN - 1);
strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
#else
spawn((char *[]) {program, path, NULL});
#endif
return 1;
}
return 0;
}
/* Curses setup. */
static void
init_term()
{
setlocale(LC_ALL, "");
initscr();
cbreak(); /* Get one character at a time. */
timeout(100); /* For getch(). */
noecho();
nonl(); /* No NL->CR/NL on output. */
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
curs_set(FALSE); /* Hide blinking cursor. */
if (has_colors()) {
short bg;
start_color();
#ifdef NCURSES_EXT_FUNCS
use_default_colors();
bg = -1;
#else
bg = COLOR_BLACK;
#endif
init_pair(RED, COLOR_RED, bg);
init_pair(GREEN, COLOR_GREEN, bg);
init_pair(YELLOW, COLOR_YELLOW, bg);
init_pair(BLUE, COLOR_BLUE, bg);
init_pair(CYAN, COLOR_CYAN, bg);
init_pair(MAGENTA, COLOR_MAGENTA, bg);
init_pair(WHITE, COLOR_WHITE, bg);
init_pair(BLACK, COLOR_BLACK, bg);
}
atexit((void (*)(void)) endwin);
enable_handlers();
}
/* Update the listing view. */
static void
update_view()
{
int i, j;
int numsize;
int ishidden;
int marking;
mvhline(0, 0, ' ', COLS);
attr_on(A_BOLD, NULL);
color_set(RVC_TABNUM, NULL);
mvaddch(0, COLS - 2, rover.tab + '0');
attr_off(A_BOLD, NULL);
if (rover.marks.nentries) {
numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
color_set(RVC_MARKS, NULL);
mvaddstr(0, COLS - 3 - numsize, BUF1);
} else
numsize = -1;
color_set(RVC_CWD, NULL);
mbstowcs(WBUF, CWD, PATH_MAX);
mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
wcolor_set(rover.window, RVC_BORDER, NULL);
wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
/* Selection might not be visible, due to cursor wrapping or window
shrinking. In that case, the scroll must be moved to make it visible. */
if (rover.nfiles > HEIGHT) {
SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
} else
SCROLL = 0;
marking = !strcmp(CWD, rover.marks.dirpath);
for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
ishidden = ENAME(j)[0] == '.';
if (j == ESEL)
wattr_on(rover.window, A_REVERSE, NULL);
if (ISLINK(j))
wcolor_set(rover.window, RVC_LINK, NULL);
else if (ishidden)
wcolor_set(rover.window, RVC_HIDDEN, NULL);
else if (S_ISREG(EMODE(j))) {
if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
wcolor_set(rover.window, RVC_EXEC, NULL);
else
wcolor_set(rover.window, RVC_REG, NULL);
} else if (S_ISDIR(EMODE(j)))
wcolor_set(rover.window, RVC_DIR, NULL);
else if (S_ISCHR(EMODE(j)))
wcolor_set(rover.window, RVC_CHR, NULL);
else if (S_ISBLK(EMODE(j)))
wcolor_set(rover.window, RVC_BLK, NULL);
else if (S_ISFIFO(EMODE(j)))
wcolor_set(rover.window, RVC_FIFO, NULL);
else if (S_ISSOCK(EMODE(j)))
wcolor_set(rover.window, RVC_SOCK, NULL);
if (S_ISDIR(EMODE(j))) {
mbstowcs(WBUF, ENAME(j), PATH_MAX);
if (ISLINK(j))
wcscat(WBUF, L"/");
} else {
char *suffix, *suffixes = "BKMGTPEZY";
off_t human_size = ESIZE(j) * 10;
int length = mbstowcs(WBUF, ENAME(j), PATH_MAX);
int namecols = wcswidth(WBUF, length);
for (suffix = suffixes; human_size >= 10240; suffix++)
human_size = (human_size + 512) / 1024;
if (*suffix == 'B')
swprintf(WBUF + length, PATH_MAX - length, L"%*d %c",
(int) (COLS - namecols - 6),
(int) human_size / 10, *suffix);
else
swprintf(WBUF + length, PATH_MAX - length, L"%*d.%d %c",
(int) (COLS - namecols - 8),
(int) human_size / 10, (int) human_size % 10, *suffix);
}
mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
if (marking && MARKED(j)) {
wcolor_set(rover.window, RVC_MARKS, NULL);
mvwaddch(rover.window, i + 1, 1, RVS_MARK);
} else
mvwaddch(rover.window, i + 1, 1, ' ');
if (j == ESEL)
wattr_off(rover.window, A_REVERSE, NULL);
}
for (; i < HEIGHT; i++)
mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
if (rover.nfiles > HEIGHT) {
int center, height;
center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
height = (HEIGHT-1) * HEIGHT / rover.nfiles;
if (!height) height = 1;
wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
}
BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
if (!rover.nfiles)
strcpy(BUF2, "0/0");
else
snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
color_set(RVC_STATUS, NULL);
mvaddstr(LINES - 1, STATUSPOS, BUF1);
wrefresh(rover.window);
}
/* Show a message on the status bar. */
static void
message(Color color, char *fmt, ...)
{
int len, pos;
va_list args;
va_start(args, fmt);
vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
va_end(args);
len = strlen(BUF1);
pos = (STATUSPOS - len) / 2;
attr_on(A_BOLD, NULL);
color_set(color, NULL);
mvaddstr(LINES - 1, pos, BUF1);
color_set(DEFAULT, NULL);
attr_off(A_BOLD, NULL);
}
/* Clear message area, leaving only status info. */
static void
clear_message()
{
mvhline(LINES - 1, 0, ' ', STATUSPOS);
}
/* Comparison used to sort listing entries. */
static int
rowcmp(const void *a, const void *b)
{
int isdir1, isdir2, cmpdir;
const Row *r1 = a;
const Row *r2 = b;
isdir1 = S_ISDIR(r1->mode);
isdir2 = S_ISDIR(r2->mode);
cmpdir = isdir2 - isdir1;
return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
}
/* Get all entries in current working directory. */
static int
ls(Row **rowsp, uint8_t flags)
{
DIR *dp;
struct dirent *ep;
struct stat statbuf;
Row *rows;
int i, n;
if(!(dp = opendir("."))) return -1;
n = -2; /* We don't want the entries "." and "..". */
while (readdir(dp)) n++;
if (n == 0) {
closedir(dp);
return 0;
}
rewinddir(dp);
rows = malloc(n * sizeof *rows);
i = 0;
while ((ep = readdir(dp))) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
continue;
lstat(ep->d_name, &statbuf);
rows[i].islink = S_ISLNK(statbuf.st_mode);
stat(ep->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
if (flags & SHOW_DIRS) {
rows[i].name = malloc(strlen(ep->d_name) + 2);
strcpy(rows[i].name, ep->d_name);
if (!rows[i].islink)
strcat(rows[i].name, "/");
rows[i].mode = statbuf.st_mode;
i++;
}
} else if (flags & SHOW_FILES) {
rows[i].name = malloc(strlen(ep->d_name) + 1);
strcpy(rows[i].name, ep->d_name);
rows[i].size = statbuf.st_size;
rows[i].mode = statbuf.st_mode;
i++;
}
}
n = i; /* Ignore unused space in array caused by filters. */
qsort(rows, n, sizeof (*rows), rowcmp);
closedir(dp);
*rowsp = rows;
return n;
}
static void
free_rows(Row **rowsp, int nfiles)
{
int i;
for (i = 0; i < nfiles; i++)
free((*rowsp)[i].name);
free(*rowsp);
*rowsp = NULL;
}
/* Change working directory to the path in CWD. */
static void
cd(int reset)
{
int i, j;
message(CYAN, "Loading \"%s\"...", CWD);
refresh();
if (chdir(CWD) == -1) {
getcwd(CWD, PATH_MAX-1);
if (CWD[strlen(CWD)-1] != '/')
strcat(CWD, "/");
goto done;
}
if (reset) ESEL = SCROLL = 0;
if (rover.nfiles)
free_rows(&rover.rows, rover.nfiles);
rover.nfiles = ls(&rover.rows, FLAGS);
if (!strcmp(CWD, rover.marks.dirpath)) {
for (i = 0; i < rover.nfiles; i++) {
for (j = 0; j < rover.marks.bulk; j++)
if (
rover.marks.entries[j] &&
!strcmp(rover.marks.entries[j], ENAME(i))
)
break;
MARKED(i) = j < rover.marks.bulk;
}
} else
for (i = 0; i < rover.nfiles; i++)
MARKED(i) = 0;
done:
clear_message();
update_view();
}
/* Select a target entry, if it is present. */
static void
try_to_sel(const char *target)
{
ESEL = 0;
if (!ISDIR(target))
while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
ESEL++;
while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
ESEL++;
}
/* Reload CWD, but try to keep selection. */
static void
reload()
{
if (rover.nfiles) {
strcpy(INPUT, ENAME(ESEL));
cd(0);
try_to_sel(INPUT);
update_view();
} else
cd(1);
}
static off_t
count_dir(const char *path)
{
DIR *dp;
struct dirent *ep;
struct stat statbuf;
char subpath[PATH_MAX];
off_t total;
if(!(dp = opendir(path))) return 0;
total = 0;
while ((ep = readdir(dp))) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
lstat(subpath, &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
strcat(subpath, "/");
total += count_dir(subpath);
} else
total += statbuf.st_size;
}
closedir(dp);
return total;
}
static off_t
count_marked()
{
int i;
char *entry;
off_t total;
struct stat statbuf;
total = 0;
chdir(rover.marks.dirpath);
for (i = 0; i < rover.marks.bulk; i++) {
entry = rover.marks.entries[i];
if (entry) {
if (ISDIR(entry)) {
total += count_dir(entry);
} else {
lstat(entry, &statbuf);
total += statbuf.st_size;
}
}
}
chdir(CWD);
return total;
}
/* Recursively process a source directory using CWD as destination root.
For each node (i.e. directory), do the following:
1. call pre(destination);
2. call proc() on every child leaf (i.e. files);
3. recurse into every child node;
4. call pos(source).
E.g. to move directory /src/ (and all its contents) inside /dst/:
strcpy(CWD, "/dst/");
process_dir(adddir, movfile, deldir, "/src/"); */
static int
process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
{
int ret;
DIR *dp;
struct dirent *ep;
struct stat statbuf;
char subpath[PATH_MAX];
ret = 0;
if (pre) {
char dstpath[PATH_MAX];
strcpy(dstpath, CWD);
strcat(dstpath, path + strlen(rover.marks.dirpath));
ret |= pre(dstpath);
}
if(!(dp = opendir(path))) return -1;
while ((ep = readdir(dp))) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
lstat(subpath, &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
strcat(subpath, "/");
ret |= process_dir(pre, proc, pos, subpath);
} else
ret |= proc(subpath);
}
closedir(dp);
if (pos) ret |= pos(path);
return ret;
}
/* Process all marked entries using CWD as destination root.
All marked entries that are directories will be recursively processed.
See process_dir() for details on the parameters. */
static void
process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
const char *msg_doing, const char *msg_done)
{
int i, ret;
char *entry;
char path[PATH_MAX];
clear_message();
message(CYAN, "%s...", msg_doing);
refresh();
rover.prog = (Prog) {0, count_marked(), msg_doing};
for (i = 0; i < rover.marks.bulk; i++) {
entry = rover.marks.entries[i];
if (entry) {
ret = 0;
snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
if (ISDIR(entry)) {
if (!strncmp(path, CWD, strlen(path)))
ret = -1;
else
ret = process_dir(pre, proc, pos, path);
} else
ret = proc(path);
if (!ret) {
del_mark(&rover.marks, entry);
reload();
}
}
}
rover.prog.total = 0;
reload();
if (!rover.marks.nentries)
message(GREEN, "%s all marked entries.", msg_done);
else
message(RED, "Some errors occured while %s.", msg_doing);
RV_ALERT();
}
static void
update_progress(off_t delta)
{
int percent;
if (!rover.prog.total) return;
rover.prog.partial += delta;
percent = (int) (rover.prog.partial * 100 / rover.prog.total);
message(CYAN, "%s...%d%%", rover.prog.msg, percent);
refresh();
}
/* Wrappers for file operations. */
static int delfile(const char *path) {
int ret;
struct stat st;
ret = lstat(path, &st);
if (ret < 0) return ret;
update_progress(st.st_size);
return unlink(path);
}
static PROCESS deldir = rmdir;
static int addfile(const char *path) {
/* Using creat(2) because mknod(2) doesn't seem to be portable. */
int ret;
ret = creat(path, 0644);
if (ret < 0) return ret;
return close(ret);
}
static int cpyfile(const char *srcpath) {
int src, dst, ret;
size_t size;
struct stat st;
char buf[BUFSIZ];
char dstpath[PATH_MAX];
strcpy(dstpath, CWD);
strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
ret = lstat(srcpath, &st);
if (ret < 0) return ret;
if (S_ISLNK(st.st_mode)) {
ret = readlink(srcpath, BUF1, BUFLEN-1);
if (ret < 0) return ret;
BUF1[ret] = '\0';
ret = symlink(BUF1, dstpath);
} else {
ret = src = open(srcpath, O_RDONLY);
if (ret < 0) return ret;
ret = dst = creat(dstpath, st.st_mode);
if (ret < 0) return ret;
while ((size = read(src, buf, BUFSIZ)) > 0) {
write(dst, buf, size);
update_progress(size);
sync_signals();
}
close(src);
close(dst);
ret = 0;
}
return ret;
}
static int adddir(const char *path) {
int ret;
struct stat st;
ret = stat(CWD, &st);
if (ret < 0) return ret;
return mkdir(path, st.st_mode);
}
static int movfile(const char *srcpath) {
int ret;
struct stat st;
char dstpath[PATH_MAX];
strcpy(dstpath, CWD);
strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
ret = rename(srcpath, dstpath);
if (ret == 0) {
ret = lstat(dstpath, &st);
if (ret < 0) return ret;
update_progress(st.st_size);
} else if (errno == EXDEV) {
ret = cpyfile(srcpath);
if (ret < 0) return ret;
ret = unlink(srcpath);
}
return ret;
}
static void
start_line_edit(const char *init_input)
{
curs_set(TRUE);
strncpy(INPUT, init_input, BUFLEN);
rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
rover.edit.right = BUFLEN - 1;
rover.edit.buffer[BUFLEN] = L'\0';
rover.edit_scroll = 0;
}
/* Read input and change editing state accordingly. */
static EditStat
get_line_edit()
{
wchar_t eraser, killer, wch;
int ret, length;
ret = rover_get_wch((wint_t *) &wch);
erasewchar(&eraser);
killwchar(&killer);
if (ret == KEY_CODE_YES) {
if (wch == KEY_ENTER) {
curs_set(FALSE);
return CONFIRM;
} else if (wch == KEY_LEFT) {
if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
} else if (wch == KEY_RIGHT) {
if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
} else if (wch == KEY_UP) {
while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
} else if (wch == KEY_DOWN) {
while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
} else if (wch == KEY_BACKSPACE) {
if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
} else if (wch == KEY_DC) {
if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
}
} else {
if (wch == L'\r' || wch == L'\n') {
curs_set(FALSE);
return CONFIRM;
} else if (wch == L'\t') {
curs_set(FALSE);
return CANCEL;
} else if (wch == eraser) {
if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
} else if (wch == killer) {
EDIT_CLEAR(rover.edit);
clear_message();
} else if (iswprint(wch)) {
if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
}
}
/* Encode edit contents in INPUT. */
rover.edit.buffer[rover.edit.left] = L'\0';
length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
BUFLEN-length);
return CONTINUE;
}
/* Update line input on the screen. */
static void