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.
 
 
 
 
 
 

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