My build of nnn with minor changes
25개 이상의 토픽을 선택하실 수 없습니다. 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 <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. if (regexec(regex, file, 0, NULL, 0) == 0)
  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. return 0;
  268. }
  269. char *
  270. readln(void)
  271. {
  272. int c;
  273. int i = 0;
  274. char *ln = NULL;
  275. int y, x, x0;
  276. echo();
  277. curs_set(TRUE);
  278. /* Starting point */
  279. getyx(stdscr, y, x);
  280. x0 = x;
  281. while ((c = getch()) != ERR) {
  282. if (c == KEY_ENTER || c == '\r')
  283. break;
  284. if (c == KEY_BACKSPACE) {
  285. getyx(stdscr, y, x);
  286. if (x >= x0) {
  287. i--;
  288. if (i > 0) {
  289. ln = xrealloc(ln, i * sizeof(*ln));
  290. } else {
  291. free(ln);
  292. ln = NULL;
  293. }
  294. move(y, x);
  295. printw("%c", ' ');
  296. move(y, x);
  297. } else {
  298. move(y, x0);
  299. }
  300. continue;
  301. }
  302. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  303. ln[i] = c;
  304. i++;
  305. }
  306. if (ln != NULL) {
  307. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  308. ln[i] = '\0';
  309. }
  310. curs_set(FALSE);
  311. noecho();
  312. return ln;
  313. }
  314. int
  315. canopendir(char *path)
  316. {
  317. DIR *dirp;
  318. dirp = opendir(path);
  319. if (dirp == NULL) {
  320. return 0;
  321. } else {
  322. closedir(dirp);
  323. return 1;
  324. }
  325. }
  326. void
  327. printent(struct entry *ent, int active)
  328. {
  329. char *name;
  330. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  331. char cm = 0;
  332. /* Copy name locally */
  333. name = xstrdup(ent->name);
  334. if (S_ISDIR(ent->mode)) {
  335. cm = '/';
  336. maxlen--;
  337. } else if (S_ISLNK(ent->mode)) {
  338. cm = '@';
  339. maxlen--;
  340. } else if (S_ISSOCK(ent->mode)) {
  341. cm = '=';
  342. maxlen--;
  343. } else if (S_ISFIFO(ent->mode)) {
  344. cm = '|';
  345. maxlen--;
  346. } else if (ent->mode & S_IXUSR) {
  347. cm = '*';
  348. maxlen--;
  349. }
  350. /* No text wrapping in entries */
  351. if (strlen(name) > maxlen)
  352. name[maxlen] = '\0';
  353. if (cm == 0)
  354. printw("%s%s\n", active ? CURSR : EMPTY, name);
  355. else
  356. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  357. free(name);
  358. }
  359. int
  360. dentfill(char *path, struct entry **dents,
  361. int (*filter)(regex_t *, char *), regex_t *re)
  362. {
  363. DIR *dirp;
  364. struct dirent *dp;
  365. struct stat sb;
  366. char *newpath;
  367. int r, n = 0;
  368. dirp = opendir(path);
  369. if (dirp == NULL)
  370. return 0;
  371. while ((dp = readdir(dirp)) != NULL) {
  372. /* Skip self and parent */
  373. if (strcmp(dp->d_name, ".") == 0
  374. || strcmp(dp->d_name, "..") == 0)
  375. continue;
  376. if (filter(re, dp->d_name) == 0)
  377. continue;
  378. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  379. (*dents)[n].name = xstrdup(dp->d_name);
  380. /* Get mode flags */
  381. newpath = makepath(path, dp->d_name);
  382. r = lstat(newpath, &sb);
  383. if (r == -1)
  384. printerr(1, "lstat");
  385. (*dents)[n].mode = sb.st_mode;
  386. n++;
  387. }
  388. /* Should never be null */
  389. r = closedir(dirp);
  390. if (r == -1)
  391. printerr(1, "closedir");
  392. return n;
  393. }
  394. void
  395. dentfree(struct entry *dents, int n)
  396. {
  397. int i;
  398. for (i = 0; i < n; i++)
  399. free(dents[i].name);
  400. free(dents);
  401. }
  402. char *
  403. makepath(char *dir, char *name)
  404. {
  405. char path[PATH_MAX];
  406. /* Handle absolute path */
  407. if (name[0] == '/') {
  408. strlcpy(path, name, sizeof(path));
  409. } else {
  410. /* Handle root case */
  411. if (strcmp(dir, "/") == 0) {
  412. strlcpy(path, "/", sizeof(path));
  413. strlcat(path, name, sizeof(path));
  414. } else {
  415. strlcpy(path, dir, sizeof(path));
  416. strlcat(path, "/", sizeof(path));
  417. strlcat(path, name, sizeof(path));
  418. }
  419. }
  420. return xstrdup(path);
  421. }
  422. /* Return the position of the matching entry or 0 otherwise */
  423. int
  424. dentfind(struct entry *dents, int n, char *cwd, char *path)
  425. {
  426. int i;
  427. char *tmp;
  428. if (path == NULL)
  429. return 0;
  430. for (i = 0; i < n; i++) {
  431. tmp = makepath(cwd, dents[i].name);
  432. DPRINTF_S(path);
  433. DPRINTF_S(tmp);
  434. if (strcmp(tmp, path) == 0) {
  435. free(tmp);
  436. return i;
  437. }
  438. free(tmp);
  439. }
  440. return 0;
  441. }
  442. void
  443. browse(const char *ipath, const char *ifilter)
  444. {
  445. struct entry *dents;
  446. int i, n, cur, r, fd;
  447. int nlines, odd;
  448. char *path = xstrdup(ipath);
  449. char *filter = xstrdup(ifilter);
  450. regex_t filter_re, re;
  451. char *cwd, *newpath, *oldpath = NULL;
  452. struct stat sb;
  453. char *name, *bin, *dir, *tmp;
  454. begin:
  455. /* Path and filter should be malloc(3)-ed strings at all times */
  456. n = 0;
  457. dents = NULL;
  458. if (canopendir(path) == 0) {
  459. printwarn();
  460. goto nochange;
  461. }
  462. /* Search filter */
  463. r = setfilter(&filter_re, filter);
  464. if (r != 0)
  465. goto nochange;
  466. n = dentfill(path, &dents, visible, &filter_re);
  467. qsort(dents, n, sizeof(*dents), entrycmp);
  468. /* Find cur from history */
  469. cur = dentfind(dents, n, path, oldpath);
  470. free(oldpath);
  471. oldpath = NULL;
  472. for (;;) {
  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. goto out;
  563. case S_IFREG:
  564. bin = openwith(name);
  565. if (bin == NULL) {
  566. printmsg("No association");
  567. free(newpath);
  568. goto nochange;
  569. }
  570. exitcurses();
  571. spawn(bin, newpath, NULL);
  572. initcurses();
  573. free(newpath);
  574. continue;
  575. default:
  576. printmsg("Unsupported file");
  577. goto nochange;
  578. }
  579. case SEL_FLTR:
  580. /* Read filter */
  581. printprompt("filter: ");
  582. tmp = readln();
  583. if (tmp == NULL) {
  584. clearprompt();
  585. goto nochange;
  586. }
  587. r = setfilter(&re, tmp);
  588. if (r != 0) {
  589. free(tmp);
  590. goto nochange;
  591. }
  592. free(filter);
  593. filter = tmp;
  594. filter_re = re;
  595. DPRINTF_S(filter);
  596. /* Save current */
  597. oldpath = makepath(path, dents[cur].name);
  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. goto out;
  641. }
  642. }
  643. out:
  644. dentfree(dents, n);
  645. goto begin;
  646. }
  647. int
  648. main(int argc, char *argv[])
  649. {
  650. char cwd[PATH_MAX], *ipath;
  651. char *ifilter;
  652. if (getuid() == 0)
  653. ifilter = ".";
  654. else
  655. ifilter = "^[^.]"; /* Hide dotfiles */
  656. if (argv[1] != NULL) {
  657. ipath = argv[1];
  658. } else {
  659. ipath = getcwd(cwd, sizeof(cwd));
  660. if (ipath == NULL)
  661. ipath = "/";
  662. }
  663. /* Test initial path */
  664. if (canopendir(ipath) == 0)
  665. printerr(1, ipath);
  666. /* Set locale before curses setup */
  667. setlocale(LC_ALL, "");
  668. initcurses();
  669. browse(ipath, ifilter);
  670. exitcurses();
  671. return 0;
  672. }