My build of nnn with minor changes
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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