My build of nnn with minor changes
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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