[ref] print_row() S_ISLNK
[sfm.git] / sfm.c
blob59489c7427a9b517fdb55c13af355ef7d6963a50
1 /* See LICENSE file for copyright and license details. */
3 #if defined(__linux__)
4 #define _GNU_SOURCE
5 #endif
6 #include <sys/types.h>
7 #include <sys/resource.h>
8 #include <sys/stat.h>
9 #include <sys/time.h>
10 #include <sys/wait.h>
11 #if defined(__linux__)
12 #include <sys/inotify.h>
13 #elif defined(__FreeBSD__) || defined(__NetBSD__) ||\
14 defined(__OpenBSD__) || defined(__APPLE__)
15 #include <sys/event.h>
16 #endif
18 #include <dirent.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <grp.h>
22 #include <pwd.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <libgen.h>
32 #include "termbox.h"
33 #include "util.h"
35 /* macros */
36 #define MAX_P 4096
37 #define MAX_N 255
38 #define MAX_USRI 32
39 #define MAX_EXT 4
40 #define CURSOR_NAME cpane->direntr[cpane->hdir - 1].name
42 /* enums */
43 enum { AddHi, NoHi }; /* add highlight in listdir */
45 /* typedef */
46 typedef struct {
47 char name[MAX_N];
48 gid_t group;
49 mode_t mode;
50 off_t size;
51 time_t td;
52 uid_t user;
53 } Entry;
55 typedef struct {
56 uint16_t fg;
57 uint16_t bg;
58 } Cpair;
60 typedef struct {
61 int pane_id;
62 char dirn[MAX_P]; // dir name cwd
63 char *filter;
64 Entry *direntr; // dir entries
65 int dirx; // pane cwd x pos
66 int dirc; // dir entries sum
67 int hdir; // highlighted dir
68 int firstrow;
69 int parent_firstrow;
70 int parent_row; // FIX
71 Cpair dircol;
72 int inotify_wd;
73 int event_fd;
74 } Pane;
76 typedef struct {
77 uint32_t ch;
78 char path[MAX_P];
79 } Bookmark;
81 typedef struct {
82 const char **ext;
83 size_t exlen;
84 const void *v;
85 } Rule;
87 typedef union {
88 uint16_t key; /* one of the TB_KEY_* constants */
89 uint32_t ch; /* unicode character */
90 } Evkey;
92 typedef struct {
93 const Evkey evkey;
94 void (*func)(void);
95 } Key;
97 /* function declarations */
98 static void print_tb(const char *, int, int, uint16_t, uint16_t);
99 static void printf_tb(int, int, Cpair, const char *, ...);
100 static void print_status(Cpair, const char *, ...);
101 static void print_xstatus(char, int);
102 static void print_error(char *);
103 static void print_prompt(char *);
104 static void print_info(void);
105 static void print_row(Pane *, size_t, Cpair);
106 static void clear(int, int, int, uint16_t);
107 static void clear_status(void);
108 static void clear_pane(void);
109 static void add_hi(Pane *, size_t);
110 static void rm_hi(Pane *, size_t);
111 static int check_dir(char *);
112 static mode_t chech_execf(mode_t);
113 static int sort_name(const void *const, const void *const);
114 static void get_dirp(char *);
115 static char *get_ext(char *);
116 static int get_fdt(char *, size_t, time_t);
117 static char *get_fgrp(gid_t, size_t);
118 static char *get_finfo(Entry *);
119 static char *get_fperm(mode_t);
120 static char *get_fsize(off_t);
121 static char *get_fullpath(char *, char *);
122 static char *get_fusr(uid_t, size_t);
123 static void get_dirsize(char *, off_t *);
124 static void get_hicol(Cpair *, mode_t);
125 static int delent(char *);
126 static void calcdir(void);
127 static void crnd(void);
128 static void crnf(void);
129 static void delfd(void);
130 static void mvbk(void);
131 static void mvbtm(void);
132 static void mvdwn(void);
133 static void mvdwns(void);
134 static void mvfor(void);
135 static void mvmid(void);
136 static void mvtop(void);
137 static void mvup(void);
138 static void mvups(void);
139 static void scrdwn(void);
140 static void scrdwns(void);
141 static void scrup(void);
142 static void scrups(void);
143 static int get_usrinput(char*, size_t, const char*, ...);
144 static int frules(char *);
145 static int spawn(const void *, char *);
146 static int opnf(char *);
147 static int fsev_init(void);
148 static int addwatch(void);
149 static int read_events(void);
150 static void rmwatch(Pane *);
151 static void fsev_shdn(void);
152 static ssize_t findbm(uint32_t);
153 static void start_filter(void);
154 static void start_vmode(void);
155 static void exit_vmode(void);
156 static void selup(void);
157 static void seldwn(void);
158 static void selall(void);
159 static void selref(void);
160 static void selynk(void);
161 static void selcalc(void);
162 static void paste(void);
163 static void selmv(void);
164 static void seldel(void);
165 static void init_files(void);
166 static void free_files(void);
167 static void yank(void);
168 static void rname(void);
169 static void switch_pane(void);
170 static void quit(void);
171 static void grabkeys(struct tb_event*, Key*, size_t);
172 static void start_ev(void);
173 static void refresh_pane(void);
174 static void set_direntr(struct dirent *, DIR *, char *);
175 static int listdir(int);
176 static void t_resize(void);
177 static void set_panes(void);
178 static void draw_frame(void);
179 static void start(void);
181 /* global variables */
182 static Pane pane_r, pane_l, *cpane;
183 static char *editor[2];
184 static char fed[] = "vi";
185 static int theight, twidth, scrheight;
186 static size_t selection_size = 0;
187 static char yank_file[MAX_P];
188 static int *selection;
189 static int cont_vmode = 0;
190 static char **selected_files;
191 #if defined _SYS_INOTIFY_H
192 static int inotify_fd;
193 #elif defined _SYS_EVENT_H_
194 static int kq;
195 struct kevent evlist[2]; /* events we want to monitor */
196 struct kevent chlist[2]; /* events that were triggered */
197 static struct timespec gtimeout;
198 #endif
200 /* configuration, allows nested code to access above variables */
201 #include "config.h"
203 /* function implementations */
204 static void
205 print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
207 while (*str != '\0') {
208 uint32_t uni = 0;
209 str += tb_utf8_char_to_unicode(&uni, str);
210 tb_change_cell(x, y, uni, fg, bg);
211 x++;
215 static void
216 printf_tb(int x, int y, Cpair col, const char *fmt, ...)
218 char buf[4096];
219 va_list vl;
220 va_start(vl, fmt);
221 (void)vsnprintf(buf, sizeof(buf), fmt, vl);
222 va_end(vl);
223 print_tb(buf, x, y, col.fg, col.bg);
226 static void
227 print_status(Cpair col, const char *fmt, ...)
229 char buf[256];
230 va_list vl;
231 va_start(vl, fmt);
232 (void)vsnprintf(buf, sizeof(buf), fmt, vl);
233 va_end(vl);
234 clear_status();
235 print_tb(buf, 1, theight - 1, col.fg, col.bg);
238 static void
239 print_xstatus(char c, int x)
241 uint32_t uni = 0;
242 (void)tb_utf8_char_to_unicode(&uni, &c);
243 tb_change_cell(x, theight - 1, uni, cstatus.fg, cstatus.bg);
246 static void
247 print_error(char *errmsg)
249 print_status(cerr, errmsg);
252 static void
253 print_prompt(char *prompt)
255 print_status(cprompt, prompt);
258 static void
259 print_info(void)
261 char *fileinfo;
262 fileinfo = get_finfo(&cpane->direntr[cpane->hdir - 1]);
263 print_status(cstatus, "%d/%d %s", cpane->hdir, cpane->dirc, fileinfo);
264 free(fileinfo);
267 static void
268 print_row(Pane *pane, size_t entpos, Cpair col)
270 int x, y;
271 char *result;
272 char buf[MAX_P];
273 char lnk_full[MAX_P];
274 int width;
276 width = (twidth / 2) - 4;
277 result = basename(pane->direntr[entpos].name);
278 x = pane->dirx;
279 y = entpos - cpane->firstrow + 1;
281 if (S_ISLNK(pane->direntr[entpos].mode) != 0) {
282 if (realpath(pane->direntr[entpos].name, buf) != NULL) {
283 (void)snprintf(lnk_full, MAX_N,
284 "%s -> %s", result, buf);
285 result = lnk_full;
289 printf_tb(x, y, col, "%*.*s", ~width, width, result);
292 static void
293 clear(int sx, int ex, int y, uint16_t bg)
295 /* clear line from to */
296 /* x = line number vertical */
297 /* y = column number horizontal */
298 int i;
299 for (i = sx; i < ex; i++) {
300 tb_change_cell(i, y, 0x0000, TB_DEFAULT, bg);
304 static void
305 clear_status(void)
307 clear(1, twidth - 1, theight - 1, cstatus.bg);
310 static void
311 clear_pane(void)
313 int i, ex, y;
314 y = 0, i = 0, ex = 0;
316 if (cpane->pane_id == pane_l.pane_id)
317 ex = (twidth / 2) - 1;
318 else if (cpane->pane_id == pane_r.pane_id)
319 ex = twidth - 1;
321 while (i < scrheight) {
322 clear(cpane->dirx, ex, y, TB_DEFAULT);
323 i++;
324 y++;
327 /* draw top line */
328 for (y = cpane->dirx; y < ex; ++y) {
329 tb_change_cell(y, 0, u_hl, cframe.fg, cframe.bg);
333 static void
334 add_hi(Pane *pane, size_t entpos)
336 Cpair col;
337 get_hicol(&col, pane->direntr[entpos].mode);
338 col.fg |= TB_REVERSE | TB_BOLD;
339 col.bg |= TB_REVERSE;
340 print_row(pane, entpos, col);
343 static void
344 rm_hi(Pane *pane, size_t entpos)
346 Cpair col;
347 get_hicol(&col, pane->direntr[entpos].mode);
348 print_row(pane, entpos, col);
351 static int
352 check_dir(char *path)
354 DIR *dir;
355 dir = opendir(path);
357 if (dir == NULL) {
358 if (errno == ENOTDIR) {
359 return 1;
360 } else {
361 return -1;
365 if (closedir(dir) < 0)
366 return -1;
368 return 0;
371 static mode_t
372 chech_execf(mode_t mode)
374 if (S_ISREG(mode))
375 return (((S_IXUSR | S_IXGRP | S_IXOTH) & mode));
376 return 0;
379 static int
380 sort_name(const void *const A, const void *const B)
382 int result;
383 mode_t data1 = (*(Entry *)A).mode;
384 mode_t data2 = (*(Entry *)B).mode;
386 if (data1 < data2) {
387 return -1;
388 } else if (data1 == data2) {
389 result = strncmp((*(Entry *)A).name, (*(Entry *)B).name, MAX_N);
390 return result;
391 } else {
392 return 1;
396 static void
397 get_dirp(char *cdir)
399 int counter, len, i;
401 counter = 0;
402 len = strnlen(cdir, MAX_P);
403 if (len == 1)
404 return;
406 for (i = len - 1; i > 1; i--) {
407 if (cdir[i] == '/')
408 break;
409 else
410 counter++;
413 cdir[len-counter-1] = '\0';
416 static char *
417 get_ext(char *str)
419 char *ext;
420 char dot;
421 size_t counter, len, i;
423 dot = '.';
424 counter = 0;
425 len = strnlen(str, MAX_N);
427 for (i = len - 1; i > 0; i--) {
428 if (str[i] == dot) {
429 break;
430 } else {
431 counter++;
435 ext = ecalloc(MAX_EXT + 1, sizeof(char));
436 strncpy(ext, &str[len - counter], MAX_EXT);
437 ext[MAX_EXT] = '\0';
438 return ext;
441 static int
442 get_fdt(char *result, size_t reslen, time_t status)
444 struct tm lt;
445 localtime_r(&status, &lt);
446 return strftime(result, reslen, dtfmt, &lt);
449 static char *
450 get_fgrp(gid_t status, size_t len)
452 char *result;
453 struct group *gr;
455 result = ecalloc(len, sizeof(char));
456 gr = getgrgid(status);
457 if (gr == NULL)
458 (void)snprintf(result, len - 1, "%u", status);
459 else
460 strncpy(result, gr->gr_name, len - 1);
462 return result;
465 static char *
466 get_finfo(Entry *cursor)
468 char *sz, *rst, *ur, *gr, *td, *prm;
469 size_t szlen, prmlen, urlen, grlen, tdlen, rstlen;
471 szlen = 9;
472 prmlen = 11;
473 urlen = grlen = tdlen = 32;
474 rstlen = szlen + prmlen + urlen + grlen + tdlen;
475 rst = ecalloc(rstlen, sizeof(char));
477 if (show_perm == 1) {
478 prm = get_fperm(cursor->mode);
479 strncpy(rst, prm, prmlen);
480 strcat(rst, " ");
481 free(prm);
484 if (show_ug == 1) {
485 ur = get_fusr(cursor->user, urlen);
486 gr = get_fgrp(cursor->group, grlen);
487 strncat(rst, ur, urlen);
488 strcat(rst, ":");
489 strncat(rst, gr, grlen);
490 strcat(rst, " ");
491 free(ur);
492 free(gr);
495 if (show_dt == 1) {
496 td = ecalloc(tdlen, sizeof(char));
497 if (get_fdt(td, tdlen, cursor->td) > 0) {
498 strncat(rst, td, tdlen);
499 strcat(rst, " ");
501 free(td);
504 if (show_size == 1 && S_ISREG(cursor->mode)) {
505 sz = get_fsize(cursor->size);
506 strncat(rst, sz, szlen);
507 free(sz);
510 return rst;
513 static char *
514 get_fperm(mode_t mode)
516 char *buf;
517 size_t i;
519 const char chars[] = "rwxrwxrwx";
520 buf = ecalloc(11, sizeof(char));
522 if (S_ISDIR(mode))
523 buf[0] = 'd';
524 else if (S_ISREG(mode))
525 buf[0] = '-';
526 else if (S_ISLNK(mode))
527 buf[0] = 'l';
528 else if (S_ISBLK(mode))
529 buf[0] = 'b';
530 else if (S_ISCHR(mode))
531 buf[0] = 'c';
532 else if (S_ISFIFO(mode))
533 buf[0] = 'p';
534 else if (S_ISSOCK(mode))
535 buf[0] = 's';
536 else
537 buf[0] = '?';
539 for (i = 1; i < 10; i++) {
540 buf[i] = (mode & (1 << (9 - i))) ? chars[i - 1] : '-';
542 buf[10] = '\0';
544 return buf;
547 static char *
548 get_fsize(off_t size)
550 char *result; /* need to be freed */
551 char unit;
552 int result_len;
553 int counter;
555 counter = 0;
556 result_len = 6; /* 9999X/0 */
557 result = ecalloc(result_len, sizeof(char));
559 while (size >= 1000) {
560 size /= 1024;
561 ++counter;
564 switch (counter) {
565 case 0:
566 unit = 'B';
567 break;
568 case 1:
569 unit = 'K';
570 break;
571 case 2:
572 unit = 'M';
573 break;
574 case 3:
575 unit = 'G';
576 break;
577 case 4:
578 unit = 'T';
579 break;
580 default:
581 unit = '?';
584 if (snprintf(result, result_len, "%ld%c", size, unit) < 0)
585 strncat(result, "???", result_len);
587 return result;
590 static char *
591 get_fullpath(char *first, char *second)
593 char *full_path;
595 full_path = ecalloc(MAX_P, sizeof(char));
597 if (strncmp(first, "/", MAX_P) == 0)
598 (void)snprintf(full_path, MAX_P, "/%s", second);
599 else
600 (void)snprintf(full_path, MAX_P, "%s/%s", first, second);
602 return full_path;
605 static char *
606 get_fusr(uid_t status, size_t len)
608 char *result;
609 struct passwd *pw;
611 result = ecalloc(len, sizeof(char));
612 pw = getpwuid(status);
613 if (pw == NULL)
614 (void)snprintf(result, len - 1, "%u", status);
615 else
616 strncpy(result, pw->pw_name, len - 1);
618 return result;
621 static void
622 get_dirsize(char *fullpath, off_t *fullsize)
624 DIR *dir;
625 char *ent_full;
626 mode_t mode;
627 struct dirent *entry;
628 struct stat status;
630 dir = opendir(fullpath);
631 if (dir == NULL) {
632 return;
635 while ((entry = readdir(dir)) != 0) {
636 if ((strncmp(entry->d_name, ".", 2) == 0 ||
637 strncmp(entry->d_name, "..", 3) == 0))
638 continue;
640 ent_full = get_fullpath(fullpath, entry->d_name);
641 if (lstat(ent_full, &status) == 0) {
642 mode = status.st_mode;
643 if (S_ISDIR(mode)) {
644 get_dirsize(ent_full, fullsize);
645 free(ent_full);
646 } else {
647 *fullsize += status.st_size;
648 free(ent_full);
653 closedir(dir);
654 clear_status();
657 static void
658 get_hicol(Cpair *col, mode_t mode)
660 *col = cfile;
661 if (S_ISDIR(mode))
662 *col = cdir;
663 else if (S_ISLNK(mode))
664 *col = cother;
665 else if (chech_execf(mode) > 0)
666 *col = cexec;
669 static int
670 delent(char *fullpath)
672 char *inp_conf;
673 int conf_len = 4;
674 char conf[] = "yes";
676 inp_conf = ecalloc(conf_len, sizeof(char));
677 if ((get_usrinput(inp_conf, conf_len, "delete file (yes) ?") < 0) ||
678 (strncmp(inp_conf, conf, conf_len) != 0)) {
679 free(inp_conf);
680 return 1; /* canceled by user or wrong inp_conf */
682 free(inp_conf);
684 return spawn(rm_cmd, fullpath);
687 static void
688 calcdir(void)
690 if (!S_ISDIR(cpane->direntr[cpane->hdir - 1].mode))
691 return;
693 off_t *fullsize;
694 char *csize;
695 char *result;
697 fullsize = ecalloc(50, sizeof(off_t));
698 get_dirsize(CURSOR_NAME, fullsize);
699 csize = get_fsize(*fullsize);
700 result = get_finfo(&cpane->direntr[cpane->hdir - 1]);
702 clear_status();
703 print_status(cstatus, "%d/%d %s%s",
704 cpane->hdir, cpane->dirc, result, csize);
705 free(csize);
706 free(fullsize);
707 free(result);
710 static void
711 crnd(void)
713 char *user_input, *path;
715 user_input = ecalloc(MAX_USRI, sizeof(char));
716 if (get_usrinput(user_input, MAX_USRI, "new dir") < 0) {
717 free(user_input);
718 return;
721 path = ecalloc(MAX_P, sizeof(char));
722 if (snprintf(path, MAX_P, "%s/%s", cpane->dirn, user_input) < 0) {
723 free(user_input);
724 free(path);
725 return;
728 if (mkdir(path, ndir_perm) < 0)
729 print_error(strerror(errno));
731 free(user_input);
732 free(path);
735 static void
736 crnf(void)
738 char *user_input, *path;
739 int rf;
741 user_input = ecalloc(MAX_USRI, sizeof(char));
742 if (get_usrinput(user_input, MAX_USRI, "new file") < 0) {
743 free(user_input);
744 return;
747 path = ecalloc(MAX_P, sizeof(char));
748 if (snprintf(path, MAX_P, "%s/%s", cpane->dirn, user_input) < 0) {
749 free(user_input);
750 free(path);
751 return;
754 rf = open(path, O_CREAT | O_EXCL, nf_perm);
756 if (rf < 0)
757 print_error(strerror(errno));
758 else
759 if (close(rf) < 0)
760 print_error(strerror(errno));
762 free(user_input);
763 free(path);
766 static void
767 delfd(void)
769 switch (delent(CURSOR_NAME)) {
770 case -1:
771 print_error(strerror(errno));
772 break;
773 case 0:
774 if (BETWEEN(cpane->hdir - 1, 1, cpane->dirc)) /* last entry */
775 cpane->hdir--;
776 break;
780 static void
781 mvbk(void)
783 if (cpane->dirn[0] == '/' && cpane->dirn[1] == '\0') { /* cwd = / */
784 return;
787 get_dirp(cpane->dirn);
788 if (check_dir(cpane->dirn) < 0) {
789 print_error(strerror(errno));
790 return;
793 rmwatch(cpane);
794 cpane->firstrow = cpane->parent_firstrow;
795 cpane->hdir = cpane->parent_row;
796 if (listdir(AddHi) < 0)
797 print_error(strerror(errno));
798 cpane->parent_firstrow = 0;
799 cpane->parent_row = 1;
802 static void
803 mvbtm(void)
805 if (cpane->dirc < 1)
806 return;
807 if (cpane->dirc > scrheight) {
808 rm_hi(cpane, cpane->hdir - 1);
809 cpane->hdir = cpane->dirc;
810 cpane->firstrow = cpane->dirc - scrheight + 1;
811 refresh_pane();
812 add_hi(cpane, cpane->hdir - 1);
813 } else {
814 rm_hi(cpane, cpane->hdir - 1);
815 cpane->hdir = cpane->dirc;
816 add_hi(cpane, cpane->hdir - 1);
818 print_info();
821 static void
822 mvdwn(void)
824 if (cpane->dirc < 1)
825 return;
826 if (cpane->dirc < scrheight && cpane->hdir < cpane->dirc) {
827 rm_hi(cpane, cpane->hdir - 1);
828 cpane->hdir++;
829 add_hi(cpane, cpane->hdir - 1);
830 } else {
831 mvdwns(); /* scroll */
833 print_info();
836 static void
837 mvdwns(void)
839 int real;
840 real = cpane->hdir - 1 - cpane->firstrow;
842 if (real > scrheight - 3 - scrsp && cpane->hdir + scrsp < cpane->dirc) {
843 cpane->firstrow++;
844 rm_hi(cpane, cpane->hdir - 1);
845 cpane->hdir++;
846 refresh_pane();
847 add_hi(cpane, cpane->hdir - 1);
848 } else if (cpane->hdir < cpane->dirc) {
849 rm_hi(cpane, cpane->hdir - 1);
850 cpane->hdir++;
851 add_hi(cpane, cpane->hdir - 1);
855 static void
856 mvfor(void)
858 rmwatch(cpane);
859 if (cpane->dirc < 1)
860 return;
861 int s;
863 switch (check_dir(CURSOR_NAME)) {
864 case 0:
865 strncpy(cpane->dirn, CURSOR_NAME, MAX_P);
866 cpane->parent_row = cpane->hdir;
867 cpane->parent_firstrow = cpane->firstrow;
868 cpane->hdir = 1;
869 cpane->firstrow = 0;
870 if (listdir(AddHi) < 0)
871 print_error(strerror(errno));
872 break;
873 case 1: /* not a directory open file */
874 tb_shutdown();
875 s = opnf(CURSOR_NAME);
876 if (tb_init() != 0)
877 die("tb_init");
878 t_resize();
879 if (s < 0)
880 print_error("process failed non-zero exit");
881 break;
882 case -1: /* failed to open directory */
883 print_error(strerror(errno));
887 static void
888 mvmid(void)
890 if (cpane->dirc < 1)
891 return;
892 rm_hi(cpane, cpane->hdir - 1);
893 if (cpane->dirc < scrheight / 2)
894 cpane->hdir = (cpane->dirc + 1) / 2;
895 else
896 cpane->hdir = (scrheight / 2) + cpane->firstrow;
897 add_hi(cpane, cpane->hdir - 1);
898 print_info();
901 static void
902 mvtop(void)
904 if (cpane->dirc < 1)
905 return;
906 if (cpane->dirc > scrheight) {
907 rm_hi(cpane, cpane->hdir - 1);
908 cpane->hdir = 1;
909 cpane->firstrow = 0;
910 refresh_pane();
911 add_hi(cpane, cpane->hdir - 1);
912 } else {
913 rm_hi(cpane, cpane->hdir - 1);
914 cpane->hdir = 1;
915 add_hi(cpane, cpane->hdir - 1);
916 print_info();
920 static void
921 mvup(void)
923 if (cpane->dirc < 1)
924 return;
925 if (cpane->dirc < scrheight && cpane->hdir > 1) {
926 rm_hi(cpane, cpane->hdir - 1);
927 cpane->hdir--;
928 add_hi(cpane, cpane->hdir - 1);
929 } else {
930 mvups(); /* scroll */
932 print_info();
935 static void
936 mvups(void)
938 size_t real;
939 real = cpane->hdir - 1 - cpane->firstrow;
941 if (cpane->firstrow > 0 && real < 1 + scrsp) {
942 cpane->firstrow--;
943 rm_hi(cpane, cpane->hdir - 1);
944 cpane->hdir--;
945 refresh_pane();
946 add_hi(cpane, cpane->hdir - 1);
947 } else if (cpane->hdir > 1) {
948 rm_hi(cpane, cpane->hdir - 1);
949 cpane->hdir--;
950 add_hi(cpane, cpane->hdir - 1);
954 static void
955 scrdwn(void)
957 if (cpane->dirc < 1)
958 return;
959 if (cpane->dirc < scrheight && cpane->hdir < cpane->dirc) {
960 if (cpane->hdir < cpane->dirc - scrmv) {
961 rm_hi(cpane, cpane->hdir - 1);
962 cpane->hdir += scrmv;
963 add_hi(cpane, cpane->hdir - 1);
964 } else {
965 mvbtm();
967 } else {
968 scrdwns();
970 print_info();
973 static void
974 scrdwns(void)
976 int real, dynmv;
978 real = cpane->hdir - cpane->firstrow;
979 dynmv = MIN(cpane->dirc - cpane->hdir - cpane->firstrow, scrmv);
981 if (real + scrmv + 1 > scrheight &&
982 cpane->hdir + scrsp + scrmv < cpane->dirc) { /* scroll */
983 cpane->firstrow += dynmv;
984 rm_hi(cpane, cpane->hdir - 1);
985 cpane->hdir += scrmv;
986 refresh_pane();
987 add_hi(cpane, cpane->hdir - 1);
988 } else {
989 if (cpane->hdir < cpane->dirc - scrmv - 1) {
990 rm_hi(cpane, cpane->hdir - 1);
991 cpane->hdir += scrmv;
992 add_hi(cpane, cpane->hdir - 1);
993 } else {
994 mvbtm();
999 static void
1000 scrup(void)
1002 if (cpane->dirc < 1)
1003 return;
1004 if (cpane->dirc < scrheight && cpane->hdir > 1) {
1005 if (cpane->hdir > scrmv) {
1006 rm_hi(cpane, cpane->hdir - 1);
1007 cpane->hdir = cpane->hdir - scrmv;
1008 add_hi(cpane, cpane->hdir - 1);
1009 print_info();
1010 } else {
1011 mvtop();
1013 } else {
1014 scrups();
1018 static void
1019 scrups(void)
1021 int real, dynmv;
1022 real = cpane->hdir - cpane->firstrow;
1023 dynmv = MIN(cpane->firstrow, scrmv);
1025 if (cpane->firstrow > 0 && real < scrmv + scrsp) {
1026 cpane->firstrow -= dynmv;
1027 rm_hi(cpane, cpane->hdir - 1);
1028 cpane->hdir -= scrmv;
1029 refresh_pane();
1030 add_hi(cpane, cpane->hdir - 1);
1031 } else {
1032 if (cpane->hdir > scrmv + 1) {
1033 rm_hi(cpane, cpane->hdir - 1);
1034 cpane->hdir -= scrmv;
1035 add_hi(cpane, cpane->hdir - 1);
1036 } else {
1037 mvtop();
1042 static int
1043 get_usrinput(char *out, size_t sout, const char *fmt, ...)
1045 int height = tb_height();
1046 size_t startat;
1047 struct tb_event fev;
1048 size_t counter = (size_t)1;
1049 char empty = ' ';
1050 int x = 0;
1051 int name_size = 0;
1052 char buf[256];
1054 clear_status();
1056 va_list vl;
1057 Cpair col;
1058 col = cprompt;
1059 va_start(vl, fmt);
1060 name_size = vsnprintf(buf, sizeof(buf), fmt, vl);
1061 va_end(vl);
1062 print_tb(buf, 1, height-1, col.fg, col.bg);
1063 startat = name_size + 1;
1064 tb_set_cursor((int)(startat + 1), height-1);
1065 tb_present();
1067 while (tb_poll_event(&fev) != 0) {
1068 switch (fev.type) {
1069 case TB_EVENT_KEY:
1070 if (fev.key == TB_KEY_ESC) {
1071 tb_set_cursor(-1, -1);
1072 clear_status();
1073 return -1;
1076 if (fev.key == TB_KEY_BACKSPACE ||
1077 fev.key == TB_KEY_BACKSPACE2) {
1078 if (BETWEEN(counter, 2, sout)) {
1079 out[x - 1] = '\0';
1080 counter--;
1081 x--;
1082 print_xstatus(empty, startat + counter);
1083 tb_set_cursor(startat + counter,
1084 theight - 1);
1087 } else if (fev.key == TB_KEY_ENTER) {
1088 tb_set_cursor(-1, -1);
1089 out[counter - 1] = '\0';
1090 return 0;
1092 } else {
1093 if (counter < sout) {
1094 print_xstatus((char)fev.ch,
1095 (startat + counter));
1096 out[x] = (char)fev.ch;
1097 tb_set_cursor((startat + counter + 1),
1098 theight - 1);
1099 counter++;
1100 x++;
1104 tb_present();
1105 break;
1107 default:
1108 return -1;
1112 return -1;
1115 static int
1116 frules(char *ex)
1118 size_t c, d;
1120 for (c = 0; c < LEN(rules); c++)
1121 for (d = 0; d < rules[c].exlen; d++)
1122 if (strncmp(rules[c].ext[d], ex, MAX_EXT) == 0)
1123 return c;
1124 return -1;
1127 static int
1128 spawn(const void *v, char *fn)
1130 int ws, x, argc, fd;
1131 pid_t pid, r;
1133 x = 0;
1134 argc = 0;
1136 /* count args */
1137 while (((char **)v)[x++] != NULL)
1138 argc++;
1140 char *argv[argc + 2];
1141 for ( x = 0; x < argc; x++)
1142 argv[x] = ((char **)v)[x];
1144 argv[argc] = fn;
1145 argv[argc + 1] = NULL;
1147 pid = fork();
1148 switch (pid) {
1149 case -1:
1150 return -1;
1151 case 0:
1152 fd = open("/dev/null",O_WRONLY);
1153 dup2(fd, STDERR_FILENO);
1154 execvp(argv[0], argv);
1155 exit(EXIT_SUCCESS);
1156 close(fd);
1157 default:
1158 while ((r = waitpid(pid, &ws, 0)) == -1 && errno == EINTR)
1159 continue;
1160 if (r == -1)
1161 return -1;
1162 if ((WIFEXITED(ws) != 0) && (WEXITSTATUS(ws) != 0))
1163 return -1;
1165 return 0;
1168 static int
1169 opnf(char *fn)
1171 char *ex;
1172 int c;
1174 ex = get_ext(fn);
1175 c = frules(ex);
1176 free(ex);
1178 if (c < 0) /* extension not found open in editor */
1179 return spawn(editor, fn);
1180 else
1181 return spawn((char **)rules[c].v, fn);
1184 static int
1185 fsev_init(void)
1187 #if defined _SYS_INOTIFY_H
1188 inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
1189 if (inotify_fd < 0)
1190 return -1;
1191 #elif defined _SYS_EVENT_H_
1192 kq = kqueue();
1193 if (kq < 0)
1194 return -1;
1195 #endif
1196 return 0;
1199 static int
1200 addwatch(void)
1202 #if defined _SYS_INOTIFY_H
1203 return cpane->inotify_wd = inotify_add_watch(inotify_fd, cpane->dirn,
1204 IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE |
1205 IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF);
1206 #elif defined _SYS_EVENT_H_
1207 cpane->event_fd = open(cpane->dirn, O_RDONLY);
1208 if (cpane->event_fd < 0)
1209 return cpane->event_fd;
1210 EV_SET(&evlist[cpane->pane_id], cpane->event_fd,
1211 EVFILT_VNODE, EV_ADD | EV_CLEAR,
1212 NOTE_DELETE | NOTE_EXTEND | NOTE_LINK |
1213 NOTE_RENAME | NOTE_REVOKE | NOTE_WRITE, 0, NULL);
1214 return 0;
1215 #endif
1218 static int
1219 read_events(void)
1221 #if defined _SYS_INOTIFY_H
1222 char *p;
1223 ssize_t r;
1224 struct inotify_event *event;
1225 const size_t events = 32;
1226 const size_t evbuflen =
1227 events * (sizeof(struct inotify_event) + MAX_N + 1);
1228 char buf[evbuflen];
1230 if (cpane->inotify_wd < 0)
1231 return -1;
1232 r = read(inotify_fd, buf, evbuflen);
1233 if (r <= 0)
1234 return r;
1236 for (p = buf; p < buf + r;) {
1237 event = (struct inotify_event *)p;
1238 if (!event->wd)
1239 break;
1240 if (event->mask) {
1241 return r;
1244 p += sizeof(struct inotify_event) + event->len;
1246 #elif defined _SYS_EVENT_H_
1247 return kevent(kq, evlist, 2, chlist, 2, &gtimeout);
1248 #endif
1249 return -1;
1252 static void
1253 rmwatch(Pane *pane)
1255 #if defined _SYS_INOTIFY_H
1256 if (pane->inotify_wd >= 0)
1257 inotify_rm_watch(inotify_fd, pane->inotify_wd);
1258 #elif defined _SYS_EVENT_H_
1259 close(pane->event_fd);
1260 return;
1261 #endif
1264 static void
1265 fsev_shdn(void)
1267 rmwatch(&pane_l);
1268 rmwatch(&pane_r);
1269 #if defined _SYS_INOTIFY_H
1270 close(inotify_fd);
1271 #elif defined _SYS_EVENT_H_
1272 close(kq);
1273 #endif
1276 static ssize_t
1277 findbm(uint32_t event)
1279 ssize_t i;
1281 for (i = 0; i < (ssize_t)LEN(bmarks); i++) {
1282 if (event == bmarks[i].ch) {
1283 if (check_dir(bmarks[i].path) != 0) {
1284 print_error(strerror(errno));
1285 return -1;
1287 return i;
1290 return -1;
1293 static void
1294 start_filter(void)
1296 if (cpane->dirc < 1)
1297 return;
1298 char *user_input;
1299 user_input = ecalloc(MAX_USRI, sizeof(char));
1300 if (get_usrinput(user_input, MAX_USRI, "filter") < 0) {
1301 free(user_input);
1302 return;
1304 cpane->filter = user_input;
1305 if (listdir(AddHi) < 0)
1306 print_error("no match");
1307 cpane->filter = NULL;
1308 free(user_input);
1311 static void
1312 start_vmode(void)
1314 struct tb_event fev;
1315 if (selection != NULL) {
1316 free(selection);
1317 selection = NULL;
1320 selection = ecalloc(cpane->dirc, sizeof(size_t));
1321 selection[0] = cpane->hdir;
1322 cont_vmode = 0;
1323 print_prompt("-- VISUAL --");
1324 tb_present();
1325 while (tb_poll_event(&fev) != 0) {
1326 switch (fev.type) {
1327 case TB_EVENT_KEY:
1328 grabkeys(&fev, skeys, skeyslen);
1329 if (cont_vmode == -1)
1330 return;
1331 tb_present();
1332 break;
1337 static void
1338 exit_vmode(void)
1340 refresh_pane();
1341 add_hi(cpane, cpane->hdir - 1);
1342 cont_vmode = -1;
1345 static void
1346 selup(void)
1348 mvup();
1349 print_prompt("-- VISUAL --");
1350 int index = abs(cpane->hdir - selection[0]);
1352 if (cpane->hdir < selection[0]) {
1353 selection[index] = cpane->hdir;
1354 add_hi(cpane, selection[index]);
1355 } else if (index < cpane->dirc) {
1356 selection[index + 1] = 0;
1358 if (cpane->dirc >= scrheight || cpane->hdir <= 1) { /* rehighlight all if scrolling */
1359 selref();
1363 static void
1364 seldwn(void)
1366 mvdwn();
1367 print_prompt("-- VISUAL --");
1368 int index = abs(cpane->hdir - selection[0]);
1370 if (cpane->hdir > selection[0]) {
1371 selection[index] = cpane->hdir;
1372 add_hi(cpane, selection[index] - 2);
1373 } else {
1374 selection[index + 1] = 0;
1376 if (cpane->dirc >= scrheight || cpane->hdir >= cpane->dirc) { /* rehighlight all if scrolling */
1377 selref();
1381 static void
1382 selall(void)
1384 int i;
1385 for (i = 0; i < cpane->dirc; i++) {
1386 selection[i] = i + 1;
1388 selref();
1391 static void
1392 selref(void)
1394 int i;
1395 for (i = 0; i < cpane->dirc; i++) {
1396 if (selection[i] < (scrheight + cpane->firstrow) && selection[i] > cpane->firstrow) { /* checks if in the frame of the directories */
1397 add_hi(cpane, selection[i] - 1);
1402 static void
1403 selcalc(void)
1405 int j;
1406 selection_size = 0;
1408 for (j = 0; j < cpane->dirc; j++) { /* calculate used selection size */
1409 if (selection[j] != 0)
1410 selection_size++;
1411 else
1412 break;
1416 static void
1417 free_files(void)
1419 size_t i;
1421 if (selected_files != NULL) {
1422 for (i = 0; i < selection_size; i++) {
1423 free(selected_files[i]);
1424 selected_files[i] = NULL;
1426 free(selected_files);
1427 selected_files = NULL;
1431 static void
1432 init_files(void)
1434 size_t i;
1435 free_files();
1437 selcalc();
1438 selected_files = ecalloc(selection_size, sizeof(char*));
1440 for (i = 0; i < selection_size; i++) {
1441 selected_files[i] = ecalloc(MAX_P, sizeof(char));
1442 strncpy(selected_files[i], cpane->direntr[selection[i] -1].name, MAX_P); /* TODO use pointer */
1446 static void
1447 selynk(void)
1449 init_files();
1450 refresh_pane();
1451 add_hi(cpane, cpane->hdir -1);
1452 print_status(cprompt, "%zu files are yanked", selection_size);
1453 cont_vmode = -1;
1456 static void
1457 seldel(void)
1459 char *inp_conf;
1460 int conf_len = 4;
1461 char conf[] = "yes";
1462 size_t i;
1464 inp_conf = ecalloc(conf_len, sizeof(char));
1465 if ((get_usrinput(inp_conf, conf_len, "delete file (yes) ?") < 0) ||
1466 (strncmp(inp_conf, conf, conf_len) != 0)) {
1467 free(inp_conf);
1468 return; /* canceled by user or wrong inp_conf */
1470 free(inp_conf);
1472 init_files();
1473 for (i = 0; i < selection_size; i++) {
1474 spawn(rm_cmd, selected_files[i]);
1477 cpane->hdir = cpane->dirc - selection_size;
1478 print_status(cprompt, "%zu files are deleted", selection_size);
1479 free_files();
1480 cont_vmode = -1;
1483 static void
1484 paste(void)
1486 size_t i;
1487 if (strnlen(yank_file, MAX_P) != 0) {
1488 print_status(cprompt, "coping");
1489 if (spawn(cp_cmd, cpane->dirn) != 0)
1490 print_error("coping failed");
1491 else
1492 print_status(cprompt, "file copied");
1493 yank_file[0] = '\0'; /* set yank_file len 0 */
1494 return;
1497 print_error("nothing to paste");
1499 if (selected_files == NULL)
1500 return;
1502 for (i = 0; i < selection_size; i++) {
1503 char *selcp_cmd[] = { "cp", "-r", selected_files[i], cpane->dirn, NULL };
1504 spawn(selcp_cmd,NULL);
1506 print_status(cprompt, "%zu files are copied", selection_size);
1507 free_files();
1510 static void
1511 selmv(void)
1513 size_t i;
1515 if (strnlen(yank_file, MAX_P) != 0) {
1516 print_status(cprompt, "moving");
1517 if (spawn(mv_cmd, cpane->dirn) != 0)
1518 print_error("moving failed");
1519 else
1520 print_status(cprompt, "file moved");
1521 yank_file[0] = '\0'; /* set yank_file len 0 */
1522 return;
1525 print_error("nothing to move");
1527 if (selected_files == NULL)
1528 return;
1530 for (i = 0; i < selection_size; i++) {
1531 char *selmv_cmd[] = { "mv", selected_files[i], cpane->dirn, NULL };
1532 spawn(selmv_cmd,NULL);
1534 print_status(cprompt, "%zu files are moved", selection_size);
1535 free_files();
1538 static void
1539 rname(void)
1541 char new_name[MAX_P];
1542 char *input_name;
1544 input_name = ecalloc(MAX_N, sizeof(char));
1546 if (get_usrinput(input_name, MAX_N, "rename: %s", basename(CURSOR_NAME)) < 0) {
1547 free(input_name);
1548 return;
1551 if (snprintf(new_name, MAX_P, "%s/%s", cpane->dirn, input_name) < 0) {
1552 free(input_name);
1553 print_error(strerror(errno));
1554 return;
1557 char *rename_cmd[] = { "mv", CURSOR_NAME, new_name, NULL };
1558 if (spawn(rename_cmd, NULL) < 0)
1559 print_error(strerror(errno));
1561 free(input_name);
1564 static void
1565 yank(void)
1567 strncpy(yank_file, CURSOR_NAME, MAX_P);
1568 print_status(cprompt, "1 file is yanked", selection_size);
1572 static void
1573 switch_pane(void)
1575 if (cpane->dirc > 0)
1576 rm_hi(cpane, cpane->hdir - 1);
1577 if (cpane == &pane_l)
1578 cpane = &pane_r;
1579 else if (cpane == &pane_r)
1580 cpane = &pane_l;
1581 if (cpane->dirc > 0) {
1582 add_hi(cpane, cpane->hdir - 1);
1583 print_info();
1584 } else {
1585 clear_status();
1589 static void
1590 quit(void)
1592 if (cont_vmode == -1) { /* check if selection was allocated */
1593 free(selection);
1594 if (selected_files != NULL)
1595 free_files();
1597 free(pane_l.direntr);
1598 free(pane_r.direntr);
1599 fsev_shdn();
1600 tb_shutdown();
1601 exit(EXIT_SUCCESS);
1604 static void
1605 grabkeys(struct tb_event *event, Key *key, size_t max_keys)
1607 size_t i;
1608 ssize_t b;
1610 for (i = 0; i < max_keys; i++) {
1611 if (event->ch != 0) {
1612 if (event->ch == key[i].evkey.ch) {
1613 key[i].func();
1614 return;
1616 } else if (event->key != 0) {
1617 if (event->key == key[i].evkey.key) {
1618 key[i].func();
1619 return;
1624 /* bookmarks */
1625 b = findbm(event->ch);
1626 if (b < 0)
1627 return;
1628 rmwatch(cpane);
1629 strncpy(cpane->dirn, bmarks[b].path, MAX_P);
1630 cpane->firstrow = 0;
1631 cpane->parent_row = 1;
1632 cpane->hdir = 1;
1633 if (listdir(AddHi) < 0)
1634 print_error(strerror(errno));
1637 static void
1638 start_ev(void)
1640 struct tb_event ev;
1642 for (;;) {
1643 int t = tb_peek_event(&ev, 2000);
1644 if (t < 0) {
1645 tb_shutdown();
1646 return;
1649 if (t == 1) /* keyboard event */
1650 grabkeys(&ev, nkeys, nkeyslen);
1651 else if (t == 2) /* resize event */
1652 t_resize();
1653 else if (t == 0) /* filesystem event */
1654 if (read_events() > 0) { /* TODO need refactoring */
1655 if (cpane == &pane_l) {
1656 cpane = &pane_r;
1657 if (listdir(NoHi) < 0)
1658 print_error(strerror(errno));
1659 cpane = &pane_l;
1660 if (listdir(AddHi) < 0)
1661 print_error(strerror(errno));
1662 } else if (cpane == &pane_r) {
1663 cpane = &pane_l;
1664 if (listdir(NoHi) < 0)
1665 print_error(strerror(errno));
1666 cpane = &pane_r;
1667 if (listdir(AddHi) < 0)
1668 print_error(strerror(errno));
1672 tb_present();
1673 continue;
1675 tb_shutdown();
1678 static void
1679 refresh_pane(void)
1681 size_t y, dyn_max, start_from;
1682 int width;
1683 width = (twidth / 2) - 4;
1684 Cpair col;
1686 y = 1;
1687 start_from = cpane->firstrow;
1688 dyn_max = MIN(cpane->dirc, (scrheight - 1) + cpane->firstrow);
1690 /* print each entry in directory */
1691 while (start_from < dyn_max) {
1692 get_hicol(&col, cpane->direntr[start_from].mode);
1693 print_row(cpane, start_from, col);
1694 start_from++;
1695 y++;
1698 if (cpane->dirc > 0)
1699 print_info();
1700 else
1701 clear_status();
1703 /* print current directory title */
1704 cpane->dircol.fg |= TB_BOLD;
1705 printf_tb(cpane->dirx, 0, cpane->dircol, " %.*s ", width, cpane->dirn);
1708 static void
1709 set_direntr(struct dirent *entry, DIR *dir, char *filter)
1711 int i;
1712 char *tmpfull;
1713 struct stat status;
1716 #define ADD_ENTRY \
1717 tmpfull = get_fullpath(cpane->dirn, entry->d_name); \
1718 strncpy(cpane->direntr[i].name, tmpfull, MAX_N); \
1719 if (lstat(tmpfull, &status) == 0) { \
1720 cpane->direntr[i].size = status.st_size; \
1721 cpane->direntr[i].mode = status.st_mode; \
1722 cpane->direntr[i].group = status.st_gid; \
1723 cpane->direntr[i].user = status.st_uid; \
1724 cpane->direntr[i].td = status.st_mtime; \
1725 }i++;free(tmpfull);
1727 i = 0;
1728 cpane->direntr = erealloc(cpane->direntr, cpane->dirc * sizeof(Entry));
1729 while ((entry = readdir(dir)) != 0) {
1730 if ((strncmp(entry->d_name, ".", 2) == 0 ||
1731 strncmp(entry->d_name, "..", 3) == 0))
1732 continue;
1734 if (filter == NULL) {
1735 ADD_ENTRY
1736 } else if (filter != NULL) {
1737 if (strcasestr(entry->d_name, filter) != NULL) {
1738 ADD_ENTRY
1744 cpane->dirc = i;
1747 static int
1748 listdir(int hi)
1750 DIR *dir;
1751 struct dirent *entry;
1752 int width;
1753 int filtercount = 0;
1754 size_t oldc = cpane->dirc;
1756 width = (twidth / 2) - 4;
1757 cpane->dirc = 0;
1759 dir = opendir(cpane->dirn);
1760 if (dir == NULL)
1761 return -1;
1763 /* get content and filter sum */
1764 while ((entry = readdir(dir)) != 0) {
1765 if (cpane->filter != NULL) {
1766 if (strcasestr(entry->d_name, cpane->filter) != NULL)
1767 filtercount++;
1768 } else { /* no filter */
1769 cpane->dirc++;
1773 if (cpane->filter == NULL) {
1774 clear_pane();
1775 cpane->dirc -= 2;
1778 if (cpane->filter != NULL) {
1779 if (filtercount > 0) {
1780 cpane->dirc = filtercount;
1781 clear_pane();
1782 cpane->hdir = 1;
1783 } else if (filtercount == 0) {
1784 if (closedir(dir) < 0)
1785 return -1;
1786 cpane->dirc = oldc;
1787 return -1;
1791 /* print current directory title */
1792 cpane->dircol.fg |= TB_BOLD;
1793 printf_tb(cpane->dirx, 0, cpane->dircol, " %.*s ", width, cpane->dirn);
1795 if (cpane->filter == NULL) /* dont't watch when filtering */
1796 if (addwatch() < 0)
1797 print_error("can't add watch");
1799 /* empty directory */
1800 if (cpane->dirc == 0) {
1801 clear_status();
1802 if (closedir(dir) < 0)
1803 return -1;
1804 return 0;
1807 rewinddir(dir); /* reset position */
1808 set_direntr(entry, dir, cpane->filter); /* create array of entries */
1809 qsort(cpane->direntr, cpane->dirc, sizeof(Entry), sort_name);
1810 refresh_pane();
1812 if (hi == AddHi)
1813 add_hi(cpane, cpane->hdir - 1);
1815 if (closedir(dir) < 0)
1816 return -1;
1817 return 0;
1820 static void
1821 t_resize(void)
1823 /* TODO need refactoring */
1824 tb_clear();
1825 draw_frame();
1826 pane_r.dirx = (twidth / 2) + 2;
1828 if (cpane == &pane_l) {
1829 cpane = &pane_r;
1830 refresh_pane();
1831 cpane = &pane_l;
1832 refresh_pane();
1833 if (cpane->dirc > 0)
1834 add_hi(&pane_l, pane_l.hdir - 1);
1835 } else if (cpane == &pane_r) {
1836 cpane = &pane_l;
1837 refresh_pane();
1838 cpane = &pane_r;
1839 refresh_pane();
1840 if (cpane->dirc > 0)
1841 add_hi(&pane_r, pane_r.hdir - 1);
1844 tb_present();
1847 static void
1848 get_editor(void)
1850 editor[0] = getenv("EDITOR");
1851 editor[1] = NULL;
1853 if (editor[0] == NULL)
1854 editor[0] = fed;
1857 static void
1858 set_panes(void)
1860 char *home;
1861 char cwd[MAX_P];
1863 home = getenv("HOME");
1864 if (home == NULL)
1865 home = "/";
1866 if ((getcwd(cwd, sizeof(cwd)) == NULL))
1867 strncpy(cwd, home, MAX_P);
1869 pane_l.pane_id = 0;
1870 pane_l.dirx = 2;
1871 pane_l.dircol = cpanell;
1872 pane_l.firstrow = 0;
1873 pane_l.direntr = ecalloc(0, sizeof(Entry));
1874 strncpy(pane_l.dirn, cwd, MAX_P);
1875 pane_l.hdir = 1;
1876 pane_l.inotify_wd = -1;
1877 pane_l.parent_row = 1;
1879 pane_r.pane_id = 1;
1880 pane_r.dirx = (twidth / 2) + 2;
1881 pane_r.dircol = cpanelr;
1882 pane_r.firstrow = 0;
1883 pane_r.direntr = ecalloc(0, sizeof(Entry));
1884 strncpy(pane_r.dirn, home, MAX_P);
1885 pane_r.hdir = 1;
1886 pane_r.inotify_wd = -1;
1887 pane_r.parent_row = 1;
1890 static void
1891 draw_frame(void)
1893 int i;
1894 theight = tb_height();
1895 twidth = tb_width();
1896 scrheight = theight - 2;
1898 /* 2 horizontal lines */
1899 for (i = 1; i < twidth - 1; ++i) {
1900 tb_change_cell(i, 0, u_hl, cframe.fg, cframe.bg);
1901 tb_change_cell(i, theight - 2, u_hl, cframe.fg, cframe.bg);
1904 /* 4 vertical lines */
1905 for (i = 1; i < theight - 1; ++i) {
1906 tb_change_cell(0, i, u_vl, cframe.fg, cframe.bg);
1907 tb_change_cell((twidth - 1) / 2, i - 1, u_vl, cframe.fg,
1908 cframe.bg);
1909 tb_change_cell(((twidth - 1) / 2) + 1, i - 1, u_vl, cframe.fg,
1910 cframe.bg);
1911 tb_change_cell(twidth - 1, i, u_vl, cframe.fg, cframe.bg);
1914 /* 4 corners */
1915 tb_change_cell(0, 0, u_cnw, cframe.fg, cframe.bg);
1916 tb_change_cell(twidth - 1, 0, u_cne, cframe.fg, cframe.bg);
1917 tb_change_cell(0, theight - 2, u_csw, cframe.fg, cframe.bg);
1918 tb_change_cell(twidth - 1, theight - 2, u_cse, cframe.fg, cframe.bg);
1920 /* 2 middel top and bottom */
1921 tb_change_cell((twidth - 1) / 2, 0, u_mn, cframe.fg, cframe.bg);
1922 tb_change_cell((twidth - 1) / 2, theight - 2, u_ms, cframe.fg, cframe.bg);
1925 static void
1926 start(void)
1928 if (tb_init() != 0)
1929 die("tb_init");
1930 if (tb_select_output_mode(TB_OUTPUT_256) != TB_OUTPUT_256)
1931 if (tb_select_output_mode(TB_OUTPUT_NORMAL) != TB_OUTPUT_NORMAL)
1932 die("output error");
1934 draw_frame();
1935 set_panes();
1936 get_editor();
1937 if (fsev_init() < 0)
1938 print_error(strerror(errno));
1939 cpane = &pane_r;
1940 if (listdir(NoHi) < 0)
1941 print_error(strerror(errno));
1942 cpane = &pane_l;
1943 if (listdir(AddHi) < 0)
1944 print_error(strerror(errno));
1945 tb_present();
1946 start_ev();
1950 main(int argc, char *argv[])
1952 #ifdef __OpenBSD__
1953 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1954 NULL) == -1)
1955 die("pledge");
1956 #endif /* __OpenBSD__ */
1957 if (argc == 1)
1958 start();
1959 else if (argc == 2 && strncmp("-v", argv[1], 2) == 0)
1960 die("sfm-" VERSION);
1961 else
1962 die("usage: sfm [-v]");
1963 return 0;