aboutsummaryrefslogtreecommitdiffstats
path: root/noice.c
blob: a399d24f453ec493a852ea5c1526b89c7b77e691 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <curses.h>
#include <libgen.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#ifdef DEBUG
#define DPRINTF_D(x) printw(#x "=%d\n", x)
#define DPRINTF_S(x) printw(#x "=%s\n", x)
#define DPRINTF_P(x) printw(#x "=0x%p\n", x)
#else
#define DPRINTF_D(x)
#define DPRINTF_S(x)
#define DPRINTF_P(x)
#endif /* DEBUG */

#define LEN(x) (sizeof(x) / sizeof(*(x)))

/*
 * Layout:
 * .---------
 * | cwd: /mnt/path
 * |
 * |  > file0
 * |    file1
 *      ...
 * |    filen
 * |
 * | msg: invalid extension
 * '------
 */

int die = 0;

struct assoc {
	char *ext;
	char *bin;
} assocs[] = {
	{ "avi", "mplayer" },
	{ "mp4", "mplayer" },
	{ "mkv", "mplayer" },
	{ "mp3", "mplayer" },
	{ "ogg", "mplayer" },
	{ "srt", "less" },
	{ "txt", "less" },
};

char *
extension(char *file)
{
	char *dot;

	dot = strrchr(file, '.');
	if (dot == NULL || dot == file)
		return NULL;
	else
		return dot + 1;
}

char *
openwith(char *ext)
{
	int i;

	for (i = 0; i < LEN(assocs); i++)
		if (strncmp(assocs[i].ext, ext, strlen(ext)) == 0)
			return assocs[i].bin;
	return NULL;
}

int
dentcmp(const void *va, const void *vb)
{
	const struct dirent *a, *b;

	a = *(struct dirent **)va;
	b = *(struct dirent **)vb;

	return strcmp(a->d_name, b->d_name);
}

/* Warning shows up at the bottom */
void
printwarn(char *prefix)
{
	move(LINES - 1, 0);
	printw("%s: %s\n", prefix, strerror(errno));
}

/* Kill curses and display error before exiting */
void
printerr(int ret, char *prefix)
{
	endwin();
	printf("%s: %s\n", prefix, strerror(errno));
	exit(ret);
}

/*
 * Returns 0 normally
 * On movement it updates *cur
 * Returns 1 on quit
 * Returns 2 on go in
 * Returns 3 on go up
 */
int
nextsel(int *cur, int max)
{
	int c;

	c = getch();
	switch (c) {
	case 'q':
		return 1;
	/* go up */
	case KEY_BACKSPACE:
	case KEY_LEFT:
	case 'h':
		return 2;
	/* go in */
	case KEY_ENTER:
	case '\r':
	case KEY_RIGHT:
	case 'l':
		return 3;
	/* next */
	case 'j':
	case KEY_DOWN:
		if (*cur < max - 1)
			(*cur)++;
		break;
	/* prev */
	case 'k':
	case KEY_UP:
		if (*cur > 0)
			(*cur)--;
		break;
	}

	return 0;
}

int
testopen(char *path)
{
	int fd;

	fd = open(path, O_RDONLY);
	if (fd == -1) {
		return 0;
	} else {
		close(fd);
		return 1;
	}
}

int
testopendir(char *path)
{
	DIR *dirp;

	dirp = opendir(path);
	if (dirp == NULL) {
		return 0;
	} else {
		closedir(dirp);
		return 1;
	}
}

void
browse(const char *ipath)
{
	DIR *dirp;
	struct dirent *dp;
	struct dirent **dents;
	int i, n, cur;
	int r, ret;
	char *path = strdup(ipath);

begin:
	/* Path is a malloc(3)-ed string */
	n = 0;
	cur = 0;
	dents = NULL;

	dirp = opendir(path); 
	if (dirp == NULL) {
		printwarn("opendir");
		goto nochange;
	}

	while ((dp = readdir(dirp)) != NULL) {
		/* Skip self and parent */
		if (strncmp(dp->d_name, ".", 2) == 0
		    || strncmp(dp->d_name, "..", 3) == 0)
			continue;
		dents = realloc(dents, (n + 1) * sizeof(*dents));
		if (dents == NULL)
			printerr(1, "realloc");
		dents[n] = dp;
		n++;
	}

	qsort(dents, n, sizeof(*dents), dentcmp);

	for (;;) {
redraw:
		/* Clean screen */
		erase();

		/* Strip slashes */
		for (i = strlen(path) - 1; i > -1; i--)
			if (path[i] == '/')
				path[i] = '\0';
			else
				break;

		DPRINTF_D(cur);
		DPRINTF_S(path);

		/* Print cwd */
		printw("cwd: %s%s\n\n",
		    strncmp(path, "", 1) == 0 ? "/" : "",
		    path);

		/* Print listing */
		for (i = 0; i < n; i++)
			printw(" %s %s\n",
			    i == cur ? ">" : " ",
			    dents[i]->d_name);

nochange:
		ret = nextsel(&cur, n);
		if (ret == 1) {
			free(path);
			return;
		}
		if (ret == 2) {
			/* Handle root case */
			if (strncmp(path, "", 1) == 0) {
				goto nochange;
			} else {
				path = dirname(path);
				goto out;
			}
		}
		if (ret == 3) {
			char *name, *file = NULL;
			char *newpath;
			char *ext, *bin;
			pid_t pid;

			name = dents[cur]->d_name;

			switch (dents[cur]->d_type) {
			case DT_DIR:
				newpath = malloc(strlen(path) + 1
				    + strlen(name) + 1);
				sprintf(newpath, "%s/%s", path, name);
				if (testopen(newpath)) {
					free(path);
					path = newpath;
					goto out;
				} else {
					printwarn(newpath);
					free(newpath);
					goto nochange;
				}
			case DT_REG:
				file = malloc(strlen(path) + 1
				    + strlen(name) + 1);
				sprintf(file, "%s/%s", path, name);
				DPRINTF_S(file);

				/* Open with */
				ext = extension(name);
				if (ext == NULL) {
					printwarn("invalid extension\n");
					goto nochange;
				}
				bin = openwith(ext);
				if (bin == NULL) {
					printwarn("no association\n");
					goto nochange;
				}
				DPRINTF_S(ext);
				DPRINTF_S(bin);

				/* Run program */
				pid = fork();
				if (pid == 0)
					execlp(bin, bin, file, NULL);
				else
					waitpid(pid, NULL, 0);

				free(file);

				/* Screen may be messed up */
				clear();
				/* Some programs reset this */
				keypad(stdscr, TRUE);
				goto redraw;
			default:
				DPRINTF_D(dents[cur]->d_type);
			}
		}
	}

out:
	free(dents);

	r = closedir(dirp); 
	if (r == -1)
		printerr(1, "closedir");

	goto begin;
}

int
main(int argc, char *argv[])
{
	char *ipath = argv[1] != NULL ? argv[1] : "/";

	/* Test initial path */
	if (!testopendir(ipath))
		printerr(1, ipath);

	/* Set locale before curses setup */
	setlocale(LC_ALL, "");

	/* Init curses */
	initscr();
	cbreak();
	noecho();
	nonl();
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	curs_set(FALSE); /* Hide cursor */

	browse(ipath);

	endwin(); /* Restore terminal */

	return 0;
}