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