My build of nnn with minor changes
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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