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
) - strlen(path
) - 2);
329 spawn((char *[]) {RV_SHELL
, "-c", BUF1
, NULL
});
331 spawn((char *[]) {program
, path
, NULL
});
342 setlocale(LC_ALL
, "");
344 cbreak(); /* Get one character at a time. */
345 timeout(100); /* For getch(). */
347 nonl(); /* No NL->CR/NL on output. */
348 intrflush(stdscr
, FALSE
);
349 keypad(stdscr
, TRUE
);
350 curs_set(FALSE
); /* Hide blinking cursor. */
354 #ifdef NCURSES_EXT_FUNCS
355 use_default_colors();
360 init_pair(RED
, COLOR_RED
, bg
);
361 init_pair(GREEN
, COLOR_GREEN
, bg
);
362 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
363 init_pair(BLUE
, COLOR_BLUE
, bg
);
364 init_pair(CYAN
, COLOR_CYAN
, bg
);
365 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
366 init_pair(WHITE
, COLOR_WHITE
, bg
);
367 init_pair(BLACK
, COLOR_BLACK
, bg
);
369 atexit((void (*)(void)) endwin
);
373 /* Update the listing view. */
382 mvhline(0, 0, ' ', COLS
);
383 attr_on(A_BOLD
, NULL
);
384 color_set(RVC_TABNUM
, NULL
);
385 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
386 attr_off(A_BOLD
, NULL
);
387 if (rover
.marks
.nentries
) {
388 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
389 color_set(RVC_MARKS
, NULL
);
390 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
393 color_set(RVC_CWD
, NULL
);
394 mbstowcs(WBUF
, CWD
, PATH_MAX
);
395 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
396 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
397 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
398 ESEL
= MAX(MIN(ESEL
, rover
.nfiles
- 1), 0);
399 /* Selection might not be visible, due to cursor wrapping or window
400 shrinking. In that case, the scroll must be moved to make it visible. */
401 if (rover
.nfiles
> HEIGHT
) {
402 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
403 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
406 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
407 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
408 ishidden
= ENAME(j
)[0] == '.';
410 wattr_on(rover
.window
, A_REVERSE
, NULL
);
412 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
414 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
415 else if (S_ISREG(EMODE(j
))) {
416 if (EMODE(j
) & (S_IXUSR
| S_IXGRP
| S_IXOTH
))
417 wcolor_set(rover
.window
, RVC_EXEC
, NULL
);
419 wcolor_set(rover
.window
, RVC_REG
, NULL
);
420 } else if (S_ISDIR(EMODE(j
)))
421 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
422 else if (S_ISCHR(EMODE(j
)))
423 wcolor_set(rover
.window
, RVC_CHR
, NULL
);
424 else if (S_ISBLK(EMODE(j
)))
425 wcolor_set(rover
.window
, RVC_BLK
, NULL
);
426 else if (S_ISFIFO(EMODE(j
)))
427 wcolor_set(rover
.window
, RVC_FIFO
, NULL
);
428 else if (S_ISSOCK(EMODE(j
)))
429 wcolor_set(rover
.window
, RVC_SOCK
, NULL
);
430 if (S_ISDIR(EMODE(j
))) {
431 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
435 char *suffix
, *suffixes
= "BKMGTPEZY";
436 off_t human_size
= ESIZE(j
) * 10;
437 int length
= mbstowcs(NULL
, ENAME(j
), 0);
438 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
439 human_size
= (human_size
+ 512) / 1024;
441 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
442 (int) (COLS
- length
- 6),
443 (int) human_size
/ 10, *suffix
);
445 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
446 (int) (COLS
- length
- 8),
447 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
449 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
450 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
451 if (marking
&& MARKED(j
)) {
452 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
453 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
455 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
457 wattr_off(rover
.window
, A_REVERSE
, NULL
);
459 for (; i
< HEIGHT
; i
++)
460 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
461 if (rover
.nfiles
> HEIGHT
) {
463 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
464 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
465 if (!height
) height
= 1;
466 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
467 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
469 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
470 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
471 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
475 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
476 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
477 color_set(RVC_STATUS
, NULL
);
478 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
479 wrefresh(rover
.window
);
482 /* Show a message on the status bar. */
484 message(Color color
, char *fmt
, ...)
490 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
493 pos
= (STATUSPOS
- len
) / 2;
494 attr_on(A_BOLD
, NULL
);
495 color_set(color
, NULL
);
496 mvaddstr(LINES
- 1, pos
, BUF1
);
497 color_set(DEFAULT
, NULL
);
498 attr_off(A_BOLD
, NULL
);
501 /* Clear message area, leaving only status info. */
505 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
508 /* Comparison used to sort listing entries. */
510 rowcmp(const void *a
, const void *b
)
512 int isdir1
, isdir2
, cmpdir
;
515 isdir1
= S_ISDIR(r1
->mode
);
516 isdir2
= S_ISDIR(r2
->mode
);
517 cmpdir
= isdir2
- isdir1
;
518 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
521 /* Get all entries in current working directory. */
523 ls(Row
**rowsp
, uint8_t flags
)
531 if(!(dp
= opendir("."))) return -1;
532 n
= -2; /* We don't want the entries "." and "..". */
533 while (readdir(dp
)) n
++;
535 rows
= malloc(n
* sizeof *rows
);
537 while ((ep
= readdir(dp
))) {
538 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
540 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
542 lstat(ep
->d_name
, &statbuf
);
543 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
544 stat(ep
->d_name
, &statbuf
);
545 if (S_ISDIR(statbuf
.st_mode
)) {
546 if (flags
& SHOW_DIRS
) {
547 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
548 strcpy(rows
[i
].name
, ep
->d_name
);
550 strcat(rows
[i
].name
, "/");
551 rows
[i
].mode
= statbuf
.st_mode
;
554 } else if (flags
& SHOW_FILES
) {
555 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
556 strcpy(rows
[i
].name
, ep
->d_name
);
557 rows
[i
].size
= statbuf
.st_size
;
558 rows
[i
].mode
= statbuf
.st_mode
;
562 n
= i
; /* Ignore unused space in array caused by filters. */
563 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
570 free_rows(Row
**rowsp
, int nfiles
)
574 for (i
= 0; i
< nfiles
; i
++)
575 free((*rowsp
)[i
].name
);
580 /* Change working directory to the path in CWD. */
586 message(CYAN
, "Loading...");
588 if (reset
) ESEL
= SCROLL
= 0;
591 free_rows(&rover
.rows
, rover
.nfiles
);
592 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
593 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
594 for (i
= 0; i
< rover
.nfiles
; i
++) {
595 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
597 rover
.marks
.entries
[j
] &&
598 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
601 MARKED(i
) = j
< rover
.marks
.bulk
;
604 for (i
= 0; i
< rover
.nfiles
; i
++)
610 /* Select a target entry, if it is present. */
612 try_to_sel(const char *target
)
616 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
618 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
622 /* Reload CWD, but try to keep selection. */
627 strcpy(INPUT
, ENAME(ESEL
));
636 count_dir(const char *path
)
641 char subpath
[PATH_MAX
];
644 if(!(dp
= opendir(path
))) return 0;
646 while ((ep
= readdir(dp
))) {
647 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
649 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
650 lstat(subpath
, &statbuf
);
651 if (S_ISDIR(statbuf
.st_mode
)) {
652 strcat(subpath
, "/");
653 total
+= count_dir(subpath
);
655 total
+= statbuf
.st_size
;
670 chdir(rover
.marks
.dirpath
);
671 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
672 entry
= rover
.marks
.entries
[i
];
675 total
+= count_dir(entry
);
677 lstat(entry
, &statbuf
);
678 total
+= statbuf
.st_size
;
686 /* Recursively process a source directory using CWD as destination root.
687 For each node (i.e. directory), do the following:
688 1. call pre(destination);
689 2. call proc() on every child leaf (i.e. files);
690 3. recurse into every child node;
692 E.g. to move directory /src/ (and all its contents) inside /dst/:
693 strcpy(CWD, "/dst/");
694 process_dir(adddir, movfile, deldir, "/src/"); */
696 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
702 char subpath
[PATH_MAX
];
706 char dstpath
[PATH_MAX
];
707 strcpy(dstpath
, CWD
);
708 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
711 if(!(dp
= opendir(path
))) return -1;
712 while ((ep
= readdir(dp
))) {
713 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
715 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
716 lstat(subpath
, &statbuf
);
717 if (S_ISDIR(statbuf
.st_mode
)) {
718 strcat(subpath
, "/");
719 ret
|= process_dir(pre
, proc
, pos
, subpath
);
721 ret
|= proc(subpath
);
724 if (pos
) ret
|= pos(path
);
728 /* Process all marked entries using CWD as destination root.
729 All marked entries that are directories will be recursively processed.
730 See process_dir() for details on the parameters. */
732 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
733 const char *msg_doing
, const char *msg_done
)
740 message(CYAN
, "%s...", msg_doing
);
742 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
743 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
744 entry
= rover
.marks
.entries
[i
];
747 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
749 if (!strncmp(path
, CWD
, strlen(path
)))
752 ret
= process_dir(pre
, proc
, pos
, path
);
756 del_mark(&rover
.marks
, entry
);
761 rover
.prog
.total
= 0;
763 if (!rover
.marks
.nentries
)
764 message(GREEN
, "%s all marked entries.", msg_done
);
766 message(RED
, "Some errors occured while %s.", msg_doing
);
771 update_progress(off_t delta
)
775 if (!rover
.prog
.total
) return;
776 rover
.prog
.partial
+= delta
;
777 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
778 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
782 /* Wrappers for file operations. */
783 static int delfile(const char *path
) {
787 ret
= lstat(path
, &st
);
788 if (ret
< 0) return ret
;
789 update_progress(st
.st_size
);
792 static PROCESS deldir
= rmdir
;
793 static int addfile(const char *path
) {
794 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
797 ret
= creat(path
, 0644);
798 if (ret
< 0) return ret
;
801 static int cpyfile(const char *srcpath
) {
806 char dstpath
[PATH_MAX
];
808 strcpy(dstpath
, CWD
);
809 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
810 ret
= lstat(srcpath
, &st
);
811 if (ret
< 0) return ret
;
812 if (S_ISLNK(st
.st_mode
)) {
813 ret
= readlink(srcpath
, BUF1
, BUFLEN
-1);
814 if (ret
< 0) return ret
;
816 ret
= symlink(BUF1
, dstpath
);
818 ret
= src
= open(srcpath
, O_RDONLY
);
819 if (ret
< 0) return ret
;
820 ret
= dst
= creat(dstpath
, st
.st_mode
);
821 if (ret
< 0) return ret
;
822 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
823 write(dst
, buf
, size
);
824 update_progress(size
);
833 static int adddir(const char *path
) {
837 ret
= stat(CWD
, &st
);
838 if (ret
< 0) return ret
;
839 return mkdir(path
, st
.st_mode
);
841 static int movfile(const char *srcpath
) {
844 char dstpath
[PATH_MAX
];
846 strcpy(dstpath
, CWD
);
847 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
848 ret
= rename(srcpath
, dstpath
);
850 ret
= lstat(dstpath
, &st
);
851 if (ret
< 0) return ret
;
852 update_progress(st
.st_size
);
853 } else if (errno
== EXDEV
) {
854 ret
= cpyfile(srcpath
);
855 if (ret
< 0) return ret
;
856 ret
= unlink(srcpath
);
862 start_line_edit(const char *init_input
)
865 strncpy(INPUT
, init_input
, BUFLEN
);
866 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
867 rover
.edit
.right
= BUFLEN
- 1;
868 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
869 rover
.edit_scroll
= 0;
872 /* Read input and change editing state accordingly. */
876 wchar_t eraser
, killer
, wch
;
879 ret
= rover_get_wch((wint_t *) &wch
);
882 if (ret
== KEY_CODE_YES
) {
883 if (wch
== KEY_ENTER
) {
886 } else if (wch
== KEY_LEFT
) {
887 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
888 } else if (wch
== KEY_RIGHT
) {
889 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
890 } else if (wch
== KEY_UP
) {
891 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
892 } else if (wch
== KEY_DOWN
) {
893 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
894 } else if (wch
== KEY_BACKSPACE
) {
895 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
896 } else if (wch
== KEY_DC
) {
897 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
900 if (wch
== L
'\r' || wch
== L
'\n') {
903 } else if (wch
== L
'\t') {
906 } else if (wch
== eraser
) {
907 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
908 } else if (wch
== killer
) {
909 EDIT_CLEAR(rover
.edit
);
911 } else if (iswprint(wch
)) {
912 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
915 /* Encode edit contents in INPUT. */
916 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
917 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
918 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
923 /* Update line input on the screen. */
925 update_input(const char *prompt
, Color color
)
927 int plen
, ilen
, maxlen
;
929 plen
= strlen(prompt
);
930 ilen
= mbstowcs(NULL
, INPUT
, 0);
931 maxlen
= STATUSPOS
- plen
- 2;
932 if (ilen
- rover
.edit_scroll
< maxlen
)
933 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
934 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
935 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
936 else if (rover
.edit
.left
< rover
.edit_scroll
)
937 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
938 color_set(RVC_PROMPT
, NULL
);
939 mvaddstr(LINES
- 1, 0, prompt
);
940 color_set(color
, NULL
);
941 mbstowcs(WBUF
, INPUT
, COLS
);
942 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
943 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
944 color_set(DEFAULT
, NULL
);
945 if (rover
.edit_scroll
)
946 mvaddch(LINES
- 1, plen
- 1, '<');
947 if (ilen
> rover
.edit_scroll
+ maxlen
)
948 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
949 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
953 main(int argc
, char *argv
[])
961 FILE *save_cwd_file
= NULL
;
962 FILE *save_marks_file
= NULL
;
965 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
966 printf("rover %s\n", RV_VERSION
);
968 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
970 "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
971 " Browse current directory or the ones specified.\n\n"
972 " or: rover -h|--help\n"
973 " Print this help message and exit.\n\n"
974 " or: rover -v|--version\n"
975 " Print program version and exit.\n\n"
976 "See rover(1) for more information.\n"
977 "Rover homepage: <https://github.com/lecram/rover>.\n"
980 } else if (!strcmp(argv
[1], "-d") || !strcmp(argv
[1], "--save-cwd")) {
982 save_cwd_file
= fopen(argv
[2], "w");
983 argc
-= 2; argv
+= 2;
985 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
988 } else if (!strcmp(argv
[1], "-m") || !strcmp(argv
[1], "--save-marks")) {
990 save_marks_file
= fopen(argv
[2], "a");
991 argc
-= 2; argv
+= 2;
993 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
1000 for (i
= 0; i
< 10; i
++) {
1001 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
1002 rover
.tabs
[i
].flags
= SHOW_FILES
| SHOW_DIRS
;
1004 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
1005 for (i
= 1; i
< argc
&& i
< 10; i
++) {
1006 if ((d
= opendir(argv
[i
]))) {
1007 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
1010 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
1012 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
1013 for (i
++; i
< 10; i
++)
1014 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
1015 for (i
= 0; i
< 10; i
++)
1016 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
1017 strcat(rover
.tabs
[i
].cwd
, "/");
1019 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
1020 init_marks(&rover
.marks
);
1026 if (!strcmp(key
, RVK_QUIT
)) break;
1027 else if (ch
>= '0' && ch
<= '9') {
1028 rover
.tab
= ch
- '0';
1030 } else if (!strcmp(key
, RVK_HELP
)) {
1031 spawn((char *[]) {"man", "rover", NULL
});
1032 } else if (!strcmp(key
, RVK_DOWN
)) {
1033 if (!rover
.nfiles
) continue;
1034 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
1036 } else if (!strcmp(key
, RVK_UP
)) {
1037 if (!rover
.nfiles
) continue;
1038 ESEL
= MAX(ESEL
- 1, 0);
1040 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
1041 if (!rover
.nfiles
) continue;
1042 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
1043 if (rover
.nfiles
> HEIGHT
)
1044 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
1046 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
1047 if (!rover
.nfiles
) continue;
1048 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
1049 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
1051 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1052 if (!rover
.nfiles
) continue;
1055 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1056 if (!rover
.nfiles
) continue;
1057 ESEL
= rover
.nfiles
- 1;
1059 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1060 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1061 if (chdir(ENAME(ESEL
)) == -1) {
1062 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1065 strcat(CWD
, ENAME(ESEL
));
1067 } else if (!strcmp(key
, RVK_CD_UP
)) {
1068 char *dirname
, first
;
1069 if (!strcmp(CWD
, "/")) continue;
1070 CWD
[strlen(CWD
) - 1] = '\0';
1071 dirname
= strrchr(CWD
, '/') + 1;
1076 dirname
[strlen(dirname
)] = '/';
1077 try_to_sel(dirname
);
1079 if (rover
.nfiles
> HEIGHT
)
1080 SCROLL
= ESEL
- HEIGHT
/ 2;
1082 } else if (!strcmp(key
, RVK_HOME
)) {
1083 strcpy(CWD
, getenv("HOME"));
1084 if (CWD
[strlen(CWD
) - 1] != '/')
1087 } else if (!strcmp(key
, RVK_TARGET
)) {
1089 int is_dir
= S_ISDIR(EMODE(ESEL
));
1090 ssize_t len
= readlink(ENAME(ESEL
), BUF1
, BUFLEN
-1);
1091 if (len
== -1) continue;
1093 if (access(BUF1
, F_OK
) == -1) {
1097 msg
= "Cannot access \"%s\".";
1100 msg
= "\"%s\" does not exist.";
1103 msg
= "Cannot navigate to \"%s\".";
1105 strcpy(BUF2
, BUF1
); /* message() uses BUF1. */
1106 message(RED
, msg
, BUF2
);
1109 realpath(BUF1
, CWD
);
1111 if (CWD
[len
- 1] == '/')
1112 CWD
[len
- 1] = '\0';
1113 bname
= strrchr(CWD
, '/') + 1;
1123 } else if (!strcmp(key
, RVK_REFRESH
)) {
1125 } else if (!strcmp(key
, RVK_SHELL
)) {
1126 program
= getenv("SHELL");
1129 spawn((char *[]) {RV_SHELL
, "-c", program
, NULL
});
1131 spawn((char *[]) {program
, NULL
});
1135 } else if (!strcmp(key
, RVK_VIEW
)) {
1136 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1137 if (open_with_env("PAGER", ENAME(ESEL
)))
1139 } else if (!strcmp(key
, RVK_EDIT
)) {
1140 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1141 if (open_with_env("EDITOR", ENAME(ESEL
)))
1143 } else if (!strcmp(key
, RVK_OPEN
)) {
1144 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1145 if (open_with_env("ROVER_OPEN", ENAME(ESEL
)))
1147 } else if (!strcmp(key
, RVK_SEARCH
)) {
1148 int oldsel
, oldscroll
, length
;
1149 if (!rover
.nfiles
) continue;
1152 start_line_edit("");
1153 update_input(RVP_SEARCH
, RED
);
1154 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1157 length
= strlen(INPUT
);
1159 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1160 if (!strncmp(ENAME(sel
), INPUT
, length
))
1162 if (sel
< rover
.nfiles
) {
1165 if (rover
.nfiles
> HEIGHT
) {
1168 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1169 SCROLL
= rover
.nfiles
- HEIGHT
;
1179 update_input(RVP_SEARCH
, color
);
1181 if (edit_stat
== CANCEL
) {
1187 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1188 FLAGS
^= SHOW_FILES
;
1190 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1193 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1194 FLAGS
^= SHOW_HIDDEN
;
1196 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1198 start_line_edit("");
1199 update_input(RVP_NEW_FILE
, RED
);
1200 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1201 int length
= strlen(INPUT
);
1203 for (i
= 0; i
< rover
.nfiles
; i
++) {
1205 !strncmp(ENAME(i
), INPUT
, length
) &&
1206 (!strcmp(ENAME(i
) + length
, "") ||
1207 !strcmp(ENAME(i
) + length
, "/"))
1213 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1216 if (edit_stat
== CONFIRM
) {
1218 if (addfile(INPUT
) == 0) {
1223 message(RED
, "Could not create \"%s\".", INPUT
);
1225 message(RED
, "\"%s\" already exists.", INPUT
);
1227 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1229 start_line_edit("");
1230 update_input(RVP_NEW_DIR
, RED
);
1231 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1232 int length
= strlen(INPUT
);
1234 for (i
= 0; i
< rover
.nfiles
; i
++) {
1236 !strncmp(ENAME(i
), INPUT
, length
) &&
1237 (!strcmp(ENAME(i
) + length
, "") ||
1238 !strcmp(ENAME(i
) + length
, "/"))
1244 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1247 if (edit_stat
== CONFIRM
) {
1249 if (adddir(INPUT
) == 0) {
1255 message(RED
, "Could not create \"%s/\".", INPUT
);
1257 message(RED
, "\"%s\" already exists.", INPUT
);
1259 } else if (!strcmp(key
, RVK_RENAME
)) {
1263 strcpy(INPUT
, ENAME(ESEL
));
1264 last
= INPUT
+ strlen(INPUT
) - 1;
1265 if ((isdir
= *last
== '/'))
1267 start_line_edit(INPUT
);
1268 update_input(RVP_RENAME
, RED
);
1269 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1270 int length
= strlen(INPUT
);
1272 for (i
= 0; i
< rover
.nfiles
; i
++)
1274 !strncmp(ENAME(i
), INPUT
, length
) &&
1275 (!strcmp(ENAME(i
) + length
, "") ||
1276 !strcmp(ENAME(i
) + length
, "/"))
1281 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1284 if (edit_stat
== CONFIRM
) {
1288 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1289 del_mark(&rover
.marks
, ENAME(ESEL
));
1290 add_mark(&rover
.marks
, CWD
, INPUT
);
1296 message(RED
, "\"%s\" already exists.", INPUT
);
1298 } else if (!strcmp(key
, RVK_DELETE
)) {
1300 message(YELLOW
, "Delete \"%s\"? (Y/n)", ENAME(ESEL
));
1301 if (rover_getch() == 'Y') {
1302 const char *name
= ENAME(ESEL
);
1303 int ret
= ISDIR(ENAME(ESEL
)) ? deldir(name
) : delfile(name
);
1306 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1310 message(RED
, "No entry selected for deletion.");
1311 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1313 del_mark(&rover
.marks
, ENAME(ESEL
));
1315 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1316 MARKED(ESEL
) = !MARKED(ESEL
);
1317 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1319 } else if (!strcmp(key
, RVK_INVMARK
)) {
1320 for (i
= 0; i
< rover
.nfiles
; i
++) {
1322 del_mark(&rover
.marks
, ENAME(i
));
1324 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1325 MARKED(i
) = !MARKED(i
);
1328 } else if (!strcmp(key
, RVK_MARKALL
)) {
1329 for (i
= 0; i
< rover
.nfiles
; i
++)
1331 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1335 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1336 if (rover
.marks
.nentries
) {
1337 message(YELLOW
, "Delete all marked entries? (Y/n)");
1338 if (rover_getch() == 'Y')
1339 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1343 message(RED
, "No entries marked for deletion.");
1344 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1345 if (rover
.marks
.nentries
)
1346 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1348 message(RED
, "No entries marked for copying.");
1349 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1350 if (rover
.marks
.nentries
)
1351 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1353 message(RED
, "No entries marked for moving.");
1357 free_rows(&rover
.rows
, rover
.nfiles
);
1358 delwin(rover
.window
);
1359 if (save_cwd_file
!= NULL
) {
1360 fputs(CWD
, save_cwd_file
);
1361 fclose(save_cwd_file
);
1363 if (save_marks_file
!= NULL
) {
1364 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
1365 entry
= rover
.marks
.entries
[i
];
1367 fprintf(save_marks_file
, "%s%s\n", rover
.marks
.dirpath
, entry
);
1369 fclose(save_marks_file
);
1371 free_marks(&rover
.marks
);