1 #define _XOPEN_SOURCE 700
2 #define _XOPEN_SOURCE_EXTENDED
3 #define _FILE_OFFSET_BITS 64
11 #include <sys/types.h> /* pid_t, ... */
13 #include <limits.h> /* PATH_MAX */
14 #include <locale.h> /* setlocale(), LC_ALL */
15 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
16 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
18 #include <fcntl.h> /* open() */
19 #include <sys/wait.h> /* waitpid() */
20 #include <signal.h> /* struct sigaction, sigaction() */
27 /* This signal is not defined by POSIX, but should be
28 present on all systems that have resizable terminals. */
34 #define BUFLEN PATH_MAX
35 static char BUF1
[BUFLEN
];
36 static char BUF2
[BUFLEN
];
37 static char INPUT
[BUFLEN
];
38 static wchar_t WBUF
[BUFLEN
];
40 /* Argument buffers for execvp(). */
42 static char *ARGS
[MAXARGS
];
44 /* Listing view parameters. */
45 #define HEIGHT (LINES-4)
46 #define STATUSPOS (COLS-16)
48 /* Listing view flags. */
49 #define SHOW_FILES 0x01u
50 #define SHOW_DIRS 0x02u
51 #define SHOW_HIDDEN 0x04u
53 /* Marks parameters. */
55 #define BULK_THRESH 256
57 /* Information associated to each entry in listing. */
66 /* Dynamic array of marked entries. */
67 typedef struct Marks
{
68 char dirpath
[PATH_MAX
];
74 /* Line editing state. */
76 wchar_t buffer
[BUFLEN
+1];
80 /* Each tab only stores the following information. */
103 volatile sig_atomic_t pending_winch
;
108 /* Macros for accessing global state. */
109 #define ENAME(I) rover.rows[I].name
110 #define ESIZE(I) rover.rows[I].size
111 #define EMODE(I) rover.rows[I].mode
112 #define ISLINK(I) rover.rows[I].islink
113 #define MARKED(I) rover.rows[I].marked
114 #define SCROLL rover.tabs[rover.tab].scroll
115 #define ESEL rover.tabs[rover.tab].esel
116 #define FLAGS rover.tabs[rover.tab].flags
117 #define CWD rover.tabs[rover.tab].cwd
120 #define MIN(A, B) ((A) < (B) ? (A) : (B))
121 #define MAX(A, B) ((A) > (B) ? (A) : (B))
122 #define ISDIR(E) (strchr((E), '/') != NULL)
124 /* Line Editing Macros. */
125 #define EDIT_FULL(E) ((E).left == (E).right)
126 #define EDIT_CAN_LEFT(E) ((E).left)
127 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
128 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
129 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
130 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
131 #define EDIT_BACKSPACE(E) (E).left--
132 #define EDIT_DELETE(E) (E).right++
133 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
135 typedef enum EditStat
{CONTINUE
, CONFIRM
, CANCEL
} EditStat
;
136 typedef enum Color
{DEFAULT
, RED
, GREEN
, YELLOW
, BLUE
, CYAN
, MAGENTA
, WHITE
, BLACK
} Color
;
137 typedef int (*PROCESS
)(const char *path
);
140 init_marks(Marks
*marks
)
142 strcpy(marks
->dirpath
, "");
143 marks
->bulk
= BULK_INIT
;
145 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
148 /* Unmark all entries. */
150 mark_none(Marks
*marks
)
154 strcpy(marks
->dirpath
, "");
155 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
156 if (marks
->entries
[i
]) {
157 free(marks
->entries
[i
]);
158 marks
->entries
[i
] = NULL
;
161 if (marks
->bulk
> BULK_THRESH
) {
162 /* Reset bulk to free some memory. */
163 free(marks
->entries
);
164 marks
->bulk
= BULK_INIT
;
165 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
170 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
174 if (!strcmp(marks
->dirpath
, dirpath
)) {
175 /* Append mark to directory. */
176 if (marks
->nentries
== marks
->bulk
) {
177 /* Expand bulk to accomodate new entry. */
178 int extra
= marks
->bulk
/ 2;
179 marks
->bulk
+= extra
; /* bulk *= 1.5; */
180 marks
->entries
= realloc(marks
->entries
,
181 marks
->bulk
* sizeof *marks
->entries
);
182 memset(&marks
->entries
[marks
->nentries
], 0,
183 extra
* sizeof *marks
->entries
);
186 /* Search for empty slot (there must be one). */
187 for (i
= 0; i
< marks
->bulk
; i
++)
188 if (!marks
->entries
[i
])
192 /* Directory changed. Discard old marks. */
194 strcpy(marks
->dirpath
, dirpath
);
197 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
198 strcpy(marks
->entries
[i
], entry
);
203 del_mark(Marks
*marks
, char *entry
)
207 if (marks
->nentries
> 1) {
208 for (i
= 0; i
< marks
->bulk
; i
++)
209 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
211 free(marks
->entries
[i
]);
212 marks
->entries
[i
] = NULL
;
219 free_marks(Marks
*marks
)
223 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
224 if (marks
->entries
[i
]) {
225 free(marks
->entries
[i
]);
228 free(marks
->entries
);
232 handle_winch(int sig
)
234 rover
.pending_winch
= 1;
242 memset(&sa
, 0, sizeof (struct sigaction
));
243 sa
.sa_handler
= handle_winch
;
244 sigaction(SIGWINCH
, &sa
, NULL
);
252 memset(&sa
, 0, sizeof (struct sigaction
));
253 sa
.sa_handler
= SIG_DFL
;
254 sigaction(SIGWINCH
, &sa
, NULL
);
257 static void update_view();
259 /* Handle any signals received since last call. */
263 if (rover
.pending_winch
) {
264 /* SIGWINCH received: resize application accordingly. */
265 delwin(rover
.window
);
269 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
270 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
271 SCROLL
= ESEL
- HEIGHT
;
273 rover
.pending_winch
= 0;
277 /* This function must be used in place of getch().
278 It handles signals while waiting for user input. */
284 while ((ch
= getch()) == ERR
)
289 /* This function must be used in place of get_wch().
290 It handles signals while waiting for user input. */
292 rover_get_wch(wint_t *wch
)
296 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
301 /* Do a fork-exec to external program (e.g. $EDITOR). */
308 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
311 /* fork() succeeded. */
314 waitpid(pid
, &status
, 0);
316 kill(getpid(), SIGWINCH
);
317 } else if (pid
== 0) {
319 execvp(ARGS
[0], ARGS
);
327 setlocale(LC_ALL
, "");
329 cbreak(); /* Get one character at a time. */
330 timeout(100); /* For getch(). */
332 nonl(); /* No NL->CR/NL on output. */
333 intrflush(stdscr
, FALSE
);
334 keypad(stdscr
, TRUE
);
335 curs_set(FALSE
); /* Hide blinking cursor. */
339 #ifdef NCURSES_EXT_FUNCS
340 use_default_colors();
345 init_pair(RED
, COLOR_RED
, bg
);
346 init_pair(GREEN
, COLOR_GREEN
, bg
);
347 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
348 init_pair(BLUE
, COLOR_BLUE
, bg
);
349 init_pair(CYAN
, COLOR_CYAN
, bg
);
350 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
351 init_pair(WHITE
, COLOR_WHITE
, bg
);
352 init_pair(BLACK
, COLOR_BLACK
, bg
);
354 atexit((void (*)(void)) endwin
);
358 /* Update the listing view. */
367 mvhline(0, 0, ' ', COLS
);
368 attr_on(A_BOLD
, NULL
);
369 color_set(RVC_TABNUM
, NULL
);
370 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
371 attr_off(A_BOLD
, NULL
);
372 if (rover
.marks
.nentries
) {
373 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
374 color_set(RVC_MARKS
, NULL
);
375 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
378 color_set(RVC_CWD
, NULL
);
379 mbstowcs(WBUF
, CWD
, PATH_MAX
);
380 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
381 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
382 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
383 ESEL
= MAX(MIN(ESEL
, rover
.nfiles
- 1), 0);
384 /* Selection might not be visible, due to cursor wrapping or window
385 shrinking. In that case, the scroll must be moved to make it visible. */
386 if (rover
.nfiles
> HEIGHT
) {
387 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
388 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
391 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
392 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
393 ishidden
= ENAME(j
)[0] == '.';
395 wattr_on(rover
.window
, A_REVERSE
, NULL
);
397 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
399 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
400 else if (S_ISREG(EMODE(j
))) {
401 if (EMODE(j
) & (S_IXUSR
| S_IXGRP
| S_IXOTH
))
402 wcolor_set(rover
.window
, RVC_EXEC
, NULL
);
404 wcolor_set(rover
.window
, RVC_REG
, NULL
);
405 } else if (S_ISDIR(EMODE(j
)))
406 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
407 else if (S_ISCHR(EMODE(j
)))
408 wcolor_set(rover
.window
, RVC_CHR
, NULL
);
409 else if (S_ISBLK(EMODE(j
)))
410 wcolor_set(rover
.window
, RVC_BLK
, NULL
);
411 else if (S_ISFIFO(EMODE(j
)))
412 wcolor_set(rover
.window
, RVC_FIFO
, NULL
);
413 else if (S_ISSOCK(EMODE(j
)))
414 wcolor_set(rover
.window
, RVC_SOCK
, NULL
);
415 if (!S_ISDIR(EMODE(j
))) {
416 char *suffix
, *suffixes
= "BKMGTPEZY";
417 off_t human_size
= ESIZE(j
) * 10;
418 int length
= mbstowcs(NULL
, ENAME(j
), 0);
419 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
420 human_size
= (human_size
+ 512) / 1024;
422 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
423 (int) (COLS
- length
- 6),
424 (int) human_size
/ 10, *suffix
);
426 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
427 (int) (COLS
- length
- 8),
428 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
430 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
431 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
432 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
433 if (marking
&& MARKED(j
)) {
434 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
435 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
437 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
439 wattr_off(rover
.window
, A_REVERSE
, NULL
);
441 for (; i
< HEIGHT
; i
++)
442 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
443 if (rover
.nfiles
> HEIGHT
) {
445 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
446 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
447 if (!height
) height
= 1;
448 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
449 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
451 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
452 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
453 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
457 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
458 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
459 color_set(RVC_STATUS
, NULL
);
460 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
461 wrefresh(rover
.window
);
464 /* Show a message on the status bar. */
466 message(Color color
, char *fmt
, ...)
472 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
475 pos
= (STATUSPOS
- len
) / 2;
476 attr_on(A_BOLD
, NULL
);
477 color_set(color
, NULL
);
478 mvaddstr(LINES
- 1, pos
, BUF1
);
479 color_set(DEFAULT
, NULL
);
480 attr_off(A_BOLD
, NULL
);
483 /* Clear message area, leaving only status info. */
487 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
490 /* Comparison used to sort listing entries. */
492 rowcmp(const void *a
, const void *b
)
494 int isdir1
, isdir2
, cmpdir
;
497 isdir1
= S_ISDIR(r1
->mode
);
498 isdir2
= S_ISDIR(r2
->mode
);
499 cmpdir
= isdir2
- isdir1
;
500 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
503 /* Get all entries in current working directory. */
505 ls(Row
**rowsp
, uint8_t flags
)
513 if(!(dp
= opendir("."))) return -1;
514 n
= -2; /* We don't want the entries "." and "..". */
515 while (readdir(dp
)) n
++;
517 rows
= malloc(n
* sizeof *rows
);
519 while ((ep
= readdir(dp
))) {
520 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
522 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
524 lstat(ep
->d_name
, &statbuf
);
525 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
526 stat(ep
->d_name
, &statbuf
);
527 if (S_ISDIR(statbuf
.st_mode
)) {
528 if (flags
& SHOW_DIRS
) {
529 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
530 strcpy(rows
[i
].name
, ep
->d_name
);
531 strcat(rows
[i
].name
, "/");
532 rows
[i
].mode
= statbuf
.st_mode
;
535 } else if (flags
& SHOW_FILES
) {
536 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
537 strcpy(rows
[i
].name
, ep
->d_name
);
538 rows
[i
].size
= statbuf
.st_size
;
539 rows
[i
].mode
= statbuf
.st_mode
;
543 n
= i
; /* Ignore unused space in array caused by filters. */
544 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
551 free_rows(Row
**rowsp
, int nfiles
)
555 for (i
= 0; i
< nfiles
; i
++)
556 free((*rowsp
)[i
].name
);
561 /* Change working directory to the path in CWD. */
567 message(CYAN
, "Loading...");
569 if (reset
) ESEL
= SCROLL
= 0;
572 free_rows(&rover
.rows
, rover
.nfiles
);
573 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
574 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
575 for (i
= 0; i
< rover
.nfiles
; i
++) {
576 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
578 rover
.marks
.entries
[j
] &&
579 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
582 MARKED(i
) = j
< rover
.marks
.bulk
;
585 for (i
= 0; i
< rover
.nfiles
; i
++)
591 /* Select a target entry, if it is present. */
593 try_to_sel(const char *target
)
597 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
599 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
603 /* Reload CWD, but try to keep selection. */
608 strcpy(INPUT
, ENAME(ESEL
));
617 count_dir(const char *path
)
622 char subpath
[PATH_MAX
];
625 if(!(dp
= opendir(path
))) return 0;
627 while ((ep
= readdir(dp
))) {
628 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
630 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
631 lstat(subpath
, &statbuf
);
632 if (S_ISDIR(statbuf
.st_mode
)) {
633 strcat(subpath
, "/");
634 total
+= count_dir(subpath
);
636 total
+= statbuf
.st_size
;
651 chdir(rover
.marks
.dirpath
);
652 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
653 entry
= rover
.marks
.entries
[i
];
656 total
+= count_dir(entry
);
658 lstat(entry
, &statbuf
);
659 total
+= statbuf
.st_size
;
667 /* Recursively process a source directory using CWD as destination root.
668 For each node (i.e. directory), do the following:
669 1. call pre(destination);
670 2. call proc() on every child leaf (i.e. files);
671 3. recurse into every child node;
673 E.g. to move directory /src/ (and all its contents) inside /dst/:
674 strcpy(CWD, "/dst/");
675 process_dir(adddir, movfile, deldir, "/src/"); */
677 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
683 char subpath
[PATH_MAX
];
687 char dstpath
[PATH_MAX
];
688 strcpy(dstpath
, CWD
);
689 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
692 if(!(dp
= opendir(path
))) return -1;
693 while ((ep
= readdir(dp
))) {
694 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
696 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
697 stat(subpath
, &statbuf
);
698 if (S_ISDIR(statbuf
.st_mode
)) {
699 strcat(subpath
, "/");
700 ret
|= process_dir(pre
, proc
, pos
, subpath
);
702 ret
|= proc(subpath
);
705 if (pos
) ret
|= pos(path
);
709 /* Process all marked entries using CWD as destination root.
710 All marked entries that are directories will be recursively processed.
711 See process_dir() for details on the parameters. */
713 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
714 const char *msg_doing
, const char *msg_done
)
721 message(CYAN
, "%s...", msg_doing
);
723 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
724 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
725 entry
= rover
.marks
.entries
[i
];
728 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
730 if (!strncmp(path
, CWD
, strlen(path
)))
733 ret
= process_dir(pre
, proc
, pos
, path
);
737 del_mark(&rover
.marks
, entry
);
742 rover
.prog
.total
= 0;
744 if (!rover
.marks
.nentries
)
745 message(GREEN
, "%s all marked entries.", msg_done
);
747 message(RED
, "Some errors occured while %s.", msg_doing
);
752 update_progress(off_t delta
)
756 if (!rover
.prog
.total
) return;
757 rover
.prog
.partial
+= delta
;
758 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
759 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
763 /* Wrappers for file operations. */
764 static int delfile(const char *path
) {
768 ret
= lstat(path
, &st
);
769 if (ret
< 0) return ret
;
770 update_progress(st
.st_size
);
773 static PROCESS deldir
= rmdir
;
774 static int addfile(const char *path
) {
775 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
778 ret
= creat(path
, 0644);
779 if (ret
< 0) return ret
;
782 static int cpyfile(const char *srcpath
) {
787 char dstpath
[PATH_MAX
];
789 ret
= src
= open(srcpath
, O_RDONLY
);
790 if (ret
< 0) return ret
;
791 ret
= fstat(src
, &st
);
792 if (ret
< 0) return ret
;
793 strcpy(dstpath
, CWD
);
794 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
795 ret
= dst
= creat(dstpath
, st
.st_mode
);
796 if (ret
< 0) return ret
;
797 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
798 write(dst
, buf
, size
);
799 update_progress(size
);
806 static int adddir(const char *path
) {
810 ret
= stat(CWD
, &st
);
811 if (ret
< 0) return ret
;
812 return mkdir(path
, st
.st_mode
);
814 static int movfile(const char *srcpath
) {
817 char dstpath
[PATH_MAX
];
819 strcpy(dstpath
, CWD
);
820 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
821 ret
= rename(srcpath
, dstpath
);
823 ret
= lstat(dstpath
, &st
);
824 if (ret
< 0) return ret
;
825 update_progress(st
.st_size
);
826 } else if (errno
== EXDEV
) {
827 ret
= cpyfile(srcpath
);
828 if (ret
< 0) return ret
;
829 ret
= unlink(srcpath
);
835 start_line_edit(const char *init_input
)
838 strncpy(INPUT
, init_input
, BUFLEN
);
839 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
840 rover
.edit
.right
= BUFLEN
- 1;
841 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
842 rover
.edit_scroll
= 0;
845 /* Read input and change editing state accordingly. */
849 wchar_t eraser
, killer
, wch
;
852 ret
= rover_get_wch((wint_t *) &wch
);
855 if (ret
== KEY_CODE_YES
) {
856 if (wch
== KEY_ENTER
) {
859 } else if (wch
== KEY_LEFT
) {
860 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
861 } else if (wch
== KEY_RIGHT
) {
862 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
863 } else if (wch
== KEY_UP
) {
864 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
865 } else if (wch
== KEY_DOWN
) {
866 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
867 } else if (wch
== KEY_BACKSPACE
) {
868 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
869 } else if (wch
== KEY_DC
) {
870 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
873 if (wch
== L
'\r' || wch
== L
'\n') {
876 } else if (wch
== L
'\t') {
879 } else if (wch
== eraser
) {
880 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
881 } else if (wch
== killer
) {
882 EDIT_CLEAR(rover
.edit
);
884 } else if (iswprint(wch
)) {
885 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
888 /* Encode edit contents in INPUT. */
889 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
890 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
891 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
896 /* Update line input on the screen. */
898 update_input(const char *prompt
, Color color
)
900 int plen
, ilen
, maxlen
;
902 plen
= strlen(prompt
);
903 ilen
= mbstowcs(NULL
, INPUT
, 0);
904 maxlen
= STATUSPOS
- plen
- 2;
905 if (ilen
- rover
.edit_scroll
< maxlen
)
906 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
907 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
908 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
909 else if (rover
.edit
.left
< rover
.edit_scroll
)
910 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
911 color_set(RVC_PROMPT
, NULL
);
912 mvaddstr(LINES
- 1, 0, prompt
);
913 color_set(color
, NULL
);
914 mbstowcs(WBUF
, INPUT
, COLS
);
915 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
916 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
917 color_set(DEFAULT
, NULL
);
918 if (rover
.edit_scroll
)
919 mvaddch(LINES
- 1, plen
- 1, '<');
920 if (ilen
> rover
.edit_scroll
+ maxlen
)
921 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
922 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
926 main(int argc
, char *argv
[])
934 FILE *save_cwd_file
= NULL
;
935 FILE *save_marks_file
= NULL
;
938 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
939 printf("rover %s\n", RV_VERSION
);
941 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
944 " [-d|--save-cwd FILE]"
945 " [-m|--save-marks FILE]"
946 " [DIR [DIR [DIR [...]]]]\n"
947 " Browse current directory or the ones specified.\n"
948 " If FILE is given, write last visited path to it.\n\n"
949 " or: rover -h|--help\n"
950 " Print this help message and exit.\n\n"
951 " or: rover -v|--version\n"
952 " Print program version and exit.\n\n"
953 "See rover(1) for more information.\n\n"
954 "Rover homepage: <https://github.com/lecram/rover>.\n"
957 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
959 save_cwd_file
= fopen(argv
[2], "w");
960 argc
-= 2; argv
+= 2;
962 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
965 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
967 save_marks_file
= fopen(argv
[2], "a");
968 argc
-= 2; argv
+= 2;
970 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
977 for (i
= 0; i
< 10; i
++) {
978 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
979 rover
.tabs
[i
].flags
= SHOW_FILES
| SHOW_DIRS
;
981 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
982 for (i
= 1; i
< argc
&& i
< 10; i
++) {
983 if ((d
= opendir(argv
[i
]))) {
984 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
987 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
989 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
990 for (i
++; i
< 10; i
++)
991 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
992 for (i
= 0; i
< 10; i
++)
993 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
994 strcat(rover
.tabs
[i
].cwd
, "/");
996 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
997 init_marks(&rover
.marks
);
1003 if (!strcmp(key
, RVK_QUIT
)) break;
1004 else if (ch
>= '0' && ch
<= '9') {
1005 rover
.tab
= ch
- '0';
1007 } else if (!strcmp(key
, RVK_HELP
)) {
1012 } else if (!strcmp(key
, RVK_DOWN
)) {
1013 if (!rover
.nfiles
) continue;
1014 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
1016 } else if (!strcmp(key
, RVK_UP
)) {
1017 if (!rover
.nfiles
) continue;
1018 ESEL
= MAX(ESEL
- 1, 0);
1020 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
1021 if (!rover
.nfiles
) continue;
1022 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
1023 if (rover
.nfiles
> HEIGHT
)
1024 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
1026 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
1027 if (!rover
.nfiles
) continue;
1028 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
1029 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
1031 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1032 if (!rover
.nfiles
) continue;
1035 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1036 if (!rover
.nfiles
) continue;
1037 ESEL
= rover
.nfiles
- 1;
1039 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1040 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1041 if (chdir(ENAME(ESEL
)) == -1) {
1042 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1045 strcat(CWD
, ENAME(ESEL
));
1047 } else if (!strcmp(key
, RVK_CD_UP
)) {
1048 char *dirname
, first
;
1049 if (!strcmp(CWD
, "/")) continue;
1050 CWD
[strlen(CWD
) - 1] = '\0';
1051 dirname
= strrchr(CWD
, '/') + 1;
1056 dirname
[strlen(dirname
)] = '/';
1057 try_to_sel(dirname
);
1059 if (rover
.nfiles
> HEIGHT
)
1060 SCROLL
= ESEL
- HEIGHT
/ 2;
1062 } else if (!strcmp(key
, RVK_HOME
)) {
1063 strcpy(CWD
, getenv("HOME"));
1064 if (CWD
[strlen(CWD
) - 1] != '/')
1067 } else if (!strcmp(key
, RVK_REFRESH
)) {
1069 } else if (!strcmp(key
, RVK_SHELL
)) {
1070 program
= getenv("SHELL");
1077 } else if (!strcmp(key
, RVK_VIEW
)) {
1078 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1079 program
= getenv("PAGER");
1082 ARGS
[1] = ENAME(ESEL
);
1086 } else if (!strcmp(key
, RVK_EDIT
)) {
1087 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1088 program
= getenv("EDITOR");
1091 ARGS
[1] = ENAME(ESEL
);
1096 } else if (!strcmp(key
, RVK_SEARCH
)) {
1097 int oldsel
, oldscroll
, length
;
1098 if (!rover
.nfiles
) continue;
1101 start_line_edit("");
1102 update_input(RVP_SEARCH
, RED
);
1103 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1106 length
= strlen(INPUT
);
1108 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1109 if (!strncmp(ENAME(sel
), INPUT
, length
))
1111 if (sel
< rover
.nfiles
) {
1114 if (rover
.nfiles
> HEIGHT
) {
1117 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1118 SCROLL
= rover
.nfiles
- HEIGHT
;
1128 update_input(RVP_SEARCH
, color
);
1130 if (edit_stat
== CANCEL
) {
1136 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1137 FLAGS
^= SHOW_FILES
;
1139 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1142 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1143 FLAGS
^= SHOW_HIDDEN
;
1145 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1147 start_line_edit("");
1148 update_input(RVP_NEW_FILE
, RED
);
1149 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1150 int length
= strlen(INPUT
);
1152 for (i
= 0; i
< rover
.nfiles
; i
++) {
1154 !strncmp(ENAME(i
), INPUT
, length
) &&
1155 (!strcmp(ENAME(i
) + length
, "") ||
1156 !strcmp(ENAME(i
) + length
, "/"))
1162 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1165 if (edit_stat
== CONFIRM
) {
1172 message(RED
, "\"%s\" already exists.", INPUT
);
1174 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1176 start_line_edit("");
1177 update_input(RVP_NEW_DIR
, RED
);
1178 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1179 int length
= strlen(INPUT
);
1181 for (i
= 0; i
< rover
.nfiles
; i
++) {
1183 !strncmp(ENAME(i
), INPUT
, length
) &&
1184 (!strcmp(ENAME(i
) + length
, "") ||
1185 !strcmp(ENAME(i
) + length
, "/"))
1191 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1194 if (edit_stat
== CONFIRM
) {
1202 message(RED
, "\"%s\" already exists.", INPUT
);
1204 } else if (!strcmp(key
, RVK_RENAME
)) {
1208 strcpy(INPUT
, ENAME(ESEL
));
1209 last
= INPUT
+ strlen(INPUT
) - 1;
1210 if ((isdir
= *last
== '/'))
1212 start_line_edit(INPUT
);
1213 update_input(RVP_RENAME
, RED
);
1214 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1215 int length
= strlen(INPUT
);
1217 for (i
= 0; i
< rover
.nfiles
; i
++)
1219 !strncmp(ENAME(i
), INPUT
, length
) &&
1220 (!strcmp(ENAME(i
) + length
, "") ||
1221 !strcmp(ENAME(i
) + length
, "/"))
1226 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1229 if (edit_stat
== CONFIRM
) {
1233 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1234 del_mark(&rover
.marks
, ENAME(ESEL
));
1235 add_mark(&rover
.marks
, CWD
, INPUT
);
1241 message(RED
, "\"%s\" already exists.", INPUT
);
1243 } else if (!strcmp(key
, RVK_DELETE
)) {
1245 message(YELLOW
, "Delete \"%s\"? (Y to confirm)", ENAME(ESEL
));
1246 if (rover_getch() == 'Y') {
1247 const char *name
= ENAME(ESEL
);
1248 int ret
= S_ISDIR(EMODE(ESEL
)) ? deldir(name
) : delfile(name
);
1251 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1255 message(RED
, "No entry selected for deletion.");
1256 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1258 del_mark(&rover
.marks
, ENAME(ESEL
));
1260 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1261 MARKED(ESEL
) = !MARKED(ESEL
);
1262 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1264 } else if (!strcmp(key
, RVK_INVMARK
)) {
1265 for (i
= 0; i
< rover
.nfiles
; i
++) {
1267 del_mark(&rover
.marks
, ENAME(i
));
1269 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1270 MARKED(i
) = !MARKED(i
);
1273 } else if (!strcmp(key
, RVK_MARKALL
)) {
1274 for (i
= 0; i
< rover
.nfiles
; i
++)
1276 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1280 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1281 if (rover
.marks
.nentries
) {
1282 message(YELLOW
, "Delete all marked entries? (Y to confirm)");
1283 if (rover_getch() == 'Y')
1284 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1288 message(RED
, "No entries marked for deletion.");
1289 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1290 if (rover
.marks
.nentries
)
1291 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1293 message(RED
, "No entries marked for copying.");
1294 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1295 if (rover
.marks
.nentries
)
1296 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1298 message(RED
, "No entries marked for moving.");
1302 free_rows(&rover
.rows
, rover
.nfiles
);
1303 delwin(rover
.window
);
1304 if (save_cwd_file
!= NULL
) {
1305 fputs(CWD
, save_cwd_file
);
1306 fclose(save_cwd_file
);
1308 if (save_marks_file
!= NULL
) {
1309 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
1310 entry
= rover
.marks
.entries
[i
];
1312 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1314 fclose(save_marks_file
);
1316 free_marks(&rover
.marks
);