My build of nnn with minor changes
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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