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