My build of nnn with minor changes
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

968 Zeilen
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_RUN,
  66. SEL_RUNARG,
  67. };
  68. struct key {
  69. int sym; /* Key pressed */
  70. enum action act; /* Action */
  71. char *run; /* Program to run */
  72. char *env; /* Environment variable to run */
  73. };
  74. #include "config.h"
  75. struct entry {
  76. char name[PATH_MAX];
  77. mode_t mode;
  78. time_t t;
  79. off_t size;
  80. };
  81. /* Global context */
  82. struct entry *dents;
  83. int ndents, cur;
  84. int idle;
  85. char *opener = NULL;
  86. char *fallback_opener = NULL;
  87. char size_buf[12]; /* Buffer to hold human readable size */
  88. const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
  89. /*
  90. * Layout:
  91. * .---------
  92. * | cwd: /mnt/path
  93. * |
  94. * | file0
  95. * | file1
  96. * | > file2
  97. * | file3
  98. * | file4
  99. * ...
  100. * | filen
  101. * |
  102. * | Permission denied
  103. * '------
  104. */
  105. void (*printptr)(struct entry *ent, int active);
  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 ((struct entry *)vb)->t - ((struct entry *)va)->t;
  250. if (sizeorder)
  251. return ((struct entry *)vb)->size - ((struct entry *)va)->size;
  252. return xstricmp(((struct entry *)va)->name, ((struct entry *)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. char*
  387. coolsize(off_t size)
  388. {
  389. int i = 0;
  390. long double fsize = (double)size;
  391. while (fsize > 1024) {
  392. fsize /= 1024;
  393. i++;
  394. }
  395. snprintf(size_buf, 12, "%.*Lf%s", i, fsize, size_units[i]);
  396. return size_buf;
  397. }
  398. void
  399. printent_long(struct entry *ent, int active)
  400. {
  401. static char buf[18];
  402. const static struct tm *p;
  403. static char name[PATH_MAX + 2];
  404. p = localtime(&ent->t);
  405. strftime(buf, 18, "%b %d %H:%M %Y", p);
  406. if (active)
  407. attron(A_REVERSE);
  408. if (S_ISDIR(ent->mode)) {
  409. sprintf(name, "%s/", ent->name);
  410. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  411. } else if (S_ISLNK(ent->mode)) {
  412. sprintf(name, "%s@", ent->name);
  413. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  414. } else if (S_ISSOCK(ent->mode)) {
  415. sprintf(name, "%s=", ent->name);
  416. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  417. } else if (S_ISFIFO(ent->mode)) {
  418. sprintf(name, "%s|", ent->name);
  419. printw("%s%-32.32s %-18.18s\n", cur(active), name, buf);
  420. } else if (S_ISBLK(ent->mode))
  421. printw("%s%-32.32s b %-18.18s\n", cur(active), ent->name, buf);
  422. else if (S_ISCHR(ent->mode))
  423. printw("%s%-32.32s c %-18.18s\n", cur(active), ent->name, buf);
  424. else if (ent->mode & S_IXUSR) {
  425. sprintf(name, "%s*", ent->name);
  426. printw("%s%-32.32s %-18.18s %s\n", cur(active), name,
  427. buf, coolsize(ent->size));
  428. } else
  429. printw("%s%-32.32s %-18.18s %s\n", cur(active), ent->name,
  430. buf, coolsize(ent->size));
  431. if (active)
  432. attroff(A_REVERSE);
  433. }
  434. int
  435. dentfill(char *path, struct entry **dents,
  436. int (*filter)(regex_t *, char *), regex_t *re)
  437. {
  438. char newpath[PATH_MAX];
  439. DIR *dirp;
  440. struct dirent *dp;
  441. struct stat sb;
  442. int r, n = 0;
  443. dirp = opendir(path);
  444. if (dirp == NULL)
  445. return 0;
  446. while ((dp = readdir(dirp)) != NULL) {
  447. /* Skip self and parent */
  448. if (strcmp(dp->d_name, ".") == 0 ||
  449. strcmp(dp->d_name, "..") == 0)
  450. continue;
  451. if (filter(re, dp->d_name) == 0)
  452. continue;
  453. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  454. strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  455. /* Get mode flags */
  456. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  457. r = lstat(newpath, &sb);
  458. if (r == -1)
  459. printerr(1, "lstat");
  460. (*dents)[n].mode = sb.st_mode;
  461. (*dents)[n].t = sb.st_mtime;
  462. (*dents)[n].size = sb.st_size;
  463. n++;
  464. }
  465. /* Should never be null */
  466. r = closedir(dirp);
  467. if (r == -1)
  468. printerr(1, "closedir");
  469. return n;
  470. }
  471. void
  472. dentfree(struct entry *dents)
  473. {
  474. free(dents);
  475. }
  476. /* Return the position of the matching entry or 0 otherwise */
  477. int
  478. dentfind(struct entry *dents, int n, char *cwd, char *path)
  479. {
  480. char tmp[PATH_MAX];
  481. int i;
  482. if (path == NULL)
  483. return 0;
  484. for (i = 0; i < n; i++) {
  485. mkpath(cwd, dents[i].name, tmp, sizeof(tmp));
  486. DPRINTF_S(path);
  487. DPRINTF_S(tmp);
  488. if (strcmp(tmp, path) == 0)
  489. return i;
  490. }
  491. return 0;
  492. }
  493. int
  494. populate(char *path, char *oldpath, char *fltr)
  495. {
  496. regex_t re;
  497. int r;
  498. /* Can fail when permissions change while browsing */
  499. if (canopendir(path) == 0)
  500. return -1;
  501. /* Search filter */
  502. r = setfilter(&re, fltr);
  503. if (r != 0)
  504. return -1;
  505. dentfree(dents);
  506. ndents = 0;
  507. dents = NULL;
  508. ndents = dentfill(path, &dents, visible, &re);
  509. qsort(dents, ndents, sizeof(*dents), entrycmp);
  510. /* Find cur from history */
  511. cur = dentfind(dents, ndents, path, oldpath);
  512. return 0;
  513. }
  514. void
  515. redraw(char *path)
  516. {
  517. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  518. size_t ncols;
  519. int nlines, odd;
  520. int i;
  521. nlines = MIN(LINES - 4, ndents);
  522. /* Clean screen */
  523. erase();
  524. /* Strip trailing slashes */
  525. for (i = strlen(path) - 1; i > 0; i--)
  526. if (path[i] == '/')
  527. path[i] = '\0';
  528. else
  529. break;
  530. DPRINTF_D(cur);
  531. DPRINTF_S(path);
  532. /* No text wrapping in cwd line */
  533. ncols = COLS;
  534. if (ncols > PATH_MAX)
  535. ncols = PATH_MAX;
  536. strlcpy(cwd, path, ncols);
  537. cwd[ncols - strlen(CWD) - 1] = '\0';
  538. if (!realpath(path, cwdresolved)) {
  539. printmsg("Cannot resolve path");
  540. return;
  541. }
  542. printw(CWD "%s\n\n", cwdresolved);
  543. /* Print listing */
  544. odd = ISODD(nlines);
  545. if (cur < (nlines >> 1)) {
  546. for (i = 0; i < nlines; i++)
  547. printptr(&dents[i], i == cur);
  548. } else if (cur >= ndents - (nlines >> 1)) {
  549. for (i = ndents - nlines; i < ndents; i++)
  550. printptr(&dents[i], i == cur);
  551. } else {
  552. nlines >>= 1;
  553. for (i = cur - nlines; i < cur + nlines + odd; i++)
  554. printptr(&dents[i], i == cur);
  555. }
  556. if (showdetail) {
  557. if (ndents) {
  558. sprintf(cwd, "%d items [%s]", ndents, dents[cur].name);
  559. printmsg(cwd);
  560. } else
  561. printmsg("0 items");
  562. }
  563. }
  564. void
  565. browse(char *ipath, char *ifilter)
  566. {
  567. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  568. char fltr[LINE_MAX];
  569. char *bin, *dir, *tmp, *run, *env;
  570. struct stat sb;
  571. regex_t re;
  572. int r, fd;
  573. strlcpy(path, ipath, sizeof(path));
  574. strlcpy(fltr, ifilter, sizeof(fltr));
  575. oldpath[0] = '\0';
  576. begin:
  577. r = populate(path, oldpath, fltr);
  578. if (r == -1) {
  579. printwarn();
  580. goto nochange;
  581. }
  582. for (;;) {
  583. redraw(path);
  584. nochange:
  585. switch (nextsel(&run, &env)) {
  586. case SEL_QUIT:
  587. dentfree(dents);
  588. return;
  589. case SEL_BACK:
  590. /* There is no going back */
  591. if (strcmp(path, "/") == 0 ||
  592. strcmp(path, ".") == 0 ||
  593. strchr(path, '/') == NULL)
  594. goto nochange;
  595. dir = xdirname(path);
  596. if (canopendir(dir) == 0) {
  597. printwarn();
  598. goto nochange;
  599. }
  600. /* Save history */
  601. strlcpy(oldpath, path, sizeof(oldpath));
  602. strlcpy(path, dir, sizeof(path));
  603. /* Reset filter */
  604. strlcpy(fltr, ifilter, sizeof(fltr));
  605. goto begin;
  606. case SEL_GOIN:
  607. /* Cannot descend in empty directories */
  608. if (ndents == 0)
  609. goto nochange;
  610. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  611. DPRINTF_S(newpath);
  612. /* Get path info */
  613. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  614. if (fd == -1) {
  615. printwarn();
  616. goto nochange;
  617. }
  618. r = fstat(fd, &sb);
  619. if (r == -1) {
  620. printwarn();
  621. close(fd);
  622. goto nochange;
  623. }
  624. close(fd);
  625. DPRINTF_U(sb.st_mode);
  626. switch (sb.st_mode & S_IFMT) {
  627. case S_IFDIR:
  628. if (canopendir(newpath) == 0) {
  629. printwarn();
  630. goto nochange;
  631. }
  632. strlcpy(path, newpath, sizeof(path));
  633. /* Reset filter */
  634. strlcpy(fltr, ifilter, sizeof(fltr));
  635. goto begin;
  636. case S_IFREG:
  637. /* If default mime opener is set, use it */
  638. if (opener) {
  639. char cmd[MAX_LEN];
  640. int status;
  641. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  642. opener, newpath);
  643. status = system(cmd);
  644. continue;
  645. }
  646. /* Try custom applications */
  647. bin = openwith(newpath);
  648. char *execvim = "vim";
  649. if (bin == NULL) {
  650. /* If a custom handler application is not set, open
  651. plain text files with vim, then try fallback_opener */
  652. FILE *fp;
  653. char cmd[MAX_LEN];
  654. int status;
  655. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  656. fp = popen(cmd, "r");
  657. if (fp == NULL)
  658. goto nochange;
  659. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  660. pclose(fp);
  661. goto nochange;
  662. }
  663. pclose(fp);
  664. if (strstr(cmd, "ASCII text") != NULL)
  665. bin = execvim;
  666. else if (fallback_opener) {
  667. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  668. fallback_opener, 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. default:
  681. printmsg("Unsupported file");
  682. goto nochange;
  683. }
  684. case SEL_FLTR:
  685. /* Read filter */
  686. printprompt("filter: ");
  687. tmp = readln();
  688. if (tmp == NULL)
  689. tmp = ifilter;
  690. /* Check and report regex errors */
  691. r = setfilter(&re, tmp);
  692. if (r != 0)
  693. goto nochange;
  694. strlcpy(fltr, tmp, sizeof(fltr));
  695. DPRINTF_S(fltr);
  696. /* Save current */
  697. if (ndents > 0)
  698. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  699. goto begin;
  700. case SEL_NEXT:
  701. if (cur < ndents - 1)
  702. cur++;
  703. else if (ndents)
  704. /* Roll over, set cursor to first entry */
  705. cur = 0;
  706. break;
  707. case SEL_PREV:
  708. if (cur > 0)
  709. cur--;
  710. else if (ndents)
  711. /* Roll over, set cursor to last entry */
  712. cur = ndents - 1;
  713. break;
  714. case SEL_PGDN:
  715. if (cur < ndents - 1)
  716. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  717. break;
  718. case SEL_PGUP:
  719. if (cur > 0)
  720. cur -= MIN((LINES - 4) / 2, cur);
  721. break;
  722. case SEL_HOME:
  723. cur = 0;
  724. break;
  725. case SEL_END:
  726. cur = ndents - 1;
  727. break;
  728. case SEL_CD:
  729. /* Read target dir */
  730. printprompt("chdir: ");
  731. tmp = readln();
  732. if (tmp == NULL) {
  733. clearprompt();
  734. goto nochange;
  735. }
  736. mkpath(path, tmp, newpath, sizeof(newpath));
  737. if (canopendir(newpath) == 0) {
  738. printwarn();
  739. goto nochange;
  740. }
  741. strlcpy(path, newpath, sizeof(path));
  742. /* Reset filter */
  743. strlcpy(fltr, ifilter, sizeof(fltr))
  744. DPRINTF_S(path);
  745. goto begin;
  746. case SEL_CDHOME:
  747. tmp = getenv("HOME");
  748. if (tmp == NULL) {
  749. clearprompt();
  750. goto nochange;
  751. }
  752. if (canopendir(tmp) == 0) {
  753. printwarn();
  754. goto nochange;
  755. }
  756. strlcpy(path, tmp, sizeof(path));
  757. /* Reset filter */
  758. strlcpy(fltr, ifilter, sizeof(fltr));
  759. DPRINTF_S(path);
  760. goto begin;
  761. case SEL_TOGGLEDOT:
  762. showhidden ^= 1;
  763. initfilter(showhidden, &ifilter);
  764. strlcpy(fltr, ifilter, sizeof(fltr));
  765. goto begin;
  766. case SEL_DETAIL:
  767. showdetail = !showdetail;
  768. showdetail ? (printptr = &printent_long) : (printptr = &printent);
  769. /* Save current */
  770. if (ndents > 0)
  771. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  772. goto begin;
  773. case SEL_FSIZE:
  774. sizeorder = !sizeorder;
  775. mtimeorder = 0;
  776. /* Save current */
  777. if (ndents > 0)
  778. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  779. goto begin;
  780. case SEL_MTIME:
  781. mtimeorder = !mtimeorder;
  782. sizeorder = 0;
  783. /* Save current */
  784. if (ndents > 0)
  785. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  786. goto begin;
  787. case SEL_REDRAW:
  788. /* Save current */
  789. if (ndents > 0)
  790. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  791. goto begin;
  792. case SEL_RUN:
  793. run = xgetenv(env, run);
  794. exitcurses();
  795. spawn(run, NULL, path);
  796. initcurses();
  797. /* Re-populate as directory content may have changed */
  798. goto begin;
  799. case SEL_RUNARG:
  800. run = xgetenv(env, run);
  801. exitcurses();
  802. spawn(run, dents[cur].name, path);
  803. initcurses();
  804. break;
  805. }
  806. /* Screensaver */
  807. if (idletimeout != 0 && idle == idletimeout) {
  808. idle = 0;
  809. exitcurses();
  810. spawn(idlecmd, NULL, NULL);
  811. initcurses();
  812. }
  813. }
  814. }
  815. void
  816. usage(char *argv0)
  817. {
  818. fprintf(stderr, "usage: %s [dir]\n", argv0);
  819. exit(1);
  820. }
  821. int
  822. main(int argc, char *argv[])
  823. {
  824. char cwd[PATH_MAX], *ipath;
  825. char *ifilter;
  826. if (argc > 2)
  827. usage(argv[0]);
  828. /* Confirm we are in a terminal */
  829. if (!isatty(0) || !isatty(1)) {
  830. fprintf(stderr, "stdin or stdout is not a tty\n");
  831. exit(1);
  832. }
  833. if (getuid() == 0)
  834. showhidden = 1;
  835. initfilter(showhidden, &ifilter);
  836. printptr = &printent;
  837. if (argv[1] != NULL) {
  838. ipath = argv[1];
  839. } else {
  840. ipath = getcwd(cwd, sizeof(cwd));
  841. if (ipath == NULL)
  842. ipath = "/";
  843. }
  844. /* Get the default desktop mime opener, if set */
  845. opener = getenv("NOICE_OPENER");
  846. /* Get the fallback desktop mime opener, if set */
  847. fallback_opener = getenv("NOICE_FALLBACK_OPENER");
  848. signal(SIGINT, SIG_IGN);
  849. /* Test initial path */
  850. if (canopendir(ipath) == 0) {
  851. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  852. exit(1);
  853. }
  854. /* Set locale before curses setup */
  855. setlocale(LC_ALL, "");
  856. initcurses();
  857. browse(ipath, ifilter);
  858. exitcurses();
  859. exit(0);
  860. }