My build of nnn with minor changes
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

635 lines
11 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. #include "queue.h"
  19. #ifdef DEBUG
  20. #define DEBUG_FD 8
  21. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  22. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  23. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  24. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  25. #else
  26. #define DPRINTF_D(x)
  27. #define DPRINTF_U(x)
  28. #define DPRINTF_S(x)
  29. #define DPRINTF_P(x)
  30. #endif /* DEBUG */
  31. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  32. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  33. #define ISODD(x) ((x) & 1)
  34. #define CONTROL(c) ((c) ^ 0x40)
  35. struct assoc {
  36. char *regex; /* Regex to match on filename */
  37. char *bin; /* Program */
  38. };
  39. /* Configuration */
  40. struct assoc assocs[] = {
  41. { "\\.(avi|mp4|mkv|mp3|ogg)$", "mplayer" },
  42. { "\\.(png|jpg|gif)$", "feh" },
  43. { "\\.(html|svg)$", "firefox" },
  44. { "\\.pdf$", "mupdf" },
  45. { "\\.sh$", "sh" },
  46. { ".*", "less" },
  47. };
  48. struct entry {
  49. char *name;
  50. mode_t mode;
  51. };
  52. struct history {
  53. int pos;
  54. SLIST_ENTRY(history) entry;
  55. };
  56. SLIST_HEAD(histhead, history) histhead = SLIST_HEAD_INITIALIZER(histhead);
  57. #define CWD "cwd: "
  58. #define CURSR " > "
  59. #define EMPTY " "
  60. /*
  61. * Layout:
  62. * .---------
  63. * | cwd: /mnt/path
  64. * |
  65. * | file0
  66. * | file1
  67. * | > file2
  68. * | file3
  69. * | file4
  70. * ...
  71. * | filen
  72. * |
  73. * | Permission denied
  74. * '------
  75. */
  76. int die = 0;
  77. void printmsg(char *msg);
  78. void printwarn(void);
  79. void printerr(int ret, char *prefix);
  80. char *
  81. openwith(char *file)
  82. {
  83. regex_t regex;
  84. char *bin = NULL;
  85. int i;
  86. for (i = 0; i < LEN(assocs); i++) {
  87. if (regcomp(&regex, assocs[i].regex,
  88. REG_NOSUB | REG_EXTENDED) != 0)
  89. continue;
  90. if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
  91. bin = assocs[i].bin;
  92. break;
  93. }
  94. }
  95. DPRINTF_S(bin);
  96. return bin;
  97. }
  98. int
  99. setfilter(regex_t *regex, char *filter)
  100. {
  101. char *errbuf;
  102. int r;
  103. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
  104. if (r != 0) {
  105. errbuf = malloc(COLS * sizeof(char));
  106. regerror(r, regex, errbuf, COLS * sizeof(char));
  107. printmsg(errbuf);
  108. free(errbuf);
  109. }
  110. return r;
  111. }
  112. int
  113. visible(regex_t *regex, char *file)
  114. {
  115. if (regexec(regex, file, 0, NULL, 0) != REG_NOMATCH)
  116. return 1;
  117. return 0;
  118. }
  119. int
  120. entrycmp(const void *va, const void *vb)
  121. {
  122. const struct entry *a, *b;
  123. a = (struct entry *)va;
  124. b = (struct entry *)vb;
  125. return strcmp(a->name, b->name);
  126. }
  127. void
  128. initcurses(void)
  129. {
  130. initscr();
  131. cbreak();
  132. noecho();
  133. nonl();
  134. intrflush(stdscr, FALSE);
  135. keypad(stdscr, TRUE);
  136. curs_set(FALSE); /* Hide cursor */
  137. }
  138. void
  139. exitcurses(void)
  140. {
  141. endwin(); /* Restore terminal */
  142. }
  143. /* Messages show up at the bottom */
  144. void
  145. printmsg(char *msg)
  146. {
  147. move(LINES - 1, 0);
  148. printw("%s\n", msg);
  149. }
  150. /* Display warning as a message */
  151. void
  152. printwarn(void)
  153. {
  154. printmsg(strerror(errno));
  155. }
  156. /* Kill curses and display error before exiting */
  157. void
  158. printerr(int ret, char *prefix)
  159. {
  160. exitcurses();
  161. printf("%s: %s\n", prefix, strerror(errno));
  162. exit(ret);
  163. }
  164. /*
  165. * Returns 0 normally
  166. * On movement it updates *cur
  167. * Returns SEL_{QUIT,BACK,GOIN,FLTR} otherwise
  168. */
  169. #define SEL_QUIT 1
  170. #define SEL_BACK 2
  171. #define SEL_GOIN 3
  172. #define SEL_FLTR 4
  173. int
  174. nextsel(int *cur, int max)
  175. {
  176. int c;
  177. c = getch();
  178. switch (c) {
  179. case 'q':
  180. return SEL_QUIT;
  181. /* Back */
  182. case KEY_BACKSPACE:
  183. case KEY_LEFT:
  184. case 'h':
  185. return SEL_BACK;
  186. /* Inside */
  187. case KEY_ENTER:
  188. case '\r':
  189. case KEY_RIGHT:
  190. case 'l':
  191. return SEL_GOIN;
  192. /* Filter */
  193. case '/':
  194. case '&':
  195. return SEL_FLTR;
  196. /* Next */
  197. case 'j':
  198. case KEY_DOWN:
  199. case CONTROL('N'):
  200. if (*cur < max - 1)
  201. (*cur)++;
  202. break;
  203. /* Previous */
  204. case 'k':
  205. case KEY_UP:
  206. case CONTROL('P'):
  207. if (*cur > 0)
  208. (*cur)--;
  209. break;
  210. /* Page down */
  211. case KEY_NPAGE:
  212. case CONTROL('D'):
  213. if (*cur < max -1)
  214. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  215. break;
  216. /* Page up */
  217. case KEY_PPAGE:
  218. case CONTROL('U'):
  219. if (*cur > 0)
  220. (*cur) -= MIN((LINES - 4) / 2, *cur);
  221. break;
  222. }
  223. return 0;
  224. }
  225. char *
  226. readln(void)
  227. {
  228. int c;
  229. int i = 0;
  230. char *ln = NULL;
  231. int y, x, x0;
  232. echo();
  233. curs_set(TRUE);
  234. /* Starting point */
  235. getyx(stdscr, y, x);
  236. x0 = x;
  237. while (c = getch()) {
  238. if (c == KEY_ENTER || c == '\r')
  239. break;
  240. if (c == KEY_BACKSPACE) {
  241. getyx(stdscr, y, x);
  242. if (x >= x0) {
  243. ln = realloc(ln, (i - 1) * sizeof(*ln));
  244. i--;
  245. move(y, x);
  246. printw("%c", ' ');
  247. move(y, x);
  248. } else {
  249. move(y, x0);
  250. }
  251. continue;
  252. }
  253. ln = realloc(ln, (i + 1) * sizeof(*ln));
  254. ln[i] = c;
  255. i++;
  256. }
  257. if (ln != NULL) {
  258. ln = realloc(ln, (i + 1) * sizeof(*ln));
  259. ln[i] = '\0';
  260. }
  261. curs_set(FALSE);
  262. noecho();
  263. return ln;
  264. }
  265. int
  266. testopendir(char *path)
  267. {
  268. DIR *dirp;
  269. dirp = opendir(path);
  270. if (dirp == NULL) {
  271. return 0;
  272. } else {
  273. closedir(dirp);
  274. return 1;
  275. }
  276. }
  277. void
  278. printent(struct entry *ent, int active)
  279. {
  280. char *name;
  281. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  282. char cm = 0;
  283. /* Copy name locally */
  284. name = strdup(ent->name);
  285. if (name == NULL)
  286. printerr(1, "strdup name");
  287. if (S_ISDIR(ent->mode)) {
  288. cm = '/';
  289. maxlen--;
  290. } else if (S_ISLNK(ent->mode)) {
  291. cm = '@';
  292. maxlen--;
  293. }
  294. /* No text wrapping in entries */
  295. if (strlen(name) > maxlen)
  296. name[maxlen] = '\0';
  297. if (cm == 0)
  298. printw("%s%s\n", active ? CURSR : EMPTY, name);
  299. else
  300. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  301. free(name);
  302. }
  303. void
  304. browse(const char *ipath, const char *ifilter)
  305. {
  306. DIR *dirp;
  307. int dfd;
  308. struct dirent *dp;
  309. struct entry *dents;
  310. int i, n, cur;
  311. int r, ret;
  312. char *path = strdup(ipath);
  313. char *filter = strdup(ifilter);
  314. regex_t filter_re;
  315. char *cwd;
  316. struct stat sb;
  317. cur = 0;
  318. begin:
  319. /* Path and filter should be malloc(3)-ed strings at all times */
  320. n = 0;
  321. dents = NULL;
  322. dirp = opendir(path);
  323. if (dirp == NULL) {
  324. printwarn();
  325. goto nochange;
  326. }
  327. /* Search filter */
  328. r = setfilter(&filter_re, filter);
  329. if (r != 0)
  330. goto nochange;
  331. while ((dp = readdir(dirp)) != NULL) {
  332. char *name;
  333. /* Skip self and parent */
  334. if (strcmp(dp->d_name, ".") == 0
  335. || strcmp(dp->d_name, "..") == 0)
  336. continue;
  337. if (!visible(&filter_re, dp->d_name))
  338. continue;
  339. /* Deep copy because readdir(3) reuses the entries */
  340. dents = realloc(dents, (n + 1) * sizeof(*dents));
  341. if (dents == NULL)
  342. printerr(1, "realloc");
  343. dents[n].name = strdup(dp->d_name);
  344. if (dents[n].name == NULL)
  345. printerr(1, "strdup");
  346. /* Handle root case */
  347. if (strcmp(path, "/") == 0)
  348. asprintf(&name, "/%s", dents[n].name);
  349. else
  350. asprintf(&name, "%s/%s", path, dents[n].name);
  351. /* Get mode flags */
  352. r = lstat(name, &sb);
  353. free(name);
  354. if (r == -1)
  355. printerr(1, "stat");
  356. dents[n].mode = sb.st_mode;
  357. n++;
  358. }
  359. /* Make sure cur is in range */
  360. cur = MIN(cur, n - 1);
  361. qsort(dents, n, sizeof(*dents), entrycmp);
  362. for (;;) {
  363. int nlines;
  364. int maxlen;
  365. int odd;
  366. char *pathnew;
  367. char *name;
  368. char *bin;
  369. pid_t pid;
  370. int fd;
  371. char *dir;
  372. char *tmp;
  373. regex_t re;
  374. struct history *hist;
  375. redraw:
  376. nlines = MIN(LINES - 4, n);
  377. /* Clean screen */
  378. erase();
  379. /* Strip trailing slashes */
  380. for (i = strlen(path) - 1; i > 0; i--)
  381. if (path[i] == '/')
  382. path[i] = '\0';
  383. else
  384. break;
  385. DPRINTF_D(cur);
  386. DPRINTF_S(path);
  387. /* No text wrapping in cwd line */
  388. cwd = malloc(COLS * sizeof(char));
  389. strlcpy(cwd, path, COLS * sizeof(char));
  390. cwd[COLS - strlen(CWD) - 1] = '\0';
  391. printw(CWD "%s\n\n", cwd);
  392. /* Print listing */
  393. odd = ISODD(nlines);
  394. if (cur < nlines / 2) {
  395. for (i = 0; i < nlines; i++)
  396. printent(&dents[i], i == cur);
  397. } else if (cur >= n - nlines / 2) {
  398. for (i = n - nlines; i < n; i++)
  399. printent(&dents[i], i == cur);
  400. } else {
  401. for (i = cur - nlines / 2;
  402. i < cur + nlines / 2 + odd; i++)
  403. printent(&dents[i], i == cur);
  404. }
  405. nochange:
  406. ret = nextsel(&cur, n);
  407. switch (ret) {
  408. case SEL_QUIT:
  409. free(path);
  410. free(filter);
  411. /* Forget history */
  412. while (!SLIST_EMPTY(&histhead)) {
  413. hist = SLIST_FIRST(&histhead);
  414. SLIST_REMOVE_HEAD(&histhead, entry);
  415. free(hist);
  416. }
  417. return;
  418. case SEL_BACK:
  419. /* There is no going back */
  420. if (strcmp(path, "/") == 0) {
  421. goto nochange;
  422. } else {
  423. dir = dirname(path);
  424. tmp = malloc(strlen(dir) + 1);
  425. strlcpy(tmp, dir, strlen(dir) + 1);
  426. free(path);
  427. path = tmp;
  428. free(filter);
  429. filter = strdup(ifilter); /* Reset filter */
  430. /* Recall history */
  431. hist = SLIST_FIRST(&histhead);
  432. if (hist != NULL) {
  433. cur = hist->pos;
  434. DPRINTF_D(hist->pos);
  435. SLIST_REMOVE_HEAD(&histhead, entry);
  436. free(hist);
  437. } else {
  438. cur = 0;
  439. }
  440. goto out;
  441. }
  442. case SEL_GOIN:
  443. /* Cannot descend in empty directories */
  444. if (n == 0)
  445. goto nochange;
  446. name = dents[cur].name;
  447. /* Handle root case */
  448. if (strcmp(path, "/") == 0)
  449. asprintf(&pathnew, "/%s", name);
  450. else
  451. asprintf(&pathnew, "%s/%s", path, name);
  452. DPRINTF_S(name);
  453. DPRINTF_S(pathnew);
  454. /* Get path info */
  455. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  456. if (fd == -1) {
  457. printwarn();
  458. free(pathnew);
  459. goto nochange;
  460. }
  461. r = fstat(fd, &sb);
  462. close(fd);
  463. if (r == -1) {
  464. printwarn();
  465. free(pathnew);
  466. goto nochange;
  467. }
  468. DPRINTF_U(sb.st_mode);
  469. /* Directory */
  470. if (S_ISDIR(sb.st_mode)) {
  471. free(path);
  472. path = pathnew;
  473. free(filter);
  474. filter = strdup(ifilter); /* Reset filter */
  475. /* Save history */
  476. hist = malloc(sizeof(struct history));
  477. hist->pos = cur;
  478. SLIST_INSERT_HEAD(&histhead, hist, entry);
  479. cur = 0;
  480. goto out;
  481. }
  482. /* Regular file */
  483. if (S_ISREG(sb.st_mode)) {
  484. /* Open with */
  485. bin = openwith(name);
  486. if (bin == NULL) {
  487. printmsg("No association");
  488. free(pathnew);
  489. goto nochange;
  490. }
  491. exitcurses();
  492. /* Run program */
  493. pid = fork();
  494. if (pid == 0)
  495. execlp(bin, bin, pathnew, NULL);
  496. else
  497. waitpid(pid, NULL, 0);
  498. initcurses();
  499. free(pathnew);
  500. goto redraw;
  501. }
  502. /* All the rest */
  503. printmsg("Unsupported file");
  504. free(pathnew);
  505. goto nochange;
  506. case SEL_FLTR:
  507. /* Read filter */
  508. printmsg("");
  509. move(LINES - 1, 0);
  510. printw("filter: ");
  511. tmp = readln();
  512. if (tmp == NULL) {
  513. printmsg("");
  514. goto nochange;
  515. }
  516. r = setfilter(&re, tmp);
  517. if (r != 0) {
  518. free(tmp);
  519. goto nochange;
  520. }
  521. free(filter);
  522. filter = tmp;
  523. filter_re = re;
  524. DPRINTF_S(filter);
  525. cur = 0;
  526. goto out;
  527. }
  528. }
  529. out:
  530. for (i = 0; i < n; i++)
  531. free(dents[i].name);
  532. free(dents);
  533. /* Should never be null */
  534. r = closedir(dirp);
  535. if (r == -1)
  536. printerr(1, "closedir");
  537. goto begin;
  538. }
  539. int
  540. main(int argc, char *argv[])
  541. {
  542. char *ipath = argv[1] != NULL ? argv[1] : "/";
  543. char *ifilter = "^[^.].*"; /* Hide dotfiles */
  544. /* Test initial path */
  545. if (!testopendir(ipath))
  546. printerr(1, ipath);
  547. /* Set locale before curses setup */
  548. setlocale(LC_ALL, "");
  549. initcurses();
  550. browse(ipath, ifilter);
  551. exitcurses();
  552. return 0;
  553. }