My build of nnn with minor changes
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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