1 #define _XOPEN_SOURCE_EXTENDED
2 #define _FILE_OFFSET_BITS 64
10 #include <sys/types.h> /* pid_t, ... */
12 #include <limits.h> /* PATH_MAX */
13 #include <locale.h> /* setlocale(), LC_ALL */
14 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
15 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
17 #include <fcntl.h> /* open() */
18 #include <sys/wait.h> /* waitpid() */
19 #include <signal.h> /* struct sigaction, sigaction() */
27 static char BUF1
[BUFLEN
];
28 static char BUF2
[BUFLEN
];
29 static char INPUT
[BUFLEN
];
30 static wchar_t WBUF
[BUFLEN
];
32 /* Argument buffers for execvp(). */
34 static char *ARGS
[MAXARGS
];
36 /* Listing view parameters. */
37 #define HEIGHT (LINES-4)
38 #define STATUSPOS (COLS-16)
40 /* Listing view flags. */
41 #define SHOW_FILES 0x01u
42 #define SHOW_DIRS 0x02u
43 #define SHOW_HIDDEN 0x04u
45 /* Marks parameters. */
47 #define BULK_THRESH 256
49 /* Information associated to each entry in listing. */
58 /* Dynamic array of marked entries. */
59 typedef struct Marks
{
60 char dirpath
[PATH_MAX
];
66 /* Line editing state. */
68 wchar_t buffer
[BUFLEN
+1];
72 /* Global state. Some basic info is allocated for ten tabs. */
81 char cwd
[10][PATH_MAX
];
85 volatile sig_atomic_t pending_winch
;
88 /* Macros for accessing global state. */
89 #define ENAME(I) rover.rows[I].name
90 #define ESIZE(I) rover.rows[I].size
91 #define EMODE(I) rover.rows[I].mode
92 #define ISLINK(I) rover.rows[I].islink
93 #define MARKED(I) rover.rows[I].marked
94 #define SCROLL rover.scroll[rover.tab]
95 #define ESEL rover.esel[rover.tab]
96 #define FLAGS rover.flags[rover.tab]
97 #define CWD rover.cwd[rover.tab]
100 #define MIN(A, B) ((A) < (B) ? (A) : (B))
101 #define MAX(A, B) ((A) > (B) ? (A) : (B))
102 #define ISDIR(E) (strchr((E), '/') != NULL)
104 /* Line Editing Macros. */
105 #define EDIT_FULL(E) ((E).left == (E).right)
106 #define EDIT_CAN_LEFT(E) ((E).left)
107 #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
108 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
109 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
110 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
111 #define EDIT_BACKSPACE(E) (E).left--
112 #define EDIT_DELETE(E) (E).right++
113 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
115 typedef enum EditStat
{CONTINUE
, CONFIRM
, CANCEL
} EditStat
;
116 typedef enum Color
{DEFAULT
, RED
, GREEN
, YELLOW
, BLUE
, CYAN
, MAGENTA
, WHITE
, BLACK
} Color
;
117 typedef int (*PROCESS
)(const char *path
);
120 init_marks(Marks
*marks
)
122 strcpy(marks
->dirpath
, "");
123 marks
->bulk
= BULK_INIT
;
125 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
128 /* Unmark all entries. */
130 mark_none(Marks
*marks
)
134 strcpy(marks
->dirpath
, "");
135 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
136 if (marks
->entries
[i
]) {
137 free(marks
->entries
[i
]);
138 marks
->entries
[i
] = NULL
;
141 if (marks
->bulk
> BULK_THRESH
) {
142 /* Reset bulk to free some memory. */
143 free(marks
->entries
);
144 marks
->bulk
= BULK_INIT
;
145 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
150 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
154 if (!strcmp(marks
->dirpath
, dirpath
)) {
155 /* Append mark to directory. */
156 if (marks
->nentries
== marks
->bulk
) {
157 /* Expand bulk to accomodate new entry. */
158 int extra
= marks
->bulk
/ 2;
159 marks
->bulk
+= extra
; /* bulk *= 1.5; */
160 marks
->entries
= realloc(marks
->entries
,
161 marks
->bulk
* sizeof *marks
->entries
);
162 memset(&marks
->entries
[marks
->nentries
], 0,
163 extra
* sizeof *marks
->entries
);
166 /* Search for empty slot (there must be one). */
167 for (i
= 0; i
< marks
->bulk
; i
++)
168 if (!marks
->entries
[i
])
172 /* Directory changed. Discard old marks. */
174 strcpy(marks
->dirpath
, dirpath
);
177 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
178 strcpy(marks
->entries
[i
], entry
);
183 del_mark(Marks
*marks
, char *entry
)
187 if (marks
->nentries
> 1) {
188 for (i
= 0; i
< marks
->bulk
; i
++)
189 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
191 free(marks
->entries
[i
]);
192 marks
->entries
[i
] = NULL
;
199 free_marks(Marks
*marks
)
203 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
204 if (marks
->entries
[i
]) {
205 free(marks
->entries
[i
]);
208 free(marks
->entries
);
212 handle_winch(int sig
)
214 rover
.pending_winch
= 1;
222 memset(&sa
, 0, sizeof (struct sigaction
));
223 sa
.sa_handler
= handle_winch
;
224 sigaction(SIGWINCH
, &sa
, NULL
);
232 memset(&sa
, 0, sizeof (struct sigaction
));
233 sa
.sa_handler
= SIG_DFL
;
234 sigaction(SIGWINCH
, &sa
, NULL
);
237 static void update_view();
239 /* Handle any signals received since last call. */
243 if (rover
.pending_winch
) {
244 /* SIGWINCH received: resize application accordingly. */
245 delwin(rover
.window
);
249 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
250 if (HEIGHT
< rover
.nfiles
&& SCROLL
+ HEIGHT
> rover
.nfiles
)
251 SCROLL
= ESEL
- HEIGHT
;
253 rover
.pending_winch
= 0;
257 /* This function must be used in place of getch().
258 It handles signals while waiting for user input. */
264 while ((ch
= getch()) == ERR
)
269 /* This function must be used in place of get_wch().
270 It handles signals while waiting for user input. */
272 rover_get_wch(wint_t *wch
)
276 while ((ret
= get_wch(wch
)) == (wint_t) ERR
)
281 /* Do a fork-exec to external program (e.g. $EDITOR). */
288 setenv("RVSEL", rover
.nfiles
? ENAME(ESEL
) : "", 1);
291 /* fork() succeeded. */
294 waitpid(pid
, &status
, 0);
296 kill(getpid(), SIGWINCH
);
297 } else if (pid
== 0) {
299 execvp(ARGS
[0], ARGS
);
307 setlocale(LC_ALL
, "");
309 cbreak(); /* Get one character at a time. */
310 timeout(100); /* For getch(). */
312 nonl(); /* No NL->CR/NL on output. */
313 intrflush(stdscr
, FALSE
);
314 keypad(stdscr
, TRUE
);
315 curs_set(FALSE
); /* Hide blinking cursor. */
319 #ifdef NCURSES_EXT_FUNCS
320 use_default_colors();
325 init_pair(RED
, COLOR_RED
, bg
);
326 init_pair(GREEN
, COLOR_GREEN
, bg
);
327 init_pair(YELLOW
, COLOR_YELLOW
, bg
);
328 init_pair(BLUE
, COLOR_BLUE
, bg
);
329 init_pair(CYAN
, COLOR_CYAN
, bg
);
330 init_pair(MAGENTA
, COLOR_MAGENTA
, bg
);
331 init_pair(WHITE
, COLOR_WHITE
, bg
);
332 init_pair(BLACK
, COLOR_BLACK
, bg
);
334 atexit((void (*)(void)) endwin
);
338 /* Update the listing view. */
347 mvhline(0, 0, ' ', COLS
);
348 attr_on(A_BOLD
, NULL
);
349 color_set(RVC_TABNUM
, NULL
);
350 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
351 attr_off(A_BOLD
, NULL
);
352 if (rover
.marks
.nentries
) {
353 numsize
= snprintf(BUF1
, BUFLEN
, "%d", rover
.marks
.nentries
);
354 color_set(RVC_MARKS
, NULL
);
355 mvaddstr(0, COLS
- 3 - numsize
, BUF1
);
358 color_set(RVC_CWD
, NULL
);
359 mbstowcs(WBUF
, CWD
, PATH_MAX
);
360 mvaddnwstr(0, 0, WBUF
, COLS
- 4 - numsize
);
361 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
362 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
363 /* Selection might not be visible, due to cursor wrapping or window
364 shrinking. In that case, the scroll must be moved to make it visible. */
365 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
366 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
367 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
368 ishidden
= ENAME(j
)[0] == '.';
369 isdir
= S_ISDIR(EMODE(j
));
371 wattr_on(rover
.window
, A_REVERSE
, NULL
);
373 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
375 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
377 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
379 wcolor_set(rover
.window
, RVC_FILE
, NULL
);
381 char *suffix
, *suffixes
= "BKMGTPEZY";
382 off_t human_size
= ESIZE(j
) * 10;
383 int length
= mbstowcs(NULL
, ENAME(j
), 0);
384 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
385 human_size
= (human_size
+ 512) / 1024;
387 swprintf(WBUF
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
388 (int) (COLS
- length
- 6),
389 (int) human_size
/ 10, *suffix
);
391 swprintf(WBUF
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
392 (int) (COLS
- length
- 8),
393 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
395 mbstowcs(WBUF
, ENAME(j
), PATH_MAX
);
396 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
397 mvwaddnwstr(rover
.window
, i
+ 1, 2, WBUF
, COLS
- 4);
398 if (marking
&& MARKED(j
)) {
399 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
400 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
402 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
404 wattr_off(rover
.window
, A_REVERSE
, NULL
);
406 for (; i
< HEIGHT
; i
++)
407 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
408 if (rover
.nfiles
> HEIGHT
) {
410 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
411 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
412 if (!height
) height
= 1;
413 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
414 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
416 BUF1
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
417 BUF1
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
418 BUF1
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
422 snprintf(BUF2
, BUFLEN
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
423 snprintf(BUF1
+3, BUFLEN
-3, "%12s", BUF2
);
424 color_set(RVC_STATUS
, NULL
);
425 mvaddstr(LINES
- 1, STATUSPOS
, BUF1
);
426 wrefresh(rover
.window
);
429 /* Show a message on the status bar. */
431 message(const char *msg
, Color color
)
436 pos
= (STATUSPOS
- len
) / 2;
437 attr_on(A_BOLD
, NULL
);
438 color_set(color
, NULL
);
439 mvaddstr(LINES
- 1, pos
, msg
);
440 color_set(DEFAULT
, NULL
);
441 attr_off(A_BOLD
, NULL
);
444 /* Clear message area, leaving only status info. */
448 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
451 /* Comparison used to sort listing entries. */
453 rowcmp(const void *a
, const void *b
)
455 int isdir1
, isdir2
, cmpdir
;
458 isdir1
= S_ISDIR(r1
->mode
);
459 isdir2
= S_ISDIR(r2
->mode
);
460 cmpdir
= isdir2
- isdir1
;
461 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
464 /* Get all entries in current working directory. */
466 ls(Row
**rowsp
, uint8_t flags
)
474 if(!(dp
= opendir("."))) return -1;
475 n
= -2; /* We don't want the entries "." and "..". */
476 while (readdir(dp
)) n
++;
478 rows
= malloc(n
* sizeof *rows
);
480 while ((ep
= readdir(dp
))) {
481 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
483 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
485 lstat(ep
->d_name
, &statbuf
);
486 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
487 stat(ep
->d_name
, &statbuf
);
488 if (S_ISDIR(statbuf
.st_mode
)) {
489 if (flags
& SHOW_DIRS
) {
490 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
491 strcpy(rows
[i
].name
, ep
->d_name
);
492 strcat(rows
[i
].name
, "/");
493 rows
[i
].mode
= statbuf
.st_mode
;
496 } else if (flags
& SHOW_FILES
) {
497 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
498 strcpy(rows
[i
].name
, ep
->d_name
);
499 rows
[i
].size
= statbuf
.st_size
;
500 rows
[i
].mode
= statbuf
.st_mode
;
504 n
= i
; /* Ignore unused space in array caused by filters. */
505 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
512 free_rows(Row
**rowsp
, int nfiles
)
516 for (i
= 0; i
< nfiles
; i
++)
517 free((*rowsp
)[i
].name
);
522 /* Change working directory to the path in CWD. */
528 message("Loading...", CYAN
);
530 if (reset
) ESEL
= SCROLL
= 0;
533 free_rows(&rover
.rows
, rover
.nfiles
);
534 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
535 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
536 for (i
= 0; i
< rover
.nfiles
; i
++) {
537 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
539 rover
.marks
.entries
[j
] &&
540 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
543 MARKED(i
) = j
< rover
.marks
.bulk
;
546 for (i
= 0; i
< rover
.nfiles
; i
++)
552 /* Select a target entry, if it is present. */
554 try_to_sel(const char *target
)
558 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
560 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
562 if (rover
.nfiles
> HEIGHT
) {
563 SCROLL
= ESEL
- HEIGHT
/ 2;
564 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
568 /* Reload CWD, but try to keep selection. */
573 strcpy(INPUT
, ENAME(ESEL
));
581 /* Recursively process a source directory using CWD as destination root.
582 For each node (i.e. directory), do the following:
583 1. call pre(destination);
584 2. call proc() on every child leaf (i.e. files);
585 3. recurse into every child node;
587 E.g. to move directory /src/ (and all its contents) inside /dst/:
588 strcpy(CWD, "/dst/");
589 process_dir(adddir, movfile, deldir, "/src/"); */
591 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
597 char subpath
[PATH_MAX
];
601 char dstpath
[PATH_MAX
];
602 strcpy(dstpath
, CWD
);
603 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
606 if(!(dp
= opendir(path
))) return -1;
607 while ((ep
= readdir(dp
))) {
608 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
610 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
611 stat(subpath
, &statbuf
);
612 if (S_ISDIR(statbuf
.st_mode
)) {
613 strcat(subpath
, "/");
614 ret
|= process_dir(pre
, proc
, pos
, subpath
);
616 ret
|= proc(subpath
);
619 if (pos
) ret
|= pos(path
);
623 /* Process all marked entries using CWD as destination root.
624 All marked entries that are directories will be recursively processed.
625 See process_dir() for details on the parameters. */
627 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
)
633 message("Processing...", CYAN
);
635 for (i
= 0; i
< rover
.marks
.bulk
; i
++)
636 if (rover
.marks
.entries
[i
]) {
638 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, rover
.marks
.entries
[i
]);
639 if (ISDIR(rover
.marks
.entries
[i
])) {
640 if (!strncmp(path
, CWD
, strlen(path
)))
643 ret
= process_dir(pre
, proc
, pos
, path
);
646 if (!ret
) del_mark(&rover
.marks
, rover
.marks
.entries
[i
]);
649 if (!rover
.marks
.nentries
)
650 message("Done.", GREEN
);
652 message("Some errors occured.", RED
);
655 /* Wrappers for file operations. */
656 static PROCESS delfile
= unlink
;
657 static PROCESS deldir
= rmdir
;
658 static int addfile(const char *path
) {
659 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
662 ret
= creat(path
, 0644);
663 if (ret
< 0) return ret
;
666 static int cpyfile(const char *srcpath
) {
671 char dstpath
[PATH_MAX
];
673 ret
= src
= open(srcpath
, O_RDONLY
);
674 if (ret
< 0) return ret
;
675 ret
= fstat(src
, &st
);
676 if (ret
< 0) return ret
;
677 strcpy(dstpath
, CWD
);
678 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
679 ret
= dst
= creat(dstpath
, st
.st_mode
);
680 if (ret
< 0) return ret
;
681 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
682 write(dst
, buf
, size
);
689 static int adddir(const char *path
) {
693 ret
= stat(CWD
, &st
);
694 if (ret
< 0) return ret
;
695 return mkdir(path
, st
.st_mode
);
697 static int movfile(const char *srcpath
) {
699 char dstpath
[PATH_MAX
];
701 strcpy(dstpath
, CWD
);
702 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
703 ret
= rename(srcpath
, dstpath
);
704 if (ret
< 0 && errno
== EXDEV
) {
705 ret
= cpyfile(srcpath
);
706 if (ret
< 0) return ret
;
707 ret
= delfile(srcpath
);
713 start_line_edit(const char *init_input
)
716 strncpy(INPUT
, init_input
, BUFLEN
);
717 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, BUFLEN
);
718 rover
.edit
.right
= BUFLEN
- 1;
719 rover
.edit
.buffer
[BUFLEN
] = L
'\0';
720 rover
.edit_scroll
= 0;
723 /* Read input and change editing state accordingly. */
727 wchar_t eraser
, killer
, wch
;
730 ret
= rover_get_wch((wint_t *) &wch
);
733 if (ret
== KEY_CODE_YES
) {
734 if (wch
== KEY_ENTER
) {
737 } else if (wch
== KEY_LEFT
) {
738 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
739 } else if (wch
== KEY_RIGHT
) {
740 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
741 } else if (wch
== KEY_UP
) {
742 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
743 } else if (wch
== KEY_DOWN
) {
744 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
745 } else if (wch
== KEY_BACKSPACE
) {
746 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
747 } else if (wch
== KEY_DC
) {
748 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
751 if (wch
== L
'\r' || wch
== L
'\n') {
754 } else if (wch
== L
'\t') {
757 } else if (wch
== eraser
) {
758 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
759 } else if (wch
== killer
) {
760 EDIT_CLEAR(rover
.edit
);
762 } else if (iswprint(wch
)) {
763 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
766 /* Encode edit contents in INPUT. */
767 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
768 length
= wcstombs(INPUT
, rover
.edit
.buffer
, BUFLEN
);
769 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
774 /* Update line input on the screen. */
776 update_input(char *prompt
, Color color
)
778 int plen
, ilen
, maxlen
;
780 plen
= strlen(prompt
);
781 ilen
= mbstowcs(NULL
, INPUT
, 0);
782 maxlen
= STATUSPOS
- plen
- 2;
783 if (ilen
- rover
.edit_scroll
< maxlen
)
784 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
785 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
786 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
787 else if (rover
.edit
.left
< rover
.edit_scroll
)
788 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
789 color_set(RVC_PROMPT
, NULL
);
790 mvaddstr(LINES
- 1, 0, prompt
);
791 color_set(color
, NULL
);
792 mbstowcs(WBUF
, INPUT
, COLS
);
793 mvaddnwstr(LINES
- 1, plen
, &WBUF
[rover
.edit_scroll
], maxlen
);
794 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
795 color_set(DEFAULT
, NULL
);
796 if (rover
.edit_scroll
)
797 mvaddch(LINES
- 1, plen
- 1, '<');
798 if (ilen
> rover
.edit_scroll
+ maxlen
)
799 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
800 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
804 main(int argc
, char *argv
[])
811 const char *save_cwd_file
= NULL
;
814 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
815 printf("rover %s\n", RV_VERSION
);
817 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
819 "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
820 " or: rover [OPTION]\n"
821 "Browse current working directory or the ones specified.\n\n"
823 " -h, --help print this help message and exit\n"
824 " -v, --version print program version and exit\n\n"
825 "See rover(1) for more information.\n\n"
826 "Rover homepage: <https://github.com/lecram/rover>.\n"
829 } else if (argc
> 2 && !strcmp(argv
[1], "--save-cwd")) {
830 save_cwd_file
= argv
[2];
831 argc
-= 2; argv
+= 2;
836 for (i
= 0; i
< 10; i
++) {
837 rover
.esel
[i
] = rover
.scroll
[i
] = 0;
838 rover
.flags
[i
] = SHOW_FILES
| SHOW_DIRS
;
840 strcpy(rover
.cwd
[0], getenv("HOME"));
841 for (i
= 1; i
< argc
&& i
< 10; i
++) {
842 if ((d
= opendir(argv
[i
]))) {
843 realpath(argv
[i
], rover
.cwd
[i
]);
846 strcpy(rover
.cwd
[i
], rover
.cwd
[0]);
848 getcwd(rover
.cwd
[i
], PATH_MAX
);
849 for (i
++; i
< 10; i
++)
850 strcpy(rover
.cwd
[i
], rover
.cwd
[i
-1]);
851 for (i
= 0; i
< 10; i
++)
852 if (rover
.cwd
[i
][strlen(rover
.cwd
[i
]) - 1] != '/')
853 strcat(rover
.cwd
[i
], "/");
855 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
856 init_marks(&rover
.marks
);
862 if (!strcmp(key
, RVK_QUIT
)) break;
863 else if (ch
>= '0' && ch
<= '9') {
864 rover
.tab
= ch
- '0';
866 } else if (!strcmp(key
, RVK_HELP
)) {
871 } else if (!strcmp(key
, RVK_DOWN
)) {
872 if (!rover
.nfiles
) continue;
873 ESEL
= MIN(ESEL
+ 1, rover
.nfiles
- 1);
875 } else if (!strcmp(key
, RVK_UP
)) {
876 if (!rover
.nfiles
) continue;
877 ESEL
= MAX(ESEL
- 1, 0);
879 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
880 if (!rover
.nfiles
) continue;
881 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
882 if (rover
.nfiles
> HEIGHT
)
883 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
885 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
886 if (!rover
.nfiles
) continue;
887 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
888 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
890 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
891 if (!rover
.nfiles
) continue;
894 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
895 if (!rover
.nfiles
) continue;
896 ESEL
= rover
.nfiles
- 1;
898 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
899 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
900 strcat(CWD
, ENAME(ESEL
));
902 } else if (!strcmp(key
, RVK_CD_UP
)) {
903 char *dirname
, first
;
904 if (!strcmp(CWD
, "/")) continue;
905 CWD
[strlen(CWD
) - 1] = '\0';
906 dirname
= strrchr(CWD
, '/') + 1;
911 dirname
[strlen(dirname
)] = '/';
915 } else if (!strcmp(key
, RVK_HOME
)) {
916 strcpy(CWD
, getenv("HOME"));
917 if (CWD
[strlen(CWD
) - 1] != '/')
920 } else if (!strcmp(key
, RVK_REFRESH
)) {
922 } else if (!strcmp(key
, RVK_SHELL
)) {
923 program
= getenv("SHELL");
930 } else if (!strcmp(key
, RVK_VIEW
)) {
931 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
932 program
= getenv("PAGER");
935 ARGS
[1] = ENAME(ESEL
);
939 } else if (!strcmp(key
, RVK_EDIT
)) {
940 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
941 program
= getenv("EDITOR");
944 ARGS
[1] = ENAME(ESEL
);
949 } else if (!strcmp(key
, RVK_SEARCH
)) {
950 int oldsel
, oldscroll
, length
;
951 char *prompt
= "search: ";
952 if (!rover
.nfiles
) continue;
956 update_input(prompt
, RED
);
957 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
960 length
= strlen(INPUT
);
962 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
963 if (!strncmp(ENAME(sel
), INPUT
, length
))
965 if (sel
< rover
.nfiles
) {
968 if (rover
.nfiles
> HEIGHT
) {
971 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
972 SCROLL
= rover
.nfiles
- HEIGHT
;
982 update_input(prompt
, color
);
984 if (edit_stat
== CANCEL
) {
990 } else if (!strcmp(key
, RVK_TG_FILES
)) {
993 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
996 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
997 FLAGS
^= SHOW_HIDDEN
;
999 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1001 char *prompt
= "new file: ";
1002 start_line_edit("");
1003 update_input(prompt
, RED
);
1004 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1005 int length
= strlen(INPUT
);
1007 for (i
= 0; i
< rover
.nfiles
; i
++) {
1009 !strncmp(ENAME(i
), INPUT
, length
) &&
1010 (!strcmp(ENAME(i
) + length
, "") ||
1011 !strcmp(ENAME(i
) + length
, "/"))
1017 update_input(prompt
, ok
? GREEN
: RED
);
1020 if (edit_stat
== CONFIRM
) {
1027 message("File already exists.", RED
);
1029 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1031 char *prompt
= "new directory: ";
1032 start_line_edit("");
1033 update_input(prompt
, RED
);
1034 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1035 int length
= strlen(INPUT
);
1037 for (i
= 0; i
< rover
.nfiles
; i
++) {
1039 !strncmp(ENAME(i
), INPUT
, length
) &&
1040 (!strcmp(ENAME(i
) + length
, "") ||
1041 !strcmp(ENAME(i
) + length
, "/"))
1047 update_input(prompt
, ok
? GREEN
: RED
);
1050 if (edit_stat
== CONFIRM
) {
1058 message("File already exists.", RED
);
1060 } else if (!strcmp(key
, RVK_RENAME
)) {
1062 char *prompt
= "rename: ";
1065 strcpy(INPUT
, ENAME(ESEL
));
1066 last
= INPUT
+ strlen(INPUT
) - 1;
1067 if ((isdir
= *last
== '/'))
1069 start_line_edit(INPUT
);
1070 update_input(prompt
, RED
);
1071 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1072 int length
= strlen(INPUT
);
1074 for (i
= 0; i
< rover
.nfiles
; i
++)
1076 !strncmp(ENAME(i
), INPUT
, length
) &&
1077 (!strcmp(ENAME(i
) + length
, "") ||
1078 !strcmp(ENAME(i
) + length
, "/"))
1083 update_input(prompt
, ok
? GREEN
: RED
);
1086 if (edit_stat
== CONFIRM
) {
1090 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1091 del_mark(&rover
.marks
, ENAME(ESEL
));
1092 add_mark(&rover
.marks
, CWD
, INPUT
);
1098 message("File already exists.", RED
);
1100 } else if (!strcmp(key
, RVK_DELETE
)) {
1102 message("Delete selected entry? (Y to confirm)", YELLOW
);
1103 if (rover_getch() == 'Y') {
1104 const char *name
= ENAME(ESEL
);
1105 int ret
= S_ISDIR(EMODE(ESEL
)) ? deldir(name
) : delfile(name
);
1108 message("Could not delete entry.", RED
);
1112 message("No entry selected for deletion.", RED
);
1113 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1115 del_mark(&rover
.marks
, ENAME(ESEL
));
1117 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1118 MARKED(ESEL
) = !MARKED(ESEL
);
1119 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1121 } else if (!strcmp(key
, RVK_INVMARK
)) {
1122 for (i
= 0; i
< rover
.nfiles
; i
++) {
1124 del_mark(&rover
.marks
, ENAME(i
));
1126 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1127 MARKED(i
) = !MARKED(i
);
1130 } else if (!strcmp(key
, RVK_MARKALL
)) {
1131 for (i
= 0; i
< rover
.nfiles
; i
++)
1133 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1137 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1138 if (rover
.marks
.nentries
) {
1139 message("Delete marked entries? (Y to confirm)", YELLOW
);
1140 if (rover_getch() == 'Y')
1141 process_marked(NULL
, delfile
, deldir
);
1145 message("No entries marked for deletion.", RED
);
1146 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1147 if (rover
.marks
.nentries
)
1148 process_marked(adddir
, cpyfile
, NULL
);
1150 message("No entries marked for copying.", RED
);
1151 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1152 if (rover
.marks
.nentries
)
1153 process_marked(adddir
, movfile
, deldir
);
1155 message("No entries marked for moving.", RED
);
1159 free_rows(&rover
.rows
, rover
.nfiles
);
1160 free_marks(&rover
.marks
);
1161 delwin(rover
.window
);
1162 if (save_cwd_file
!= NULL
) {
1163 FILE *fd
= fopen(save_cwd_file
, "w");