My build of nnn with minor changes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

786 linhas
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. oldpath = NULL;
  457. begin:
  458. /* Path and filter should be malloc(3)-ed strings at all times */
  459. n = 0;
  460. dents = NULL;
  461. if (canopendir(path) == 0) {
  462. printwarn();
  463. goto nochange;
  464. }
  465. /* Search filter */
  466. r = setfilter(&filter_re, filter);
  467. if (r != 0)
  468. goto nochange;
  469. n = dentfill(path, &dents, visible, &filter_re);
  470. qsort(dents, n, sizeof(*dents), entrycmp);
  471. /* Find cur from history */
  472. cur = dentfind(dents, n, path, oldpath);
  473. if (oldpath != NULL) {
  474. free(oldpath);
  475. oldpath = NULL;
  476. }
  477. for (;;) {
  478. int nlines;
  479. int odd;
  480. char *name;
  481. char *bin;
  482. char *dir;
  483. char *tmp;
  484. regex_t re;
  485. redraw:
  486. nlines = MIN(LINES - 4, n);
  487. /* Clean screen */
  488. erase();
  489. /* Strip trailing slashes */
  490. for (i = strlen(path) - 1; i > 0; i--)
  491. if (path[i] == '/')
  492. path[i] = '\0';
  493. else
  494. break;
  495. DPRINTF_D(cur);
  496. DPRINTF_S(path);
  497. /* No text wrapping in cwd line */
  498. cwd = xmalloc(COLS * sizeof(char));
  499. strlcpy(cwd, path, COLS * sizeof(char));
  500. cwd[COLS - strlen(CWD) - 1] = '\0';
  501. printw(CWD "%s\n\n", cwd);
  502. /* Print listing */
  503. odd = ISODD(nlines);
  504. if (cur < nlines / 2) {
  505. for (i = 0; i < nlines; i++)
  506. printent(&dents[i], i == cur);
  507. } else if (cur >= n - nlines / 2) {
  508. for (i = n - nlines; i < n; i++)
  509. printent(&dents[i], i == cur);
  510. } else {
  511. for (i = cur - nlines / 2;
  512. i < cur + nlines / 2 + odd; i++)
  513. printent(&dents[i], i == cur);
  514. }
  515. nochange:
  516. switch (nextsel()) {
  517. case SEL_QUIT:
  518. free(path);
  519. free(filter);
  520. dentfree(dents, n);
  521. return;
  522. case SEL_BACK:
  523. /* There is no going back */
  524. if (strcmp(path, "/") == 0 ||
  525. strcmp(path, ".") == 0 ||
  526. strchr(path, '/') == NULL)
  527. goto nochange;
  528. if (canopendir(path) == 0) {
  529. printwarn();
  530. goto nochange;
  531. }
  532. dir = xdirname(path);
  533. /* Save history */
  534. oldpath = path;
  535. path = dir;
  536. /* Reset filter */
  537. free(filter);
  538. filter = xstrdup(ifilter);
  539. goto out;
  540. case SEL_GOIN:
  541. /* Cannot descend in empty directories */
  542. if (n == 0)
  543. goto nochange;
  544. name = dents[cur].name;
  545. newpath = makepath(path, name);
  546. DPRINTF_S(newpath);
  547. /* Get path info */
  548. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  549. if (fd == -1) {
  550. printwarn();
  551. free(newpath);
  552. goto nochange;
  553. }
  554. r = fstat(fd, &sb);
  555. if (r == -1) {
  556. printwarn();
  557. close(fd);
  558. free(newpath);
  559. goto nochange;
  560. }
  561. close(fd);
  562. DPRINTF_U(sb.st_mode);
  563. switch (sb.st_mode & S_IFMT) {
  564. case S_IFDIR:
  565. if (canopendir(newpath) == 0) {
  566. printwarn();
  567. free(newpath);
  568. goto nochange;
  569. }
  570. free(path);
  571. path = newpath;
  572. /* Reset filter */
  573. free(filter);
  574. filter = xstrdup(ifilter);
  575. goto out;
  576. case S_IFREG:
  577. bin = openwith(name);
  578. if (bin == NULL) {
  579. printmsg("No association");
  580. free(newpath);
  581. goto nochange;
  582. }
  583. exitcurses();
  584. spawn(bin, newpath, NULL);
  585. initcurses();
  586. free(newpath);
  587. goto redraw;
  588. default:
  589. printmsg("Unsupported file");
  590. goto nochange;
  591. }
  592. case SEL_FLTR:
  593. /* Read filter */
  594. printprompt("filter: ");
  595. tmp = readln();
  596. if (tmp == NULL) {
  597. clearprompt();
  598. goto nochange;
  599. }
  600. r = setfilter(&re, tmp);
  601. if (r != 0) {
  602. free(tmp);
  603. goto nochange;
  604. }
  605. free(filter);
  606. filter = tmp;
  607. filter_re = re;
  608. DPRINTF_S(filter);
  609. /* Save current */
  610. oldpath = makepath(path, dents[cur].name);
  611. goto out;
  612. case SEL_NEXT:
  613. if (cur < n - 1)
  614. cur++;
  615. break;
  616. case SEL_PREV:
  617. if (cur > 0)
  618. cur--;
  619. break;
  620. case SEL_PGDN:
  621. if (cur < n - 1)
  622. cur += MIN((LINES - 4) / 2, n - 1 - cur);
  623. break;
  624. case SEL_PGUP:
  625. if (cur > 0)
  626. cur -= MIN((LINES - 4) / 2, cur);
  627. break;
  628. case SEL_SH:
  629. exitcurses();
  630. spawn("/bin/sh", NULL, path);
  631. initcurses();
  632. break;
  633. case SEL_CD:
  634. /* Read target dir */
  635. printprompt("chdir: ");
  636. tmp = readln();
  637. if (tmp == NULL) {
  638. clearprompt();
  639. goto nochange;
  640. }
  641. newpath = makepath(path, tmp);
  642. free(tmp);
  643. if (canopendir(newpath) == 0) {
  644. free(newpath);
  645. printwarn();
  646. goto nochange;
  647. }
  648. free(path);
  649. path = newpath;
  650. free(filter);
  651. filter = xstrdup(ifilter); /* Reset filter */
  652. DPRINTF_S(path);
  653. goto out;
  654. }
  655. }
  656. out:
  657. dentfree(dents, n);
  658. goto begin;
  659. }
  660. int
  661. main(int argc, char *argv[])
  662. {
  663. char cwd[PATH_MAX], *ipath;
  664. char *ifilter;
  665. if (getuid() == 0)
  666. ifilter = ".";
  667. else
  668. ifilter = "^[^.]"; /* Hide dotfiles */
  669. if (argv[1] != NULL) {
  670. ipath = argv[1];
  671. } else {
  672. ipath = getcwd(cwd, sizeof(cwd));
  673. if (ipath == NULL)
  674. ipath = "/";
  675. }
  676. /* Test initial path */
  677. if (canopendir(ipath) == 0)
  678. printerr(1, ipath);
  679. /* Set locale before curses setup */
  680. setlocale(LC_ALL, "");
  681. initcurses();
  682. browse(ipath, ifilter);
  683. exitcurses();
  684. return 0;
  685. }