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.
 
 
 
 
 
 

999 lines
19 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 (*printptr)(struct entry *ent, int active);
  108. void printmsg(char *);
  109. void printwarn(void);
  110. void printerr(int, char *);
  111. #undef dprintf
  112. int
  113. dprintf(int fd, const char *fmt, ...)
  114. {
  115. char buf[BUFSIZ];
  116. int r;
  117. va_list ap;
  118. va_start(ap, fmt);
  119. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  120. if (r > 0)
  121. r = write(fd, buf, r);
  122. va_end(ap);
  123. return r;
  124. }
  125. void *
  126. xmalloc(size_t size)
  127. {
  128. void *p;
  129. p = malloc(size);
  130. if (p == NULL)
  131. printerr(1, "malloc");
  132. return p;
  133. }
  134. void *
  135. xrealloc(void *p, size_t size)
  136. {
  137. p = realloc(p, size);
  138. if (p == NULL)
  139. printerr(1, "realloc");
  140. return p;
  141. }
  142. char *
  143. xstrdup(const char *s)
  144. {
  145. char *p;
  146. p = strdup(s);
  147. if (p == NULL)
  148. printerr(1, "strdup");
  149. return p;
  150. }
  151. /* Some implementations of dirname(3) may modify `path' and some
  152. * return a pointer inside `path'. */
  153. char *
  154. xdirname(const char *path)
  155. {
  156. static char out[PATH_MAX];
  157. char tmp[PATH_MAX], *p;
  158. strlcpy(tmp, path, sizeof(tmp));
  159. p = dirname(tmp);
  160. if (p == NULL)
  161. printerr(1, "dirname");
  162. strlcpy(out, p, sizeof(out));
  163. return out;
  164. }
  165. void
  166. spawn(char *file, char *arg, char *dir)
  167. {
  168. pid_t pid;
  169. int status;
  170. pid = fork();
  171. if (pid == 0) {
  172. if (dir != NULL)
  173. status = chdir(dir);
  174. execlp(file, file, arg, NULL);
  175. _exit(1);
  176. } else {
  177. /* Ignore interruptions */
  178. while (waitpid(pid, &status, 0) == -1)
  179. DPRINTF_D(status);
  180. DPRINTF_D(pid);
  181. }
  182. }
  183. char *
  184. xgetenv(char *name, char *fallback)
  185. {
  186. char *value;
  187. if (name == NULL)
  188. return fallback;
  189. value = getenv(name);
  190. return value && value[0] ? value : fallback;
  191. }
  192. int
  193. xstricmp(const char *s1, const char *s2)
  194. {
  195. while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2))
  196. s1++, s2++;
  197. /* In case of alphabetically same names, make sure
  198. lower case one comes before upper case one */
  199. if (!*s1 && !*s2)
  200. return 1;
  201. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  202. }
  203. char *
  204. openwith(char *file)
  205. {
  206. regex_t regex;
  207. char *bin = NULL;
  208. int i;
  209. for (i = 0; i < LEN(assocs); i++) {
  210. if (regcomp(&regex, assocs[i].regex,
  211. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  212. continue;
  213. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  214. bin = assocs[i].bin;
  215. break;
  216. }
  217. }
  218. DPRINTF_S(bin);
  219. return bin;
  220. }
  221. int
  222. setfilter(regex_t *regex, char *filter)
  223. {
  224. char errbuf[LINE_MAX];
  225. size_t len;
  226. int r;
  227. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  228. if (r != 0) {
  229. len = COLS;
  230. if (len > sizeof(errbuf))
  231. len = sizeof(errbuf);
  232. regerror(r, regex, errbuf, len);
  233. printmsg(errbuf);
  234. }
  235. return r;
  236. }
  237. void
  238. initfilter(int dot, char **ifilter)
  239. {
  240. *ifilter = dot ? "." : "^[^.]";
  241. }
  242. int
  243. visible(regex_t *regex, char *file)
  244. {
  245. return regexec(regex, file, 0, NULL, 0) == 0;
  246. }
  247. int
  248. entrycmp(const void *va, const void *vb)
  249. {
  250. if (mtimeorder)
  251. return ((struct entry *)vb)->t - ((struct entry *)va)->t;
  252. if (sizeorder)
  253. return ((struct entry *)vb)->size - ((struct entry *)va)->size;
  254. return xstricmp(((struct entry *)va)->name, ((struct entry *)vb)->name);
  255. }
  256. void
  257. initcurses(void)
  258. {
  259. if (initscr() == NULL) {
  260. char *term = getenv("TERM");
  261. if (term != NULL)
  262. fprintf(stderr, "error opening terminal: %s\n", term);
  263. else
  264. fprintf(stderr, "failed to initialize curses\n");
  265. exit(1);
  266. }
  267. cbreak();
  268. noecho();
  269. nonl();
  270. intrflush(stdscr, FALSE);
  271. keypad(stdscr, TRUE);
  272. curs_set(FALSE); /* Hide cursor */
  273. timeout(1000); /* One second */
  274. }
  275. void
  276. exitcurses(void)
  277. {
  278. endwin(); /* Restore terminal */
  279. }
  280. /* Messages show up at the bottom */
  281. void
  282. printmsg(char *msg)
  283. {
  284. move(LINES - 1, 0);
  285. printw("%s\n", msg);
  286. }
  287. /* Display warning as a message */
  288. void
  289. printwarn(void)
  290. {
  291. printmsg(strerror(errno));
  292. }
  293. /* Kill curses and display error before exiting */
  294. void
  295. printerr(int ret, char *prefix)
  296. {
  297. exitcurses();
  298. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  299. exit(ret);
  300. }
  301. /* Clear the last line */
  302. void
  303. clearprompt(void)
  304. {
  305. printmsg("");
  306. }
  307. /* Print prompt on the last line */
  308. void
  309. printprompt(char *str)
  310. {
  311. clearprompt();
  312. printw(str);
  313. }
  314. /* Returns SEL_* if key is bound and 0 otherwise.
  315. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  316. int
  317. nextsel(char **run, char **env)
  318. {
  319. int c, i;
  320. c = getch();
  321. if (c == -1)
  322. idle++;
  323. else
  324. idle = 0;
  325. for (i = 0; i < LEN(bindings); i++)
  326. if (c == bindings[i].sym) {
  327. *run = bindings[i].run;
  328. *env = bindings[i].env;
  329. return bindings[i].act;
  330. }
  331. return 0;
  332. }
  333. char *
  334. readln(void)
  335. {
  336. static char ln[LINE_MAX];
  337. timeout(-1);
  338. echo();
  339. curs_set(TRUE);
  340. memset(ln, 0, sizeof(ln));
  341. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  342. noecho();
  343. curs_set(FALSE);
  344. timeout(1000);
  345. return ln[0] ? ln : NULL;
  346. }
  347. int
  348. canopendir(char *path)
  349. {
  350. DIR *dirp;
  351. dirp = opendir(path);
  352. if (dirp == NULL)
  353. return 0;
  354. closedir(dirp);
  355. return 1;
  356. }
  357. char *
  358. mkpath(char *dir, char *name, char *out, size_t n)
  359. {
  360. /* Handle absolute path */
  361. if (name[0] == '/')
  362. strlcpy(out, name, n);
  363. else {
  364. /* Handle root case */
  365. if (strcmp(dir, "/") == 0)
  366. snprintf(out, n, "/%s", name);
  367. else
  368. snprintf(out, n, "%s/%s", dir, name);
  369. }
  370. return out;
  371. }
  372. void
  373. printent(struct entry *ent, int active)
  374. {
  375. if (S_ISDIR(ent->mode))
  376. printw("%s%s/\n", active ? CURSR : EMPTY, ent->name);
  377. else if (S_ISLNK(ent->mode))
  378. printw("%s%s@\n", active ? CURSR : EMPTY, ent->name);
  379. else if (S_ISSOCK(ent->mode))
  380. printw("%s%s=\n", active ? CURSR : EMPTY, ent->name);
  381. else if (S_ISFIFO(ent->mode))
  382. printw("%s%s|\n", active ? CURSR : EMPTY, ent->name);
  383. else if (ent->mode & S_IXUSR)
  384. printw("%s%s*\n", active ? CURSR : EMPTY, ent->name);
  385. else
  386. printw("%s%s\n", active ? CURSR : EMPTY, ent->name);
  387. }
  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(char *argv0)
  837. {
  838. fprintf(stderr, "usage: %s [dir]\n", argv0);
  839. exit(1);
  840. }
  841. int
  842. main(int argc, char *argv[])
  843. {
  844. char cwd[PATH_MAX], *ipath;
  845. char *ifilter;
  846. if (argc > 2)
  847. usage(argv[0]);
  848. /* Confirm we are in a terminal */
  849. if (!isatty(0) || !isatty(1)) {
  850. fprintf(stderr, "stdin or stdout is not a tty\n");
  851. exit(1);
  852. }
  853. if (getuid() == 0)
  854. showhidden = 1;
  855. initfilter(showhidden, &ifilter);
  856. printptr = &printent;
  857. if (argv[1] != NULL) {
  858. ipath = realpath(argv[1], cwd);
  859. if (!ipath) {
  860. fprintf(stderr, "%s: no such dir\n", argv[1]);
  861. exit(1);
  862. }
  863. } else {
  864. ipath = getcwd(cwd, sizeof(cwd));
  865. if (ipath == NULL)
  866. ipath = "/";
  867. }
  868. /* Get the default desktop mime opener, if set */
  869. opener = getenv("NNN_OPENER");
  870. /* Get the fallback desktop mime opener, if set */
  871. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  872. /* Get the default copier, if set */
  873. copier = getenv("NNN_COPIER");
  874. signal(SIGINT, SIG_IGN);
  875. /* Test initial path */
  876. if (canopendir(ipath) == 0) {
  877. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  878. exit(1);
  879. }
  880. /* Set locale before curses setup */
  881. setlocale(LC_ALL, "");
  882. initcurses();
  883. browse(ipath, ifilter);
  884. exitcurses();
  885. exit(0);
  886. }