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>
7 #include <limits.h> /* PATH_MAX */
8 #include <locale.h> /* setlocale(), LC_ALL */
9 #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
10 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
11 #include <sys/stat.h>
12 #include <fcntl.h> /* open() */
13 #include <sys/wait.h> /* waitpid() */
14 #include <signal.h> /* struct sigaction, sigaction() */
15 #include <errno.h>
16 #include <curses.h>
18 #include "config.h"
20 /* String buffers. */
21 #define ROWSZ 256
22 static char ROW[ROWSZ];
23 #define STATUSSZ 256
24 static char STATUS[STATUSSZ];
25 #define INPUTSZ 256
26 static char INPUT[INPUTSZ];
28 /* Argument buffers for execvp(). */
29 #define MAXARGS 256
30 static char *ARGS[MAXARGS];
32 /* Listing view parameters. */
33 #define HEIGHT (LINES-4)
34 #define STATUSPOS (COLS-16)
36 /* Listing view flags. */
37 #define SHOW_FILES 0x01u
38 #define SHOW_DIRS 0x02u
39 #define SHOW_HIDDEN 0x04u
41 /* Marks parameters. */
42 #define BULK_INIT 5
43 #define BULK_THRESH 256
45 /* Information associated to each entry in listing. */
46 typedef struct Row {
47 char *name;
48 off_t size;
49 int marked;
50 } Row;
52 /* Dynamic array of marked entries. */
53 typedef struct Marks {
54 char dirpath[PATH_MAX];
55 int bulk;
56 int nentries;
57 char **entries;
58 } Marks;
60 /* Global state. Some basic info is allocated for ten tabs. */
61 static struct Rover {
62 int tab;
63 int nfiles;
64 int scroll[10];
65 int esel[10];
66 uint8_t flags[10];
67 Row *rows;
68 WINDOW *window;
69 char cwd[10][PATH_MAX];
70 Marks marks;
71 } rover;
73 /* Macros for accessing global state. */
74 #define ENAME(I) rover.rows[I].name
75 #define ESIZE(I) rover.rows[I].size
76 #define MARKED(I) rover.rows[I].marked
77 #define SCROLL rover.scroll[rover.tab]
78 #define ESEL rover.esel[rover.tab]
79 #define FLAGS rover.flags[rover.tab]
80 #define CWD rover.cwd[rover.tab]
82 /* Helpers. */
83 #define MIN(A, B) ((A) < (B) ? (A) : (B))
84 #define MAX(A, B) ((A) > (B) ? (A) : (B))
85 #define ISDIR(E) (strchr((E), '/') != NULL)
87 typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} Color;
88 typedef int (*PROCESS)(const char *path);
90 static void
91 init_marks(Marks *marks)
92 {
93 strcpy(marks->dirpath, "");
94 marks->bulk = BULK_INIT;
95 marks->nentries = 0;
96 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
97 }
99 /* Unmark all entries. */
100 static void
101 mark_none(Marks *marks)
103 int i;
105 strcpy(marks->dirpath, "");
106 for (i = 0; i < marks->bulk && marks->nentries; i++)
107 if (marks->entries[i]) {
108 free(marks->entries[i]);
109 marks->entries[i] = NULL;
110 marks->nentries--;
112 if (marks->bulk > BULK_THRESH) {
113 /* Reset bulk to free some memory. */
114 free(marks->entries);
115 marks->bulk = BULK_INIT;
116 marks->entries = calloc(marks->bulk, sizeof *marks->entries);
120 static void
121 add_mark(Marks *marks, char *dirpath, char *entry)
123 int i;
125 if (!strcmp(marks->dirpath, dirpath)) {
126 /* Append mark to directory. */
127 if (marks->nentries == marks->bulk) {
128 /* Expand bulk to accomodate new entry. */
129 int extra = marks->bulk >> 1;
130 marks->bulk += extra; /* bulk *= 1.5; */
131 marks->entries = realloc(marks->entries,
132 marks->bulk * sizeof *marks->entries);
133 memset(&marks->entries[marks->nentries], 0,
134 extra * sizeof *marks->entries);
135 i = marks->nentries;
136 } else {
137 /* Search for empty slot (there must be one). */
138 for (i = 0; i < marks->bulk; i++)
139 if (!marks->entries[i])
140 break;
142 } else {
143 /* Directory changed. Discard old marks. */
144 mark_none(marks);
145 strcpy(marks->dirpath, dirpath);
146 i = 0;
148 marks->entries[i] = malloc(strlen(entry) + 1);
149 strcpy(marks->entries[i], entry);
150 marks->nentries++;
153 static void
154 del_mark(Marks *marks, char *entry)
156 int i;
158 if (marks->nentries > 1) {
159 for (i = 0; i < marks->bulk; i++)
160 if (marks->entries[i] && !strcmp(marks->entries[i], entry))
161 break;
162 free(marks->entries[i]);
163 marks->entries[i] = NULL;
164 marks->nentries--;
165 } else
166 mark_none(marks);
169 static void
170 free_marks(Marks *marks)
172 int i;
174 for (i = 0; i < marks->bulk && marks->nentries; i++)
175 if (marks->entries[i]) {
176 free(marks->entries[i]);
177 marks->nentries--;
179 free(marks->entries);
182 static void update_view();
184 /* SIGSEGV handler: clean up curses before exiting. */
185 static void
186 handle_segv(int sig)
188 (void) sig;
189 endwin();
190 fprintf(stderr, "Received SIGSEGV (segmentation fault).\n");
191 exit(1);
194 /* SIGWINCH handler: resize application according to new terminal settings. */
195 static void
196 handle_winch(int sig)
198 (void) sig;
199 delwin(rover.window);
200 endwin();
201 refresh();
202 clear();
203 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
204 update_view();
207 static void
208 enable_handlers()
210 struct sigaction sa;
212 memset(&sa, 0, sizeof (struct sigaction));
213 sa.sa_handler = handle_segv;
214 sigaction(SIGSEGV, &sa, NULL);
215 sa.sa_handler = handle_winch;
216 sigaction(SIGWINCH, &sa, NULL);
219 static void
220 disable_handlers()
222 struct sigaction sa;
224 memset(&sa, 0, sizeof (struct sigaction));
225 sa.sa_handler = SIG_DFL;
226 sigaction(SIGSEGV, &sa, NULL);
227 sigaction(SIGWINCH, &sa, NULL);
230 /* Do a fork-exec to external program (e.g. $EDITOR). */
231 static void
232 spawn()
234 pid_t pid;
235 int status;
237 pid = fork();
238 if (pid > 0) {
239 /* fork() succeeded. */
240 disable_handlers();
241 endwin();
242 waitpid(pid, &status, 0);
243 enable_handlers();
244 kill(getpid(), SIGWINCH);
245 } else if (pid == 0) {
246 /* Child process. */
247 execvp(ARGS[0], ARGS);
251 /* Curses setup. */
252 static void
253 init_term()
255 setlocale(LC_ALL, "");
256 initscr();
257 cbreak(); /* Get one character at a time. */
258 noecho();
259 nonl(); /* No NL->CR/NL on output. */
260 intrflush(stdscr, FALSE);
261 keypad(stdscr, TRUE);
262 curs_set(FALSE); /* Hide blinking cursor. */
263 if (has_colors()) {
264 short bg;
265 start_color();
266 #ifdef NCURSES_EXT_FUNCS
267 use_default_colors();
268 bg = -1;
269 #else
270 bg = COLOR_BLACK;
271 #endif
272 init_pair(RED, COLOR_RED, bg);
273 init_pair(GREEN, COLOR_GREEN, bg);
274 init_pair(YELLOW, COLOR_YELLOW, bg);
275 init_pair(BLUE, COLOR_BLUE, bg);
276 init_pair(CYAN, COLOR_CYAN, bg);
277 init_pair(MAGENTA, COLOR_MAGENTA, bg);
278 init_pair(WHITE, COLOR_WHITE, bg);
280 atexit((void (*)(void)) endwin);
281 enable_handlers();
284 /* Update the listing view. */
285 static void
286 update_view()
288 int i, j;
289 int numsize;
290 int ishidden, isdir;
291 int marking;
293 mvhline(0, 0, ' ', COLS);
294 attr_on(A_BOLD, NULL);
295 color_set(RVC_TABNUM, NULL);
296 mvaddch(0, COLS - 2, rover.tab + '0');
297 color_set(DEFAULT, NULL);
298 attr_off(A_BOLD, NULL);
299 if (rover.marks.nentries) {
300 numsize = snprintf(STATUS, STATUSSZ, "%d", rover.marks.nentries);
301 color_set(RVC_NMARKS, NULL);
302 mvaddstr(0, COLS - 3 - numsize, STATUS);
303 color_set(DEFAULT, NULL);
304 } else
305 numsize = -1;
306 color_set(RVC_CWD, NULL);
307 mvaddnstr(0, 0, CWD, COLS - 4 - numsize);
308 color_set(DEFAULT, NULL);
309 wcolor_set(rover.window, RVC_BORDER, NULL);
310 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
311 wcolor_set(rover.window, DEFAULT, NULL);
312 /* Selection might not be visible, due to cursor wrapping or window
313 shrinking. In that case, the scroll must be moved to make it visible. */
314 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
315 marking = !strcmp(CWD, rover.marks.dirpath);
316 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
317 ishidden = ENAME(j)[0] == '.';
318 isdir = ISDIR(ENAME(j));
319 if (j == ESEL)
320 wattr_on(rover.window, A_REVERSE, NULL);
321 if (ishidden)
322 wcolor_set(rover.window, RVC_HIDDEN, NULL);
323 else if (isdir)
324 wcolor_set(rover.window, RVC_DIR, NULL);
325 else
326 wcolor_set(rover.window, RVC_FILE, NULL);
327 if (!isdir)
328 snprintf(ROW, ROWSZ, "%s%*d", ENAME(j),
329 (int) (COLS - strlen(ENAME(j)) - 4), (int) ESIZE(j));
330 else
331 strcpy(ROW, ENAME(j));
332 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
333 if (marking && MARKED(j))
334 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
335 else
336 mvwaddch(rover.window, i + 1, 1, ' ');
337 mvwaddnstr(rover.window, i + 1, 2, ROW, COLS - 4);
338 wcolor_set(rover.window, DEFAULT, NULL);
339 if (j == ESEL)
340 wattr_off(rover.window, A_REVERSE, NULL);
342 for (;i < HEIGHT; i++)
343 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
344 if (rover.nfiles > HEIGHT) {
345 int center, height;
346 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
347 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
348 if (!height) height = 1;
349 wcolor_set(rover.window, RVC_BORDER, NULL);
350 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
351 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
352 mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
353 wcolor_set(rover.window, DEFAULT, NULL);
355 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
356 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
357 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
358 if (!rover.nfiles)
359 strcpy(ROW, "0/0");
360 else
361 snprintf(ROW, ROWSZ, "%d/%d", ESEL + 1, rover.nfiles);
362 snprintf(STATUS+3, STATUSSZ-3, "%12s", ROW);
363 color_set(RVC_STATUS, NULL);
364 mvaddstr(LINES - 1, STATUSPOS, STATUS);
365 color_set(DEFAULT, NULL);
366 wrefresh(rover.window);
369 /* Show a message on the status bar. */
370 static void
371 message(const char *msg, Color color)
373 int len, pos;
375 len = strlen(msg);
376 pos = (STATUSPOS - len) >> 1;
377 attr_on(A_BOLD, NULL);
378 color_set(color, NULL);
379 mvaddstr(LINES - 1, pos, msg);
380 color_set(DEFAULT, NULL);
381 attr_off(A_BOLD, NULL);
384 /* Clear message area, leaving only status info. */
385 static void
386 clear_message()
388 mvhline(LINES - 1, 0, ' ', STATUSPOS);
391 /* Comparison used to sort listing entries. */
392 static int
393 rowcmp(const void *a, const void *b)
395 int isdir1, isdir2, cmpdir;
396 const Row *r1 = a;
397 const Row *r2 = b;
398 isdir1 = ISDIR(r1->name);
399 isdir2 = ISDIR(r2->name);
400 cmpdir = isdir2 - isdir1;
401 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
404 /* Get all entries in current working directory. */
405 static int
406 ls(Row **rowsp, uint8_t flags)
408 DIR *dp;
409 struct dirent *ep;
410 struct stat statbuf;
411 Row *rows;
412 int i, n;
414 if(!(dp = opendir("."))) return -1;
415 n = -2; /* We don't want the entries "." and "..". */
416 while (readdir(dp)) n++;
417 rewinddir(dp);
418 rows = malloc(n * sizeof *rows);
419 i = 0;
420 while ((ep = readdir(dp))) {
421 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
422 continue;
423 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
424 continue;
425 lstat(ep->d_name, &statbuf);
426 if (S_ISDIR(statbuf.st_mode)) {
427 if (flags & SHOW_DIRS) {
428 rows[i].name = malloc(strlen(ep->d_name) + 2);
429 strcpy(rows[i].name, ep->d_name);
430 strcat(rows[i].name, "/");
431 i++;
433 } else if (flags & SHOW_FILES) {
434 rows[i].name = malloc(strlen(ep->d_name) + 1);
435 strcpy(rows[i].name, ep->d_name);
436 rows[i].size = statbuf.st_size;
437 i++;
440 n = i; /* Ignore unused space in array caused by filters. */
441 qsort(rows, n, sizeof (*rows), rowcmp);
442 closedir(dp);
443 *rowsp = rows;
444 return n;
447 static void
448 free_rows(Row **rowsp, int nfiles)
450 int i;
452 for (i = 0; i < nfiles; i++)
453 free((*rowsp)[i].name);
454 free(*rowsp);
455 *rowsp = NULL;
458 /* Change working directory to the path in CWD. */
459 static void
460 cd(int reset)
462 int i, j;
464 message("Loading...", CYAN);
465 refresh();
466 if (reset) ESEL = SCROLL = 0;
467 chdir(CWD);
468 if (rover.nfiles)
469 free_rows(&rover.rows, rover.nfiles);
470 rover.nfiles = ls(&rover.rows, FLAGS);
471 if (!strcmp(CWD, rover.marks.dirpath)) {
472 for (i = 0; i < rover.nfiles; i++) {
473 for (j = 0; j < rover.marks.bulk; j++)
474 if (
475 rover.marks.entries[j] &&
476 !strcmp(rover.marks.entries[j], ENAME(i))
478 break;
479 MARKED(i) = j < rover.marks.bulk;
481 } else
482 for (i = 0; i < rover.nfiles; i++)
483 MARKED(i) = 0;
484 clear_message();
485 update_view();
488 /* Select a target entry, if it is present. */
489 static void
490 try_to_sel(const char *target)
492 ESEL = 0;
493 if (!ISDIR(target))
494 while ((ESEL+1) < rover.nfiles && ISDIR(ENAME(ESEL)))
495 ESEL++;
496 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
497 ESEL++;
498 if (rover.nfiles > HEIGHT) {
499 SCROLL = ESEL - (HEIGHT >> 1);
500 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
504 /* Reload CWD, but try to keep selection. */
505 static void
506 reload()
508 if (rover.nfiles) {
509 strcpy(INPUT, ENAME(ESEL));
510 cd(1);
511 try_to_sel(INPUT);
512 update_view();
513 } else
514 cd(1);
517 /* Recursively process a source directory using CWD as destination root.
518 For each node (i.e. directory), do the following:
519 1. call pre(destination);
520 2. call proc() on every child leaf (i.e. files);
521 3. recurse into every child node;
522 4. call pos(source).
523 E.g. to move directory /src/ (and all its contents) inside /dst/:
524 strcpy(CWD, "/dst/");
525 process_dir(adddir, movfile, deldir, "/src/"); */
526 static int
527 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
529 int ret;
530 DIR *dp;
531 struct dirent *ep;
532 struct stat statbuf;
533 char subpath[PATH_MAX];
535 ret = 0;
536 if (pre) {
537 char dstpath[PATH_MAX];
538 strcpy(dstpath, CWD);
539 strcat(dstpath, path + strlen(rover.marks.dirpath));
540 ret |= pre(dstpath);
542 if(!(dp = opendir(path))) return -1;
543 while ((ep = readdir(dp))) {
544 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
545 continue;
546 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
547 stat(subpath, &statbuf);
548 if (S_ISDIR(statbuf.st_mode)) {
549 strcat(subpath, "/");
550 ret |= process_dir(pre, proc, pos, subpath);
551 } else
552 ret |= proc(subpath);
554 closedir(dp);
555 if (pos) ret |= pos(path);
556 return ret;
559 /* Process all marked entries using CWD as destination root.
560 All marked entries that are directories will be recursively processed.
561 See process_dir() for details on the parameters. */
562 static void
563 process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
565 int i, ret;
566 char path[PATH_MAX];
568 clear_message();
569 message("Processing...", CYAN);
570 refresh();
571 for (i = 0; i < rover.marks.bulk; i++)
572 if (rover.marks.entries[i]) {
573 ret = 0;
574 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
575 if (ISDIR(rover.marks.entries[i])) {
576 if (!strncmp(path, CWD, strlen(path)))
577 ret = -1;
578 else
579 ret = process_dir(pre, proc, pos, path);
580 } else
581 ret = proc(path);
582 if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
584 reload();
585 if (!rover.marks.nentries)
586 message("Done.", GREEN);
587 else
588 message("Some errors occured.", RED);
591 /* Wrappers for file operations. */
592 static PROCESS delfile = unlink;
593 static PROCESS deldir = rmdir;
594 static int addfile(const char *path) {
595 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
596 int ret;
598 ret = creat(path, 0644);
599 if (ret < 0) return ret;
600 return close(ret);
602 static int cpyfile(const char *srcpath) {
603 int src, dst, ret;
604 size_t size;
605 struct stat st;
606 char buf[BUFSIZ];
607 char dstpath[PATH_MAX];
609 ret = src = open(srcpath, O_RDONLY);
610 if (ret < 0) return ret;
611 ret = fstat(src, &st);
612 if (ret < 0) return ret;
613 strcpy(dstpath, CWD);
614 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
615 ret = dst = creat(dstpath, st.st_mode);
616 if (ret < 0) return ret;
617 while ((size = read(src, buf, BUFSIZ)) > 0)
618 write(dst, buf, size);
619 close(src);
620 close(dst);
621 return 0;
623 static int adddir(const char *path) {
624 int ret;
625 struct stat st;
627 ret = stat(CWD, &st);
628 if (ret < 0) return ret;
629 return mkdir(path, st.st_mode);
631 static int movfile(const char *srcpath) {
632 int ret;
633 char dstpath[PATH_MAX];
635 strcpy(dstpath, CWD);
636 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
637 ret = rename(srcpath, dstpath);
638 if (ret < 0 && errno == EXDEV) {
639 ret = cpyfile(srcpath);
640 if (ret < 0) return ret;
641 ret = delfile(srcpath);
643 return ret;
646 /* Interactive getstr(). */
647 static int
648 igetstr(char *buffer, int maxlen)
650 int ch, length;
652 length = strlen(buffer);
653 curs_set(TRUE);
654 ch = getch();
655 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
656 curs_set(FALSE);
657 return 0;
658 } else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
659 if (length)
660 buffer[--length] = '\0';
661 } else if (ch == killchar()) {
662 length = 0;
663 buffer[0] = '\0';
664 } else if (length < maxlen - 1 && isprint(ch)) {
665 buffer[length++] = ch;
666 buffer[length] = '\0';
668 return 1;
671 /* Update line input on the screen. */
672 static void
673 update_input(char *prompt, Color color)
675 int plen, ilen;
677 plen = strlen(prompt);
678 ilen = strlen(INPUT);
679 color_set(RVC_PROMPT, NULL);
680 mvaddstr(LINES - 1, 0, prompt);
681 color_set(color, NULL);
682 mvaddstr(LINES - 1, plen, INPUT);
683 mvaddch(LINES - 1, ilen + plen, ' ');
684 move(LINES - 1, ilen + plen);
685 color_set(DEFAULT, NULL);
688 int
689 main(int argc, char *argv[])
691 int i, ch;
692 char *program;
693 const char *key;
694 DIR *d;
696 if (argc == 2) {
697 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
698 printf("rover %s\n", RV_VERSION);
699 return 0;
700 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
701 printf(
702 "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
703 " or: rover [OPTION]\n"
704 "Browse current working directory or the ones specified.\n\n"
705 "Options:\n"
706 " -h, --help print this help message and exit\n"
707 " -v, --version print program version and exit\n\n"
708 "See rover(1) for more information.\n\n"
709 "Rover homepage: <https://github.com/lecram/rover>.\n"
710 );
711 return 0;
714 init_term();
715 rover.nfiles = 0;
716 for (i = 0; i < 10; i++) {
717 rover.esel[i] = rover.scroll[i] = 0;
718 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
720 strcpy(rover.cwd[0], getenv("HOME"));
721 for (i = 1; i < argc && i < 10; i++) {
722 if ((d = opendir(argv[i]))) {
723 realpath(argv[i], rover.cwd[i]);
724 closedir(d);
725 } else
726 strcpy(rover.cwd[i], rover.cwd[0]);
728 getcwd(rover.cwd[i], PATH_MAX);
729 for (i++; i < 10; i++)
730 strcpy(rover.cwd[i], rover.cwd[i-1]);
731 for (i = 0; i < 10; i++)
732 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
733 strcat(rover.cwd[i], "/");
734 rover.tab = 1;
735 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
736 init_marks(&rover.marks);
737 cd(1);
738 while (1) {
739 ch = getch();
740 key = keyname(ch);
741 clear_message();
742 if (!strcmp(key, RVK_QUIT)) break;
743 else if (ch >= '0' && ch <= '9') {
744 rover.tab = ch - '0';
745 cd(0);
746 } else if (!strcmp(key, RVK_HELP)) {
747 ARGS[0] = "man";
748 ARGS[1] = "rover";
749 ARGS[2] = NULL;
750 spawn();
751 } else if (!strcmp(key, RVK_DOWN)) {
752 if (!rover.nfiles) continue;
753 ESEL = (ESEL + 1) % rover.nfiles;
754 update_view();
755 } else if (!strcmp(key, RVK_UP)) {
756 if (!rover.nfiles) continue;
757 ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
758 update_view();
759 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
760 if (!rover.nfiles) continue;
761 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
762 if (rover.nfiles > HEIGHT)
763 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
764 update_view();
765 } else if (!strcmp(key, RVK_JUMP_UP)) {
766 if (!rover.nfiles) continue;
767 ESEL = MAX(ESEL - RV_JUMP, 0);
768 SCROLL = MAX(SCROLL - RV_JUMP, 0);
769 update_view();
770 } else if (!strcmp(key, RVK_CD_DOWN)) {
771 if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
772 strcat(CWD, ENAME(ESEL));
773 cd(1);
774 } else if (!strcmp(key, RVK_CD_UP)) {
775 char *dirname, first;
776 if (!strcmp(CWD, "/")) continue;
777 CWD[strlen(CWD) - 1] = '\0';
778 dirname = strrchr(CWD, '/') + 1;
779 first = dirname[0];
780 dirname[0] = '\0';
781 cd(1);
782 dirname[0] = first;
783 dirname[strlen(dirname)] = '/';
784 try_to_sel(dirname);
785 dirname[0] = '\0';
786 update_view();
787 } else if (!strcmp(key, RVK_HOME)) {
788 strcpy(CWD, getenv("HOME"));
789 if (CWD[strlen(CWD) - 1] != '/')
790 strcat(CWD, "/");
791 cd(1);
792 } else if (!strcmp(key, RVK_SHELL)) {
793 program = getenv("SHELL");
794 if (program) {
795 ARGS[0] = program;
796 ARGS[1] = NULL;
797 spawn();
798 reload();
800 } else if (!strcmp(key, RVK_VIEW)) {
801 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
802 program = getenv("PAGER");
803 if (program) {
804 ARGS[0] = program;
805 ARGS[1] = ENAME(ESEL);
806 ARGS[2] = NULL;
807 spawn();
809 } else if (!strcmp(key, RVK_EDIT)) {
810 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
811 program = getenv("EDITOR");
812 if (program) {
813 ARGS[0] = program;
814 ARGS[1] = ENAME(ESEL);
815 ARGS[2] = NULL;
816 spawn();
817 cd(0);
819 } else if (!strcmp(key, RVK_SEARCH)) {
820 int oldsel, oldscroll, length;
821 char *prompt = "search: ";
822 if (!rover.nfiles) continue;
823 oldsel = ESEL;
824 oldscroll = SCROLL;
825 strcpy(INPUT, "");
826 update_input(prompt, DEFAULT);
827 while (igetstr(INPUT, INPUTSZ)) {
828 int sel;
829 Color color = RED;
830 length = strlen(INPUT);
831 if (length) {
832 for (sel = 0; sel < rover.nfiles; sel++)
833 if (!strncmp(ENAME(sel), INPUT, length))
834 break;
835 if (sel < rover.nfiles) {
836 color = GREEN;
837 ESEL = sel;
838 if (rover.nfiles > HEIGHT) {
839 if (sel < 3)
840 SCROLL = 0;
841 else if (sel - 3 > rover.nfiles - HEIGHT)
842 SCROLL = rover.nfiles - HEIGHT;
843 else
844 SCROLL = sel - 3;
847 } else {
848 ESEL = oldsel;
849 SCROLL = oldscroll;
851 update_view();
852 update_input(prompt, color);
854 clear_message();
855 update_view();
856 } else if (!strcmp(key, RVK_TG_FILES)) {
857 FLAGS ^= SHOW_FILES;
858 reload();
859 } else if (!strcmp(key, RVK_TG_DIRS)) {
860 FLAGS ^= SHOW_DIRS;
861 reload();
862 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
863 FLAGS ^= SHOW_HIDDEN;
864 reload();
865 } else if (!strcmp(key, RVK_NEW_FILE)) {
866 int ok = 0;
867 char *prompt = "new file: ";
868 strcpy(INPUT, "");
869 update_input(prompt, DEFAULT);
870 while (igetstr(INPUT, INPUTSZ)) {
871 int length = strlen(INPUT);
872 ok = 1;
873 for (i = 0; i < rover.nfiles; i++) {
874 if (
875 !strncmp(ENAME(i), INPUT, length) &&
876 (!strcmp(ENAME(i) + length, "") ||
877 !strcmp(ENAME(i) + length, "/"))
878 ) {
879 ok = 0;
880 break;
883 update_input(prompt, ok ? GREEN : RED);
885 clear_message();
886 if (strlen(INPUT)) {
887 if (ok) {
888 addfile(INPUT);
889 cd(1);
890 try_to_sel(INPUT);
891 update_view();
892 } else
893 message("File already exists.", RED);
895 } else if (!strcmp(key, RVK_NEW_DIR)) {
896 int ok = 0;
897 char *prompt = "new directory: ";
898 strcpy(INPUT, "");
899 update_input(prompt, DEFAULT);
900 while (igetstr(INPUT, INPUTSZ)) {
901 int length = strlen(INPUT);
902 ok = 1;
903 for (i = 0; i < rover.nfiles; i++) {
904 if (
905 !strncmp(ENAME(i), INPUT, length) &&
906 (!strcmp(ENAME(i) + length, "") ||
907 !strcmp(ENAME(i) + length, "/"))
908 ) {
909 ok = 0;
910 break;
913 update_input(prompt, ok ? GREEN : RED);
915 clear_message();
916 if (strlen(INPUT)) {
917 if (ok) {
918 adddir(INPUT);
919 cd(1);
920 try_to_sel(INPUT);
921 update_view();
922 } else
923 message("File already exists.", RED);
925 } else if (!strcmp(key, RVK_RENAME)) {
926 int ok = 0;
927 char *prompt = "rename: ";
928 char *last;
929 int isdir;
930 strcpy(INPUT, ENAME(ESEL));
931 last = INPUT + strlen(INPUT) - 1;
932 if ((isdir = *last == '/'))
933 *last = '\0';
934 update_input(prompt, RED);
935 while (igetstr(INPUT, INPUTSZ)) {
936 int length = strlen(INPUT);
937 ok = 1;
938 for (i = 0; i < rover.nfiles; i++)
939 if (
940 !strncmp(ENAME(i), INPUT, length) &&
941 (!strcmp(ENAME(i) + length, "") ||
942 !strcmp(ENAME(i) + length, "/"))
943 ) {
944 ok = 0;
945 break;
947 update_input(prompt, ok ? GREEN : RED);
949 clear_message();
950 if (strlen(INPUT)) {
951 if (isdir)
952 strcat(INPUT, "/");
953 if (ok) {
954 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
955 del_mark(&rover.marks, ENAME(ESEL));
956 add_mark(&rover.marks, CWD, INPUT);
958 cd(1);
959 try_to_sel(INPUT);
960 update_view();
961 } else
962 message("File already exists.", RED);
964 } else if (!strcmp(key, RVK_TG_MARK)) {
965 if (MARKED(ESEL))
966 del_mark(&rover.marks, ENAME(ESEL));
967 else
968 add_mark(&rover.marks, CWD, ENAME(ESEL));
969 MARKED(ESEL) = !MARKED(ESEL);
970 ESEL = (ESEL + 1) % rover.nfiles;
971 update_view();
972 } else if (!strcmp(key, RVK_INVMARK)) {
973 for (i = 0; i < rover.nfiles; i++) {
974 if (MARKED(i))
975 del_mark(&rover.marks, ENAME(i));
976 else
977 add_mark(&rover.marks, CWD, ENAME(i));
978 MARKED(i) = !MARKED(i);
980 update_view();
981 } else if (!strcmp(key, RVK_MARKALL)) {
982 for (i = 0; i < rover.nfiles; i++)
983 if (!MARKED(i)) {
984 add_mark(&rover.marks, CWD, ENAME(i));
985 MARKED(i) = 1;
987 update_view();
988 } else if (!strcmp(key, RVK_DELETE)) {
989 if (rover.marks.nentries) {
990 message("Delete marked entries? (Y to confirm)", YELLOW);
991 if (getch() == 'Y')
992 process_marked(NULL, delfile, deldir);
993 else
994 clear_message();
995 } else
996 message("No entries marked for deletion.", RED);
997 } else if (!strcmp(key, RVK_COPY)) {
998 if (rover.marks.nentries)
999 process_marked(adddir, cpyfile, NULL);
1000 else
1001 message("No entries marked for copying.", RED);
1002 } else if (!strcmp(key, RVK_MOVE)) {
1003 if (rover.marks.nentries)
1004 process_marked(adddir, movfile, deldir);
1005 else
1006 message("No entries marked for moving.", RED);
1009 if (rover.nfiles)
1010 free_rows(&rover.rows, rover.nfiles);
1011 free_marks(&rover.marks);
1012 delwin(rover.window);
1013 return 0;