My build of nnn with minor changes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1007 linhas
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 (*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. static char name[PATH_MAX + 2];
  406. p = localtime(&ent->t);
  407. strftime(buf, 18, "%b %d %H:%M %Y", p);
  408. if (active)
  409. attron(A_REVERSE);
  410. if (S_ISDIR(ent->mode)) {
  411. sprintf(name, "%s/", ent->name);
  412. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  413. } else if (S_ISLNK(ent->mode)) {
  414. sprintf(name, "%s@", ent->name);
  415. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  416. } else if (S_ISSOCK(ent->mode)) {
  417. sprintf(name, "%s=", ent->name);
  418. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  419. } else if (S_ISFIFO(ent->mode)) {
  420. sprintf(name, "%s|", ent->name);
  421. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  422. } else if (S_ISBLK(ent->mode))
  423. printw("%s%-32.32s b %-18.18s\n", cur(active), ent->name, buf);
  424. else if (S_ISCHR(ent->mode))
  425. printw("%s%-32.32s c %-18.18s\n", cur(active), ent->name, buf);
  426. else if (ent->mode & S_IXUSR) {
  427. sprintf(name, "%s*", ent->name);
  428. printw("%s%-32.32s %-18.18s %s\n", cur(active), name,
  429. buf, coolsize(ent->size));
  430. } else
  431. printw("%s%-32.32s %-18.18s %s\n", cur(active), ent->name,
  432. buf, coolsize(ent->size));
  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. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  520. size_t ncols;
  521. int nlines, odd;
  522. int i;
  523. nlines = MIN(LINES - 4, ndents);
  524. /* Clean screen */
  525. erase();
  526. /* Strip trailing slashes */
  527. for (i = strlen(path) - 1; i > 0; i--)
  528. if (path[i] == '/')
  529. path[i] = '\0';
  530. else
  531. break;
  532. DPRINTF_D(cur);
  533. DPRINTF_S(path);
  534. /* No text wrapping in cwd line */
  535. ncols = COLS;
  536. if (ncols > PATH_MAX)
  537. ncols = PATH_MAX;
  538. strlcpy(cwd, path, ncols);
  539. cwd[ncols - strlen(CWD) - 1] = '\0';
  540. if (!realpath(path, cwdresolved)) {
  541. printmsg("Cannot resolve path");
  542. return;
  543. }
  544. printw(CWD "%s\n\n", cwdresolved);
  545. /* Print listing */
  546. odd = ISODD(nlines);
  547. if (cur < (nlines >> 1)) {
  548. for (i = 0; i < nlines; i++)
  549. printptr(&dents[i], i == cur);
  550. } else if (cur >= ndents - (nlines >> 1)) {
  551. for (i = ndents - nlines; i < ndents; i++)
  552. printptr(&dents[i], i == cur);
  553. } else {
  554. nlines >>= 1;
  555. for (i = cur - nlines; i < cur + nlines + odd; i++)
  556. printptr(&dents[i], i == cur);
  557. }
  558. if (showdetail) {
  559. if (ndents) {
  560. static char ind;
  561. ind = '\0';
  562. if (S_ISDIR(dents[cur].mode))
  563. ind = '/';
  564. else if (S_ISLNK(dents[cur].mode))
  565. ind = '@';
  566. else if (S_ISSOCK(dents[cur].mode))
  567. ind = '=';
  568. else if (S_ISFIFO(dents[cur].mode))
  569. ind = '|';
  570. else if (dents[cur].mode & S_IXUSR)
  571. ind = '*';
  572. ind
  573. ? sprintf(cwd, "%d items [%s%c]", ndents, dents[cur].name, ind)
  574. : sprintf(cwd, "%d items [%s]", ndents, dents[cur].name);
  575. printmsg(cwd);
  576. } else
  577. printmsg("0 items");
  578. }
  579. }
  580. void
  581. browse(char *ipath, char *ifilter)
  582. {
  583. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  584. char fltr[LINE_MAX];
  585. char *bin, *dir, *tmp, *run, *env;
  586. struct stat sb;
  587. regex_t re;
  588. int r, fd;
  589. strlcpy(path, ipath, sizeof(path));
  590. strlcpy(fltr, ifilter, sizeof(fltr));
  591. oldpath[0] = '\0';
  592. begin:
  593. r = populate(path, oldpath, fltr);
  594. if (r == -1) {
  595. printwarn();
  596. goto nochange;
  597. }
  598. for (;;) {
  599. redraw(path);
  600. nochange:
  601. switch (nextsel(&run, &env)) {
  602. case SEL_QUIT:
  603. dentfree(dents);
  604. return;
  605. case SEL_BACK:
  606. /* There is no going back */
  607. if (strcmp(path, "/") == 0 ||
  608. strcmp(path, ".") == 0 ||
  609. strchr(path, '/') == NULL)
  610. goto nochange;
  611. dir = xdirname(path);
  612. if (canopendir(dir) == 0) {
  613. printwarn();
  614. goto nochange;
  615. }
  616. /* Save history */
  617. strlcpy(oldpath, path, sizeof(oldpath));
  618. strlcpy(path, dir, sizeof(path));
  619. /* Reset filter */
  620. strlcpy(fltr, ifilter, sizeof(fltr));
  621. goto begin;
  622. case SEL_GOIN:
  623. /* Cannot descend in empty directories */
  624. if (ndents == 0)
  625. goto nochange;
  626. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  627. DPRINTF_S(newpath);
  628. /* Get path info */
  629. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  630. if (fd == -1) {
  631. printwarn();
  632. goto nochange;
  633. }
  634. r = fstat(fd, &sb);
  635. if (r == -1) {
  636. printwarn();
  637. close(fd);
  638. goto nochange;
  639. }
  640. close(fd);
  641. DPRINTF_U(sb.st_mode);
  642. switch (sb.st_mode & S_IFMT) {
  643. case S_IFDIR:
  644. if (canopendir(newpath) == 0) {
  645. printwarn();
  646. goto nochange;
  647. }
  648. strlcpy(path, newpath, sizeof(path));
  649. /* Reset filter */
  650. strlcpy(fltr, ifilter, sizeof(fltr));
  651. goto begin;
  652. case S_IFREG:
  653. /* If default mime opener is set, use it */
  654. if (opener) {
  655. char cmd[MAX_LEN];
  656. int status;
  657. snprintf(cmd, MAX_LEN, "%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. char *execvim = "vim";
  665. if (bin == NULL) {
  666. /* If a custom handler application is not set, open
  667. plain text files with vim, then try fallback_opener */
  668. FILE *fp;
  669. char cmd[MAX_LEN];
  670. int status;
  671. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  672. fp = popen(cmd, "r");
  673. if (fp == NULL)
  674. goto nochange;
  675. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  676. pclose(fp);
  677. goto nochange;
  678. }
  679. pclose(fp);
  680. if (strstr(cmd, "ASCII text") != NULL)
  681. bin = execvim;
  682. else if (fallback_opener) {
  683. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  684. fallback_opener, newpath);
  685. status = system(cmd);
  686. continue;
  687. } else {
  688. printmsg("No association");
  689. goto nochange;
  690. }
  691. }
  692. exitcurses();
  693. spawn(bin, newpath, NULL);
  694. initcurses();
  695. continue;
  696. default:
  697. printmsg("Unsupported file");
  698. goto nochange;
  699. }
  700. case SEL_FLTR:
  701. /* Read filter */
  702. printprompt("filter: ");
  703. tmp = readln();
  704. if (tmp == NULL)
  705. tmp = ifilter;
  706. /* Check and report regex errors */
  707. r = setfilter(&re, tmp);
  708. if (r != 0)
  709. goto nochange;
  710. strlcpy(fltr, tmp, sizeof(fltr));
  711. DPRINTF_S(fltr);
  712. /* Save current */
  713. if (ndents > 0)
  714. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  715. goto begin;
  716. case SEL_NEXT:
  717. if (cur < ndents - 1)
  718. cur++;
  719. else if (ndents)
  720. /* Roll over, set cursor to first entry */
  721. cur = 0;
  722. break;
  723. case SEL_PREV:
  724. if (cur > 0)
  725. cur--;
  726. else if (ndents)
  727. /* Roll over, set cursor to last entry */
  728. cur = ndents - 1;
  729. break;
  730. case SEL_PGDN:
  731. if (cur < ndents - 1)
  732. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  733. break;
  734. case SEL_PGUP:
  735. if (cur > 0)
  736. cur -= MIN((LINES - 4) / 2, cur);
  737. break;
  738. case SEL_HOME:
  739. cur = 0;
  740. break;
  741. case SEL_END:
  742. cur = ndents - 1;
  743. break;
  744. case SEL_CD:
  745. /* Read target dir */
  746. printprompt("chdir: ");
  747. tmp = readln();
  748. if (tmp == NULL) {
  749. clearprompt();
  750. goto nochange;
  751. }
  752. mkpath(path, tmp, newpath, sizeof(newpath));
  753. if (canopendir(newpath) == 0) {
  754. printwarn();
  755. goto nochange;
  756. }
  757. strlcpy(path, newpath, sizeof(path));
  758. /* Reset filter */
  759. strlcpy(fltr, ifilter, sizeof(fltr))
  760. DPRINTF_S(path);
  761. goto begin;
  762. case SEL_CDHOME:
  763. tmp = getenv("HOME");
  764. if (tmp == NULL) {
  765. clearprompt();
  766. goto nochange;
  767. }
  768. if (canopendir(tmp) == 0) {
  769. printwarn();
  770. goto nochange;
  771. }
  772. strlcpy(path, tmp, sizeof(path));
  773. /* Reset filter */
  774. strlcpy(fltr, ifilter, sizeof(fltr));
  775. DPRINTF_S(path);
  776. goto begin;
  777. case SEL_TOGGLEDOT:
  778. showhidden ^= 1;
  779. initfilter(showhidden, &ifilter);
  780. strlcpy(fltr, ifilter, sizeof(fltr));
  781. goto begin;
  782. case SEL_DETAIL:
  783. showdetail = !showdetail;
  784. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  785. /* Save current */
  786. if (ndents > 0)
  787. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  788. goto begin;
  789. case SEL_FSIZE:
  790. sizeorder = !sizeorder;
  791. mtimeorder = 0;
  792. /* Save current */
  793. if (ndents > 0)
  794. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  795. goto begin;
  796. case SEL_MTIME:
  797. mtimeorder = !mtimeorder;
  798. sizeorder = 0;
  799. /* Save current */
  800. if (ndents > 0)
  801. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  802. goto begin;
  803. case SEL_REDRAW:
  804. /* Save current */
  805. if (ndents > 0)
  806. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  807. goto begin;
  808. case SEL_COPY:
  809. if (copier && ndents) {
  810. char abspath[PATH_MAX];
  811. if (strcmp(path, "/") == 0)
  812. snprintf(abspath, PATH_MAX, "/%s", dents[cur].name);
  813. else
  814. snprintf(abspath, PATH_MAX, "%s/%s", path, dents[cur].name);
  815. spawn(copier, abspath, NULL);
  816. printmsg(abspath);
  817. } else if (!copier)
  818. printmsg("NNN_COPIER is not set");
  819. goto nochange;
  820. case SEL_RUN:
  821. run = xgetenv(env, run);
  822. exitcurses();
  823. spawn(run, NULL, path);
  824. initcurses();
  825. /* Re-populate as directory content may have changed */
  826. goto begin;
  827. case SEL_RUNARG:
  828. run = xgetenv(env, run);
  829. exitcurses();
  830. spawn(run, dents[cur].name, path);
  831. initcurses();
  832. break;
  833. }
  834. /* Screensaver */
  835. if (idletimeout != 0 && idle == idletimeout) {
  836. idle = 0;
  837. exitcurses();
  838. spawn(idlecmd, NULL, NULL);
  839. initcurses();
  840. }
  841. }
  842. }
  843. void
  844. usage(char *argv0)
  845. {
  846. fprintf(stderr, "usage: %s [dir]\n", argv0);
  847. exit(1);
  848. }
  849. int
  850. main(int argc, char *argv[])
  851. {
  852. char cwd[PATH_MAX], *ipath;
  853. char *ifilter;
  854. if (argc > 2)
  855. usage(argv[0]);
  856. /* Confirm we are in a terminal */
  857. if (!isatty(0) || !isatty(1)) {
  858. fprintf(stderr, "stdin or stdout is not a tty\n");
  859. exit(1);
  860. }
  861. if (getuid() == 0)
  862. showhidden = 1;
  863. initfilter(showhidden, &ifilter);
  864. printptr = &printent;
  865. if (argv[1] != NULL) {
  866. ipath = realpath(argv[1], cwd);
  867. if (!ipath) {
  868. fprintf(stderr, "%s: no such dir\n", argv[1]);
  869. exit(1);
  870. }
  871. } else {
  872. ipath = getcwd(cwd, sizeof(cwd));
  873. if (ipath == NULL)
  874. ipath = "/";
  875. }
  876. /* Get the default desktop mime opener, if set */
  877. opener = getenv("NNN_OPENER");
  878. /* Get the fallback desktop mime opener, if set */
  879. fallback_opener = getenv("NNN_FALLBACK_OPENER");
  880. /* Get the default copier, if set */
  881. copier = getenv("NNN_COPIER");
  882. signal(SIGINT, SIG_IGN);
  883. /* Test initial path */
  884. if (canopendir(ipath) == 0) {
  885. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  886. exit(1);
  887. }
  888. /* Set locale before curses setup */
  889. setlocale(LC_ALL, "");
  890. initcurses();
  891. browse(ipath, ifilter);
  892. exitcurses();
  893. exit(0);
  894. }