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.
 
 
 
 
 
 

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