My build of nnn with minor changes
 
 
 
 
 
 

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