My build of nnn with minor changes
 
 
 
 
 
 

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