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.
 
 
 
 
 
 

466 line
7.6 KiB

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