My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

420 lines
6.7 KiB

  1. #include <sys/types.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <dirent.h>
  5. #include <curses.h>
  6. #include <libgen.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <signal.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #ifdef DEBUG
  14. #define DPRINTF_D(x) printw(#x "=%d\n", x)
  15. #define DPRINTF_S(x) printw(#x "=%s\n", x)
  16. #define DPRINTF_P(x) printw(#x "=0x%p\n", x)
  17. #else
  18. #define DPRINTF_D(x)
  19. #define DPRINTF_S(x)
  20. #define DPRINTF_P(x)
  21. #endif /* DEBUG */
  22. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  23. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  24. #define ISODD(x) ((x) & 1)
  25. /*
  26. * Layout:
  27. * .---------
  28. * | cwd: /mnt/path
  29. * |
  30. * | > file0
  31. * | file1
  32. * ...
  33. * | filen
  34. * |
  35. * | msg: invalid extension
  36. * '------
  37. */
  38. int die = 0;
  39. struct entry {
  40. char name[MAXNAMLEN + 1];
  41. };
  42. struct assoc {
  43. char *ext; /* Extension */
  44. char *bin; /* Program */
  45. } assocs[] = {
  46. { ".avi", "mplayer" },
  47. { ".mp4", "mplayer" },
  48. { ".mkv", "mplayer" },
  49. { ".mp3", "mplayer" },
  50. { ".ogg", "mplayer" },
  51. { ".srt", "less" },
  52. { ".txt", "less" },
  53. { "README", "less" },
  54. };
  55. char *
  56. openwith(char *file)
  57. {
  58. char *ext = NULL;
  59. char *bin = NULL;
  60. int i;
  61. ext = strrchr(file, '.');
  62. if (ext == NULL)
  63. ext = file;
  64. DPRINTF_S(ext);
  65. for (i = 0; i < LEN(assocs); i++)
  66. if (strncmp(assocs[i].ext, ext, strlen(ext) + 1) == 0)
  67. bin = assocs[i].bin;
  68. DPRINTF_S(bin);
  69. return bin;
  70. }
  71. int
  72. dentcmp(const void *va, const void *vb)
  73. {
  74. const struct dirent *a, *b;
  75. a = *(struct dirent **)va;
  76. b = *(struct dirent **)vb;
  77. return strcmp(a->d_name, b->d_name);
  78. }
  79. void
  80. initcurses(void)
  81. {
  82. initscr();
  83. cbreak();
  84. noecho();
  85. nonl();
  86. intrflush(stdscr, FALSE);
  87. keypad(stdscr, TRUE);
  88. curs_set(FALSE); /* Hide cursor */
  89. }
  90. void
  91. exitcurses(void)
  92. {
  93. endwin(); /* Restore terminal */
  94. }
  95. /* Messages show up at the bottom */
  96. void
  97. printmsg(char *msg)
  98. {
  99. move(LINES - 1, 0);
  100. printw("%s\n", msg);
  101. }
  102. /* Display warning as a message */
  103. void
  104. printwarn(void)
  105. {
  106. printmsg(strerror(errno));
  107. }
  108. /* Kill curses and display error before exiting */
  109. void
  110. printerr(int ret, char *prefix)
  111. {
  112. endwin();
  113. printf("%s: %s\n", prefix, strerror(errno));
  114. exit(ret);
  115. }
  116. /*
  117. * Returns 0 normally
  118. * On movement it updates *cur
  119. * Returns 1 on quit
  120. * Returns 2 on go in
  121. * Returns 3 on go up
  122. */
  123. int
  124. nextsel(int *cur, int max)
  125. {
  126. int c;
  127. c = getch();
  128. switch (c) {
  129. case 'q':
  130. return 1;
  131. /* go up */
  132. case KEY_BACKSPACE:
  133. case KEY_LEFT:
  134. case 'h':
  135. return 2;
  136. /* go in */
  137. case KEY_ENTER:
  138. case '\r':
  139. case KEY_RIGHT:
  140. case 'l':
  141. return 3;
  142. /* next */
  143. case 'j':
  144. case KEY_DOWN:
  145. if (*cur < max - 1)
  146. (*cur)++;
  147. break;
  148. /* prev */
  149. case 'k':
  150. case KEY_UP:
  151. if (*cur > 0)
  152. (*cur)--;
  153. break;
  154. }
  155. return 0;
  156. }
  157. int
  158. testopen(char *path)
  159. {
  160. int fd;
  161. fd = open(path, O_RDONLY);
  162. if (fd == -1) {
  163. return 0;
  164. } else {
  165. close(fd);
  166. return 1;
  167. }
  168. }
  169. int
  170. testopendir(char *path)
  171. {
  172. DIR *dirp;
  173. dirp = opendir(path);
  174. if (dirp == NULL) {
  175. return 0;
  176. } else {
  177. closedir(dirp);
  178. return 1;
  179. }
  180. }
  181. void
  182. browse(const char *ipath)
  183. {
  184. DIR *dirp;
  185. struct dirent *dp;
  186. struct dirent **dents;
  187. int i, n, cur;
  188. int r, ret;
  189. char *path = strdup(ipath);
  190. char *cwd;
  191. begin:
  192. /* Path should be a malloc(3)-ed string at all times */
  193. n = 0;
  194. cur = 0;
  195. dents = NULL;
  196. dirp = opendir(path);
  197. if (dirp == NULL) {
  198. printwarn();
  199. goto nochange;
  200. }
  201. while ((dp = readdir(dirp)) != NULL) {
  202. /* Skip self and parent */
  203. if (strncmp(dp->d_name, ".", 2) == 0
  204. || strncmp(dp->d_name, "..", 3) == 0)
  205. continue;
  206. dents = realloc(dents, (n + 1) * sizeof(*dents));
  207. if (dents == NULL)
  208. printerr(1, "realloc");
  209. dents[n] = dp;
  210. n++;
  211. }
  212. qsort(dents, n, sizeof(*dents), dentcmp);
  213. for (;;) {
  214. int nlines;
  215. struct entry *tmpents;
  216. int odd;
  217. redraw:
  218. nlines = MIN(LINES - 4, n);
  219. /* Clean screen */
  220. erase();
  221. /* Strip slashes */
  222. for (i = strlen(path) - 1; i > -1; i--)
  223. if (path[i] == '/')
  224. path[i] = '\0';
  225. else
  226. break;
  227. DPRINTF_D(cur);
  228. DPRINTF_S(path);
  229. #define CWD "cwd: "
  230. #define CURSR " > "
  231. #define EMPTY " "
  232. /* No text wrapping in cwd line */
  233. cwd = malloc(COLS * sizeof(char));
  234. strncpy(cwd, path, COLS);
  235. cwd[COLS - strlen(CWD) - 1] = '\0';
  236. /* No text wrapping in entries */
  237. tmpents = malloc(n * sizeof(*tmpents));
  238. for (i = 0; i < n; i++) {
  239. strncpy(tmpents[i].name, dents[i]->d_name,
  240. sizeof(tmpents[i].name));
  241. tmpents[i].name[COLS - strlen(CURSR) - 1] = '\0';
  242. }
  243. /* Print cwd */
  244. printw(CWD "%s%s\n\n",
  245. strncmp(cwd, "", 1) == 0 ? "/" : "",
  246. cwd);
  247. /* Print listing */
  248. odd = ISODD(nlines);
  249. if (cur < nlines / 2) {
  250. for (i = 0; i < nlines; i++)
  251. printw("%s%s\n",
  252. i == cur ? CURSR : EMPTY,
  253. tmpents[i].name);
  254. } else if (cur >= n - nlines / 2) {
  255. for (i = n - nlines; i < n; i++)
  256. printw("%s%s\n",
  257. i == cur ? CURSR : EMPTY,
  258. tmpents[i].name);
  259. } else {
  260. for (i = cur - nlines / 2;
  261. i < cur + nlines / 2 + odd; i++)
  262. printw("%s%s\n",
  263. i == cur ? CURSR : EMPTY,
  264. tmpents[i].name);
  265. }
  266. free(tmpents);
  267. nochange:
  268. ret = nextsel(&cur, n);
  269. if (ret == 1) {
  270. free(path);
  271. return;
  272. }
  273. if (ret == 2) {
  274. /* Handle root case */
  275. if (strncmp(path, "", 1) == 0) {
  276. goto nochange;
  277. } else {
  278. char *dir, *tmp;
  279. dir = dirname(path);
  280. tmp = malloc(strlen(dir) + 1);
  281. strncpy(tmp, dir, strlen(dir) + 1);
  282. free(path);
  283. path = tmp;
  284. goto out;
  285. }
  286. }
  287. if (ret == 3) {
  288. char *name, *file = NULL;
  289. char *newpath;
  290. char *bin;
  291. pid_t pid;
  292. /* Cannot descend in empty directories */
  293. if (n == 0)
  294. goto nochange;
  295. name = dents[cur]->d_name;
  296. switch (dents[cur]->d_type) {
  297. case DT_DIR:
  298. newpath = malloc(strlen(path) + 1
  299. + strlen(name) + 1);
  300. sprintf(newpath, "%s/%s", path, name);
  301. if (testopen(newpath)) {
  302. free(path);
  303. path = newpath;
  304. goto out;
  305. } else {
  306. printwarn();
  307. free(newpath);
  308. goto nochange;
  309. }
  310. case DT_REG:
  311. file = malloc(strlen(path) + 1
  312. + strlen(name) + 1);
  313. sprintf(file, "%s/%s", path, name);
  314. DPRINTF_S(file);
  315. /* Open with */
  316. bin = openwith(name);
  317. if (bin == NULL) {
  318. printmsg("No association");
  319. goto nochange;
  320. }
  321. exitcurses();
  322. /* Run program */
  323. pid = fork();
  324. if (pid == 0)
  325. execlp(bin, bin, file, NULL);
  326. else
  327. waitpid(pid, NULL, 0);
  328. initcurses();
  329. free(file);
  330. goto redraw;
  331. default:
  332. DPRINTF_D(dents[cur]->d_type);
  333. }
  334. }
  335. }
  336. out:
  337. free(dents);
  338. r = closedir(dirp);
  339. if (r == -1)
  340. printerr(1, "closedir");
  341. goto begin;
  342. }
  343. int
  344. main(int argc, char *argv[])
  345. {
  346. char *ipath = argv[1] != NULL ? argv[1] : "/";
  347. /* Test initial path */
  348. if (!testopendir(ipath))
  349. printerr(1, ipath);
  350. /* Set locale before curses setup */
  351. setlocale(LC_ALL, "");
  352. initcurses();
  353. browse(ipath);
  354. exitcurses();
  355. return 0;
  356. }