Blame


1 2b2f7365 2021-11-26 op /* needed for some ncurses stuff */
2 c9f42136 2021-11-25 op #define _XOPEN_SOURCE_EXTENDED
3 c9f42136 2021-11-25 op #define _FILE_OFFSET_BITS 64
4 c9f42136 2021-11-25 op
5 8ada055d 2021-11-26 op #include <sys/stat.h>
6 8ada055d 2021-11-26 op #include <sys/types.h>
7 8ada055d 2021-11-26 op #include <sys/wait.h>
8 8ada055d 2021-11-26 op
9 01a2cff6 2021-12-12 op #include <assert.h>
10 8ada055d 2021-11-26 op #include <ctype.h>
11 8ada055d 2021-11-26 op #include <curses.h>
12 8ada055d 2021-11-26 op #include <dirent.h>
13 cc9de337 2021-11-26 op #include <err.h>
14 8ada055d 2021-11-26 op #include <errno.h>
15 8ada055d 2021-11-26 op #include <fcntl.h>
16 cc9de337 2021-11-26 op #include <getopt.h>
17 8ada055d 2021-11-26 op #include <libgen.h>
18 8ada055d 2021-11-26 op #include <limits.h>
19 8ada055d 2021-11-26 op #include <locale.h>
20 8ada055d 2021-11-26 op #include <signal.h>
21 8ada055d 2021-11-26 op #include <stdarg.h>
22 c9f42136 2021-11-25 op #include <stdint.h>
23 8ada055d 2021-11-26 op #include <stdio.h>
24 8ada055d 2021-11-26 op #include <stdlib.h>
25 8ada055d 2021-11-26 op #include <string.h>
26 8ada055d 2021-11-26 op #include <unistd.h>
27 c9f42136 2021-11-25 op #include <wchar.h>
28 c9f42136 2021-11-25 op #include <wctype.h>
29 c9f42136 2021-11-25 op
30 e03d1219 2021-12-13 op #ifndef FM_SHELL
31 e03d1219 2021-12-13 op #define FM_SHELL "/bin/sh"
32 63fe4ed0 2021-12-13 op #endif
33 63fe4ed0 2021-12-13 op
34 63fe4ed0 2021-12-13 op #ifndef FM_PAGER
35 63fe4ed0 2021-12-13 op #define FM_PAGER "/usr/bin/less"
36 e03d1219 2021-12-13 op #endif
37 e03d1219 2021-12-13 op
38 d244a655 2021-12-13 op #ifndef FM_ED
39 d244a655 2021-12-13 op #define FM_ED "/bin/ed"
40 d244a655 2021-12-13 op #endif
41 d244a655 2021-12-13 op
42 2c5122bc 2021-12-13 op #ifndef FM_OPENER
43 2c5122bc 2021-12-13 op #define FM_OPENER "xdg-open"
44 2c5122bc 2021-12-13 op #endif
45 2c5122bc 2021-12-13 op
46 c9f42136 2021-11-25 op #include "config.h"
47 cc9de337 2021-11-26 op
48 cc9de337 2021-11-26 op struct option opts[] = {
49 cc9de337 2021-11-26 op {"help", no_argument, NULL, 'h'},
50 cc9de337 2021-11-26 op {"version", no_argument, NULL, 'v'},
51 cc9de337 2021-11-26 op {NULL, 0, NULL, 0}
52 cc9de337 2021-11-26 op };
53 c9f42136 2021-11-25 op
54 01a2cff6 2021-12-12 op static char clipboard[PATH_MAX];
55 01a2cff6 2021-12-12 op
56 c9f42136 2021-11-25 op /* String buffers. */
57 c9f42136 2021-11-25 op #define BUFLEN PATH_MAX
58 c9f42136 2021-11-25 op static char BUF1[BUFLEN];
59 c9f42136 2021-11-25 op static char BUF2[BUFLEN];
60 c9f42136 2021-11-25 op static char INPUT[BUFLEN];
61 c9f42136 2021-11-25 op static wchar_t WBUF[BUFLEN];
62 c9f42136 2021-11-25 op
63 c9f42136 2021-11-25 op /* Paths to external programs. */
64 c9f42136 2021-11-25 op static char *user_shell;
65 c9f42136 2021-11-25 op static char *user_pager;
66 c9f42136 2021-11-25 op static char *user_editor;
67 c9f42136 2021-11-25 op static char *user_open;
68 c9f42136 2021-11-25 op
69 c9f42136 2021-11-25 op /* Listing view parameters. */
70 c9f42136 2021-11-25 op #define HEIGHT (LINES-4)
71 c9f42136 2021-11-25 op #define STATUSPOS (COLS-16)
72 c9f42136 2021-11-25 op
73 c9f42136 2021-11-25 op /* Listing view flags. */
74 c9f42136 2021-11-25 op #define SHOW_FILES 0x01u
75 c9f42136 2021-11-25 op #define SHOW_DIRS 0x02u
76 c9f42136 2021-11-25 op #define SHOW_HIDDEN 0x04u
77 c9f42136 2021-11-25 op
78 c9f42136 2021-11-25 op /* Marks parameters. */
79 c9f42136 2021-11-25 op #define BULK_INIT 5
80 c9f42136 2021-11-25 op #define BULK_THRESH 256
81 c9f42136 2021-11-25 op
82 c9f42136 2021-11-25 op /* Information associated to each entry in listing. */
83 3a4fbb34 2021-11-26 op struct row {
84 c66e2de7 2021-11-25 op char *name;
85 c66e2de7 2021-11-25 op off_t size;
86 c66e2de7 2021-11-25 op mode_t mode;
87 c66e2de7 2021-11-25 op int islink;
88 c66e2de7 2021-11-25 op int marked;
89 3a4fbb34 2021-11-26 op };
90 c9f42136 2021-11-25 op
91 c9f42136 2021-11-25 op /* Dynamic array of marked entries. */
92 3a4fbb34 2021-11-26 op struct marks {
93 c66e2de7 2021-11-25 op char dirpath[PATH_MAX];
94 c66e2de7 2021-11-25 op int bulk;
95 c66e2de7 2021-11-25 op int nentries;
96 c66e2de7 2021-11-25 op char **entries;
97 3a4fbb34 2021-11-26 op };
98 c9f42136 2021-11-25 op
99 c9f42136 2021-11-25 op /* Line editing state. */
100 3a4fbb34 2021-11-26 op struct edit {
101 c66e2de7 2021-11-25 op wchar_t buffer[BUFLEN + 1];
102 c66e2de7 2021-11-25 op int left, right;
103 3a4fbb34 2021-11-26 op };
104 c9f42136 2021-11-25 op
105 c9f42136 2021-11-25 op /* Each tab only stores the following information. */
106 3a4fbb34 2021-11-26 op struct tab {
107 c66e2de7 2021-11-25 op int scroll;
108 c66e2de7 2021-11-25 op int esel;
109 c66e2de7 2021-11-25 op uint8_t flags;
110 c66e2de7 2021-11-25 op char cwd[PATH_MAX];
111 3a4fbb34 2021-11-26 op };
112 c9f42136 2021-11-25 op
113 3a4fbb34 2021-11-26 op struct prog {
114 c66e2de7 2021-11-25 op off_t partial;
115 c66e2de7 2021-11-25 op off_t total;
116 c66e2de7 2021-11-25 op const char *msg;
117 3a4fbb34 2021-11-26 op };
118 c9f42136 2021-11-25 op
119 c9f42136 2021-11-25 op /* Global state. */
120 c3e1e821 2021-11-26 op static struct state {
121 c66e2de7 2021-11-25 op int tab;
122 c66e2de7 2021-11-25 op int nfiles;
123 3a4fbb34 2021-11-26 op struct row *rows;
124 c66e2de7 2021-11-25 op WINDOW *window;
125 3a4fbb34 2021-11-26 op struct marks marks;
126 3a4fbb34 2021-11-26 op struct edit edit;
127 c66e2de7 2021-11-25 op int edit_scroll;
128 c66e2de7 2021-11-25 op volatile sig_atomic_t pending_usr1;
129 c66e2de7 2021-11-25 op volatile sig_atomic_t pending_winch;
130 3a4fbb34 2021-11-26 op struct prog prog;
131 3a4fbb34 2021-11-26 op struct tab tabs[10];
132 c3e1e821 2021-11-26 op } fm;
133 c9f42136 2021-11-25 op
134 c9f42136 2021-11-25 op /* Macros for accessing global state. */
135 c3e1e821 2021-11-26 op #define ENAME(I) fm.rows[I].name
136 c3e1e821 2021-11-26 op #define ESIZE(I) fm.rows[I].size
137 c3e1e821 2021-11-26 op #define EMODE(I) fm.rows[I].mode
138 c3e1e821 2021-11-26 op #define ISLINK(I) fm.rows[I].islink
139 c3e1e821 2021-11-26 op #define MARKED(I) fm.rows[I].marked
140 c3e1e821 2021-11-26 op #define SCROLL fm.tabs[fm.tab].scroll
141 c3e1e821 2021-11-26 op #define ESEL fm.tabs[fm.tab].esel
142 c3e1e821 2021-11-26 op #define FLAGS fm.tabs[fm.tab].flags
143 c3e1e821 2021-11-26 op #define CWD fm.tabs[fm.tab].cwd
144 c9f42136 2021-11-25 op
145 c9f42136 2021-11-25 op /* Helpers. */
146 c9f42136 2021-11-25 op #define MIN(A, B) ((A) < (B) ? (A) : (B))
147 c9f42136 2021-11-25 op #define MAX(A, B) ((A) > (B) ? (A) : (B))
148 c9f42136 2021-11-25 op #define ISDIR(E) (strchr((E), '/') != NULL)
149 c71d4135 2021-12-01 op #define CTRL(x) ((x) & 0x1f)
150 126b027d 2021-12-01 op #define nitems(a) (sizeof(a)/sizeof(a[0]))
151 c9f42136 2021-11-25 op
152 c9f42136 2021-11-25 op /* Line Editing Macros. */
153 c9f42136 2021-11-25 op #define EDIT_FULL(E) ((E).left == (E).right)
154 c9f42136 2021-11-25 op #define EDIT_CAN_LEFT(E) ((E).left)
155 c9f42136 2021-11-25 op #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
156 c9f42136 2021-11-25 op #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
157 c9f42136 2021-11-25 op #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
158 c9f42136 2021-11-25 op #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
159 c9f42136 2021-11-25 op #define EDIT_BACKSPACE(E) (E).left--
160 c9f42136 2021-11-25 op #define EDIT_DELETE(E) (E).right++
161 c9f42136 2021-11-25 op #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
162 c9f42136 2021-11-25 op
163 f92b5d94 2021-11-26 op enum editstate { CONTINUE, CONFIRM, CANCEL };
164 f92b5d94 2021-11-26 op enum color { DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK };
165 f92b5d94 2021-11-26 op
166 c9f42136 2021-11-25 op typedef int (*PROCESS)(const char *path);
167 c9f42136 2021-11-25 op
168 dbbe06a3 2021-11-26 op #ifndef __dead
169 dbbe06a3 2021-11-26 op #define __dead __attribute__((noreturn))
170 dbbe06a3 2021-11-26 op #endif
171 dbbe06a3 2021-11-26 op
172 85f3c2a1 2021-12-01 op static inline __dead void
173 dbbe06a3 2021-11-26 op quit(const char *reason)
174 dbbe06a3 2021-11-26 op {
175 dbbe06a3 2021-11-26 op int saved_errno;
176 dbbe06a3 2021-11-26 op
177 dbbe06a3 2021-11-26 op saved_errno = errno;
178 dbbe06a3 2021-11-26 op endwin();
179 dbbe06a3 2021-11-26 op nocbreak();
180 dbbe06a3 2021-11-26 op fflush(stderr);
181 dbbe06a3 2021-11-26 op errno = saved_errno;
182 dbbe06a3 2021-11-26 op err(1, "%s", reason);
183 dbbe06a3 2021-11-26 op }
184 dbbe06a3 2021-11-26 op
185 dbbe06a3 2021-11-26 op static inline void *
186 dbbe06a3 2021-11-26 op xmalloc(size_t size)
187 dbbe06a3 2021-11-26 op {
188 dbbe06a3 2021-11-26 op void *d;
189 dbbe06a3 2021-11-26 op
190 dbbe06a3 2021-11-26 op if ((d = malloc(size)) == NULL)
191 dbbe06a3 2021-11-26 op quit("malloc");
192 dbbe06a3 2021-11-26 op return d;
193 dbbe06a3 2021-11-26 op }
194 dbbe06a3 2021-11-26 op
195 dbbe06a3 2021-11-26 op static inline void *
196 dbbe06a3 2021-11-26 op xcalloc(size_t nmemb, size_t size)
197 dbbe06a3 2021-11-26 op {
198 dbbe06a3 2021-11-26 op void *d;
199 dbbe06a3 2021-11-26 op
200 dbbe06a3 2021-11-26 op if ((d = calloc(nmemb, size)) == NULL)
201 dbbe06a3 2021-11-26 op quit("calloc");
202 dbbe06a3 2021-11-26 op return d;
203 dbbe06a3 2021-11-26 op }
204 dbbe06a3 2021-11-26 op
205 dbbe06a3 2021-11-26 op static inline void *
206 dbbe06a3 2021-11-26 op xrealloc(void *p, size_t size)
207 dbbe06a3 2021-11-26 op {
208 dbbe06a3 2021-11-26 op void *d;
209 dbbe06a3 2021-11-26 op
210 d80d006a 2021-12-01 op if ((d = realloc(p, size)) == NULL)
211 dbbe06a3 2021-11-26 op quit("realloc");
212 dbbe06a3 2021-11-26 op return d;
213 dbbe06a3 2021-11-26 op }
214 dbbe06a3 2021-11-26 op
215 acf1203a 2023-02-08 op static inline int
216 acf1203a 2023-02-08 op xasprintf(char **ret, const char *fmt, ...)
217 acf1203a 2023-02-08 op {
218 acf1203a 2023-02-08 op va_list ap;
219 acf1203a 2023-02-08 op int r;
220 acf1203a 2023-02-08 op
221 acf1203a 2023-02-08 op va_start(ap, fmt);
222 acf1203a 2023-02-08 op r = vasprintf(ret, fmt, ap);
223 acf1203a 2023-02-08 op va_end(ap);
224 acf1203a 2023-02-08 op
225 acf1203a 2023-02-08 op if (r == -1)
226 acf1203a 2023-02-08 op quit("asprintf");
227 acf1203a 2023-02-08 op return r;
228 acf1203a 2023-02-08 op }
229 acf1203a 2023-02-08 op
230 acf1203a 2023-02-08 op static inline char *
231 acf1203a 2023-02-08 op xstrdup(char *str)
232 acf1203a 2023-02-08 op {
233 acf1203a 2023-02-08 op char *s;
234 acf1203a 2023-02-08 op
235 acf1203a 2023-02-08 op if ((s = strdup(str)) == NULL)
236 acf1203a 2023-02-08 op quit("strdup");
237 acf1203a 2023-02-08 op return s;
238 acf1203a 2023-02-08 op }
239 acf1203a 2023-02-08 op
240 c9f42136 2021-11-25 op static void
241 3a4fbb34 2021-11-26 op init_marks(struct marks *marks)
242 c9f42136 2021-11-25 op {
243 acf1203a 2023-02-08 op strlcpy(marks->dirpath, "", sizeof(marks->dirpath));
244 c66e2de7 2021-11-25 op marks->bulk = BULK_INIT;
245 c66e2de7 2021-11-25 op marks->nentries = 0;
246 06e3ae5b 2021-11-26 op marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
247 c9f42136 2021-11-25 op }
248 c9f42136 2021-11-25 op
249 c9f42136 2021-11-25 op /* Unmark all entries. */
250 c9f42136 2021-11-25 op static void
251 3a4fbb34 2021-11-26 op mark_none(struct marks *marks)
252 c9f42136 2021-11-25 op {
253 c66e2de7 2021-11-25 op int i;
254 c9f42136 2021-11-25 op
255 acf1203a 2023-02-08 op strlcpy(marks->dirpath, "", sizeof(marks->dirpath));
256 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk && marks->nentries; i++)
257 c66e2de7 2021-11-25 op if (marks->entries[i]) {
258 c66e2de7 2021-11-25 op free(marks->entries[i]);
259 c66e2de7 2021-11-25 op marks->entries[i] = NULL;
260 c66e2de7 2021-11-25 op marks->nentries--;
261 c66e2de7 2021-11-25 op }
262 c66e2de7 2021-11-25 op if (marks->bulk > BULK_THRESH) {
263 c66e2de7 2021-11-25 op /* Reset bulk to free some memory. */
264 c66e2de7 2021-11-25 op free(marks->entries);
265 c66e2de7 2021-11-25 op marks->bulk = BULK_INIT;
266 06e3ae5b 2021-11-26 op marks->entries = xcalloc(marks->bulk, sizeof(*marks->entries));
267 c66e2de7 2021-11-25 op }
268 c9f42136 2021-11-25 op }
269 c9f42136 2021-11-25 op
270 c9f42136 2021-11-25 op static void
271 3a4fbb34 2021-11-26 op add_mark(struct marks *marks, char *dirpath, char *entry)
272 c9f42136 2021-11-25 op {
273 c66e2de7 2021-11-25 op int i;
274 c9f42136 2021-11-25 op
275 c66e2de7 2021-11-25 op if (!strcmp(marks->dirpath, dirpath)) {
276 c66e2de7 2021-11-25 op /* Append mark to directory. */
277 c66e2de7 2021-11-25 op if (marks->nentries == marks->bulk) {
278 c66e2de7 2021-11-25 op /* Expand bulk to accomodate new entry. */
279 c66e2de7 2021-11-25 op int extra = marks->bulk / 2;
280 c66e2de7 2021-11-25 op marks->bulk += extra; /* bulk *= 1.5; */
281 dbbe06a3 2021-11-26 op marks->entries = xrealloc(marks->entries,
282 06e3ae5b 2021-11-26 op marks->bulk * sizeof(*marks->entries));
283 c66e2de7 2021-11-25 op memset(&marks->entries[marks->nentries], 0,
284 06e3ae5b 2021-11-26 op extra * sizeof(*marks->entries));
285 c66e2de7 2021-11-25 op i = marks->nentries;
286 c66e2de7 2021-11-25 op } else {
287 c66e2de7 2021-11-25 op /* Search for empty slot (there must be one). */
288 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk; i++)
289 c66e2de7 2021-11-25 op if (!marks->entries[i])
290 c66e2de7 2021-11-25 op break;
291 c66e2de7 2021-11-25 op }
292 c66e2de7 2021-11-25 op } else {
293 c66e2de7 2021-11-25 op /* Directory changed. Discard old marks. */
294 c66e2de7 2021-11-25 op mark_none(marks);
295 acf1203a 2023-02-08 op strlcpy(marks->dirpath, dirpath, sizeof(marks->dirpath));
296 c66e2de7 2021-11-25 op i = 0;
297 c66e2de7 2021-11-25 op }
298 acf1203a 2023-02-08 op marks->entries[i] = xstrdup(entry);
299 c66e2de7 2021-11-25 op marks->nentries++;
300 c9f42136 2021-11-25 op }
301 c9f42136 2021-11-25 op
302 c9f42136 2021-11-25 op static void
303 3a4fbb34 2021-11-26 op del_mark(struct marks *marks, char *entry)
304 c9f42136 2021-11-25 op {
305 c66e2de7 2021-11-25 op int i;
306 c9f42136 2021-11-25 op
307 c66e2de7 2021-11-25 op if (marks->nentries > 1) {
308 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk; i++)
309 c66e2de7 2021-11-25 op if (marks->entries[i] &&
310 c66e2de7 2021-11-25 op !strcmp(marks->entries[i], entry))
311 c66e2de7 2021-11-25 op break;
312 c66e2de7 2021-11-25 op free(marks->entries[i]);
313 c66e2de7 2021-11-25 op marks->entries[i] = NULL;
314 c66e2de7 2021-11-25 op marks->nentries--;
315 c66e2de7 2021-11-25 op } else
316 c66e2de7 2021-11-25 op mark_none(marks);
317 c9f42136 2021-11-25 op }
318 c9f42136 2021-11-25 op
319 c9f42136 2021-11-25 op static void
320 3a4fbb34 2021-11-26 op free_marks(struct marks *marks)
321 c9f42136 2021-11-25 op {
322 c66e2de7 2021-11-25 op int i;
323 c9f42136 2021-11-25 op
324 c66e2de7 2021-11-25 op for (i = 0; i < marks->bulk && marks->nentries; i++)
325 c66e2de7 2021-11-25 op if (marks->entries[i]) {
326 c66e2de7 2021-11-25 op free(marks->entries[i]);
327 c66e2de7 2021-11-25 op marks->nentries--;
328 c66e2de7 2021-11-25 op }
329 c66e2de7 2021-11-25 op free(marks->entries);
330 c9f42136 2021-11-25 op }
331 c9f42136 2021-11-25 op
332 c9f42136 2021-11-25 op static void
333 c9f42136 2021-11-25 op handle_usr1(int sig)
334 c9f42136 2021-11-25 op {
335 c3e1e821 2021-11-26 op fm.pending_usr1 = 1;
336 c9f42136 2021-11-25 op }
337 c9f42136 2021-11-25 op
338 c9f42136 2021-11-25 op static void
339 c9f42136 2021-11-25 op handle_winch(int sig)
340 c9f42136 2021-11-25 op {
341 c3e1e821 2021-11-26 op fm.pending_winch = 1;
342 c9f42136 2021-11-25 op }
343 c9f42136 2021-11-25 op
344 c9f42136 2021-11-25 op static void
345 ae787901 2021-12-01 op enable_handlers(void)
346 c9f42136 2021-11-25 op {
347 c66e2de7 2021-11-25 op struct sigaction sa;
348 c9f42136 2021-11-25 op
349 c66e2de7 2021-11-25 op memset(&sa, 0, sizeof(struct sigaction));
350 c66e2de7 2021-11-25 op sa.sa_handler = handle_usr1;
351 c66e2de7 2021-11-25 op sigaction(SIGUSR1, &sa, NULL);
352 c66e2de7 2021-11-25 op sa.sa_handler = handle_winch;
353 c66e2de7 2021-11-25 op sigaction(SIGWINCH, &sa, NULL);
354 c9f42136 2021-11-25 op }
355 c9f42136 2021-11-25 op
356 c9f42136 2021-11-25 op static void
357 ae787901 2021-12-01 op disable_handlers(void)
358 c9f42136 2021-11-25 op {
359 c66e2de7 2021-11-25 op struct sigaction sa;
360 c9f42136 2021-11-25 op
361 c66e2de7 2021-11-25 op memset(&sa, 0, sizeof(struct sigaction));
362 c66e2de7 2021-11-25 op sa.sa_handler = SIG_DFL;
363 c66e2de7 2021-11-25 op sigaction(SIGUSR1, &sa, NULL);
364 c66e2de7 2021-11-25 op sigaction(SIGWINCH, &sa, NULL);
365 c9f42136 2021-11-25 op }
366 c9f42136 2021-11-25 op
367 ae787901 2021-12-01 op static void reload(void);
368 ae787901 2021-12-01 op static void update_view(void);
369 c9f42136 2021-11-25 op
370 c9f42136 2021-11-25 op /* Handle any signals received since last call. */
371 c9f42136 2021-11-25 op static void
372 ae787901 2021-12-01 op sync_signals(void)
373 c9f42136 2021-11-25 op {
374 c3e1e821 2021-11-26 op if (fm.pending_usr1) {
375 c66e2de7 2021-11-25 op /* SIGUSR1 received: refresh directory listing. */
376 c66e2de7 2021-11-25 op reload();
377 c3e1e821 2021-11-26 op fm.pending_usr1 = 0;
378 c66e2de7 2021-11-25 op }
379 c3e1e821 2021-11-26 op if (fm.pending_winch) {
380 c66e2de7 2021-11-25 op /* SIGWINCH received: resize application accordingly. */
381 c3e1e821 2021-11-26 op delwin(fm.window);
382 c66e2de7 2021-11-25 op endwin();
383 c66e2de7 2021-11-25 op refresh();
384 c66e2de7 2021-11-25 op clear();
385 c3e1e821 2021-11-26 op fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
386 c3e1e821 2021-11-26 op if (HEIGHT < fm.nfiles && SCROLL + HEIGHT > fm.nfiles)
387 c66e2de7 2021-11-25 op SCROLL = ESEL - HEIGHT;
388 c66e2de7 2021-11-25 op update_view();
389 c3e1e821 2021-11-26 op fm.pending_winch = 0;
390 c66e2de7 2021-11-25 op }
391 c9f42136 2021-11-25 op }
392 c9f42136 2021-11-25 op
393 c66e2de7 2021-11-25 op /*
394 c66e2de7 2021-11-25 op * This function must be used in place of getch(). It handles signals
395 c66e2de7 2021-11-25 op * while waiting for user input.
396 c66e2de7 2021-11-25 op */
397 c9f42136 2021-11-25 op static int
398 c3e1e821 2021-11-26 op fm_getch()
399 c9f42136 2021-11-25 op {
400 c66e2de7 2021-11-25 op int ch;
401 c9f42136 2021-11-25 op
402 c66e2de7 2021-11-25 op while ((ch = getch()) == ERR)
403 c66e2de7 2021-11-25 op sync_signals();
404 c66e2de7 2021-11-25 op return ch;
405 c9f42136 2021-11-25 op }
406 c9f42136 2021-11-25 op
407 c66e2de7 2021-11-25 op /*
408 c66e2de7 2021-11-25 op * This function must be used in place of get_wch(). It handles
409 c66e2de7 2021-11-25 op * signals while waiting for user input.
410 c66e2de7 2021-11-25 op */
411 c9f42136 2021-11-25 op static int
412 c3e1e821 2021-11-26 op fm_get_wch(wint_t *wch)
413 c9f42136 2021-11-25 op {
414 c66e2de7 2021-11-25 op wint_t ret;
415 c9f42136 2021-11-25 op
416 c66e2de7 2021-11-25 op while ((ret = get_wch(wch)) == (wint_t)ERR)
417 c66e2de7 2021-11-25 op sync_signals();
418 c66e2de7 2021-11-25 op return ret;
419 c9f42136 2021-11-25 op }
420 c9f42136 2021-11-25 op
421 c9f42136 2021-11-25 op /* Get user programs from the environment. */
422 c9f42136 2021-11-25 op
423 c3e1e821 2021-11-26 op #define FM_ENV(dst, src) if ((dst = getenv("FM_" #src)) == NULL) \
424 c66e2de7 2021-11-25 op dst = getenv(#src);
425 c9f42136 2021-11-25 op
426 c9f42136 2021-11-25 op static void
427 c9f42136 2021-11-25 op get_user_programs()
428 c9f42136 2021-11-25 op {
429 c3e1e821 2021-11-26 op FM_ENV(user_shell, SHELL);
430 c3e1e821 2021-11-26 op FM_ENV(user_pager, PAGER);
431 c3e1e821 2021-11-26 op FM_ENV(user_editor, VISUAL);
432 c66e2de7 2021-11-25 op if (!user_editor)
433 c3e1e821 2021-11-26 op FM_ENV(user_editor, EDITOR);
434 c3e1e821 2021-11-26 op FM_ENV(user_open, OPEN);
435 c9f42136 2021-11-25 op }
436 c9f42136 2021-11-25 op
437 c9f42136 2021-11-25 op /* Do a fork-exec to external program (e.g. $EDITOR). */
438 c9f42136 2021-11-25 op static void
439 126b027d 2021-12-01 op spawn(const char *argv0, ...)
440 c9f42136 2021-11-25 op {
441 c66e2de7 2021-11-25 op pid_t pid;
442 c66e2de7 2021-11-25 op int status;
443 126b027d 2021-12-01 op size_t i;
444 126b027d 2021-12-01 op const char *argv[16], *last;
445 126b027d 2021-12-01 op va_list ap;
446 bdf4854f 2021-12-01 op
447 bdf4854f 2021-12-01 op memset(argv, 0, sizeof(argv));
448 c9f42136 2021-11-25 op
449 126b027d 2021-12-01 op va_start(ap, argv0);
450 126b027d 2021-12-01 op argv[0] = argv0;
451 126b027d 2021-12-01 op for (i = 1; i < nitems(argv); ++i) {
452 126b027d 2021-12-01 op last = va_arg(ap, const char *);
453 126b027d 2021-12-01 op if (last == NULL)
454 126b027d 2021-12-01 op break;
455 126b027d 2021-12-01 op argv[i] = last;
456 126b027d 2021-12-01 op }
457 126b027d 2021-12-01 op va_end(ap);
458 126b027d 2021-12-01 op
459 126b027d 2021-12-01 op if (last != NULL)
460 126b027d 2021-12-01 op abort();
461 126b027d 2021-12-01 op
462 126b027d 2021-12-01 op disable_handlers();
463 126b027d 2021-12-01 op endwin();
464 126b027d 2021-12-01 op
465 126b027d 2021-12-01 op switch (pid = fork()) {
466 126b027d 2021-12-01 op case -1:
467 126b027d 2021-12-01 op quit("fork");
468 126b027d 2021-12-01 op case 0: /* child */
469 126b027d 2021-12-01 op setenv("RVSEL", fm.nfiles ? ENAME(ESEL) : "", 1);
470 126b027d 2021-12-01 op execvp(argv[0], (char *const *)argv);
471 126b027d 2021-12-01 op quit("execvp");
472 126b027d 2021-12-01 op default:
473 c66e2de7 2021-11-25 op waitpid(pid, &status, 0);
474 c66e2de7 2021-11-25 op enable_handlers();
475 c66e2de7 2021-11-25 op kill(getpid(), SIGWINCH);
476 c66e2de7 2021-11-25 op }
477 c9f42136 2021-11-25 op }
478 c9f42136 2021-11-25 op
479 c9f42136 2021-11-25 op static void
480 c9f42136 2021-11-25 op shell_escaped_cat(char *buf, char *str, size_t n)
481 c9f42136 2021-11-25 op {
482 c66e2de7 2021-11-25 op char *p = buf + strlen(buf);
483 c66e2de7 2021-11-25 op *p++ = '\'';
484 c66e2de7 2021-11-25 op for (n--; n; n--, str++) {
485 c66e2de7 2021-11-25 op switch (*str) {
486 c66e2de7 2021-11-25 op case '\'':
487 c66e2de7 2021-11-25 op if (n < 4)
488 c66e2de7 2021-11-25 op goto done;
489 acf1203a 2023-02-08 op strlcpy(p, "'\\''", n);
490 c66e2de7 2021-11-25 op n -= 4;
491 c66e2de7 2021-11-25 op p += 4;
492 c66e2de7 2021-11-25 op break;
493 c66e2de7 2021-11-25 op case '\0':
494 c66e2de7 2021-11-25 op goto done;
495 c66e2de7 2021-11-25 op default:
496 c66e2de7 2021-11-25 op *p = *str;
497 c66e2de7 2021-11-25 op p++;
498 c66e2de7 2021-11-25 op }
499 c66e2de7 2021-11-25 op }
500 c9f42136 2021-11-25 op done:
501 c66e2de7 2021-11-25 op strncat(p, "'", n);
502 c9f42136 2021-11-25 op }
503 c9f42136 2021-11-25 op
504 c9f42136 2021-11-25 op static int
505 c9f42136 2021-11-25 op open_with_env(char *program, char *path)
506 c9f42136 2021-11-25 op {
507 c66e2de7 2021-11-25 op if (program) {
508 c9f42136 2021-11-25 op #ifdef RV_SHELL
509 c66e2de7 2021-11-25 op strncpy(BUF1, program, BUFLEN - 1);
510 c66e2de7 2021-11-25 op strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
511 c66e2de7 2021-11-25 op shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
512 126b027d 2021-12-01 op spawn(RV_SHELL, "-c", BUF1, NULL );
513 c9f42136 2021-11-25 op #else
514 126b027d 2021-12-01 op spawn(program, path, NULL);
515 c9f42136 2021-11-25 op #endif
516 c66e2de7 2021-11-25 op return 1;
517 c66e2de7 2021-11-25 op }
518 c66e2de7 2021-11-25 op return 0;
519 c9f42136 2021-11-25 op }
520 c9f42136 2021-11-25 op
521 c9f42136 2021-11-25 op /* Curses setup. */
522 c9f42136 2021-11-25 op static void
523 c9f42136 2021-11-25 op init_term()
524 c9f42136 2021-11-25 op {
525 c66e2de7 2021-11-25 op setlocale(LC_ALL, "");
526 c66e2de7 2021-11-25 op initscr();
527 4f68a4bd 2021-12-12 op raw();
528 c66e2de7 2021-11-25 op timeout(100); /* For getch(). */
529 c66e2de7 2021-11-25 op noecho();
530 c66e2de7 2021-11-25 op nonl(); /* No NL->CR/NL on output. */
531 c66e2de7 2021-11-25 op intrflush(stdscr, FALSE);
532 c66e2de7 2021-11-25 op keypad(stdscr, TRUE);
533 c66e2de7 2021-11-25 op curs_set(FALSE); /* Hide blinking cursor. */
534 c66e2de7 2021-11-25 op if (has_colors()) {
535 c66e2de7 2021-11-25 op short bg;
536 c66e2de7 2021-11-25 op start_color();
537 c9f42136 2021-11-25 op #ifdef NCURSES_EXT_FUNCS
538 c66e2de7 2021-11-25 op use_default_colors();
539 c66e2de7 2021-11-25 op bg = -1;
540 c9f42136 2021-11-25 op #else
541 c66e2de7 2021-11-25 op bg = COLOR_BLACK;
542 c9f42136 2021-11-25 op #endif
543 c66e2de7 2021-11-25 op init_pair(RED, COLOR_RED, bg);
544 c66e2de7 2021-11-25 op init_pair(GREEN, COLOR_GREEN, bg);
545 c66e2de7 2021-11-25 op init_pair(YELLOW, COLOR_YELLOW, bg);
546 c66e2de7 2021-11-25 op init_pair(BLUE, COLOR_BLUE, bg);
547 c66e2de7 2021-11-25 op init_pair(CYAN, COLOR_CYAN, bg);
548 c66e2de7 2021-11-25 op init_pair(MAGENTA, COLOR_MAGENTA, bg);
549 c66e2de7 2021-11-25 op init_pair(WHITE, COLOR_WHITE, bg);
550 c66e2de7 2021-11-25 op init_pair(BLACK, COLOR_BLACK, bg);
551 c66e2de7 2021-11-25 op }
552 c66e2de7 2021-11-25 op atexit((void (*)(void))endwin);
553 c66e2de7 2021-11-25 op enable_handlers();
554 c9f42136 2021-11-25 op }
555 c9f42136 2021-11-25 op
556 c9f42136 2021-11-25 op /* Update the listing view. */
557 c9f42136 2021-11-25 op static void
558 c9f42136 2021-11-25 op update_view()
559 c9f42136 2021-11-25 op {
560 c66e2de7 2021-11-25 op int i, j;
561 c66e2de7 2021-11-25 op int numsize;
562 c66e2de7 2021-11-25 op int ishidden;
563 c66e2de7 2021-11-25 op int marking;
564 c9f42136 2021-11-25 op
565 c66e2de7 2021-11-25 op mvhline(0, 0, ' ', COLS);
566 c66e2de7 2021-11-25 op attr_on(A_BOLD, NULL);
567 c66e2de7 2021-11-25 op color_set(RVC_TABNUM, NULL);
568 c3e1e821 2021-11-26 op mvaddch(0, COLS - 2, fm.tab + '0');
569 c66e2de7 2021-11-25 op attr_off(A_BOLD, NULL);
570 c3e1e821 2021-11-26 op if (fm.marks.nentries) {
571 c3e1e821 2021-11-26 op numsize = snprintf(BUF1, BUFLEN, "%d", fm.marks.nentries);
572 c66e2de7 2021-11-25 op color_set(RVC_MARKS, NULL);
573 c66e2de7 2021-11-25 op mvaddstr(0, COLS - 3 - numsize, BUF1);
574 c66e2de7 2021-11-25 op } else
575 c66e2de7 2021-11-25 op numsize = -1;
576 c66e2de7 2021-11-25 op color_set(RVC_CWD, NULL);
577 c66e2de7 2021-11-25 op mbstowcs(WBUF, CWD, PATH_MAX);
578 c66e2de7 2021-11-25 op mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
579 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_BORDER, NULL);
580 c3e1e821 2021-11-26 op wborder(fm.window, 0, 0, 0, 0, 0, 0, 0, 0);
581 c3e1e821 2021-11-26 op ESEL = MAX(MIN(ESEL, fm.nfiles - 1), 0);
582 c66e2de7 2021-11-25 op
583 c66e2de7 2021-11-25 op /*
584 c66e2de7 2021-11-25 op * Selection might not be visible, due to cursor wrapping or
585 c66e2de7 2021-11-25 op * window shrinking. In that case, the scroll must be moved to
586 c66e2de7 2021-11-25 op * make it visible.
587 c66e2de7 2021-11-25 op */
588 c3e1e821 2021-11-26 op if (fm.nfiles > HEIGHT) {
589 c66e2de7 2021-11-25 op SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
590 c3e1e821 2021-11-26 op SCROLL = MIN(MAX(SCROLL, 0), fm.nfiles - HEIGHT);
591 c66e2de7 2021-11-25 op } else
592 c66e2de7 2021-11-25 op SCROLL = 0;
593 c3e1e821 2021-11-26 op marking = !strcmp(CWD, fm.marks.dirpath);
594 c3e1e821 2021-11-26 op for (i = 0, j = SCROLL; i < HEIGHT && j < fm.nfiles; i++, j++) {
595 c66e2de7 2021-11-25 op ishidden = ENAME(j)[0] == '.';
596 c66e2de7 2021-11-25 op if (j == ESEL)
597 c3e1e821 2021-11-26 op wattr_on(fm.window, A_REVERSE, NULL);
598 c66e2de7 2021-11-25 op if (ISLINK(j))
599 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_LINK, NULL);
600 c66e2de7 2021-11-25 op else if (ishidden)
601 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_HIDDEN, NULL);
602 c66e2de7 2021-11-25 op else if (S_ISREG(EMODE(j))) {
603 c66e2de7 2021-11-25 op if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
604 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_EXEC, NULL);
605 c66e2de7 2021-11-25 op else
606 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_REG, NULL);
607 c66e2de7 2021-11-25 op } else if (S_ISDIR(EMODE(j)))
608 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_DIR, NULL);
609 c66e2de7 2021-11-25 op else if (S_ISCHR(EMODE(j)))
610 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_CHR, NULL);
611 c66e2de7 2021-11-25 op else if (S_ISBLK(EMODE(j)))
612 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_BLK, NULL);
613 c66e2de7 2021-11-25 op else if (S_ISFIFO(EMODE(j)))
614 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_FIFO, NULL);
615 c66e2de7 2021-11-25 op else if (S_ISSOCK(EMODE(j)))
616 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_SOCK, NULL);
617 c66e2de7 2021-11-25 op if (S_ISDIR(EMODE(j))) {
618 c66e2de7 2021-11-25 op mbstowcs(WBUF, ENAME(j), PATH_MAX);
619 c66e2de7 2021-11-25 op if (ISLINK(j))
620 acf1203a 2023-02-08 op wcslcat(WBUF, L"/", sizeof(WBUF));
621 c66e2de7 2021-11-25 op } else {
622 61ac2a4b 2021-12-01 op const char *suffix, *suffixes = "BKMGTPEZY";
623 c66e2de7 2021-11-25 op off_t human_size = ESIZE(j) * 10;
624 c66e2de7 2021-11-25 op int length = mbstowcs(WBUF, ENAME(j), PATH_MAX);
625 c66e2de7 2021-11-25 op int namecols = wcswidth(WBUF, length);
626 c66e2de7 2021-11-25 op for (suffix = suffixes; human_size >= 10240; suffix++)
627 c66e2de7 2021-11-25 op human_size = (human_size + 512) / 1024;
628 c66e2de7 2021-11-25 op if (*suffix == 'B')
629 c66e2de7 2021-11-25 op swprintf(WBUF + length, PATH_MAX - length,
630 c66e2de7 2021-11-25 op L"%*d %c",
631 c66e2de7 2021-11-25 op (int)(COLS - namecols - 6),
632 c66e2de7 2021-11-25 op (int)human_size / 10, *suffix);
633 c66e2de7 2021-11-25 op else
634 c66e2de7 2021-11-25 op swprintf(WBUF + length, PATH_MAX - length,
635 c66e2de7 2021-11-25 op L"%*d.%d %c",
636 c66e2de7 2021-11-25 op (int)(COLS - namecols - 8),
637 c66e2de7 2021-11-25 op (int)human_size / 10,
638 c66e2de7 2021-11-25 op (int)human_size % 10, *suffix);
639 c66e2de7 2021-11-25 op }
640 c3e1e821 2021-11-26 op mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
641 c3e1e821 2021-11-26 op mvwaddnwstr(fm.window, i + 1, 2, WBUF, COLS - 4);
642 c66e2de7 2021-11-25 op if (marking && MARKED(j)) {
643 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_MARKS, NULL);
644 c3e1e821 2021-11-26 op mvwaddch(fm.window, i + 1, 1, RVS_MARK);
645 c66e2de7 2021-11-25 op } else
646 c3e1e821 2021-11-26 op mvwaddch(fm.window, i + 1, 1, ' ');
647 c66e2de7 2021-11-25 op if (j == ESEL)
648 c3e1e821 2021-11-26 op wattr_off(fm.window, A_REVERSE, NULL);
649 c66e2de7 2021-11-25 op }
650 c66e2de7 2021-11-25 op for (; i < HEIGHT; i++)
651 c3e1e821 2021-11-26 op mvwhline(fm.window, i + 1, 1, ' ', COLS - 2);
652 c3e1e821 2021-11-26 op if (fm.nfiles > HEIGHT) {
653 c66e2de7 2021-11-25 op int center, height;
654 c3e1e821 2021-11-26 op center = (SCROLL + HEIGHT / 2) * HEIGHT / fm.nfiles;
655 c3e1e821 2021-11-26 op height = (HEIGHT - 1) * HEIGHT / fm.nfiles;
656 c66e2de7 2021-11-25 op if (!height)
657 c66e2de7 2021-11-25 op height = 1;
658 c3e1e821 2021-11-26 op wcolor_set(fm.window, RVC_SCROLLBAR, NULL);
659 c3e1e821 2021-11-26 op mvwvline(fm.window, center - height/2 + 1, COLS - 1,
660 c66e2de7 2021-11-25 op RVS_SCROLLBAR, height);
661 c66e2de7 2021-11-25 op }
662 c66e2de7 2021-11-25 op BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
663 c66e2de7 2021-11-25 op BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
664 c66e2de7 2021-11-25 op BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
665 c3e1e821 2021-11-26 op if (!fm.nfiles)
666 acf1203a 2023-02-08 op strlcpy(BUF2, "0/0", sizeof(BUF2));
667 c66e2de7 2021-11-25 op else
668 c3e1e821 2021-11-26 op snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, fm.nfiles);
669 c66e2de7 2021-11-25 op snprintf(BUF1 + 3, BUFLEN - 3, "%12s", BUF2);
670 c66e2de7 2021-11-25 op color_set(RVC_STATUS, NULL);
671 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, STATUSPOS, BUF1);
672 c3e1e821 2021-11-26 op wrefresh(fm.window);
673 c9f42136 2021-11-25 op }
674 c9f42136 2021-11-25 op
675 c9f42136 2021-11-25 op /* Show a message on the status bar. */
676 afd4e08d 2021-12-01 op static void __attribute__((format(printf, 2, 3)))
677 21c41be6 2021-12-01 op message(enum color c, const char *fmt, ...)
678 c9f42136 2021-11-25 op {
679 c66e2de7 2021-11-25 op int len, pos;
680 c66e2de7 2021-11-25 op va_list args;
681 c9f42136 2021-11-25 op
682 c66e2de7 2021-11-25 op va_start(args, fmt);
683 c66e2de7 2021-11-25 op vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
684 c66e2de7 2021-11-25 op va_end(args);
685 c66e2de7 2021-11-25 op len = strlen(BUF1);
686 c66e2de7 2021-11-25 op pos = (STATUSPOS - len) / 2;
687 c66e2de7 2021-11-25 op attr_on(A_BOLD, NULL);
688 f92b5d94 2021-11-26 op color_set(c, NULL);
689 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, pos, BUF1);
690 c66e2de7 2021-11-25 op color_set(DEFAULT, NULL);
691 c66e2de7 2021-11-25 op attr_off(A_BOLD, NULL);
692 c9f42136 2021-11-25 op }
693 c9f42136 2021-11-25 op
694 c9f42136 2021-11-25 op /* Clear message area, leaving only status info. */
695 c9f42136 2021-11-25 op static void
696 c9f42136 2021-11-25 op clear_message()
697 c9f42136 2021-11-25 op {
698 c66e2de7 2021-11-25 op mvhline(LINES - 1, 0, ' ', STATUSPOS);
699 c9f42136 2021-11-25 op }
700 c9f42136 2021-11-25 op
701 c9f42136 2021-11-25 op /* Comparison used to sort listing entries. */
702 c9f42136 2021-11-25 op static int
703 c9f42136 2021-11-25 op rowcmp(const void *a, const void *b)
704 c9f42136 2021-11-25 op {
705 c66e2de7 2021-11-25 op int isdir1, isdir2, cmpdir;
706 3a4fbb34 2021-11-26 op const struct row *r1 = a;
707 3a4fbb34 2021-11-26 op const struct row *r2 = b;
708 c66e2de7 2021-11-25 op isdir1 = S_ISDIR(r1->mode);
709 c66e2de7 2021-11-25 op isdir2 = S_ISDIR(r2->mode);
710 c66e2de7 2021-11-25 op cmpdir = isdir2 - isdir1;
711 c66e2de7 2021-11-25 op return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
712 c9f42136 2021-11-25 op }
713 c9f42136 2021-11-25 op
714 c9f42136 2021-11-25 op /* Get all entries in current working directory. */
715 c9f42136 2021-11-25 op static int
716 3a4fbb34 2021-11-26 op ls(struct row **rowsp, uint8_t flags)
717 c9f42136 2021-11-25 op {
718 c66e2de7 2021-11-25 op DIR *dp;
719 c66e2de7 2021-11-25 op struct dirent *ep;
720 c66e2de7 2021-11-25 op struct stat statbuf;
721 3a4fbb34 2021-11-26 op struct row *rows;
722 c66e2de7 2021-11-25 op int i, n;
723 c9f42136 2021-11-25 op
724 c66e2de7 2021-11-25 op if (!(dp = opendir(".")))
725 c66e2de7 2021-11-25 op return -1;
726 c66e2de7 2021-11-25 op n = -2; /* We don't want the entries "." and "..". */
727 c66e2de7 2021-11-25 op while (readdir(dp))
728 c66e2de7 2021-11-25 op n++;
729 c66e2de7 2021-11-25 op if (n == 0) {
730 c66e2de7 2021-11-25 op closedir(dp);
731 c66e2de7 2021-11-25 op return 0;
732 c66e2de7 2021-11-25 op }
733 c66e2de7 2021-11-25 op rewinddir(dp);
734 06e3ae5b 2021-11-26 op rows = xmalloc(n * sizeof(*rows));
735 c66e2de7 2021-11-25 op i = 0;
736 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
737 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
738 c66e2de7 2021-11-25 op continue;
739 c66e2de7 2021-11-25 op if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
740 c66e2de7 2021-11-25 op continue;
741 c66e2de7 2021-11-25 op lstat(ep->d_name, &statbuf);
742 c66e2de7 2021-11-25 op rows[i].islink = S_ISLNK(statbuf.st_mode);
743 c66e2de7 2021-11-25 op stat(ep->d_name, &statbuf);
744 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
745 c66e2de7 2021-11-25 op if (flags & SHOW_DIRS) {
746 acf1203a 2023-02-08 op xasprintf(&rows[i].name, "%s%s",
747 acf1203a 2023-02-08 op ep->d_name, rows[i].islink ? "" : "/");
748 c66e2de7 2021-11-25 op rows[i].mode = statbuf.st_mode;
749 c66e2de7 2021-11-25 op i++;
750 c66e2de7 2021-11-25 op }
751 c66e2de7 2021-11-25 op } else if (flags & SHOW_FILES) {
752 acf1203a 2023-02-08 op rows[i].name = xstrdup(ep->d_name);
753 c66e2de7 2021-11-25 op rows[i].size = statbuf.st_size;
754 c66e2de7 2021-11-25 op rows[i].mode = statbuf.st_mode;
755 c66e2de7 2021-11-25 op i++;
756 c66e2de7 2021-11-25 op }
757 c66e2de7 2021-11-25 op }
758 c66e2de7 2021-11-25 op n = i; /* Ignore unused space in array caused by filters. */
759 c66e2de7 2021-11-25 op qsort(rows, n, sizeof(*rows), rowcmp);
760 c66e2de7 2021-11-25 op closedir(dp);
761 c66e2de7 2021-11-25 op *rowsp = rows;
762 c66e2de7 2021-11-25 op return n;
763 c9f42136 2021-11-25 op }
764 c9f42136 2021-11-25 op
765 c9f42136 2021-11-25 op static void
766 3a4fbb34 2021-11-26 op free_rows(struct row **rowsp, int nfiles)
767 c9f42136 2021-11-25 op {
768 c66e2de7 2021-11-25 op int i;
769 c9f42136 2021-11-25 op
770 c66e2de7 2021-11-25 op for (i = 0; i < nfiles; i++)
771 c66e2de7 2021-11-25 op free((*rowsp)[i].name);
772 c66e2de7 2021-11-25 op free(*rowsp);
773 c66e2de7 2021-11-25 op *rowsp = NULL;
774 c9f42136 2021-11-25 op }
775 c9f42136 2021-11-25 op
776 c9f42136 2021-11-25 op /* Change working directory to the path in CWD. */
777 c9f42136 2021-11-25 op static void
778 c9f42136 2021-11-25 op cd(int reset)
779 c9f42136 2021-11-25 op {
780 c66e2de7 2021-11-25 op int i, j;
781 c9f42136 2021-11-25 op
782 c66e2de7 2021-11-25 op message(CYAN, "Loading \"%s\"...", CWD);
783 c66e2de7 2021-11-25 op refresh();
784 c66e2de7 2021-11-25 op if (chdir(CWD) == -1) {
785 c66e2de7 2021-11-25 op getcwd(CWD, PATH_MAX - 1);
786 c66e2de7 2021-11-25 op if (CWD[strlen(CWD) - 1] != '/')
787 acf1203a 2023-02-08 op strlcat(CWD, "/", sizeof(CWD));
788 c66e2de7 2021-11-25 op goto done;
789 c66e2de7 2021-11-25 op }
790 c66e2de7 2021-11-25 op if (reset)
791 c66e2de7 2021-11-25 op ESEL = SCROLL = 0;
792 c3e1e821 2021-11-26 op if (fm.nfiles)
793 c3e1e821 2021-11-26 op free_rows(&fm.rows, fm.nfiles);
794 c3e1e821 2021-11-26 op fm.nfiles = ls(&fm.rows, FLAGS);
795 c3e1e821 2021-11-26 op if (!strcmp(CWD, fm.marks.dirpath)) {
796 c3e1e821 2021-11-26 op for (i = 0; i < fm.nfiles; i++) {
797 c3e1e821 2021-11-26 op for (j = 0; j < fm.marks.bulk; j++)
798 c3e1e821 2021-11-26 op if (fm.marks.entries[j] &&
799 c3e1e821 2021-11-26 op !strcmp(fm.marks.entries[j], ENAME(i)))
800 c66e2de7 2021-11-25 op break;
801 c3e1e821 2021-11-26 op MARKED(i) = j < fm.marks.bulk;
802 c66e2de7 2021-11-25 op }
803 c66e2de7 2021-11-25 op } else
804 c3e1e821 2021-11-26 op for (i = 0; i < fm.nfiles; i++)
805 c66e2de7 2021-11-25 op MARKED(i) = 0;
806 c9f42136 2021-11-25 op done:
807 c66e2de7 2021-11-25 op clear_message();
808 c66e2de7 2021-11-25 op update_view();
809 c9f42136 2021-11-25 op }
810 c9f42136 2021-11-25 op
811 c9f42136 2021-11-25 op /* Select a target entry, if it is present. */
812 c9f42136 2021-11-25 op static void
813 c9f42136 2021-11-25 op try_to_sel(const char *target)
814 c9f42136 2021-11-25 op {
815 c66e2de7 2021-11-25 op ESEL = 0;
816 c66e2de7 2021-11-25 op if (!ISDIR(target))
817 c3e1e821 2021-11-26 op while ((ESEL + 1) < fm.nfiles && S_ISDIR(EMODE(ESEL)))
818 c66e2de7 2021-11-25 op ESEL++;
819 c3e1e821 2021-11-26 op while ((ESEL + 1) < fm.nfiles && strcoll(ENAME(ESEL), target) < 0)
820 c66e2de7 2021-11-25 op ESEL++;
821 c9f42136 2021-11-25 op }
822 c9f42136 2021-11-25 op
823 c9f42136 2021-11-25 op /* Reload CWD, but try to keep selection. */
824 c9f42136 2021-11-25 op static void
825 c9f42136 2021-11-25 op reload()
826 c9f42136 2021-11-25 op {
827 c3e1e821 2021-11-26 op if (fm.nfiles) {
828 acf1203a 2023-02-08 op strlcpy(INPUT, ENAME(ESEL), sizeof(INPUT));
829 c66e2de7 2021-11-25 op cd(0);
830 c66e2de7 2021-11-25 op try_to_sel(INPUT);
831 c66e2de7 2021-11-25 op update_view();
832 c66e2de7 2021-11-25 op } else
833 c66e2de7 2021-11-25 op cd(1);
834 c9f42136 2021-11-25 op }
835 c9f42136 2021-11-25 op
836 c9f42136 2021-11-25 op static off_t
837 c9f42136 2021-11-25 op count_dir(const char *path)
838 c9f42136 2021-11-25 op {
839 c66e2de7 2021-11-25 op DIR *dp;
840 c66e2de7 2021-11-25 op struct dirent *ep;
841 c66e2de7 2021-11-25 op struct stat statbuf;
842 c66e2de7 2021-11-25 op char subpath[PATH_MAX];
843 c66e2de7 2021-11-25 op off_t total;
844 c9f42136 2021-11-25 op
845 c66e2de7 2021-11-25 op if (!(dp = opendir(path)))
846 c66e2de7 2021-11-25 op return 0;
847 c66e2de7 2021-11-25 op total = 0;
848 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
849 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
850 c66e2de7 2021-11-25 op continue;
851 c66e2de7 2021-11-25 op snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
852 c66e2de7 2021-11-25 op lstat(subpath, &statbuf);
853 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
854 acf1203a 2023-02-08 op strlcat(subpath, "/", sizeof(subpath));
855 c66e2de7 2021-11-25 op total += count_dir(subpath);
856 c66e2de7 2021-11-25 op } else
857 c66e2de7 2021-11-25 op total += statbuf.st_size;
858 c66e2de7 2021-11-25 op }
859 c66e2de7 2021-11-25 op closedir(dp);
860 c66e2de7 2021-11-25 op return total;
861 c9f42136 2021-11-25 op }
862 c9f42136 2021-11-25 op
863 c9f42136 2021-11-25 op static off_t
864 c9f42136 2021-11-25 op count_marked()
865 c9f42136 2021-11-25 op {
866 c66e2de7 2021-11-25 op int i;
867 c66e2de7 2021-11-25 op char *entry;
868 c66e2de7 2021-11-25 op off_t total;
869 c66e2de7 2021-11-25 op struct stat statbuf;
870 c9f42136 2021-11-25 op
871 c66e2de7 2021-11-25 op total = 0;
872 c3e1e821 2021-11-26 op chdir(fm.marks.dirpath);
873 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
874 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
875 c66e2de7 2021-11-25 op if (entry) {
876 c66e2de7 2021-11-25 op if (ISDIR(entry)) {
877 c66e2de7 2021-11-25 op total += count_dir(entry);
878 c66e2de7 2021-11-25 op } else {
879 c66e2de7 2021-11-25 op lstat(entry, &statbuf);
880 c66e2de7 2021-11-25 op total += statbuf.st_size;
881 c66e2de7 2021-11-25 op }
882 c66e2de7 2021-11-25 op }
883 c66e2de7 2021-11-25 op }
884 c66e2de7 2021-11-25 op chdir(CWD);
885 c66e2de7 2021-11-25 op return total;
886 c9f42136 2021-11-25 op }
887 c9f42136 2021-11-25 op
888 c66e2de7 2021-11-25 op /*
889 c66e2de7 2021-11-25 op * Recursively process a source directory using CWD as destination
890 c66e2de7 2021-11-25 op * root. For each node (i.e. directory), do the following:
891 c66e2de7 2021-11-25 op *
892 c66e2de7 2021-11-25 op * 1. call pre(destination);
893 c66e2de7 2021-11-25 op * 2. call proc() on every child leaf (i.e. files);
894 c66e2de7 2021-11-25 op * 3. recurse into every child node;
895 c66e2de7 2021-11-25 op * 4. call pos(source).
896 c66e2de7 2021-11-25 op *
897 c66e2de7 2021-11-25 op * E.g. to move directory /src/ (and all its contents) inside /dst/:
898 acf1203a 2023-02-08 op * strlcpy(CWD, "/dst/", sizeof(CWD));
899 c66e2de7 2021-11-25 op * process_dir(adddir, movfile, deldir, "/src/");
900 c66e2de7 2021-11-25 op */
901 c9f42136 2021-11-25 op static int
902 c9f42136 2021-11-25 op process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
903 c9f42136 2021-11-25 op {
904 c66e2de7 2021-11-25 op int ret;
905 c66e2de7 2021-11-25 op DIR *dp;
906 c66e2de7 2021-11-25 op struct dirent *ep;
907 c66e2de7 2021-11-25 op struct stat statbuf;
908 c66e2de7 2021-11-25 op char subpath[PATH_MAX];
909 c9f42136 2021-11-25 op
910 c66e2de7 2021-11-25 op ret = 0;
911 c66e2de7 2021-11-25 op if (pre) {
912 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
913 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
914 acf1203a 2023-02-08 op strlcat(dstpath, path + strlen(fm.marks.dirpath),
915 acf1203a 2023-02-08 op sizeof(dstpath));
916 c66e2de7 2021-11-25 op ret |= pre(dstpath);
917 c66e2de7 2021-11-25 op }
918 c66e2de7 2021-11-25 op if (!(dp = opendir(path)))
919 c66e2de7 2021-11-25 op return -1;
920 c66e2de7 2021-11-25 op while ((ep = readdir(dp))) {
921 c66e2de7 2021-11-25 op if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
922 c66e2de7 2021-11-25 op continue;
923 c66e2de7 2021-11-25 op snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
924 c66e2de7 2021-11-25 op lstat(subpath, &statbuf);
925 c66e2de7 2021-11-25 op if (S_ISDIR(statbuf.st_mode)) {
926 c66e2de7 2021-11-25 op strcat(subpath, "/");
927 c66e2de7 2021-11-25 op ret |= process_dir(pre, proc, pos, subpath);
928 c66e2de7 2021-11-25 op } else
929 c66e2de7 2021-11-25 op ret |= proc(subpath);
930 c66e2de7 2021-11-25 op }
931 c66e2de7 2021-11-25 op closedir(dp);
932 c66e2de7 2021-11-25 op if (pos)
933 c66e2de7 2021-11-25 op ret |= pos(path);
934 c66e2de7 2021-11-25 op return ret;
935 c9f42136 2021-11-25 op }
936 c9f42136 2021-11-25 op
937 c66e2de7 2021-11-25 op /*
938 c66e2de7 2021-11-25 op * Process all marked entries using CWD as destination root. All
939 c66e2de7 2021-11-25 op * marked entries that are directories will be recursively processed.
940 c66e2de7 2021-11-25 op * See process_dir() for details on the parameters.
941 c66e2de7 2021-11-25 op */
942 c9f42136 2021-11-25 op static void
943 c66e2de7 2021-11-25 op process_marked(PROCESS pre, PROCESS proc, PROCESS pos, const char *msg_doing,
944 c66e2de7 2021-11-25 op const char *msg_done)
945 c66e2de7 2021-11-25 op {
946 c66e2de7 2021-11-25 op int i, ret;
947 c66e2de7 2021-11-25 op char *entry;
948 c66e2de7 2021-11-25 op char path[PATH_MAX];
949 c66e2de7 2021-11-25 op
950 c66e2de7 2021-11-25 op clear_message();
951 c66e2de7 2021-11-25 op message(CYAN, "%s...", msg_doing);
952 c66e2de7 2021-11-25 op refresh();
953 c3e1e821 2021-11-26 op fm.prog = (struct prog){0, count_marked(), msg_doing};
954 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
955 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
956 c66e2de7 2021-11-25 op if (entry) {
957 c66e2de7 2021-11-25 op ret = 0;
958 c3e1e821 2021-11-26 op snprintf(path, PATH_MAX, "%s%s", fm.marks.dirpath,
959 c66e2de7 2021-11-25 op entry);
960 c66e2de7 2021-11-25 op if (ISDIR(entry)) {
961 c66e2de7 2021-11-25 op if (!strncmp(path, CWD, strlen(path)))
962 c66e2de7 2021-11-25 op ret = -1;
963 c66e2de7 2021-11-25 op else
964 c66e2de7 2021-11-25 op ret = process_dir(pre, proc, pos, path);
965 c66e2de7 2021-11-25 op } else
966 c66e2de7 2021-11-25 op ret = proc(path);
967 c66e2de7 2021-11-25 op if (!ret) {
968 c3e1e821 2021-11-26 op del_mark(&fm.marks, entry);
969 c66e2de7 2021-11-25 op reload();
970 c66e2de7 2021-11-25 op }
971 c66e2de7 2021-11-25 op }
972 c66e2de7 2021-11-25 op }
973 c3e1e821 2021-11-26 op fm.prog.total = 0;
974 c66e2de7 2021-11-25 op reload();
975 c3e1e821 2021-11-26 op if (!fm.marks.nentries)
976 c66e2de7 2021-11-25 op message(GREEN, "%s all marked entries.", msg_done);
977 c66e2de7 2021-11-25 op else
978 c66e2de7 2021-11-25 op message(RED, "Some errors occured while %s.", msg_doing);
979 c66e2de7 2021-11-25 op RV_ALERT();
980 c66e2de7 2021-11-25 op }
981 c66e2de7 2021-11-25 op
982 c66e2de7 2021-11-25 op static void
983 c9f42136 2021-11-25 op update_progress(off_t delta)
984 c9f42136 2021-11-25 op {
985 c66e2de7 2021-11-25 op int percent;
986 c9f42136 2021-11-25 op
987 c3e1e821 2021-11-26 op if (!fm.prog.total)
988 c66e2de7 2021-11-25 op return;
989 c3e1e821 2021-11-26 op fm.prog.partial += delta;
990 c3e1e821 2021-11-26 op percent = (int)(fm.prog.partial * 100 / fm.prog.total);
991 c3e1e821 2021-11-26 op message(CYAN, "%s...%d%%", fm.prog.msg, percent);
992 c66e2de7 2021-11-25 op refresh();
993 c9f42136 2021-11-25 op }
994 c9f42136 2021-11-25 op
995 c9f42136 2021-11-25 op /* Wrappers for file operations. */
996 c66e2de7 2021-11-25 op static int
997 c66e2de7 2021-11-25 op delfile(const char *path)
998 c66e2de7 2021-11-25 op {
999 c66e2de7 2021-11-25 op int ret;
1000 c66e2de7 2021-11-25 op struct stat st;
1001 c9f42136 2021-11-25 op
1002 c66e2de7 2021-11-25 op ret = lstat(path, &st);
1003 c66e2de7 2021-11-25 op if (ret < 0)
1004 c66e2de7 2021-11-25 op return ret;
1005 c66e2de7 2021-11-25 op update_progress(st.st_size);
1006 c66e2de7 2021-11-25 op return unlink(path);
1007 c9f42136 2021-11-25 op }
1008 c66e2de7 2021-11-25 op
1009 c66e2de7 2021-11-25 op static int
1010 c66e2de7 2021-11-25 op addfile(const char *path)
1011 c66e2de7 2021-11-25 op {
1012 c66e2de7 2021-11-25 op /* Using creat(2) because mknod(2) doesn't seem to be portable. */
1013 c66e2de7 2021-11-25 op int ret;
1014 c9f42136 2021-11-25 op
1015 c66e2de7 2021-11-25 op ret = creat(path, 0644);
1016 c66e2de7 2021-11-25 op if (ret < 0)
1017 c66e2de7 2021-11-25 op return ret;
1018 c66e2de7 2021-11-25 op return close(ret);
1019 c9f42136 2021-11-25 op }
1020 c9f42136 2021-11-25 op
1021 c66e2de7 2021-11-25 op static int
1022 c66e2de7 2021-11-25 op cpyfile(const char *srcpath)
1023 c66e2de7 2021-11-25 op {
1024 c66e2de7 2021-11-25 op int src, dst, ret;
1025 c66e2de7 2021-11-25 op size_t size;
1026 c66e2de7 2021-11-25 op struct stat st;
1027 c66e2de7 2021-11-25 op char buf[BUFSIZ];
1028 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
1029 c66e2de7 2021-11-25 op
1030 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
1031 acf1203a 2023-02-08 op strlcat(dstpath, srcpath + strlen(fm.marks.dirpath), sizeof(dstpath));
1032 c66e2de7 2021-11-25 op ret = lstat(srcpath, &st);
1033 c66e2de7 2021-11-25 op if (ret < 0)
1034 c66e2de7 2021-11-25 op return ret;
1035 c66e2de7 2021-11-25 op if (S_ISLNK(st.st_mode)) {
1036 c66e2de7 2021-11-25 op ret = readlink(srcpath, BUF1, BUFLEN - 1);
1037 c66e2de7 2021-11-25 op if (ret < 0)
1038 c66e2de7 2021-11-25 op return ret;
1039 c66e2de7 2021-11-25 op BUF1[ret] = '\0';
1040 c66e2de7 2021-11-25 op ret = symlink(BUF1, dstpath);
1041 c66e2de7 2021-11-25 op } else {
1042 c66e2de7 2021-11-25 op ret = src = open(srcpath, O_RDONLY);
1043 c66e2de7 2021-11-25 op if (ret < 0)
1044 c66e2de7 2021-11-25 op return ret;
1045 c66e2de7 2021-11-25 op ret = dst = creat(dstpath, st.st_mode);
1046 c66e2de7 2021-11-25 op if (ret < 0)
1047 c66e2de7 2021-11-25 op return ret;
1048 c66e2de7 2021-11-25 op while ((size = read(src, buf, BUFSIZ)) > 0) {
1049 c66e2de7 2021-11-25 op write(dst, buf, size);
1050 c66e2de7 2021-11-25 op update_progress(size);
1051 c66e2de7 2021-11-25 op sync_signals();
1052 c66e2de7 2021-11-25 op }
1053 c66e2de7 2021-11-25 op close(src);
1054 c66e2de7 2021-11-25 op close(dst);
1055 c66e2de7 2021-11-25 op ret = 0;
1056 c66e2de7 2021-11-25 op }
1057 c66e2de7 2021-11-25 op return ret;
1058 c9f42136 2021-11-25 op }
1059 c9f42136 2021-11-25 op
1060 c66e2de7 2021-11-25 op static int
1061 c66e2de7 2021-11-25 op adddir(const char *path)
1062 c66e2de7 2021-11-25 op {
1063 c66e2de7 2021-11-25 op int ret;
1064 c66e2de7 2021-11-25 op struct stat st;
1065 c66e2de7 2021-11-25 op
1066 c66e2de7 2021-11-25 op ret = stat(CWD, &st);
1067 c66e2de7 2021-11-25 op if (ret < 0)
1068 c66e2de7 2021-11-25 op return ret;
1069 c66e2de7 2021-11-25 op return mkdir(path, st.st_mode);
1070 c9f42136 2021-11-25 op }
1071 c9f42136 2021-11-25 op
1072 c66e2de7 2021-11-25 op static int
1073 c66e2de7 2021-11-25 op movfile(const char *srcpath)
1074 c66e2de7 2021-11-25 op {
1075 c66e2de7 2021-11-25 op int ret;
1076 c66e2de7 2021-11-25 op struct stat st;
1077 c66e2de7 2021-11-25 op char dstpath[PATH_MAX];
1078 c66e2de7 2021-11-25 op
1079 acf1203a 2023-02-08 op strlcpy(dstpath, CWD, sizeof(dstpath));
1080 acf1203a 2023-02-08 op strlcat(dstpath, srcpath + strlen(fm.marks.dirpath), sizeof(dstpath));
1081 c66e2de7 2021-11-25 op ret = rename(srcpath, dstpath);
1082 c66e2de7 2021-11-25 op if (ret == 0) {
1083 c66e2de7 2021-11-25 op ret = lstat(dstpath, &st);
1084 c66e2de7 2021-11-25 op if (ret < 0)
1085 c66e2de7 2021-11-25 op return ret;
1086 c66e2de7 2021-11-25 op update_progress(st.st_size);
1087 c66e2de7 2021-11-25 op } else if (errno == EXDEV) {
1088 c66e2de7 2021-11-25 op ret = cpyfile(srcpath);
1089 c66e2de7 2021-11-25 op if (ret < 0)
1090 c66e2de7 2021-11-25 op return ret;
1091 c66e2de7 2021-11-25 op ret = unlink(srcpath);
1092 c66e2de7 2021-11-25 op }
1093 c66e2de7 2021-11-25 op return ret;
1094 c9f42136 2021-11-25 op }
1095 c9f42136 2021-11-25 op
1096 c9f42136 2021-11-25 op static void
1097 c9f42136 2021-11-25 op start_line_edit(const char *init_input)
1098 c9f42136 2021-11-25 op {
1099 c66e2de7 2021-11-25 op curs_set(TRUE);
1100 c66e2de7 2021-11-25 op strncpy(INPUT, init_input, BUFLEN);
1101 c3e1e821 2021-11-26 op fm.edit.left = mbstowcs(fm.edit.buffer, init_input, BUFLEN);
1102 c3e1e821 2021-11-26 op fm.edit.right = BUFLEN - 1;
1103 c3e1e821 2021-11-26 op fm.edit.buffer[BUFLEN] = L'\0';
1104 c3e1e821 2021-11-26 op fm.edit_scroll = 0;
1105 c9f42136 2021-11-25 op }
1106 c9f42136 2021-11-25 op
1107 c9f42136 2021-11-25 op /* Read input and change editing state accordingly. */
1108 f92b5d94 2021-11-26 op static enum editstate
1109 c9f42136 2021-11-25 op get_line_edit()
1110 c9f42136 2021-11-25 op {
1111 c66e2de7 2021-11-25 op wchar_t eraser, killer, wch;
1112 c66e2de7 2021-11-25 op int ret, length;
1113 c9f42136 2021-11-25 op
1114 c3e1e821 2021-11-26 op ret = fm_get_wch((wint_t *)&wch);
1115 c66e2de7 2021-11-25 op erasewchar(&eraser);
1116 c66e2de7 2021-11-25 op killwchar(&killer);
1117 c66e2de7 2021-11-25 op if (ret == KEY_CODE_YES) {
1118 c66e2de7 2021-11-25 op if (wch == KEY_ENTER) {
1119 c66e2de7 2021-11-25 op curs_set(FALSE);
1120 c66e2de7 2021-11-25 op return CONFIRM;
1121 c66e2de7 2021-11-25 op } else if (wch == KEY_LEFT) {
1122 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1123 c3e1e821 2021-11-26 op EDIT_LEFT(fm.edit);
1124 c66e2de7 2021-11-25 op } else if (wch == KEY_RIGHT) {
1125 c3e1e821 2021-11-26 op if (EDIT_CAN_RIGHT(fm.edit))
1126 c3e1e821 2021-11-26 op EDIT_RIGHT(fm.edit);
1127 c66e2de7 2021-11-25 op } else if (wch == KEY_UP) {
1128 c3e1e821 2021-11-26 op while (EDIT_CAN_LEFT(fm.edit))
1129 c3e1e821 2021-11-26 op EDIT_LEFT(fm.edit);
1130 c66e2de7 2021-11-25 op } else if (wch == KEY_DOWN) {
1131 c3e1e821 2021-11-26 op while (EDIT_CAN_RIGHT(fm.edit))
1132 c3e1e821 2021-11-26 op EDIT_RIGHT(fm.edit);
1133 c66e2de7 2021-11-25 op } else if (wch == KEY_BACKSPACE) {
1134 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1135 c3e1e821 2021-11-26 op EDIT_BACKSPACE(fm.edit);
1136 c66e2de7 2021-11-25 op } else if (wch == KEY_DC) {
1137 c3e1e821 2021-11-26 op if (EDIT_CAN_RIGHT(fm.edit))
1138 c3e1e821 2021-11-26 op EDIT_DELETE(fm.edit);
1139 c66e2de7 2021-11-25 op }
1140 c66e2de7 2021-11-25 op } else {
1141 c66e2de7 2021-11-25 op if (wch == L'\r' || wch == L'\n') {
1142 c66e2de7 2021-11-25 op curs_set(FALSE);
1143 c66e2de7 2021-11-25 op return CONFIRM;
1144 c66e2de7 2021-11-25 op } else if (wch == L'\t') {
1145 c66e2de7 2021-11-25 op curs_set(FALSE);
1146 c66e2de7 2021-11-25 op return CANCEL;
1147 c66e2de7 2021-11-25 op } else if (wch == eraser) {
1148 c3e1e821 2021-11-26 op if (EDIT_CAN_LEFT(fm.edit))
1149 c3e1e821 2021-11-26 op EDIT_BACKSPACE(fm.edit);
1150 c66e2de7 2021-11-25 op } else if (wch == killer) {
1151 c3e1e821 2021-11-26 op EDIT_CLEAR(fm.edit);
1152 c66e2de7 2021-11-25 op clear_message();
1153 c66e2de7 2021-11-25 op } else if (iswprint(wch)) {
1154 c3e1e821 2021-11-26 op if (!EDIT_FULL(fm.edit))
1155 c3e1e821 2021-11-26 op EDIT_INSERT(fm.edit, wch);
1156 c66e2de7 2021-11-25 op }
1157 c66e2de7 2021-11-25 op }
1158 c66e2de7 2021-11-25 op /* Encode edit contents in INPUT. */
1159 c3e1e821 2021-11-26 op fm.edit.buffer[fm.edit.left] = L'\0';
1160 c3e1e821 2021-11-26 op length = wcstombs(INPUT, fm.edit.buffer, BUFLEN);
1161 c3e1e821 2021-11-26 op wcstombs(&INPUT[length], &fm.edit.buffer[fm.edit.right + 1],
1162 c66e2de7 2021-11-25 op BUFLEN - length);
1163 c66e2de7 2021-11-25 op return CONTINUE;
1164 c9f42136 2021-11-25 op }
1165 c9f42136 2021-11-25 op
1166 c9f42136 2021-11-25 op /* Update line input on the screen. */
1167 c9f42136 2021-11-25 op static void
1168 f92b5d94 2021-11-26 op update_input(const char *prompt, enum color c)
1169 c9f42136 2021-11-25 op {
1170 c66e2de7 2021-11-25 op int plen, ilen, maxlen;
1171 c9f42136 2021-11-25 op
1172 c66e2de7 2021-11-25 op plen = strlen(prompt);
1173 c66e2de7 2021-11-25 op ilen = mbstowcs(NULL, INPUT, 0);
1174 c66e2de7 2021-11-25 op maxlen = STATUSPOS - plen - 2;
1175 c3e1e821 2021-11-26 op if (ilen - fm.edit_scroll < maxlen)
1176 c3e1e821 2021-11-26 op fm.edit_scroll = MAX(ilen - maxlen, 0);
1177 c3e1e821 2021-11-26 op else if (fm.edit.left > fm.edit_scroll + maxlen - 1)
1178 c3e1e821 2021-11-26 op fm.edit_scroll = fm.edit.left - maxlen;
1179 c3e1e821 2021-11-26 op else if (fm.edit.left < fm.edit_scroll)
1180 c3e1e821 2021-11-26 op fm.edit_scroll = MAX(fm.edit.left - maxlen, 0);
1181 c66e2de7 2021-11-25 op color_set(RVC_PROMPT, NULL);
1182 c66e2de7 2021-11-25 op mvaddstr(LINES - 1, 0, prompt);
1183 f92b5d94 2021-11-26 op color_set(c, NULL);
1184 c66e2de7 2021-11-25 op mbstowcs(WBUF, INPUT, COLS);
1185 c3e1e821 2021-11-26 op mvaddnwstr(LINES - 1, plen, &WBUF[fm.edit_scroll], maxlen);
1186 c3e1e821 2021-11-26 op mvaddch(LINES - 1, plen + MIN(ilen - fm.edit_scroll, maxlen + 1),
1187 c66e2de7 2021-11-25 op ' ');
1188 c66e2de7 2021-11-25 op color_set(DEFAULT, NULL);
1189 c3e1e821 2021-11-26 op if (fm.edit_scroll)
1190 c66e2de7 2021-11-25 op mvaddch(LINES - 1, plen - 1, '<');
1191 c3e1e821 2021-11-26 op if (ilen > fm.edit_scroll + maxlen)
1192 c66e2de7 2021-11-25 op mvaddch(LINES - 1, plen + maxlen, '>');
1193 c3e1e821 2021-11-26 op move(LINES - 1, plen + fm.edit.left - fm.edit_scroll);
1194 c9f42136 2021-11-25 op }
1195 c9f42136 2021-11-25 op
1196 c71d4135 2021-12-01 op static void
1197 a1bd5162 2021-12-01 op cmd_down(void)
1198 c71d4135 2021-12-01 op {
1199 a1bd5162 2021-12-01 op if (fm.nfiles)
1200 a1bd5162 2021-12-01 op ESEL = MIN(ESEL + 1, fm.nfiles - 1);
1201 a1bd5162 2021-12-01 op }
1202 c71d4135 2021-12-01 op
1203 a1bd5162 2021-12-01 op static void
1204 a1bd5162 2021-12-01 op cmd_up(void)
1205 a1bd5162 2021-12-01 op {
1206 a1bd5162 2021-12-01 op if (fm.nfiles)
1207 a1bd5162 2021-12-01 op ESEL = MAX(ESEL - 1, 0);
1208 a1bd5162 2021-12-01 op }
1209 c71d4135 2021-12-01 op
1210 a1bd5162 2021-12-01 op static void
1211 a1bd5162 2021-12-01 op cmd_scroll_down(void)
1212 a1bd5162 2021-12-01 op {
1213 a1bd5162 2021-12-01 op if (!fm.nfiles)
1214 a1bd5162 2021-12-01 op return;
1215 a1bd5162 2021-12-01 op ESEL = MIN(ESEL + HEIGHT, fm.nfiles - 1);
1216 a1bd5162 2021-12-01 op if (fm.nfiles > HEIGHT)
1217 a1bd5162 2021-12-01 op SCROLL = MIN(SCROLL + HEIGHT, fm.nfiles - HEIGHT);
1218 a1bd5162 2021-12-01 op }
1219 c9f5f229 2021-12-01 op
1220 a1bd5162 2021-12-01 op static void
1221 a1bd5162 2021-12-01 op cmd_scroll_up(void)
1222 a1bd5162 2021-12-01 op {
1223 a1bd5162 2021-12-01 op if (!fm.nfiles)
1224 a1bd5162 2021-12-01 op return;
1225 a1bd5162 2021-12-01 op ESEL = MAX(ESEL - HEIGHT, 0);
1226 a1bd5162 2021-12-01 op SCROLL = MAX(SCROLL - HEIGHT, 0);
1227 a1bd5162 2021-12-01 op }
1228 c71d4135 2021-12-01 op
1229 a1bd5162 2021-12-01 op static void
1230 a1bd5162 2021-12-01 op cmd_man(void)
1231 a1bd5162 2021-12-01 op {
1232 a1bd5162 2021-12-01 op spawn("man", "fm", NULL);
1233 6d283064 2021-12-01 op }
1234 6d283064 2021-12-01 op
1235 6d283064 2021-12-01 op static void
1236 6d283064 2021-12-01 op cmd_jump_top(void)
1237 6d283064 2021-12-01 op {
1238 6d283064 2021-12-01 op if (fm.nfiles)
1239 6d283064 2021-12-01 op ESEL = 0;
1240 6d283064 2021-12-01 op }
1241 6d283064 2021-12-01 op
1242 6d283064 2021-12-01 op static void
1243 6d283064 2021-12-01 op cmd_jump_bottom(void)
1244 6d283064 2021-12-01 op {
1245 6d283064 2021-12-01 op if (fm.nfiles)
1246 6d283064 2021-12-01 op ESEL = fm.nfiles - 1;
1247 6adcf849 2021-12-01 op }
1248 6adcf849 2021-12-01 op
1249 6adcf849 2021-12-01 op static void
1250 6adcf849 2021-12-01 op cmd_cd_down(void)
1251 6adcf849 2021-12-01 op {
1252 6adcf849 2021-12-01 op if (!fm.nfiles || !S_ISDIR(EMODE(ESEL)))
1253 6adcf849 2021-12-01 op return;
1254 6adcf849 2021-12-01 op if (chdir(ENAME(ESEL)) == -1) {
1255 6adcf849 2021-12-01 op message(RED, "cd: %s: %s", ENAME(ESEL), strerror(errno));
1256 6adcf849 2021-12-01 op return;
1257 6adcf849 2021-12-01 op }
1258 6adcf849 2021-12-01 op strlcat(CWD, ENAME(ESEL), sizeof(CWD));
1259 6adcf849 2021-12-01 op cd(1);
1260 a1bd5162 2021-12-01 op }
1261 a1bd5162 2021-12-01 op
1262 a1bd5162 2021-12-01 op static void
1263 6adcf849 2021-12-01 op cmd_cd_up(void)
1264 6adcf849 2021-12-01 op {
1265 6adcf849 2021-12-01 op char *dirname, first;
1266 6adcf849 2021-12-01 op
1267 6adcf849 2021-12-01 op if (!strcmp(CWD, "/"))
1268 6adcf849 2021-12-01 op return;
1269 6adcf849 2021-12-01 op
1270 6adcf849 2021-12-01 op /* dirname(3) basically */
1271 6adcf849 2021-12-01 op dirname = strrchr(CWD, '/');
1272 6adcf849 2021-12-01 op *dirname-- = '\0';
1273 6adcf849 2021-12-01 op dirname = strrchr(CWD, '/') + 1;
1274 6adcf849 2021-12-01 op
1275 6adcf849 2021-12-01 op first = dirname[0];
1276 6adcf849 2021-12-01 op dirname[0] = '\0';
1277 6adcf849 2021-12-01 op cd(1);
1278 6adcf849 2021-12-01 op dirname[0] = first;
1279 6adcf849 2021-12-01 op strlcat(dirname, "/", sizeof(dirname));
1280 6adcf849 2021-12-01 op try_to_sel(dirname);
1281 6adcf849 2021-12-01 op dirname[0] = '\0';
1282 6adcf849 2021-12-01 op if (fm.nfiles > HEIGHT)
1283 6adcf849 2021-12-01 op SCROLL = ESEL - HEIGHT / 2;
1284 6adcf849 2021-12-01 op }
1285 6adcf849 2021-12-01 op
1286 6adcf849 2021-12-01 op static void
1287 6adcf849 2021-12-01 op cmd_home(void)
1288 6adcf849 2021-12-01 op {
1289 6adcf849 2021-12-01 op const char *home;
1290 6adcf849 2021-12-01 op
1291 6adcf849 2021-12-01 op if ((home = getenv("HOME")) == NULL) {
1292 6adcf849 2021-12-01 op message(RED, "HOME is not defined!");
1293 6adcf849 2021-12-01 op return;
1294 6adcf849 2021-12-01 op }
1295 6adcf849 2021-12-01 op
1296 6adcf849 2021-12-01 op strlcpy(CWD, home, sizeof(CWD));
1297 6adcf849 2021-12-01 op if (*CWD != '\0' && CWD[strlen(CWD) - 1] != '/')
1298 6adcf849 2021-12-01 op strlcat(CWD, "/", sizeof(CWD));
1299 6adcf849 2021-12-01 op cd(1);
1300 6adcf849 2021-12-01 op }
1301 6adcf849 2021-12-01 op
1302 6adcf849 2021-12-01 op static void
1303 01a2cff6 2021-12-12 op cmd_copy_path(void)
1304 01a2cff6 2021-12-12 op {
1305 01a2cff6 2021-12-12 op const char *path;
1306 01a2cff6 2021-12-12 op FILE *f;
1307 01a2cff6 2021-12-12 op
1308 01a2cff6 2021-12-12 op if ((path = getenv("CLIP")) == NULL ||
1309 01a2cff6 2021-12-12 op (f = fopen(path, "w")) == NULL) {
1310 01a2cff6 2021-12-12 op /* use internal clipboard */
1311 01a2cff6 2021-12-12 op strlcpy(clipboard, CWD, sizeof(clipboard));
1312 01a2cff6 2021-12-12 op strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1313 01a2cff6 2021-12-12 op return;
1314 01a2cff6 2021-12-12 op }
1315 01a2cff6 2021-12-12 op
1316 01a2cff6 2021-12-12 op fprintf(f, "%s%s", CWD, ENAME(ESEL));
1317 01a2cff6 2021-12-12 op fputc('\0', f);
1318 01a2cff6 2021-12-12 op fclose(f);
1319 01a2cff6 2021-12-12 op }
1320 01a2cff6 2021-12-12 op
1321 01a2cff6 2021-12-12 op static void
1322 01a2cff6 2021-12-12 op cmd_paste_path(void)
1323 01a2cff6 2021-12-12 op {
1324 01a2cff6 2021-12-12 op ssize_t r;
1325 01a2cff6 2021-12-12 op const char *path;
1326 01a2cff6 2021-12-12 op char *nl;
1327 01a2cff6 2021-12-12 op FILE *f;
1328 01a2cff6 2021-12-12 op char p[PATH_MAX];
1329 01a2cff6 2021-12-12 op
1330 01a2cff6 2021-12-12 op if ((path = getenv("CLIP")) != NULL &&
1331 01a2cff6 2021-12-12 op (f = fopen(path, "w")) != NULL) {
1332 01a2cff6 2021-12-12 op r = fread(clipboard, 1, sizeof(clipboard)-1, f);
1333 01a2cff6 2021-12-12 op fclose(f);
1334 01a2cff6 2021-12-12 op if (r == -1 || r == 0)
1335 01a2cff6 2021-12-12 op goto err;
1336 01a2cff6 2021-12-12 op if ((nl = memmem(clipboard, r, "", 1)) == NULL)
1337 01a2cff6 2021-12-12 op goto err;
1338 01a2cff6 2021-12-12 op }
1339 01a2cff6 2021-12-12 op
1340 01a2cff6 2021-12-12 op strlcpy(p, clipboard, sizeof(p));
1341 01a2cff6 2021-12-12 op strlcpy(CWD, clipboard, sizeof(CWD));
1342 01a2cff6 2021-12-12 op if ((nl = strrchr(CWD, '/')) != NULL) {
1343 01a2cff6 2021-12-12 op *nl = '\0';
1344 01a2cff6 2021-12-12 op nl = strrchr(p, '/');
1345 01a2cff6 2021-12-12 op assert(nl != NULL);
1346 01a2cff6 2021-12-12 op }
1347 01a2cff6 2021-12-12 op if (strcmp(CWD, "/") != 0)
1348 01a2cff6 2021-12-12 op strlcat(CWD, "/", sizeof(CWD));
1349 01a2cff6 2021-12-12 op cd(1);
1350 01a2cff6 2021-12-12 op try_to_sel(nl+1);
1351 01a2cff6 2021-12-12 op return;
1352 01a2cff6 2021-12-12 op
1353 01a2cff6 2021-12-12 op err:
1354 01a2cff6 2021-12-12 op memset(clipboard, 0, sizeof(clipboard));
1355 01a2cff6 2021-12-12 op }
1356 01a2cff6 2021-12-12 op
1357 01a2cff6 2021-12-12 op static void
1358 38d7b472 2021-12-13 op cmd_reload(void)
1359 38d7b472 2021-12-13 op {
1360 38d7b472 2021-12-13 op reload();
1361 38d7b472 2021-12-13 op }
1362 38d7b472 2021-12-13 op
1363 38d7b472 2021-12-13 op static void
1364 e03d1219 2021-12-13 op cmd_shell(void)
1365 e03d1219 2021-12-13 op {
1366 e03d1219 2021-12-13 op const char *shell;
1367 e03d1219 2021-12-13 op
1368 e03d1219 2021-12-13 op if ((shell = getenv("SHELL")) == NULL)
1369 e03d1219 2021-12-13 op shell = FM_SHELL;
1370 e03d1219 2021-12-13 op spawn(shell, NULL);
1371 63fe4ed0 2021-12-13 op }
1372 63fe4ed0 2021-12-13 op
1373 63fe4ed0 2021-12-13 op static void
1374 63fe4ed0 2021-12-13 op cmd_view(void)
1375 63fe4ed0 2021-12-13 op {
1376 63fe4ed0 2021-12-13 op const char *pager;
1377 63fe4ed0 2021-12-13 op
1378 63fe4ed0 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1379 63fe4ed0 2021-12-13 op return;
1380 63fe4ed0 2021-12-13 op
1381 63fe4ed0 2021-12-13 op if ((pager = getenv("PAGER")) == NULL)
1382 63fe4ed0 2021-12-13 op pager = FM_PAGER;
1383 63fe4ed0 2021-12-13 op spawn(pager, ENAME(ESEL), NULL);
1384 d244a655 2021-12-13 op }
1385 d244a655 2021-12-13 op
1386 d244a655 2021-12-13 op static void
1387 d244a655 2021-12-13 op cmd_edit(void)
1388 d244a655 2021-12-13 op {
1389 d244a655 2021-12-13 op const char *editor;
1390 d244a655 2021-12-13 op
1391 d244a655 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1392 d244a655 2021-12-13 op return;
1393 d244a655 2021-12-13 op
1394 d244a655 2021-12-13 op if ((editor = getenv("VISUAL")) == NULL ||
1395 d244a655 2021-12-13 op (editor = getenv("EDITOR")) == NULL)
1396 d244a655 2021-12-13 op editor = FM_ED;
1397 d244a655 2021-12-13 op
1398 d244a655 2021-12-13 op spawn(editor, ENAME(ESEL), NULL);
1399 2c5122bc 2021-12-13 op }
1400 2c5122bc 2021-12-13 op
1401 2c5122bc 2021-12-13 op static void
1402 2c5122bc 2021-12-13 op cmd_open(void)
1403 2c5122bc 2021-12-13 op {
1404 2c5122bc 2021-12-13 op const char *opener;
1405 2c5122bc 2021-12-13 op
1406 2c5122bc 2021-12-13 op if (!fm.nfiles || S_ISDIR(EMODE(ESEL)))
1407 2c5122bc 2021-12-13 op return;
1408 2c5122bc 2021-12-13 op
1409 2c5122bc 2021-12-13 op if ((opener = getenv("OPENER")) == NULL)
1410 2c5122bc 2021-12-13 op opener = FM_OPENER;
1411 2c5122bc 2021-12-13 op
1412 2c5122bc 2021-12-13 op spawn(opener, ENAME(ESEL), NULL);
1413 2c5122bc 2021-12-13 op }
1414 2c5122bc 2021-12-13 op
1415 2c5122bc 2021-12-13 op static void
1416 2c5122bc 2021-12-13 op cmd_mark(void)
1417 2c5122bc 2021-12-13 op {
1418 2c5122bc 2021-12-13 op if (MARKED(ESEL))
1419 2c5122bc 2021-12-13 op del_mark(&fm.marks, ENAME(ESEL));
1420 2c5122bc 2021-12-13 op else
1421 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1422 2c5122bc 2021-12-13 op
1423 2c5122bc 2021-12-13 op MARKED(ESEL) = !MARKED(ESEL);
1424 2c5122bc 2021-12-13 op ESEL = (ESEL + 1) % fm.nfiles;
1425 2c5122bc 2021-12-13 op }
1426 2c5122bc 2021-12-13 op
1427 2c5122bc 2021-12-13 op static void
1428 2c5122bc 2021-12-13 op cmd_toggle_mark(void)
1429 2c5122bc 2021-12-13 op {
1430 2c5122bc 2021-12-13 op int i;
1431 2c5122bc 2021-12-13 op
1432 2c5122bc 2021-12-13 op for (i = 0; i < fm.nfiles; ++i) {
1433 2c5122bc 2021-12-13 op if (MARKED(i))
1434 2c5122bc 2021-12-13 op del_mark(&fm.marks, ENAME(i));
1435 2c5122bc 2021-12-13 op else
1436 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1437 2c5122bc 2021-12-13 op MARKED(i) = !MARKED(i);
1438 2c5122bc 2021-12-13 op }
1439 e03d1219 2021-12-13 op }
1440 e03d1219 2021-12-13 op
1441 e03d1219 2021-12-13 op static void
1442 2c5122bc 2021-12-13 op cmd_mark_all(void)
1443 2c5122bc 2021-12-13 op {
1444 2c5122bc 2021-12-13 op int i;
1445 2c5122bc 2021-12-13 op
1446 2c5122bc 2021-12-13 op for (i = 0; i < fm.nfiles; ++i)
1447 2c5122bc 2021-12-13 op if (!MARKED(i)) {
1448 2c5122bc 2021-12-13 op add_mark(&fm.marks, CWD, ENAME(ESEL));
1449 2c5122bc 2021-12-13 op MARKED(i) = 1;
1450 2c5122bc 2021-12-13 op }
1451 2c5122bc 2021-12-13 op }
1452 2c5122bc 2021-12-13 op
1453 2c5122bc 2021-12-13 op static void
1454 a1bd5162 2021-12-01 op loop(void)
1455 a1bd5162 2021-12-01 op {
1456 a1bd5162 2021-12-01 op int meta, ch, c;
1457 a1bd5162 2021-12-01 op struct binding {
1458 a1bd5162 2021-12-01 op int ch;
1459 a1bd5162 2021-12-01 op #define K_META 1
1460 a1bd5162 2021-12-01 op #define K_CTRL 2
1461 a1bd5162 2021-12-01 op int chflags;
1462 a1bd5162 2021-12-01 op void (*fn)(void);
1463 a1bd5162 2021-12-01 op #define X_UPDV 1
1464 a1bd5162 2021-12-01 op #define X_QUIT 2
1465 a1bd5162 2021-12-01 op int flags;
1466 a1bd5162 2021-12-01 op } bindings[] = {
1467 6d283064 2021-12-01 op {'<', K_META, cmd_jump_top, X_UPDV},
1468 6d283064 2021-12-01 op {'>', K_META, cmd_jump_bottom, X_UPDV},
1469 a1bd5162 2021-12-01 op {'?', 0, cmd_man, 0},
1470 6d283064 2021-12-01 op {'G', 0, cmd_jump_bottom, X_UPDV},
1471 6adcf849 2021-12-01 op {'H', 0, cmd_home, X_UPDV},
1472 a1bd5162 2021-12-01 op {'J', 0, cmd_scroll_down, X_UPDV},
1473 a1bd5162 2021-12-01 op {'K', 0, cmd_scroll_up, X_UPDV},
1474 2c5122bc 2021-12-13 op {'M', 0, cmd_mark_all, X_UPDV},
1475 584ee48a 2021-12-13 op {'P', 0, cmd_paste_path, X_UPDV},
1476 a1bd5162 2021-12-01 op {'V', K_CTRL, cmd_scroll_down, X_UPDV},
1477 584ee48a 2021-12-13 op {'Y', 0, cmd_copy_path, X_UPDV},
1478 584ee48a 2021-12-13 op {'^', 0, cmd_cd_up, X_UPDV},
1479 584ee48a 2021-12-13 op {'b', 0, cmd_cd_up, X_UPDV},
1480 d244a655 2021-12-13 op {'e', 0, cmd_edit, X_UPDV},
1481 584ee48a 2021-12-13 op {'f', 0, cmd_cd_down, X_UPDV},
1482 6d283064 2021-12-01 op {'g', 0, cmd_jump_top, X_UPDV},
1483 a1bd5162 2021-12-01 op {'g', K_CTRL, NULL, X_UPDV},
1484 584ee48a 2021-12-13 op {'h', 0, cmd_cd_up, X_UPDV},
1485 a1bd5162 2021-12-01 op {'j', 0, cmd_down, X_UPDV},
1486 a1bd5162 2021-12-01 op {'k', 0, cmd_up, X_UPDV},
1487 584ee48a 2021-12-13 op {'l', 0, cmd_cd_down, X_UPDV},
1488 584ee48a 2021-12-13 op {'l', K_CTRL, cmd_reload, X_UPDV},
1489 a1bd5162 2021-12-01 op {'n', 0, cmd_down, X_UPDV},
1490 a1bd5162 2021-12-01 op {'n', K_CTRL, cmd_down, X_UPDV},
1491 2c5122bc 2021-12-13 op {'m', 0, cmd_mark, X_UPDV},
1492 e03d1219 2021-12-13 op {'m', K_CTRL, cmd_shell, X_UPDV},
1493 2c5122bc 2021-12-13 op {'o', 0, cmd_open, X_UPDV},
1494 a1bd5162 2021-12-01 op {'p', 0, cmd_up, X_UPDV},
1495 a1bd5162 2021-12-01 op {'p', K_CTRL, cmd_up, X_UPDV},
1496 a1bd5162 2021-12-01 op {'q', 0, NULL, X_QUIT},
1497 2c5122bc 2021-12-13 op {'t', 0, cmd_toggle_mark, X_UPDV},
1498 63fe4ed0 2021-12-13 op {'v', 0, cmd_view, X_UPDV},
1499 a1bd5162 2021-12-01 op {'v', K_META, cmd_scroll_up, X_UPDV},
1500 1a7d56bf 2021-12-12 op {KEY_DOWN, 0, cmd_scroll_down, X_UPDV},
1501 a1bd5162 2021-12-01 op {KEY_NPAGE, 0, cmd_scroll_down, X_UPDV},
1502 a1bd5162 2021-12-01 op {KEY_PPAGE, 0, cmd_scroll_up, X_UPDV},
1503 a1bd5162 2021-12-01 op {KEY_RESIZE, 0, NULL, X_UPDV},
1504 a1bd5162 2021-12-01 op {KEY_RESIZE, K_META, NULL, X_UPDV},
1505 1a7d56bf 2021-12-12 op {KEY_UP, 0, cmd_scroll_up, X_UPDV},
1506 a1bd5162 2021-12-01 op }, *b;
1507 a1bd5162 2021-12-01 op size_t i;
1508 a1bd5162 2021-12-01 op
1509 a1bd5162 2021-12-01 op for (;;) {
1510 a1bd5162 2021-12-01 op again:
1511 a1bd5162 2021-12-01 op meta = 0;
1512 a1bd5162 2021-12-01 op ch = fm_getch();
1513 a1bd5162 2021-12-01 op if (ch == '\e') {
1514 a1bd5162 2021-12-01 op meta = 1;
1515 a1bd5162 2021-12-01 op if ((ch = fm_getch()) == '\e') {
1516 a1bd5162 2021-12-01 op meta = 0;
1517 a1bd5162 2021-12-01 op ch = '\e';
1518 a1bd5162 2021-12-01 op }
1519 c71d4135 2021-12-01 op }
1520 a1bd5162 2021-12-01 op
1521 a1bd5162 2021-12-01 op clear_message();
1522 a1bd5162 2021-12-01 op
1523 a1bd5162 2021-12-01 op for (i = 0; i < nitems(bindings); ++i) {
1524 a1bd5162 2021-12-01 op b = &bindings[i];
1525 a1bd5162 2021-12-01 op c = b->ch;
1526 a1bd5162 2021-12-01 op if (b->chflags & K_CTRL)
1527 a1bd5162 2021-12-01 op c = CTRL(c);
1528 a1bd5162 2021-12-01 op if ((!meta && b->chflags & K_META) || ch != c)
1529 a1bd5162 2021-12-01 op continue;
1530 a1bd5162 2021-12-01 op
1531 a1bd5162 2021-12-01 op if (b->flags & X_QUIT)
1532 a1bd5162 2021-12-01 op return;
1533 a1bd5162 2021-12-01 op if (b->fn != NULL)
1534 a1bd5162 2021-12-01 op b->fn();
1535 a1bd5162 2021-12-01 op if (b->flags & X_UPDV)
1536 a1bd5162 2021-12-01 op update_view();
1537 a1bd5162 2021-12-01 op
1538 a1bd5162 2021-12-01 op goto again;
1539 a1bd5162 2021-12-01 op }
1540 a1bd5162 2021-12-01 op
1541 a1bd5162 2021-12-01 op message(RED, "%s%s is undefined",
1542 a1bd5162 2021-12-01 op meta ? "M-": "", keyname(ch));
1543 a1bd5162 2021-12-01 op refresh();
1544 c71d4135 2021-12-01 op }
1545 d248a263 2023-02-08 op }
1546 d248a263 2023-02-08 op
1547 d248a263 2023-02-08 op static __dead void
1548 d248a263 2023-02-08 op usage(void)
1549 d248a263 2023-02-08 op {
1550 d248a263 2023-02-08 op fprintf(stderr, "usage: %s [-hv] [-d file] [-m file] [dirs ...]\n",
1551 d248a263 2023-02-08 op getprogname());
1552 d248a263 2023-02-08 op fprintf(stderr, "version: %s\n", RV_VERSION);
1553 d248a263 2023-02-08 op exit(1);
1554 c71d4135 2021-12-01 op }
1555 c71d4135 2021-12-01 op
1556 c9f42136 2021-11-25 op int
1557 c9f42136 2021-11-25 op main(int argc, char *argv[])
1558 c9f42136 2021-11-25 op {
1559 c66e2de7 2021-11-25 op int i, ch;
1560 c66e2de7 2021-11-25 op char *entry;
1561 c66e2de7 2021-11-25 op DIR *d;
1562 c66e2de7 2021-11-25 op FILE *save_cwd_file = NULL;
1563 c66e2de7 2021-11-25 op FILE *save_marks_file = NULL;
1564 5d347732 2023-02-08 op
1565 5d347732 2023-02-08 op if (pledge("stdio rpath wpath cpath tty proc exec", NULL) == -1)
1566 5d347732 2023-02-08 op err(1, "pledge");
1567 c9f42136 2021-11-25 op
1568 cc9de337 2021-11-26 op while ((ch = getopt_long(argc, argv, "d:hm:v", opts, NULL)) != -1) {
1569 cc9de337 2021-11-26 op switch (ch) {
1570 cc9de337 2021-11-26 op case 'd':
1571 cc9de337 2021-11-26 op if ((save_cwd_file = fopen(optarg, "w")) == NULL)
1572 cc9de337 2021-11-26 op err(1, "open %s", optarg);
1573 cc9de337 2021-11-26 op break;
1574 cc9de337 2021-11-26 op case 'h':
1575 d248a263 2023-02-08 op usage();
1576 d248a263 2023-02-08 op break;
1577 cc9de337 2021-11-26 op case 'm':
1578 cc9de337 2021-11-26 op if ((save_marks_file = fopen(optarg, "a")) == NULL)
1579 cc9de337 2021-11-26 op err(1, "open %s", optarg);
1580 cc9de337 2021-11-26 op break;
1581 cc9de337 2021-11-26 op case 'v':
1582 cc9de337 2021-11-26 op printf("version: fm %s\n", RV_VERSION);
1583 c66e2de7 2021-11-25 op return 0;
1584 725ac023 2023-02-08 op default:
1585 725ac023 2023-02-08 op usage();
1586 c66e2de7 2021-11-25 op }
1587 c66e2de7 2021-11-25 op }
1588 cc9de337 2021-11-26 op
1589 c66e2de7 2021-11-25 op get_user_programs();
1590 c66e2de7 2021-11-25 op init_term();
1591 c3e1e821 2021-11-26 op fm.nfiles = 0;
1592 c66e2de7 2021-11-25 op for (i = 0; i < 10; i++) {
1593 c3e1e821 2021-11-26 op fm.tabs[i].esel = fm.tabs[i].scroll = 0;
1594 c3e1e821 2021-11-26 op fm.tabs[i].flags = RV_FLAGS;
1595 c66e2de7 2021-11-25 op }
1596 acf1203a 2023-02-08 op strlcpy(fm.tabs[0].cwd, getenv("HOME"), sizeof(fm.tabs[0].cwd));
1597 c66e2de7 2021-11-25 op for (i = 1; i < argc && i < 10; i++) {
1598 c66e2de7 2021-11-25 op if ((d = opendir(argv[i]))) {
1599 c3e1e821 2021-11-26 op realpath(argv[i], fm.tabs[i].cwd);
1600 c66e2de7 2021-11-25 op closedir(d);
1601 acf1203a 2023-02-08 op } else {
1602 acf1203a 2023-02-08 op strlcpy(fm.tabs[i].cwd, fm.tabs[0].cwd,
1603 acf1203a 2023-02-08 op sizeof(fm.tabs[i].cwd));
1604 acf1203a 2023-02-08 op }
1605 c66e2de7 2021-11-25 op }
1606 c3e1e821 2021-11-26 op getcwd(fm.tabs[i].cwd, PATH_MAX);
1607 c66e2de7 2021-11-25 op for (i++; i < 10; i++)
1608 acf1203a 2023-02-08 op strlcpy(fm.tabs[i].cwd, fm.tabs[i - 1].cwd,
1609 acf1203a 2023-02-08 op sizeof(fm.tabs[i].cwd));
1610 c66e2de7 2021-11-25 op for (i = 0; i < 10; i++)
1611 c3e1e821 2021-11-26 op if (fm.tabs[i].cwd[strlen(fm.tabs[i].cwd) - 1] != '/')
1612 acf1203a 2023-02-08 op strlcat(fm.tabs[i].cwd, "/", sizeof(fm.tabs[i].cwd));
1613 c3e1e821 2021-11-26 op fm.tab = 1;
1614 c3e1e821 2021-11-26 op fm.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
1615 c3e1e821 2021-11-26 op init_marks(&fm.marks);
1616 c66e2de7 2021-11-25 op cd(1);
1617 01a2cff6 2021-12-12 op strlcpy(clipboard, CWD, sizeof(clipboard));
1618 c3e1e821 2021-11-26 op if (fm.nfiles > 0)
1619 01a2cff6 2021-12-12 op strlcat(clipboard, ENAME(ESEL), sizeof(clipboard));
1620 c71d4135 2021-12-01 op
1621 c71d4135 2021-12-01 op loop();
1622 c71d4135 2021-12-01 op
1623 c3e1e821 2021-11-26 op if (fm.nfiles)
1624 c3e1e821 2021-11-26 op free_rows(&fm.rows, fm.nfiles);
1625 c3e1e821 2021-11-26 op delwin(fm.window);
1626 c66e2de7 2021-11-25 op if (save_cwd_file != NULL) {
1627 c66e2de7 2021-11-25 op fputs(CWD, save_cwd_file);
1628 c66e2de7 2021-11-25 op fclose(save_cwd_file);
1629 c66e2de7 2021-11-25 op }
1630 c66e2de7 2021-11-25 op if (save_marks_file != NULL) {
1631 c3e1e821 2021-11-26 op for (i = 0; i < fm.marks.bulk; i++) {
1632 c3e1e821 2021-11-26 op entry = fm.marks.entries[i];
1633 c66e2de7 2021-11-25 op if (entry)
1634 c66e2de7 2021-11-25 op fprintf(save_marks_file, "%s%s\n",
1635 c3e1e821 2021-11-26 op fm.marks.dirpath, entry);
1636 c66e2de7 2021-11-25 op }
1637 c66e2de7 2021-11-25 op fclose(save_marks_file);
1638 c66e2de7 2021-11-25 op }
1639 c3e1e821 2021-11-26 op free_marks(&fm.marks);
1640 c66e2de7 2021-11-25 op return 0;
1641 c9f42136 2021-11-25 op }