My build of nnn with minor changes
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

642 рядки
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. int status;
  376. redraw:
  377. nlines = MIN(LINES - 4, n);
  378. /* Clean screen */
  379. erase();
  380. /* Strip trailing slashes */
  381. for (i = strlen(path) - 1; i > 0; i--)
  382. if (path[i] == '/')
  383. path[i] = '\0';
  384. else
  385. break;
  386. DPRINTF_D(cur);
  387. DPRINTF_S(path);
  388. /* No text wrapping in cwd line */
  389. cwd = malloc(COLS * sizeof(char));
  390. strlcpy(cwd, path, COLS * sizeof(char));
  391. cwd[COLS - strlen(CWD) - 1] = '\0';
  392. printw(CWD "%s\n\n", cwd);
  393. /* Print listing */
  394. odd = ISODD(nlines);
  395. if (cur < nlines / 2) {
  396. for (i = 0; i < nlines; i++)
  397. printent(&dents[i], i == cur);
  398. } else if (cur >= n - nlines / 2) {
  399. for (i = n - nlines; i < n; i++)
  400. printent(&dents[i], i == cur);
  401. } else {
  402. for (i = cur - nlines / 2;
  403. i < cur + nlines / 2 + odd; i++)
  404. printent(&dents[i], i == cur);
  405. }
  406. nochange:
  407. ret = nextsel(&cur, n);
  408. switch (ret) {
  409. case SEL_QUIT:
  410. free(path);
  411. free(filter);
  412. /* Forget history */
  413. while (!SLIST_EMPTY(&histhead)) {
  414. hist = SLIST_FIRST(&histhead);
  415. SLIST_REMOVE_HEAD(&histhead, entry);
  416. free(hist);
  417. }
  418. return;
  419. case SEL_BACK:
  420. /* There is no going back */
  421. if (strcmp(path, "/") == 0) {
  422. goto nochange;
  423. } else {
  424. dir = dirname(path);
  425. tmp = malloc(strlen(dir) + 1);
  426. strlcpy(tmp, dir, strlen(dir) + 1);
  427. free(path);
  428. path = tmp;
  429. free(filter);
  430. filter = strdup(ifilter); /* Reset filter */
  431. /* Recall history */
  432. hist = SLIST_FIRST(&histhead);
  433. if (hist != NULL) {
  434. cur = hist->pos;
  435. DPRINTF_D(hist->pos);
  436. SLIST_REMOVE_HEAD(&histhead, entry);
  437. free(hist);
  438. } else {
  439. cur = 0;
  440. }
  441. goto out;
  442. }
  443. case SEL_GOIN:
  444. /* Cannot descend in empty directories */
  445. if (n == 0)
  446. goto nochange;
  447. name = dents[cur].name;
  448. /* Handle root case */
  449. if (strcmp(path, "/") == 0)
  450. asprintf(&pathnew, "/%s", name);
  451. else
  452. asprintf(&pathnew, "%s/%s", path, name);
  453. DPRINTF_S(name);
  454. DPRINTF_S(pathnew);
  455. /* Get path info */
  456. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  457. if (fd == -1) {
  458. printwarn();
  459. free(pathnew);
  460. goto nochange;
  461. }
  462. r = fstat(fd, &sb);
  463. close(fd);
  464. if (r == -1) {
  465. printwarn();
  466. free(pathnew);
  467. goto nochange;
  468. }
  469. DPRINTF_U(sb.st_mode);
  470. /* Directory */
  471. if (S_ISDIR(sb.st_mode)) {
  472. free(path);
  473. path = pathnew;
  474. free(filter);
  475. filter = strdup(ifilter); /* Reset filter */
  476. /* Save history */
  477. hist = malloc(sizeof(struct history));
  478. hist->pos = cur;
  479. SLIST_INSERT_HEAD(&histhead, hist, entry);
  480. cur = 0;
  481. goto out;
  482. }
  483. /* Regular file */
  484. if (S_ISREG(sb.st_mode)) {
  485. /* Open with */
  486. bin = openwith(name);
  487. if (bin == NULL) {
  488. printmsg("No association");
  489. free(pathnew);
  490. goto nochange;
  491. }
  492. exitcurses();
  493. /* Run program */
  494. pid = fork();
  495. if (pid == 0) {
  496. execlp(bin, bin, pathnew, NULL);
  497. _exit(0);
  498. } else {
  499. /* Ignore interruptions */
  500. while (waitpid(pid, &status,
  501. 0) == -1)
  502. DPRINTF_D(status);
  503. DPRINTF_D(pid);
  504. }
  505. initcurses();
  506. free(pathnew);
  507. goto redraw;
  508. }
  509. /* All the rest */
  510. printmsg("Unsupported file");
  511. free(pathnew);
  512. goto nochange;
  513. case SEL_FLTR:
  514. /* Read filter */
  515. printmsg("");
  516. move(LINES - 1, 0);
  517. printw("filter: ");
  518. tmp = readln();
  519. if (tmp == NULL) {
  520. printmsg("");
  521. goto nochange;
  522. }
  523. r = setfilter(&re, tmp);
  524. if (r != 0) {
  525. free(tmp);
  526. goto nochange;
  527. }
  528. free(filter);
  529. filter = tmp;
  530. filter_re = re;
  531. DPRINTF_S(filter);
  532. cur = 0;
  533. goto out;
  534. }
  535. }
  536. out:
  537. for (i = 0; i < n; i++)
  538. free(dents[i].name);
  539. free(dents);
  540. /* Should never be null */
  541. r = closedir(dirp);
  542. if (r == -1)
  543. printerr(1, "closedir");
  544. goto begin;
  545. }
  546. int
  547. main(int argc, char *argv[])
  548. {
  549. char *ipath = argv[1] != NULL ? argv[1] : "/";
  550. char *ifilter = "^[^.].*"; /* Hide dotfiles */
  551. /* Test initial path */
  552. if (!testopendir(ipath))
  553. printerr(1, ipath);
  554. /* Set locale before curses setup */
  555. setlocale(LC_ALL, "");
  556. initcurses();
  557. browse(ipath, ifilter);
  558. exitcurses();
  559. return 0;
  560. }