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 message(const char *msg, Color color);
183 static void clear_message();
185 static void handle_segv(int sig);
186 static void handle_winch(int sig);
188 /* Curses setup. */
189 static void
190 init_term()
192 struct sigaction sa;
194 setlocale(LC_ALL, "");
195 initscr();
196 cbreak(); /* Get one character at a time. */
197 noecho();
198 nonl(); /* No NL->CR/NL on output. */
199 intrflush(stdscr, FALSE);
200 keypad(stdscr, TRUE);
201 curs_set(FALSE); /* Hide blinking cursor. */
202 if (has_colors()) {
203 short bg;
204 start_color();
205 #ifdef NCURSES_EXT_FUNCS
206 use_default_colors();
207 bg = -1;
208 #else
209 bg = COLOR_BLACK;
210 #endif
211 init_pair(RED, COLOR_RED, bg);
212 init_pair(GREEN, COLOR_GREEN, bg);
213 init_pair(YELLOW, COLOR_YELLOW, bg);
214 init_pair(BLUE, COLOR_BLUE, bg);
215 init_pair(CYAN, COLOR_CYAN, bg);
216 init_pair(MAGENTA, COLOR_MAGENTA, bg);
217 init_pair(WHITE, COLOR_WHITE, bg);
219 atexit((void (*)(void)) endwin);
220 memset(&sa, 0, sizeof (struct sigaction));
221 /* Setup SIGSEGV handler. */
222 sa.sa_handler = handle_segv;
223 sigaction(SIGSEGV, &sa, NULL);
224 /* Setup SIGWINCH handler. */
225 sa.sa_handler = handle_winch;
226 sigaction(SIGWINCH, &sa, NULL);
229 /* Update the listing view. */
230 static void
231 update_view()
233 int i, j;
234 int numsize;
235 int ishidden, isdir;
236 int marking;
238 mvhline(0, 0, ' ', COLS);
239 attr_on(A_BOLD, NULL);
240 color_set(RVC_TABNUM, NULL);
241 mvaddch(0, COLS - 2, rover.tab + '0');
242 color_set(DEFAULT, NULL);
243 attr_off(A_BOLD, NULL);
244 if (rover.marks.nentries) {
245 numsize = snprintf(STATUS, STATUSSZ, "%d", rover.marks.nentries);
246 color_set(RVC_NMARKS, NULL);
247 mvaddstr(0, COLS - 3 - numsize, STATUS);
248 color_set(DEFAULT, NULL);
249 } else
250 numsize = -1;
251 color_set(RVC_CWD, NULL);
252 mvaddnstr(0, 0, CWD, COLS - 4 - numsize);
253 color_set(DEFAULT, NULL);
254 wcolor_set(rover.window, RVC_BORDER, NULL);
255 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
256 wcolor_set(rover.window, DEFAULT, NULL);
257 /* Selection might not be visible, due to cursor wrapping or window
258 shrinking. In that case, the scroll must be moved to make it visible. */
259 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
260 marking = !strcmp(CWD, rover.marks.dirpath);
261 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
262 ishidden = ENAME(j)[0] == '.';
263 isdir = ISDIR(ENAME(j));
264 if (j == ESEL)
265 wattr_on(rover.window, A_REVERSE, NULL);
266 if (ishidden)
267 wcolor_set(rover.window, RVC_HIDDEN, NULL);
268 else if (isdir)
269 wcolor_set(rover.window, RVC_DIR, NULL);
270 else
271 wcolor_set(rover.window, RVC_FILE, NULL);
272 if (!isdir)
273 snprintf(ROW, ROWSZ, "%s%*d", ENAME(j),
274 (int) (COLS - strlen(ENAME(j)) - 4), (int) ESIZE(j));
275 else
276 strcpy(ROW, ENAME(j));
277 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
278 if (marking && MARKED(j))
279 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
280 else
281 mvwaddch(rover.window, i + 1, 1, ' ');
282 mvwaddnstr(rover.window, i + 1, 2, ROW, COLS - 4);
283 wcolor_set(rover.window, DEFAULT, NULL);
284 if (j == ESEL)
285 wattr_off(rover.window, A_REVERSE, NULL);
287 for (;i < HEIGHT; i++)
288 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
289 if (rover.nfiles > HEIGHT) {
290 int center, height;
291 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
292 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
293 if (!height) height = 1;
294 wcolor_set(rover.window, RVC_BORDER, NULL);
295 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
296 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
297 mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
298 wcolor_set(rover.window, DEFAULT, NULL);
300 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
301 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
302 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
303 if (!rover.nfiles)
304 strcpy(ROW, "0/0");
305 else
306 snprintf(ROW, ROWSZ, "%d/%d", ESEL + 1, rover.nfiles);
307 snprintf(STATUS+3, STATUSSZ-3, "%12s", ROW);
308 color_set(RVC_STATUS, NULL);
309 mvaddstr(LINES - 1, STATUSPOS, STATUS);
310 color_set(DEFAULT, NULL);
311 wrefresh(rover.window);
314 /* SIGSEGV handler: clean up curses before exiting. */
315 static void
316 handle_segv(int sig)
318 (void) sig;
319 endwin();
320 fprintf(stderr, "Received SIGSEGV (segmentation fault).\n");
321 exit(1);
324 /* SIGWINCH handler: resize application according to new terminal settings. */
325 static void
326 handle_winch(int sig)
328 (void) sig;
329 delwin(rover.window);
330 endwin();
331 refresh();
332 clear();
333 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
334 update_view();
337 /* Comparison used to sort listing entries. */
338 static int
339 rowcmp(const void *a, const void *b)
341 int isdir1, isdir2, cmpdir;
342 const Row *r1 = a;
343 const Row *r2 = b;
344 isdir1 = ISDIR(r1->name);
345 isdir2 = ISDIR(r2->name);
346 cmpdir = isdir2 - isdir1;
347 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
350 /* Get all entries in current working directory. */
351 static int
352 ls(Row **rowsp, uint8_t flags)
354 DIR *dp;
355 struct dirent *ep;
356 struct stat statbuf;
357 Row *rows;
358 int i, n;
360 if(!(dp = opendir("."))) return -1;
361 n = -2; /* We don't want the entries "." and "..". */
362 while (readdir(dp)) n++;
363 rewinddir(dp);
364 rows = malloc(n * sizeof *rows);
365 i = 0;
366 while ((ep = readdir(dp))) {
367 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
368 continue;
369 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
370 continue;
371 lstat(ep->d_name, &statbuf);
372 if (S_ISDIR(statbuf.st_mode)) {
373 if (flags & SHOW_DIRS) {
374 rows[i].name = malloc(strlen(ep->d_name) + 2);
375 strcpy(rows[i].name, ep->d_name);
376 strcat(rows[i].name, "/");
377 i++;
379 } else if (flags & SHOW_FILES) {
380 rows[i].name = malloc(strlen(ep->d_name) + 1);
381 strcpy(rows[i].name, ep->d_name);
382 rows[i].size = statbuf.st_size;
383 i++;
386 n = i; /* Ignore unused space in array caused by filters. */
387 qsort(rows, n, sizeof (*rows), rowcmp);
388 closedir(dp);
389 *rowsp = rows;
390 return n;
393 static void
394 free_rows(Row **rowsp, int nfiles)
396 int i;
398 for (i = 0; i < nfiles; i++)
399 free((*rowsp)[i].name);
400 free(*rowsp);
401 *rowsp = NULL;
404 /* Change working directory to the path in CWD. */
405 static void
406 cd(int reset)
408 int i, j;
410 message("Loading...", CYAN);
411 refresh();
412 if (reset) ESEL = SCROLL = 0;
413 chdir(CWD);
414 if (rover.nfiles)
415 free_rows(&rover.rows, rover.nfiles);
416 rover.nfiles = ls(&rover.rows, FLAGS);
417 if (!strcmp(CWD, rover.marks.dirpath)) {
418 for (i = 0; i < rover.nfiles; i++) {
419 for (j = 0; j < rover.marks.bulk; j++)
420 if (
421 rover.marks.entries[j] &&
422 !strcmp(rover.marks.entries[j], ENAME(i))
424 break;
425 MARKED(i) = j < rover.marks.bulk;
427 } else
428 for (i = 0; i < rover.nfiles; i++)
429 MARKED(i) = 0;
430 clear_message();
431 update_view();
434 /* Select a target entry, if it is present. */
435 static void
436 try_to_sel(const char *target)
438 ESEL = 0;
439 if (!ISDIR(target))
440 while ((ESEL+1) < rover.nfiles && ISDIR(ENAME(ESEL)))
441 ESEL++;
442 while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
443 ESEL++;
444 if (rover.nfiles > HEIGHT) {
445 SCROLL = ESEL - (HEIGHT >> 1);
446 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
450 /* Reload CWD, but try to keep selection. */
451 static void
452 reload()
454 if (rover.nfiles) {
455 strcpy(INPUT, ENAME(ESEL));
456 cd(1);
457 try_to_sel(INPUT);
458 update_view();
459 } else
460 cd(1);
463 /* Recursively process a source directory using CWD as destination root.
464 For each node (i.e. directory), do the following:
465 1. call pre(destination);
466 2. call proc() on every child leaf (i.e. files);
467 3. recurse into every child node;
468 4. call pos(source).
469 E.g. to move directory /src/ (and all its contents) inside /dst/:
470 strcpy(CWD, "/dst/");
471 process_dir(adddir, movfile, deldir, "/src/"); */
472 static int
473 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
475 int ret;
476 DIR *dp;
477 struct dirent *ep;
478 struct stat statbuf;
479 char subpath[PATH_MAX];
481 ret = 0;
482 if (pre) {
483 char dstpath[PATH_MAX];
484 strcpy(dstpath, CWD);
485 strcat(dstpath, path + strlen(rover.marks.dirpath));
486 ret |= pre(dstpath);
488 if(!(dp = opendir(path))) return -1;
489 while ((ep = readdir(dp))) {
490 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
491 continue;
492 snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
493 stat(subpath, &statbuf);
494 if (S_ISDIR(statbuf.st_mode)) {
495 strcat(subpath, "/");
496 ret |= process_dir(pre, proc, pos, subpath);
497 } else
498 ret |= proc(subpath);
500 closedir(dp);
501 if (pos) ret |= pos(path);
502 return ret;
505 /* Process all marked entries using CWD as destination root.
506 All marked entries that are directories will be recursively processed.
507 See process_dir() for details on the parameters. */
508 static void
509 process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
511 int i, ret;
512 char path[PATH_MAX];
514 clear_message();
515 message("Processing...", CYAN);
516 refresh();
517 for (i = 0; i < rover.marks.bulk; i++)
518 if (rover.marks.entries[i]) {
519 ret = 0;
520 snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
521 if (ISDIR(rover.marks.entries[i])) {
522 if (!strncmp(path, CWD, strlen(path)))
523 ret = -1;
524 else
525 ret = process_dir(pre, proc, pos, path);
526 } else
527 ret = proc(path);
528 if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
530 reload();
531 if (!rover.marks.nentries)
532 message("Done.", GREEN);
533 else
534 message("Some errors occured.", RED);
537 /* Wrappers for file operations. */
538 static PROCESS delfile = unlink;
539 static PROCESS deldir = rmdir;
540 static int addfile(const char *path) {
541 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
542 int ret;
544 ret = creat(path, 0644);
545 if (ret < 0) return ret;
546 return close(ret);
548 static int cpyfile(const char *srcpath) {
549 int src, dst, ret;
550 size_t size;
551 struct stat st;
552 char buf[BUFSIZ];
553 char dstpath[PATH_MAX];
555 ret = src = open(srcpath, O_RDONLY);
556 if (ret < 0) return ret;
557 ret = fstat(src, &st);
558 if (ret < 0) return ret;
559 strcpy(dstpath, CWD);
560 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
561 ret = dst = creat(dstpath, st.st_mode);
562 if (ret < 0) return ret;
563 while ((size = read(src, buf, BUFSIZ)) > 0)
564 write(dst, buf, size);
565 close(src);
566 close(dst);
567 return 0;
569 static int adddir(const char *path) {
570 int ret;
571 struct stat st;
573 ret = stat(CWD, &st);
574 if (ret < 0) return ret;
575 return mkdir(path, st.st_mode);
577 static int movfile(const char *srcpath) {
578 int ret;
579 char dstpath[PATH_MAX];
581 strcpy(dstpath, CWD);
582 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
583 ret = rename(srcpath, dstpath);
584 if (ret < 0 && errno == EXDEV) {
585 ret = cpyfile(srcpath);
586 if (ret < 0) return ret;
587 ret = delfile(srcpath);
589 return ret;
592 /* Do a fork-exec to external program (e.g. $EDITOR). */
593 static void
594 spawn()
596 pid_t pid;
597 int status;
598 struct sigaction sa;
600 pid = fork();
601 if (pid > 0) {
602 /* fork() succeeded. */
603 memset(&sa, 0, sizeof (struct sigaction));
604 sa.sa_handler = SIG_DFL;
605 sigaction(SIGSEGV, &sa, NULL);
606 sigaction(SIGWINCH, &sa, NULL);
607 endwin();
608 waitpid(pid, &status, 0);
609 init_term();
610 handle_winch(0);
611 } else if (pid == 0) {
612 /* Child process. */
613 execvp(ARGS[0], ARGS);
617 /* Interactive getstr(). */
618 static int
619 igetstr(char *buffer, int maxlen)
621 int ch, length;
623 length = strlen(buffer);
624 curs_set(TRUE);
625 ch = getch();
626 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
627 curs_set(FALSE);
628 return 0;
629 } else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
630 if (length)
631 buffer[--length] = '\0';
632 } else if (ch == killchar()) {
633 length = 0;
634 buffer[0] = '\0';
635 } else if (length < maxlen - 1 && isprint(ch)) {
636 buffer[length++] = ch;
637 buffer[length] = '\0';
639 return 1;
642 /* Update line input on the screen. */
643 static void
644 update_input(char *prompt, Color color)
646 int plen, ilen;
648 plen = strlen(prompt);
649 ilen = strlen(INPUT);
650 color_set(RVC_PROMPT, NULL);
651 mvaddstr(LINES - 1, 0, prompt);
652 color_set(color, NULL);
653 mvaddstr(LINES - 1, plen, INPUT);
654 mvaddch(LINES - 1, ilen + plen, ' ');
655 move(LINES - 1, ilen + plen);
656 color_set(DEFAULT, NULL);
659 /* Show a message on the status bar. */
660 static void
661 message(const char *msg, Color color)
663 int len, pos;
665 len = strlen(msg);
666 pos = (STATUSPOS - len) >> 1;
667 attr_on(A_BOLD, NULL);
668 color_set(color, NULL);
669 mvaddstr(LINES - 1, pos, msg);
670 color_set(DEFAULT, NULL);
671 attr_off(A_BOLD, NULL);
674 /* Clear message area, leaving only status info. */
675 static void
676 clear_message()
678 mvhline(LINES - 1, 0, ' ', STATUSPOS);
681 int
682 main(int argc, char *argv[])
684 int i, ch;
685 char *program;
686 const char *key;
687 DIR *d;
689 if (argc == 2) {
690 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
691 printf("rover %s\n", RV_VERSION);
692 return 0;
693 } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
694 printf(
695 "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
696 " or: rover [OPTION]\n"
697 "Browse current working directory or the ones specified.\n\n"
698 "Options:\n"
699 " -h, --help print this help message and exit\n"
700 " -v, --version print program version and exit\n\n"
701 "See rover(1) for more information.\n\n"
702 "Rover homepage: <https://github.com/lecram/rover>.\n"
703 );
704 return 0;
707 init_term();
708 rover.nfiles = 0;
709 for (i = 0; i < 10; i++) {
710 rover.esel[i] = rover.scroll[i] = 0;
711 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
713 strcpy(rover.cwd[0], getenv("HOME"));
714 for (i = 1; i < argc && i < 10; i++) {
715 if ((d = opendir(argv[i]))) {
716 realpath(argv[i], rover.cwd[i]);
717 closedir(d);
718 } else
719 strcpy(rover.cwd[i], rover.cwd[0]);
721 getcwd(rover.cwd[i], PATH_MAX);
722 for (i++; i < 10; i++)
723 strcpy(rover.cwd[i], rover.cwd[i-1]);
724 for (i = 0; i < 10; i++)
725 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
726 strcat(rover.cwd[i], "/");
727 rover.tab = 1;
728 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
729 init_marks(&rover.marks);
730 cd(1);
731 while (1) {
732 ch = getch();
733 key = keyname(ch);
734 clear_message();
735 if (!strcmp(key, RVK_QUIT)) break;
736 else if (ch >= '0' && ch <= '9') {
737 rover.tab = ch - '0';
738 cd(0);
739 } else if (!strcmp(key, RVK_HELP)) {
740 ARGS[0] = "man";
741 ARGS[1] = "rover";
742 ARGS[2] = NULL;
743 spawn();
744 } else if (!strcmp(key, RVK_DOWN)) {
745 if (!rover.nfiles) continue;
746 ESEL = (ESEL + 1) % rover.nfiles;
747 update_view();
748 } else if (!strcmp(key, RVK_UP)) {
749 if (!rover.nfiles) continue;
750 ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
751 update_view();
752 } else if (!strcmp(key, RVK_JUMP_DOWN)) {
753 if (!rover.nfiles) continue;
754 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
755 if (rover.nfiles > HEIGHT)
756 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
757 update_view();
758 } else if (!strcmp(key, RVK_JUMP_UP)) {
759 if (!rover.nfiles) continue;
760 ESEL = MAX(ESEL - RV_JUMP, 0);
761 SCROLL = MAX(SCROLL - RV_JUMP, 0);
762 update_view();
763 } else if (!strcmp(key, RVK_CD_DOWN)) {
764 if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
765 strcat(CWD, ENAME(ESEL));
766 cd(1);
767 } else if (!strcmp(key, RVK_CD_UP)) {
768 char *dirname, first;
769 if (!strcmp(CWD, "/")) continue;
770 CWD[strlen(CWD) - 1] = '\0';
771 dirname = strrchr(CWD, '/') + 1;
772 first = dirname[0];
773 dirname[0] = '\0';
774 cd(1);
775 dirname[0] = first;
776 dirname[strlen(dirname)] = '/';
777 try_to_sel(dirname);
778 dirname[0] = '\0';
779 update_view();
780 } else if (!strcmp(key, RVK_HOME)) {
781 strcpy(CWD, getenv("HOME"));
782 if (CWD[strlen(CWD) - 1] != '/')
783 strcat(CWD, "/");
784 cd(1);
785 } else if (!strcmp(key, RVK_SHELL)) {
786 program = getenv("SHELL");
787 if (program) {
788 ARGS[0] = program;
789 ARGS[1] = NULL;
790 spawn();
791 reload();
793 } else if (!strcmp(key, RVK_VIEW)) {
794 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
795 program = getenv("PAGER");
796 if (program) {
797 ARGS[0] = program;
798 ARGS[1] = ENAME(ESEL);
799 ARGS[2] = NULL;
800 spawn();
802 } else if (!strcmp(key, RVK_EDIT)) {
803 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
804 program = getenv("EDITOR");
805 if (program) {
806 ARGS[0] = program;
807 ARGS[1] = ENAME(ESEL);
808 ARGS[2] = NULL;
809 spawn();
810 cd(0);
812 } else if (!strcmp(key, RVK_SEARCH)) {
813 int oldsel, oldscroll, length;
814 char *prompt = "search: ";
815 if (!rover.nfiles) continue;
816 oldsel = ESEL;
817 oldscroll = SCROLL;
818 strcpy(INPUT, "");
819 update_input(prompt, DEFAULT);
820 while (igetstr(INPUT, INPUTSZ)) {
821 int sel;
822 Color color = RED;
823 length = strlen(INPUT);
824 if (length) {
825 for (sel = 0; sel < rover.nfiles; sel++)
826 if (!strncmp(ENAME(sel), INPUT, length))
827 break;
828 if (sel < rover.nfiles) {
829 color = GREEN;
830 ESEL = sel;
831 if (rover.nfiles > HEIGHT) {
832 if (sel < 3)
833 SCROLL = 0;
834 else if (sel - 3 > rover.nfiles - HEIGHT)
835 SCROLL = rover.nfiles - HEIGHT;
836 else
837 SCROLL = sel - 3;
840 } else {
841 ESEL = oldsel;
842 SCROLL = oldscroll;
844 update_view();
845 update_input(prompt, color);
847 clear_message();
848 update_view();
849 } else if (!strcmp(key, RVK_TG_FILES)) {
850 FLAGS ^= SHOW_FILES;
851 reload();
852 } else if (!strcmp(key, RVK_TG_DIRS)) {
853 FLAGS ^= SHOW_DIRS;
854 reload();
855 } else if (!strcmp(key, RVK_TG_HIDDEN)) {
856 FLAGS ^= SHOW_HIDDEN;
857 reload();
858 } else if (!strcmp(key, RVK_NEW_FILE)) {
859 int ok = 0;
860 char *prompt = "new file: ";
861 strcpy(INPUT, "");
862 update_input(prompt, DEFAULT);
863 while (igetstr(INPUT, INPUTSZ)) {
864 int length = strlen(INPUT);
865 ok = 1;
866 for (i = 0; i < rover.nfiles; i++) {
867 if (
868 !strncmp(ENAME(i), INPUT, length) &&
869 (!strcmp(ENAME(i) + length, "") ||
870 !strcmp(ENAME(i) + length, "/"))
871 ) {
872 ok = 0;
873 break;
876 update_input(prompt, ok ? GREEN : RED);
878 clear_message();
879 if (strlen(INPUT)) {
880 if (ok) {
881 addfile(INPUT);
882 cd(1);
883 try_to_sel(INPUT);
884 update_view();
885 } else
886 message("File already exists.", RED);
888 } else if (!strcmp(key, RVK_NEW_DIR)) {
889 int ok = 0;
890 char *prompt = "new directory: ";
891 strcpy(INPUT, "");
892 update_input(prompt, DEFAULT);
893 while (igetstr(INPUT, INPUTSZ)) {
894 int length = strlen(INPUT);
895 ok = 1;
896 for (i = 0; i < rover.nfiles; i++) {
897 if (
898 !strncmp(ENAME(i), INPUT, length) &&
899 (!strcmp(ENAME(i) + length, "") ||
900 !strcmp(ENAME(i) + length, "/"))
901 ) {
902 ok = 0;
903 break;
906 update_input(prompt, ok ? GREEN : RED);
908 clear_message();
909 if (strlen(INPUT)) {
910 if (ok) {
911 adddir(INPUT);
912 cd(1);
913 try_to_sel(INPUT);
914 update_view();
915 } else
916 message("File already exists.", RED);
918 } else if (!strcmp(key, RVK_RENAME)) {
919 int ok = 0;
920 char *prompt = "rename: ";
921 char *last;
922 int isdir;
923 strcpy(INPUT, ENAME(ESEL));
924 last = INPUT + strlen(INPUT) - 1;
925 if ((isdir = *last == '/'))
926 *last = '\0';
927 update_input(prompt, RED);
928 while (igetstr(INPUT, INPUTSZ)) {
929 int length = strlen(INPUT);
930 ok = 1;
931 for (i = 0; i < rover.nfiles; i++)
932 if (
933 !strncmp(ENAME(i), INPUT, length) &&
934 (!strcmp(ENAME(i) + length, "") ||
935 !strcmp(ENAME(i) + length, "/"))
936 ) {
937 ok = 0;
938 break;
940 update_input(prompt, ok ? GREEN : RED);
942 clear_message();
943 if (strlen(INPUT)) {
944 if (isdir)
945 strcat(INPUT, "/");
946 if (ok) {
947 if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
948 del_mark(&rover.marks, ENAME(ESEL));
949 add_mark(&rover.marks, CWD, INPUT);
951 cd(1);
952 try_to_sel(INPUT);
953 update_view();
954 } else
955 message("File already exists.", RED);
957 } else if (!strcmp(key, RVK_TG_MARK)) {
958 if (MARKED(ESEL))
959 del_mark(&rover.marks, ENAME(ESEL));
960 else
961 add_mark(&rover.marks, CWD, ENAME(ESEL));
962 MARKED(ESEL) = !MARKED(ESEL);
963 ESEL = (ESEL + 1) % rover.nfiles;
964 update_view();
965 } else if (!strcmp(key, RVK_INVMARK)) {
966 for (i = 0; i < rover.nfiles; i++) {
967 if (MARKED(i))
968 del_mark(&rover.marks, ENAME(i));
969 else
970 add_mark(&rover.marks, CWD, ENAME(i));
971 MARKED(i) = !MARKED(i);
973 update_view();
974 } else if (!strcmp(key, RVK_MARKALL)) {
975 for (i = 0; i < rover.nfiles; i++)
976 if (!MARKED(i)) {
977 add_mark(&rover.marks, CWD, ENAME(i));
978 MARKED(i) = 1;
980 update_view();
981 } else if (!strcmp(key, RVK_DELETE)) {
982 if (rover.marks.nentries) {
983 message("Delete marked entries? (Y to confirm)", YELLOW);
984 if (getch() == 'Y')
985 process_marked(NULL, delfile, deldir);
986 else
987 clear_message();
988 } else
989 message("No entries marked for deletion.", RED);
990 } else if (!strcmp(key, RVK_COPY)) {
991 if (rover.marks.nentries)
992 process_marked(adddir, cpyfile, NULL);
993 else
994 message("No entries marked for copying.", RED);
995 } else if (!strcmp(key, RVK_MOVE)) {
996 if (rover.marks.nentries)
997 process_marked(adddir, movfile, deldir);
998 else
999 message("No entries marked for moving.", RED);
1002 if (rover.nfiles)
1003 free_rows(&rover.rows, rover.nfiles);
1004 free_marks(&rover.marks);
1005 delwin(rover.window);
1006 return 0;