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.
 
 
 
 
 
 

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