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.
 
 
 
 
 
 

1012 line
20 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <curses.h>
  6. #include <dirent.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <libgen.h>
  10. #include <limits.h>
  11. #include <locale.h>
  12. #include <regex.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <time.h>
  20. #include "util.h"
  21. #ifdef DEBUG
  22. #define DEBUG_FD 8
  23. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  24. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  25. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  26. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  27. #else
  28. #define DPRINTF_D(x)
  29. #define DPRINTF_U(x)
  30. #define DPRINTF_S(x)
  31. #define DPRINTF_P(x)
  32. #endif /* DEBUG */
  33. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  34. #undef MIN
  35. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  36. #define ISODD(x) ((x) & 1)
  37. #define CONTROL(c) ((c) ^ 0x40)
  38. #define TOUPPER(ch) \
  39. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  40. #define MAX_LEN 1024
  41. #define cur(flag) (flag ? CURSR : EMPTY)
  42. struct assoc {
  43. char *regex; /* Regex to match on filename */
  44. char *bin; /* Program */
  45. };
  46. /* Supported actions */
  47. enum action {
  48. SEL_QUIT = 1,
  49. SEL_BACK,
  50. SEL_GOIN,
  51. SEL_FLTR,
  52. SEL_NEXT,
  53. SEL_PREV,
  54. SEL_PGDN,
  55. SEL_PGUP,
  56. SEL_HOME,
  57. SEL_END,
  58. SEL_CD,
  59. SEL_CDHOME,
  60. SEL_TOGGLEDOT,
  61. SEL_DETAIL,
  62. SEL_FSIZE,
  63. SEL_MTIME,
  64. SEL_REDRAW,
  65. SEL_COPY,
  66. SEL_RUN,
  67. SEL_RUNARG,
  68. };
  69. struct key {
  70. int sym; /* Key pressed */
  71. enum action act; /* Action */
  72. char *run; /* Program to run */
  73. char *env; /* Environment variable to run */
  74. };
  75. #include "config.h"
  76. struct entry {
  77. char name[PATH_MAX];
  78. mode_t mode;
  79. time_t t;
  80. off_t size;
  81. };
  82. /* Global context */
  83. struct entry *dents;
  84. int ndents, cur;
  85. int idle;
  86. char *opener = NULL;
  87. char *fallback_opener = NULL;
  88. char *copier = NULL;
  89. char size_buf[12]; /* Buffer to hold human readable size */
  90. const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  91. /*
  92. * Layout:
  93. * .---------
  94. * | cwd: /mnt/path
  95. * |
  96. * | file0
  97. * | file1
  98. * | > file2
  99. * | file3
  100. * | file4
  101. * ...
  102. * | filen
  103. * |
  104. * | Permission denied
  105. * '------
  106. */
  107. void printmsg(char *);
  108. void printwarn(void);
  109. void printerr(int, char *);
  110. #undef dprintf
  111. int
  112. dprintf(int fd, const char *fmt, ...)
  113. {
  114. char buf[BUFSIZ];
  115. int r;
  116. va_list ap;
  117. va_start(ap, fmt);
  118. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  119. if (r > 0)
  120. r = write(fd, buf, r);
  121. va_end(ap);
  122. return r;
  123. }
  124. void *
  125. xmalloc(size_t size)
  126. {
  127. void *p;
  128. p = malloc(size);
  129. if (p == NULL)
  130. printerr(1, "malloc");
  131. return p;
  132. }
  133. void *
  134. xrealloc(void *p, size_t size)
  135. {
  136. p = realloc(p, size);
  137. if (p == NULL)
  138. printerr(1, "realloc");
  139. return p;
  140. }
  141. char *
  142. xstrdup(const char *s)
  143. {
  144. char *p;
  145. p = strdup(s);
  146. if (p == NULL)
  147. printerr(1, "strdup");
  148. return p;
  149. }
  150. /* Some implementations of dirname(3) may modify `path' and some
  151. * return a pointer inside `path'. */
  152. char *
  153. xdirname(const char *path)
  154. {
  155. static char out[PATH_MAX];
  156. char tmp[PATH_MAX], *p;
  157. strlcpy(tmp, path, sizeof(tmp));
  158. p = dirname(tmp);
  159. if (p == NULL)
  160. printerr(1, "dirname");
  161. strlcpy(out, p, sizeof(out));
  162. return out;
  163. }
  164. void
  165. spawn(char *file, char *arg, char *dir)
  166. {
  167. pid_t pid;
  168. int status;
  169. pid = fork();
  170. if (pid == 0) {
  171. if (dir != NULL)
  172. status = chdir(dir);
  173. execlp(file, file, arg, NULL);
  174. _exit(1);
  175. } else {
  176. /* Ignore interruptions */
  177. while (waitpid(pid, &status, 0) == -1)
  178. DPRINTF_D(status);
  179. DPRINTF_D(pid);
  180. }
  181. }
  182. char *
  183. xgetenv(char *name, char *fallback)
  184. {
  185. char *value;
  186. if (name == NULL)
  187. return fallback;
  188. value = getenv(name);
  189. return value && value[0] ? value : fallback;
  190. }
  191. int
  192. xstricmp(const char *s1, const char *s2)
  193. {
  194. while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2))
  195. s1++, s2++;
  196. /* In case of alphabetically same names, make sure
  197. lower case one comes before upper case one */
  198. if (!*s1 && !*s2)
  199. return 1;
  200. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  201. }
  202. char *
  203. openwith(char *file)
  204. {
  205. regex_t regex;
  206. char *bin = NULL;
  207. int i;
  208. for (i = 0; i < LEN(assocs); i++) {
  209. if (regcomp(&regex, assocs[i].regex,
  210. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  211. continue;
  212. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  213. bin = assocs[i].bin;
  214. break;
  215. }
  216. }
  217. DPRINTF_S(bin);
  218. return bin;
  219. }
  220. int
  221. setfilter(regex_t *regex, char *filter)
  222. {
  223. char errbuf[LINE_MAX];
  224. size_t len;
  225. int r;
  226. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  227. if (r != 0) {
  228. len = COLS;
  229. if (len > sizeof(errbuf))
  230. len = sizeof(errbuf);
  231. regerror(r, regex, errbuf, len);
  232. printmsg(errbuf);
  233. }
  234. return r;
  235. }
  236. void
  237. initfilter(int dot, char **ifilter)
  238. {
  239. *ifilter = dot ? "." : "^[^.]";
  240. }
  241. int
  242. visible(regex_t *regex, char *file)
  243. {
  244. return regexec(regex, file, 0, NULL, 0) == 0;
  245. }
  246. int
  247. entrycmp(const void *va, const void *vb)
  248. {
  249. if (mtimeorder)
  250. return ((struct entry *)vb)->t - ((struct entry *)va)->t;
  251. if (sizeorder)
  252. return ((struct entry *)vb)->size - ((struct entry *)va)->size;
  253. return xstricmp(((struct entry *)va)->name, ((struct entry *)vb)->name);
  254. }
  255. void
  256. initcurses(void)
  257. {
  258. if (initscr() == NULL) {
  259. char *term = getenv("TERM");
  260. if (term != NULL)
  261. fprintf(stderr, "error opening terminal: %s\n", term);
  262. else
  263. fprintf(stderr, "failed to initialize curses\n");
  264. exit(1);
  265. }
  266. cbreak();
  267. noecho();
  268. nonl();
  269. intrflush(stdscr, FALSE);
  270. keypad(stdscr, TRUE);
  271. curs_set(FALSE); /* Hide cursor */
  272. timeout(1000); /* One second */
  273. }
  274. void
  275. exitcurses(void)
  276. {
  277. endwin(); /* Restore terminal */
  278. }
  279. /* Messages show up at the bottom */
  280. void
  281. printmsg(char *msg)
  282. {
  283. move(LINES - 1, 0);
  284. printw("%s\n", msg);
  285. }
  286. /* Display warning as a message */
  287. void
  288. printwarn(void)
  289. {
  290. printmsg(strerror(errno));
  291. }
  292. /* Kill curses and display error before exiting */
  293. void
  294. printerr(int ret, char *prefix)
  295. {
  296. exitcurses();
  297. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  298. exit(ret);
  299. }
  300. /* Clear the last line */
  301. void
  302. clearprompt(void)
  303. {
  304. printmsg("");
  305. }
  306. /* Print prompt on the last line */
  307. void
  308. printprompt(char *str)
  309. {
  310. clearprompt();
  311. printw(str);
  312. }
  313. /* Returns SEL_* if key is bound and 0 otherwise.
  314. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  315. int
  316. nextsel(char **run, char **env)
  317. {
  318. int c, i;
  319. c = getch();
  320. if (c == -1)
  321. idle++;
  322. else
  323. idle = 0;
  324. for (i = 0; i < LEN(bindings); i++)
  325. if (c == bindings[i].sym) {
  326. *run = bindings[i].run;
  327. *env = bindings[i].env;
  328. return bindings[i].act;
  329. }
  330. return 0;
  331. }
  332. char *
  333. readln(void)
  334. {
  335. static char ln[LINE_MAX];
  336. timeout(-1);
  337. echo();
  338. curs_set(TRUE);
  339. memset(ln, 0, sizeof(ln));
  340. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  341. noecho();
  342. curs_set(FALSE);
  343. timeout(1000);
  344. return ln[0] ? ln : NULL;
  345. }
  346. int
  347. canopendir(char *path)
  348. {
  349. DIR *dirp;
  350. dirp = opendir(path);
  351. if (dirp == NULL)
  352. return 0;
  353. closedir(dirp);
  354. return 1;
  355. }
  356. char *
  357. mkpath(char *dir, char *name, char *out, size_t n)
  358. {
  359. /* Handle absolute path */
  360. if (name[0] == '/')
  361. strlcpy(out, name, n);
  362. else {
  363. /* Handle root case */
  364. if (strcmp(dir, "/") == 0)
  365. snprintf(out, n, "/%s", name);
  366. else
  367. snprintf(out, n, "%s/%s", dir, name);
  368. }
  369. return out;
  370. }
  371. void
  372. printent(struct entry *ent, int active)
  373. {
  374. if (S_ISDIR(ent->mode))
  375. printw("%s%s/\n", active ? CURSR : EMPTY, ent->name);
  376. else if (S_ISLNK(ent->mode))
  377. printw("%s%s@\n", active ? CURSR : EMPTY, ent->name);
  378. else if (S_ISSOCK(ent->mode))
  379. printw("%s%s=\n", active ? CURSR : EMPTY, ent->name);
  380. else if (S_ISFIFO(ent->mode))
  381. printw("%s%s|\n", active ? CURSR : EMPTY, ent->name);
  382. else if (ent->mode & S_IXUSR)
  383. printw("%s%s*\n", active ? CURSR : EMPTY, ent->name);
  384. else
  385. printw("%s%s\n", active ? CURSR : EMPTY, ent->name);
  386. }
  387. void (*printptr)(struct entry *ent, int active) = &printent;
  388. char*
  389. coolsize(off_t size)
  390. {
  391. int i = 0;
  392. long double fsize = (double)size;
  393. while (fsize > 1024) {
  394. fsize /= 1024;
  395. i++;
  396. }
  397. snprintf(size_buf, 12, "%.*Lf%s", i, fsize, size_units[i]);
  398. return size_buf;
  399. }
  400. void
  401. printent_long(struct entry *ent, int active)
  402. {
  403. static char buf[18];
  404. const static struct tm *p;
  405. p = localtime(&ent->t);
  406. strftime(buf, 18, "%b %d %H:%M %Y", p);
  407. if (active)
  408. attron(A_REVERSE);
  409. if (S_ISDIR(ent->mode))
  410. printw("%s%-17.17s %s/\n", cur(active), buf, ent->name);
  411. else if (S_ISLNK(ent->mode))
  412. printw("%s%-17.17s %s@\n", cur(active), buf, ent->name);
  413. else if (S_ISSOCK(ent->mode))
  414. printw("%s%-17.17s %s=\n", cur(active), buf, ent->name);
  415. else if (S_ISFIFO(ent->mode))
  416. printw("%s%-17.17s %s|\n", cur(active), buf, ent->name);
  417. else if (S_ISBLK(ent->mode))
  418. printw("%s%-17.17s b %s\n", cur(active), buf, ent->name);
  419. else if (S_ISCHR(ent->mode))
  420. printw("%s%-17.17s c %s\n", cur(active), buf, ent->name);
  421. else if (ent->mode & S_IXUSR)
  422. printw("%s%-17.17s %8.8s %s*\n", cur(active), buf, coolsize(ent->size), ent->name);
  423. else
  424. printw("%s%-17.17s %8.8s %s\n", cur(active), buf, coolsize(ent->size), ent->name);
  425. if (active)
  426. attroff(A_REVERSE);
  427. }
  428. int
  429. dentfill(char *path, struct entry **dents,
  430. int (*filter)(regex_t *, char *), regex_t *re)
  431. {
  432. char newpath[PATH_MAX];
  433. DIR *dirp;
  434. struct dirent *dp;
  435. struct stat sb;
  436. int r, n = 0;
  437. dirp = opendir(path);
  438. if (dirp == NULL)
  439. return 0;
  440. while ((dp = readdir(dirp)) != NULL) {
  441. /* Skip self and parent */
  442. if (strcmp(dp->d_name, ".") == 0 ||
  443. strcmp(dp->d_name, "..") == 0)
  444. continue;
  445. if (filter(re, dp->d_name) == 0)
  446. continue;
  447. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  448. strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  449. /* Get mode flags */
  450. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  451. r = lstat(newpath, &sb);
  452. if (r == -1)
  453. printerr(1, "lstat");
  454. (*dents)[n].mode = sb.st_mode;
  455. (*dents)[n].t = sb.st_mtime;
  456. (*dents)[n].size = sb.st_size;
  457. n++;
  458. }
  459. /* Should never be null */
  460. r = closedir(dirp);
  461. if (r == -1)
  462. printerr(1, "closedir");
  463. return n;
  464. }
  465. void
  466. dentfree(struct entry *dents)
  467. {
  468. free(dents);
  469. }
  470. /* Return the position of the matching entry or 0 otherwise */
  471. int
  472. dentfind(struct entry *dents, int n, char *cwd, char *path)
  473. {
  474. char tmp[PATH_MAX];
  475. int i;
  476. if (path == NULL)
  477. return 0;
  478. for (i = 0; i < n; i++) {
  479. mkpath(cwd, dents[i].name, tmp, sizeof(tmp));
  480. DPRINTF_S(path);
  481. DPRINTF_S(tmp);
  482. if (strcmp(tmp, path) == 0)
  483. return i;
  484. }
  485. return 0;
  486. }
  487. int
  488. populate(char *path, char *oldpath, char *fltr)
  489. {
  490. regex_t re;
  491. int r;
  492. /* Can fail when permissions change while browsing */
  493. if (canopendir(path) == 0)
  494. return -1;
  495. /* Search filter */
  496. r = setfilter(&re, fltr);
  497. if (r != 0)
  498. return -1;
  499. dentfree(dents);
  500. ndents = 0;
  501. dents = NULL;
  502. ndents = dentfill(path, &dents, visible, &re);
  503. qsort(dents, ndents, sizeof(*dents), entrycmp);
  504. /* Find cur from history */
  505. cur = dentfind(dents, ndents, path, oldpath);
  506. return 0;
  507. }
  508. void
  509. redraw(char *path)
  510. {
  511. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  512. size_t ncols;
  513. int nlines, odd;
  514. int i;
  515. nlines = MIN(LINES - 4, ndents);
  516. /* Clean screen */
  517. erase();
  518. /* Strip trailing slashes */
  519. for (i = strlen(path) - 1; i > 0; i--)
  520. if (path[i] == '/')
  521. path[i] = '\0';
  522. else
  523. break;
  524. DPRINTF_D(cur);
  525. DPRINTF_S(path);
  526. /* No text wrapping in cwd line */
  527. ncols = COLS;
  528. if (ncols > PATH_MAX)
  529. ncols = PATH_MAX;
  530. strlcpy(cwd, path, ncols);
  531. cwd[ncols - strlen(CWD) - 1] = '\0';
  532. if (!realpath(path, cwdresolved)) {
  533. printmsg("Cannot resolve path");
  534. return;
  535. }
  536. printw(CWD "%s\n\n", cwdresolved);
  537. /* Print listing */
  538. odd = ISODD(nlines);
  539. if (cur < (nlines >> 1)) {
  540. for (i = 0; i < nlines; i++)
  541. printptr(&dents[i], i == cur);
  542. } else if (cur >= ndents - (nlines >> 1)) {
  543. for (i = ndents - nlines; i < ndents; i++)
  544. printptr(&dents[i], i == cur);
  545. } else {
  546. nlines >>= 1;
  547. for (i = cur - nlines; i < cur + nlines + odd; i++)
  548. printptr(&dents[i], i == cur);
  549. }
  550. if (showdetail) {
  551. if (ndents) {
  552. static char ind;
  553. ind = '\0';
  554. if (S_ISDIR(dents[cur].mode))
  555. ind = '/';
  556. else if (S_ISLNK(dents[cur].mode))
  557. ind = '@';
  558. else if (S_ISSOCK(dents[cur].mode))
  559. ind = '=';
  560. else if (S_ISFIFO(dents[cur].mode))
  561. ind = '|';
  562. else if (dents[cur].mode & S_IXUSR)
  563. ind = '*';
  564. ind
  565. ? sprintf(cwd, "%d items [%s%c]", ndents, dents[cur].name, ind)
  566. : sprintf(cwd, "%d items [%s]", ndents, dents[cur].name);
  567. printmsg(cwd);
  568. } else
  569. printmsg("0 items");
  570. }
  571. }
  572. void
  573. browse(char *ipath, char *ifilter)
  574. {
  575. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  576. char fltr[LINE_MAX];
  577. char *bin, *dir, *tmp, *run, *env;
  578. struct stat sb;
  579. regex_t re;
  580. int r, fd;
  581. strlcpy(path, ipath, sizeof(path));
  582. strlcpy(fltr, ifilter, sizeof(fltr));
  583. oldpath[0] = '\0';
  584. begin:
  585. r = populate(path, oldpath, fltr);
  586. if (r == -1) {
  587. printwarn();
  588. goto nochange;
  589. }
  590. for (;;) {
  591. redraw(path);
  592. nochange:
  593. switch (nextsel(&run, &env)) {
  594. case SEL_QUIT:
  595. dentfree(dents);
  596. return;
  597. case SEL_BACK:
  598. /* There is no going back */
  599. if (strcmp(path, "/") == 0 ||
  600. strcmp(path, ".") == 0 ||
  601. strchr(path, '/') == NULL)
  602. goto nochange;
  603. dir = xdirname(path);
  604. if (canopendir(dir) == 0) {
  605. printwarn();
  606. goto nochange;
  607. }
  608. /* Save history */
  609. strlcpy(oldpath, path, sizeof(oldpath));
  610. strlcpy(path, dir, sizeof(path));
  611. /* Reset filter */
  612. strlcpy(fltr, ifilter, sizeof(fltr));
  613. goto begin;
  614. case SEL_GOIN:
  615. /* Cannot descend in empty directories */
  616. if (ndents == 0)
  617. goto nochange;
  618. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  619. DPRINTF_S(newpath);
  620. /* Get path info */
  621. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  622. if (fd == -1) {
  623. printwarn();
  624. goto nochange;
  625. }
  626. r = fstat(fd, &sb);
  627. if (r == -1) {
  628. printwarn();
  629. close(fd);
  630. goto nochange;
  631. }
  632. close(fd);
  633. DPRINTF_U(sb.st_mode);
  634. switch (sb.st_mode & S_IFMT) {
  635. case S_IFDIR:
  636. if (canopendir(newpath) == 0) {
  637. printwarn();
  638. goto nochange;
  639. }
  640. strlcpy(path, newpath, sizeof(path));
  641. /* Reset filter */
  642. strlcpy(fltr, ifilter, sizeof(fltr));
  643. goto begin;
  644. case S_IFREG:
  645. /* If default mime opener is set, use it */
  646. if (opener) {
  647. char cmd[MAX_LEN];
  648. int status;
  649. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  650. opener, newpath);
  651. status = system(cmd);
  652. continue;
  653. }
  654. /* Try custom applications */
  655. bin = openwith(newpath);
  656. char *execvi = "vi";
  657. if (bin == NULL) {
  658. /* If a custom handler application is not set, open
  659. plain text files with vi, then try fallback_opener */
  660. FILE *fp;
  661. char cmd[MAX_LEN];
  662. int status;
  663. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  664. fp = popen(cmd, "r");
  665. if (fp == NULL)
  666. goto nochange;
  667. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  668. pclose(fp);
  669. goto nochange;
  670. }
  671. pclose(fp);
  672. if (strstr(cmd, "ASCII text") != NULL)
  673. bin = execvi;
  674. else if (fallback_opener) {
  675. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  676. fallback_opener, newpath);
  677. status = system(cmd);
  678. continue;
  679. } else {
  680. printmsg("No association");
  681. goto nochange;
  682. }
  683. }
  684. exitcurses();
  685. spawn(bin, newpath, NULL);
  686. initcurses();
  687. continue;
  688. default:
  689. printmsg("Unsupported file");
  690. goto nochange;
  691. }
  692. case SEL_FLTR:
  693. /* Read filter */
  694. printprompt("filter: ");
  695. tmp = readln();
  696. if (tmp == NULL)
  697. tmp = ifilter;
  698. /* Check and report regex errors */
  699. r = setfilter(&re, tmp);
  700. if (r != 0)
  701. goto nochange;
  702. strlcpy(fltr, tmp, sizeof(fltr));
  703. DPRINTF_S(fltr);
  704. /* Save current */
  705. if (ndents > 0)
  706. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  707. goto begin;
  708. case SEL_NEXT:
  709. if (cur < ndents - 1)
  710. cur++;
  711. else if (ndents)
  712. /* Roll over, set cursor to first entry */
  713. cur = 0;
  714. break;
  715. case SEL_PREV:
  716. if (cur > 0)
  717. cur--;
  718. else if (ndents)
  719. /* Roll over, set cursor to last entry */
  720. cur = ndents - 1;
  721. break;
  722. case SEL_PGDN:
  723. if (cur < ndents - 1)
  724. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  725. break;
  726. case SEL_PGUP:
  727. if (cur > 0)
  728. cur -= MIN((LINES - 4) / 2, cur);
  729. break;
  730. case SEL_HOME:
  731. cur = 0;
  732. break;
  733. case SEL_END:
  734. cur = ndents - 1;
  735. break;
  736. case SEL_CD:
  737. /* Read target dir */
  738. printprompt("chdir: ");
  739. tmp = readln();
  740. if (tmp == NULL) {
  741. clearprompt();
  742. goto nochange;
  743. }
  744. mkpath(path, tmp, newpath, sizeof(newpath));
  745. if (canopendir(newpath) == 0) {
  746. printwarn();
  747. goto nochange;
  748. }
  749. strlcpy(path, newpath, sizeof(path));
  750. /* Reset filter */
  751. strlcpy(fltr, ifilter, sizeof(fltr))
  752. DPRINTF_S(path);
  753. goto begin;
  754. case SEL_CDHOME:
  755. tmp = getenv("HOME");
  756. if (tmp == NULL) {
  757. clearprompt();
  758. goto nochange;
  759. }
  760. if (canopendir(tmp) == 0) {
  761. printwarn();
  762. goto nochange;
  763. }
  764. strlcpy(path, tmp, sizeof(path));
  765. /* Reset filter */
  766. strlcpy(fltr, ifilter, sizeof(fltr));
  767. DPRINTF_S(path);
  768. goto begin;
  769. case SEL_TOGGLEDOT:
  770. showhidden ^= 1;
  771. initfilter(showhidden, &ifilter);
  772. strlcpy(fltr, ifilter, sizeof(fltr));
  773. goto begin;
  774. case SEL_DETAIL:
  775. showdetail = !showdetail;
  776. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  777. /* Save current */
  778. if (ndents > 0)
  779. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  780. goto begin;
  781. case SEL_FSIZE:
  782. sizeorder = !sizeorder;
  783. mtimeorder = 0;
  784. /* Save current */
  785. if (ndents > 0)
  786. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  787. goto begin;
  788. case SEL_MTIME:
  789. mtimeorder = !mtimeorder;
  790. sizeorder = 0;
  791. /* Save current */
  792. if (ndents > 0)
  793. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  794. goto begin;
  795. case SEL_REDRAW:
  796. /* Save current */
  797. if (ndents > 0)
  798. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  799. goto begin;
  800. case SEL_COPY:
  801. if (copier && ndents) {
  802. char abspath[PATH_MAX];
  803. if (strcmp(path, "/") == 0)
  804. snprintf(abspath, PATH_MAX, "/%s", dents[cur].name);
  805. else
  806. snprintf(abspath, PATH_MAX, "%s/%s", path, dents[cur].name);
  807. spawn(copier, abspath, NULL);
  808. printmsg(abspath);
  809. } else if (!copier)
  810. printmsg("NNN_COPIER is not set");
  811. goto nochange;
  812. case SEL_RUN:
  813. run = xgetenv(env, run);
  814. exitcurses();
  815. spawn(run, NULL, path);
  816. initcurses();
  817. /* Re-populate as directory content may have changed */
  818. goto begin;
  819. case SEL_RUNARG:
  820. run = xgetenv(env, run);
  821. exitcurses();
  822. spawn(run, dents[cur].name, path);
  823. initcurses();
  824. break;
  825. }
  826. /* Screensaver */
  827. if (idletimeout != 0 && idle == idletimeout) {
  828. idle = 0;
  829. exitcurses();
  830. spawn(idlecmd, NULL, NULL);
  831. initcurses();
  832. }
  833. }
  834. }
  835. void
  836. usage(void)
  837. {
  838. fprintf(stderr, "usage: nnn [-d] [dir]\n");
  839. exit(1);
  840. }
  841. int
  842. main(int argc, char *argv[])
  843. {
  844. char cwd[PATH_MAX], *ipath;
  845. char *ifilter;
  846. int opt = 0;
  847. /* Confirm we are in a terminal */
  848. if (!isatty(0) || !isatty(1)) {
  849. fprintf(stderr, "stdin or stdout is not a tty\n");
  850. exit(1);
  851. }
  852. if (argc > 3)
  853. usage();
  854. while ((opt = getopt(argc, argv, "d")) != -1) {
  855. switch (opt) {
  856. case 'd':
  857. /* Open in detail mode, if set */
  858. showdetail = 1;
  859. printptr = &printent_long;
  860. break;
  861. default:
  862. usage();
  863. }
  864. }
  865. if (argc == optind) {
  866. /* Start in the current directory */
  867. ipath = getcwd(cwd, sizeof(cwd));
  868. if (ipath == NULL)
  869. ipath = "/";
  870. } else {
  871. ipath = realpath(argv[optind], cwd);
  872. if (!ipath) {
  873. fprintf(stderr, "%s: no such dir\n", argv[optind]);
  874. exit(1);
  875. }
  876. }
  877. if (getuid() == 0)
  878. showhidden = 1;
  879. initfilter(showhidden, &ifilter);
  880. /* Get the default desktop mime opener, if set */
  881. opener = getenv("NNN_OPENER");
  882. /* Get the fallback desktop mime opener, if set */
  883. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  884. /* Get the default copier, if set */
  885. copier = getenv("NNN_COPIER");
  886. signal(SIGINT, SIG_IGN);
  887. /* Test initial path */
  888. if (canopendir(ipath) == 0) {
  889. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  890. exit(1);
  891. }
  892. /* Set locale before curses setup */
  893. setlocale(LC_ALL, "");
  894. initcurses();
  895. browse(ipath, ifilter);
  896. exitcurses();
  897. exit(0);
  898. }