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