Blob


1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <sys/types.h> /* pid_t, ... */
6 #include <stdio.h> /* FILENAME_MAX */
7 #include <locale.h> /* setlocale(), LC_ALL */
8 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
9 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
10 #include <sys/stat.h>
11 #include <fcntl.h> /* open() */
12 #include <sys/wait.h> /* waitpid() */
13 #include <signal.h> /* struct sigaction, sigaction() */
14 #include <curses.h>
16 #include "config.h"
18 /* String buffers. */
19 #define ROWSZ 256
20 static char ROW[ROWSZ];
21 #define STATUSSZ 256
22 static char STATUS[STATUSSZ];
23 #define INPUTSZ 256
24 static char INPUT[INPUTSZ];
26 /* Argument buffers for execvp(). */
27 #define MAXARGS 256
28 static char *ARGS[MAXARGS];
30 /* Listing view parameters. */
31 #define HEIGHT (LINES-4)
32 #define STATUSPOS (COLS-16)
34 /* Listing view flags. */
35 #define SHOW_FILES 0x01u
36 #define SHOW_DIRS 0x02u
37 #define SHOW_HIDDEN 0x04u
39 /* Marks parameters. */
40 #define BULK_INIT 5
41 #define BULK_THRESH 256
43 /* Information associated to each entry in listing. */
44 typedef struct {
45 char *name;
46 off_t size;
47 int marked;
48 } row_t;
50 /* Dynamic array of marked entries. */
51 typedef struct {
52 char dirpath[FILENAME_MAX];
53 int bulk;
54 int nentries;
55 char **entries;
56 } marks_t;
58 /* Global state. Some basic info is allocated for ten tabs. */
59 static struct rover_t {
60 int tab;
61 int nfiles;
62 int scroll[10];
63 int esel[10];
64 uint8_t flags[10];
65 row_t *rows;
66 WINDOW *window;
67 char cwd[10][FILENAME_MAX];
68 marks_t marks;
69 } rover;
71 /* Macros for accessing global state. */
72 #define ENAME(I) rover.rows[I].name
73 #define ESIZE(I) rover.rows[I].size
74 #define MARKED(I) rover.rows[I].marked
75 #define SCROLL rover.scroll[rover.tab]
76 #define ESEL rover.esel[rover.tab]
77 #define FLAGS rover.flags[rover.tab]
78 #define CWD rover.cwd[rover.tab]
80 /* Helpers. */
81 #define MIN(A, B) ((A) < (B) ? (A) : (B))
82 #define MAX(A, B) ((A) > (B) ? (A) : (B))
83 #define ISDIR(E) (strchr((E), '/') != NULL)
85 typedef enum {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} color_t;
86 typedef int (*PROCESS)(const char *path);
88 static void
89 init_marks(marks_t *marks)
90 {
91 strcpy(marks->dirpath, "");
92 marks->bulk = BULK_INIT;
93 marks->nentries = 0;
94 marks->entries = (char **) calloc(marks->bulk, sizeof(char *));
95 }
97 /* Unmark all entries. */
98 static void
99 mark_none(marks_t *marks)
101 int i;
103 strcpy(marks->dirpath, "");
104 for (i = 0; i < marks->bulk && marks->nentries; i++)
105 if (marks->entries[i]) {
106 free(marks->entries[i]);
107 marks->entries[i] = NULL;
108 marks->nentries--;
110 if (marks->bulk > BULK_THRESH) {
111 /* Reset bulk to free some memory. */
112 free(marks->entries);
113 marks->bulk = BULK_INIT;
114 marks->entries = (char **) calloc(marks->bulk, sizeof(char *));
118 static void
119 add_mark(marks_t *marks, char *dirpath, char *entry)
121 int i;
123 if (!strcmp(marks->dirpath, dirpath)) {
124 /* Append mark to directory. */
125 if (marks->nentries == marks->bulk) {
126 /* Expand bulk to accomodate new entry. */
127 int extra = marks->bulk >> 1;
128 marks->bulk += extra; /* bulk *= 1.5; */
129 marks->entries = (char **) realloc(
130 marks->entries, marks->bulk * sizeof(char *)
131 );
132 memset(&marks->entries[marks->nentries], 0, extra * sizeof(char *));
133 i = marks->nentries;
134 } else {
135 /* Search for empty slot (there must be one). */
136 for (i = 0; i < marks->bulk; i++)
137 if (!marks->entries[i])
138 break;
140 } else {
141 /* Directory changed. Discard old marks. */
142 mark_none(marks);
143 strcpy(marks->dirpath, dirpath);
144 i = 0;
146 marks->entries[i] = (char *) malloc(strlen(entry) + 1);
147 strcpy(marks->entries[i], entry);
148 marks->nentries++;
151 static void
152 del_mark(marks_t *marks, char *entry)
154 int i;
156 if (marks->nentries > 1) {
157 for (i = 0; i < marks->bulk; i++)
158 if (marks->entries[i] && !strcmp(marks->entries[i], entry))
159 break;
160 free(marks->entries[i]);
161 marks->entries[i] = NULL;
162 marks->nentries--;
163 } else
164 mark_none(marks);
167 static void
168 free_marks(marks_t *marks)
170 int i;
172 for (i = 0; i < marks->bulk && marks->nentries; i++)
173 if (marks->entries[i]) {
174 free(marks->entries[i]);
175 marks->nentries--;
177 free(marks->entries);
180 static void message(const char *msg, color_t color);
181 static void clear_message();
183 static void handle_segv(int sig);
184 static void handle_winch(int sig);
186 /* Curses setup. */
187 static void
188 init_term()
190 struct sigaction sa;
192 setlocale(LC_ALL, "");
193 initscr();
194 cbreak(); /* Get one character at a time. */
195 noecho();
196 nonl(); /* No NL->CR/NL on output. */
197 intrflush(stdscr, FALSE);
198 keypad(stdscr, TRUE);
199 curs_set(FALSE); /* Hide blinking cursor. */
200 memset(&sa, 0, sizeof(struct sigaction));
201 /* Setup SIGSEGV handler. */
202 sa.sa_handler = handle_segv;
203 sigaction(SIGSEGV, &sa, NULL);
204 /* Setup SIGWINCH handler. */
205 sa.sa_handler = handle_winch;
206 sigaction(SIGWINCH, &sa, NULL);
207 if (has_colors()) {
208 start_color();
209 init_pair(RED, COLOR_RED, COLOR_BLACK);
210 init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);
211 init_pair(YELLOW, COLOR_YELLOW,COLOR_BLACK);
212 init_pair(BLUE, COLOR_BLUE, COLOR_BLACK);
213 init_pair(CYAN, COLOR_CYAN, COLOR_BLACK);
214 init_pair(MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
215 init_pair(WHITE, COLOR_WHITE, COLOR_BLACK);
217 atexit((void (*)(void)) endwin);
220 /* Update the listing view. */
221 static void
222 update_view()
224 int i, j;
225 int ishidden, isdir;
226 int marking;
228 mvhline(0, 0, ' ', COLS);
229 color_set(RVC_CWD, NULL);
230 mvaddnstr(0, 0, CWD, COLS);
231 color_set(DEFAULT, NULL);
232 attr_on(A_BOLD, NULL);
233 color_set(RVC_TABNUM, NULL);
234 mvaddch(0, COLS-4, rover.tab + '0');
235 color_set(DEFAULT, NULL);
236 attr_off(A_BOLD, NULL);
237 wclear(rover.window);
238 wcolor_set(rover.window, RVC_BORDER, NULL);
239 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
240 wcolor_set(rover.window, DEFAULT, NULL);
241 /* Selection might not be visible, due to cursor wrapping or window
242 shrinking. In that case, the scroll must be moved to make it visible. */
243 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
244 marking = !strcmp(CWD, rover.marks.dirpath);
245 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
246 ishidden = ENAME(j)[0] == '.';
247 isdir = ISDIR(ENAME(j));
248 if (j == ESEL)
249 wattr_on(rover.window, A_REVERSE, NULL);
250 if (ishidden)
251 wcolor_set(rover.window, RVC_HIDDEN, NULL);
252 else if (isdir)
253 wcolor_set(rover.window, RVC_DIR, NULL);
254 else
255 wcolor_set(rover.window, RVC_FILE, NULL);
256 if (!isdir)
257 sprintf(ROW, "%s%*d", ENAME(j),
258 COLS - strlen(ENAME(j)) - 4, (int) ESIZE(j));
259 else
260 strcpy(ROW, ENAME(j));
261 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
262 if (marking && MARKED(j))
263 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
264 else
265 mvwaddch(rover.window, i + 1, 1, ' ');
266 mvwaddnstr(rover.window, i + 1, 2, ROW, COLS - 4);
267 wcolor_set(rover.window, DEFAULT, NULL);
268 if (j == ESEL)
269 wattr_off(rover.window, A_REVERSE, NULL);
271 if (rover.nfiles > HEIGHT) {
272 int center, height;
273 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
274 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
275 if (!height) height = 1;
276 wcolor_set(rover.window, RVC_BORDER, NULL);
277 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
278 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
279 mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
280 wcolor_set(rover.window, DEFAULT, NULL);
282 wrefresh(rover.window);
283 if (rover.marks.nentries) {
284 sprintf(STATUS, "%7d)", rover.marks.nentries);
285 *strrchr(STATUS, ' ') = '(';
286 color_set(RVC_NMARKS, NULL);
287 mvaddstr(0, COLS-15, STATUS);
288 color_set(DEFAULT, NULL);
290 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
291 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
292 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
293 if (!rover.nfiles)
294 strcpy(ROW, "0/0");
295 else
296 sprintf(ROW, "%d/%d", ESEL + 1, rover.nfiles);
297 sprintf(STATUS+3, "%12s", ROW);
298 color_set(RVC_STATUS, NULL);
299 mvaddstr(LINES - 1, STATUSPOS, STATUS);
300 color_set(DEFAULT, NULL);
301 refresh();
304 /* SIGSEGV handler: clean up curses before exiting. */
305 static void
306 handle_segv(int sig)
308 (void) sig;
309 endwin();
310 puts("Received SIGSEGV (segmentation fault).");
311 exit(1);
314 /* SIGWINCH handler: resize application according to new terminal settings. */
315 static void
316 handle_winch(int sig)
318 (void) sig;
319 delwin(rover.window);
320 endwin();
321 refresh();
322 clear();
323 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
324 update_view();
327 /* Comparison used to sort listing entries. */
328 static int
329 rowcmp(const void *a, const void *b)
331 int isdir1, isdir2, cmpdir;
332 const row_t *r1 = a;
333 const row_t *r2 = b;
334 isdir1 = ISDIR(r1->name);
335 isdir2 = ISDIR(r2->name);
336 cmpdir = isdir2 - isdir1;
337 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
340 /* Get all entries for a given path (usually cwd). */
341 static int
342 ls(char *path, row_t **rowsp, uint8_t flags)
344 DIR *dp;
345 struct dirent *ep;
346 struct stat statbuf;
347 row_t *rows;
348 int i, n;
350 if(!(dp = opendir(path))) return -1;
351 n = -2; /* We don't want the entries "." and "..". */
352 while (readdir(dp)) n++;
353 rewinddir(dp);
354 rows = (row_t *) malloc(n * sizeof(row_t));
355 i = 0;
356 while ((ep = readdir(dp))) {
357 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
358 continue;
359 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
360 continue;
361 /* FIXME: ANSI C doesn't have lstat(). How do we handle symlinks? */
362 stat(ep->d_name, &statbuf);
363 if (S_ISDIR(statbuf.st_mode)) {
364 if (flags & SHOW_DIRS) {
365 rows[i].name = (char *) malloc(strlen(ep->d_name) + 2);
366 strcpy(rows[i].name, ep->d_name);
367 strcat(rows[i].name, "/");
368 i++;
370 } else if (flags & SHOW_FILES) {
371 rows[i].name = (char *) malloc(strlen(ep->d_name) + 1);
372 strcpy(rows[i].name, ep->d_name);
373 rows[i].size = statbuf.st_size;
374 i++;
377 n = i; /* Ignore unused space in array caused by filters. */
378 qsort(rows, n, sizeof(row_t), rowcmp);
379 closedir(dp);
380 *rowsp = rows;
381 return n;
384 static void
385 free_rows(row_t **rowsp, int nfiles)
387 int i;
389 for (i = 0; i < nfiles; i++)
390 free((*rowsp)[i].name);
391 free(*rowsp);
392 *rowsp = NULL;
395 /* Change working directory. */
396 /* NOTE: The caller needs to write the new path to CWD
397 * *before* calling this function. */
398 static void
399 cd(int reset)
401 int i, j;
403 message("Loading...", CYAN);
404 refresh();
405 if (reset) ESEL = SCROLL = 0;
406 chdir(CWD);
407 if (rover.nfiles)
408 free_rows(&rover.rows, rover.nfiles);
409 rover.nfiles = ls(CWD, &rover.rows, FLAGS);
410 if (!strcmp(CWD, rover.marks.dirpath)) {
411 for (i = 0; i < rover.nfiles; i++) {
412 for (j = 0; j < rover.marks.bulk; j++)
413 if (
414 rover.marks.entries[j] &&
415 !strcmp(rover.marks.entries[j], ENAME(i))
417 break;
418 MARKED(i) = j < rover.marks.bulk;
420 } else
421 for (i = 0; i < rover.nfiles; i++)
422 MARKED(i) = 0;
423 clear_message();
424 update_view();
427 /* Select a target entry, if it is present. */
428 static void
429 try_to_sel(const char *target)
431 ESEL = 0;
432 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
433 ESEL++;
434 if (rover.nfiles > HEIGHT) {
435 SCROLL = ESEL - (HEIGHT >> 1);
436 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
440 /* Reload CWD, but try to keep selection. */
441 static void
442 reload()
444 if (rover.nfiles) {
445 strcpy(INPUT, ENAME(ESEL));
446 cd(1);
447 try_to_sel(INPUT);
448 update_view();
449 } else
450 cd(1);
453 /* Recursively process a source directory using CWD as destination root.
454 * For each node (i.e. directory), do the following:
455 * 1. call pre(destination);
456 * 2. call proc() on every child leaf (i.e. files);
457 * 3. recurse into every child node;
458 * 4. call pos(source).
459 * E.g. to move directory /src/ (and all its contents) inside /dst/:
460 * strcpy(CWD, "/dst/");
461 * process_dir(adddir, movfile, deldir, "/src/");
462 */
463 static int
464 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
466 int ret;
467 DIR *dp;
468 struct dirent *ep;
469 struct stat statbuf;
470 char subpath[FILENAME_MAX];
472 ret = 0;
473 if (pre) {
474 char dstpath[FILENAME_MAX];
475 strcpy(dstpath, CWD);
476 strcat(dstpath, path + strlen(rover.marks.dirpath));
477 ret |= pre(dstpath);
479 if(!(dp = opendir(path))) return -1;
480 while ((ep = readdir(dp))) {
481 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
482 continue;
483 sprintf(subpath, "%s%s", path, ep->d_name);
484 stat(subpath, &statbuf);
485 if (S_ISDIR(statbuf.st_mode)) {
486 strcat(subpath, "/");
487 ret |= process_dir(pre, proc, pos, subpath);
488 } else
489 ret |= proc(subpath);
491 closedir(dp);
492 if (pos) ret |= pos(path);
493 return ret;
496 /* Process all marked entries using CWD as destination root.
497 * All marked entries that are directories will be recursively processed.
498 * See process_dir() for details on the parameters.
499 */
500 static void
501 process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
503 int i, ret;
504 char path[FILENAME_MAX];
506 clear_message();
507 message("Processing...", CYAN);
508 refresh();
509 for (i = 0; i < rover.marks.bulk; i++)
510 if (rover.marks.entries[i]) {
511 ret = 0;
512 sprintf(path, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
513 if (ISDIR(rover.marks.entries[i])) {
514 if (!strncmp(path, CWD, strlen(path)))
515 ret = -1;
516 else
517 ret = process_dir(pre, proc, pos, path);
518 } else
519 ret = proc(path);
520 if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
522 reload();
523 if (!rover.marks.nentries)
524 message("Done.", GREEN);
525 else
526 message("Some errors occured.", RED);
529 /* Wrappers for file operations. */
530 static PROCESS delfile = unlink;
531 static PROCESS deldir = rmdir;
532 static int addfile(const char *path) {
533 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
534 int ret;
536 ret = creat(path, 0644);
537 if (ret < 0) return ret;
538 return close(ret);
540 static int cpyfile(const char *srcpath) {
541 int src, dst, ret;
542 size_t size;
543 struct stat st;
544 char buf[BUFSIZ];
545 char dstpath[FILENAME_MAX];
547 ret = src = open(srcpath, O_RDONLY);
548 if (ret < 0) return ret;
549 ret = fstat(src, &st);
550 if (ret < 0) return ret;
551 strcpy(dstpath, CWD);
552 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
553 ret = dst = creat(dstpath, st.st_mode);
554 if (ret < 0) return ret;
555 while ((size = read(src, buf, BUFSIZ)) > 0)
556 write(dst, buf, size);
557 close(src);
558 close(dst);
559 return 0;
561 static int adddir(const char *path) {
562 int ret;
563 struct stat st;
565 ret = stat(CWD, &st);
566 if (ret < 0) return ret;
567 return mkdir(path, st.st_mode);
569 static int movfile(const char *srcpath) {
570 char dstpath[FILENAME_MAX];
572 strcpy(dstpath, CWD);
573 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
574 return rename(srcpath, dstpath);
577 /* Do a fork-exec to external program (e.g. $EDITOR). */
578 static void
579 spawn()
581 pid_t pid;
582 int status;
584 pid = fork();
585 if (pid > 0) {
586 /* fork() succeeded. */
587 endwin();
588 waitpid(pid, &status, 0);
589 init_term();
590 doupdate();
591 } else if (pid == 0) {
592 /* Child process. */
593 execvp(ARGS[0], ARGS);
597 /* Interactive getstr(). */
598 static int
599 igetstr(char *buffer, int maxlen)
601 int ch, length;
603 length = strlen(buffer);
604 curs_set(TRUE);
605 ch = getch();
606 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
607 curs_set(FALSE);
608 return 0;
609 } else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
610 if (length)
611 buffer[--length] = '\0';
612 } else if (ch == killchar()) {
613 length = 0;
614 buffer[0] = '\0';
615 } else if (length < maxlen - 1 && isprint(ch)) {
616 buffer[length++] = ch;
617 buffer[length] = '\0';
619 return 1;
622 /* Update line input on the screen. */
623 static void
624 update_input(char *prompt, color_t color)
626 int plen, ilen;
628 plen = strlen(prompt);
629 ilen = strlen(INPUT);
630 color_set(RVC_PROMPT, NULL);
631 mvaddstr(LINES - 1, 0, prompt);
632 color_set(color, NULL);
633 mvaddstr(LINES - 1, plen, INPUT);
634 mvaddch(LINES - 1, ilen + plen, ' ');
635 move(LINES - 1, ilen + plen);
636 color_set(DEFAULT, NULL);
639 /* Show a message on the status bar. */
640 static void
641 message(const char *msg, color_t color)
643 int len, pos;
645 len = strlen(msg);
646 pos = (STATUSPOS - len) >> 1;
647 attr_on(A_BOLD, NULL);
648 color_set(color, NULL);
649 mvaddstr(LINES - 1, pos, msg);
650 color_set(DEFAULT, NULL);
651 attr_off(A_BOLD, NULL);
654 /* Clear message area, leaving only status info. */
655 static void
656 clear_message()
658 mvhline(LINES - 1, 0, ' ', STATUSPOS);
661 int
662 main(int argc, char *argv[])
664 int i, ch;
665 char *program, *key;
666 DIR *d;
668 if (argc == 2) {
669 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
670 printf("rover %s\n", RV_VERSION);
671 return 0;
672 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
673 printf(
674 "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
675 " or: rover [OPTION]\n"
676 "Browse current working directory or the ones specified.\n\n"
677 "Options:\n"
678 " -h, --help print this help message and exit\n"
679 " -v, --version print program version and exit\n\n"
680 "See rover(1) for more information.\n\n"
681 "Rover homepage: <https://github.com/lecram/rover>.\n"
682 );
683 return 0;
686 init_term();
687 rover.nfiles = 0;
688 for (i = 0; i < 10; i++) {
689 rover.esel[i] = rover.scroll[i] = 0;
690 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
692 strcpy(rover.cwd[0], getenv("HOME"));
693 for (i = 1; i < argc && i < 10; i++) {
694 if ((d = opendir(argv[i]))) {
695 strcpy(rover.cwd[i], argv[i]);
696 closedir(d);
697 } else
698 strcpy(rover.cwd[i], rover.cwd[0]);
700 getcwd(rover.cwd[i], FILENAME_MAX);
701 for (i++; i < 10; i++)
702 strcpy(rover.cwd[i], rover.cwd[i-1]);
703 for (i = 0; i < 10; i++)
704 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
705 strcat(rover.cwd[i], "/");
706 rover.tab = 1;
707 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
708 init_marks(&rover.marks);
709 cd(1);
710 while (1) {
711 ch = getch();
712 key = keyname(ch);
713 clear_message();
714 if (!strcmp(key, RVK_QUIT)) break;
715 else if (ch >= '0' && ch <= '9') {
716 rover.tab = ch - '0';
717 cd(0);
718 } else if (!strcmp(key, RVK_HELP)) {
719 ARGS[0] = "man";
720 ARGS[1] = "rover";
721 ARGS[2] = NULL;
722 spawn();
723 } else if (!strcmp(key, RVK_DOWN)) {
724 if (!rover.nfiles) continue;
725 ESEL = (ESEL + 1) % rover.nfiles;
726 update_view();
727 } else if (!strcmp(key, RVK_UP)) {
728 if (!rover.nfiles) continue;
729 ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
730 update_view();
731 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
732 if (!rover.nfiles) continue;
733 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
734 if (rover.nfiles > HEIGHT)
735 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
736 update_view();
737 } else if (!strcmp(key, RVK_JUMP_UP)) {
738 if (!rover.nfiles) continue;
739 ESEL = MAX(ESEL - RV_JUMP, 0);
740 SCROLL = MAX(SCROLL - RV_JUMP, 0);
741 update_view();
742 } else if (!strcmp(key, RVK_CD_DOWN)) {
743 if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
744 strcat(CWD, ENAME(ESEL));
745 cd(1);
746 } else if (!strcmp(key, RVK_CD_UP)) {
747 char *dirname, first;
748 if (!strcmp(CWD, "/")) continue;
749 CWD[strlen(CWD) - 1] = '\0';
750 dirname = strrchr(CWD, '/') + 1;
751 first = dirname[0];
752 dirname[0] = '\0';
753 cd(1);
754 dirname[0] = first;
755 dirname[strlen(dirname)] = '/';
756 try_to_sel(dirname);
757 dirname[0] = '\0';
758 update_view();
759 } else if (!strcmp(key, RVK_HOME)) {
760 strcpy(CWD, getenv("HOME"));
761 if (CWD[strlen(CWD) - 1] != '/')
762 strcat(CWD, "/");
763 cd(1);
764 } else if (!strcmp(key, RVK_SHELL)) {
765 program = getenv("SHELL");
766 if (program) {
767 ARGS[0] = program;
768 ARGS[1] = NULL;
769 spawn();
770 reload();
772 } else if (!strcmp(key, RVK_VIEW)) {
773 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
774 program = getenv("PAGER");
775 if (program) {
776 ARGS[0] = program;
777 ARGS[1] = ENAME(ESEL);
778 ARGS[2] = NULL;
779 spawn();
781 } else if (!strcmp(key, RVK_EDIT)) {
782 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
783 program = getenv("EDITOR");
784 if (program) {
785 ARGS[0] = program;
786 ARGS[1] = ENAME(ESEL);
787 ARGS[2] = NULL;
788 spawn();
789 cd(0);
791 } else if (!strcmp(key, RVK_SEARCH)) {
792 int oldsel, oldscroll, length;
793 char *prompt = "search: ";
794 if (!rover.nfiles) continue;
795 oldsel = ESEL;
796 oldscroll = SCROLL;
797 strcpy(INPUT, "");
798 update_input(prompt, DEFAULT);
799 while (igetstr(INPUT, INPUTSZ)) {
800 int sel;
801 color_t color = RED;
802 length = strlen(INPUT);
803 if (length) {
804 for (sel = 0; sel < rover.nfiles; sel++)
805 if (!strncmp(ENAME(sel), INPUT, length))
806 break;
807 if (sel < rover.nfiles) {
808 color = GREEN;
809 ESEL = sel;
810 if (rover.nfiles > HEIGHT) {
811 if (sel < 3)
812 SCROLL = 0;
813 else if (sel - 3 > rover.nfiles - HEIGHT)
814 SCROLL = rover.nfiles - HEIGHT;
815 else
816 SCROLL = sel - 3;
819 } else {
820 ESEL = oldsel;
821 SCROLL = oldscroll;
823 update_view();
824 update_input(prompt, color);
826 clear_message();
827 update_view();
828 } else if (!strcmp(key, RVK_TG_FILES)) {
829 FLAGS ^= SHOW_FILES;
830 reload();
831 } else if (!strcmp(key, RVK_TG_DIRS)) {
832 FLAGS ^= SHOW_DIRS;
833 reload();
834 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
835 FLAGS ^= SHOW_HIDDEN;
836 reload();
837 } else if (!strcmp(key, RVK_NEW_FILE)) {
838 int ok = 0;
839 char *prompt = "new file: ";
840 strcpy(INPUT, "");
841 update_input(prompt, DEFAULT);
842 while (igetstr(INPUT, INPUTSZ)) {
843 int length = strlen(INPUT);
844 ok = 1;
845 for (i = 0; i < rover.nfiles; i++) {
846 if (
847 !strncmp(ENAME(i), INPUT, length) &&
848 (!strcmp(ENAME(i) + length, "") ||
849 !strcmp(ENAME(i) + length, "/"))
850 ) {
851 ok = 0;
852 break;
855 update_input(prompt, ok ? GREEN : RED);
857 clear_message();
858 if (strlen(INPUT)) {
859 if (ok) {
860 addfile(INPUT);
861 cd(1);
862 try_to_sel(INPUT);
863 update_view();
864 } else
865 message("File already exists.", RED);
867 } else if (!strcmp(key, RVK_NEW_DIR)) {
868 int ok = 0;
869 char *prompt = "new directory: ";
870 strcpy(INPUT, "");
871 update_input(prompt, DEFAULT);
872 while (igetstr(INPUT, INPUTSZ)) {
873 int length = strlen(INPUT);
874 ok = 1;
875 for (i = 0; i < rover.nfiles; i++) {
876 if (
877 !strncmp(ENAME(i), INPUT, length) &&
878 (!strcmp(ENAME(i) + length, "") ||
879 !strcmp(ENAME(i) + length, "/"))
880 ) {
881 ok = 0;
882 break;
885 update_input(prompt, ok ? GREEN : RED);
887 clear_message();
888 if (strlen(INPUT)) {
889 if (ok) {
890 adddir(INPUT);
891 cd(1);
892 try_to_sel(INPUT);
893 update_view();
894 } else
895 message("File already exists.", RED);
897 } else if (!strcmp(key, RVK_RENAME)) {
898 int ok = 0;
899 char *prompt = "rename: ";
900 strcpy(INPUT, ENAME(ESEL));
901 update_input(prompt, RED);
902 while (igetstr(INPUT, INPUTSZ)) {
903 int length = strlen(INPUT);
904 ok = 1;
905 for (i = 0; i < rover.nfiles; i++) {
906 if (
907 !strncmp(ENAME(i), INPUT, length) &&
908 (!strcmp(ENAME(i) + length, "") ||
909 !strcmp(ENAME(i) + length, "/"))
910 ) {
911 ok = 0;
912 break;
915 update_input(prompt, ok ? GREEN : RED);
917 clear_message();
918 if (strlen(INPUT)) {
919 if (ok) {
920 rename(ENAME(ESEL), INPUT);
921 cd(1);
922 try_to_sel(INPUT);
923 update_view();
924 } else
925 message("File already exists.", RED);
927 } else if (!strcmp(key, RVK_TG_MARK)) {
928 if (MARKED(ESEL))
929 del_mark(&rover.marks, ENAME(ESEL));
930 else
931 add_mark(&rover.marks, CWD, ENAME(ESEL));
932 MARKED(ESEL) = !MARKED(ESEL);
933 ESEL = (ESEL + 1) % rover.nfiles;
934 update_view();
935 } else if (!strcmp(key, RVK_INVMARK)) {
936 for (i = 0; i < rover.nfiles; i++) {
937 if (MARKED(i))
938 del_mark(&rover.marks, ENAME(i));
939 else
940 add_mark(&rover.marks, CWD, ENAME(i));
941 MARKED(i) = !MARKED(i);
943 update_view();
944 } else if (!strcmp(key, RVK_MARKALL)) {
945 for (i = 0; i < rover.nfiles; i++)
946 if (!MARKED(i)) {
947 add_mark(&rover.marks, CWD, ENAME(i));
948 MARKED(i) = 1;
950 update_view();
951 } else if (!strcmp(key, RVK_DELETE)) {
952 if (rover.marks.nentries) {
953 message("Delete marked entries? (Y to confirm)", YELLOW);
954 if (getch() == 'Y')
955 process_marked(NULL, delfile, deldir);
956 else
957 clear_message();
958 } else
959 message("No entries marked for deletion.", RED);
960 } else if (!strcmp(key, RVK_COPY)) {
961 if (rover.marks.nentries)
962 process_marked(adddir, cpyfile, NULL);
963 else
964 message("No entries marked for copying.", RED);
965 } else if (!strcmp(key, RVK_MOVE)) {
966 if (rover.marks.nentries)
967 process_marked(adddir, movfile, deldir);
968 else
969 message("No entries marked for moving.", RED);
972 if (rover.nfiles)
973 free_rows(&rover.rows, rover.nfiles);
974 free_marks(&rover.marks);
975 delwin(rover.window);
976 return 0;