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(), ... */
19 #include <fcntl.h> /* open() */
20 #include <sys/wait.h> /* waitpid() */
21 #include <signal.h> /* struct sigaction, sigaction() */
28 /* This signal is not defined by POSIX, but should be
29 present on all systems that have resizable terminals. */
35 #define BUFLEN PATH_MAX
36 static char BUF1
[BUFLEN
];
37 static char BUF2
[BUFLEN
];
38 static char INPUT
[BUFLEN
];
39 static wchar_t WBUF
[BUFLEN
];
41 /* Listing view parameters. */
42 #define HEIGHT (LINES-4)
43 #define STATUSPOS (COLS-16)
45 /* Listing view flags. */
46 #define SHOW_FILES 0x01u
47 #define SHOW_DIRS 0x02u
48 #define SHOW_HIDDEN 0x04u
50 /* Marks parameters. */
52 #define BULK_THRESH 256
54 /* Information associated to each entry in listing. */
63 /* Dynamic array of marked entries. */
64 typedef struct Marks
{
65 char dirpath
[PATH_MAX
];
71 /* Line editing state. */
73 wchar_t buffer
[BUFLEN
+1];
77 /* Each tab only stores the following information. */
100 volatile sig_atomic_t pending_winch
;
105 /* Macros for accessing global state. */
106 #define ENAME(I) rover.rows[I].name
107 #define ESIZE(I) rover.rows[I].size
108 #define EMODE(I) rover.rows[I].mode
109 #define ISLINK(I) rover.rows[I].islink
110 #define MARKED(I) rover.rows[I].marked
111 #define SCROLL rover.tabs[rover.tab].scroll
112 #define ESEL rover.tabs[rover.tab].esel
113 #define FLAGS rover.tabs[rover.tab].flags
114 #define CWD rover.tabs[rover.tab].cwd
117 #define MIN(A, B) ((A) < (B) ? (A) : (B))
118 #define MAX(A, B) ((A) > (B) ? (A) : (B))
119 #define ISDIR(E) (strchr((E), '/') != NULL)
121 /* Line Editing Macros. */
122 #define EDIT_FULL(E) ((E).left == (E).right)
123 #define EDIT_CAN_LEFT(E) ((E).left)
124 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
125 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
126 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
127 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
128 #define EDIT_BACKSPACE(E) (E).left--
129 #define EDIT_DELETE(E) (E).right++
130 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
132 typedef enum EditStat
{CONTINUE
, CONFIRM
, CANCEL
} EditStat
;
133 typedef enum Color
{DEFAULT
, RED
, GREEN
, YELLOW
, BLUE
, CYAN
, MAGENTA
, WHITE
, BLACK
} Color
;
134 typedef int (*PROCESS
)(const char *path
);
137 init_marks(Marks
*marks
)
139 strcpy(marks
->dirpath
, "");
140 marks
->bulk
= BULK_INIT
;
142 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
145 /* Unmark all entries. */
147 mark_none(Marks
*marks
)
151 strcpy(marks
->dirpath
, "");
152 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
153 if (marks
->entries
[i
]) {
154 free(marks
->entries
[i
]);
155 marks
->entries
[i
] = NULL
;
158 if (marks
->bulk
> BULK_THRESH
) {
159 /* Reset bulk to free some memory. */
160 free(marks
->entries
);
161 marks
->bulk
= BULK_INIT
;
162 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
167 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
171 if (!strcmp(marks
->dirpath
, dirpath
)) {
172 /* Append mark to directory. */
173 if (marks
->nentries
== marks
->bulk
) {
174 /* Expand bulk to accomodate new entry. */
175 int extra
= marks
->bulk
/ 2;
176 marks
->bulk
+= extra
; /* bulk *= 1.5; */
177 marks
->entries
= realloc(marks
->entries
,
178 marks
->bulk
* sizeof *marks
->entries
);
179 memset(&marks
->entries
[marks
->nentries
], 0,
180 extra
* sizeof *marks
->entries
);
183 /* Search for empty slot (there must be one). */
184 for (i
= 0; i
< marks
->bulk
; i
++)
185 if (!marks
->entries
[i
])
189 /* Directory changed. Discard old marks. */
191 strcpy(marks
->dirpath
, dirpath
);
194 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
195 strcpy(marks
->entries
[i
], entry
);
200 del_mark(Marks
*marks
, char *entry
)
204 if (marks
->nentries
> 1) {
205 for (i
= 0; i
< marks
->bulk
; i
++)
206 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
208 free(marks
->entries
[i
]);
209 marks
->entries
[i
] = NULL
;
216 free_marks(Marks
*marks
)
220 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
221 if (marks
->entries
[i
]) {
222 free(marks
->entries
[i
]);
225 free(marks
->entries
);
229 handle_winch(int sig
)
231 rover
.pending_winch
= 1;
239 memset(&sa
, 0, sizeof (struct sigaction
));
240 sa
.sa_handler
= handle_winch
;
241 sigaction(SIGWINCH
, &sa
, NULL
);
249 memset(&sa
, 0, sizeof (struct sigaction
));
250 sa
.sa_handler
= SIG_DFL
;
251 sigaction(SIGWINCH
, &sa
, NULL
);
254 static void update_view();
256 /* Handle any signals received since last call. */
260 if (rover
.pending_winch
) {
261 /* SIGWINCH received: resize application accordingly. */
262 delwin(rover
.window
);
266 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
267 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
268 SCROLL
= ESEL
- HEIGHT
;
270 rover
.pending_winch
= 0;
274 /* This function must be used in place of getch().
275 It handles signals while waiting for user input. */
281 while ((ch
= getch()) == ERR
)
286 /* This function must be used in place of get_wch().
287 It handles signals while waiting for user input. */
289 rover_get_wch(wint_t *wch
)
293 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
298 /* Do a fork-exec to external program (e.g. $EDITOR). */
305 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
308 /* fork() succeeded. */
311 waitpid(pid
, &status
, 0);
313 kill(getpid(), SIGWINCH
);
314 } else if (pid
== 0) {
316 execvp(args
[0], args
);
321 shell_escaped_cat(char *buf
, char *str
, size_t n
)
323 char *p
= buf
+ strlen(buf
);
325 for (n
--; n
; n
--, str
++) {
346 open_with_env(const char *env
, char *path
)
348 char *program
= getenv(env
);
351 strncpy(BUF1
, program
, BUFLEN
- 1);
352 strncat(BUF1
, " ", BUFLEN
- strlen(program
) - 1);
353 shell_escaped_cat(BUF1
, path
, BUFLEN
- strlen(program
) - 2);
354 spawn((char *[]) {RV_SHELL
, "-c", BUF1
, NULL
});
356 spawn((char *[]) {program
, path
, NULL
});
367 setlocale(LC_ALL
, "");
369 cbreak(); /* Get one character at a time. */
370 timeout(100); /* For getch(). */
372 nonl(); /* No NL->CR/NL on output. */
373 intrflush(stdscr
, FALSE
);
374 keypad(stdscr
, TRUE
);
375 curs_set(FALSE
); /* Hide blinking cursor. */
379 #ifdef NCURSES_EXT_FUNCS
380 use_default_colors();
385 init_pair(RED
, COLOR_RED
, bg
);
386 init_pair(GREEN
, COLOR_GREEN
, bg
);
387 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
388 init_pair(BLUE
, COLOR_BLUE
, bg
);
389 init_pair(CYAN
, COLOR_CYAN
, bg
);
390 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
391 init_pair(WHITE
, COLOR_WHITE
, bg
);
392 init_pair(BLACK
, COLOR_BLACK
, bg
);
394 atexit((void (*)(void)) endwin
);
398 /* Update the listing view. */
407 mvhline(0, 0, ' ', COLS
);
408 attr_on(A_BOLD
, NULL
);
409 color_set(RVC_TABNUM
, NULL
);
410 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
411 attr_off(A_BOLD
, NULL
);
412 if (rover
.marks
.nentries
) {
413 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
414 color_set(RVC_MARKS
, NULL
);
415 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
418 color_set(RVC_CWD
, NULL
);
419 mbstowcs(WBUF
, CWD
, PATH_MAX
);
420 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
421 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
422 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
423 ESEL
= MAX(MIN(ESEL
, rover
.nfiles
- 1), 0);
424 /* Selection might not be visible, due to cursor wrapping or window
425 shrinking. In that case, the scroll must be moved to make it visible. */
426 if (rover
.nfiles
> HEIGHT
) {
427 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
428 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
431 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
432 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
433 ishidden
= ENAME(j
)[0] == '.';
435 wattr_on(rover
.window
, A_REVERSE
, NULL
);
437 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
439 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
440 else if (S_ISREG(EMODE(j
))) {
441 if (EMODE(j
) & (S_IXUSR
| S_IXGRP
| S_IXOTH
))
442 wcolor_set(rover
.window
, RVC_EXEC
, NULL
);
444 wcolor_set(rover
.window
, RVC_REG
, NULL
);
445 } else if (S_ISDIR(EMODE(j
)))
446 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
447 else if (S_ISCHR(EMODE(j
)))
448 wcolor_set(rover
.window
, RVC_CHR
, NULL
);
449 else if (S_ISBLK(EMODE(j
)))
450 wcolor_set(rover
.window
, RVC_BLK
, NULL
);
451 else if (S_ISFIFO(EMODE(j
)))
452 wcolor_set(rover
.window
, RVC_FIFO
, NULL
);
453 else if (S_ISSOCK(EMODE(j
)))
454 wcolor_set(rover
.window
, RVC_SOCK
, NULL
);
455 if (S_ISDIR(EMODE(j
))) {
456 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
460 char *suffix
, *suffixes
= "BKMGTPEZY";
461 off_t human_size
= ESIZE(j
) * 10;
462 int length
= mbstowcs(NULL
, ENAME(j
), 0);
463 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
464 human_size
= (human_size
+ 512) / 1024;
466 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
467 (int) (COLS
- length
- 6),
468 (int) human_size
/ 10, *suffix
);
470 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
471 (int) (COLS
- length
- 8),
472 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
474 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
475 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
476 if (marking
&& MARKED(j
)) {
477 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
478 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
480 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
482 wattr_off(rover
.window
, A_REVERSE
, NULL
);
484 for (; i
< HEIGHT
; i
++)
485 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
486 if (rover
.nfiles
> HEIGHT
) {
488 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
489 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
490 if (!height
) height
= 1;
491 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
492 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
494 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
495 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
496 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
500 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
501 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
502 color_set(RVC_STATUS
, NULL
);
503 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
504 wrefresh(rover
.window
);
507 /* Show a message on the status bar. */
509 message(Color color
, char *fmt
, ...)
515 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
518 pos
= (STATUSPOS
- len
) / 2;
519 attr_on(A_BOLD
, NULL
);
520 color_set(color
, NULL
);
521 mvaddstr(LINES
- 1, pos
, BUF1
);
522 color_set(DEFAULT
, NULL
);
523 attr_off(A_BOLD
, NULL
);
526 /* Clear message area, leaving only status info. */
530 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
533 /* Comparison used to sort listing entries. */
535 rowcmp(const void *a
, const void *b
)
537 int isdir1
, isdir2
, cmpdir
;
540 isdir1
= S_ISDIR(r1
->mode
);
541 isdir2
= S_ISDIR(r2
->mode
);
542 cmpdir
= isdir2
- isdir1
;
543 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
546 /* Get all entries in current working directory. */
548 ls(Row
**rowsp
, uint8_t flags
)
556 if(!(dp
= opendir("."))) return -1;
557 n
= -2; /* We don't want the entries "." and "..". */
558 while (readdir(dp
)) n
++;
560 rows
= malloc(n
* sizeof *rows
);
562 while ((ep
= readdir(dp
))) {
563 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
565 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
567 lstat(ep
->d_name
, &statbuf
);
568 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
569 stat(ep
->d_name
, &statbuf
);
570 if (S_ISDIR(statbuf
.st_mode
)) {
571 if (flags
& SHOW_DIRS
) {
572 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
573 strcpy(rows
[i
].name
, ep
->d_name
);
575 strcat(rows
[i
].name
, "/");
576 rows
[i
].mode
= statbuf
.st_mode
;
579 } else if (flags
& SHOW_FILES
) {
580 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
581 strcpy(rows
[i
].name
, ep
->d_name
);
582 rows
[i
].size
= statbuf
.st_size
;
583 rows
[i
].mode
= statbuf
.st_mode
;
587 n
= i
; /* Ignore unused space in array caused by filters. */
588 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
595 free_rows(Row
**rowsp
, int nfiles
)
599 for (i
= 0; i
< nfiles
; i
++)
600 free((*rowsp
)[i
].name
);
605 /* Change working directory to the path in CWD. */
611 message(CYAN
, "Loading...");
613 if (reset
) ESEL
= SCROLL
= 0;
616 free_rows(&rover
.rows
, rover
.nfiles
);
617 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
618 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
619 for (i
= 0; i
< rover
.nfiles
; i
++) {
620 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
622 rover
.marks
.entries
[j
] &&
623 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
626 MARKED(i
) = j
< rover
.marks
.bulk
;
629 for (i
= 0; i
< rover
.nfiles
; i
++)
635 /* Select a target entry, if it is present. */
637 try_to_sel(const char *target
)
641 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
643 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
647 /* Reload CWD, but try to keep selection. */
652 strcpy(INPUT
, ENAME(ESEL
));
661 count_dir(const char *path
)
666 char subpath
[PATH_MAX
];
669 if(!(dp
= opendir(path
))) return 0;
671 while ((ep
= readdir(dp
))) {
672 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
674 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
675 lstat(subpath
, &statbuf
);
676 if (S_ISDIR(statbuf
.st_mode
)) {
677 strcat(subpath
, "/");
678 total
+= count_dir(subpath
);
680 total
+= statbuf
.st_size
;
695 chdir(rover
.marks
.dirpath
);
696 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
697 entry
= rover
.marks
.entries
[i
];
700 total
+= count_dir(entry
);
702 lstat(entry
, &statbuf
);
703 total
+= statbuf
.st_size
;
711 /* Recursively process a source directory using CWD as destination root.
712 For each node (i.e. directory), do the following:
713 1. call pre(destination);
714 2. call proc() on every child leaf (i.e. files);
715 3. recurse into every child node;
717 E.g. to move directory /src/ (and all its contents) inside /dst/:
718 strcpy(CWD, "/dst/");
719 process_dir(adddir, movfile, deldir, "/src/"); */
721 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
727 char subpath
[PATH_MAX
];
731 char dstpath
[PATH_MAX
];
732 strcpy(dstpath
, CWD
);
733 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
736 if(!(dp
= opendir(path
))) return -1;
737 while ((ep
= readdir(dp
))) {
738 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
740 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
741 lstat(subpath
, &statbuf
);
742 if (S_ISDIR(statbuf
.st_mode
)) {
743 strcat(subpath
, "/");
744 ret
|= process_dir(pre
, proc
, pos
, subpath
);
746 ret
|= proc(subpath
);
749 if (pos
) ret
|= pos(path
);
753 /* Process all marked entries using CWD as destination root.
754 All marked entries that are directories will be recursively processed.
755 See process_dir() for details on the parameters. */
757 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
758 const char *msg_doing
, const char *msg_done
)
765 message(CYAN
, "%s...", msg_doing
);
767 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
768 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
769 entry
= rover
.marks
.entries
[i
];
772 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
774 if (!strncmp(path
, CWD
, strlen(path
)))
777 ret
= process_dir(pre
, proc
, pos
, path
);
781 del_mark(&rover
.marks
, entry
);
786 rover
.prog
.total
= 0;
788 if (!rover
.marks
.nentries
)
789 message(GREEN
, "%s all marked entries.", msg_done
);
791 message(RED
, "Some errors occured while %s.", msg_doing
);
796 update_progress(off_t delta
)
800 if (!rover
.prog
.total
) return;
801 rover
.prog
.partial
+= delta
;
802 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
803 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
807 /* Wrappers for file operations. */
808 static int delfile(const char *path
) {
812 ret
= lstat(path
, &st
);
813 if (ret
< 0) return ret
;
814 update_progress(st
.st_size
);
817 static PROCESS deldir
= rmdir
;
818 static int addfile(const char *path
) {
819 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
822 ret
= creat(path
, 0644);
823 if (ret
< 0) return ret
;
826 static int cpyfile(const char *srcpath
) {
831 char dstpath
[PATH_MAX
];
833 strcpy(dstpath
, CWD
);
834 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
835 ret
= lstat(srcpath
, &st
);
836 if (ret
< 0) return ret
;
837 if (S_ISLNK(st
.st_mode
)) {
838 ret
= readlink(srcpath
, BUF1
, BUFLEN
-1);
839 if (ret
< 0) return ret
;
841 ret
= symlink(BUF1
, dstpath
);
843 ret
= src
= open(srcpath
, O_RDONLY
);
844 if (ret
< 0) return ret
;
845 ret
= dst
= creat(dstpath
, st
.st_mode
);
846 if (ret
< 0) return ret
;
847 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
848 write(dst
, buf
, size
);
849 update_progress(size
);
858 static int adddir(const char *path
) {
862 ret
= stat(CWD
, &st
);
863 if (ret
< 0) return ret
;
864 return mkdir(path
, st
.st_mode
);
866 static int movfile(const char *srcpath
) {
869 char dstpath
[PATH_MAX
];
871 strcpy(dstpath
, CWD
);
872 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
873 ret
= rename(srcpath
, dstpath
);
875 ret
= lstat(dstpath
, &st
);
876 if (ret
< 0) return ret
;
877 update_progress(st
.st_size
);
878 } else if (errno
== EXDEV
) {
879 ret
= cpyfile(srcpath
);
880 if (ret
< 0) return ret
;
881 ret
= unlink(srcpath
);
887 start_line_edit(const char *init_input
)
890 strncpy(INPUT
, init_input
, BUFLEN
);
891 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
892 rover
.edit
.right
= BUFLEN
- 1;
893 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
894 rover
.edit_scroll
= 0;
897 /* Read input and change editing state accordingly. */
901 wchar_t eraser
, killer
, wch
;
904 ret
= rover_get_wch((wint_t *) &wch
);
907 if (ret
== KEY_CODE_YES
) {
908 if (wch
== KEY_ENTER
) {
911 } else if (wch
== KEY_LEFT
) {
912 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
913 } else if (wch
== KEY_RIGHT
) {
914 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
915 } else if (wch
== KEY_UP
) {
916 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
917 } else if (wch
== KEY_DOWN
) {
918 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
919 } else if (wch
== KEY_BACKSPACE
) {
920 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
921 } else if (wch
== KEY_DC
) {
922 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
925 if (wch
== L
'\r' || wch
== L
'\n') {
928 } else if (wch
== L
'\t') {
931 } else if (wch
== eraser
) {
932 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
933 } else if (wch
== killer
) {
934 EDIT_CLEAR(rover
.edit
);
936 } else if (iswprint(wch
)) {
937 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
940 /* Encode edit contents in INPUT. */
941 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
942 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
943 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
948 /* Update line input on the screen. */
950 update_input(const char *prompt
, Color color
)
952 int plen
, ilen
, maxlen
;
954 plen
= strlen(prompt
);
955 ilen
= mbstowcs(NULL
, INPUT
, 0);
956 maxlen
= STATUSPOS
- plen
- 2;
957 if (ilen
- rover
.edit_scroll
< maxlen
)
958 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
959 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
960 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
961 else if (rover
.edit
.left
< rover
.edit_scroll
)
962 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
963 color_set(RVC_PROMPT
, NULL
);
964 mvaddstr(LINES
- 1, 0, prompt
);
965 color_set(color
, NULL
);
966 mbstowcs(WBUF
, INPUT
, COLS
);
967 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
968 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
969 color_set(DEFAULT
, NULL
);
970 if (rover
.edit_scroll
)
971 mvaddch(LINES
- 1, plen
- 1, '<');
972 if (ilen
> rover
.edit_scroll
+ maxlen
)
973 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
974 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
978 main(int argc
, char *argv
[])
986 FILE *save_cwd_file
= NULL
;
987 FILE *save_marks_file
= NULL
;
990 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
991 printf("rover %s\n", RV_VERSION
);
993 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
995 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
996 " Browse current directory or the ones specified.\n\n"
997 " or: rover -h|--help\n"
998 " Print this help message and exit.\n\n"
999 " or: rover -v|--version\n"
1000 " Print program version and exit.\n\n"
1001 "See rover(1) for more information.\n"
1002 "Rover homepage: <https://github.com/lecram/rover>.\n"
1005 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
1007 save_cwd_file
= fopen(argv
[2], "w");
1008 argc
-= 2; argv
+= 2;
1010 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1013 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
1015 save_marks_file
= fopen(argv
[2], "a");
1016 argc
-= 2; argv
+= 2;
1018 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1025 for (i
= 0; i
< 10; i
++) {
1026 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
1027 rover
.tabs
[i
].flags
= SHOW_FILES
| SHOW_DIRS
;
1029 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
1030 for (i
= 1; i
< argc
&& i
< 10; i
++) {
1031 if ((d
= opendir(argv
[i
]))) {
1032 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
1035 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
1037 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
1038 for (i
++; i
< 10; i
++)
1039 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
1040 for (i
= 0; i
< 10; i
++)
1041 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
1042 strcat(rover
.tabs
[i
].cwd
, "/");
1044 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
1045 init_marks(&rover
.marks
);
1051 if (!strcmp(key
, RVK_QUIT
)) break;
1052 else if (ch
>= '0' && ch
<= '9') {
1053 rover
.tab
= ch
- '0';
1055 } else if (!strcmp(key
, RVK_HELP
)) {
1056 spawn((char *[]) {"man", "rover", NULL
});
1057 } else if (!strcmp(key
, RVK_DOWN
)) {
1058 if (!rover
.nfiles
) continue;
1059 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
1061 } else if (!strcmp(key
, RVK_UP
)) {
1062 if (!rover
.nfiles
) continue;
1063 ESEL
= MAX(ESEL
- 1, 0);
1065 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
1066 if (!rover
.nfiles
) continue;
1067 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
1068 if (rover
.nfiles
> HEIGHT
)
1069 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
1071 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
1072 if (!rover
.nfiles
) continue;
1073 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
1074 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
1076 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1077 if (!rover
.nfiles
) continue;
1080 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1081 if (!rover
.nfiles
) continue;
1082 ESEL
= rover
.nfiles
- 1;
1084 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1085 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1086 if (chdir(ENAME(ESEL
)) == -1) {
1087 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1090 strcat(CWD
, ENAME(ESEL
));
1092 } else if (!strcmp(key
, RVK_CD_UP
)) {
1093 char *dirname
, first
;
1094 if (!strcmp(CWD
, "/")) continue;
1095 CWD
[strlen(CWD
) - 1] = '\0';
1096 dirname
= strrchr(CWD
, '/') + 1;
1101 dirname
[strlen(dirname
)] = '/';
1102 try_to_sel(dirname
);
1104 if (rover
.nfiles
> HEIGHT
)
1105 SCROLL
= ESEL
- HEIGHT
/ 2;
1107 } else if (!strcmp(key
, RVK_HOME
)) {
1108 strcpy(CWD
, getenv("HOME"));
1109 if (CWD
[strlen(CWD
) - 1] != '/')
1112 } else if (!strcmp(key
, RVK_TARGET
)) {
1114 int is_dir
= S_ISDIR(EMODE(ESEL
));
1115 ssize_t len
= readlink(ENAME(ESEL
), BUF1
, BUFLEN
-1);
1116 if (len
== -1) continue;
1118 if (access(BUF1
, F_OK
) == -1) {
1122 msg
= "Cannot access \"%s\".";
1125 msg
= "\"%s\" does not exist.";
1128 msg
= "Cannot navigate to \"%s\".";
1130 strcpy(BUF2
, BUF1
); /* message() uses BUF1. */
1131 message(RED
, msg
, BUF2
);
1134 realpath(BUF1
, CWD
);
1136 if (CWD
[len
- 1] == '/')
1137 CWD
[len
- 1] = '\0';
1138 bname
= strrchr(CWD
, '/') + 1;
1148 } else if (!strcmp(key
, RVK_REFRESH
)) {
1150 } else if (!strcmp(key
, RVK_SHELL
)) {
1151 program
= getenv("SHELL");
1154 spawn((char *[]) {RV_SHELL
, "-c", program
, NULL
});
1156 spawn((char *[]) {program
, NULL
});
1160 } else if (!strcmp(key
, RVK_VIEW
)) {
1161 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1162 if (open_with_env("PAGER", ENAME(ESEL
)))
1164 } else if (!strcmp(key
, RVK_EDIT
)) {
1165 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1166 if (open_with_env("EDITOR", ENAME(ESEL
)))
1168 } else if (!strcmp(key
, RVK_OPEN
)) {
1169 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1170 if (open_with_env("ROVER_OPEN", ENAME(ESEL
)))
1172 } else if (!strcmp(key
, RVK_SEARCH
)) {
1173 int oldsel
, oldscroll
, length
;
1174 if (!rover
.nfiles
) continue;
1177 start_line_edit("");
1178 update_input(RVP_SEARCH
, RED
);
1179 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1182 length
= strlen(INPUT
);
1184 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1185 if (!strncmp(ENAME(sel
), INPUT
, length
))
1187 if (sel
< rover
.nfiles
) {
1190 if (rover
.nfiles
> HEIGHT
) {
1193 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1194 SCROLL
= rover
.nfiles
- HEIGHT
;
1204 update_input(RVP_SEARCH
, color
);
1206 if (edit_stat
== CANCEL
) {
1212 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1213 FLAGS
^= SHOW_FILES
;
1215 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1218 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1219 FLAGS
^= SHOW_HIDDEN
;
1221 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1223 start_line_edit("");
1224 update_input(RVP_NEW_FILE
, RED
);
1225 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1226 int length
= strlen(INPUT
);
1228 for (i
= 0; i
< rover
.nfiles
; i
++) {
1230 !strncmp(ENAME(i
), INPUT
, length
) &&
1231 (!strcmp(ENAME(i
) + length
, "") ||
1232 !strcmp(ENAME(i
) + length
, "/"))
1238 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1241 if (edit_stat
== CONFIRM
) {
1243 if (addfile(INPUT
) == 0) {
1248 message(RED
, "Could not create \"%s\".", INPUT
);
1250 message(RED
, "\"%s\" already exists.", INPUT
);
1252 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1254 start_line_edit("");
1255 update_input(RVP_NEW_DIR
, RED
);
1256 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1257 int length
= strlen(INPUT
);
1259 for (i
= 0; i
< rover
.nfiles
; i
++) {
1261 !strncmp(ENAME(i
), INPUT
, length
) &&
1262 (!strcmp(ENAME(i
) + length
, "") ||
1263 !strcmp(ENAME(i
) + length
, "/"))
1269 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1272 if (edit_stat
== CONFIRM
) {
1274 if (adddir(INPUT
) == 0) {
1280 message(RED
, "Could not create \"%s/\".", INPUT
);
1282 message(RED
, "\"%s\" already exists.", INPUT
);
1284 } else if (!strcmp(key
, RVK_RENAME
)) {
1288 strcpy(INPUT
, ENAME(ESEL
));
1289 last
= INPUT
+ strlen(INPUT
) - 1;
1290 if ((isdir
= *last
== '/'))
1292 start_line_edit(INPUT
);
1293 update_input(RVP_RENAME
, RED
);
1294 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1295 int length
= strlen(INPUT
);
1297 for (i
= 0; i
< rover
.nfiles
; i
++)
1299 !strncmp(ENAME(i
), INPUT
, length
) &&
1300 (!strcmp(ENAME(i
) + length
, "") ||
1301 !strcmp(ENAME(i
) + length
, "/"))
1306 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1309 if (edit_stat
== CONFIRM
) {
1313 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1314 del_mark(&rover
.marks
, ENAME(ESEL
));
1315 add_mark(&rover
.marks
, CWD
, INPUT
);
1321 message(RED
, "\"%s\" already exists.", INPUT
);
1323 } else if (!strcmp(key
, RVK_DELETE
)) {
1325 message(YELLOW
, "Delete \"%s\"? (Y/n)", ENAME(ESEL
));
1326 if (rover_getch() == 'Y') {
1327 const char *name
= ENAME(ESEL
);
1328 int ret
= ISDIR(ENAME(ESEL
)) ? deldir(name
) : delfile(name
);
1331 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1335 message(RED
, "No entry selected for deletion.");
1336 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1338 del_mark(&rover
.marks
, ENAME(ESEL
));
1340 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1341 MARKED(ESEL
) = !MARKED(ESEL
);
1342 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1344 } else if (!strcmp(key
, RVK_INVMARK
)) {
1345 for (i
= 0; i
< rover
.nfiles
; i
++) {
1347 del_mark(&rover
.marks
, ENAME(i
));
1349 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1350 MARKED(i
) = !MARKED(i
);
1353 } else if (!strcmp(key
, RVK_MARKALL
)) {
1354 for (i
= 0; i
< rover
.nfiles
; i
++)
1356 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1360 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1361 if (rover
.marks
.nentries
) {
1362 message(YELLOW
, "Delete all marked entries? (Y/n)");
1363 if (rover_getch() == 'Y')
1364 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1368 message(RED
, "No entries marked for deletion.");
1369 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1370 if (rover
.marks
.nentries
)
1371 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1373 message(RED
, "No entries marked for copying.");
1374 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1375 if (rover
.marks
.nentries
)
1376 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1378 message(RED
, "No entries marked for moving.");
1382 free_rows(&rover
.rows
, rover
.nfiles
);
1383 delwin(rover
.window
);
1384 if (save_cwd_file
!= NULL
) {
1385 fputs(CWD
, save_cwd_file
);
1386 fclose(save_cwd_file
);
1388 if (save_marks_file
!= NULL
) {
1389 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
1390 entry
= rover
.marks
.entries
[i
];
1392 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1394 fclose(save_marks_file
);
1396 free_marks(&rover
.marks
);