My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

690 line
12 KiB

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