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.
 
 
 
 
 
 

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