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.
 
 
 
 
 
 

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