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;
135 else {
136 /* Search for empty slot (there must be one). */
137 for (i = 0; i < marks->bulk; i++)
138 if (!marks->entries[i])
139 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] = (char *) malloc(strlen(entry) + 1);
149 strcpy(marks->entries[i], entry);
150 marks->nentries++;
153 static void
154 del_mark(marks_t *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--;
166 else mark_none(marks);
169 static void
170 free_marks(marks_t *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_t color);
184 static void handle_segv(int sig);
185 static void handle_winch(int sig);
187 /* Curses setup. */
188 static void
189 init_term()
191 struct sigaction sa;
193 setlocale(LC_ALL, "");
194 initscr();
195 cbreak(); /* Get one character at a time. */
196 noecho();
197 nonl(); /* No NL->CR/NL on output. */
198 intrflush(stdscr, FALSE);
199 keypad(stdscr, TRUE);
200 curs_set(FALSE); /* Hide blinking cursor. */
201 memset(&sa, 0, sizeof(struct sigaction));
202 /* Setup SIGSEGV handler. */
203 sa.sa_handler = handle_segv;
204 sigaction(SIGSEGV, &sa, NULL);
205 /* Setup SIGWINCH handler. */
206 sa.sa_handler = handle_winch;
207 sigaction(SIGWINCH, &sa, NULL);
208 if (has_colors()) {
209 start_color();
210 init_pair(RED, COLOR_RED, COLOR_BLACK);
211 init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);
212 init_pair(YELLOW, COLOR_YELLOW,COLOR_BLACK);
213 init_pair(BLUE, COLOR_BLUE, COLOR_BLACK);
214 init_pair(CYAN, COLOR_CYAN, COLOR_BLACK);
215 init_pair(MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
216 init_pair(WHITE, COLOR_WHITE, COLOR_BLACK);
218 atexit((void (*)(void)) endwin);
221 /* Update the listing view. */
222 static void
223 update_view()
225 int i, j;
226 int ishidden, isdir;
227 int marking;
229 mvhline(0, 0, ' ', COLS);
230 color_set(RVC_CWD, NULL);
231 mvaddnstr(0, 0, CWD, COLS);
232 color_set(DEFAULT, NULL);
233 attr_on(A_BOLD, NULL);
234 color_set(RVC_TABNUM, NULL);
235 mvaddch(0, COLS-4, rover.tab + '0');
236 color_set(DEFAULT, NULL);
237 attr_off(A_BOLD, NULL);
238 wclear(rover.window);
239 wcolor_set(rover.window, RVC_BORDER, NULL);
240 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
241 wcolor_set(rover.window, DEFAULT, NULL);
242 /* Selection might not be visible, due to cursor wrapping or window
243 shrinking. In that case, the scroll must be moved to make it visible. */
244 SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
245 marking = !strcmp(CWD, rover.marks.dirpath);
246 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
247 ishidden = ENAME(j)[0] == '.';
248 isdir = ISDIR(ENAME(j));
249 if (j == ESEL)
250 wattr_on(rover.window, A_REVERSE, NULL);
251 if (ishidden)
252 wcolor_set(rover.window, RVC_HIDDEN, NULL);
253 else if (isdir)
254 wcolor_set(rover.window, RVC_DIR, NULL);
255 else
256 wcolor_set(rover.window, RVC_FILE, NULL);
257 if (!isdir)
258 sprintf(ROW, "%s%*d", ENAME(j),
259 COLS - strlen(ENAME(j)) - 4, (int) ESIZE(j));
260 else
261 strcpy(ROW, ENAME(j));
262 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
263 if (marking && MARKED(j))
264 mvwaddch(rover.window, i + 1, 1, RVS_MARK);
265 else
266 mvwaddch(rover.window, i + 1, 1, ' ');
267 mvwaddnstr(rover.window, i + 1, 3, ROW, COLS - 4);
268 wcolor_set(rover.window, DEFAULT, NULL);
269 if (j == ESEL)
270 wattr_off(rover.window, A_REVERSE, NULL);
272 if (rover.nfiles > HEIGHT) {
273 int center, height;
274 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
275 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
276 if (!height) height = 1;
277 wcolor_set(rover.window, RVC_BORDER, NULL);
278 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
279 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
280 mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
281 wcolor_set(rover.window, DEFAULT, NULL);
283 wrefresh(rover.window);
284 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
285 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
286 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
287 if (!rover.nfiles)
288 strcpy(ROW, "0/0");
289 else
290 sprintf(ROW, "%d/%d", ESEL + 1, rover.nfiles);
291 sprintf(STATUS+3, "%12s", ROW);
292 color_set(RVC_STATUS, NULL);
293 mvaddstr(LINES - 1, STATUSPOS, STATUS);
294 color_set(DEFAULT, NULL);
295 refresh();
298 /* SIGSEGV handler: clean up curses before exiting. */
299 static void
300 handle_segv(int sig)
302 (void) sig;
303 endwin();
304 puts("Received SIGSEGV (segmentation fault).");
305 exit(1);
308 /* SIGWINCH handler: resize application according to new terminal settings. */
309 static void
310 handle_winch(int sig)
312 (void) sig;
313 delwin(rover.window);
314 endwin();
315 refresh();
316 clear();
317 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
318 update_view();
321 /* Comparison used to sort listing entries. */
322 static int
323 rowcmp(const void *a, const void *b)
325 int isdir1, isdir2, cmpdir;
326 const row_t *r1 = a;
327 const row_t *r2 = b;
328 isdir1 = ISDIR(r1->name);
329 isdir2 = ISDIR(r2->name);
330 cmpdir = isdir2 - isdir1;
331 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
334 /* Get all entries for a given path (usually cwd). */
335 static int
336 ls(char *path, row_t **rowsp, uint8_t flags)
338 DIR *dp;
339 struct dirent *ep;
340 struct stat statbuf;
341 row_t *rows;
342 int i, n;
344 if(!(dp = opendir(path))) return -1;
345 n = -2; /* We don't want the entries "." and "..". */
346 while (readdir(dp)) n++;
347 rewinddir(dp);
348 rows = (row_t *) malloc(n * sizeof(row_t));
349 i = 0;
350 while ((ep = readdir(dp))) {
351 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
352 continue;
353 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
354 continue;
355 /* FIXME: ANSI C doesn't have lstat(). How do we handle symlinks? */
356 stat(ep->d_name, &statbuf);
357 if (S_ISDIR(statbuf.st_mode)) {
358 if (flags & SHOW_DIRS) {
359 rows[i].name = (char *) malloc(strlen(ep->d_name) + 2);
360 strcpy(rows[i].name, ep->d_name);
361 strcat(rows[i].name, "/");
362 i++;
365 else if (flags & SHOW_FILES) {
366 rows[i].name = (char *) malloc(strlen(ep->d_name) + 1);
367 strcpy(rows[i].name, ep->d_name);
368 rows[i].size = statbuf.st_size;
369 i++;
372 n = i; /* Ignore unused space in array caused by filters. */
373 qsort(rows, n, sizeof(row_t), rowcmp);
374 closedir(dp);
375 *rowsp = rows;
376 return n;
379 static void
380 free_rows(row_t **rowsp, int nfiles)
382 int i;
384 for (i = 0; i < nfiles; i++)
385 free((*rowsp)[i].name);
386 free(*rowsp);
387 *rowsp = NULL;
390 /* Change working directory. */
391 /* NOTE: The caller needs to write the new path to CWD
392 * *before* calling this function. */
393 static void
394 cd(int reset)
396 int i, j;
398 if (reset) ESEL = SCROLL = 0;
399 chdir(CWD);
400 if (rover.nfiles)
401 free_rows(&rover.rows, rover.nfiles);
402 rover.nfiles = ls(CWD, &rover.rows, FLAGS);
403 if (!strcmp(CWD, rover.marks.dirpath)) {
404 for (i = 0; i < rover.nfiles; i++) {
405 for (j = 0; j < rover.marks.bulk; j++)
406 if (
407 rover.marks.entries[j] &&
408 !strcmp(rover.marks.entries[j], ENAME(i))
410 break;
411 MARKED(i) = j < rover.marks.bulk;
414 else for (i = 0; i < rover.nfiles; i++)
415 MARKED(i) = 0;
416 update_view();
419 /* Recursively process a source directory using CWD as destination root.
420 * For each node (i.e. directory), do the following:
421 * 1. call pre(destination);
422 * 2. call proc() on every child leaf (i.e. files);
423 * 3. recurse into every child node;
424 * 4. call pos(source).
425 * E.g. to move directory /src/ (and all its contents) inside /dst/:
426 * strcpy(CWD, "/dst/");
427 * process_dir(adddir, movfile, deldir, "/src/");
428 */
429 static void
430 process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
432 DIR *dp;
433 struct dirent *ep;
434 struct stat statbuf;
435 char subpath[FILENAME_MAX];
437 if (pre) {
438 char dstpath[FILENAME_MAX];
439 strcpy(dstpath, CWD);
440 strcat(dstpath, path + strlen(rover.marks.dirpath));
441 pre(dstpath);
443 if(!(dp = opendir(path))) return;
444 while ((ep = readdir(dp))) {
445 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
446 continue;
447 sprintf(subpath, "%s%s", path, ep->d_name);
448 stat(subpath, &statbuf);
449 if (S_ISDIR(statbuf.st_mode)) {
450 strcat(subpath, "/");
451 process_dir(pre, proc, pos, subpath);
453 else proc(subpath);
455 closedir(dp);
456 if (pos) pos(path);
459 /* Process all marked entries using CWD as destination root.
460 * All marked entries that are directories will be recursively processed.
461 * See process_dir() for details on the parameters.
462 */
463 static void
464 process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
466 int i;
467 char path[FILENAME_MAX];
469 for (i = 0; i < rover.marks.bulk; i++)
470 if (rover.marks.entries[i]) {
471 sprintf(path, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
472 if (ISDIR(rover.marks.entries[i])) {
473 if (!strncmp(path, CWD, strlen(path)))
474 message("Cannot copy/move directory inside itself.", RED);
475 else
476 process_dir(pre, proc, pos, path);
478 else proc(path);
480 mark_none(&rover.marks);
481 cd(1);
484 /* Wrappers for file operations. */
485 static PROCESS delfile = unlink;
486 static PROCESS deldir = rmdir;
487 static int addfile(const char *path) {
488 /* Using creat(2) because mknod(2) doesn't seem to be portable. */
489 int ret;
491 ret = creat(path, 0644);
492 if (ret < 0) return ret;
493 return close(ret);
495 static int cpyfile(const char *srcpath) {
496 int src, dst, ret;
497 size_t size;
498 struct stat st;
499 char buf[BUFSIZ];
500 char dstpath[FILENAME_MAX];
502 ret = src = open(srcpath, O_RDONLY);
503 if (ret < 0) return ret;
504 ret = fstat(src, &st);
505 if (ret < 0) return ret;
506 strcpy(dstpath, CWD);
507 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
508 ret = dst = creat(dstpath, st.st_mode);
509 if (ret < 0) return ret;
510 while ((size = read(src, buf, BUFSIZ)) > 0)
511 write(dst, buf, size);
512 close(src);
513 close(dst);
514 return 0;
516 static int adddir(const char *path) {
517 int ret;
518 struct stat st;
520 ret = stat(CWD, &st);
521 if (ret < 0) return ret;
522 return mkdir(path, st.st_mode);
524 static int movfile(const char *srcpath) {
525 char dstpath[FILENAME_MAX];
527 strcpy(dstpath, CWD);
528 strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
529 return rename(srcpath, dstpath);
532 /* Do a fork-exec to external program (e.g. $EDITOR). */
533 static void
534 spawn()
536 pid_t pid;
537 int status;
539 pid = fork();
540 if (pid > 0) {
541 /* fork() succeeded. */
542 endwin();
543 waitpid(pid, &status, 0);
544 init_term();
545 doupdate();
547 else if (pid == 0) {
548 /* Child process. */
549 execvp(ARGS[0], ARGS);
553 /* Interactive getstr(). */
554 static int
555 igetstr(char *buffer, int maxlen)
557 int ch, length;
559 length = strlen(buffer);
560 curs_set(TRUE);
561 ch = getch();
562 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
563 curs_set(FALSE);
564 return 0;
566 else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
567 if (length)
568 buffer[--length] = '\0';
570 else if (ch == killchar()) {
571 length = 0;
572 buffer[0] = '\0';
574 else if (length < maxlen - 1 && isprint(ch)) {
575 buffer[length++] = ch;
576 buffer[length] = '\0';
578 return 1;
581 /* Update line input on the screen. */
582 static void
583 update_input(char *prompt, color_t color)
585 int plen, ilen;
587 plen = strlen(prompt);
588 ilen = strlen(INPUT);
589 color_set(RVC_PROMPT, NULL);
590 mvaddstr(LINES - 1, 0, prompt);
591 color_set(color, NULL);
592 mvaddstr(LINES - 1, plen, INPUT);
593 mvaddch(LINES - 1, ilen + plen, ' ');
594 move(LINES - 1, ilen + plen);
595 color_set(DEFAULT, NULL);
598 /* Show a message on the status bar. */
599 static void
600 message(const char *msg, color_t color)
602 int len, pos;
604 len = strlen(msg);
605 pos = (STATUSPOS - len) >> 1;
606 attr_on(A_BOLD, NULL);
607 color_set(color, NULL);
608 mvaddstr(LINES - 1, pos, msg);
609 color_set(DEFAULT, NULL);
610 attr_off(A_BOLD, NULL);
613 int
614 main(int argc, char *argv[])
616 int i, ch;
617 char *program, *key;
618 DIR *d;
620 init_term();
621 rover.nfiles = 0;
622 for (i = 0; i < 10; i++) {
623 rover.esel[i] = rover.scroll[i] = 0;
624 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
626 strcpy(rover.cwd[0], getenv("HOME"));
627 for (i = 1; i < argc && i < 10; i++) {
628 if ((d = opendir(argv[i]))) {
629 strcpy(rover.cwd[i], argv[i]);
630 closedir(d);
632 else strcpy(rover.cwd[i], rover.cwd[0]);
634 getcwd(rover.cwd[i], FILENAME_MAX);
635 for (i++; i < 10; i++)
636 strcpy(rover.cwd[i], rover.cwd[i-1]);
637 for (i = 0; i < 10; i++)
638 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
639 strcat(rover.cwd[i], "/");
640 rover.tab = 1;
641 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
642 init_marks(&rover.marks);
643 cd(1);
644 while (1) {
645 ch = getch();
646 key = keyname(ch);
647 mvhline(LINES - 1, 0, ' ', STATUSPOS); /* Clear message area. */
648 if (!strcmp(key, RVK_QUIT)) break;
649 else if (ch >= '0' && ch <= '9') {
650 rover.tab = ch - '0';
651 cd(0);
653 else if (!strcmp(key, RVK_DOWN)) {
654 if (!rover.nfiles) continue;
655 ESEL = (ESEL + 1) % rover.nfiles;
656 update_view();
658 else if (!strcmp(key, RVK_UP)) {
659 if (!rover.nfiles) continue;
660 ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
661 update_view();
663 else if (!strcmp(key, RVK_JUMP_DOWN)) {
664 if (!rover.nfiles) continue;
665 ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
666 if (rover.nfiles > HEIGHT)
667 SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
668 update_view();
670 else if (!strcmp(key, RVK_JUMP_UP)) {
671 if (!rover.nfiles) continue;
672 ESEL = MAX(ESEL - RV_JUMP, 0);
673 SCROLL = MAX(SCROLL - RV_JUMP, 0);
674 update_view();
676 else if (!strcmp(key, RVK_CD_DOWN)) {
677 if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
678 strcat(CWD, ENAME(ESEL));
679 cd(1);
681 else if (!strcmp(key, RVK_CD_UP)) {
682 char *dirname, first;
683 if (strlen(CWD) == 1) continue;
684 CWD[strlen(CWD) - 1] = '\0';
685 dirname = strrchr(CWD, '/') + 1;
686 first = dirname[0];
687 dirname[0] = '\0';
688 cd(1);
689 if ((FLAGS & SHOW_DIRS) &&
690 ((FLAGS & SHOW_HIDDEN) || (first != '.'))
691 ) {
692 dirname[0] = first;
693 dirname[strlen(dirname)] = '/';
694 while (strcmp(ENAME(ESEL), dirname))
695 ESEL++;
696 if (rover.nfiles > HEIGHT) {
697 SCROLL = ESEL - (HEIGHT >> 1);
698 SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
700 dirname[0] = '\0';
701 update_view();
704 else if (!strcmp(key, RVK_HOME)) {
705 strcpy(CWD, getenv("HOME"));
706 if (CWD[strlen(CWD) - 1] != '/')
707 strcat(CWD, "/");
708 cd(1);
710 else if (!strcmp(key, RVK_SHELL)) {
711 program = getenv("SHELL");
712 if (program) {
713 ARGS[0] = program;
714 ARGS[1] = NULL;
715 spawn();
716 cd(1);
719 else if (!strcmp(key, RVK_VIEW)) {
720 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
721 program = getenv("PAGER");
722 if (program) {
723 ARGS[0] = program;
724 ARGS[1] = ENAME(ESEL);
725 ARGS[2] = NULL;
726 spawn();
729 else if (!strcmp(key, RVK_EDIT)) {
730 if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
731 program = getenv("EDITOR");
732 if (program) {
733 ARGS[0] = program;
734 ARGS[1] = ENAME(ESEL);
735 ARGS[2] = NULL;
736 spawn();
737 cd(0);
740 else if (!strcmp(key, RVK_SEARCH)) {
741 int oldsel, oldscroll, length;
742 char *prompt = "search: ";
743 if (!rover.nfiles) continue;
744 oldsel = ESEL;
745 oldscroll = SCROLL;
746 strcpy(INPUT, "");
747 update_input(prompt, DEFAULT);
748 while (igetstr(INPUT, INPUTSZ)) {
749 int sel;
750 color_t color = RED;
751 length = strlen(INPUT);
752 if (length) {
753 for (sel = 0; sel < rover.nfiles; sel++)
754 if (!strncmp(ENAME(sel), INPUT, length))
755 break;
756 if (sel < rover.nfiles) {
757 color = GREEN;
758 ESEL = sel;
759 if (rover.nfiles > HEIGHT) {
760 if (sel < 3)
761 SCROLL = 0;
762 else if (sel - 3 > rover.nfiles - HEIGHT)
763 SCROLL = rover.nfiles - HEIGHT;
764 else
765 SCROLL = sel - 3;
769 else {
770 ESEL = oldsel;
771 SCROLL = oldscroll;
773 update_view();
774 update_input(prompt, color);
776 mvhline(LINES - 1, 0, ' ', STATUSPOS);
777 update_view();
779 else if (!strcmp(key, RVK_TG_FILES)) {
780 FLAGS ^= SHOW_FILES;
781 cd(1);
783 else if (!strcmp(key, RVK_TG_DIRS)) {
784 FLAGS ^= SHOW_DIRS;
785 cd(1);
787 else if (!strcmp(key, RVK_TG_HIDDEN)) {
788 FLAGS ^= SHOW_HIDDEN;
789 cd(1);
791 else if (!strcmp(key, RVK_NEW_FILE)) {
792 int ok = 0;
793 char *prompt = "new file: ";
794 strcpy(INPUT, "");
795 update_input(prompt, DEFAULT);
796 while (igetstr(INPUT, INPUTSZ)) {
797 int length = strlen(INPUT);
798 ok = 1;
799 for (i = 0; i < rover.nfiles; i++) {
800 if (
801 !strncmp(ENAME(i), INPUT, length) &&
802 (!strcmp(ENAME(i) + length, "") ||
803 !strcmp(ENAME(i) + length, "/"))
804 ) {
805 ok = 0;
806 break;
809 update_input(prompt, ok ? GREEN : RED);
811 mvhline(LINES - 1, 0, ' ', STATUSPOS);
812 if (strlen(INPUT)) {
813 if (ok) { addfile(INPUT); cd(1); }
814 else message("File already exists.", RED);
817 else if (!strcmp(key, RVK_NEW_DIR)) {
818 int ok = 0;
819 char *prompt = "new directory: ";
820 strcpy(INPUT, "");
821 update_input(prompt, DEFAULT);
822 while (igetstr(INPUT, INPUTSZ)) {
823 int length = strlen(INPUT);
824 ok = 1;
825 for (i = 0; i < rover.nfiles; i++) {
826 if (
827 !strncmp(ENAME(i), INPUT, length) &&
828 (!strcmp(ENAME(i) + length, "") ||
829 !strcmp(ENAME(i) + length, "/"))
830 ) {
831 ok = 0;
832 break;
835 update_input(prompt, ok ? GREEN : RED);
837 mvhline(LINES - 1, 0, ' ', STATUSPOS);
838 if (strlen(INPUT)) {
839 if (ok) { adddir(INPUT); cd(1); }
840 else message("File already exists.", RED);
843 else if (!strcmp(key, RVK_RENAME)) {
844 int ok = 0;
845 char *prompt = "rename: ";
846 strcpy(INPUT, ENAME(ESEL));
847 update_input(prompt, RED);
848 while (igetstr(INPUT, INPUTSZ)) {
849 int length = strlen(INPUT);
850 ok = 1;
851 for (i = 0; i < rover.nfiles; i++) {
852 if (
853 !strncmp(ENAME(i), INPUT, length) &&
854 (!strcmp(ENAME(i) + length, "") ||
855 !strcmp(ENAME(i) + length, "/"))
856 ) {
857 ok = 0;
858 break;
861 update_input(prompt, ok ? GREEN : RED);
863 mvhline(LINES - 1, 0, ' ', STATUSPOS);
864 if (strlen(INPUT)) {
865 if (ok) { rename(ENAME(ESEL), INPUT); cd(1); }
866 else message("File already exists.", RED);
869 else if (!strcmp(key, RVK_TG_MARK)) {
870 if (MARKED(ESEL))
871 del_mark(&rover.marks, ENAME(ESEL));
872 else
873 add_mark(&rover.marks, CWD, ENAME(ESEL));
874 MARKED(ESEL) = !MARKED(ESEL);
875 ESEL = (ESEL + 1) % rover.nfiles;
876 update_view();
878 else if (!strcmp(key, RVK_INVMARK)) {
879 for (i = 0; i < rover.nfiles; i++) {
880 if (MARKED(i))
881 del_mark(&rover.marks, ENAME(i));
882 else
883 add_mark(&rover.marks, CWD, ENAME(i));
884 MARKED(i) = !MARKED(i);
886 update_view();
888 else if (!strcmp(key, RVK_MARKALL)) {
889 for (i = 0; i < rover.nfiles; i++)
890 if (!MARKED(i)) {
891 add_mark(&rover.marks, CWD, ENAME(i));
892 MARKED(i) = 1;
894 update_view();
896 else if (!strcmp(key, RVK_DELETE)) {
897 if (rover.marks.nentries) {
898 message("Delete marked entries? (Y to confirm)", YELLOW);
899 if (getch() == 'Y')
900 process_marked(NULL, delfile, deldir);
901 mvhline(LINES - 1, 0, ' ', STATUSPOS); /* Clear message area. */
903 else message("No entries marked for deletion.", RED);
905 else if (!strcmp(key, RVK_COPY)) {
906 if (rover.marks.nentries)
907 process_marked(adddir, cpyfile, NULL);
908 else message("No entries marked for copying.", RED);
910 else if (!strcmp(key, RVK_MOVE)) {
911 if (rover.marks.nentries)
912 process_marked(adddir, movfile, deldir);
913 else message("No entries marked for moving.", RED);
916 if (rover.nfiles)
917 free_rows(&rover.rows, rover.nfiles);
918 free_marks(&rover.marks);
919 delwin(rover.window);
920 return 0;