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.
 
 
 
 
 
 

687 lines
12 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. enum {
  173. SEL_QUIT = 1,
  174. SEL_BACK,
  175. SEL_GOIN,
  176. SEL_FLTR,
  177. SEL_SH,
  178. SEL_CD,
  179. };
  180. int
  181. nextsel(int *cur, int max)
  182. {
  183. int c;
  184. c = getch();
  185. switch (c) {
  186. case 'q':
  187. return SEL_QUIT;
  188. /* Back */
  189. case KEY_BACKSPACE:
  190. case KEY_LEFT:
  191. case 'h':
  192. return SEL_BACK;
  193. /* Inside */
  194. case KEY_ENTER:
  195. case '\r':
  196. case KEY_RIGHT:
  197. case 'l':
  198. return SEL_GOIN;
  199. /* Filter */
  200. case '/':
  201. case '&':
  202. return SEL_FLTR;
  203. /* Next */
  204. case 'j':
  205. case KEY_DOWN:
  206. case CONTROL('N'):
  207. if (*cur < max - 1)
  208. (*cur)++;
  209. break;
  210. /* Previous */
  211. case 'k':
  212. case KEY_UP:
  213. case CONTROL('P'):
  214. if (*cur > 0)
  215. (*cur)--;
  216. break;
  217. /* Page down */
  218. case KEY_NPAGE:
  219. case CONTROL('D'):
  220. if (*cur < max -1)
  221. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  222. break;
  223. /* Page up */
  224. case KEY_PPAGE:
  225. case CONTROL('U'):
  226. if (*cur > 0)
  227. (*cur) -= MIN((LINES - 4) / 2, *cur);
  228. break;
  229. case '!':
  230. return SEL_SH;
  231. case 'c':
  232. return SEL_CD;
  233. }
  234. return 0;
  235. }
  236. char *
  237. readln(void)
  238. {
  239. int c;
  240. int i = 0;
  241. char *ln = NULL;
  242. int y, x, x0;
  243. echo();
  244. curs_set(TRUE);
  245. /* Starting point */
  246. getyx(stdscr, y, x);
  247. x0 = x;
  248. while (c = getch()) {
  249. if (c == KEY_ENTER || c == '\r')
  250. break;
  251. if (c == KEY_BACKSPACE) {
  252. getyx(stdscr, y, x);
  253. if (x >= x0) {
  254. if (i > 0) {
  255. ln = realloc(ln, (i - 1) * sizeof(*ln));
  256. i--;
  257. } else {
  258. free(ln);
  259. ln = NULL;
  260. }
  261. move(y, x);
  262. printw("%c", ' ');
  263. move(y, x);
  264. } else {
  265. move(y, x0);
  266. }
  267. continue;
  268. }
  269. ln = realloc(ln, (i + 1) * sizeof(*ln));
  270. ln[i] = c;
  271. i++;
  272. }
  273. if (ln != NULL) {
  274. ln = realloc(ln, (i + 1) * sizeof(*ln));
  275. ln[i] = '\0';
  276. }
  277. curs_set(FALSE);
  278. noecho();
  279. return ln;
  280. }
  281. int
  282. testopendir(char *path)
  283. {
  284. DIR *dirp;
  285. dirp = opendir(path);
  286. if (dirp == NULL) {
  287. return 0;
  288. } else {
  289. closedir(dirp);
  290. return 1;
  291. }
  292. }
  293. void
  294. printent(struct entry *ent, int active)
  295. {
  296. char *name;
  297. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  298. char cm = 0;
  299. /* Copy name locally */
  300. name = strdup(ent->name);
  301. if (name == NULL)
  302. printerr(1, "strdup name");
  303. if (S_ISDIR(ent->mode)) {
  304. cm = '/';
  305. maxlen--;
  306. } else if (S_ISLNK(ent->mode)) {
  307. cm = '@';
  308. maxlen--;
  309. }
  310. /* No text wrapping in entries */
  311. if (strlen(name) > maxlen)
  312. name[maxlen] = '\0';
  313. if (cm == 0)
  314. printw("%s%s\n", active ? CURSR : EMPTY, name);
  315. else
  316. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  317. free(name);
  318. }
  319. void
  320. browse(const char *ipath, const char *ifilter)
  321. {
  322. DIR *dirp;
  323. int dfd;
  324. struct dirent *dp;
  325. struct entry *dents;
  326. int i, n, cur;
  327. int r, ret;
  328. char *path = strdup(ipath);
  329. char *filter = strdup(ifilter);
  330. regex_t filter_re;
  331. char *cwd;
  332. struct stat sb;
  333. cur = 0;
  334. begin:
  335. /* Path and filter should be malloc(3)-ed strings at all times */
  336. n = 0;
  337. dents = NULL;
  338. dirp = opendir(path);
  339. if (dirp == NULL) {
  340. printwarn();
  341. goto nochange;
  342. }
  343. /* Search filter */
  344. r = setfilter(&filter_re, filter);
  345. if (r != 0)
  346. goto nochange;
  347. while ((dp = readdir(dirp)) != NULL) {
  348. char *name;
  349. /* Skip self and parent */
  350. if (strcmp(dp->d_name, ".") == 0
  351. || strcmp(dp->d_name, "..") == 0)
  352. continue;
  353. if (!visible(&filter_re, dp->d_name))
  354. continue;
  355. /* Deep copy because readdir(3) reuses the entries */
  356. dents = realloc(dents, (n + 1) * sizeof(*dents));
  357. if (dents == NULL)
  358. printerr(1, "realloc");
  359. dents[n].name = strdup(dp->d_name);
  360. if (dents[n].name == NULL)
  361. printerr(1, "strdup");
  362. /* Handle root case */
  363. if (strcmp(path, "/") == 0)
  364. asprintf(&name, "/%s", dents[n].name);
  365. else
  366. asprintf(&name, "%s/%s", path, dents[n].name);
  367. /* Get mode flags */
  368. r = lstat(name, &sb);
  369. free(name);
  370. if (r == -1)
  371. printerr(1, "stat");
  372. dents[n].mode = sb.st_mode;
  373. n++;
  374. }
  375. /* Make sure cur is in range */
  376. cur = MIN(cur, n - 1);
  377. qsort(dents, n, sizeof(*dents), entrycmp);
  378. for (;;) {
  379. int nlines;
  380. int maxlen;
  381. int odd;
  382. char *pathnew;
  383. char *name;
  384. char *bin;
  385. int fd;
  386. char *dir;
  387. char *tmp;
  388. regex_t re;
  389. struct history *hist;
  390. redraw:
  391. nlines = MIN(LINES - 4, n);
  392. /* Clean screen */
  393. erase();
  394. /* Strip trailing slashes */
  395. for (i = strlen(path) - 1; i > 0; i--)
  396. if (path[i] == '/')
  397. path[i] = '\0';
  398. else
  399. break;
  400. DPRINTF_D(cur);
  401. DPRINTF_S(path);
  402. /* No text wrapping in cwd line */
  403. cwd = malloc(COLS * sizeof(char));
  404. strlcpy(cwd, path, COLS * sizeof(char));
  405. cwd[COLS - strlen(CWD) - 1] = '\0';
  406. printw(CWD "%s\n\n", cwd);
  407. /* Print listing */
  408. odd = ISODD(nlines);
  409. if (cur < nlines / 2) {
  410. for (i = 0; i < nlines; i++)
  411. printent(&dents[i], i == cur);
  412. } else if (cur >= n - nlines / 2) {
  413. for (i = n - nlines; i < n; i++)
  414. printent(&dents[i], i == cur);
  415. } else {
  416. for (i = cur - nlines / 2;
  417. i < cur + nlines / 2 + odd; i++)
  418. printent(&dents[i], i == cur);
  419. }
  420. nochange:
  421. ret = nextsel(&cur, n);
  422. switch (ret) {
  423. case SEL_QUIT:
  424. free(path);
  425. free(filter);
  426. /* Forget history */
  427. while (!SLIST_EMPTY(&histhead)) {
  428. hist = SLIST_FIRST(&histhead);
  429. SLIST_REMOVE_HEAD(&histhead, entry);
  430. free(hist);
  431. }
  432. return;
  433. case SEL_BACK:
  434. /* There is no going back */
  435. if (strcmp(path, "/") == 0) {
  436. goto nochange;
  437. } else {
  438. dir = dirname(path);
  439. tmp = malloc(strlen(dir) + 1);
  440. strlcpy(tmp, dir, strlen(dir) + 1);
  441. free(path);
  442. path = tmp;
  443. free(filter);
  444. filter = strdup(ifilter); /* Reset filter */
  445. /* Recall history */
  446. hist = SLIST_FIRST(&histhead);
  447. if (hist != NULL) {
  448. cur = hist->pos;
  449. DPRINTF_D(hist->pos);
  450. SLIST_REMOVE_HEAD(&histhead, entry);
  451. free(hist);
  452. } else {
  453. cur = 0;
  454. }
  455. goto out;
  456. }
  457. case SEL_GOIN:
  458. /* Cannot descend in empty directories */
  459. if (n == 0)
  460. goto nochange;
  461. name = dents[cur].name;
  462. /* Handle root case */
  463. if (strcmp(path, "/") == 0)
  464. asprintf(&pathnew, "/%s", name);
  465. else
  466. asprintf(&pathnew, "%s/%s", path, name);
  467. DPRINTF_S(name);
  468. DPRINTF_S(pathnew);
  469. /* Get path info */
  470. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  471. if (fd == -1) {
  472. printwarn();
  473. free(pathnew);
  474. goto nochange;
  475. }
  476. r = fstat(fd, &sb);
  477. close(fd);
  478. if (r == -1) {
  479. printwarn();
  480. free(pathnew);
  481. goto nochange;
  482. }
  483. DPRINTF_U(sb.st_mode);
  484. /* Directory */
  485. if (S_ISDIR(sb.st_mode)) {
  486. free(path);
  487. path = pathnew;
  488. free(filter);
  489. filter = strdup(ifilter); /* Reset filter */
  490. /* Save history */
  491. hist = malloc(sizeof(struct history));
  492. hist->pos = cur;
  493. SLIST_INSERT_HEAD(&histhead, hist, entry);
  494. cur = 0;
  495. goto out;
  496. }
  497. /* Regular file */
  498. if (S_ISREG(sb.st_mode)) {
  499. /* Open with */
  500. bin = openwith(name);
  501. if (bin == NULL) {
  502. printmsg("No association");
  503. free(pathnew);
  504. goto nochange;
  505. }
  506. exitcurses();
  507. spawn(bin, pathnew);
  508. initcurses();
  509. free(pathnew);
  510. goto redraw;
  511. }
  512. /* All the rest */
  513. printmsg("Unsupported file");
  514. free(pathnew);
  515. goto nochange;
  516. case SEL_FLTR:
  517. /* Read filter */
  518. printmsg("");
  519. move(LINES - 1, 0);
  520. printw("filter: ");
  521. tmp = readln();
  522. if (tmp == NULL) {
  523. printmsg("");
  524. goto nochange;
  525. }
  526. r = setfilter(&re, tmp);
  527. if (r != 0) {
  528. free(tmp);
  529. goto nochange;
  530. }
  531. free(filter);
  532. filter = tmp;
  533. filter_re = re;
  534. DPRINTF_S(filter);
  535. cur = 0;
  536. goto out;
  537. case SEL_SH:
  538. if (chdir(path) == -1)
  539. printwarn();
  540. exitcurses();
  541. spawn("/bin/sh", NULL);
  542. initcurses();
  543. if (chdir(ipath) == -1)
  544. printwarn();
  545. break;
  546. case SEL_CD:
  547. /* Read target dir */
  548. printmsg("");
  549. move(LINES - 1, 0);
  550. printw("chdir: ");
  551. tmp = readln();
  552. if (tmp == NULL) {
  553. printmsg("");
  554. goto nochange;
  555. }
  556. if (testopendir(tmp) == 0) {
  557. printwarn();
  558. goto nochange;
  559. } else {
  560. free(path);
  561. path = tmp;
  562. free(filter);
  563. filter = strdup(ifilter); /* Reset filter */
  564. DPRINTF_S(path);
  565. cur = 0;
  566. goto out;
  567. }
  568. }
  569. }
  570. out:
  571. for (i = 0; i < n; i++)
  572. free(dents[i].name);
  573. free(dents);
  574. /* Should never be null */
  575. r = closedir(dirp);
  576. if (r == -1)
  577. printerr(1, "closedir");
  578. goto begin;
  579. }
  580. int
  581. main(int argc, char *argv[])
  582. {
  583. char cwd[PATH_MAX], *ipath;
  584. char *ifilter;
  585. if (getuid() == 0)
  586. ifilter = ".*";
  587. else
  588. ifilter = "^[^.].*"; /* Hide dotfiles */
  589. if (argv[1] != NULL) {
  590. ipath = argv[1];
  591. } else {
  592. ipath = getcwd(cwd, sizeof(cwd));
  593. if (ipath == NULL)
  594. ipath = "/";
  595. }
  596. /* Test initial path */
  597. if (!testopendir(ipath))
  598. printerr(1, ipath);
  599. /* Set locale before curses setup */
  600. setlocale(LC_ALL, "");
  601. initcurses();
  602. browse(ipath, ifilter);
  603. exitcurses();
  604. return 0;
  605. }