Blame


1 71ee1627 2014-11-07 marcelgmr #include <stdlib.h>
2 bae1431c 2014-11-07 marcelgmr #include <stdint.h>
3 6beff8a2 2014-11-08 marcelgmr #include <ctype.h>
4 6beff8a2 2014-11-08 marcelgmr #include <string.h>
5 5f52c474 2014-11-08 marcelgmr #include <sys/types.h> /* pid_t, ... */
6 c04b0d89 2015-03-30 marcelgmr #include <stdio.h>
7 c04b0d89 2015-03-30 marcelgmr #include <limits.h> /* PATH_MAX */
8 71ee1627 2014-11-07 marcelgmr #include <locale.h> /* setlocale(), LC_ALL */
9 b7ee4ee2 2014-11-14 marcelgmr #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
10 71ee1627 2014-11-07 marcelgmr #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
11 71ee1627 2014-11-07 marcelgmr #include <sys/stat.h>
12 b7ee4ee2 2014-11-14 marcelgmr #include <fcntl.h> /* open() */
13 71ee1627 2014-11-07 marcelgmr #include <sys/wait.h> /* waitpid() */
14 b529416b 2014-11-11 marcelgmr #include <signal.h> /* struct sigaction, sigaction() */
15 8a757b0f 2015-03-07 marcelgmr #include <errno.h>
16 71ee1627 2014-11-07 marcelgmr #include <curses.h>
17 71ee1627 2014-11-07 marcelgmr
18 71ee1627 2014-11-07 marcelgmr #include "config.h"
19 71ee1627 2014-11-07 marcelgmr
20 3cd6bb98 2014-11-11 marcelgmr /* String buffers. */
21 576e768d 2014-11-08 marcelgmr #define ROWSZ 256
22 3cd6bb98 2014-11-11 marcelgmr static char ROW[ROWSZ];
23 a17e36e3 2014-11-07 marcelgmr #define STATUSSZ 256
24 3cd6bb98 2014-11-11 marcelgmr static char STATUS[STATUSSZ];
25 1447784d 2014-11-14 marcelgmr #define INPUTSZ 256
26 1447784d 2014-11-14 marcelgmr static char INPUT[INPUTSZ];
27 3cd6bb98 2014-11-11 marcelgmr
28 3cd6bb98 2014-11-11 marcelgmr /* Argument buffers for execvp(). */
29 71ee1627 2014-11-07 marcelgmr #define MAXARGS 256
30 3cd6bb98 2014-11-11 marcelgmr static char *ARGS[MAXARGS];
31 71ee1627 2014-11-07 marcelgmr
32 60128fcd 2014-11-15 marcelgmr /* Listing view parameters. */
33 60128fcd 2014-11-15 marcelgmr #define HEIGHT (LINES-4)
34 60128fcd 2014-11-15 marcelgmr #define STATUSPOS (COLS-16)
35 f05efd9e 2014-11-07 marcelgmr
36 3cd6bb98 2014-11-11 marcelgmr /* Listing view flags. */
37 bae1431c 2014-11-07 marcelgmr #define SHOW_FILES 0x01u
38 bae1431c 2014-11-07 marcelgmr #define SHOW_DIRS 0x02u
39 bae1431c 2014-11-07 marcelgmr #define SHOW_HIDDEN 0x04u
40 bae1431c 2014-11-07 marcelgmr
41 50c63039 2014-11-14 marcelgmr /* Marks parameters. */
42 18016333 2014-11-14 marcelgmr #define BULK_INIT 5
43 18016333 2014-11-14 marcelgmr #define BULK_THRESH 256
44 18016333 2014-11-14 marcelgmr
45 3cd6bb98 2014-11-11 marcelgmr /* Information associated to each entry in listing. */
46 8f853fb5 2015-03-25 marcelgmr typedef struct Row {
47 576e768d 2014-11-08 marcelgmr char *name;
48 576e768d 2014-11-08 marcelgmr off_t size;
49 18016333 2014-11-14 marcelgmr int marked;
50 eef51ac5 2015-03-24 marcelgmr } Row;
51 576e768d 2014-11-08 marcelgmr
52 5c4a6b62 2014-11-14 marcelgmr /* Dynamic array of marked entries. */
53 8f853fb5 2015-03-25 marcelgmr typedef struct Marks {
54 c04b0d89 2015-03-30 marcelgmr char dirpath[PATH_MAX];
55 18016333 2014-11-14 marcelgmr int bulk;
56 18016333 2014-11-14 marcelgmr int nentries;
57 18016333 2014-11-14 marcelgmr char **entries;
58 eef51ac5 2015-03-24 marcelgmr } Marks;
59 18016333 2014-11-14 marcelgmr
60 3cd6bb98 2014-11-11 marcelgmr /* Global state. Some basic info is allocated for ten tabs. */
61 eef51ac5 2015-03-24 marcelgmr static struct Rover {
62 ada8fba9 2014-11-09 marcelgmr int tab;
63 71ee1627 2014-11-07 marcelgmr int nfiles;
64 ada8fba9 2014-11-09 marcelgmr int scroll[10];
65 7c74df90 2014-11-15 marcelgmr int esel[10];
66 47e9f26d 2014-11-09 marcelgmr uint8_t flags[10];
67 eef51ac5 2015-03-24 marcelgmr Row *rows;
68 71ee1627 2014-11-07 marcelgmr WINDOW *window;
69 c04b0d89 2015-03-30 marcelgmr char cwd[10][PATH_MAX];
70 eef51ac5 2015-03-24 marcelgmr Marks marks;
71 f05efd9e 2014-11-07 marcelgmr } rover;
72 71ee1627 2014-11-07 marcelgmr
73 3cd6bb98 2014-11-11 marcelgmr /* Macros for accessing global state. */
74 7c74df90 2014-11-15 marcelgmr #define ENAME(I) rover.rows[I].name
75 7c74df90 2014-11-15 marcelgmr #define ESIZE(I) rover.rows[I].size
76 18016333 2014-11-14 marcelgmr #define MARKED(I) rover.rows[I].marked
77 3cd6bb98 2014-11-11 marcelgmr #define SCROLL rover.scroll[rover.tab]
78 7c74df90 2014-11-15 marcelgmr #define ESEL rover.esel[rover.tab]
79 3cd6bb98 2014-11-11 marcelgmr #define FLAGS rover.flags[rover.tab]
80 3cd6bb98 2014-11-11 marcelgmr #define CWD rover.cwd[rover.tab]
81 845f2b87 2014-11-14 marcelgmr
82 60128fcd 2014-11-15 marcelgmr /* Helpers. */
83 60128fcd 2014-11-15 marcelgmr #define MIN(A, B) ((A) < (B) ? (A) : (B))
84 60128fcd 2014-11-15 marcelgmr #define MAX(A, B) ((A) > (B) ? (A) : (B))
85 7c74df90 2014-11-15 marcelgmr #define ISDIR(E) (strchr((E), '/') != NULL)
86 7c74df90 2014-11-15 marcelgmr
87 8f853fb5 2015-03-25 marcelgmr typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE} Color;
88 845f2b87 2014-11-14 marcelgmr typedef int (*PROCESS)(const char *path);
89 576e768d 2014-11-08 marcelgmr
90 d06e977d 2014-11-14 marcelgmr static void
91 eef51ac5 2015-03-24 marcelgmr init_marks(Marks *marks)
92 18016333 2014-11-14 marcelgmr {
93 18016333 2014-11-14 marcelgmr strcpy(marks->dirpath, "");
94 18016333 2014-11-14 marcelgmr marks->bulk = BULK_INIT;
95 18016333 2014-11-14 marcelgmr marks->nentries = 0;
96 f41c2ea1 2014-12-23 marcelgmr marks->entries = calloc(marks->bulk, sizeof *marks->entries);
97 18016333 2014-11-14 marcelgmr }
98 18016333 2014-11-14 marcelgmr
99 5c4a6b62 2014-11-14 marcelgmr /* Unmark all entries. */
100 d06e977d 2014-11-14 marcelgmr static void
101 eef51ac5 2015-03-24 marcelgmr mark_none(Marks *marks)
102 18016333 2014-11-14 marcelgmr {
103 18016333 2014-11-14 marcelgmr int i;
104 18016333 2014-11-14 marcelgmr
105 8ba07de5 2014-11-14 marcelgmr strcpy(marks->dirpath, "");
106 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk && marks->nentries; i++)
107 18016333 2014-11-14 marcelgmr if (marks->entries[i]) {
108 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
109 9a852838 2014-11-14 marcelgmr marks->entries[i] = NULL;
110 18016333 2014-11-14 marcelgmr marks->nentries--;
111 18016333 2014-11-14 marcelgmr }
112 18016333 2014-11-14 marcelgmr if (marks->bulk > BULK_THRESH) {
113 18016333 2014-11-14 marcelgmr /* Reset bulk to free some memory. */
114 18016333 2014-11-14 marcelgmr free(marks->entries);
115 18016333 2014-11-14 marcelgmr marks->bulk = BULK_INIT;
116 f41c2ea1 2014-12-23 marcelgmr marks->entries = calloc(marks->bulk, sizeof *marks->entries);
117 18016333 2014-11-14 marcelgmr }
118 18016333 2014-11-14 marcelgmr }
119 18016333 2014-11-14 marcelgmr
120 d06e977d 2014-11-14 marcelgmr static void
121 eef51ac5 2015-03-24 marcelgmr add_mark(Marks *marks, char *dirpath, char *entry)
122 18016333 2014-11-14 marcelgmr {
123 18016333 2014-11-14 marcelgmr int i;
124 18016333 2014-11-14 marcelgmr
125 18016333 2014-11-14 marcelgmr if (!strcmp(marks->dirpath, dirpath)) {
126 18016333 2014-11-14 marcelgmr /* Append mark to directory. */
127 18016333 2014-11-14 marcelgmr if (marks->nentries == marks->bulk) {
128 18016333 2014-11-14 marcelgmr /* Expand bulk to accomodate new entry. */
129 18016333 2014-11-14 marcelgmr int extra = marks->bulk >> 1;
130 18016333 2014-11-14 marcelgmr marks->bulk += extra; /* bulk *= 1.5; */
131 f41c2ea1 2014-12-23 marcelgmr marks->entries = realloc(marks->entries,
132 f41c2ea1 2014-12-23 marcelgmr marks->bulk * sizeof *marks->entries);
133 f41c2ea1 2014-12-23 marcelgmr memset(&marks->entries[marks->nentries], 0,
134 f41c2ea1 2014-12-23 marcelgmr extra * sizeof *marks->entries);
135 18016333 2014-11-14 marcelgmr i = marks->nentries;
136 590d39af 2014-12-02 marcelgmr } else {
137 18016333 2014-11-14 marcelgmr /* Search for empty slot (there must be one). */
138 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk; i++)
139 18016333 2014-11-14 marcelgmr if (!marks->entries[i])
140 18016333 2014-11-14 marcelgmr break;
141 18016333 2014-11-14 marcelgmr }
142 590d39af 2014-12-02 marcelgmr } else {
143 18016333 2014-11-14 marcelgmr /* Directory changed. Discard old marks. */
144 18016333 2014-11-14 marcelgmr mark_none(marks);
145 18016333 2014-11-14 marcelgmr strcpy(marks->dirpath, dirpath);
146 18016333 2014-11-14 marcelgmr i = 0;
147 18016333 2014-11-14 marcelgmr }
148 f41c2ea1 2014-12-23 marcelgmr marks->entries[i] = malloc(strlen(entry) + 1);
149 18016333 2014-11-14 marcelgmr strcpy(marks->entries[i], entry);
150 18016333 2014-11-14 marcelgmr marks->nentries++;
151 18016333 2014-11-14 marcelgmr }
152 18016333 2014-11-14 marcelgmr
153 d06e977d 2014-11-14 marcelgmr static void
154 eef51ac5 2015-03-24 marcelgmr del_mark(Marks *marks, char *entry)
155 18016333 2014-11-14 marcelgmr {
156 18016333 2014-11-14 marcelgmr int i;
157 18016333 2014-11-14 marcelgmr
158 18016333 2014-11-14 marcelgmr if (marks->nentries > 1) {
159 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk; i++)
160 18016333 2014-11-14 marcelgmr if (marks->entries[i] && !strcmp(marks->entries[i], entry))
161 18016333 2014-11-14 marcelgmr break;
162 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
163 18016333 2014-11-14 marcelgmr marks->entries[i] = NULL;
164 18016333 2014-11-14 marcelgmr marks->nentries--;
165 590d39af 2014-12-02 marcelgmr } else
166 590d39af 2014-12-02 marcelgmr mark_none(marks);
167 18016333 2014-11-14 marcelgmr }
168 18016333 2014-11-14 marcelgmr
169 d06e977d 2014-11-14 marcelgmr static void
170 eef51ac5 2015-03-24 marcelgmr free_marks(Marks *marks)
171 18016333 2014-11-14 marcelgmr {
172 18016333 2014-11-14 marcelgmr int i;
173 18016333 2014-11-14 marcelgmr
174 18016333 2014-11-14 marcelgmr for (i = 0; i < marks->bulk && marks->nentries; i++)
175 18016333 2014-11-14 marcelgmr if (marks->entries[i]) {
176 18016333 2014-11-14 marcelgmr free(marks->entries[i]);
177 18016333 2014-11-14 marcelgmr marks->nentries--;
178 18016333 2014-11-14 marcelgmr }
179 18016333 2014-11-14 marcelgmr free(marks->entries);
180 18016333 2014-11-14 marcelgmr }
181 b25c9834 2014-11-14 marcelgmr
182 eef51ac5 2015-03-24 marcelgmr static void message(const char *msg, Color color);
183 d0cdbd3c 2014-11-15 marcelgmr static void clear_message();
184 18016333 2014-11-14 marcelgmr
185 f340950f 2014-11-11 marcelgmr static void handle_segv(int sig);
186 3cd6bb98 2014-11-11 marcelgmr static void handle_winch(int sig);
187 e1c42cfe 2014-11-11 marcelgmr
188 3cd6bb98 2014-11-11 marcelgmr /* Curses setup. */
189 71ee1627 2014-11-07 marcelgmr static void
190 f05efd9e 2014-11-07 marcelgmr init_term()
191 71ee1627 2014-11-07 marcelgmr {
192 e1c42cfe 2014-11-11 marcelgmr struct sigaction sa;
193 e1c42cfe 2014-11-11 marcelgmr
194 71ee1627 2014-11-07 marcelgmr setlocale(LC_ALL, "");
195 71ee1627 2014-11-07 marcelgmr initscr();
196 71ee1627 2014-11-07 marcelgmr cbreak(); /* Get one character at a time. */
197 71ee1627 2014-11-07 marcelgmr noecho();
198 71ee1627 2014-11-07 marcelgmr nonl(); /* No NL->CR/NL on output. */
199 71ee1627 2014-11-07 marcelgmr intrflush(stdscr, FALSE);
200 71ee1627 2014-11-07 marcelgmr keypad(stdscr, TRUE);
201 71ee1627 2014-11-07 marcelgmr curs_set(FALSE); /* Hide blinking cursor. */
202 71ee1627 2014-11-07 marcelgmr if (has_colors()) {
203 eb265708 2015-03-24 marcelgmr short bg;
204 71ee1627 2014-11-07 marcelgmr start_color();
205 eb265708 2015-03-24 marcelgmr #ifdef NCURSES_EXT_FUNCS
206 eb265708 2015-03-24 marcelgmr use_default_colors();
207 eb265708 2015-03-24 marcelgmr bg = -1;
208 eb265708 2015-03-24 marcelgmr #else
209 eb265708 2015-03-24 marcelgmr bg = COLOR_BLACK;
210 eb265708 2015-03-24 marcelgmr #endif
211 eb265708 2015-03-24 marcelgmr init_pair(RED, COLOR_RED, bg);
212 eb265708 2015-03-24 marcelgmr init_pair(GREEN, COLOR_GREEN, bg);
213 eb265708 2015-03-24 marcelgmr init_pair(YELLOW, COLOR_YELLOW, bg);
214 eb265708 2015-03-24 marcelgmr init_pair(BLUE, COLOR_BLUE, bg);
215 eb265708 2015-03-24 marcelgmr init_pair(CYAN, COLOR_CYAN, bg);
216 eb265708 2015-03-24 marcelgmr init_pair(MAGENTA, COLOR_MAGENTA, bg);
217 eb265708 2015-03-24 marcelgmr init_pair(WHITE, COLOR_WHITE, bg);
218 71ee1627 2014-11-07 marcelgmr }
219 7c74df90 2014-11-15 marcelgmr atexit((void (*)(void)) endwin);
220 8f853fb5 2015-03-25 marcelgmr memset(&sa, 0, sizeof (struct sigaction));
221 8f853fb5 2015-03-25 marcelgmr /* Setup SIGSEGV handler. */
222 8f853fb5 2015-03-25 marcelgmr sa.sa_handler = handle_segv;
223 8f853fb5 2015-03-25 marcelgmr sigaction(SIGSEGV, &sa, NULL);
224 8f853fb5 2015-03-25 marcelgmr /* Setup SIGWINCH handler. */
225 8f853fb5 2015-03-25 marcelgmr sa.sa_handler = handle_winch;
226 8f853fb5 2015-03-25 marcelgmr sigaction(SIGWINCH, &sa, NULL);
227 71ee1627 2014-11-07 marcelgmr }
228 71ee1627 2014-11-07 marcelgmr
229 3cd6bb98 2014-11-11 marcelgmr /* Update the listing view. */
230 71ee1627 2014-11-07 marcelgmr static void
231 743e70b8 2014-11-14 marcelgmr update_view()
232 71ee1627 2014-11-07 marcelgmr {
233 222db952 2014-11-08 marcelgmr int i, j;
234 38347b2a 2015-03-30 marcelgmr int numsize;
235 576e768d 2014-11-08 marcelgmr int ishidden, isdir;
236 18016333 2014-11-14 marcelgmr int marking;
237 71ee1627 2014-11-07 marcelgmr
238 41aae0b0 2014-11-11 marcelgmr mvhline(0, 0, ' ', COLS);
239 41aae0b0 2014-11-11 marcelgmr attr_on(A_BOLD, NULL);
240 41aae0b0 2014-11-11 marcelgmr color_set(RVC_TABNUM, NULL);
241 38347b2a 2015-03-30 marcelgmr mvaddch(0, COLS - 2, rover.tab + '0');
242 41aae0b0 2014-11-11 marcelgmr color_set(DEFAULT, NULL);
243 41aae0b0 2014-11-11 marcelgmr attr_off(A_BOLD, NULL);
244 38347b2a 2015-03-30 marcelgmr if (rover.marks.nentries) {
245 e5fe9820 2015-04-17 marcelgmr numsize = snprintf(STATUS, STATUSSZ, "%d", rover.marks.nentries);
246 38347b2a 2015-03-30 marcelgmr color_set(RVC_NMARKS, NULL);
247 38347b2a 2015-03-30 marcelgmr mvaddstr(0, COLS - 3 - numsize, STATUS);
248 38347b2a 2015-03-30 marcelgmr color_set(DEFAULT, NULL);
249 38347b2a 2015-03-30 marcelgmr } else
250 38347b2a 2015-03-30 marcelgmr numsize = -1;
251 38347b2a 2015-03-30 marcelgmr color_set(RVC_CWD, NULL);
252 38347b2a 2015-03-30 marcelgmr mvaddnstr(0, 0, CWD, COLS - 4 - numsize);
253 38347b2a 2015-03-30 marcelgmr color_set(DEFAULT, NULL);
254 41aae0b0 2014-11-11 marcelgmr wcolor_set(rover.window, RVC_BORDER, NULL);
255 41aae0b0 2014-11-11 marcelgmr wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
256 41aae0b0 2014-11-11 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
257 667d4907 2014-11-14 marcelgmr /* Selection might not be visible, due to cursor wrapping or window
258 667d4907 2014-11-14 marcelgmr shrinking. In that case, the scroll must be moved to make it visible. */
259 60128fcd 2014-11-15 marcelgmr SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
260 18016333 2014-11-14 marcelgmr marking = !strcmp(CWD, rover.marks.dirpath);
261 ada8fba9 2014-11-09 marcelgmr for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
262 7c74df90 2014-11-15 marcelgmr ishidden = ENAME(j)[0] == '.';
263 7c74df90 2014-11-15 marcelgmr isdir = ISDIR(ENAME(j));
264 7c74df90 2014-11-15 marcelgmr if (j == ESEL)
265 f05efd9e 2014-11-07 marcelgmr wattr_on(rover.window, A_REVERSE, NULL);
266 576e768d 2014-11-08 marcelgmr if (ishidden)
267 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_HIDDEN, NULL);
268 576e768d 2014-11-08 marcelgmr else if (isdir)
269 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_DIR, NULL);
270 37233869 2014-11-07 marcelgmr else
271 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, RVC_FILE, NULL);
272 576e768d 2014-11-08 marcelgmr if (!isdir)
273 e5fe9820 2015-04-17 marcelgmr snprintf(ROW, ROWSZ, "%s%*d", ENAME(j),
274 14b5cd28 2015-01-06 marcelgmr (int) (COLS - strlen(ENAME(j)) - 4), (int) ESIZE(j));
275 576e768d 2014-11-08 marcelgmr else
276 7c74df90 2014-11-15 marcelgmr strcpy(ROW, ENAME(j));
277 a81852bf 2014-11-08 marcelgmr mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
278 a651a76d 2014-11-14 marcelgmr if (marking && MARKED(j))
279 e6a7541a 2014-11-14 marcelgmr mvwaddch(rover.window, i + 1, 1, RVS_MARK);
280 18016333 2014-11-14 marcelgmr else
281 18016333 2014-11-14 marcelgmr mvwaddch(rover.window, i + 1, 1, ' ');
282 b9837001 2014-11-24 marcelgmr mvwaddnstr(rover.window, i + 1, 2, ROW, COLS - 4);
283 37233869 2014-11-07 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
284 7c74df90 2014-11-15 marcelgmr if (j == ESEL)
285 f05efd9e 2014-11-07 marcelgmr wattr_off(rover.window, A_REVERSE, NULL);
286 a81852bf 2014-11-08 marcelgmr }
287 37087140 2015-03-30 marcelgmr for (;i < HEIGHT; i++)
288 37087140 2015-03-30 marcelgmr mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
289 a81852bf 2014-11-08 marcelgmr if (rover.nfiles > HEIGHT) {
290 a81852bf 2014-11-08 marcelgmr int center, height;
291 ada8fba9 2014-11-09 marcelgmr center = (SCROLL + (HEIGHT >> 1)) * HEIGHT / rover.nfiles;
292 a81852bf 2014-11-08 marcelgmr height = (HEIGHT-1) * HEIGHT / rover.nfiles;
293 a81852bf 2014-11-08 marcelgmr if (!height) height = 1;
294 a81852bf 2014-11-08 marcelgmr wcolor_set(rover.window, RVC_BORDER, NULL);
295 a81852bf 2014-11-08 marcelgmr wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
296 2a2b72b6 2014-11-08 marcelgmr wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
297 4c8a3f34 2014-11-14 marcelgmr mvwvline(rover.window, center-(height>>1)+1, COLS-1, RVS_SCROLLBAR, height);
298 a81852bf 2014-11-08 marcelgmr wcolor_set(rover.window, DEFAULT, NULL);
299 71ee1627 2014-11-07 marcelgmr }
300 47e9f26d 2014-11-09 marcelgmr STATUS[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
301 47e9f26d 2014-11-09 marcelgmr STATUS[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
302 47e9f26d 2014-11-09 marcelgmr STATUS[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
303 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles)
304 222db952 2014-11-08 marcelgmr strcpy(ROW, "0/0");
305 bb90b2d2 2014-11-08 marcelgmr else
306 e5fe9820 2015-04-17 marcelgmr snprintf(ROW, ROWSZ, "%d/%d", ESEL + 1, rover.nfiles);
307 e5fe9820 2015-04-17 marcelgmr snprintf(STATUS+3, STATUSSZ-3, "%12s", ROW);
308 e1f31679 2014-11-07 marcelgmr color_set(RVC_STATUS, NULL);
309 50c63039 2014-11-14 marcelgmr mvaddstr(LINES - 1, STATUSPOS, STATUS);
310 e1f31679 2014-11-07 marcelgmr color_set(DEFAULT, NULL);
311 37087140 2015-03-30 marcelgmr wrefresh(rover.window);
312 71ee1627 2014-11-07 marcelgmr }
313 71ee1627 2014-11-07 marcelgmr
314 41aae0b0 2014-11-11 marcelgmr /* SIGSEGV handler: clean up curses before exiting. */
315 41aae0b0 2014-11-11 marcelgmr static void
316 41aae0b0 2014-11-11 marcelgmr handle_segv(int sig)
317 41aae0b0 2014-11-11 marcelgmr {
318 41aae0b0 2014-11-11 marcelgmr (void) sig;
319 7c74df90 2014-11-15 marcelgmr endwin();
320 ee9a7a2a 2015-01-16 marcelgmr fprintf(stderr, "Received SIGSEGV (segmentation fault).\n");
321 41aae0b0 2014-11-11 marcelgmr exit(1);
322 41aae0b0 2014-11-11 marcelgmr }
323 41aae0b0 2014-11-11 marcelgmr
324 41aae0b0 2014-11-11 marcelgmr /* SIGWINCH handler: resize application according to new terminal settings. */
325 41aae0b0 2014-11-11 marcelgmr static void
326 41aae0b0 2014-11-11 marcelgmr handle_winch(int sig)
327 41aae0b0 2014-11-11 marcelgmr {
328 41aae0b0 2014-11-11 marcelgmr (void) sig;
329 41aae0b0 2014-11-11 marcelgmr delwin(rover.window);
330 41aae0b0 2014-11-11 marcelgmr endwin();
331 41aae0b0 2014-11-11 marcelgmr refresh();
332 41aae0b0 2014-11-11 marcelgmr clear();
333 41aae0b0 2014-11-11 marcelgmr rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
334 743e70b8 2014-11-14 marcelgmr update_view();
335 41aae0b0 2014-11-11 marcelgmr }
336 41aae0b0 2014-11-11 marcelgmr
337 3cd6bb98 2014-11-11 marcelgmr /* Comparison used to sort listing entries. */
338 3cd6bb98 2014-11-11 marcelgmr static int
339 3cd6bb98 2014-11-11 marcelgmr rowcmp(const void *a, const void *b)
340 3cd6bb98 2014-11-11 marcelgmr {
341 3cd6bb98 2014-11-11 marcelgmr int isdir1, isdir2, cmpdir;
342 eef51ac5 2015-03-24 marcelgmr const Row *r1 = a;
343 eef51ac5 2015-03-24 marcelgmr const Row *r2 = b;
344 7c74df90 2014-11-15 marcelgmr isdir1 = ISDIR(r1->name);
345 7c74df90 2014-11-15 marcelgmr isdir2 = ISDIR(r2->name);
346 3cd6bb98 2014-11-11 marcelgmr cmpdir = isdir2 - isdir1;
347 3cd6bb98 2014-11-11 marcelgmr return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
348 3cd6bb98 2014-11-11 marcelgmr }
349 3cd6bb98 2014-11-11 marcelgmr
350 496dd2b0 2015-03-24 marcelgmr /* Get all entries in current working directory. */
351 3cd6bb98 2014-11-11 marcelgmr static int
352 496dd2b0 2015-03-24 marcelgmr ls(Row **rowsp, uint8_t flags)
353 3cd6bb98 2014-11-11 marcelgmr {
354 3cd6bb98 2014-11-11 marcelgmr DIR *dp;
355 3cd6bb98 2014-11-11 marcelgmr struct dirent *ep;
356 3cd6bb98 2014-11-11 marcelgmr struct stat statbuf;
357 eef51ac5 2015-03-24 marcelgmr Row *rows;
358 3cd6bb98 2014-11-11 marcelgmr int i, n;
359 3cd6bb98 2014-11-11 marcelgmr
360 496dd2b0 2015-03-24 marcelgmr if(!(dp = opendir("."))) return -1;
361 3cd6bb98 2014-11-11 marcelgmr n = -2; /* We don't want the entries "." and "..". */
362 3cd6bb98 2014-11-11 marcelgmr while (readdir(dp)) n++;
363 3cd6bb98 2014-11-11 marcelgmr rewinddir(dp);
364 f41c2ea1 2014-12-23 marcelgmr rows = malloc(n * sizeof *rows);
365 3cd6bb98 2014-11-11 marcelgmr i = 0;
366 3cd6bb98 2014-11-11 marcelgmr while ((ep = readdir(dp))) {
367 3cd6bb98 2014-11-11 marcelgmr if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
368 3cd6bb98 2014-11-11 marcelgmr continue;
369 3cd6bb98 2014-11-11 marcelgmr if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
370 3cd6bb98 2014-11-11 marcelgmr continue;
371 f1e9b90e 2015-03-30 marcelgmr lstat(ep->d_name, &statbuf);
372 3cd6bb98 2014-11-11 marcelgmr if (S_ISDIR(statbuf.st_mode)) {
373 3cd6bb98 2014-11-11 marcelgmr if (flags & SHOW_DIRS) {
374 f41c2ea1 2014-12-23 marcelgmr rows[i].name = malloc(strlen(ep->d_name) + 2);
375 3cd6bb98 2014-11-11 marcelgmr strcpy(rows[i].name, ep->d_name);
376 3cd6bb98 2014-11-11 marcelgmr strcat(rows[i].name, "/");
377 3cd6bb98 2014-11-11 marcelgmr i++;
378 3cd6bb98 2014-11-11 marcelgmr }
379 590d39af 2014-12-02 marcelgmr } else if (flags & SHOW_FILES) {
380 f41c2ea1 2014-12-23 marcelgmr rows[i].name = malloc(strlen(ep->d_name) + 1);
381 3cd6bb98 2014-11-11 marcelgmr strcpy(rows[i].name, ep->d_name);
382 3cd6bb98 2014-11-11 marcelgmr rows[i].size = statbuf.st_size;
383 3cd6bb98 2014-11-11 marcelgmr i++;
384 3cd6bb98 2014-11-11 marcelgmr }
385 3cd6bb98 2014-11-11 marcelgmr }
386 3cd6bb98 2014-11-11 marcelgmr n = i; /* Ignore unused space in array caused by filters. */
387 f41c2ea1 2014-12-23 marcelgmr qsort(rows, n, sizeof (*rows), rowcmp);
388 3cd6bb98 2014-11-11 marcelgmr closedir(dp);
389 3cd6bb98 2014-11-11 marcelgmr *rowsp = rows;
390 3cd6bb98 2014-11-11 marcelgmr return n;
391 3cd6bb98 2014-11-11 marcelgmr }
392 3cd6bb98 2014-11-11 marcelgmr
393 3cd6bb98 2014-11-11 marcelgmr static void
394 eef51ac5 2015-03-24 marcelgmr free_rows(Row **rowsp, int nfiles)
395 3cd6bb98 2014-11-11 marcelgmr {
396 3cd6bb98 2014-11-11 marcelgmr int i;
397 3cd6bb98 2014-11-11 marcelgmr
398 3cd6bb98 2014-11-11 marcelgmr for (i = 0; i < nfiles; i++)
399 3cd6bb98 2014-11-11 marcelgmr free((*rowsp)[i].name);
400 3cd6bb98 2014-11-11 marcelgmr free(*rowsp);
401 3cd6bb98 2014-11-11 marcelgmr *rowsp = NULL;
402 3cd6bb98 2014-11-11 marcelgmr }
403 3cd6bb98 2014-11-11 marcelgmr
404 8f853fb5 2015-03-25 marcelgmr /* Change working directory to the path in CWD. */
405 71ee1627 2014-11-07 marcelgmr static void
406 ada8fba9 2014-11-09 marcelgmr cd(int reset)
407 71ee1627 2014-11-07 marcelgmr {
408 18016333 2014-11-14 marcelgmr int i, j;
409 18016333 2014-11-14 marcelgmr
410 d0cdbd3c 2014-11-15 marcelgmr message("Loading...", CYAN);
411 d0cdbd3c 2014-11-15 marcelgmr refresh();
412 60128fcd 2014-11-15 marcelgmr if (reset) ESEL = SCROLL = 0;
413 ea0214c3 2014-11-09 marcelgmr chdir(CWD);
414 f05efd9e 2014-11-07 marcelgmr if (rover.nfiles)
415 576e768d 2014-11-08 marcelgmr free_rows(&rover.rows, rover.nfiles);
416 496dd2b0 2015-03-24 marcelgmr rover.nfiles = ls(&rover.rows, FLAGS);
417 18016333 2014-11-14 marcelgmr if (!strcmp(CWD, rover.marks.dirpath)) {
418 18016333 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
419 18016333 2014-11-14 marcelgmr for (j = 0; j < rover.marks.bulk; j++)
420 18016333 2014-11-14 marcelgmr if (
421 18016333 2014-11-14 marcelgmr rover.marks.entries[j] &&
422 7c74df90 2014-11-15 marcelgmr !strcmp(rover.marks.entries[j], ENAME(i))
423 18016333 2014-11-14 marcelgmr )
424 18016333 2014-11-14 marcelgmr break;
425 18016333 2014-11-14 marcelgmr MARKED(i) = j < rover.marks.bulk;
426 18016333 2014-11-14 marcelgmr }
427 590d39af 2014-12-02 marcelgmr } else
428 590d39af 2014-12-02 marcelgmr for (i = 0; i < rover.nfiles; i++)
429 590d39af 2014-12-02 marcelgmr MARKED(i) = 0;
430 d0cdbd3c 2014-11-15 marcelgmr clear_message();
431 743e70b8 2014-11-14 marcelgmr update_view();
432 548a3163 2014-11-14 marcelgmr }
433 548a3163 2014-11-14 marcelgmr
434 430cd7d8 2014-11-19 marcelgmr /* Select a target entry, if it is present. */
435 430cd7d8 2014-11-19 marcelgmr static void
436 430cd7d8 2014-11-19 marcelgmr try_to_sel(const char *target)
437 430cd7d8 2014-11-19 marcelgmr {
438 430cd7d8 2014-11-19 marcelgmr ESEL = 0;
439 f499f7c7 2014-12-03 marcelgmr if (!ISDIR(target))
440 f499f7c7 2014-12-03 marcelgmr while ((ESEL+1) < rover.nfiles && ISDIR(ENAME(ESEL)))
441 f499f7c7 2014-12-03 marcelgmr ESEL++;
442 00ecdc1e 2014-11-19 marcelgmr while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
443 430cd7d8 2014-11-19 marcelgmr ESEL++;
444 430cd7d8 2014-11-19 marcelgmr if (rover.nfiles > HEIGHT) {
445 430cd7d8 2014-11-19 marcelgmr SCROLL = ESEL - (HEIGHT >> 1);
446 430cd7d8 2014-11-19 marcelgmr SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
447 430cd7d8 2014-11-19 marcelgmr }
448 430cd7d8 2014-11-19 marcelgmr }
449 430cd7d8 2014-11-19 marcelgmr
450 00ecdc1e 2014-11-19 marcelgmr /* Reload CWD, but try to keep selection. */
451 00ecdc1e 2014-11-19 marcelgmr static void
452 00ecdc1e 2014-11-19 marcelgmr reload()
453 00ecdc1e 2014-11-19 marcelgmr {
454 00ecdc1e 2014-11-19 marcelgmr if (rover.nfiles) {
455 00ecdc1e 2014-11-19 marcelgmr strcpy(INPUT, ENAME(ESEL));
456 00ecdc1e 2014-11-19 marcelgmr cd(1);
457 00ecdc1e 2014-11-19 marcelgmr try_to_sel(INPUT);
458 00ecdc1e 2014-11-19 marcelgmr update_view();
459 590d39af 2014-12-02 marcelgmr } else
460 590d39af 2014-12-02 marcelgmr cd(1);
461 00ecdc1e 2014-11-19 marcelgmr }
462 00ecdc1e 2014-11-19 marcelgmr
463 5c4a6b62 2014-11-14 marcelgmr /* Recursively process a source directory using CWD as destination root.
464 c8891c90 2014-12-23 marcelgmr For each node (i.e. directory), do the following:
465 c8891c90 2014-12-23 marcelgmr 1. call pre(destination);
466 c8891c90 2014-12-23 marcelgmr 2. call proc() on every child leaf (i.e. files);
467 c8891c90 2014-12-23 marcelgmr 3. recurse into every child node;
468 c8891c90 2014-12-23 marcelgmr 4. call pos(source).
469 c8891c90 2014-12-23 marcelgmr E.g. to move directory /src/ (and all its contents) inside /dst/:
470 c8891c90 2014-12-23 marcelgmr strcpy(CWD, "/dst/");
471 c8891c90 2014-12-23 marcelgmr process_dir(adddir, movfile, deldir, "/src/"); */
472 2a117cd2 2014-11-15 marcelgmr static int
473 845f2b87 2014-11-14 marcelgmr process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
474 548a3163 2014-11-14 marcelgmr {
475 2a117cd2 2014-11-15 marcelgmr int ret;
476 548a3163 2014-11-14 marcelgmr DIR *dp;
477 548a3163 2014-11-14 marcelgmr struct dirent *ep;
478 548a3163 2014-11-14 marcelgmr struct stat statbuf;
479 c04b0d89 2015-03-30 marcelgmr char subpath[PATH_MAX];
480 548a3163 2014-11-14 marcelgmr
481 2a117cd2 2014-11-15 marcelgmr ret = 0;
482 68ff0591 2014-11-14 marcelgmr if (pre) {
483 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
484 68ff0591 2014-11-14 marcelgmr strcpy(dstpath, CWD);
485 68ff0591 2014-11-14 marcelgmr strcat(dstpath, path + strlen(rover.marks.dirpath));
486 2a117cd2 2014-11-15 marcelgmr ret |= pre(dstpath);
487 68ff0591 2014-11-14 marcelgmr }
488 2a117cd2 2014-11-15 marcelgmr if(!(dp = opendir(path))) return -1;
489 548a3163 2014-11-14 marcelgmr while ((ep = readdir(dp))) {
490 548a3163 2014-11-14 marcelgmr if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
491 548a3163 2014-11-14 marcelgmr continue;
492 e5fe9820 2015-04-17 marcelgmr snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
493 548a3163 2014-11-14 marcelgmr stat(subpath, &statbuf);
494 548a3163 2014-11-14 marcelgmr if (S_ISDIR(statbuf.st_mode)) {
495 548a3163 2014-11-14 marcelgmr strcat(subpath, "/");
496 2a117cd2 2014-11-15 marcelgmr ret |= process_dir(pre, proc, pos, subpath);
497 590d39af 2014-12-02 marcelgmr } else
498 590d39af 2014-12-02 marcelgmr ret |= proc(subpath);
499 548a3163 2014-11-14 marcelgmr }
500 548a3163 2014-11-14 marcelgmr closedir(dp);
501 2a117cd2 2014-11-15 marcelgmr if (pos) ret |= pos(path);
502 2a117cd2 2014-11-15 marcelgmr return ret;
503 548a3163 2014-11-14 marcelgmr }
504 548a3163 2014-11-14 marcelgmr
505 5c4a6b62 2014-11-14 marcelgmr /* Process all marked entries using CWD as destination root.
506 c8891c90 2014-12-23 marcelgmr All marked entries that are directories will be recursively processed.
507 c8891c90 2014-12-23 marcelgmr See process_dir() for details on the parameters. */
508 548a3163 2014-11-14 marcelgmr static void
509 845f2b87 2014-11-14 marcelgmr process_marked(PROCESS pre, PROCESS proc, PROCESS pos)
510 548a3163 2014-11-14 marcelgmr {
511 2a117cd2 2014-11-15 marcelgmr int i, ret;
512 c04b0d89 2015-03-30 marcelgmr char path[PATH_MAX];
513 548a3163 2014-11-14 marcelgmr
514 d0cdbd3c 2014-11-15 marcelgmr clear_message();
515 d0cdbd3c 2014-11-15 marcelgmr message("Processing...", CYAN);
516 d0cdbd3c 2014-11-15 marcelgmr refresh();
517 548a3163 2014-11-14 marcelgmr for (i = 0; i < rover.marks.bulk; i++)
518 548a3163 2014-11-14 marcelgmr if (rover.marks.entries[i]) {
519 2a117cd2 2014-11-15 marcelgmr ret = 0;
520 e5fe9820 2015-04-17 marcelgmr snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, rover.marks.entries[i]);
521 7c74df90 2014-11-15 marcelgmr if (ISDIR(rover.marks.entries[i])) {
522 b25c9834 2014-11-14 marcelgmr if (!strncmp(path, CWD, strlen(path)))
523 2a117cd2 2014-11-15 marcelgmr ret = -1;
524 b25c9834 2014-11-14 marcelgmr else
525 2a117cd2 2014-11-15 marcelgmr ret = process_dir(pre, proc, pos, path);
526 590d39af 2014-12-02 marcelgmr } else
527 590d39af 2014-12-02 marcelgmr ret = proc(path);
528 2a117cd2 2014-11-15 marcelgmr if (!ret) del_mark(&rover.marks, rover.marks.entries[i]);
529 2a117cd2 2014-11-15 marcelgmr }
530 00ecdc1e 2014-11-19 marcelgmr reload();
531 2a117cd2 2014-11-15 marcelgmr if (!rover.marks.nentries)
532 bad36707 2014-11-19 marcelgmr message("Done.", GREEN);
533 2a117cd2 2014-11-15 marcelgmr else
534 bad36707 2014-11-19 marcelgmr message("Some errors occured.", RED);
535 3cd6bb98 2014-11-11 marcelgmr }
536 3cd6bb98 2014-11-11 marcelgmr
537 b7ee4ee2 2014-11-14 marcelgmr /* Wrappers for file operations. */
538 ca8ebdce 2014-11-14 marcelgmr static PROCESS delfile = unlink;
539 b7ee4ee2 2014-11-14 marcelgmr static PROCESS deldir = rmdir;
540 1447784d 2014-11-14 marcelgmr static int addfile(const char *path) {
541 1447784d 2014-11-14 marcelgmr /* Using creat(2) because mknod(2) doesn't seem to be portable. */
542 1447784d 2014-11-14 marcelgmr int ret;
543 1447784d 2014-11-14 marcelgmr
544 1447784d 2014-11-14 marcelgmr ret = creat(path, 0644);
545 1447784d 2014-11-14 marcelgmr if (ret < 0) return ret;
546 1447784d 2014-11-14 marcelgmr return close(ret);
547 1447784d 2014-11-14 marcelgmr }
548 ca8ebdce 2014-11-14 marcelgmr static int cpyfile(const char *srcpath) {
549 b7ee4ee2 2014-11-14 marcelgmr int src, dst, ret;
550 b7ee4ee2 2014-11-14 marcelgmr size_t size;
551 b7ee4ee2 2014-11-14 marcelgmr struct stat st;
552 b7ee4ee2 2014-11-14 marcelgmr char buf[BUFSIZ];
553 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
554 b7ee4ee2 2014-11-14 marcelgmr
555 b7ee4ee2 2014-11-14 marcelgmr ret = src = open(srcpath, O_RDONLY);
556 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
557 b7ee4ee2 2014-11-14 marcelgmr ret = fstat(src, &st);
558 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
559 b7ee4ee2 2014-11-14 marcelgmr strcpy(dstpath, CWD);
560 b7ee4ee2 2014-11-14 marcelgmr strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
561 b7ee4ee2 2014-11-14 marcelgmr ret = dst = creat(dstpath, st.st_mode);
562 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
563 b7ee4ee2 2014-11-14 marcelgmr while ((size = read(src, buf, BUFSIZ)) > 0)
564 b7ee4ee2 2014-11-14 marcelgmr write(dst, buf, size);
565 b7ee4ee2 2014-11-14 marcelgmr close(src);
566 b7ee4ee2 2014-11-14 marcelgmr close(dst);
567 b7ee4ee2 2014-11-14 marcelgmr return 0;
568 b7ee4ee2 2014-11-14 marcelgmr }
569 b7ee4ee2 2014-11-14 marcelgmr static int adddir(const char *path) {
570 b7ee4ee2 2014-11-14 marcelgmr int ret;
571 b7ee4ee2 2014-11-14 marcelgmr struct stat st;
572 b7ee4ee2 2014-11-14 marcelgmr
573 b7ee4ee2 2014-11-14 marcelgmr ret = stat(CWD, &st);
574 b7ee4ee2 2014-11-14 marcelgmr if (ret < 0) return ret;
575 b7ee4ee2 2014-11-14 marcelgmr return mkdir(path, st.st_mode);
576 ca8ebdce 2014-11-14 marcelgmr }
577 0ea64589 2014-11-14 marcelgmr static int movfile(const char *srcpath) {
578 8a757b0f 2015-03-07 marcelgmr int ret;
579 c04b0d89 2015-03-30 marcelgmr char dstpath[PATH_MAX];
580 ca8ebdce 2014-11-14 marcelgmr
581 0ea64589 2014-11-14 marcelgmr strcpy(dstpath, CWD);
582 0ea64589 2014-11-14 marcelgmr strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
583 8a757b0f 2015-03-07 marcelgmr ret = rename(srcpath, dstpath);
584 8a757b0f 2015-03-07 marcelgmr if (ret < 0 && errno == EXDEV) {
585 8a757b0f 2015-03-07 marcelgmr ret = cpyfile(srcpath);
586 8a757b0f 2015-03-07 marcelgmr if (ret < 0) return ret;
587 8a757b0f 2015-03-07 marcelgmr ret = delfile(srcpath);
588 8a757b0f 2015-03-07 marcelgmr }
589 8a757b0f 2015-03-07 marcelgmr return ret;
590 b7ee4ee2 2014-11-14 marcelgmr }
591 b7ee4ee2 2014-11-14 marcelgmr
592 6c0f3741 2014-11-11 marcelgmr /* Do a fork-exec to external program (e.g. $EDITOR). */
593 3cd6bb98 2014-11-11 marcelgmr static void
594 71ee1627 2014-11-07 marcelgmr spawn()
595 71ee1627 2014-11-07 marcelgmr {
596 71ee1627 2014-11-07 marcelgmr pid_t pid;
597 71ee1627 2014-11-07 marcelgmr int status;
598 4c4d5598 2015-04-17 marcelgmr struct sigaction sa;
599 71ee1627 2014-11-07 marcelgmr
600 71ee1627 2014-11-07 marcelgmr pid = fork();
601 71ee1627 2014-11-07 marcelgmr if (pid > 0) {
602 71ee1627 2014-11-07 marcelgmr /* fork() succeeded. */
603 e1a8adcd 2015-04-17 marcelgmr memset(&sa, 0, sizeof (struct sigaction));
604 e1a8adcd 2015-04-17 marcelgmr sa.sa_handler = SIG_DFL;
605 e1a8adcd 2015-04-17 marcelgmr sigaction(SIGSEGV, &sa, NULL);
606 e1a8adcd 2015-04-17 marcelgmr sigaction(SIGWINCH, &sa, NULL);
607 7c74df90 2014-11-15 marcelgmr endwin();
608 ea0214c3 2014-11-09 marcelgmr waitpid(pid, &status, 0);
609 f05efd9e 2014-11-07 marcelgmr init_term();
610 3e2a6afd 2015-04-17 marcelgmr handle_winch(0);
611 590d39af 2014-12-02 marcelgmr } else if (pid == 0) {
612 71ee1627 2014-11-07 marcelgmr /* Child process. */
613 3cd6bb98 2014-11-11 marcelgmr execvp(ARGS[0], ARGS);
614 411ec846 2014-11-08 marcelgmr }
615 411ec846 2014-11-08 marcelgmr }
616 411ec846 2014-11-08 marcelgmr
617 411ec846 2014-11-08 marcelgmr /* Interactive getstr(). */
618 3cd6bb98 2014-11-11 marcelgmr static int
619 411ec846 2014-11-08 marcelgmr igetstr(char *buffer, int maxlen)
620 411ec846 2014-11-08 marcelgmr {
621 411ec846 2014-11-08 marcelgmr int ch, length;
622 411ec846 2014-11-08 marcelgmr
623 411ec846 2014-11-08 marcelgmr length = strlen(buffer);
624 d5fe31b0 2014-11-14 marcelgmr curs_set(TRUE);
625 411ec846 2014-11-08 marcelgmr ch = getch();
626 d5fe31b0 2014-11-14 marcelgmr if (ch == '\r' || ch == '\n' || ch == KEY_DOWN || ch == KEY_ENTER) {
627 d5fe31b0 2014-11-14 marcelgmr curs_set(FALSE);
628 411ec846 2014-11-08 marcelgmr return 0;
629 590d39af 2014-12-02 marcelgmr } else if (ch == erasechar() || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
630 411ec846 2014-11-08 marcelgmr if (length)
631 411ec846 2014-11-08 marcelgmr buffer[--length] = '\0';
632 590d39af 2014-12-02 marcelgmr } else if (ch == killchar()) {
633 411ec846 2014-11-08 marcelgmr length = 0;
634 411ec846 2014-11-08 marcelgmr buffer[0] = '\0';
635 590d39af 2014-12-02 marcelgmr } else if (length < maxlen - 1 && isprint(ch)) {
636 411ec846 2014-11-08 marcelgmr buffer[length++] = ch;
637 411ec846 2014-11-08 marcelgmr buffer[length] = '\0';
638 411ec846 2014-11-08 marcelgmr }
639 411ec846 2014-11-08 marcelgmr return 1;
640 743e70b8 2014-11-14 marcelgmr }
641 743e70b8 2014-11-14 marcelgmr
642 5c4a6b62 2014-11-14 marcelgmr /* Update line input on the screen. */
643 743e70b8 2014-11-14 marcelgmr static void
644 eef51ac5 2015-03-24 marcelgmr update_input(char *prompt, Color color)
645 743e70b8 2014-11-14 marcelgmr {
646 743e70b8 2014-11-14 marcelgmr int plen, ilen;
647 743e70b8 2014-11-14 marcelgmr
648 743e70b8 2014-11-14 marcelgmr plen = strlen(prompt);
649 743e70b8 2014-11-14 marcelgmr ilen = strlen(INPUT);
650 743e70b8 2014-11-14 marcelgmr color_set(RVC_PROMPT, NULL);
651 743e70b8 2014-11-14 marcelgmr mvaddstr(LINES - 1, 0, prompt);
652 743e70b8 2014-11-14 marcelgmr color_set(color, NULL);
653 743e70b8 2014-11-14 marcelgmr mvaddstr(LINES - 1, plen, INPUT);
654 743e70b8 2014-11-14 marcelgmr mvaddch(LINES - 1, ilen + plen, ' ');
655 743e70b8 2014-11-14 marcelgmr move(LINES - 1, ilen + plen);
656 743e70b8 2014-11-14 marcelgmr color_set(DEFAULT, NULL);
657 e1c42cfe 2014-11-11 marcelgmr }
658 e1c42cfe 2014-11-11 marcelgmr
659 a31a7b29 2014-11-15 marcelgmr /* Show a message on the status bar. */
660 50c63039 2014-11-14 marcelgmr static void
661 eef51ac5 2015-03-24 marcelgmr message(const char *msg, Color color)
662 50c63039 2014-11-14 marcelgmr {
663 50c63039 2014-11-14 marcelgmr int len, pos;
664 50c63039 2014-11-14 marcelgmr
665 50c63039 2014-11-14 marcelgmr len = strlen(msg);
666 50c63039 2014-11-14 marcelgmr pos = (STATUSPOS - len) >> 1;
667 50c63039 2014-11-14 marcelgmr attr_on(A_BOLD, NULL);
668 50c63039 2014-11-14 marcelgmr color_set(color, NULL);
669 50c63039 2014-11-14 marcelgmr mvaddstr(LINES - 1, pos, msg);
670 50c63039 2014-11-14 marcelgmr color_set(DEFAULT, NULL);
671 50c63039 2014-11-14 marcelgmr attr_off(A_BOLD, NULL);
672 50c63039 2014-11-14 marcelgmr }
673 50c63039 2014-11-14 marcelgmr
674 d0cdbd3c 2014-11-15 marcelgmr /* Clear message area, leaving only status info. */
675 865c9839 2014-11-15 marcelgmr static void
676 865c9839 2014-11-15 marcelgmr clear_message()
677 865c9839 2014-11-15 marcelgmr {
678 865c9839 2014-11-15 marcelgmr mvhline(LINES - 1, 0, ' ', STATUSPOS);
679 865c9839 2014-11-15 marcelgmr }
680 865c9839 2014-11-15 marcelgmr
681 71ee1627 2014-11-07 marcelgmr int
682 38a357b6 2014-11-09 marcelgmr main(int argc, char *argv[])
683 71ee1627 2014-11-07 marcelgmr {
684 ada8fba9 2014-11-09 marcelgmr int i, ch;
685 14b5cd28 2015-01-06 marcelgmr char *program;
686 14b5cd28 2015-01-06 marcelgmr const char *key;
687 38a357b6 2014-11-09 marcelgmr DIR *d;
688 71ee1627 2014-11-07 marcelgmr
689 ff684771 2014-12-02 marcelgmr if (argc == 2) {
690 ff684771 2014-12-02 marcelgmr if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
691 ff684771 2014-12-02 marcelgmr printf("rover %s\n", RV_VERSION);
692 ff684771 2014-12-02 marcelgmr return 0;
693 ff684771 2014-12-02 marcelgmr } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
694 ff684771 2014-12-02 marcelgmr printf(
695 ff684771 2014-12-02 marcelgmr "Usage: rover [DIRECTORY [DIRECTORY [DIRECTORY [...]]]]\n"
696 ff684771 2014-12-02 marcelgmr " or: rover [OPTION]\n"
697 ff684771 2014-12-02 marcelgmr "Browse current working directory or the ones specified.\n\n"
698 ff684771 2014-12-02 marcelgmr "Options:\n"
699 ff684771 2014-12-02 marcelgmr " -h, --help print this help message and exit\n"
700 ff684771 2014-12-02 marcelgmr " -v, --version print program version and exit\n\n"
701 ff684771 2014-12-02 marcelgmr "See rover(1) for more information.\n\n"
702 ff684771 2014-12-02 marcelgmr "Rover homepage: <https://github.com/lecram/rover>.\n"
703 ff684771 2014-12-02 marcelgmr );
704 ff684771 2014-12-02 marcelgmr return 0;
705 ff684771 2014-12-02 marcelgmr }
706 ff684771 2014-12-02 marcelgmr }
707 f05efd9e 2014-11-07 marcelgmr init_term();
708 f05efd9e 2014-11-07 marcelgmr rover.nfiles = 0;
709 47e9f26d 2014-11-09 marcelgmr for (i = 0; i < 10; i++) {
710 7c74df90 2014-11-15 marcelgmr rover.esel[i] = rover.scroll[i] = 0;
711 47e9f26d 2014-11-09 marcelgmr rover.flags[i] = SHOW_FILES | SHOW_DIRS;
712 47e9f26d 2014-11-09 marcelgmr }
713 ada8fba9 2014-11-09 marcelgmr strcpy(rover.cwd[0], getenv("HOME"));
714 38a357b6 2014-11-09 marcelgmr for (i = 1; i < argc && i < 10; i++) {
715 7c74df90 2014-11-15 marcelgmr if ((d = opendir(argv[i]))) {
716 c04b0d89 2015-03-30 marcelgmr realpath(argv[i], rover.cwd[i]);
717 38a357b6 2014-11-09 marcelgmr closedir(d);
718 590d39af 2014-12-02 marcelgmr } else
719 590d39af 2014-12-02 marcelgmr strcpy(rover.cwd[i], rover.cwd[0]);
720 38a357b6 2014-11-09 marcelgmr }
721 c04b0d89 2015-03-30 marcelgmr getcwd(rover.cwd[i], PATH_MAX);
722 38a357b6 2014-11-09 marcelgmr for (i++; i < 10; i++)
723 38a357b6 2014-11-09 marcelgmr strcpy(rover.cwd[i], rover.cwd[i-1]);
724 38a357b6 2014-11-09 marcelgmr for (i = 0; i < 10; i++)
725 38a357b6 2014-11-09 marcelgmr if (rover.cwd[i][strlen(rover.cwd[i]) - 1] != '/')
726 38a357b6 2014-11-09 marcelgmr strcat(rover.cwd[i], "/");
727 ada8fba9 2014-11-09 marcelgmr rover.tab = 1;
728 ada8fba9 2014-11-09 marcelgmr rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
729 18016333 2014-11-14 marcelgmr init_marks(&rover.marks);
730 ada8fba9 2014-11-09 marcelgmr cd(1);
731 0b301167 2014-11-07 marcelgmr while (1) {
732 ada8fba9 2014-11-09 marcelgmr ch = getch();
733 ada8fba9 2014-11-09 marcelgmr key = keyname(ch);
734 865c9839 2014-11-15 marcelgmr clear_message();
735 a31a7b29 2014-11-15 marcelgmr if (!strcmp(key, RVK_QUIT)) break;
736 ada8fba9 2014-11-09 marcelgmr else if (ch >= '0' && ch <= '9') {
737 ada8fba9 2014-11-09 marcelgmr rover.tab = ch - '0';
738 ada8fba9 2014-11-09 marcelgmr cd(0);
739 ff684771 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_HELP)) {
740 ff684771 2014-12-02 marcelgmr ARGS[0] = "man";
741 ff684771 2014-12-02 marcelgmr ARGS[1] = "rover";
742 ff684771 2014-12-02 marcelgmr ARGS[2] = NULL;
743 ff684771 2014-12-02 marcelgmr spawn();
744 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_DOWN)) {
745 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
746 7c74df90 2014-11-15 marcelgmr ESEL = (ESEL + 1) % rover.nfiles;
747 743e70b8 2014-11-14 marcelgmr update_view();
748 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_UP)) {
749 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
750 7c74df90 2014-11-15 marcelgmr ESEL = ESEL ? ESEL - 1 : rover.nfiles - 1;
751 743e70b8 2014-11-14 marcelgmr update_view();
752 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_JUMP_DOWN)) {
753 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
754 60128fcd 2014-11-15 marcelgmr ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
755 60128fcd 2014-11-15 marcelgmr if (rover.nfiles > HEIGHT)
756 60128fcd 2014-11-15 marcelgmr SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
757 743e70b8 2014-11-14 marcelgmr update_view();
758 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_JUMP_UP)) {
759 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
760 60128fcd 2014-11-15 marcelgmr ESEL = MAX(ESEL - RV_JUMP, 0);
761 60128fcd 2014-11-15 marcelgmr SCROLL = MAX(SCROLL - RV_JUMP, 0);
762 743e70b8 2014-11-14 marcelgmr update_view();
763 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_CD_DOWN)) {
764 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || !ISDIR(ENAME(ESEL))) continue;
765 7c74df90 2014-11-15 marcelgmr strcat(CWD, ENAME(ESEL));
766 ada8fba9 2014-11-09 marcelgmr cd(1);
767 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_CD_UP)) {
768 45020106 2014-11-08 marcelgmr char *dirname, first;
769 58fbefc5 2014-11-19 marcelgmr if (!strcmp(CWD, "/")) continue;
770 ada8fba9 2014-11-09 marcelgmr CWD[strlen(CWD) - 1] = '\0';
771 ada8fba9 2014-11-09 marcelgmr dirname = strrchr(CWD, '/') + 1;
772 45020106 2014-11-08 marcelgmr first = dirname[0];
773 45020106 2014-11-08 marcelgmr dirname[0] = '\0';
774 ada8fba9 2014-11-09 marcelgmr cd(1);
775 430cd7d8 2014-11-19 marcelgmr dirname[0] = first;
776 430cd7d8 2014-11-19 marcelgmr dirname[strlen(dirname)] = '/';
777 430cd7d8 2014-11-19 marcelgmr try_to_sel(dirname);
778 430cd7d8 2014-11-19 marcelgmr dirname[0] = '\0';
779 430cd7d8 2014-11-19 marcelgmr update_view();
780 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_HOME)) {
781 ada8fba9 2014-11-09 marcelgmr strcpy(CWD, getenv("HOME"));
782 ada8fba9 2014-11-09 marcelgmr if (CWD[strlen(CWD) - 1] != '/')
783 ada8fba9 2014-11-09 marcelgmr strcat(CWD, "/");
784 ada8fba9 2014-11-09 marcelgmr cd(1);
785 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_SHELL)) {
786 71ee1627 2014-11-07 marcelgmr program = getenv("SHELL");
787 71ee1627 2014-11-07 marcelgmr if (program) {
788 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
789 3cd6bb98 2014-11-11 marcelgmr ARGS[1] = NULL;
790 71ee1627 2014-11-07 marcelgmr spawn();
791 00ecdc1e 2014-11-19 marcelgmr reload();
792 71ee1627 2014-11-07 marcelgmr }
793 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_VIEW)) {
794 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
795 4b66a2c4 2014-11-07 marcelgmr program = getenv("PAGER");
796 4b66a2c4 2014-11-07 marcelgmr if (program) {
797 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
798 7c74df90 2014-11-15 marcelgmr ARGS[1] = ENAME(ESEL);
799 3cd6bb98 2014-11-11 marcelgmr ARGS[2] = NULL;
800 4b66a2c4 2014-11-07 marcelgmr spawn();
801 4b66a2c4 2014-11-07 marcelgmr }
802 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_EDIT)) {
803 7c74df90 2014-11-15 marcelgmr if (!rover.nfiles || ISDIR(ENAME(ESEL))) continue;
804 71ee1627 2014-11-07 marcelgmr program = getenv("EDITOR");
805 71ee1627 2014-11-07 marcelgmr if (program) {
806 3cd6bb98 2014-11-11 marcelgmr ARGS[0] = program;
807 7c74df90 2014-11-15 marcelgmr ARGS[1] = ENAME(ESEL);
808 3cd6bb98 2014-11-11 marcelgmr ARGS[2] = NULL;
809 71ee1627 2014-11-07 marcelgmr spawn();
810 39ee8f33 2014-11-14 marcelgmr cd(0);
811 71ee1627 2014-11-07 marcelgmr }
812 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_SEARCH)) {
813 1447784d 2014-11-14 marcelgmr int oldsel, oldscroll, length;
814 743e70b8 2014-11-14 marcelgmr char *prompt = "search: ";
815 bb90b2d2 2014-11-08 marcelgmr if (!rover.nfiles) continue;
816 7c74df90 2014-11-15 marcelgmr oldsel = ESEL;
817 ada8fba9 2014-11-09 marcelgmr oldscroll = SCROLL;
818 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
819 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
820 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
821 1447784d 2014-11-14 marcelgmr int sel;
822 eef51ac5 2015-03-24 marcelgmr Color color = RED;
823 1447784d 2014-11-14 marcelgmr length = strlen(INPUT);
824 a17e36e3 2014-11-07 marcelgmr if (length) {
825 f05efd9e 2014-11-07 marcelgmr for (sel = 0; sel < rover.nfiles; sel++)
826 7c74df90 2014-11-15 marcelgmr if (!strncmp(ENAME(sel), INPUT, length))
827 a17e36e3 2014-11-07 marcelgmr break;
828 f05efd9e 2014-11-07 marcelgmr if (sel < rover.nfiles) {
829 a17e36e3 2014-11-07 marcelgmr color = GREEN;
830 7c74df90 2014-11-15 marcelgmr ESEL = sel;
831 f05efd9e 2014-11-07 marcelgmr if (rover.nfiles > HEIGHT) {
832 89cfd83b 2014-11-08 marcelgmr if (sel < 3)
833 ada8fba9 2014-11-09 marcelgmr SCROLL = 0;
834 89cfd83b 2014-11-08 marcelgmr else if (sel - 3 > rover.nfiles - HEIGHT)
835 ada8fba9 2014-11-09 marcelgmr SCROLL = rover.nfiles - HEIGHT;
836 a17e36e3 2014-11-07 marcelgmr else
837 ada8fba9 2014-11-09 marcelgmr SCROLL = sel - 3;
838 a17e36e3 2014-11-07 marcelgmr }
839 a17e36e3 2014-11-07 marcelgmr }
840 590d39af 2014-12-02 marcelgmr } else {
841 7c74df90 2014-11-15 marcelgmr ESEL = oldsel;
842 ada8fba9 2014-11-09 marcelgmr SCROLL = oldscroll;
843 411ec846 2014-11-08 marcelgmr }
844 743e70b8 2014-11-14 marcelgmr update_view();
845 743e70b8 2014-11-14 marcelgmr update_input(prompt, color);
846 a17e36e3 2014-11-07 marcelgmr }
847 865c9839 2014-11-15 marcelgmr clear_message();
848 743e70b8 2014-11-14 marcelgmr update_view();
849 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_FILES)) {
850 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_FILES;
851 00ecdc1e 2014-11-19 marcelgmr reload();
852 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_DIRS)) {
853 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_DIRS;
854 00ecdc1e 2014-11-19 marcelgmr reload();
855 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_HIDDEN)) {
856 47e9f26d 2014-11-09 marcelgmr FLAGS ^= SHOW_HIDDEN;
857 00ecdc1e 2014-11-19 marcelgmr reload();
858 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_NEW_FILE)) {
859 743e70b8 2014-11-14 marcelgmr int ok = 0;
860 743e70b8 2014-11-14 marcelgmr char *prompt = "new file: ";
861 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
862 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
863 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
864 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
865 1447784d 2014-11-14 marcelgmr ok = 1;
866 e444d3b1 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
867 ddbe9d62 2014-11-14 marcelgmr if (
868 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
869 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
870 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
871 ddbe9d62 2014-11-14 marcelgmr ) {
872 1447784d 2014-11-14 marcelgmr ok = 0;
873 1447784d 2014-11-14 marcelgmr break;
874 1447784d 2014-11-14 marcelgmr }
875 e444d3b1 2014-11-14 marcelgmr }
876 743e70b8 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
877 1447784d 2014-11-14 marcelgmr }
878 865c9839 2014-11-15 marcelgmr clear_message();
879 743e70b8 2014-11-14 marcelgmr if (strlen(INPUT)) {
880 430cd7d8 2014-11-19 marcelgmr if (ok) {
881 430cd7d8 2014-11-19 marcelgmr addfile(INPUT);
882 430cd7d8 2014-11-19 marcelgmr cd(1);
883 430cd7d8 2014-11-19 marcelgmr try_to_sel(INPUT);
884 430cd7d8 2014-11-19 marcelgmr update_view();
885 590d39af 2014-12-02 marcelgmr } else
886 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
887 1447784d 2014-11-14 marcelgmr }
888 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_NEW_DIR)) {
889 743e70b8 2014-11-14 marcelgmr int ok = 0;
890 743e70b8 2014-11-14 marcelgmr char *prompt = "new directory: ";
891 1447784d 2014-11-14 marcelgmr strcpy(INPUT, "");
892 743e70b8 2014-11-14 marcelgmr update_input(prompt, DEFAULT);
893 1447784d 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
894 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
895 1447784d 2014-11-14 marcelgmr ok = 1;
896 e444d3b1 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
897 ddbe9d62 2014-11-14 marcelgmr if (
898 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
899 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
900 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
901 ddbe9d62 2014-11-14 marcelgmr ) {
902 1447784d 2014-11-14 marcelgmr ok = 0;
903 1447784d 2014-11-14 marcelgmr break;
904 1447784d 2014-11-14 marcelgmr }
905 e444d3b1 2014-11-14 marcelgmr }
906 743e70b8 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
907 1447784d 2014-11-14 marcelgmr }
908 865c9839 2014-11-15 marcelgmr clear_message();
909 743e70b8 2014-11-14 marcelgmr if (strlen(INPUT)) {
910 430cd7d8 2014-11-19 marcelgmr if (ok) {
911 430cd7d8 2014-11-19 marcelgmr adddir(INPUT);
912 430cd7d8 2014-11-19 marcelgmr cd(1);
913 430cd7d8 2014-11-19 marcelgmr try_to_sel(INPUT);
914 430cd7d8 2014-11-19 marcelgmr update_view();
915 590d39af 2014-12-02 marcelgmr } else
916 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
917 1447784d 2014-11-14 marcelgmr }
918 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_RENAME)) {
919 bdb195d0 2014-11-14 marcelgmr int ok = 0;
920 bdb195d0 2014-11-14 marcelgmr char *prompt = "rename: ";
921 f499f7c7 2014-12-03 marcelgmr char *last;
922 f499f7c7 2014-12-03 marcelgmr int isdir;
923 7c74df90 2014-11-15 marcelgmr strcpy(INPUT, ENAME(ESEL));
924 f499f7c7 2014-12-03 marcelgmr last = INPUT + strlen(INPUT) - 1;
925 f499f7c7 2014-12-03 marcelgmr if ((isdir = *last == '/'))
926 f499f7c7 2014-12-03 marcelgmr *last = '\0';
927 ddbe9d62 2014-11-14 marcelgmr update_input(prompt, RED);
928 bdb195d0 2014-11-14 marcelgmr while (igetstr(INPUT, INPUTSZ)) {
929 ddbe9d62 2014-11-14 marcelgmr int length = strlen(INPUT);
930 bdb195d0 2014-11-14 marcelgmr ok = 1;
931 5f047452 2014-12-03 marcelgmr for (i = 0; i < rover.nfiles; i++)
932 ddbe9d62 2014-11-14 marcelgmr if (
933 7c74df90 2014-11-15 marcelgmr !strncmp(ENAME(i), INPUT, length) &&
934 7c74df90 2014-11-15 marcelgmr (!strcmp(ENAME(i) + length, "") ||
935 7c74df90 2014-11-15 marcelgmr !strcmp(ENAME(i) + length, "/"))
936 ddbe9d62 2014-11-14 marcelgmr ) {
937 bdb195d0 2014-11-14 marcelgmr ok = 0;
938 bdb195d0 2014-11-14 marcelgmr break;
939 bdb195d0 2014-11-14 marcelgmr }
940 bdb195d0 2014-11-14 marcelgmr update_input(prompt, ok ? GREEN : RED);
941 bdb195d0 2014-11-14 marcelgmr }
942 865c9839 2014-11-15 marcelgmr clear_message();
943 bdb195d0 2014-11-14 marcelgmr if (strlen(INPUT)) {
944 f499f7c7 2014-12-03 marcelgmr if (isdir)
945 f499f7c7 2014-12-03 marcelgmr strcat(INPUT, "/");
946 00ecdc1e 2014-11-19 marcelgmr if (ok) {
947 dbb53e68 2014-12-03 marcelgmr if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
948 dbb53e68 2014-12-03 marcelgmr del_mark(&rover.marks, ENAME(ESEL));
949 dbb53e68 2014-12-03 marcelgmr add_mark(&rover.marks, CWD, INPUT);
950 dbb53e68 2014-12-03 marcelgmr }
951 00ecdc1e 2014-11-19 marcelgmr cd(1);
952 00ecdc1e 2014-11-19 marcelgmr try_to_sel(INPUT);
953 00ecdc1e 2014-11-19 marcelgmr update_view();
954 590d39af 2014-12-02 marcelgmr } else
955 590d39af 2014-12-02 marcelgmr message("File already exists.", RED);
956 bdb195d0 2014-11-14 marcelgmr }
957 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_TG_MARK)) {
958 7c74df90 2014-11-15 marcelgmr if (MARKED(ESEL))
959 7c74df90 2014-11-15 marcelgmr del_mark(&rover.marks, ENAME(ESEL));
960 18016333 2014-11-14 marcelgmr else
961 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(ESEL));
962 7c74df90 2014-11-15 marcelgmr MARKED(ESEL) = !MARKED(ESEL);
963 7c74df90 2014-11-15 marcelgmr ESEL = (ESEL + 1) % rover.nfiles;
964 743e70b8 2014-11-14 marcelgmr update_view();
965 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_INVMARK)) {
966 5fc8b927 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++) {
967 5fc8b927 2014-11-14 marcelgmr if (MARKED(i))
968 7c74df90 2014-11-15 marcelgmr del_mark(&rover.marks, ENAME(i));
969 5fc8b927 2014-11-14 marcelgmr else
970 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(i));
971 5fc8b927 2014-11-14 marcelgmr MARKED(i) = !MARKED(i);
972 5fc8b927 2014-11-14 marcelgmr }
973 743e70b8 2014-11-14 marcelgmr update_view();
974 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_MARKALL)) {
975 5fc8b927 2014-11-14 marcelgmr for (i = 0; i < rover.nfiles; i++)
976 5fc8b927 2014-11-14 marcelgmr if (!MARKED(i)) {
977 7c74df90 2014-11-15 marcelgmr add_mark(&rover.marks, CWD, ENAME(i));
978 5fc8b927 2014-11-14 marcelgmr MARKED(i) = 1;
979 5fc8b927 2014-11-14 marcelgmr }
980 743e70b8 2014-11-14 marcelgmr update_view();
981 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_DELETE)) {
982 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries) {
983 a31a7b29 2014-11-15 marcelgmr message("Delete marked entries? (Y to confirm)", YELLOW);
984 a31a7b29 2014-11-15 marcelgmr if (getch() == 'Y')
985 a31a7b29 2014-11-15 marcelgmr process_marked(NULL, delfile, deldir);
986 590d39af 2014-12-02 marcelgmr else
987 590d39af 2014-12-02 marcelgmr clear_message();
988 590d39af 2014-12-02 marcelgmr } else
989 590d39af 2014-12-02 marcelgmr message("No entries marked for deletion.", RED);
990 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_COPY)) {
991 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries)
992 a31a7b29 2014-11-15 marcelgmr process_marked(adddir, cpyfile, NULL);
993 590d39af 2014-12-02 marcelgmr else
994 590d39af 2014-12-02 marcelgmr message("No entries marked for copying.", RED);
995 590d39af 2014-12-02 marcelgmr } else if (!strcmp(key, RVK_MOVE)) {
996 a31a7b29 2014-11-15 marcelgmr if (rover.marks.nentries)
997 a31a7b29 2014-11-15 marcelgmr process_marked(adddir, movfile, deldir);
998 590d39af 2014-12-02 marcelgmr else
999 590d39af 2014-12-02 marcelgmr message("No entries marked for moving.", RED);
1000 a31a7b29 2014-11-15 marcelgmr }
1001 576e768d 2014-11-08 marcelgmr }
1002 18016333 2014-11-14 marcelgmr if (rover.nfiles)
1003 18016333 2014-11-14 marcelgmr free_rows(&rover.rows, rover.nfiles);
1004 c3758004 2014-11-15 marcelgmr free_marks(&rover.marks);
1005 f05efd9e 2014-11-07 marcelgmr delwin(rover.window);
1006 71ee1627 2014-11-07 marcelgmr return 0;
1007 71ee1627 2014-11-07 marcelgmr }