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 open_with_env(const char *env
, char *path
)
323 char *program
= getenv(env
);
326 strncpy(BUF1
, program
, BUFLEN
- 1);
327 strncat(BUF1
, " '", BUFLEN
- strlen(program
) - 1);
328 strncat(BUF1
, path
, BUFLEN
- strlen(program
) - 3);
329 strncat(BUF1
, "'", BUFLEN
- strlen(program
) - strlen(path
) - 3);
330 spawn((char *[]) {RV_SHELL
, "-c", BUF1
, NULL
});
332 spawn((char *[]) {program
, path
, NULL
});
343 setlocale(LC_ALL
, "");
345 cbreak(); /* Get one character at a time. */
346 timeout(100); /* For getch(). */
348 nonl(); /* No NL->CR/NL on output. */
349 intrflush(stdscr
, FALSE
);
350 keypad(stdscr
, TRUE
);
351 curs_set(FALSE
); /* Hide blinking cursor. */
355 #ifdef NCURSES_EXT_FUNCS
356 use_default_colors();
361 init_pair(RED
, COLOR_RED
, bg
);
362 init_pair(GREEN
, COLOR_GREEN
, bg
);
363 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
364 init_pair(BLUE
, COLOR_BLUE
, bg
);
365 init_pair(CYAN
, COLOR_CYAN
, bg
);
366 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
367 init_pair(WHITE
, COLOR_WHITE
, bg
);
368 init_pair(BLACK
, COLOR_BLACK
, bg
);
370 atexit((void (*)(void)) endwin
);
374 /* Update the listing view. */
383 mvhline(0, 0, ' ', COLS
);
384 attr_on(A_BOLD
, NULL
);
385 color_set(RVC_TABNUM
, NULL
);
386 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
387 attr_off(A_BOLD
, NULL
);
388 if (rover
.marks
.nentries
) {
389 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
390 color_set(RVC_MARKS
, NULL
);
391 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
394 color_set(RVC_CWD
, NULL
);
395 mbstowcs(WBUF
, CWD
, PATH_MAX
);
396 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
397 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
398 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
399 ESEL
= MAX(MIN(ESEL
, rover
.nfiles
- 1), 0);
400 /* Selection might not be visible, due to cursor wrapping or window
401 shrinking. In that case, the scroll must be moved to make it visible. */
402 if (rover
.nfiles
> HEIGHT
) {
403 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
404 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
407 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
408 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
409 ishidden
= ENAME(j
)[0] == '.';
411 wattr_on(rover
.window
, A_REVERSE
, NULL
);
413 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
415 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
416 else if (S_ISREG(EMODE(j
))) {
417 if (EMODE(j
) & (S_IXUSR
| S_IXGRP
| S_IXOTH
))
418 wcolor_set(rover
.window
, RVC_EXEC
, NULL
);
420 wcolor_set(rover
.window
, RVC_REG
, NULL
);
421 } else if (S_ISDIR(EMODE(j
)))
422 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
423 else if (S_ISCHR(EMODE(j
)))
424 wcolor_set(rover
.window
, RVC_CHR
, NULL
);
425 else if (S_ISBLK(EMODE(j
)))
426 wcolor_set(rover
.window
, RVC_BLK
, NULL
);
427 else if (S_ISFIFO(EMODE(j
)))
428 wcolor_set(rover
.window
, RVC_FIFO
, NULL
);
429 else if (S_ISSOCK(EMODE(j
)))
430 wcolor_set(rover
.window
, RVC_SOCK
, NULL
);
431 if (S_ISDIR(EMODE(j
))) {
432 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
436 char *suffix
, *suffixes
= "BKMGTPEZY";
437 off_t human_size
= ESIZE(j
) * 10;
438 int length
= mbstowcs(NULL
, ENAME(j
), 0);
439 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
440 human_size
= (human_size
+ 512) / 1024;
442 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
443 (int) (COLS
- length
- 6),
444 (int) human_size
/ 10, *suffix
);
446 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
447 (int) (COLS
- length
- 8),
448 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
450 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
451 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
452 if (marking
&& MARKED(j
)) {
453 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
454 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
456 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
458 wattr_off(rover
.window
, A_REVERSE
, NULL
);
460 for (; i
< HEIGHT
; i
++)
461 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
462 if (rover
.nfiles
> HEIGHT
) {
464 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
465 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
466 if (!height
) height
= 1;
467 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
468 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
470 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
471 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
472 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
476 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
477 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
478 color_set(RVC_STATUS
, NULL
);
479 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
480 wrefresh(rover
.window
);
483 /* Show a message on the status bar. */
485 message(Color color
, char *fmt
, ...)
491 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
494 pos
= (STATUSPOS
- len
) / 2;
495 attr_on(A_BOLD
, NULL
);
496 color_set(color
, NULL
);
497 mvaddstr(LINES
- 1, pos
, BUF1
);
498 color_set(DEFAULT
, NULL
);
499 attr_off(A_BOLD
, NULL
);
502 /* Clear message area, leaving only status info. */
506 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
509 /* Comparison used to sort listing entries. */
511 rowcmp(const void *a
, const void *b
)
513 int isdir1
, isdir2
, cmpdir
;
516 isdir1
= S_ISDIR(r1
->mode
);
517 isdir2
= S_ISDIR(r2
->mode
);
518 cmpdir
= isdir2
- isdir1
;
519 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
522 /* Get all entries in current working directory. */
524 ls(Row
**rowsp
, uint8_t flags
)
532 if(!(dp
= opendir("."))) return -1;
533 n
= -2; /* We don't want the entries "." and "..". */
534 while (readdir(dp
)) n
++;
536 rows
= malloc(n
* sizeof *rows
);
538 while ((ep
= readdir(dp
))) {
539 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
541 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
543 lstat(ep
->d_name
, &statbuf
);
544 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
545 stat(ep
->d_name
, &statbuf
);
546 if (S_ISDIR(statbuf
.st_mode
)) {
547 if (flags
& SHOW_DIRS
) {
548 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
549 strcpy(rows
[i
].name
, ep
->d_name
);
551 strcat(rows
[i
].name
, "/");
552 rows
[i
].mode
= statbuf
.st_mode
;
555 } else if (flags
& SHOW_FILES
) {
556 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
557 strcpy(rows
[i
].name
, ep
->d_name
);
558 rows
[i
].size
= statbuf
.st_size
;
559 rows
[i
].mode
= statbuf
.st_mode
;
563 n
= i
; /* Ignore unused space in array caused by filters. */
564 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
571 free_rows(Row
**rowsp
, int nfiles
)
575 for (i
= 0; i
< nfiles
; i
++)
576 free((*rowsp
)[i
].name
);
581 /* Change working directory to the path in CWD. */
587 message(CYAN
, "Loading...");
589 if (reset
) ESEL
= SCROLL
= 0;
592 free_rows(&rover
.rows
, rover
.nfiles
);
593 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
594 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
595 for (i
= 0; i
< rover
.nfiles
; i
++) {
596 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
598 rover
.marks
.entries
[j
] &&
599 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
602 MARKED(i
) = j
< rover
.marks
.bulk
;
605 for (i
= 0; i
< rover
.nfiles
; i
++)
611 /* Select a target entry, if it is present. */
613 try_to_sel(const char *target
)
617 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
619 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
623 /* Reload CWD, but try to keep selection. */
628 strcpy(INPUT
, ENAME(ESEL
));
637 count_dir(const char *path
)
642 char subpath
[PATH_MAX
];
645 if(!(dp
= opendir(path
))) return 0;
647 while ((ep
= readdir(dp
))) {
648 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
650 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
651 lstat(subpath
, &statbuf
);
652 if (S_ISDIR(statbuf
.st_mode
)) {
653 strcat(subpath
, "/");
654 total
+= count_dir(subpath
);
656 total
+= statbuf
.st_size
;
671 chdir(rover
.marks
.dirpath
);
672 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
673 entry
= rover
.marks
.entries
[i
];
676 total
+= count_dir(entry
);
678 lstat(entry
, &statbuf
);
679 total
+= statbuf
.st_size
;
687 /* Recursively process a source directory using CWD as destination root.
688 For each node (i.e. directory), do the following:
689 1. call pre(destination);
690 2. call proc() on every child leaf (i.e. files);
691 3. recurse into every child node;
693 E.g. to move directory /src/ (and all its contents) inside /dst/:
694 strcpy(CWD, "/dst/");
695 process_dir(adddir, movfile, deldir, "/src/"); */
697 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
703 char subpath
[PATH_MAX
];
707 char dstpath
[PATH_MAX
];
708 strcpy(dstpath
, CWD
);
709 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
712 if(!(dp
= opendir(path
))) return -1;
713 while ((ep
= readdir(dp
))) {
714 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
716 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
717 lstat(subpath
, &statbuf
);
718 if (S_ISDIR(statbuf
.st_mode
)) {
719 strcat(subpath
, "/");
720 ret
|= process_dir(pre
, proc
, pos
, subpath
);
722 ret
|= proc(subpath
);
725 if (pos
) ret
|= pos(path
);
729 /* Process all marked entries using CWD as destination root.
730 All marked entries that are directories will be recursively processed.
731 See process_dir() for details on the parameters. */
733 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
734 const char *msg_doing
, const char *msg_done
)
741 message(CYAN
, "%s...", msg_doing
);
743 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
744 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
745 entry
= rover
.marks
.entries
[i
];
748 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
750 if (!strncmp(path
, CWD
, strlen(path
)))
753 ret
= process_dir(pre
, proc
, pos
, path
);
757 del_mark(&rover
.marks
, entry
);
762 rover
.prog
.total
= 0;
764 if (!rover
.marks
.nentries
)
765 message(GREEN
, "%s all marked entries.", msg_done
);
767 message(RED
, "Some errors occured while %s.", msg_doing
);
772 update_progress(off_t delta
)
776 if (!rover
.prog
.total
) return;
777 rover
.prog
.partial
+= delta
;
778 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
779 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
783 /* Wrappers for file operations. */
784 static int delfile(const char *path
) {
788 ret
= lstat(path
, &st
);
789 if (ret
< 0) return ret
;
790 update_progress(st
.st_size
);
793 static PROCESS deldir
= rmdir
;
794 static int addfile(const char *path
) {
795 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
798 ret
= creat(path
, 0644);
799 if (ret
< 0) return ret
;
802 static int cpyfile(const char *srcpath
) {
807 char dstpath
[PATH_MAX
];
809 strcpy(dstpath
, CWD
);
810 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
811 ret
= lstat(srcpath
, &st
);
812 if (ret
< 0) return ret
;
813 if (S_ISLNK(st
.st_mode
)) {
814 ret
= readlink(srcpath
, BUF1
, BUFLEN
-1);
815 if (ret
< 0) return ret
;
817 ret
= symlink(BUF1
, dstpath
);
819 ret
= src
= open(srcpath
, O_RDONLY
);
820 if (ret
< 0) return ret
;
821 ret
= dst
= creat(dstpath
, st
.st_mode
);
822 if (ret
< 0) return ret
;
823 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
824 write(dst
, buf
, size
);
825 update_progress(size
);
834 static int adddir(const char *path
) {
838 ret
= stat(CWD
, &st
);
839 if (ret
< 0) return ret
;
840 return mkdir(path
, st
.st_mode
);
842 static int movfile(const char *srcpath
) {
845 char dstpath
[PATH_MAX
];
847 strcpy(dstpath
, CWD
);
848 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
849 ret
= rename(srcpath
, dstpath
);
851 ret
= lstat(dstpath
, &st
);
852 if (ret
< 0) return ret
;
853 update_progress(st
.st_size
);
854 } else if (errno
== EXDEV
) {
855 ret
= cpyfile(srcpath
);
856 if (ret
< 0) return ret
;
857 ret
= unlink(srcpath
);
863 start_line_edit(const char *init_input
)
866 strncpy(INPUT
, init_input
, BUFLEN
);
867 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
868 rover
.edit
.right
= BUFLEN
- 1;
869 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
870 rover
.edit_scroll
= 0;
873 /* Read input and change editing state accordingly. */
877 wchar_t eraser
, killer
, wch
;
880 ret
= rover_get_wch((wint_t *) &wch
);
883 if (ret
== KEY_CODE_YES
) {
884 if (wch
== KEY_ENTER
) {
887 } else if (wch
== KEY_LEFT
) {
888 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
889 } else if (wch
== KEY_RIGHT
) {
890 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
891 } else if (wch
== KEY_UP
) {
892 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
893 } else if (wch
== KEY_DOWN
) {
894 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
895 } else if (wch
== KEY_BACKSPACE
) {
896 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
897 } else if (wch
== KEY_DC
) {
898 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
901 if (wch
== L
'\r' || wch
== L
'\n') {
904 } else if (wch
== L
'\t') {
907 } else if (wch
== eraser
) {
908 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
909 } else if (wch
== killer
) {
910 EDIT_CLEAR(rover
.edit
);
912 } else if (iswprint(wch
)) {
913 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
916 /* Encode edit contents in INPUT. */
917 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
918 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
919 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
924 /* Update line input on the screen. */
926 update_input(const char *prompt
, Color color
)
928 int plen
, ilen
, maxlen
;
930 plen
= strlen(prompt
);
931 ilen
= mbstowcs(NULL
, INPUT
, 0);
932 maxlen
= STATUSPOS
- plen
- 2;
933 if (ilen
- rover
.edit_scroll
< maxlen
)
934 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
935 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
936 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
937 else if (rover
.edit
.left
< rover
.edit_scroll
)
938 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
939 color_set(RVC_PROMPT
, NULL
);
940 mvaddstr(LINES
- 1, 0, prompt
);
941 color_set(color
, NULL
);
942 mbstowcs(WBUF
, INPUT
, COLS
);
943 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
944 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
945 color_set(DEFAULT
, NULL
);
946 if (rover
.edit_scroll
)
947 mvaddch(LINES
- 1, plen
- 1, '<');
948 if (ilen
> rover
.edit_scroll
+ maxlen
)
949 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
950 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
954 main(int argc
, char *argv
[])
962 FILE *save_cwd_file
= NULL
;
963 FILE *save_marks_file
= NULL
;
966 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
967 printf("rover %s\n", RV_VERSION
);
969 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
971 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
972 " Browse current directory or the ones specified.\n\n"
973 " or: rover -h|--help\n"
974 " Print this help message and exit.\n\n"
975 " or: rover -v|--version\n"
976 " Print program version and exit.\n\n"
977 "See rover(1) for more information.\n"
978 "Rover homepage: <https://github.com/lecram/rover>.\n"
981 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
983 save_cwd_file
= fopen(argv
[2], "w");
984 argc
-= 2; argv
+= 2;
986 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
989 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
991 save_marks_file
= fopen(argv
[2], "a");
992 argc
-= 2; argv
+= 2;
994 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1001 for (i
= 0; i
< 10; i
++) {
1002 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
1003 rover
.tabs
[i
].flags
= SHOW_FILES
| SHOW_DIRS
;
1005 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
1006 for (i
= 1; i
< argc
&& i
< 10; i
++) {
1007 if ((d
= opendir(argv
[i
]))) {
1008 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
1011 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
1013 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
1014 for (i
++; i
< 10; i
++)
1015 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
1016 for (i
= 0; i
< 10; i
++)
1017 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
1018 strcat(rover
.tabs
[i
].cwd
, "/");
1020 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
1021 init_marks(&rover
.marks
);
1027 if (!strcmp(key
, RVK_QUIT
)) break;
1028 else if (ch
>= '0' && ch
<= '9') {
1029 rover
.tab
= ch
- '0';
1031 } else if (!strcmp(key
, RVK_HELP
)) {
1032 spawn((char *[]) {"man", "rover", NULL
});
1033 } else if (!strcmp(key
, RVK_DOWN
)) {
1034 if (!rover
.nfiles
) continue;
1035 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
1037 } else if (!strcmp(key
, RVK_UP
)) {
1038 if (!rover
.nfiles
) continue;
1039 ESEL
= MAX(ESEL
- 1, 0);
1041 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
1042 if (!rover
.nfiles
) continue;
1043 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
1044 if (rover
.nfiles
> HEIGHT
)
1045 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
1047 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
1048 if (!rover
.nfiles
) continue;
1049 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
1050 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
1052 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1053 if (!rover
.nfiles
) continue;
1056 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1057 if (!rover
.nfiles
) continue;
1058 ESEL
= rover
.nfiles
- 1;
1060 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1061 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1062 if (chdir(ENAME(ESEL
)) == -1) {
1063 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1066 strcat(CWD
, ENAME(ESEL
));
1068 } else if (!strcmp(key
, RVK_CD_UP
)) {
1069 char *dirname
, first
;
1070 if (!strcmp(CWD
, "/")) continue;
1071 CWD
[strlen(CWD
) - 1] = '\0';
1072 dirname
= strrchr(CWD
, '/') + 1;
1077 dirname
[strlen(dirname
)] = '/';
1078 try_to_sel(dirname
);
1080 if (rover
.nfiles
> HEIGHT
)
1081 SCROLL
= ESEL
- HEIGHT
/ 2;
1083 } else if (!strcmp(key
, RVK_HOME
)) {
1084 strcpy(CWD
, getenv("HOME"));
1085 if (CWD
[strlen(CWD
) - 1] != '/')
1088 } else if (!strcmp(key
, RVK_TARGET
)) {
1090 int is_dir
= S_ISDIR(EMODE(ESEL
));
1091 ssize_t len
= readlink(ENAME(ESEL
), BUF1
, BUFLEN
-1);
1092 if (len
== -1) continue;
1094 if (access(BUF1
, F_OK
) == -1) {
1098 msg
= "Cannot access \"%s\".";
1101 msg
= "\"%s\" does not exist.";
1104 msg
= "Cannot navigate to \"%s\".";
1106 strcpy(BUF2
, BUF1
); /* message() uses BUF1. */
1107 message(RED
, msg
, BUF2
);
1110 realpath(BUF1
, CWD
);
1112 if (CWD
[len
- 1] == '/')
1113 CWD
[len
- 1] = '\0';
1114 bname
= strrchr(CWD
, '/') + 1;
1124 } else if (!strcmp(key
, RVK_REFRESH
)) {
1126 } else if (!strcmp(key
, RVK_SHELL
)) {
1127 program
= getenv("SHELL");
1130 spawn((char *[]) {RV_SHELL
, "-c", program
, NULL
});
1132 spawn((char *[]) {program
, NULL
});
1136 } else if (!strcmp(key
, RVK_VIEW
)) {
1137 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1138 if (open_with_env("PAGER", ENAME(ESEL
)))
1140 } else if (!strcmp(key
, RVK_EDIT
)) {
1141 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1142 if (open_with_env("EDITOR", ENAME(ESEL
)))
1144 } else if (!strcmp(key
, RVK_OPEN
)) {
1145 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1146 if (open_with_env("ROVER_OPEN", ENAME(ESEL
)))
1148 } else if (!strcmp(key
, RVK_SEARCH
)) {
1149 int oldsel
, oldscroll
, length
;
1150 if (!rover
.nfiles
) continue;
1153 start_line_edit("");
1154 update_input(RVP_SEARCH
, RED
);
1155 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1158 length
= strlen(INPUT
);
1160 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1161 if (!strncmp(ENAME(sel
), INPUT
, length
))
1163 if (sel
< rover
.nfiles
) {
1166 if (rover
.nfiles
> HEIGHT
) {
1169 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1170 SCROLL
= rover
.nfiles
- HEIGHT
;
1180 update_input(RVP_SEARCH
, color
);
1182 if (edit_stat
== CANCEL
) {
1188 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1189 FLAGS
^= SHOW_FILES
;
1191 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1194 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1195 FLAGS
^= SHOW_HIDDEN
;
1197 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1199 start_line_edit("");
1200 update_input(RVP_NEW_FILE
, RED
);
1201 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1202 int length
= strlen(INPUT
);
1204 for (i
= 0; i
< rover
.nfiles
; i
++) {
1206 !strncmp(ENAME(i
), INPUT
, length
) &&
1207 (!strcmp(ENAME(i
) + length
, "") ||
1208 !strcmp(ENAME(i
) + length
, "/"))
1214 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1217 if (edit_stat
== CONFIRM
) {
1219 if (addfile(INPUT
) == 0) {
1224 message(RED
, "Could not create \"%s\".", INPUT
);
1226 message(RED
, "\"%s\" already exists.", INPUT
);
1228 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1230 start_line_edit("");
1231 update_input(RVP_NEW_DIR
, RED
);
1232 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1233 int length
= strlen(INPUT
);
1235 for (i
= 0; i
< rover
.nfiles
; i
++) {
1237 !strncmp(ENAME(i
), INPUT
, length
) &&
1238 (!strcmp(ENAME(i
) + length
, "") ||
1239 !strcmp(ENAME(i
) + length
, "/"))
1245 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1248 if (edit_stat
== CONFIRM
) {
1250 if (adddir(INPUT
) == 0) {
1256 message(RED
, "Could not create \"%s/\".", INPUT
);
1258 message(RED
, "\"%s\" already exists.", INPUT
);
1260 } else if (!strcmp(key
, RVK_RENAME
)) {
1264 strcpy(INPUT
, ENAME(ESEL
));
1265 last
= INPUT
+ strlen(INPUT
) - 1;
1266 if ((isdir
= *last
== '/'))
1268 start_line_edit(INPUT
);
1269 update_input(RVP_RENAME
, RED
);
1270 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1271 int length
= strlen(INPUT
);
1273 for (i
= 0; i
< rover
.nfiles
; i
++)
1275 !strncmp(ENAME(i
), INPUT
, length
) &&
1276 (!strcmp(ENAME(i
) + length
, "") ||
1277 !strcmp(ENAME(i
) + length
, "/"))
1282 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1285 if (edit_stat
== CONFIRM
) {
1289 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1290 del_mark(&rover
.marks
, ENAME(ESEL
));
1291 add_mark(&rover
.marks
, CWD
, INPUT
);
1297 message(RED
, "\"%s\" already exists.", INPUT
);
1299 } else if (!strcmp(key
, RVK_DELETE
)) {
1301 message(YELLOW
, "Delete \"%s\"? (Y/n)", ENAME(ESEL
));
1302 if (rover_getch() == 'Y') {
1303 const char *name
= ENAME(ESEL
);
1304 int ret
= ISDIR(ENAME(ESEL
)) ? deldir(name
) : delfile(name
);
1307 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1311 message(RED
, "No entry selected for deletion.");
1312 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1314 del_mark(&rover
.marks
, ENAME(ESEL
));
1316 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1317 MARKED(ESEL
) = !MARKED(ESEL
);
1318 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1320 } else if (!strcmp(key
, RVK_INVMARK
)) {
1321 for (i
= 0; i
< rover
.nfiles
; i
++) {
1323 del_mark(&rover
.marks
, ENAME(i
));
1325 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1326 MARKED(i
) = !MARKED(i
);
1329 } else if (!strcmp(key
, RVK_MARKALL
)) {
1330 for (i
= 0; i
< rover
.nfiles
; i
++)
1332 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1336 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1337 if (rover
.marks
.nentries
) {
1338 message(YELLOW
, "Delete all marked entries? (Y/n)");
1339 if (rover_getch() == 'Y')
1340 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1344 message(RED
, "No entries marked for deletion.");
1345 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1346 if (rover
.marks
.nentries
)
1347 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1349 message(RED
, "No entries marked for copying.");
1350 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1351 if (rover
.marks
.nentries
)
1352 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1354 message(RED
, "No entries marked for moving.");
1358 free_rows(&rover
.rows
, rover
.nfiles
);
1359 delwin(rover
.window
);
1360 if (save_cwd_file
!= NULL
) {
1361 fputs(CWD
, save_cwd_file
);
1362 fclose(save_cwd_file
);
1364 if (save_marks_file
!= NULL
) {
1365 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
1366 entry
= rover
.marks
.entries
[i
];
1368 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1370 fclose(save_marks_file
);
1372 free_marks(&rover
.marks
);