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() */
9 #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
10 #include <sys/stat.h>
11 #include <sys/wait.h> /* waitpid() */
12 #include <curses.h>
14 #include "config.h"
16 #define ROWSZ 256
17 char ROW[ROWSZ];
18 #define STATUSSZ 256
19 char STATUS[STATUSSZ];
20 #define SEARCHSZ 256
21 char SEARCH[SEARCHSZ];
22 #define MAXARGS 256
23 char *args[MAXARGS];
25 typedef enum {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} color_t;
27 #define HEIGHT (LINES-4)
29 #define SHOW_FILES 0x01u
30 #define SHOW_DIRS 0x02u
31 #define SHOW_HIDDEN 0x04u
33 typedef struct {
34 char *name;
35 off_t size;
36 } row_t;
38 struct rover_t {
39 int tab;
40 int nfiles;
41 int scroll[10];
42 int fsel[10];
43 uint8_t flags[10];
44 row_t *rows;
45 WINDOW *window;
46 char cwd[10][FILENAME_MAX];
47 } rover;
49 #define FNAME(I) rover.rows[I].name
50 #define FSIZE(I) rover.rows[I].size
51 #define SCROLL rover.scroll[rover.tab]
52 #define FSEL rover.fsel[rover.tab]
53 #define FLAGS rover.flags[rover.tab]
54 #define CWD rover.cwd[rover.tab]
56 static int
57 rowcmp(const void *a, const void *b)
58 {
59 int isdir1, isdir2, cmpdir;
60 const row_t *r1 = a;
61 const row_t *r2 = b;
62 isdir1 = strchr(r1->name, '/') != NULL;
63 isdir2 = strchr(r2->name, '/') != NULL;
64 cmpdir = isdir2 - isdir1;
65 return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
66 }
68 void
69 free_rows(row_t **rowsp, int nfiles)
70 {
71 int i;
73 for (i = 0; i < nfiles; i++)
74 free((*rowsp)[i].name);
75 free(*rowsp);
76 *rowsp = NULL;
77 }
79 int
80 ls(char *path, row_t **rowsp, uint8_t flags)
81 {
82 DIR *dp;
83 struct dirent *ep;
84 struct stat statbuf;
85 row_t *rows;
86 int i, n;
88 if((dp = opendir(path)) == NULL)
89 return -1;
90 n = -2; /* We don't want the entries "." and "..". */
91 while (readdir(dp)) n++;
92 rewinddir(dp);
93 rows = (row_t *) malloc(n * sizeof(row_t));
94 i = 0;
95 while ((ep = readdir(dp))) {
96 if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
97 continue;
98 if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
99 continue;
100 /* FIXME: ANSI C doesn't have lstat(). How do we handle symlinks? */
101 stat(ep->d_name, &statbuf);
102 if (S_ISDIR(statbuf.st_mode)) {
103 if (flags & SHOW_DIRS) {
104 rows[i].name = (char *) malloc(strlen(ep->d_name) + 2);
105 strcpy(rows[i].name, ep->d_name);
106 strcat(rows[i].name, "/");
107 i++;
110 else if (flags & SHOW_FILES) {
111 rows[i].name = (char *) malloc(strlen(ep->d_name) + 1);
112 strcpy(rows[i].name, ep->d_name);
113 rows[i].size = statbuf.st_size;
114 i++;
117 n = i; /* Ignore unused space in array caused by filters. */
118 qsort(rows, n, sizeof(row_t), rowcmp);
119 closedir(dp);
120 *rowsp = rows;
121 return n;
124 static void
125 clean_term()
127 endwin();
130 static void
131 init_term()
133 setlocale(LC_ALL, "");
134 initscr();
135 cbreak(); /* Get one character at a time. */
136 noecho();
137 nonl(); /* No NL->CR/NL on output. */
138 intrflush(stdscr, FALSE);
139 keypad(stdscr, TRUE);
140 curs_set(FALSE); /* Hide blinking cursor. */
141 if (has_colors()) {
142 start_color();
143 init_pair(RED, COLOR_RED, COLOR_BLACK);
144 init_pair(GREEN, COLOR_GREEN, COLOR_BLACK);
145 init_pair(YELLOW, COLOR_YELLOW,COLOR_BLACK);
146 init_pair(BLUE, COLOR_BLUE, COLOR_BLACK);
147 init_pair(CYAN, COLOR_CYAN, COLOR_BLACK);
148 init_pair(MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
149 init_pair(WHITE, COLOR_WHITE, COLOR_BLACK);
151 atexit(clean_term);
154 static void
155 update_browser()
157 int i, j;
158 int ishidden, isdir;
160 for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
161 ishidden = FNAME(j)[0] == '.';
162 isdir = strchr(FNAME(j), '/') != NULL;
163 if (j == FSEL)
164 wattr_on(rover.window, A_REVERSE, NULL);
165 if (ishidden)
166 wcolor_set(rover.window, RVC_HIDDEN, NULL);
167 else if (isdir)
168 wcolor_set(rover.window, RVC_DIR, NULL);
169 else
170 wcolor_set(rover.window, RVC_FILE, NULL);
171 if (!isdir)
172 sprintf(ROW, "%s%*d", FNAME(j),
173 COLS - strlen(FNAME(j)) - 2, (int) FSIZE(j));
174 else
175 strcpy(ROW, FNAME(j));
176 mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
177 mvwaddnstr(rover.window, i + 1, 1, ROW, COLS - 2);
178 wcolor_set(rover.window, DEFAULT, NULL);
179 if (j == FSEL)
180 wattr_off(rover.window, A_REVERSE, NULL);
182 if (rover.nfiles > HEIGHT) {
183 int center, height;
184 center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
185 height = (HEIGHT-1) * HEIGHT / rover.nfiles;
186 if (!height) height = 1;
187 wcolor_set(rover.window, RVC_BORDER, NULL);
188 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
189 wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
190 mvwvline(rover.window, center-(height>>1)+1, COLS-1, ACS_CKBOARD, height);
191 wcolor_set(rover.window, DEFAULT, NULL);
193 wrefresh(rover.window);
194 STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
195 STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
196 STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
197 if (!rover.nfiles)
198 strcpy(ROW, "0/0");
199 else
200 sprintf(ROW, "%d/%d", FSEL + 1, rover.nfiles);
201 sprintf(STATUS+3, "%*s", 12, ROW);
202 color_set(RVC_STATUS, NULL);
203 mvaddstr(LINES - 1, COLS - 15, STATUS);
204 color_set(DEFAULT, NULL);
205 refresh();
208 /* NOTE: The caller needs to write the new path to CWD
209 * *before* calling this function. */
210 static void
211 cd(int reset)
213 if (reset)
214 FSEL = SCROLL = 0;
215 chdir(CWD);
216 mvhline(0, 0, ' ', COLS);
217 color_set(RVC_CWD, NULL);
218 mvaddnstr(0, 0, CWD, COLS);
219 color_set(DEFAULT, NULL);
220 move(0, COLS-2);
221 attr_on(A_BOLD, NULL);
222 color_set(RVC_TABNUM, NULL);
223 echochar(rover.tab + '0');
224 color_set(DEFAULT, NULL);
225 attr_off(A_BOLD, NULL);
226 if (rover.nfiles)
227 free_rows(&rover.rows, rover.nfiles);
228 rover.nfiles = ls(CWD, &rover.rows, FLAGS);
229 wclear(rover.window);
230 wcolor_set(rover.window, RVC_BORDER, NULL);
231 wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
232 wcolor_set(rover.window, DEFAULT, NULL);
233 update_browser();
236 static void
237 spawn()
239 pid_t pid;
240 int status;
242 pid = fork();
243 if (pid > 0) {
244 /* fork() succeeded. */
245 clean_term();
246 waitpid(pid, &status, 0);
247 init_term();
248 doupdate();
250 else if (pid == 0) {
251 /* Child process. */
252 execvp(args[0], args);
256 /* Interactive getstr(). */
257 int
258 igetstr(char *buffer, int maxlen)
260 int ch, length;
262 length = strlen(buffer);
263 ch = getch();
264 if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER)
265 return 0;
266 else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
267 if (length)
268 buffer[--length] = '\0';
270 else if (ch == killchar()) {
271 length = 0;
272 buffer[0] = '\0';
274 else if (length < maxlen - 1 && isprint(ch)) {
275 buffer[length++] = ch;
276 buffer[length] = '\0';
278 return 1;
281 int
282 main(int argc, char *argv[])
284 int i, ch;
285 char *program, *key;
286 DIR *d;
288 init_term();
289 /* Avoid invalid free() calls in cd() by zeroing the tally. */
290 rover.nfiles = 0;
291 for (i = 0; i < 10; i++) {
292 rover.fsel[i] = rover.scroll[i] = 0;
293 rover.flags[i] = SHOW_FILES | SHOW_DIRS;
295 strcpy(rover.cwd[0], getenv("HOME"));
296 for (i = 1; i < argc && i < 10; i++) {
297 d = opendir(argv[i]);
298 if (d) {
299 strcpy(rover.cwd[i], argv[i]);
300 closedir(d);
302 else strcpy(rover.cwd[i], rover.cwd[0]);
304 getcwd(rover.cwd[i], FILENAME_MAX);
305 for (i++; i < 10; i++)
306 strcpy(rover.cwd[i], rover.cwd[i-1]);
307 for (i = 0; i < 10; i++)
308 if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
309 strcat(rover.cwd[i], "/");
310 rover.tab = 1;
311 rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
312 cd(1);
313 while (1) {
314 ch = getch();
315 key = keyname(ch);
316 if (!strcmp(key, RVK_QUIT))
317 break;
318 else if (ch >= '0' && ch <= '9') {
319 rover.tab = ch - '0';
320 cd(0);
322 else if (!strcmp(key, RVK_DOWN)) {
323 if (!rover.nfiles) continue;
324 if (FSEL == rover.nfiles - 1)
325 SCROLL = FSEL = 0;
326 else {
327 FSEL++;
328 if ((FSEL - SCROLL) == HEIGHT)
329 SCROLL++;
331 update_browser();
333 else if (!strcmp(key, RVK_UP)) {
334 if (!rover.nfiles) continue;
335 if (FSEL == 0) {
336 FSEL = rover.nfiles - 1;
337 SCROLL = rover.nfiles - HEIGHT;
338 if (SCROLL < 0)
339 SCROLL = 0;
341 else {
342 FSEL--;
343 if (FSEL < SCROLL)
344 SCROLL--;
346 update_browser();
348 else if (!strcmp(key, RVK_JUMP_DOWN)) {
349 if (!rover.nfiles) continue;
350 FSEL += RV_JUMP;
351 if (FSEL >= rover.nfiles)
352 FSEL = rover.nfiles - 1;
353 if (rover.nfiles > HEIGHT) {
354 SCROLL += RV_JUMP;
355 if (SCROLL > rover.nfiles - HEIGHT)
356 SCROLL = rover.nfiles - HEIGHT;
358 update_browser();
360 else if (!strcmp(key, RVK_JUMP_UP)) {
361 if (!rover.nfiles) continue;
362 FSEL -= RV_JUMP;
363 if (FSEL < 0)
364 FSEL = 0;
365 SCROLL -= RV_JUMP;
366 if (SCROLL < 0)
367 SCROLL = 0;
368 update_browser();
370 else if (!strcmp(key, RVK_CD_DOWN)) {
371 if (!rover.nfiles) continue;
372 if (strchr(FNAME(FSEL), '/') == NULL)
373 continue;
374 strcat(CWD, FNAME(FSEL));
375 cd(1);
377 else if (!strcmp(key, RVK_CD_UP)) {
378 char *dirname, first;
379 if (strlen(CWD) == 1)
380 continue;
381 CWD[strlen(CWD) - 1] = '\0';
382 dirname = strrchr(CWD, '/') + 1;
383 first = dirname[0];
384 dirname[0] = '\0';
385 cd(1);
386 if ((FLAGS & SHOW_DIRS) &&
387 ((FLAGS & SHOW_HIDDEN) || (first != '.'))
388 ) {
389 dirname[0] = first;
390 dirname[strlen(dirname)] = '/';
391 while (strcmp(FNAME(FSEL), dirname))
392 FSEL++;
393 if (rover.nfiles > HEIGHT) {
394 SCROLL = FSEL - (HEIGHT >> 1);
395 if (SCROLL < 0)
396 SCROLL = 0;
397 if (SCROLL > rover.nfiles - HEIGHT)
398 SCROLL = rover.nfiles - HEIGHT;
400 dirname[0] = '\0';
401 update_browser();
404 else if (!strcmp(key, RVK_HOME)) {
405 strcpy(CWD, getenv("HOME"));
406 if (CWD[strlen(CWD) - 1] != '/')
407 strcat(CWD, "/");
408 cd(1);
410 else if (!strcmp(key, RVK_SHELL)) {
411 program = getenv("SHELL");
412 if (program) {
413 args[0] = program;
414 args[1] = NULL;
415 spawn();
418 else if (!strcmp(key, RVK_VIEW)) {
419 if (!rover.nfiles) continue;
420 if (strchr(FNAME(FSEL), '/') != NULL)
421 continue;
422 program = getenv("PAGER");
423 if (program) {
424 args[0] = program;
425 args[1] = FNAME(FSEL);
426 args[2] = NULL;
427 spawn();
430 else if (!strcmp(key, RVK_EDIT)) {
431 if (!rover.nfiles) continue;
432 if (strchr(FNAME(FSEL), '/') != NULL)
433 continue;
434 program = getenv("EDITOR");
435 if (program) {
436 args[0] = program;
437 args[1] = FNAME(FSEL);
438 args[2] = NULL;
439 spawn();
442 else if (!strcmp(key, RVK_SEARCH)) {
443 int oldsel, oldscroll;
444 if (!rover.nfiles) continue;
445 oldsel = FSEL;
446 oldscroll = SCROLL;
447 *SEARCH = '\0';
448 color_set(RVC_PROMPT, NULL);
449 mvaddstr(LINES - 1, 0, "search: ");
450 color_set(DEFAULT, NULL);
451 while (igetstr(SEARCH, SEARCHSZ)) {
452 int length, sel;
453 color_t color;
454 length = strlen(SEARCH);
455 if (length) {
456 for (sel = 0; sel < rover.nfiles; sel++)
457 if (!strncmp(FNAME(sel), SEARCH, length))
458 break;
459 if (sel < rover.nfiles) {
460 color = GREEN;
461 FSEL = sel;
462 if (rover.nfiles > HEIGHT) {
463 if (sel < 3)
464 SCROLL = 0;
465 else if (sel - 3 > rover.nfiles - HEIGHT)
466 SCROLL = rover.nfiles - HEIGHT;
467 else
468 SCROLL = sel - 3;
471 else
472 color = RED;
474 else {
475 FSEL = oldsel;
476 SCROLL = oldscroll;
478 update_browser();
479 strcat(SEARCH, " ");
480 color_set(color, NULL);
481 mvaddstr(LINES - 1, 8, SEARCH);
482 color_set(DEFAULT, NULL);
483 SEARCH[length] = '\0';
485 move(LINES - 1, 0);
486 clrtoeol();
487 update_browser();
489 else if (!strcmp(key, RVK_TG_FILES)) {
490 FLAGS ^= SHOW_FILES;
491 cd(1);
493 else if (!strcmp(key, RVK_TG_DIRS)) {
494 FLAGS ^= SHOW_DIRS;
495 cd(1);
497 else if (!strcmp(key, RVK_TG_HIDDEN)) {
498 FLAGS ^= SHOW_HIDDEN;
499 cd(1);
502 if (rover.nfiles) {
503 free_rows(&rover.rows, rover.nfiles);
505 delwin(rover.window);
506 return 0;