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.
 
 
 
 
 
 

781 line
12 KiB

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <curses.h>
  8. #include <libgen.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <regex.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <signal.h>
  15. #include <string.h>
  16. #include <unistd.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. #undef MIN
  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. #include "config.h"
  40. struct entry {
  41. char *name;
  42. mode_t mode;
  43. };
  44. /*
  45. * Layout:
  46. * .---------
  47. * | cwd: /mnt/path
  48. * |
  49. * | file0
  50. * | file1
  51. * | > file2
  52. * | file3
  53. * | file4
  54. * ...
  55. * | filen
  56. * |
  57. * | Permission denied
  58. * '------
  59. */
  60. void printmsg(char *msg);
  61. void printwarn(void);
  62. void printerr(int ret, char *prefix);
  63. void *
  64. xmalloc(size_t size)
  65. {
  66. void *p;
  67. p = malloc(size);
  68. if (p == NULL)
  69. printerr(1, "malloc");
  70. return p;
  71. }
  72. void *
  73. xrealloc(void *p, size_t size)
  74. {
  75. p = realloc(p, size);
  76. if (p == NULL)
  77. printerr(1, "realloc");
  78. return p;
  79. }
  80. char *
  81. xstrdup(const char *s)
  82. {
  83. char *p;
  84. p = strdup(s);
  85. if (p == NULL)
  86. printerr(1, "strdup");
  87. return p;
  88. }
  89. char *
  90. xrealpath(const char *path)
  91. {
  92. char *p;
  93. p = realpath(path, NULL);
  94. if (p == NULL)
  95. printerr(1, "realpath");
  96. return p;
  97. }
  98. char *
  99. xdirname(const char *path)
  100. {
  101. char *p, *tmp;
  102. /* Some implementations of dirname(3) may modify `path' and some
  103. * return a pointer inside `path` and we cannot free(3) the
  104. * original string if we lose track of it. */
  105. tmp = xstrdup(path);
  106. p = dirname(tmp);
  107. if (p == NULL) {
  108. free(tmp);
  109. printerr(1, "dirname");
  110. }
  111. /* Make sure this is a malloc(3)-ed string */
  112. p = xstrdup(p);
  113. free(tmp);
  114. return p;
  115. }
  116. void
  117. spawn(const char *file, const char *arg, const char *dir)
  118. {
  119. pid_t pid;
  120. int status;
  121. pid = fork();
  122. if (pid == 0) {
  123. if (dir != NULL)
  124. chdir(dir);
  125. execlp(file, file, arg, NULL);
  126. _exit(1);
  127. } else {
  128. /* Ignore interruptions */
  129. while (waitpid(pid, &status, 0) == -1)
  130. DPRINTF_D(status);
  131. DPRINTF_D(pid);
  132. }
  133. }
  134. char *
  135. openwith(char *file)
  136. {
  137. regex_t regex;
  138. char *bin = NULL;
  139. int i;
  140. for (i = 0; i < LEN(assocs); i++) {
  141. if (regcomp(&regex, assocs[i].regex,
  142. REG_NOSUB | REG_EXTENDED) != 0)
  143. continue;
  144. if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
  145. bin = assocs[i].bin;
  146. break;
  147. }
  148. }
  149. DPRINTF_S(bin);
  150. return bin;
  151. }
  152. int
  153. setfilter(regex_t *regex, char *filter)
  154. {
  155. char *errbuf;
  156. int r;
  157. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
  158. if (r != 0) {
  159. errbuf = xmalloc(COLS * sizeof(char));
  160. regerror(r, regex, errbuf, COLS * sizeof(char));
  161. printmsg(errbuf);
  162. free(errbuf);
  163. }
  164. return r;
  165. }
  166. int
  167. visible(regex_t *regex, char *file)
  168. {
  169. if (regexec(regex, file, 0, NULL, 0) != REG_NOMATCH)
  170. return 1;
  171. return 0;
  172. }
  173. int
  174. entrycmp(const void *va, const void *vb)
  175. {
  176. const struct entry *a, *b;
  177. a = (struct entry *)va;
  178. b = (struct entry *)vb;
  179. return strcmp(a->name, b->name);
  180. }
  181. void
  182. initcurses(void)
  183. {
  184. initscr();
  185. cbreak();
  186. noecho();
  187. nonl();
  188. intrflush(stdscr, FALSE);
  189. keypad(stdscr, TRUE);
  190. curs_set(FALSE); /* Hide cursor */
  191. }
  192. void
  193. exitcurses(void)
  194. {
  195. endwin(); /* Restore terminal */
  196. }
  197. /* Messages show up at the bottom */
  198. void
  199. printmsg(char *msg)
  200. {
  201. move(LINES - 1, 0);
  202. printw("%s\n", msg);
  203. }
  204. /* Display warning as a message */
  205. void
  206. printwarn(void)
  207. {
  208. printmsg(strerror(errno));
  209. }
  210. /* Kill curses and display error before exiting */
  211. void
  212. printerr(int ret, char *prefix)
  213. {
  214. exitcurses();
  215. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  216. exit(ret);
  217. }
  218. /* Clear the last line */
  219. void
  220. clearprompt(void)
  221. {
  222. printmsg("");
  223. }
  224. /* Print prompt on the last line */
  225. void
  226. printprompt(char *str)
  227. {
  228. clearprompt();
  229. printw(str);
  230. }
  231. /*
  232. * Returns 0 normally
  233. * On movement it updates *cur
  234. * Returns SEL_{QUIT,BACK,GOIN,FLTR,SH,CD} otherwise
  235. */
  236. enum {
  237. SEL_QUIT = 1,
  238. SEL_BACK,
  239. SEL_GOIN,
  240. SEL_FLTR,
  241. SEL_SH,
  242. SEL_CD,
  243. };
  244. int
  245. nextsel(int *cur, int max)
  246. {
  247. int c;
  248. c = getch();
  249. switch (c) {
  250. case 'q':
  251. return SEL_QUIT;
  252. /* Back */
  253. case KEY_BACKSPACE:
  254. case KEY_LEFT:
  255. case 'h':
  256. return SEL_BACK;
  257. /* Inside */
  258. case KEY_ENTER:
  259. case '\r':
  260. case KEY_RIGHT:
  261. case 'l':
  262. return SEL_GOIN;
  263. /* Filter */
  264. case '/':
  265. case '&':
  266. return SEL_FLTR;
  267. /* Next */
  268. case 'j':
  269. case KEY_DOWN:
  270. case CONTROL('N'):
  271. if (*cur < max - 1)
  272. (*cur)++;
  273. break;
  274. /* Previous */
  275. case 'k':
  276. case KEY_UP:
  277. case CONTROL('P'):
  278. if (*cur > 0)
  279. (*cur)--;
  280. break;
  281. /* Page down */
  282. case KEY_NPAGE:
  283. case CONTROL('D'):
  284. if (*cur < max -1)
  285. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  286. break;
  287. /* Page up */
  288. case KEY_PPAGE:
  289. case CONTROL('U'):
  290. if (*cur > 0)
  291. (*cur) -= MIN((LINES - 4) / 2, *cur);
  292. break;
  293. case '!':
  294. return SEL_SH;
  295. case 'c':
  296. return SEL_CD;
  297. }
  298. return 0;
  299. }
  300. char *
  301. readln(void)
  302. {
  303. int c;
  304. int i = 0;
  305. char *ln = NULL;
  306. int y, x, x0;
  307. echo();
  308. curs_set(TRUE);
  309. /* Starting point */
  310. getyx(stdscr, y, x);
  311. x0 = x;
  312. while ((c = getch()) != ERR) {
  313. if (c == KEY_ENTER || c == '\r')
  314. break;
  315. if (c == KEY_BACKSPACE) {
  316. getyx(stdscr, y, x);
  317. if (x >= x0) {
  318. i--;
  319. if (i > 0) {
  320. ln = xrealloc(ln, i * sizeof(*ln));
  321. } else {
  322. free(ln);
  323. ln = NULL;
  324. }
  325. move(y, x);
  326. printw("%c", ' ');
  327. move(y, x);
  328. } else {
  329. move(y, x0);
  330. }
  331. continue;
  332. }
  333. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  334. ln[i] = c;
  335. i++;
  336. }
  337. if (ln != NULL) {
  338. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  339. ln[i] = '\0';
  340. }
  341. curs_set(FALSE);
  342. noecho();
  343. return ln;
  344. }
  345. int
  346. canopendir(char *path)
  347. {
  348. DIR *dirp;
  349. dirp = opendir(path);
  350. if (dirp == NULL) {
  351. return 0;
  352. } else {
  353. closedir(dirp);
  354. return 1;
  355. }
  356. }
  357. void
  358. printent(struct entry *ent, int active)
  359. {
  360. char *name;
  361. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  362. char cm = 0;
  363. /* Copy name locally */
  364. name = xstrdup(ent->name);
  365. if (S_ISDIR(ent->mode)) {
  366. cm = '/';
  367. maxlen--;
  368. } else if (S_ISLNK(ent->mode)) {
  369. cm = '@';
  370. maxlen--;
  371. } else if (ent->mode & S_IXUSR) {
  372. cm = '*';
  373. maxlen--;
  374. }
  375. /* No text wrapping in entries */
  376. if (strlen(name) > maxlen)
  377. name[maxlen] = '\0';
  378. if (cm == 0)
  379. printw("%s%s\n", active ? CURSR : EMPTY, name);
  380. else
  381. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  382. free(name);
  383. }
  384. int
  385. dentfill(DIR *dirp, struct entry **dents,
  386. int (*filter)(regex_t *, char *), regex_t *re)
  387. {
  388. struct dirent *dp;
  389. struct stat sb;
  390. int n = 0;
  391. int r;
  392. while ((dp = readdir(dirp)) != NULL) {
  393. /* Skip self and parent */
  394. if (strcmp(dp->d_name, ".") == 0
  395. || strcmp(dp->d_name, "..") == 0)
  396. continue;
  397. if (filter(re, dp->d_name) == 0)
  398. continue;
  399. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  400. (*dents)[n].name = xstrdup(dp->d_name);
  401. /* Get mode flags */
  402. r = fstatat(dirfd(dirp), dp->d_name, &sb,
  403. AT_SYMLINK_NOFOLLOW);
  404. if (r == -1)
  405. printerr(1, "stat");
  406. (*dents)[n].mode = sb.st_mode;
  407. n++;
  408. }
  409. return n;
  410. }
  411. void
  412. dentfree(struct entry *dents, int n)
  413. {
  414. int i;
  415. for (i = 0; i < n; i++)
  416. free(dents[i].name);
  417. free(dents);
  418. }
  419. char *
  420. makepath(char *dir, char *name)
  421. {
  422. char *path;
  423. /* Handle absolute path */
  424. if (name[0] == '/') {
  425. path = xstrdup(name);
  426. } else {
  427. /* Handle root case */
  428. if (strcmp(dir, "/") == 0)
  429. asprintf(&path, "/%s", name);
  430. else
  431. asprintf(&path, "%s/%s", dir, name);
  432. }
  433. return path;
  434. }
  435. /* Return the position of the matching entry or 0 otherwise */
  436. int
  437. dentfind(struct entry *dents, int n, char *cwd, char *path)
  438. {
  439. int i;
  440. char *tmp;
  441. if (path == NULL)
  442. return 0;
  443. for (i = 0; i < n; i++) {
  444. tmp = makepath(cwd, dents[i].name);
  445. DPRINTF_S(path);
  446. DPRINTF_S(tmp);
  447. if (strcmp(tmp, path) == 0) {
  448. free(tmp);
  449. return i;
  450. }
  451. free(tmp);
  452. }
  453. return 0;
  454. }
  455. void
  456. browse(const char *ipath, const char *ifilter)
  457. {
  458. DIR *dirp;
  459. struct entry *dents;
  460. int i, n, cur;
  461. int r, ret, fd;
  462. char *path = xstrdup(ipath);
  463. char *filter = xstrdup(ifilter);
  464. regex_t filter_re;
  465. char *cwd, *newpath;
  466. struct stat sb;
  467. char *oldpath;
  468. cur = 0;
  469. oldpath = NULL;
  470. begin:
  471. /* Path and filter should be malloc(3)-ed strings at all times */
  472. n = 0;
  473. dents = NULL;
  474. dirp = opendir(path);
  475. if (dirp == NULL) {
  476. printwarn();
  477. goto nochange;
  478. }
  479. /* Search filter */
  480. r = setfilter(&filter_re, filter);
  481. if (r != 0)
  482. goto nochange;
  483. n = dentfill(dirp, &dents, visible, &filter_re);
  484. qsort(dents, n, sizeof(*dents), entrycmp);
  485. /* Find cur from history */
  486. cur = dentfind(dents, n, path, oldpath);
  487. if (oldpath != NULL) {
  488. free(oldpath);
  489. oldpath = NULL;
  490. }
  491. for (;;) {
  492. int nlines;
  493. int odd;
  494. char *name;
  495. char *bin;
  496. char *dir;
  497. char *tmp;
  498. regex_t re;
  499. redraw:
  500. nlines = MIN(LINES - 4, n);
  501. /* Clean screen */
  502. erase();
  503. DPRINTF_D(cur);
  504. DPRINTF_S(path);
  505. /* No text wrapping in cwd line */
  506. cwd = xmalloc(COLS * sizeof(char));
  507. strlcpy(cwd, path, COLS * sizeof(char));
  508. cwd[COLS - strlen(CWD) - 1] = '\0';
  509. printw(CWD "%s\n\n", cwd);
  510. /* Print listing */
  511. odd = ISODD(nlines);
  512. if (cur < nlines / 2) {
  513. for (i = 0; i < nlines; i++)
  514. printent(&dents[i], i == cur);
  515. } else if (cur >= n - nlines / 2) {
  516. for (i = n - nlines; i < n; i++)
  517. printent(&dents[i], i == cur);
  518. } else {
  519. for (i = cur - nlines / 2;
  520. i < cur + nlines / 2 + odd; i++)
  521. printent(&dents[i], i == cur);
  522. }
  523. nochange:
  524. ret = nextsel(&cur, n);
  525. switch (ret) {
  526. case SEL_QUIT:
  527. free(path);
  528. free(filter);
  529. dentfree(dents, n);
  530. return;
  531. case SEL_BACK:
  532. /* There is no going back */
  533. if (strcmp(path, "/") == 0 ||
  534. strcmp(path, ".") == 0 ||
  535. strchr(path, '/') == NULL)
  536. goto nochange;
  537. if (canopendir(path) == 0) {
  538. printwarn();
  539. goto nochange;
  540. }
  541. dir = xdirname(path);
  542. /* Save history */
  543. oldpath = path;
  544. path = dir;
  545. /* Reset filter */
  546. free(filter);
  547. filter = xstrdup(ifilter);
  548. goto out;
  549. case SEL_GOIN:
  550. /* Cannot descend in empty directories */
  551. if (n == 0)
  552. goto nochange;
  553. name = dents[cur].name;
  554. newpath = makepath(path, name);
  555. DPRINTF_S(newpath);
  556. /* Get path info */
  557. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  558. if (fd == -1) {
  559. printwarn();
  560. free(newpath);
  561. goto nochange;
  562. }
  563. r = fstat(fd, &sb);
  564. if (r == -1) {
  565. printwarn();
  566. close(fd);
  567. free(newpath);
  568. goto nochange;
  569. }
  570. close(fd);
  571. DPRINTF_U(sb.st_mode);
  572. switch (sb.st_mode & S_IFMT) {
  573. case S_IFDIR:
  574. if (canopendir(newpath) == 0) {
  575. printwarn();
  576. free(newpath);
  577. goto nochange;
  578. }
  579. free(path);
  580. path = newpath;
  581. /* Reset filter */
  582. free(filter);
  583. filter = xstrdup(ifilter);
  584. cur = 0;
  585. goto out;
  586. case S_IFREG:
  587. bin = openwith(name);
  588. if (bin == NULL) {
  589. printmsg("No association");
  590. free(newpath);
  591. goto nochange;
  592. }
  593. exitcurses();
  594. spawn(bin, newpath, NULL);
  595. initcurses();
  596. free(newpath);
  597. goto redraw;
  598. default:
  599. printmsg("Unsupported file");
  600. goto nochange;
  601. }
  602. case SEL_FLTR:
  603. /* Read filter */
  604. printprompt("filter: ");
  605. tmp = readln();
  606. if (tmp == NULL) {
  607. clearprompt();
  608. goto nochange;
  609. }
  610. r = setfilter(&re, tmp);
  611. if (r != 0) {
  612. free(tmp);
  613. goto nochange;
  614. }
  615. free(filter);
  616. filter = tmp;
  617. filter_re = re;
  618. DPRINTF_S(filter);
  619. cur = 0;
  620. goto out;
  621. case SEL_SH:
  622. exitcurses();
  623. spawn("/bin/sh", NULL, path);
  624. initcurses();
  625. break;
  626. case SEL_CD:
  627. /* Read target dir */
  628. printprompt("chdir: ");
  629. tmp = readln();
  630. if (tmp == NULL) {
  631. clearprompt();
  632. goto nochange;
  633. }
  634. newpath = makepath(path, tmp);
  635. free(tmp);
  636. if (canopendir(newpath) == 0) {
  637. free(newpath);
  638. printwarn();
  639. goto nochange;
  640. }
  641. free(path);
  642. path = newpath;
  643. free(filter);
  644. filter = xstrdup(ifilter); /* Reset filter */
  645. DPRINTF_S(path);
  646. cur = 0;
  647. goto out;
  648. }
  649. }
  650. out:
  651. dentfree(dents, n);
  652. /* Should never be null */
  653. r = closedir(dirp);
  654. if (r == -1)
  655. printerr(1, "closedir");
  656. goto begin;
  657. }
  658. int
  659. main(int argc, char *argv[])
  660. {
  661. char cwd[PATH_MAX], *ipath;
  662. char *ifilter;
  663. if (getuid() == 0)
  664. ifilter = ".*";
  665. else
  666. ifilter = "^[^.].*"; /* Hide dotfiles */
  667. if (argv[1] != NULL) {
  668. ipath = argv[1];
  669. } else {
  670. ipath = getcwd(cwd, sizeof(cwd));
  671. if (ipath == NULL)
  672. ipath = "/";
  673. }
  674. /* Test initial path */
  675. if (canopendir(ipath) == 0)
  676. printerr(1, ipath);
  677. /* Set locale before curses setup */
  678. setlocale(LC_ALL, "");
  679. initcurses();
  680. browse(ipath, ifilter);
  681. exitcurses();
  682. return 0;
  683. }