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() */
28 #define BUFLEN PATH_MAX
29 static char BUF1
[BUFLEN
];
30 static char BUF2
[BUFLEN
];
31 static char INPUT
[BUFLEN
];
32 static wchar_t WBUF
[BUFLEN
];
34 /* Argument buffers for execvp(). */
36 static char *ARGS
[MAXARGS
];
38 /* Listing view parameters. */
39 #define HEIGHT (LINES-4)
40 #define STATUSPOS (COLS-16)
42 /* Listing view flags. */
43 #define SHOW_FILES 0x01u
44 #define SHOW_DIRS 0x02u
45 #define SHOW_HIDDEN 0x04u
47 /* Marks parameters. */
49 #define BULK_THRESH 256
51 /* Information associated to each entry in listing. */
60 /* Dynamic array of marked entries. */
61 typedef struct Marks
{
62 char dirpath
[PATH_MAX
];
68 /* Line editing state. */
70 wchar_t buffer
[BUFLEN
+1];
74 /* Each tab only stores the following information. */
97 volatile sig_atomic_t pending_winch
;
102 /* Macros for accessing global state. */
103 #define ENAME(I) rover.rows[I].name
104 #define ESIZE(I) rover.rows[I].size
105 #define EMODE(I) rover.rows[I].mode
106 #define ISLINK(I) rover.rows[I].islink
107 #define MARKED(I) rover.rows[I].marked
108 #define SCROLL rover.tabs[rover.tab].scroll
109 #define ESEL rover.tabs[rover.tab].esel
110 #define FLAGS rover.tabs[rover.tab].flags
111 #define CWD rover.tabs[rover.tab].cwd
114 #define MIN(A, B) ((A) < (B) ? (A) : (B))
115 #define MAX(A, B) ((A) > (B) ? (A) : (B))
116 #define ISDIR(E) (strchr((E), '/') != NULL)
118 /* Line Editing Macros. */
119 #define EDIT_FULL(E) ((E).left == (E).right)
120 #define EDIT_CAN_LEFT(E) ((E).left)
121 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
122 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
123 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
124 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
125 #define EDIT_BACKSPACE(E) (E).left--
126 #define EDIT_DELETE(E) (E).right++
127 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
129 typedef enum EditStat
{CONTINUE
, CONFIRM
, CANCEL
} EditStat
;
130 typedef enum Color
{DEFAULT
, RED
, GREEN
, YELLOW
, BLUE
, CYAN
, MAGENTA
, WHITE
, BLACK
} Color
;
131 typedef int (*PROCESS
)(const char *path
);
134 init_marks(Marks
*marks
)
136 strcpy(marks
->dirpath
, "");
137 marks
->bulk
= BULK_INIT
;
139 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
142 /* Unmark all entries. */
144 mark_none(Marks
*marks
)
148 strcpy(marks
->dirpath
, "");
149 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
150 if (marks
->entries
[i
]) {
151 free(marks
->entries
[i
]);
152 marks
->entries
[i
] = NULL
;
155 if (marks
->bulk
> BULK_THRESH
) {
156 /* Reset bulk to free some memory. */
157 free(marks
->entries
);
158 marks
->bulk
= BULK_INIT
;
159 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
164 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
168 if (!strcmp(marks
->dirpath
, dirpath
)) {
169 /* Append mark to directory. */
170 if (marks
->nentries
== marks
->bulk
) {
171 /* Expand bulk to accomodate new entry. */
172 int extra
= marks
->bulk
/ 2;
173 marks
->bulk
+= extra
; /* bulk *= 1.5; */
174 marks
->entries
= realloc(marks
->entries
,
175 marks
->bulk
* sizeof *marks
->entries
);
176 memset(&marks
->entries
[marks
->nentries
], 0,
177 extra
* sizeof *marks
->entries
);
180 /* Search for empty slot (there must be one). */
181 for (i
= 0; i
< marks
->bulk
; i
++)
182 if (!marks
->entries
[i
])
186 /* Directory changed. Discard old marks. */
188 strcpy(marks
->dirpath
, dirpath
);
191 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
192 strcpy(marks
->entries
[i
], entry
);
197 del_mark(Marks
*marks
, char *entry
)
201 if (marks
->nentries
> 1) {
202 for (i
= 0; i
< marks
->bulk
; i
++)
203 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
205 free(marks
->entries
[i
]);
206 marks
->entries
[i
] = NULL
;
213 free_marks(Marks
*marks
)
217 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
218 if (marks
->entries
[i
]) {
219 free(marks
->entries
[i
]);
222 free(marks
->entries
);
226 handle_winch(int sig
)
228 rover
.pending_winch
= 1;
236 memset(&sa
, 0, sizeof (struct sigaction
));
237 sa
.sa_handler
= handle_winch
;
238 sigaction(SIGWINCH
, &sa
, NULL
);
246 memset(&sa
, 0, sizeof (struct sigaction
));
247 sa
.sa_handler
= SIG_DFL
;
248 sigaction(SIGWINCH
, &sa
, NULL
);
251 static void update_view();
253 /* Handle any signals received since last call. */
257 if (rover
.pending_winch
) {
258 /* SIGWINCH received: resize application accordingly. */
259 delwin(rover
.window
);
263 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
264 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
265 SCROLL
= ESEL
- HEIGHT
;
267 rover
.pending_winch
= 0;
271 /* This function must be used in place of getch().
272 It handles signals while waiting for user input. */
278 while ((ch
= getch()) == ERR
)
283 /* This function must be used in place of get_wch().
284 It handles signals while waiting for user input. */
286 rover_get_wch(wint_t *wch
)
290 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
295 /* Do a fork-exec to external program (e.g. $EDITOR). */
302 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
305 /* fork() succeeded. */
308 waitpid(pid
, &status
, 0);
310 kill(getpid(), SIGWINCH
);
311 } else if (pid
== 0) {
313 execvp(ARGS
[0], ARGS
);
321 setlocale(LC_ALL
, "");
323 cbreak(); /* Get one character at a time. */
324 timeout(100); /* For getch(). */
326 nonl(); /* No NL->CR/NL on output. */
327 intrflush(stdscr
, FALSE
);
328 keypad(stdscr
, TRUE
);
329 curs_set(FALSE
); /* Hide blinking cursor. */
333 #ifdef NCURSES_EXT_FUNCS
334 use_default_colors();
339 init_pair(RED
, COLOR_RED
, bg
);
340 init_pair(GREEN
, COLOR_GREEN
, bg
);
341 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
342 init_pair(BLUE
, COLOR_BLUE
, bg
);
343 init_pair(CYAN
, COLOR_CYAN
, bg
);
344 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
345 init_pair(WHITE
, COLOR_WHITE
, bg
);
346 init_pair(BLACK
, COLOR_BLACK
, bg
);
348 atexit((void (*)(void)) endwin
);
352 /* Update the listing view. */
361 mvhline(0, 0, ' ', COLS
);
362 attr_on(A_BOLD
, NULL
);
363 color_set(RVC_TABNUM
, NULL
);
364 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
365 attr_off(A_BOLD
, NULL
);
366 if (rover
.marks
.nentries
) {
367 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
368 color_set(RVC_MARKS
, NULL
);
369 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
372 color_set(RVC_CWD
, NULL
);
373 mbstowcs(WBUF
, CWD
, PATH_MAX
);
374 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
375 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
376 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
377 ESEL
= MAX(MIN(ESEL
, rover
.nfiles
- 1), 0);
378 /* Selection might not be visible, due to cursor wrapping or window
379 shrinking. In that case, the scroll must be moved to make it visible. */
380 if (rover
.nfiles
> HEIGHT
) {
381 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
382 SCROLL
= MIN(SCROLL
, rover
.nfiles
- HEIGHT
);
385 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
386 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
387 ishidden
= ENAME(j
)[0] == '.';
388 isdir
= S_ISDIR(EMODE(j
));
390 wattr_on(rover
.window
, A_REVERSE
, NULL
);
392 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
394 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
396 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
398 wcolor_set(rover
.window
, RVC_FILE
, NULL
);
400 char *suffix
, *suffixes
= "BKMGTPEZY";
401 off_t human_size
= ESIZE(j
) * 10;
402 int length
= mbstowcs(NULL
, ENAME(j
), 0);
403 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
404 human_size
= (human_size
+ 512) / 1024;
406 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
407 (int) (COLS
- length
- 6),
408 (int) human_size
/ 10, *suffix
);
410 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
411 (int) (COLS
- length
- 8),
412 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
414 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
415 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
416 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
417 if (marking
&& MARKED(j
)) {
418 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
419 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
421 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
423 wattr_off(rover
.window
, A_REVERSE
, NULL
);
425 for (; i
< HEIGHT
; i
++)
426 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
427 if (rover
.nfiles
> HEIGHT
) {
429 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
430 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
431 if (!height
) height
= 1;
432 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
433 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
435 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
436 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
437 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
441 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
442 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
443 color_set(RVC_STATUS
, NULL
);
444 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
445 wrefresh(rover
.window
);
448 /* Show a message on the status bar. */
450 message(Color color
, char *fmt
, ...)
456 vsnprintf(BUF1
, MIN(BUFLEN
, STATUSPOS
), fmt
, args
);
459 pos
= (STATUSPOS
- len
) / 2;
460 attr_on(A_BOLD
, NULL
);
461 color_set(color
, NULL
);
462 mvaddstr(LINES
- 1, pos
, BUF1
);
463 color_set(DEFAULT
, NULL
);
464 attr_off(A_BOLD
, NULL
);
467 /* Clear message area, leaving only status info. */
471 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
474 /* Comparison used to sort listing entries. */
476 rowcmp(const void *a
, const void *b
)
478 int isdir1
, isdir2
, cmpdir
;
481 isdir1
= S_ISDIR(r1
->mode
);
482 isdir2
= S_ISDIR(r2
->mode
);
483 cmpdir
= isdir2
- isdir1
;
484 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
487 /* Get all entries in current working directory. */
489 ls(Row
**rowsp
, uint8_t flags
)
497 if(!(dp
= opendir("."))) return -1;
498 n
= -2; /* We don't want the entries "." and "..". */
499 while (readdir(dp
)) n
++;
501 rows
= malloc(n
* sizeof *rows
);
503 while ((ep
= readdir(dp
))) {
504 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
506 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
508 lstat(ep
->d_name
, &statbuf
);
509 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
510 stat(ep
->d_name
, &statbuf
);
511 if (S_ISDIR(statbuf
.st_mode
)) {
512 if (flags
& SHOW_DIRS
) {
513 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
514 strcpy(rows
[i
].name
, ep
->d_name
);
515 strcat(rows
[i
].name
, "/");
516 rows
[i
].mode
= statbuf
.st_mode
;
519 } else if (flags
& SHOW_FILES
) {
520 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
521 strcpy(rows
[i
].name
, ep
->d_name
);
522 rows
[i
].size
= statbuf
.st_size
;
523 rows
[i
].mode
= statbuf
.st_mode
;
527 n
= i
; /* Ignore unused space in array caused by filters. */
528 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
535 free_rows(Row
**rowsp
, int nfiles
)
539 for (i
= 0; i
< nfiles
; i
++)
540 free((*rowsp
)[i
].name
);
545 /* Change working directory to the path in CWD. */
551 message(CYAN
, "Loading...");
553 if (reset
) ESEL
= SCROLL
= 0;
556 free_rows(&rover
.rows
, rover
.nfiles
);
557 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
558 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
559 for (i
= 0; i
< rover
.nfiles
; i
++) {
560 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
562 rover
.marks
.entries
[j
] &&
563 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
566 MARKED(i
) = j
< rover
.marks
.bulk
;
569 for (i
= 0; i
< rover
.nfiles
; i
++)
575 /* Select a target entry, if it is present. */
577 try_to_sel(const char *target
)
581 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
583 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
587 /* Reload CWD, but try to keep selection. */
592 strcpy(INPUT
, ENAME(ESEL
));
601 count_dir(const char *path
)
606 char subpath
[PATH_MAX
];
609 if(!(dp
= opendir(path
))) return 0;
611 while ((ep
= readdir(dp
))) {
612 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
614 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
615 lstat(subpath
, &statbuf
);
616 if (S_ISDIR(statbuf
.st_mode
)) {
617 strcat(subpath
, "/");
618 total
+= count_dir(subpath
);
620 total
+= statbuf
.st_size
;
635 chdir(rover
.marks
.dirpath
);
636 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
637 entry
= rover
.marks
.entries
[i
];
640 total
+= count_dir(entry
);
642 lstat(entry
, &statbuf
);
643 total
+= statbuf
.st_size
;
651 /* Recursively process a source directory using CWD as destination root.
652 For each node (i.e. directory), do the following:
653 1. call pre(destination);
654 2. call proc() on every child leaf (i.e. files);
655 3. recurse into every child node;
657 E.g. to move directory /src/ (and all its contents) inside /dst/:
658 strcpy(CWD, "/dst/");
659 process_dir(adddir, movfile, deldir, "/src/"); */
661 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
667 char subpath
[PATH_MAX
];
671 char dstpath
[PATH_MAX
];
672 strcpy(dstpath
, CWD
);
673 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
676 if(!(dp
= opendir(path
))) return -1;
677 while ((ep
= readdir(dp
))) {
678 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
680 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
681 stat(subpath
, &statbuf
);
682 if (S_ISDIR(statbuf
.st_mode
)) {
683 strcat(subpath
, "/");
684 ret
|= process_dir(pre
, proc
, pos
, subpath
);
686 ret
|= proc(subpath
);
689 if (pos
) ret
|= pos(path
);
693 /* Process all marked entries using CWD as destination root.
694 All marked entries that are directories will be recursively processed.
695 See process_dir() for details on the parameters. */
697 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
,
698 const char *msg_doing
, const char *msg_done
)
705 message(CYAN
, "%s...", msg_doing
);
707 rover
.prog
= (Prog
) {0, count_marked(), msg_doing
};
708 for (i
= 0; i
< rover
.marks
.bulk
; i
++) {
709 entry
= rover
.marks
.entries
[i
];
712 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, entry
);
714 if (!strncmp(path
, CWD
, strlen(path
)))
717 ret
= process_dir(pre
, proc
, pos
, path
);
720 if (!ret
) del_mark(&rover
.marks
, entry
);
723 rover
.prog
.total
= 0;
725 if (!rover
.marks
.nentries
)
726 message(GREEN
, "%s all marked entries.", msg_done
);
728 message(RED
, "Some errors occured while %s.", msg_doing
);
733 update_progress(off_t delta
)
737 if (!rover
.prog
.total
) return;
738 rover
.prog
.partial
+= delta
;
739 percent
= (int) (rover
.prog
.partial
* 100 / rover
.prog
.total
);
740 message(CYAN
, "%s...%d%%", rover
.prog
.msg
, percent
);
744 /* Wrappers for file operations. */
745 static int delfile(const char *path
) {
749 ret
= lstat(path
, &st
);
750 if (ret
< 0) return ret
;
751 update_progress(st
.st_size
);
754 static PROCESS deldir
= rmdir
;
755 static int addfile(const char *path
) {
756 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
759 ret
= creat(path
, 0644);
760 if (ret
< 0) return ret
;
763 static int cpyfile(const char *srcpath
) {
768 char dstpath
[PATH_MAX
];
770 ret
= src
= open(srcpath
, O_RDONLY
);
771 if (ret
< 0) return ret
;
772 ret
= fstat(src
, &st
);
773 if (ret
< 0) return ret
;
774 strcpy(dstpath
, CWD
);
775 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
776 ret
= dst
= creat(dstpath
, st
.st_mode
);
777 if (ret
< 0) return ret
;
778 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
779 write(dst
, buf
, size
);
780 update_progress(size
);
787 static int adddir(const char *path
) {
791 ret
= stat(CWD
, &st
);
792 if (ret
< 0) return ret
;
793 return mkdir(path
, st
.st_mode
);
795 static int movfile(const char *srcpath
) {
798 char dstpath
[PATH_MAX
];
800 strcpy(dstpath
, CWD
);
801 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
802 ret
= rename(srcpath
, dstpath
);
804 ret
= lstat(srcpath
, &st
);
805 if (ret
< 0) return ret
;
806 update_progress(st
.st_size
);
807 } else if (errno
== EXDEV
) {
808 ret
= cpyfile(srcpath
);
809 if (ret
< 0) return ret
;
810 ret
= unlink(srcpath
);
816 start_line_edit(const char *init_input
)
819 strncpy(INPUT
, init_input
, BUFLEN
);
820 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
821 rover
.edit
.right
= BUFLEN
- 1;
822 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
823 rover
.edit_scroll
= 0;
826 /* Read input and change editing state accordingly. */
830 wchar_t eraser
, killer
, wch
;
833 ret
= rover_get_wch((wint_t *) &wch
);
836 if (ret
== KEY_CODE_YES
) {
837 if (wch
== KEY_ENTER
) {
840 } else if (wch
== KEY_LEFT
) {
841 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
842 } else if (wch
== KEY_RIGHT
) {
843 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
844 } else if (wch
== KEY_UP
) {
845 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
846 } else if (wch
== KEY_DOWN
) {
847 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
848 } else if (wch
== KEY_BACKSPACE
) {
849 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
850 } else if (wch
== KEY_DC
) {
851 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
854 if (wch
== L
'\r' || wch
== L
'\n') {
857 } else if (wch
== L
'\t') {
860 } else if (wch
== eraser
) {
861 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
862 } else if (wch
== killer
) {
863 EDIT_CLEAR(rover
.edit
);
865 } else if (iswprint(wch
)) {
866 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
869 /* Encode edit contents in INPUT. */
870 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
871 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
872 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
877 /* Update line input on the screen. */
879 update_input(const char *prompt
, Color color
)
881 int plen
, ilen
, maxlen
;
883 plen
= strlen(prompt
);
884 ilen
= mbstowcs(NULL
, INPUT
, 0);
885 maxlen
= STATUSPOS
- plen
- 2;
886 if (ilen
- rover
.edit_scroll
< maxlen
)
887 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
888 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
889 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
890 else if (rover
.edit
.left
< rover
.edit_scroll
)
891 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
892 color_set(RVC_PROMPT
, NULL
);
893 mvaddstr(LINES
- 1, 0, prompt
);
894 color_set(color
, NULL
);
895 mbstowcs(WBUF
, INPUT
, COLS
);
896 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
897 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
898 color_set(DEFAULT
, NULL
);
899 if (rover
.edit_scroll
)
900 mvaddch(LINES
- 1, plen
- 1, '<');
901 if (ilen
> rover
.edit_scroll
+ maxlen
)
902 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
903 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
907 main(int argc
, char *argv
[])
914 FILE *save_cwd_file
= NULL
;
917 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
918 printf("rover %s\n", RV_VERSION
);
920 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
922 "Usage: rover [-s|--save-cwd FILE] [DIR [DIR [DIR [...]]]]\n"
923 " Browse current directory or the ones specified.\n"
924 " If FILE is given, write last visited path to it.\n\n"
925 " or: rover -h|--help\n"
926 " Print this help message and exit.\n\n"
927 " or: rover -v|--version\n"
928 " Print program version and exit.\n\n"
929 "See rover(1) for more information.\n\n"
930 "Rover homepage: <https://github.com/lecram/rover>.\n"
933 } else if (!strcmp(argv
[1], "-s") || !strcmp(argv
[1], "--save-cwd")) {
935 save_cwd_file
= fopen(argv
[2], "w");
936 argc
-= 2; argv
+= 2;
938 fprintf(stderr
, "error: missing argument to %s\n", argv
[1]);
945 for (i
= 0; i
< 10; i
++) {
946 rover
.tabs
[i
].esel
= rover
.tabs
[i
].scroll
= 0;
947 rover
.tabs
[i
].flags
= SHOW_FILES
| SHOW_DIRS
;
949 strcpy(rover
.tabs
[0].cwd
, getenv("HOME"));
950 for (i
= 1; i
< argc
&& i
< 10; i
++) {
951 if ((d
= opendir(argv
[i
]))) {
952 realpath(argv
[i
], rover
.tabs
[i
].cwd
);
955 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[0].cwd
);
957 getcwd(rover
.tabs
[i
].cwd
, PATH_MAX
);
958 for (i
++; i
< 10; i
++)
959 strcpy(rover
.tabs
[i
].cwd
, rover
.tabs
[i
-1].cwd
);
960 for (i
= 0; i
< 10; i
++)
961 if (rover
.tabs
[i
].cwd
[strlen(rover
.tabs
[i
].cwd
) - 1] != '/')
962 strcat(rover
.tabs
[i
].cwd
, "/");
964 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
965 init_marks(&rover
.marks
);
971 if (!strcmp(key
, RVK_QUIT
)) break;
972 else if (ch
>= '0' && ch
<= '9') {
973 rover
.tab
= ch
- '0';
975 } else if (!strcmp(key
, RVK_HELP
)) {
980 } else if (!strcmp(key
, RVK_DOWN
)) {
981 if (!rover
.nfiles
) continue;
982 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
984 } else if (!strcmp(key
, RVK_UP
)) {
985 if (!rover
.nfiles
) continue;
986 ESEL
= MAX(ESEL
- 1, 0);
988 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
989 if (!rover
.nfiles
) continue;
990 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
991 if (rover
.nfiles
> HEIGHT
)
992 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
994 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
995 if (!rover
.nfiles
) continue;
996 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
997 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
999 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
1000 if (!rover
.nfiles
) continue;
1003 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
1004 if (!rover
.nfiles
) continue;
1005 ESEL
= rover
.nfiles
- 1;
1007 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
1008 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
1009 if (chdir(ENAME(ESEL
)) == -1) {
1010 message(RED
, "Cannot access \"%s\".", ENAME(ESEL
));
1013 strcat(CWD
, ENAME(ESEL
));
1015 } else if (!strcmp(key
, RVK_CD_UP
)) {
1016 char *dirname
, first
;
1017 if (!strcmp(CWD
, "/")) continue;
1018 CWD
[strlen(CWD
) - 1] = '\0';
1019 dirname
= strrchr(CWD
, '/') + 1;
1024 dirname
[strlen(dirname
)] = '/';
1025 try_to_sel(dirname
);
1027 if (rover
.nfiles
> HEIGHT
)
1028 SCROLL
= ESEL
- HEIGHT
/ 2;
1030 } else if (!strcmp(key
, RVK_HOME
)) {
1031 strcpy(CWD
, getenv("HOME"));
1032 if (CWD
[strlen(CWD
) - 1] != '/')
1035 } else if (!strcmp(key
, RVK_REFRESH
)) {
1037 } else if (!strcmp(key
, RVK_SHELL
)) {
1038 program
= getenv("SHELL");
1045 } else if (!strcmp(key
, RVK_VIEW
)) {
1046 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1047 program
= getenv("PAGER");
1050 ARGS
[1] = ENAME(ESEL
);
1054 } else if (!strcmp(key
, RVK_EDIT
)) {
1055 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
1056 program
= getenv("EDITOR");
1059 ARGS
[1] = ENAME(ESEL
);
1064 } else if (!strcmp(key
, RVK_SEARCH
)) {
1065 int oldsel
, oldscroll
, length
;
1066 if (!rover
.nfiles
) continue;
1069 start_line_edit("");
1070 update_input(RVP_SEARCH
, RED
);
1071 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1074 length
= strlen(INPUT
);
1076 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
1077 if (!strncmp(ENAME(sel
), INPUT
, length
))
1079 if (sel
< rover
.nfiles
) {
1082 if (rover
.nfiles
> HEIGHT
) {
1085 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
1086 SCROLL
= rover
.nfiles
- HEIGHT
;
1096 update_input(RVP_SEARCH
, color
);
1098 if (edit_stat
== CANCEL
) {
1104 } else if (!strcmp(key
, RVK_TG_FILES
)) {
1105 FLAGS
^= SHOW_FILES
;
1107 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1110 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1111 FLAGS
^= SHOW_HIDDEN
;
1113 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1115 start_line_edit("");
1116 update_input(RVP_NEW_FILE
, RED
);
1117 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1118 int length
= strlen(INPUT
);
1120 for (i
= 0; i
< rover
.nfiles
; i
++) {
1122 !strncmp(ENAME(i
), INPUT
, length
) &&
1123 (!strcmp(ENAME(i
) + length
, "") ||
1124 !strcmp(ENAME(i
) + length
, "/"))
1130 update_input(RVP_NEW_FILE
, ok
? GREEN
: RED
);
1133 if (edit_stat
== CONFIRM
) {
1140 message(RED
, "\"%s\" already exists.", INPUT
);
1142 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1144 start_line_edit("");
1145 update_input(RVP_NEW_DIR
, RED
);
1146 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1147 int length
= strlen(INPUT
);
1149 for (i
= 0; i
< rover
.nfiles
; i
++) {
1151 !strncmp(ENAME(i
), INPUT
, length
) &&
1152 (!strcmp(ENAME(i
) + length
, "") ||
1153 !strcmp(ENAME(i
) + length
, "/"))
1159 update_input(RVP_NEW_DIR
, ok
? GREEN
: RED
);
1162 if (edit_stat
== CONFIRM
) {
1170 message(RED
, "\"%s\" already exists.", INPUT
);
1172 } else if (!strcmp(key
, RVK_RENAME
)) {
1176 strcpy(INPUT
, ENAME(ESEL
));
1177 last
= INPUT
+ strlen(INPUT
) - 1;
1178 if ((isdir
= *last
== '/'))
1180 start_line_edit(INPUT
);
1181 update_input(RVP_RENAME
, RED
);
1182 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1183 int length
= strlen(INPUT
);
1185 for (i
= 0; i
< rover
.nfiles
; i
++)
1187 !strncmp(ENAME(i
), INPUT
, length
) &&
1188 (!strcmp(ENAME(i
) + length
, "") ||
1189 !strcmp(ENAME(i
) + length
, "/"))
1194 update_input(RVP_RENAME
, ok
? GREEN
: RED
);
1197 if (edit_stat
== CONFIRM
) {
1201 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1202 del_mark(&rover
.marks
, ENAME(ESEL
));
1203 add_mark(&rover
.marks
, CWD
, INPUT
);
1209 message(RED
, "\"%s\" already exists.", INPUT
);
1211 } else if (!strcmp(key
, RVK_DELETE
)) {
1213 message(YELLOW
, "Delete \"%s\"? (Y to confirm)", ENAME(ESEL
));
1214 if (rover_getch() == 'Y') {
1215 const char *name
= ENAME(ESEL
);
1216 int ret
= S_ISDIR(EMODE(ESEL
)) ? deldir(name
) : delfile(name
);
1219 message(RED
, "Could not delete \"%s\".", ENAME(ESEL
));
1223 message(RED
, "No entry selected for deletion.");
1224 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1226 del_mark(&rover
.marks
, ENAME(ESEL
));
1228 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1229 MARKED(ESEL
) = !MARKED(ESEL
);
1230 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1232 } else if (!strcmp(key
, RVK_INVMARK
)) {
1233 for (i
= 0; i
< rover
.nfiles
; i
++) {
1235 del_mark(&rover
.marks
, ENAME(i
));
1237 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1238 MARKED(i
) = !MARKED(i
);
1241 } else if (!strcmp(key
, RVK_MARKALL
)) {
1242 for (i
= 0; i
< rover
.nfiles
; i
++)
1244 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1248 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1249 if (rover
.marks
.nentries
) {
1250 message(YELLOW
, "Delete all marked entries? (Y to confirm)");
1251 if (rover_getch() == 'Y')
1252 process_marked(NULL
, delfile
, deldir
, "Deleting", "Deleted");
1256 message(RED
, "No entries marked for deletion.");
1257 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1258 if (rover
.marks
.nentries
)
1259 process_marked(adddir
, cpyfile
, NULL
, "Copying", "Copied");
1261 message(RED
, "No entries marked for copying.");
1262 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1263 if (rover
.marks
.nentries
)
1264 process_marked(adddir
, movfile
, deldir
, "Moving", "Moved");
1266 message(RED
, "No entries marked for moving.");
1270 free_rows(&rover
.rows
, rover
.nfiles
);
1271 free_marks(&rover
.marks
);
1272 delwin(rover
.window
);
1273 if (save_cwd_file
!= NULL
) {
1274 fputs(CWD
, save_cwd_file
);
1275 fclose(save_cwd_file
);