[rm] mvmid()
[sfm.git] / sfm.c
blobf01621fba0ab66a880f0fceb971c30a3cc522fbb
1 /* See LICENSE file for copyright and license details. */
3 #if defined(__linux__)
4 #define _GNU_SOURCE
5 #elif defined(__APPLE__)
6 #define _DARWIN_C_SOURCE
7 #elif defined(__FreeBSD__)
8 #define __BSD_VISIBLE 1
9 #endif
10 #include <sys/types.h>
11 #include <sys/resource.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15 #if defined(__linux__)
16 #include <sys/inotify.h>
17 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
18 defined(__APPLE__)
19 #include <sys/event.h>
20 #endif
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <grp.h>
26 #include <libgen.h>
27 #include <pthread.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
38 #include "termbox.h"
39 #include "util.h"
41 /* macros */
42 #define MAX_P 4096
43 #define MAX_N 255
44 #define MAX_USRI 32
45 #define MAX_EXT 4
46 #define MAX_STATUS 255
47 #define MAX_LINE 4096
48 #define MAX_USRN 32
49 #define MAX_GRPN 32
50 #define MAX_DTF 32
51 #define CURSOR(x) (x)->direntr[(x)->hdir - 1]
53 /* typedef */
54 typedef struct {
55 char name[MAX_N];
56 gid_t group;
57 mode_t mode;
58 off_t size;
59 time_t dt;
60 uid_t user;
61 } Entry;
63 typedef struct {
64 uint16_t fg;
65 uint16_t bg;
66 } Cpair;
68 typedef struct {
69 int pane_id;
70 char dirn[MAX_P]; // dir name cwd
71 char *filter;
72 Entry *direntr; // dir entries
73 int dirx; // pane cwd x pos
74 int dirc; // dir entries sum
75 int hdir; // highlighted dir
76 int ex;
77 int firstrow;
78 int parent_firstrow;
79 int parent_row; // FIX
80 Cpair dircol;
81 int inotify_wd;
82 int event_fd;
83 } Pane;
85 typedef struct {
86 const char **ext;
87 size_t exlen;
88 const void *v;
89 size_t vlen;
90 } Rule;
92 typedef union {
93 uint16_t key; /* one of the TB_KEY_* constants */
94 uint32_t ch; /* unicode character */
95 } Evkey;
97 typedef union {
98 int i;
99 const void *v;
100 } Arg;
102 typedef struct {
103 const Evkey evkey;
104 void (*func)(const Arg *);
105 const Arg arg;
106 } Key;
108 /* function declarations */
109 static void print_tb(const char *, int, int, uint16_t, uint16_t);
110 static void printf_tb(int, int, Cpair, const char *, ...);
111 static void print_status(Cpair, const char *, ...);
112 static void print_xstatus(char, int);
113 static void print_error(char *);
114 static void print_prompt(char *);
115 static void print_info(Pane *, char *);
116 static void print_row(Pane *, size_t, Cpair);
117 static void clear(int, int, int, uint16_t);
118 static void clear_status(void);
119 static void clear_pane(Pane *);
120 static void add_hi(Pane *, size_t);
121 static void rm_hi(Pane *, size_t);
122 static int check_dir(char *);
123 static mode_t chech_execf(mode_t);
124 static int sort_name(const void *const, const void *const);
125 static void get_dirp(char *);
126 static char *get_ext(char *);
127 static int get_fdt(char *, time_t);
128 static char *get_fgrp(gid_t);
129 static char *get_fperm(mode_t);
130 static char *get_fsize(off_t);
131 static char *get_fullpath(char *, char *);
132 static char *get_fusr(uid_t);
133 static void get_dirsize(char *, off_t *);
134 static void get_hicol(Cpair *, mode_t);
135 static void delent(const Arg *arg);
136 static void calcdir(const Arg *arg);
137 static void crnd(const Arg *arg);
138 static void crnf(const Arg *arg);
139 static void mv_ver(const Arg *arg);
140 static void mvbk(const Arg *arg);
141 static void mvbtm(const Arg *arg);
142 static void mvfwd(const Arg *arg);
143 static void mvtop(const Arg *arg);
144 static void bkmrk(const Arg *arg);
145 static int get_usrinput(char *, size_t, const char *, ...);
146 static int frules(char *);
147 static int spawn(const void *, size_t, const void *, size_t, char *);
148 static int opnf(char *);
149 static int fsev_init(void);
150 static int addwatch(Pane *);
151 static int read_events(void);
152 static void rmwatch(Pane *);
153 static void fsev_shdn(void);
154 static void toggle_df(const Arg *arg);
155 static void start_filter(const Arg *arg);
156 static void start_vmode(const Arg *arg);
157 static void exit_vmode(const Arg *arg);
158 static void selup(const Arg *arg);
159 static void seldwn(const Arg *arg);
160 static void selall(const Arg *arg);
161 static void selref(void);
162 static void selynk(const Arg *arg);
163 static void selcalc(void);
164 static void paste(const Arg *arg);
165 static void selmv(const Arg *arg);
166 static void seldel(const Arg *arg);
167 static void init_files(void);
168 static void free_files(void);
169 static void yank(const Arg *arg);
170 static void rname(const Arg *arg);
171 static void switch_pane(const Arg *arg);
172 static void quit(const Arg *arg);
173 static void grabkeys(struct tb_event *, Key *, size_t);
174 static void *read_th(void *arg);
175 static void start_ev(void);
176 static void refresh_pane(Pane *);
177 static void set_direntr(Pane *, struct dirent *, DIR *, char *);
178 static int listdir(Pane *);
179 static void t_resize(void);
180 static void set_panes(void);
181 static void draw_frame(void);
182 static void start(void);
184 /* global variables */
185 static pthread_t fsev_thread;
186 static Pane panes[2];
187 static Pane *cpane;
188 static int pane_idx;
189 static char *editor[2];
190 static char fed[] = "vi";
191 static int theight, twidth, scrheight;
192 static int *sel_indexes;
193 static size_t sel_len = 0;
194 static char **sel_files;
195 static int cont_vmode = 0;
196 pid_t main_pid;
197 #if defined(_SYS_INOTIFY_H)
198 #define READEVSZ 16
199 static int inotify_fd;
200 #elif defined(_SYS_EVENT_H_)
201 #define READEVSZ 0
202 static int kq;
203 struct kevent evlist[2]; /* events we want to monitor */
204 struct kevent chlist[2]; /* events that were triggered */
205 static struct timespec gtimeout;
206 #endif
207 #if defined(__linux__) || defined(__FreeBSD__)
208 #define OFF_T "%ld"
209 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
210 #define OFF_T "%lld"
211 #endif
212 enum { Left, Right }; /* panes */
214 /* configuration, allows nested code to access above variables */
215 #include "config.h"
217 /* function implementations */
218 static void
219 print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
221 while (*str != '\0') {
222 uint32_t uni = 0;
223 str += tb_utf8_char_to_unicode(&uni, str);
224 tb_change_cell(x, y, uni, fg, bg);
225 x++;
229 static void
230 printf_tb(int x, int y, Cpair col, const char *fmt, ...)
232 char buf[MAX_LINE];
233 va_list vl;
234 va_start(vl, fmt);
235 (void)vsnprintf(buf, MAX_LINE, fmt, vl);
236 va_end(vl);
237 print_tb(buf, x, y, col.fg, col.bg);
240 static void
241 print_status(Cpair col, const char *fmt, ...)
243 char buf[MAX_STATUS];
244 va_list vl;
245 va_start(vl, fmt);
246 (void)vsnprintf(buf, MAX_STATUS, fmt, vl);
247 va_end(vl);
248 clear_status();
249 print_tb(buf, 1, theight - 1, col.fg, col.bg);
252 static void
253 print_xstatus(char c, int x)
255 uint32_t uni = 0;
256 (void)tb_utf8_char_to_unicode(&uni, &c);
257 tb_change_cell(x, theight - 1, uni, cstatus.fg, cstatus.bg);
260 static void
261 print_error(char *errmsg)
263 print_status(cerr, errmsg);
266 static void
267 print_prompt(char *prompt)
269 print_status(cprompt, prompt);
272 static void
273 print_info(Pane *pane, char *dirsize)
275 char *sz, *ur, *gr, *dt, *prm;
277 dt = ecalloc(MAX_DTF, sizeof(char));
279 prm = get_fperm(CURSOR(pane).mode);
280 ur = get_fusr(CURSOR(pane).user);
281 gr = get_fgrp(CURSOR(pane).group);
283 if (get_fdt(dt, CURSOR(pane).dt) < 0)
284 *dt = '\0';
286 if (S_ISREG(CURSOR(pane).mode)) {
287 sz = get_fsize(CURSOR(pane).size);
288 } else {
289 if (dirsize == NULL) {
290 sz = ecalloc(1, sizeof(char));
291 *sz = '\0';
292 } else {
293 sz = dirsize;
297 print_status(cstatus, "%02d/%02d %s %s:%s %s %s", pane->hdir,
298 pane->dirc, prm, ur, gr, dt, sz);
300 free(prm);
301 free(ur);
302 free(gr);
303 free(dt);
304 free(sz);
307 static void
308 print_row(Pane *pane, size_t entpos, Cpair col)
310 int x, y;
311 char *result;
312 char buf[MAX_P];
313 char lnk_full[MAX_P];
314 int width;
316 width = (twidth / 2) - 4;
317 result = basename(pane->direntr[entpos].name);
318 x = pane->dirx;
319 y = entpos - pane->firstrow + 1;
321 if (S_ISLNK(pane->direntr[entpos].mode) != 0) {
322 if (realpath(pane->direntr[entpos].name, buf) != NULL) {
323 (void)snprintf(
324 lnk_full, MAX_N, "%s -> %s", result, buf);
325 result = lnk_full;
329 printf_tb(x, y, col, "%*.*s", ~width, width, result);
332 static void
333 clear(int sx, int ex, int y, uint16_t bg)
335 /* clear line from to */
336 /* x = line number vertical */
337 /* y = column number horizontal */
338 int i;
339 for (i = sx; i < ex; i++) {
340 tb_change_cell(i, y, 0x0000, TB_DEFAULT, bg);
344 static void
345 clear_status(void)
347 clear(1, twidth - 1, theight - 1, cstatus.bg);
350 static void
351 clear_pane(Pane *pane)
353 int i, y;
354 y = 0, i = 0;
356 while (i < scrheight) {
357 clear(pane->dirx, pane->ex, y, TB_DEFAULT);
358 i++;
359 y++;
362 /* draw top line */
363 for (y = pane->dirx; y < pane->ex; ++y) {
364 tb_change_cell(y, 0, u_hl, cframe.fg, cframe.bg);
368 static void
369 add_hi(Pane *pane, size_t entpos)
371 Cpair col;
372 get_hicol(&col, pane->direntr[entpos].mode);
373 col.fg |= TB_REVERSE | TB_BOLD;
374 col.bg |= TB_REVERSE;
375 print_row(pane, entpos, col);
378 static void
379 rm_hi(Pane *pane, size_t entpos)
381 Cpair col;
382 get_hicol(&col, pane->direntr[entpos].mode);
383 print_row(pane, entpos, col);
386 static int
387 check_dir(char *path)
389 DIR *dir;
390 dir = opendir(path);
392 if (dir == NULL) {
393 if (errno == ENOTDIR) {
394 return 1;
395 } else {
396 return -1;
400 if (closedir(dir) < 0)
401 return -1;
403 return 0;
406 static mode_t
407 chech_execf(mode_t mode)
409 if (S_ISREG(mode))
410 return (((S_IXUSR | S_IXGRP | S_IXOTH) & mode));
411 return 0;
414 static int
415 sort_name(const void *const A, const void *const B)
417 int result;
418 mode_t data1 = (*(Entry *)A).mode;
419 mode_t data2 = (*(Entry *)B).mode;
421 if (data1 < data2) {
422 return -1;
423 } else if (data1 == data2) {
424 result = strncmp((*(Entry *)A).name, (*(Entry *)B).name, MAX_N);
425 return result;
426 } else {
427 return 1;
431 static void
432 get_dirp(char *cdir)
434 int counter, len, i;
436 counter = 0;
437 len = strnlen(cdir, MAX_P);
438 if (len == 1)
439 return;
441 for (i = len - 1; i > 1; i--) {
442 if (cdir[i] == '/')
443 break;
444 else
445 counter++;
448 cdir[len - counter - 1] = '\0';
451 static char *
452 get_ext(char *str)
454 char *ext;
455 char dot;
456 size_t counter, len, i;
458 dot = '.';
459 counter = 0;
460 len = strnlen(str, MAX_N);
462 for (i = len - 1; i > 0; i--) {
463 if (str[i] == dot) {
464 break;
465 } else {
466 counter++;
470 ext = ecalloc(MAX_EXT + 1, sizeof(char));
471 strncpy(ext, &str[len - counter], MAX_EXT);
472 ext[MAX_EXT] = '\0';
473 return ext;
476 static int
477 get_fdt(char *result, time_t status)
479 struct tm lt;
480 localtime_r(&status, &lt);
481 return strftime(result, MAX_DTF, dtfmt, &lt);
484 static char *
485 get_fgrp(gid_t status)
487 char *result;
488 struct group *gr;
490 result = ecalloc(MAX_GRPN, sizeof(char));
491 gr = getgrgid(status);
492 if (gr == NULL)
493 (void)snprintf(result, MAX_GRPN, "%u", status);
494 else
495 strncpy(result, gr->gr_name, MAX_GRPN);
497 result[MAX_GRPN - 1] = '\0';
498 return result;
501 static char *
502 get_fperm(mode_t mode)
504 char *buf;
505 size_t i;
507 const char chars[] = "rwxrwxrwx";
508 buf = ecalloc(11, sizeof(char));
510 if (S_ISDIR(mode))
511 buf[0] = 'd';
512 else if (S_ISREG(mode))
513 buf[0] = '-';
514 else if (S_ISLNK(mode))
515 buf[0] = 'l';
516 else if (S_ISBLK(mode))
517 buf[0] = 'b';
518 else if (S_ISCHR(mode))
519 buf[0] = 'c';
520 else if (S_ISFIFO(mode))
521 buf[0] = 'p';
522 else if (S_ISSOCK(mode))
523 buf[0] = 's';
524 else
525 buf[0] = '?';
527 for (i = 1; i < 10; i++) {
528 buf[i] = (mode & (1 << (9 - i))) ? chars[i - 1] : '-';
530 buf[10] = '\0';
532 return buf;
535 static char *
536 get_fsize(off_t size)
538 char *result; /* need to be freed */
539 char unit;
540 int result_len;
541 int counter;
543 counter = 0;
544 result_len = 6; /* 9999X/0 */
545 result = ecalloc(result_len, sizeof(char));
547 while (size >= 1000) {
548 size /= 1024;
549 ++counter;
552 switch (counter) {
553 case 0:
554 unit = 'B';
555 break;
556 case 1:
557 unit = 'K';
558 break;
559 case 2:
560 unit = 'M';
561 break;
562 case 3:
563 unit = 'G';
564 break;
565 case 4:
566 unit = 'T';
567 break;
568 default:
569 unit = '?';
572 if (snprintf(result, result_len, OFF_T "%c", size, unit) < 0)
573 strncat(result, "???", result_len);
575 return result;
578 static char *
579 get_fullpath(char *first, char *second)
581 char *full_path;
583 full_path = ecalloc(MAX_P, sizeof(char));
585 if (strncmp(first, "/", MAX_P) == 0)
586 (void)snprintf(full_path, MAX_P, "/%s", second);
587 else
588 (void)snprintf(full_path, MAX_P, "%s/%s", first, second);
590 return full_path;
593 static char *
594 get_fusr(uid_t status)
596 char *result;
597 struct passwd *pw;
599 result = ecalloc(MAX_USRN, sizeof(char));
600 pw = getpwuid(status);
601 if (pw == NULL)
602 (void)snprintf(result, MAX_USRN, "%u", status);
603 else
604 strncpy(result, pw->pw_name, MAX_USRN);
606 result[MAX_USRN - 1] = '\0';
607 return result;
610 static void
611 get_dirsize(char *fullpath, off_t *fullsize)
613 DIR *dir;
614 char *ent_full;
615 mode_t mode;
616 struct dirent *entry;
617 struct stat status;
619 dir = opendir(fullpath);
620 if (dir == NULL) {
621 return;
624 while ((entry = readdir(dir)) != 0) {
625 if ((strncmp(entry->d_name, ".", 2) == 0 ||
626 strncmp(entry->d_name, "..", 3) == 0))
627 continue;
629 ent_full = get_fullpath(fullpath, entry->d_name);
630 if (lstat(ent_full, &status) == 0) {
631 mode = status.st_mode;
632 if (S_ISDIR(mode)) {
633 get_dirsize(ent_full, fullsize);
634 free(ent_full);
635 } else {
636 *fullsize += status.st_size;
637 free(ent_full);
642 closedir(dir);
643 clear_status();
646 static void
647 get_hicol(Cpair *col, mode_t mode)
649 *col = cfile;
650 if (S_ISDIR(mode))
651 *col = cdir;
652 else if (S_ISLNK(mode))
653 *col = cother;
654 else if (chech_execf(mode) > 0)
655 *col = cexec;
658 static void
659 delent(const Arg *arg)
661 if (cpane->dirc < 1)
662 return;
663 char *inp_conf;
664 int conf_len = 4;
665 char conf[] = "yes";
667 inp_conf = ecalloc(conf_len, sizeof(char));
668 if ((get_usrinput(inp_conf, conf_len, "delete file (yes) ?") < 0) ||
669 (strncmp(inp_conf, conf, conf_len) != 0)) {
670 free(inp_conf);
671 return; /* canceled by user or wrong inp_conf */
673 free(inp_conf);
675 char *tmp[1];
676 tmp[0] = CURSOR(cpane).name;
677 if (spawn(rm_cmd, rm_cmd_len, tmp, 1, NULL) < 0) {
678 print_error(strerror(errno));
679 return;
683 static void
684 calcdir(const Arg *arg)
686 if (cpane->dirc < 1)
687 return;
688 if (!S_ISDIR(CURSOR(cpane).mode))
689 return;
691 off_t *fullsize;
692 char *csize;
694 fullsize = ecalloc(1, sizeof(off_t));
695 get_dirsize(CURSOR(cpane).name, fullsize);
696 csize = get_fsize(*fullsize);
698 CURSOR(cpane).size = *fullsize;
699 print_info(cpane, csize);
700 free(fullsize);
703 static void
704 crnd(const Arg *arg)
706 char *user_input, *path;
708 user_input = ecalloc(MAX_USRI, sizeof(char));
709 if (get_usrinput(user_input, MAX_USRI, "new dir") < 0) {
710 free(user_input);
711 return;
714 path = ecalloc(MAX_P, sizeof(char));
715 if (snprintf(path, MAX_P, "%s/%s", cpane->dirn, user_input) < 0) {
716 free(user_input);
717 free(path);
718 return;
721 PERROR(mkdir(path, ndir_perm) < 0);
723 free(user_input);
724 free(path);
727 static void
728 crnf(const Arg *arg)
730 char *user_input, *path;
731 int rf;
733 user_input = ecalloc(MAX_USRI, sizeof(char));
734 if (get_usrinput(user_input, MAX_USRI, "new file") < 0) {
735 free(user_input);
736 return;
739 path = ecalloc(MAX_P, sizeof(char));
740 if (snprintf(path, MAX_P, "%s/%s", cpane->dirn, user_input) < 0) {
741 free(user_input);
742 free(path);
743 return;
746 rf = open(path, O_CREAT | O_EXCL, nf_perm);
748 if (rf < 0)
749 print_error(strerror(errno));
750 else if (close(rf) < 0)
751 print_error(strerror(errno));
753 free(user_input);
754 free(path);
756 static void
757 mv_ver(const Arg *arg)
760 if (cpane->dirc < 1)
761 return;
762 if (cpane->hdir - arg->i < 1) /* first line */
763 return;
765 if (cpane->hdir - arg->i > cpane->dirc) /* last line */
766 return;
768 if (cpane->firstrow > 0 && arg->i > 0 &&
769 cpane->hdir <= (cpane->firstrow + arg->i)) { /* scroll up */
770 cpane->firstrow = cpane->firstrow - arg->i;
771 rm_hi(cpane, cpane->hdir - 1);
772 cpane->hdir = cpane->hdir - arg->i;
773 refresh_pane(cpane);
774 add_hi(cpane, cpane->hdir - 1);
775 return;
778 if (cpane->hdir - cpane->firstrow >= scrheight + arg->i &&
779 arg->i < 0) { /* scroll down */
780 cpane->firstrow = cpane->firstrow - arg->i;
781 rm_hi(cpane, cpane->hdir - 1);
782 cpane->hdir = cpane->hdir - arg->i;
783 refresh_pane(cpane);
784 add_hi(cpane, cpane->hdir - 1);
785 return;
788 rm_hi(cpane, cpane->hdir - 1);
789 cpane->hdir = cpane->hdir - arg->i;
790 add_hi(cpane, cpane->hdir - 1);
791 print_info(cpane, NULL);
794 static void
795 mvbk(const Arg *arg)
797 if (cpane->dirn[0] == '/' && cpane->dirn[1] == '\0') { /* cwd = / */
798 return;
801 get_dirp(cpane->dirn);
802 if (check_dir(cpane->dirn) < 0) {
803 print_error(strerror(errno));
804 return;
807 rmwatch(cpane);
808 cpane->firstrow = cpane->parent_firstrow;
809 cpane->hdir = cpane->parent_row;
810 PERROR(listdir(cpane) < 0);
811 cpane->parent_firstrow = 0;
812 cpane->parent_row = 1;
815 static void
816 mvbtm(const Arg *arg)
818 if (cpane->dirc < 1)
819 return;
820 if (cpane->dirc > scrheight) {
821 rm_hi(cpane, cpane->hdir - 1);
822 cpane->hdir = cpane->dirc;
823 cpane->firstrow = cpane->dirc - scrheight + 1;
824 refresh_pane(cpane);
825 add_hi(cpane, cpane->hdir - 1);
826 } else {
827 rm_hi(cpane, cpane->hdir - 1);
828 cpane->hdir = cpane->dirc;
829 add_hi(cpane, cpane->hdir - 1);
831 print_info(cpane, NULL);
834 static void
835 mvfwd(const Arg *arg)
837 if (cpane->dirc < 1)
838 return;
839 int s;
841 switch (check_dir(CURSOR(cpane).name)) {
842 case 0:
843 strncpy(cpane->dirn, CURSOR(cpane).name, MAX_P);
844 cpane->parent_row = cpane->hdir;
845 cpane->parent_firstrow = cpane->firstrow;
846 cpane->hdir = 1;
847 cpane->firstrow = 0;
848 rmwatch(cpane);
849 PERROR(listdir(cpane) < 0);
850 break;
851 case 1: /* not a directory open file */
852 rmwatch(cpane);
853 tb_shutdown();
854 s = opnf(CURSOR(cpane).name);
855 if (tb_init() != 0)
856 die("tb_init");
857 t_resize();
858 if (s < 0)
859 print_error("process failed non-zero exit");
860 break;
861 case -1: /* failed to open directory */
862 print_error(strerror(errno));
866 static void
867 mvtop(const Arg *arg)
869 if (cpane->dirc < 1)
870 return;
871 if (cpane->dirc > scrheight) {
872 rm_hi(cpane, cpane->hdir - 1);
873 cpane->hdir = 1;
874 cpane->firstrow = 0;
875 refresh_pane(cpane);
876 add_hi(cpane, cpane->hdir - 1);
877 } else {
878 rm_hi(cpane, cpane->hdir - 1);
879 cpane->hdir = 1;
880 add_hi(cpane, cpane->hdir - 1);
881 print_info(cpane, NULL);
885 static void
886 bkmrk(const Arg *arg)
888 if (check_dir((char *)arg->v) != 0) {
889 print_error(strerror(errno));
890 return;
893 rmwatch(cpane);
894 strncpy(cpane->dirn, (char *)arg->v, MAX_P);
895 cpane->firstrow = 0;
896 cpane->parent_row = 1;
897 cpane->hdir = 1;
898 PERROR(listdir(cpane) < 0);
901 static int
902 get_usrinput(char *result, size_t max_chars, const char *fmt, ...)
904 char msg[MAX_N];
905 size_t i, cpos, startat;
906 struct tb_event fev;
907 va_list vl;
909 i = 0;
910 cpos = 1;
912 va_start(vl, fmt);
913 startat = vsnprintf(msg, MAX_N, fmt, vl) + 1;
914 va_end(vl);
916 clear_status();
917 print_tb(msg, 1, theight - 1, cprompt.fg, cprompt.bg);
918 tb_set_cursor(startat + 1, theight - 1);
919 tb_present();
921 while (tb_poll_event(&fev) != 0) {
922 switch (fev.type) {
923 case TB_EVENT_KEY:
924 if (fev.key == TB_KEY_ESC) {
925 tb_set_cursor(-1, -1);
926 clear_status();
927 return -1;
930 if (fev.key == TB_KEY_BACKSPACE ||
931 fev.key == TB_KEY_BACKSPACE2) {
932 if (BETWEEN(cpos, 2, max_chars)) {
933 result[i - 1] = '\0';
934 cpos--;
935 i--;
936 print_xstatus(' ', startat + cpos);
937 tb_set_cursor(
938 startat + cpos, theight - 1);
941 } else if (fev.key == TB_KEY_ENTER) {
942 tb_set_cursor(-1, -1);
943 result[cpos - 1] = '\0';
944 return 0;
946 } else if (fev.key) { /* disable other TB_KEY_* */
947 break;
949 } else {
950 if (cpos < max_chars) {
951 print_xstatus((char)fev.ch,
952 (startat + cpos));
953 result[i] = (char)fev.ch;
954 tb_set_cursor((startat + cpos + 1),
955 theight - 1);
956 cpos++;
957 i++;
961 tb_present();
962 break;
964 case TB_EVENT_RESIZE:
965 t_resize();
966 clear_status();
967 print_tb(msg, 1, theight - 1, cprompt.fg, cprompt.bg);
968 print_tb(result, startat + 1, theight - 1, cstatus.fg,
969 cstatus.bg);
970 tb_present();
971 break;
973 default:
974 return -1;
978 return -1;
981 static int
982 frules(char *ex)
984 size_t c, d;
986 for (c = 0; c < LEN(rules); c++)
987 for (d = 0; d < rules[c].exlen; d++)
988 if (strncmp(rules[c].ext[d], ex, MAX_EXT) == 0)
989 return c;
990 return -1;
993 static int
994 spawn(const void *com_argv, size_t com_argc, const void *f_argv, size_t f_argc,
995 char *fn)
997 int ws;
998 size_t argc;
999 pid_t pid, r;
1001 argc = com_argc + f_argc + 2;
1002 char *argv[argc];
1004 memcpy(argv, com_argv, com_argc * sizeof(char *)); /* command */
1005 memcpy(&argv[com_argc], f_argv, f_argc * sizeof(char *)); /* files */
1007 argv[argc - 2] = fn;
1008 argv[argc - 1] = NULL;
1010 pid = fork();
1011 switch (pid) {
1012 case -1:
1013 return -1;
1014 case 0:
1015 execvp(argv[0], argv);
1016 exit(EXIT_SUCCESS);
1017 default:
1018 while ((r = waitpid(pid, &ws, 0)) == -1 && errno == EINTR)
1019 continue;
1020 if (r == -1)
1021 return -1;
1022 if ((WIFEXITED(ws) != 0) && (WEXITSTATUS(ws) != 0))
1023 return -1;
1025 return 0;
1028 static int
1029 opnf(char *fn)
1031 char *ex;
1032 int c;
1034 ex = get_ext(fn);
1035 c = frules(ex);
1036 free(ex);
1038 if (c < 0) /* extension not found open in editor */
1039 return spawn(editor, 1, NULL, 0, fn);
1040 else
1041 return spawn((char **)rules[c].v, rules[c].vlen, NULL, 0, fn);
1044 static int
1045 fsev_init(void)
1047 #if defined(_SYS_INOTIFY_H)
1048 inotify_fd = inotify_init();
1049 if (inotify_fd < 0)
1050 return -1;
1051 #elif defined(_SYS_EVENT_H_)
1052 gtimeout.tv_sec = 1;
1053 kq = kqueue();
1054 if (kq < 0)
1055 return -1;
1056 #endif
1057 return 0;
1060 static int
1061 addwatch(Pane *pane)
1063 #if defined(_SYS_INOTIFY_H)
1064 return pane->inotify_wd = inotify_add_watch(inotify_fd, pane->dirn,
1065 IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE |
1066 IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF);
1067 #elif defined(_SYS_EVENT_H_)
1068 pane->event_fd = open(pane->dirn, O_RDONLY);
1069 if (pane->event_fd < 0)
1070 return pane->event_fd;
1071 EV_SET(&evlist[pane->pane_id], pane->event_fd, EVFILT_VNODE,
1072 EV_ADD | EV_CLEAR,
1073 NOTE_DELETE | NOTE_EXTEND | NOTE_LINK | NOTE_RENAME |
1074 NOTE_REVOKE | NOTE_WRITE,
1075 0, NULL);
1076 return 0;
1077 #endif
1080 static int
1081 read_events(void)
1083 #if defined(_SYS_INOTIFY_H)
1084 char *p;
1085 ssize_t r;
1086 struct inotify_event *event;
1087 const size_t events = 32;
1088 const size_t evbuflen =
1089 events * (sizeof(struct inotify_event) + MAX_N + 1);
1090 char buf[evbuflen];
1092 if (cpane->inotify_wd < 0)
1093 return -1;
1094 r = read(inotify_fd, buf, evbuflen);
1095 if (r <= 0)
1096 return r;
1098 for (p = buf; p < buf + r;) {
1099 event = (struct inotify_event *)p;
1100 if (!event->wd)
1101 break;
1102 if (event->mask) {
1103 return r;
1106 p += sizeof(struct inotify_event) + event->len;
1108 #elif defined(_SYS_EVENT_H_)
1109 return kevent(kq, evlist, 2, chlist, 2, &gtimeout);
1110 #endif
1111 return -1;
1114 static void
1115 rmwatch(Pane *pane)
1117 #if defined(_SYS_INOTIFY_H)
1118 if (pane->inotify_wd >= 0)
1119 inotify_rm_watch(inotify_fd, pane->inotify_wd);
1120 #elif defined(_SYS_EVENT_H_)
1121 close(pane->event_fd);
1122 #endif
1125 static void
1126 fsev_shdn(void)
1128 pthread_cancel(fsev_thread);
1129 #if defined(__linux__)
1130 pthread_join(fsev_thread, NULL);
1131 #endif
1132 rmwatch(&panes[Left]);
1133 rmwatch(&panes[Right]);
1134 #if defined(_SYS_INOTIFY_H)
1135 close(inotify_fd);
1136 #elif defined(_SYS_EVENT_H_)
1137 close(kq);
1138 #endif
1141 static void
1142 toggle_df(const Arg *arg)
1144 show_dotfiles = !show_dotfiles;
1145 PERROR(listdir(&panes[Left]));
1146 PERROR(listdir(&panes[Right]));
1147 tb_present();
1150 static void
1151 start_filter(const Arg *arg)
1153 if (cpane->dirc < 1)
1154 return;
1155 char *user_input;
1156 user_input = ecalloc(MAX_USRI, sizeof(char));
1157 if (get_usrinput(user_input, MAX_USRI, "filter") < 0) {
1158 free(user_input);
1159 return;
1161 cpane->filter = user_input;
1162 if (listdir(cpane) < 0)
1163 print_error("no match");
1164 cpane->filter = NULL;
1165 free(user_input);
1168 static void
1169 start_vmode(const Arg *arg)
1171 if (cpane->dirc < 1)
1172 return;
1173 struct tb_event fev;
1174 if (sel_indexes != NULL) {
1175 free(sel_indexes);
1176 sel_indexes = NULL;
1179 sel_indexes = ecalloc(cpane->dirc, sizeof(size_t));
1180 sel_indexes[0] = cpane->hdir;
1181 cont_vmode = 0;
1182 print_prompt("-- VISUAL --");
1183 tb_present();
1184 while (tb_poll_event(&fev) != 0) {
1185 switch (fev.type) {
1186 case TB_EVENT_KEY:
1187 grabkeys(&fev, vkeys, vkeyslen);
1188 if (cont_vmode == -1)
1189 return;
1190 tb_present();
1191 break;
1196 static void
1197 exit_vmode(const Arg *arg)
1199 refresh_pane(cpane);
1200 add_hi(cpane, cpane->hdir - 1);
1201 cont_vmode = -1;
1204 static void
1205 selup(const Arg *arg)
1207 mv_ver(arg);
1208 print_prompt("-- VISUAL --");
1209 int index = abs(cpane->hdir - sel_indexes[0]);
1211 if (cpane->hdir < sel_indexes[0]) {
1212 sel_indexes[index] = cpane->hdir;
1213 add_hi(cpane, sel_indexes[index]);
1214 } else if (index < cpane->dirc) {
1215 sel_indexes[index + 1] = 0;
1217 if (cpane->dirc >= scrheight ||
1218 cpane->hdir <= 1) { /* rehighlight all if scrolling */
1219 selref();
1223 static void
1224 seldwn(const Arg *arg)
1226 mv_ver(arg);
1227 print_prompt("-- VISUAL --");
1228 int index = abs(cpane->hdir - sel_indexes[0]);
1230 if (cpane->hdir > sel_indexes[0]) {
1231 sel_indexes[index] = cpane->hdir;
1232 add_hi(cpane, sel_indexes[index] - 2);
1233 } else {
1234 sel_indexes[index + 1] = 0;
1236 if (cpane->dirc >= scrheight ||
1237 cpane->hdir >= cpane->dirc) { /* rehighlight all if scrolling */
1238 selref();
1242 static void
1243 selall(const Arg *arg)
1245 int i;
1246 for (i = 0; i < cpane->dirc; i++) {
1247 sel_indexes[i] = i + 1;
1249 selref();
1252 static void
1253 selref(void)
1255 int i;
1256 for (i = 0; i < cpane->dirc; i++) {
1257 if (sel_indexes[i] < (scrheight + cpane->firstrow) &&
1258 sel_indexes[i] >
1259 cpane->firstrow) { /* checks if in the frame of the directories */
1260 add_hi(cpane, sel_indexes[i] - 1);
1265 static void
1266 selcalc(void)
1268 int j;
1269 sel_len = 0;
1271 for (j = 0; j < cpane->dirc; j++) { /* calculate used selection size */
1272 if (sel_indexes[j] != 0)
1273 sel_len++;
1274 else
1275 break;
1279 static void
1280 free_files(void)
1282 size_t i;
1284 if (sel_files != NULL) {
1285 for (i = 0; i < sel_len; i++) {
1286 free(sel_files[i]);
1287 sel_files[i] = NULL;
1289 free(sel_files);
1290 sel_files = NULL;
1294 static void
1295 init_files(void)
1297 size_t i;
1298 free_files();
1300 selcalc();
1301 sel_files = ecalloc(sel_len, sizeof(char *));
1303 for (i = 0; i < sel_len; i++) {
1304 sel_files[i] = ecalloc(MAX_P, sizeof(char));
1305 strncpy(sel_files[i], cpane->direntr[sel_indexes[i] - 1].name,
1306 MAX_P);
1310 static void
1311 selynk(const Arg *arg)
1313 init_files();
1314 refresh_pane(cpane);
1315 add_hi(cpane, cpane->hdir - 1);
1316 print_status(cprompt, "%zu files are yanked", sel_len);
1317 cont_vmode = -1;
1320 static void
1321 seldel(const Arg *arg)
1323 char *inp_conf;
1325 inp_conf = ecalloc(delconf_len, sizeof(char));
1326 if ((get_usrinput(inp_conf, delconf_len, "delete file (yes) ?") < 0) ||
1327 (strncmp(inp_conf, delconf, delconf_len) != 0)) {
1328 free(inp_conf);
1329 return; /* canceled by user or wrong inp_conf */
1331 free(inp_conf);
1333 init_files();
1335 if (spawn(rm_cmd, rm_cmd_len, sel_files, sel_len, NULL) < 0)
1336 print_error(strerror(errno));
1337 else
1338 print_status(cprompt, "%zu files are deleted", sel_len);
1340 free_files();
1341 cont_vmode = -1;
1344 static void
1345 paste(const Arg *arg)
1347 if (sel_files == NULL) {
1348 print_error("nothing to paste");
1349 return;
1352 if (spawn(cp_cmd, cp_cmd_len, sel_files, sel_len, cpane->dirn) < 0)
1353 print_error(strerror(errno));
1354 else
1355 print_status(cprompt, "%zu files are copied", sel_len);
1357 free_files();
1360 static void
1361 selmv(const Arg *arg)
1363 if (sel_files == NULL) {
1364 print_error("nothing to move");
1365 return;
1368 if (spawn(mv_cmd, mv_cmd_len, sel_files, sel_len, cpane->dirn) < 0)
1369 print_error(strerror(errno));
1370 else
1371 print_status(cprompt, "%zu files are moved", sel_len);
1373 free_files();
1376 static void
1377 rname(const Arg *arg)
1379 if (cpane->dirc < 1)
1380 return;
1381 char new_name[MAX_P];
1382 char *input_name;
1384 input_name = ecalloc(MAX_N, sizeof(char));
1386 if (get_usrinput(input_name, MAX_N, "rename: %s",
1387 basename(CURSOR(cpane).name)) < 0) {
1388 free(input_name);
1389 return;
1392 if (snprintf(new_name, MAX_P, "%s/%s", cpane->dirn, input_name) < 0) {
1393 free(input_name);
1394 print_error(strerror(errno));
1395 return;
1398 char *rename_cmd[] = { "mv", CURSOR(cpane).name, new_name };
1399 PERROR(spawn(rename_cmd, 3, NULL, 0, NULL) < 0);
1401 free(input_name);
1404 static void
1405 yank(const Arg *arg)
1407 if (cpane->dirc < 1)
1408 return;
1410 free_files();
1411 sel_len = 1;
1412 sel_files = ecalloc(sel_len, sizeof(char *));
1413 sel_files[0] = ecalloc(MAX_P, sizeof(char));
1414 strncpy(sel_files[0], CURSOR(cpane).name, MAX_P);
1415 print_status(cprompt, "1 file is yanked", sel_len);
1418 static void
1419 switch_pane(const Arg *arg)
1421 if (cpane->dirc > 0)
1422 rm_hi(cpane, cpane->hdir - 1);
1423 cpane = &panes[pane_idx ^= 1];
1424 if (cpane->dirc > 0) {
1425 add_hi(cpane, cpane->hdir - 1);
1426 print_info(cpane, NULL);
1427 } else {
1428 clear_status();
1432 static void
1433 quit(const Arg *arg)
1435 if (cont_vmode == -1) { /* check if selection was allocated */
1436 free(sel_indexes);
1437 if (sel_files != NULL)
1438 free_files();
1440 free(panes[Left].direntr);
1441 free(panes[Right].direntr);
1442 fsev_shdn();
1443 tb_shutdown();
1444 exit(EXIT_SUCCESS);
1447 static void
1448 grabkeys(struct tb_event *event, Key *key, size_t max_keys)
1450 size_t i;
1452 for (i = 0; i < max_keys; i++) {
1453 if (event->ch != 0) {
1454 if (event->ch == key[i].evkey.ch) {
1455 key[i].func(&key[i].arg);
1456 return;
1458 } else if (event->key != 0) {
1459 if (event->key == key[i].evkey.key) {
1460 key[i].func(&key[i].arg);
1461 return;
1467 void *
1468 read_th(void *arg)
1470 struct timespec tim;
1471 tim.tv_sec = 0;
1472 tim.tv_nsec = 5000000L; /* 0.005 sec */
1474 while (1)
1475 if (read_events() > READEVSZ) {
1476 kill(main_pid, SIGUSR1);
1477 nanosleep(&tim, NULL);
1479 return arg;
1482 static void
1483 start_ev(void)
1485 struct tb_event ev;
1487 while (tb_poll_event(&ev) != 0) {
1488 switch (ev.type) {
1489 case TB_EVENT_KEY:
1490 grabkeys(&ev, nkeys, nkeyslen);
1491 tb_present();
1492 break;
1493 case TB_EVENT_RESIZE:
1494 t_resize();
1495 break;
1496 default:
1497 break;
1500 tb_shutdown();
1503 static void
1504 refresh_pane(Pane *pane)
1506 size_t y, dyn_max, start_from;
1507 int width;
1508 width = (twidth / 2) - 4;
1509 Cpair col;
1511 y = 1;
1512 start_from = pane->firstrow;
1513 dyn_max = MIN(pane->dirc, (scrheight - 1) + pane->firstrow);
1515 /* print each entry in directory */
1516 while (start_from < dyn_max) {
1517 get_hicol(&col, pane->direntr[start_from].mode);
1518 print_row(pane, start_from, col);
1519 start_from++;
1520 y++;
1523 if (pane->dirc > 0)
1524 print_info(pane, NULL);
1525 else
1526 clear_status();
1528 /* print current directory title */
1529 pane->dircol.fg |= TB_BOLD;
1530 printf_tb(pane->dirx, 0, pane->dircol, " %.*s ", width, pane->dirn);
1533 static void
1534 set_direntr(Pane *pane, struct dirent *entry, DIR *dir, char *filter)
1536 int i;
1537 char *tmpfull;
1538 struct stat status;
1540 #define ADD_ENTRY \
1541 tmpfull = get_fullpath(pane->dirn, entry->d_name); \
1542 strncpy(pane->direntr[i].name, tmpfull, MAX_N); \
1543 if (lstat(tmpfull, &status) == 0) { \
1544 pane->direntr[i].size = status.st_size; \
1545 pane->direntr[i].mode = status.st_mode; \
1546 pane->direntr[i].group = status.st_gid; \
1547 pane->direntr[i].user = status.st_uid; \
1548 pane->direntr[i].dt = status.st_mtime; \
1550 i++; \
1551 free(tmpfull);
1553 i = 0;
1554 pane->direntr =
1555 erealloc(pane->direntr, (10 + pane->dirc) * sizeof(Entry));
1556 while ((entry = readdir(dir)) != 0) {
1557 if (show_dotfiles == 1) {
1558 if (entry->d_name[0] == '.' &&
1559 (entry->d_name[1] == '\0' ||
1560 entry->d_name[1] == '.'))
1561 continue;
1562 } else {
1563 if (entry->d_name[0] == '.')
1564 continue;
1567 if (filter == NULL) {
1568 ADD_ENTRY
1569 } else if (filter != NULL) {
1570 if (strcasestr(entry->d_name, filter) != NULL) {
1571 ADD_ENTRY
1576 pane->dirc = i;
1579 static int
1580 listdir(Pane *pane)
1582 DIR *dir;
1583 struct dirent *entry;
1584 int width;
1585 int filtercount = 0;
1586 size_t oldc = pane->dirc;
1588 width = (twidth / 2) - 4;
1589 pane->dirc = 0;
1591 dir = opendir(pane->dirn);
1592 if (dir == NULL)
1593 return -1;
1595 /* get content and filter sum */
1596 while ((entry = readdir(dir)) != 0) {
1597 if (pane->filter != NULL) {
1598 if (strcasestr(entry->d_name, pane->filter) != NULL)
1599 filtercount++;
1600 } else { /* no filter */
1601 pane->dirc++;
1605 if (pane->filter == NULL) {
1606 clear_pane(pane);
1607 pane->dirc -= 2;
1610 if (pane->filter != NULL) {
1611 if (filtercount > 0) {
1612 pane->dirc = filtercount;
1613 clear_pane(pane);
1614 pane->hdir = 1;
1615 } else if (filtercount == 0) {
1616 if (closedir(dir) < 0)
1617 return -1;
1618 pane->dirc = oldc;
1619 return -1;
1623 /* print current directory title */
1624 pane->dircol.fg |= TB_BOLD;
1625 printf_tb(pane->dirx, 0, pane->dircol, " %.*s ", width, pane->dirn);
1627 if (pane->filter == NULL) /* dont't watch when filtering */
1628 if (addwatch(pane) < 0)
1629 print_error("can't add watch");
1631 /* empty directory */
1632 if (pane->dirc == 0) {
1633 clear_status();
1634 if (closedir(dir) < 0)
1635 return -1;
1636 return 0;
1639 rewinddir(dir); /* reset position */
1640 set_direntr(
1641 pane, entry, dir, pane->filter); /* create array of entries */
1642 qsort(pane->direntr, pane->dirc, sizeof(Entry), sort_name);
1643 refresh_pane(pane);
1645 if (pane->hdir > pane->dirc)
1646 pane->hdir = pane->dirc;
1648 if (pane == cpane && pane->dirc > 0)
1649 add_hi(pane, pane->hdir - 1);
1651 if (closedir(dir) < 0)
1652 return -1;
1653 return 0;
1656 static void
1657 t_resize(void)
1659 tb_clear();
1660 draw_frame();
1661 panes[Right].dirx = (twidth / 2) + 2;
1662 refresh_pane(&panes[Left]);
1663 refresh_pane(&panes[Right]);
1664 if (cpane->dirc > 0)
1665 add_hi(cpane, cpane->hdir - 1);
1666 tb_present();
1669 static void
1670 get_editor(void)
1672 editor[0] = getenv("EDITOR");
1673 editor[1] = NULL;
1675 if (editor[0] == NULL)
1676 editor[0] = fed;
1679 static void
1680 set_panes(void)
1682 char *home;
1683 char cwd[MAX_P];
1685 home = getenv("HOME");
1686 if (home == NULL)
1687 home = "/";
1688 if ((getcwd(cwd, sizeof(cwd)) == NULL))
1689 strncpy(cwd, home, MAX_P);
1691 pane_idx = Left; /* cursor pane */
1692 cpane = &panes[pane_idx];
1694 panes[Left].ex = (twidth / 2) - 1;
1695 panes[Left].pane_id = 0;
1696 panes[Left].dirx = 2;
1697 panes[Left].dircol = cpanell;
1698 panes[Left].firstrow = 0;
1699 panes[Left].direntr = ecalloc(0, sizeof(Entry));
1700 strncpy(panes[Left].dirn, cwd, MAX_P);
1701 panes[Left].hdir = 1;
1702 panes[Left].inotify_wd = -1;
1703 panes[Left].parent_row = 1;
1705 panes[Right].ex = twidth - 1;
1706 panes[Right].pane_id = 1;
1707 panes[Right].dirx = (twidth / 2) + 2;
1708 panes[Right].dircol = cpanelr;
1709 panes[Right].firstrow = 0;
1710 panes[Right].direntr = ecalloc(0, sizeof(Entry));
1711 strncpy(panes[Right].dirn, home, MAX_P);
1712 panes[Right].hdir = 1;
1713 panes[Right].inotify_wd = -1;
1714 panes[Right].parent_row = 1;
1717 static void
1718 draw_frame(void)
1720 int i;
1721 theight = tb_height();
1722 twidth = tb_width();
1723 scrheight = theight - 2;
1725 /* 2 horizontal lines */
1726 for (i = 1; i < twidth - 1; ++i) {
1727 tb_change_cell(i, 0, u_hl, cframe.fg, cframe.bg);
1728 tb_change_cell(i, theight - 2, u_hl, cframe.fg, cframe.bg);
1731 /* 4 vertical lines */
1732 for (i = 1; i < theight - 1; ++i) {
1733 tb_change_cell(0, i, u_vl, cframe.fg, cframe.bg);
1734 tb_change_cell(
1735 (twidth - 1) / 2, i - 1, u_vl, cframe.fg, cframe.bg);
1736 tb_change_cell(((twidth - 1) / 2) + 1, i - 1, u_vl, cframe.fg,
1737 cframe.bg);
1738 tb_change_cell(twidth - 1, i, u_vl, cframe.fg, cframe.bg);
1741 /* 4 corners */
1742 tb_change_cell(0, 0, u_cnw, cframe.fg, cframe.bg);
1743 tb_change_cell(twidth - 1, 0, u_cne, cframe.fg, cframe.bg);
1744 tb_change_cell(0, theight - 2, u_csw, cframe.fg, cframe.bg);
1745 tb_change_cell(twidth - 1, theight - 2, u_cse, cframe.fg, cframe.bg);
1747 /* 2 middel top and bottom */
1748 tb_change_cell((twidth - 1) / 2, 0, u_mn, cframe.fg, cframe.bg);
1749 tb_change_cell(
1750 (twidth - 1) / 2, theight - 2, u_ms, cframe.fg, cframe.bg);
1753 void
1754 th_handler(int num)
1756 (void)num;
1757 PERROR(listdir(&panes[Left]));
1758 PERROR(listdir(&panes[Right]));
1759 tb_present();
1762 static int
1763 start_signal(void)
1765 struct sigaction sa;
1767 main_pid = getpid();
1768 sa.sa_handler = th_handler;
1769 sigemptyset(&sa.sa_mask);
1770 sa.sa_flags = SA_RESTART;
1771 return sigaction(SIGUSR1, &sa, NULL);
1774 static void
1775 start(void)
1777 FAIL_IF(tb_init() != 0, "tb_init()");
1778 if (tb_select_output_mode(TB_OUTPUT_256) != TB_OUTPUT_256)
1779 if (tb_select_output_mode(TB_OUTPUT_NORMAL) != TB_OUTPUT_NORMAL)
1780 die("output error");
1781 draw_frame();
1782 set_panes();
1783 get_editor();
1784 PERROR(start_signal() < 0);
1785 PERROR(fsev_init() < 0);
1786 PERROR(listdir(&panes[Left]) < 0);
1787 PERROR(listdir(&panes[Right]) < 0);
1788 tb_present();
1790 pthread_create(&fsev_thread, NULL, read_th, NULL);
1791 start_ev();
1795 main(int argc, char *argv[])
1797 #if defined(__OpenBSD__)
1798 if (pledge("cpath exec getpw proc rpath stdio tmppath tty wpath",
1799 NULL) == -1)
1800 die("pledge");
1801 #endif /* __OpenBSD__ */
1802 if (argc == 1)
1803 start();
1804 else if (argc == 2 && strncmp("-v", argv[1], 2) == 0)
1805 die("sfm-" VERSION);
1806 else
1807 die("usage: sfm [-v]");
1808 return 0;