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 ROW
[ROWSZ
];
29 static char STATUS
[STATUSSZ
];
31 static char INPUT
[INPUTSZ
];
33 /* Argument buffers for execvp(). */
35 static char *ARGS
[MAXARGS
];
37 /* Listing view parameters. */
38 #define HEIGHT (LINES-4)
39 #define STATUSPOS (COLS-16)
41 /* Listing view flags. */
42 #define SHOW_FILES 0x01u
43 #define SHOW_DIRS 0x02u
44 #define SHOW_HIDDEN 0x04u
46 /* Marks parameters. */
48 #define BULK_THRESH 256
50 /* Information associated to each entry in listing. */
59 /* Dynamic array of marked entries. */
60 typedef struct Marks
{
61 char dirpath
[PATH_MAX
];
67 /* Line editing state. */
69 wchar_t buffer
[INPUTSZ
+1];
73 /* Global state. Some basic info is allocated for ten tabs. */
82 char cwd
[10][PATH_MAX
];
86 volatile sig_atomic_t pending_winch
;
89 /* Macros for accessing global state. */
90 #define ENAME(I) rover.rows[I].name
91 #define ESIZE(I) rover.rows[I].size
92 #define EMODE(I) rover.rows[I].mode
93 #define ISLINK(I) rover.rows[I].islink
94 #define MARKED(I) rover.rows[I].marked
95 #define SCROLL rover.scroll[rover.tab]
96 #define ESEL rover.esel[rover.tab]
97 #define FLAGS rover.flags[rover.tab]
98 #define CWD rover.cwd[rover.tab]
101 #define MIN(A, B) ((A) < (B) ? (A) : (B))
102 #define MAX(A, B) ((A) > (B) ? (A) : (B))
103 #define ISDIR(E) (strchr((E), '/') != NULL)
105 /* Line Editing Macros. */
106 #define EDIT_FULL(E) ((E).left == (E).right)
107 #define EDIT_CAN_LEFT(E) ((E).left)
108 #define EDIT_CAN_RIGHT(E) ((E).right < INPUTSZ-1)
109 #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
110 #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
111 #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
112 #define EDIT_BACKSPACE(E) (E).left--
113 #define EDIT_DELETE(E) (E).right++
114 #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = INPUTSZ-1; } while(0)
116 typedef enum EditStat
{CONTINUE
, CONFIRM
, CANCEL
} EditStat
;
117 typedef enum Color
{DEFAULT
, RED
, GREEN
, YELLOW
, BLUE
, CYAN
, MAGENTA
, WHITE
, BLACK
} Color
;
118 typedef int (*PROCESS
)(const char *path
);
121 init_marks(Marks
*marks
)
123 strcpy(marks
->dirpath
, "");
124 marks
->bulk
= BULK_INIT
;
126 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
129 /* Unmark all entries. */
131 mark_none(Marks
*marks
)
135 strcpy(marks
->dirpath
, "");
136 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
137 if (marks
->entries
[i
]) {
138 free(marks
->entries
[i
]);
139 marks
->entries
[i
] = NULL
;
142 if (marks
->bulk
> BULK_THRESH
) {
143 /* Reset bulk to free some memory. */
144 free(marks
->entries
);
145 marks
->bulk
= BULK_INIT
;
146 marks
->entries
= calloc(marks
->bulk
, sizeof *marks
->entries
);
151 add_mark(Marks
*marks
, char *dirpath
, char *entry
)
155 if (!strcmp(marks
->dirpath
, dirpath
)) {
156 /* Append mark to directory. */
157 if (marks
->nentries
== marks
->bulk
) {
158 /* Expand bulk to accomodate new entry. */
159 int extra
= marks
->bulk
/ 2;
160 marks
->bulk
+= extra
; /* bulk *= 1.5; */
161 marks
->entries
= realloc(marks
->entries
,
162 marks
->bulk
* sizeof *marks
->entries
);
163 memset(&marks
->entries
[marks
->nentries
], 0,
164 extra
* sizeof *marks
->entries
);
167 /* Search for empty slot (there must be one). */
168 for (i
= 0; i
< marks
->bulk
; i
++)
169 if (!marks
->entries
[i
])
173 /* Directory changed. Discard old marks. */
175 strcpy(marks
->dirpath
, dirpath
);
178 marks
->entries
[i
] = malloc(strlen(entry
) + 1);
179 strcpy(marks
->entries
[i
], entry
);
184 del_mark(Marks
*marks
, char *entry
)
188 if (marks
->nentries
> 1) {
189 for (i
= 0; i
< marks
->bulk
; i
++)
190 if (marks
->entries
[i
] && !strcmp(marks
->entries
[i
], entry
))
192 free(marks
->entries
[i
]);
193 marks
->entries
[i
] = NULL
;
200 free_marks(Marks
*marks
)
204 for (i
= 0; i
< marks
->bulk
&& marks
->nentries
; i
++)
205 if (marks
->entries
[i
]) {
206 free(marks
->entries
[i
]);
209 free(marks
->entries
);
213 handle_winch(int sig
)
215 rover
.pending_winch
= 1;
223 memset(&sa
, 0, sizeof (struct sigaction
));
224 sa
.sa_handler
= handle_winch
;
225 sigaction(SIGWINCH
, &sa
, NULL
);
233 memset(&sa
, 0, sizeof (struct sigaction
));
234 sa
.sa_handler
= SIG_DFL
;
235 sigaction(SIGWINCH
, &sa
, NULL
);
238 static void update_view();
240 /* Handle any signals received since last call. */
244 if (rover
.pending_winch
) {
245 /* SIGWINCH received: resize application accordingly. */
246 delwin(rover
.window
);
250 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
251 SCROLL
= MAX(ESEL
- HEIGHT
, 0);
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. */
346 wchar_t wbuf
[PATH_MAX
];
348 mvhline(0, 0, ' ', COLS
);
349 attr_on(A_BOLD
, NULL
);
350 color_set(RVC_TABNUM
, NULL
);
351 mvaddch(0, COLS
- 2, rover
.tab
+ '0');
352 attr_off(A_BOLD
, NULL
);
353 if (rover
.marks
.nentries
) {
354 numsize
= snprintf(STATUS
, STATUSSZ
, "%d", rover
.marks
.nentries
);
355 color_set(RVC_MARKS
, NULL
);
356 mvaddstr(0, COLS
- 3 - numsize
, STATUS
);
359 color_set(RVC_CWD
, NULL
);
360 mbstowcs(wbuf
, CWD
, PATH_MAX
);
361 mvaddnwstr(0, 0, wbuf
, COLS
- 4 - numsize
);
362 wcolor_set(rover
.window
, RVC_BORDER
, NULL
);
363 wborder(rover
.window
, 0, 0, 0, 0, 0, 0, 0, 0);
364 /* Selection might not be visible, due to cursor wrapping or window
365 shrinking. In that case, the scroll must be moved to make it visible. */
366 SCROLL
= MAX(MIN(SCROLL
, ESEL
), ESEL
- HEIGHT
+ 1);
367 marking
= !strcmp(CWD
, rover
.marks
.dirpath
);
368 for (i
= 0, j
= SCROLL
; i
< HEIGHT
&& j
< rover
.nfiles
; i
++, j
++) {
369 ishidden
= ENAME(j
)[0] == '.';
370 isdir
= S_ISDIR(EMODE(j
));
372 wattr_on(rover
.window
, A_REVERSE
, NULL
);
374 wcolor_set(rover
.window
, RVC_LINK
, NULL
);
376 wcolor_set(rover
.window
, RVC_HIDDEN
, NULL
);
378 wcolor_set(rover
.window
, RVC_DIR
, NULL
);
380 wcolor_set(rover
.window
, RVC_FILE
, NULL
);
382 char *suffix
, *suffixes
= "BKMGTPEZY";
383 off_t human_size
= ESIZE(j
) * 10;
384 int length
= mbstowcs(NULL
, ENAME(j
), 0);
385 for (suffix
= suffixes
; human_size
>= 10240; suffix
++)
386 human_size
= (human_size
+ 512) / 1024;
388 swprintf(wbuf
, PATH_MAX
, L
"%s%*d %c", ENAME(j
),
389 (int) (COLS
- length
- 6),
390 (int) human_size
/ 10, *suffix
);
392 swprintf(wbuf
, PATH_MAX
, L
"%s%*d.%d %c", ENAME(j
),
393 (int) (COLS
- length
- 8),
394 (int) human_size
/ 10, (int) human_size
% 10, *suffix
);
396 mbstowcs(wbuf
, ENAME(j
), PATH_MAX
);
397 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
398 mvwaddnwstr(rover
.window
, i
+ 1, 2, wbuf
, COLS
- 4);
399 if (marking
&& MARKED(j
)) {
400 wcolor_set(rover
.window
, RVC_MARKS
, NULL
);
401 mvwaddch(rover
.window
, i
+ 1, 1, RVS_MARK
);
403 mvwaddch(rover
.window
, i
+ 1, 1, ' ');
405 wattr_off(rover
.window
, A_REVERSE
, NULL
);
407 for (; i
< HEIGHT
; i
++)
408 mvwhline(rover
.window
, i
+ 1, 1, ' ', COLS
- 2);
409 if (rover
.nfiles
> HEIGHT
) {
411 center
= (SCROLL
+ HEIGHT
/ 2) * HEIGHT
/ rover
.nfiles
;
412 height
= (HEIGHT
-1) * HEIGHT
/ rover
.nfiles
;
413 if (!height
) height
= 1;
414 wcolor_set(rover
.window
, RVC_SCROLLBAR
, NULL
);
415 mvwvline(rover
.window
, center
-height
/2+1, COLS
-1, RVS_SCROLLBAR
, height
);
417 STATUS
[0] = FLAGS
& SHOW_FILES
? 'F' : ' ';
418 STATUS
[1] = FLAGS
& SHOW_DIRS
? 'D' : ' ';
419 STATUS
[2] = FLAGS
& SHOW_HIDDEN
? 'H' : ' ';
423 snprintf(ROW
, ROWSZ
, "%d/%d", ESEL
+ 1, rover
.nfiles
);
424 snprintf(STATUS
+3, STATUSSZ
-3, "%12s", ROW
);
425 color_set(RVC_STATUS
, NULL
);
426 mvaddstr(LINES
- 1, STATUSPOS
, STATUS
);
427 wrefresh(rover
.window
);
430 /* Show a message on the status bar. */
432 message(const char *msg
, Color color
)
437 pos
= (STATUSPOS
- len
) / 2;
438 attr_on(A_BOLD
, NULL
);
439 color_set(color
, NULL
);
440 mvaddstr(LINES
- 1, pos
, msg
);
441 color_set(DEFAULT
, NULL
);
442 attr_off(A_BOLD
, NULL
);
445 /* Clear message area, leaving only status info. */
449 mvhline(LINES
- 1, 0, ' ', STATUSPOS
);
452 /* Comparison used to sort listing entries. */
454 rowcmp(const void *a
, const void *b
)
456 int isdir1
, isdir2
, cmpdir
;
459 isdir1
= S_ISDIR(r1
->mode
);
460 isdir2
= S_ISDIR(r2
->mode
);
461 cmpdir
= isdir2
- isdir1
;
462 return cmpdir
? cmpdir
: strcoll(r1
->name
, r2
->name
);
465 /* Get all entries in current working directory. */
467 ls(Row
**rowsp
, uint8_t flags
)
475 if(!(dp
= opendir("."))) return -1;
476 n
= -2; /* We don't want the entries "." and "..". */
477 while (readdir(dp
)) n
++;
479 rows
= malloc(n
* sizeof *rows
);
481 while ((ep
= readdir(dp
))) {
482 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
484 if (!(flags
& SHOW_HIDDEN
) && ep
->d_name
[0] == '.')
486 lstat(ep
->d_name
, &statbuf
);
487 rows
[i
].islink
= S_ISLNK(statbuf
.st_mode
);
488 stat(ep
->d_name
, &statbuf
);
489 if (S_ISDIR(statbuf
.st_mode
)) {
490 if (flags
& SHOW_DIRS
) {
491 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 2);
492 strcpy(rows
[i
].name
, ep
->d_name
);
493 strcat(rows
[i
].name
, "/");
494 rows
[i
].mode
= statbuf
.st_mode
;
497 } else if (flags
& SHOW_FILES
) {
498 rows
[i
].name
= malloc(strlen(ep
->d_name
) + 1);
499 strcpy(rows
[i
].name
, ep
->d_name
);
500 rows
[i
].size
= statbuf
.st_size
;
501 rows
[i
].mode
= statbuf
.st_mode
;
505 n
= i
; /* Ignore unused space in array caused by filters. */
506 qsort(rows
, n
, sizeof (*rows
), rowcmp
);
513 free_rows(Row
**rowsp
, int nfiles
)
517 for (i
= 0; i
< nfiles
; i
++)
518 free((*rowsp
)[i
].name
);
523 /* Change working directory to the path in CWD. */
529 message("Loading...", CYAN
);
531 if (reset
) ESEL
= SCROLL
= 0;
534 free_rows(&rover
.rows
, rover
.nfiles
);
535 rover
.nfiles
= ls(&rover
.rows
, FLAGS
);
536 if (!strcmp(CWD
, rover
.marks
.dirpath
)) {
537 for (i
= 0; i
< rover
.nfiles
; i
++) {
538 for (j
= 0; j
< rover
.marks
.bulk
; j
++)
540 rover
.marks
.entries
[j
] &&
541 !strcmp(rover
.marks
.entries
[j
], ENAME(i
))
544 MARKED(i
) = j
< rover
.marks
.bulk
;
547 for (i
= 0; i
< rover
.nfiles
; i
++)
553 /* Select a target entry, if it is present. */
555 try_to_sel(const char *target
)
559 while ((ESEL
+1) < rover
.nfiles
&& S_ISDIR(EMODE(ESEL
)))
561 while ((ESEL
+1) < rover
.nfiles
&& strcoll(ENAME(ESEL
), target
) < 0)
563 if (rover
.nfiles
> HEIGHT
) {
564 SCROLL
= ESEL
- HEIGHT
/ 2;
565 SCROLL
= MIN(MAX(SCROLL
, 0), rover
.nfiles
- HEIGHT
);
569 /* Reload CWD, but try to keep selection. */
574 strcpy(INPUT
, ENAME(ESEL
));
582 /* Recursively process a source directory using CWD as destination root.
583 For each node (i.e. directory), do the following:
584 1. call pre(destination);
585 2. call proc() on every child leaf (i.e. files);
586 3. recurse into every child node;
588 E.g. to move directory /src/ (and all its contents) inside /dst/:
589 strcpy(CWD, "/dst/");
590 process_dir(adddir, movfile, deldir, "/src/"); */
592 process_dir(PROCESS pre
, PROCESS proc
, PROCESS pos
, const char *path
)
598 char subpath
[PATH_MAX
];
602 char dstpath
[PATH_MAX
];
603 strcpy(dstpath
, CWD
);
604 strcat(dstpath
, path
+ strlen(rover
.marks
.dirpath
));
607 if(!(dp
= opendir(path
))) return -1;
608 while ((ep
= readdir(dp
))) {
609 if (!strcmp(ep
->d_name
, ".") || !strcmp(ep
->d_name
, ".."))
611 snprintf(subpath
, PATH_MAX
, "%s%s", path
, ep
->d_name
);
612 stat(subpath
, &statbuf
);
613 if (S_ISDIR(statbuf
.st_mode
)) {
614 strcat(subpath
, "/");
615 ret
|= process_dir(pre
, proc
, pos
, subpath
);
617 ret
|= proc(subpath
);
620 if (pos
) ret
|= pos(path
);
624 /* Process all marked entries using CWD as destination root.
625 All marked entries that are directories will be recursively processed.
626 See process_dir() for details on the parameters. */
628 process_marked(PROCESS pre
, PROCESS proc
, PROCESS pos
)
634 message("Processing...", CYAN
);
636 for (i
= 0; i
< rover
.marks
.bulk
; i
++)
637 if (rover
.marks
.entries
[i
]) {
639 snprintf(path
, PATH_MAX
, "%s%s", rover
.marks
.dirpath
, rover
.marks
.entries
[i
]);
640 if (ISDIR(rover
.marks
.entries
[i
])) {
641 if (!strncmp(path
, CWD
, strlen(path
)))
644 ret
= process_dir(pre
, proc
, pos
, path
);
647 if (!ret
) del_mark(&rover
.marks
, rover
.marks
.entries
[i
]);
650 if (!rover
.marks
.nentries
)
651 message("Done.", GREEN
);
653 message("Some errors occured.", RED
);
656 /* Wrappers for file operations. */
657 static PROCESS delfile
= unlink
;
658 static PROCESS deldir
= rmdir
;
659 static int addfile(const char *path
) {
660 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
663 ret
= creat(path
, 0644);
664 if (ret
< 0) return ret
;
667 static int cpyfile(const char *srcpath
) {
672 char dstpath
[PATH_MAX
];
674 ret
= src
= open(srcpath
, O_RDONLY
);
675 if (ret
< 0) return ret
;
676 ret
= fstat(src
, &st
);
677 if (ret
< 0) return ret
;
678 strcpy(dstpath
, CWD
);
679 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
680 ret
= dst
= creat(dstpath
, st
.st_mode
);
681 if (ret
< 0) return ret
;
682 while ((size
= read(src
, buf
, BUFSIZ
)) > 0) {
683 write(dst
, buf
, size
);
690 static int adddir(const char *path
) {
694 ret
= stat(CWD
, &st
);
695 if (ret
< 0) return ret
;
696 return mkdir(path
, st
.st_mode
);
698 static int movfile(const char *srcpath
) {
700 char dstpath
[PATH_MAX
];
702 strcpy(dstpath
, CWD
);
703 strcat(dstpath
, srcpath
+ strlen(rover
.marks
.dirpath
));
704 ret
= rename(srcpath
, dstpath
);
705 if (ret
< 0 && errno
== EXDEV
) {
706 ret
= cpyfile(srcpath
);
707 if (ret
< 0) return ret
;
708 ret
= delfile(srcpath
);
714 start_line_edit(const char *init_input
)
717 strncpy(INPUT
, init_input
, INPUTSZ
);
718 rover
.edit
.left
= mbstowcs(rover
.edit
.buffer
, init_input
, INPUTSZ
);
719 rover
.edit
.right
= INPUTSZ
- 1;
720 rover
.edit
.buffer
[INPUTSZ
] = L
'\0';
721 rover
.edit_scroll
= 0;
724 /* Read input and change editing state accordingly. */
728 wchar_t eraser
, killer
, wch
;
731 ret
= rover_get_wch((wint_t *) &wch
);
734 if (ret
== KEY_CODE_YES
) {
735 if (wch
== KEY_ENTER
) {
738 } else if (wch
== KEY_LEFT
) {
739 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
740 } else if (wch
== KEY_RIGHT
) {
741 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
742 } else if (wch
== KEY_UP
) {
743 while (EDIT_CAN_LEFT(rover
.edit
)) EDIT_LEFT(rover
.edit
);
744 } else if (wch
== KEY_DOWN
) {
745 while (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_RIGHT(rover
.edit
);
746 } else if (wch
== KEY_BACKSPACE
) {
747 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
748 } else if (wch
== KEY_DC
) {
749 if (EDIT_CAN_RIGHT(rover
.edit
)) EDIT_DELETE(rover
.edit
);
752 if (wch
== L
'\r' || wch
== L
'\n') {
755 } else if (wch
== L
'\t') {
758 } else if (wch
== eraser
) {
759 if (EDIT_CAN_LEFT(rover
.edit
)) EDIT_BACKSPACE(rover
.edit
);
760 } else if (wch
== killer
) {
761 EDIT_CLEAR(rover
.edit
);
763 } else if (iswprint(wch
)) {
764 if (!EDIT_FULL(rover
.edit
)) EDIT_INSERT(rover
.edit
, wch
);
767 /* Encode edit contents in INPUT. */
768 rover
.edit
.buffer
[rover
.edit
.left
] = L
'\0';
769 length
= wcstombs(INPUT
, rover
.edit
.buffer
, INPUTSZ
);
770 wcstombs(&INPUT
[length
], &rover
.edit
.buffer
[rover
.edit
.right
+1],
775 /* Update line input on the screen. */
777 update_input(char *prompt
, Color color
)
779 int plen
, ilen
, maxlen
;
782 plen
= strlen(prompt
);
783 ilen
= mbstowcs(NULL
, INPUT
, 0);
784 maxlen
= STATUSPOS
- plen
- 2;
785 if (ilen
- rover
.edit_scroll
< maxlen
)
786 rover
.edit_scroll
= MAX(ilen
- maxlen
, 0);
787 else if (rover
.edit
.left
> rover
.edit_scroll
+ maxlen
- 1)
788 rover
.edit_scroll
= rover
.edit
.left
- maxlen
;
789 else if (rover
.edit
.left
< rover
.edit_scroll
)
790 rover
.edit_scroll
= MAX(rover
.edit
.left
- maxlen
, 0);
791 color_set(RVC_PROMPT
, NULL
);
792 mvaddstr(LINES
- 1, 0, prompt
);
793 color_set(color
, NULL
);
794 mbstowcs(wbuf
, INPUT
, COLS
);
795 mvaddnwstr(LINES
- 1, plen
, &wbuf
[rover
.edit_scroll
], maxlen
);
796 mvaddch(LINES
- 1, plen
+ MIN(ilen
- rover
.edit_scroll
, maxlen
+ 1), ' ');
797 color_set(DEFAULT
, NULL
);
798 if (rover
.edit_scroll
)
799 mvaddch(LINES
- 1, plen
- 1, '<');
800 if (ilen
> rover
.edit_scroll
+ maxlen
)
801 mvaddch(LINES
- 1, plen
+ maxlen
, '>');
802 move(LINES
- 1, plen
+ rover
.edit
.left
- rover
.edit_scroll
);
806 main(int argc
, char *argv
[])
813 const char *save_cwd_file
= NULL
;
816 if (!strcmp(argv
[1], "-v") || !strcmp(argv
[1], "--version")) {
817 printf("rover %s\n", RV_VERSION
);
819 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
821 "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
822 " or: rover [OPTION]\n"
823 "Browse current working directory or the ones specified.\n\n"
825 " -h, --help print this help message and exit\n"
826 " -v, --version print program version and exit\n\n"
827 "See rover(1) for more information.\n\n"
828 "Rover homepage: <https://github.com/lecram/rover>.\n"
831 } else if (argc
> 2 && !strcmp(argv
[1], "--save-cwd")) {
832 save_cwd_file
= argv
[2];
833 argc
-= 2; argv
+= 2;
838 for (i
= 0; i
< 10; i
++) {
839 rover
.esel
[i
] = rover
.scroll
[i
] = 0;
840 rover
.flags
[i
] = SHOW_FILES
| SHOW_DIRS
;
842 strcpy(rover
.cwd
[0], getenv("HOME"));
843 for (i
= 1; i
< argc
&& i
< 10; i
++) {
844 if ((d
= opendir(argv
[i
]))) {
845 realpath(argv
[i
], rover
.cwd
[i
]);
848 strcpy(rover
.cwd
[i
], rover
.cwd
[0]);
850 getcwd(rover
.cwd
[i
], PATH_MAX
);
851 for (i
++; i
< 10; i
++)
852 strcpy(rover
.cwd
[i
], rover
.cwd
[i
-1]);
853 for (i
= 0; i
< 10; i
++)
854 if (rover
.cwd
[i
][strlen(rover
.cwd
[i
]) - 1] != '/')
855 strcat(rover
.cwd
[i
], "/");
857 rover
.window
= subwin(stdscr
, LINES
- 2, COLS
, 1, 0);
858 init_marks(&rover
.marks
);
864 if (!strcmp(key
, RVK_QUIT
)) break;
865 else if (ch
>= '0' && ch
<= '9') {
866 rover
.tab
= ch
- '0';
868 } else if (!strcmp(key
, RVK_HELP
)) {
873 } else if (!strcmp(key
, RVK_DOWN
)) {
874 if (!rover
.nfiles
) continue;
875 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
877 } else if (!strcmp(key
, RVK_UP
)) {
878 if (!rover
.nfiles
) continue;
879 ESEL
= ESEL
? ESEL
- 1 : rover
.nfiles
- 1;
881 } else if (!strcmp(key
, RVK_JUMP_DOWN
)) {
882 if (!rover
.nfiles
) continue;
883 ESEL
= MIN(ESEL
+ RV_JUMP
, rover
.nfiles
- 1);
884 if (rover
.nfiles
> HEIGHT
)
885 SCROLL
= MIN(SCROLL
+ RV_JUMP
, rover
.nfiles
- HEIGHT
);
887 } else if (!strcmp(key
, RVK_JUMP_UP
)) {
888 if (!rover
.nfiles
) continue;
889 ESEL
= MAX(ESEL
- RV_JUMP
, 0);
890 SCROLL
= MAX(SCROLL
- RV_JUMP
, 0);
892 } else if (!strcmp(key
, RVK_JUMP_TOP
)) {
893 if (!rover
.nfiles
) continue;
897 } else if (!strcmp(key
, RVK_JUMP_BOTTOM
)) {
898 if (!rover
.nfiles
) continue;
899 ESEL
= rover
.nfiles
- 1;
900 SCROLL
= MAX(rover
.nfiles
- 1 - HEIGHT
, 0);
902 } else if (!strcmp(key
, RVK_CD_DOWN
)) {
903 if (!rover
.nfiles
|| !S_ISDIR(EMODE(ESEL
))) continue;
904 strcat(CWD
, ENAME(ESEL
));
906 } else if (!strcmp(key
, RVK_CD_UP
)) {
907 char *dirname
, first
;
908 if (!strcmp(CWD
, "/")) continue;
909 CWD
[strlen(CWD
) - 1] = '\0';
910 dirname
= strrchr(CWD
, '/') + 1;
915 dirname
[strlen(dirname
)] = '/';
919 } else if (!strcmp(key
, RVK_HOME
)) {
920 strcpy(CWD
, getenv("HOME"));
921 if (CWD
[strlen(CWD
) - 1] != '/')
924 } else if (!strcmp(key
, RVK_REFRESH
)) {
926 } else if (!strcmp(key
, RVK_SHELL
)) {
927 program
= getenv("SHELL");
934 } else if (!strcmp(key
, RVK_VIEW
)) {
935 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
936 program
= getenv("PAGER");
939 ARGS
[1] = ENAME(ESEL
);
943 } else if (!strcmp(key
, RVK_EDIT
)) {
944 if (!rover
.nfiles
|| S_ISDIR(EMODE(ESEL
))) continue;
945 program
= getenv("EDITOR");
948 ARGS
[1] = ENAME(ESEL
);
953 } else if (!strcmp(key
, RVK_SEARCH
)) {
954 int oldsel
, oldscroll
, length
;
955 char *prompt
= "search: ";
956 if (!rover
.nfiles
) continue;
960 update_input(prompt
, DEFAULT
);
961 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
964 length
= strlen(INPUT
);
966 for (sel
= 0; sel
< rover
.nfiles
; sel
++)
967 if (!strncmp(ENAME(sel
), INPUT
, length
))
969 if (sel
< rover
.nfiles
) {
972 if (rover
.nfiles
> HEIGHT
) {
975 else if (sel
- 3 > rover
.nfiles
- HEIGHT
)
976 SCROLL
= rover
.nfiles
- HEIGHT
;
986 update_input(prompt
, color
);
988 if (edit_stat
== CANCEL
) {
994 } else if (!strcmp(key
, RVK_TG_FILES
)) {
997 } else if (!strcmp(key
, RVK_TG_DIRS
)) {
1000 } else if (!strcmp(key
, RVK_TG_HIDDEN
)) {
1001 FLAGS
^= SHOW_HIDDEN
;
1003 } else if (!strcmp(key
, RVK_NEW_FILE
)) {
1005 char *prompt
= "new file: ";
1006 start_line_edit("");
1007 update_input(prompt
, DEFAULT
);
1008 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1009 int length
= strlen(INPUT
);
1011 for (i
= 0; i
< rover
.nfiles
; i
++) {
1013 !strncmp(ENAME(i
), INPUT
, length
) &&
1014 (!strcmp(ENAME(i
) + length
, "") ||
1015 !strcmp(ENAME(i
) + length
, "/"))
1021 update_input(prompt
, ok
? GREEN
: RED
);
1024 if (edit_stat
== CONFIRM
&& strlen(INPUT
)) {
1031 message("File already exists.", RED
);
1033 } else if (!strcmp(key
, RVK_NEW_DIR
)) {
1035 char *prompt
= "new directory: ";
1036 start_line_edit("");
1037 update_input(prompt
, DEFAULT
);
1038 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1039 int length
= strlen(INPUT
);
1041 for (i
= 0; i
< rover
.nfiles
; i
++) {
1043 !strncmp(ENAME(i
), INPUT
, length
) &&
1044 (!strcmp(ENAME(i
) + length
, "") ||
1045 !strcmp(ENAME(i
) + length
, "/"))
1051 update_input(prompt
, ok
? GREEN
: RED
);
1054 if (edit_stat
== CONFIRM
&& strlen(INPUT
)) {
1061 message("File already exists.", RED
);
1063 } else if (!strcmp(key
, RVK_RENAME
)) {
1065 char *prompt
= "rename: ";
1068 strcpy(INPUT
, ENAME(ESEL
));
1069 last
= INPUT
+ strlen(INPUT
) - 1;
1070 if ((isdir
= *last
== '/'))
1072 start_line_edit(INPUT
);
1073 update_input(prompt
, RED
);
1074 while ((edit_stat
= get_line_edit()) == CONTINUE
) {
1075 int length
= strlen(INPUT
);
1077 for (i
= 0; i
< rover
.nfiles
; i
++)
1079 !strncmp(ENAME(i
), INPUT
, length
) &&
1080 (!strcmp(ENAME(i
) + length
, "") ||
1081 !strcmp(ENAME(i
) + length
, "/"))
1086 update_input(prompt
, ok
? GREEN
: RED
);
1089 if (edit_stat
== CONFIRM
&& strlen(INPUT
)) {
1093 if (!rename(ENAME(ESEL
), INPUT
) && MARKED(ESEL
)) {
1094 del_mark(&rover
.marks
, ENAME(ESEL
));
1095 add_mark(&rover
.marks
, CWD
, INPUT
);
1101 message("File already exists.", RED
);
1103 } else if (!strcmp(key
, RVK_DELETE
)) {
1105 message("Delete selected entry? (Y to confirm)", YELLOW
);
1106 if (rover_getch() == 'Y') {
1107 const char *name
= ENAME(ESEL
);
1108 int ret
= S_ISDIR(EMODE(ESEL
)) ? deldir(name
) : delfile(name
);
1111 message("Could not delete entry.", RED
);
1115 message("No entry selected for deletion.", RED
);
1116 } else if (!strcmp(key
, RVK_TG_MARK
)) {
1118 del_mark(&rover
.marks
, ENAME(ESEL
));
1120 add_mark(&rover
.marks
, CWD
, ENAME(ESEL
));
1121 MARKED(ESEL
) = !MARKED(ESEL
);
1122 ESEL
= (ESEL
+ 1) % rover
.nfiles
;
1124 } else if (!strcmp(key
, RVK_INVMARK
)) {
1125 for (i
= 0; i
< rover
.nfiles
; i
++) {
1127 del_mark(&rover
.marks
, ENAME(i
));
1129 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1130 MARKED(i
) = !MARKED(i
);
1133 } else if (!strcmp(key
, RVK_MARKALL
)) {
1134 for (i
= 0; i
< rover
.nfiles
; i
++)
1136 add_mark(&rover
.marks
, CWD
, ENAME(i
));
1140 } else if (!strcmp(key
, RVK_MARK_DELETE
)) {
1141 if (rover
.marks
.nentries
) {
1142 message("Delete marked entries? (Y to confirm)", YELLOW
);
1143 if (rover_getch() == 'Y')
1144 process_marked(NULL
, delfile
, deldir
);
1148 message("No entries marked for deletion.", RED
);
1149 } else if (!strcmp(key
, RVK_MARK_COPY
)) {
1150 if (rover
.marks
.nentries
)
1151 process_marked(adddir
, cpyfile
, NULL
);
1153 message("No entries marked for copying.", RED
);
1154 } else if (!strcmp(key
, RVK_MARK_MOVE
)) {
1155 if (rover
.marks
.nentries
)
1156 process_marked(adddir
, movfile
, deldir
);
1158 message("No entries marked for moving.", RED
);
1162 free_rows(&rover
.rows
, rover
.nfiles
);
1163 free_marks(&rover
.marks
);
1164 delwin(rover
.window
);
1165 if (save_cwd_file
!= NULL
) {
1166 FILE *fd
= fopen(save_cwd_file
, "w");