My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

9 anni fa
10 anni fa
9 anni fa
9 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 "util.h"
  20. #ifdef DEBUG
  21. #define DEBUG_FD 8
  22. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  23. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  24. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  25. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  26. #else
  27. #define DPRINTF_D(x)
  28. #define DPRINTF_U(x)
  29. #define DPRINTF_S(x)
  30. #define DPRINTF_P(x)
  31. #endif /* DEBUG */
  32. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  33. #undef MIN
  34. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  35. #define ISODD(x) ((x) & 1)
  36. #define CONTROL(c) ((c) ^ 0x40)
  37. #define TOUPPER(ch) \
  38. (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
  39. #define MAX_LEN 1024
  40. struct assoc {
  41. char *regex; /* Regex to match on filename */
  42. char *bin; /* Program */
  43. };
  44. /* Supported actions */
  45. enum action {
  46. SEL_QUIT = 1,
  47. SEL_BACK,
  48. SEL_GOIN,
  49. SEL_FLTR,
  50. SEL_NEXT,
  51. SEL_PREV,
  52. SEL_PGDN,
  53. SEL_PGUP,
  54. SEL_HOME,
  55. SEL_END,
  56. SEL_CD,
  57. SEL_CDHOME,
  58. SEL_TOGGLEDOT,
  59. SEL_MTIME,
  60. SEL_REDRAW,
  61. SEL_RUN,
  62. SEL_RUNARG,
  63. };
  64. struct key {
  65. int sym; /* Key pressed */
  66. enum action act; /* Action */
  67. char *run; /* Program to run */
  68. char *env; /* Environment variable to run */
  69. };
  70. #include "config.h"
  71. struct entry {
  72. char name[PATH_MAX];
  73. mode_t mode;
  74. time_t t;
  75. };
  76. /* Global context */
  77. struct entry *dents;
  78. int ndents, cur;
  79. int idle;
  80. char *opener = NULL;
  81. char *fallback_opener = NULL;
  82. /*
  83. * Layout:
  84. * .---------
  85. * | cwd: /mnt/path
  86. * |
  87. * | file0
  88. * | file1
  89. * | > file2
  90. * | file3
  91. * | file4
  92. * ...
  93. * | filen
  94. * |
  95. * | Permission denied
  96. * '------
  97. */
  98. void printmsg(char *);
  99. void printwarn(void);
  100. void printerr(int, char *);
  101. #undef dprintf
  102. int
  103. dprintf(int fd, const char *fmt, ...)
  104. {
  105. char buf[BUFSIZ];
  106. int r;
  107. va_list ap;
  108. va_start(ap, fmt);
  109. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  110. if (r > 0)
  111. r = write(fd, buf, r);
  112. va_end(ap);
  113. return r;
  114. }
  115. void *
  116. xmalloc(size_t size)
  117. {
  118. void *p;
  119. p = malloc(size);
  120. if (p == NULL)
  121. printerr(1, "malloc");
  122. return p;
  123. }
  124. void *
  125. xrealloc(void *p, size_t size)
  126. {
  127. p = realloc(p, size);
  128. if (p == NULL)
  129. printerr(1, "realloc");
  130. return p;
  131. }
  132. char *
  133. xstrdup(const char *s)
  134. {
  135. char *p;
  136. p = strdup(s);
  137. if (p == NULL)
  138. printerr(1, "strdup");
  139. return p;
  140. }
  141. /* Some implementations of dirname(3) may modify `path' and some
  142. * return a pointer inside `path'. */
  143. char *
  144. xdirname(const char *path)
  145. {
  146. static char out[PATH_MAX];
  147. char tmp[PATH_MAX], *p;
  148. strlcpy(tmp, path, sizeof(tmp));
  149. p = dirname(tmp);
  150. if (p == NULL)
  151. printerr(1, "dirname");
  152. strlcpy(out, p, sizeof(out));
  153. return out;
  154. }
  155. void
  156. spawn(char *file, char *arg, char *dir)
  157. {
  158. pid_t pid;
  159. int status;
  160. pid = fork();
  161. if (pid == 0) {
  162. if (dir != NULL)
  163. status = chdir(dir);
  164. execlp(file, file, arg, NULL);
  165. _exit(1);
  166. } else {
  167. /* Ignore interruptions */
  168. while (waitpid(pid, &status, 0) == -1)
  169. DPRINTF_D(status);
  170. DPRINTF_D(pid);
  171. }
  172. }
  173. char *
  174. xgetenv(char *name, char *fallback)
  175. {
  176. char *value;
  177. if (name == NULL)
  178. return fallback;
  179. value = getenv(name);
  180. return value && value[0] ? value : fallback;
  181. }
  182. int
  183. xstricmp(const char *s1, const char *s2)
  184. {
  185. while (*s2 != 0 && TOUPPER(*s1) == TOUPPER(*s2))
  186. s1++, s2++;
  187. /* In case of alphabetically same names, make sure
  188. lower case one comes before upper case one */
  189. if (!*s1 && !*s2)
  190. return 1;
  191. return (int) (TOUPPER(*s1) - TOUPPER(*s2));
  192. }
  193. char *
  194. openwith(char *file)
  195. {
  196. regex_t regex;
  197. char *bin = NULL;
  198. int i;
  199. for (i = 0; i < LEN(assocs); i++) {
  200. if (regcomp(&regex, assocs[i].regex,
  201. REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
  202. continue;
  203. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  204. bin = assocs[i].bin;
  205. break;
  206. }
  207. }
  208. DPRINTF_S(bin);
  209. return bin;
  210. }
  211. int
  212. setfilter(regex_t *regex, char *filter)
  213. {
  214. char errbuf[LINE_MAX];
  215. size_t len;
  216. int r;
  217. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED | REG_ICASE);
  218. if (r != 0) {
  219. len = COLS;
  220. if (len > sizeof(errbuf))
  221. len = sizeof(errbuf);
  222. regerror(r, regex, errbuf, len);
  223. printmsg(errbuf);
  224. }
  225. return r;
  226. }
  227. void
  228. initfilter(int dot, char **ifilter)
  229. {
  230. *ifilter = dot ? "." : "^[^.]";
  231. }
  232. int
  233. visible(regex_t *regex, char *file)
  234. {
  235. return regexec(regex, file, 0, NULL, 0) == 0;
  236. }
  237. int
  238. entrycmp(const void *va, const void *vb)
  239. {
  240. const struct entry *a = va, *b = vb;
  241. if (mtimeorder)
  242. return b->t - a->t;
  243. return xstricmp(a->name, b->name);
  244. }
  245. void
  246. initcurses(void)
  247. {
  248. if (initscr() == NULL) {
  249. char *term = getenv("TERM");
  250. if (term != NULL)
  251. fprintf(stderr, "error opening terminal: %s\n", term);
  252. else
  253. fprintf(stderr, "failed to initialize curses\n");
  254. exit(1);
  255. }
  256. cbreak();
  257. noecho();
  258. nonl();
  259. intrflush(stdscr, FALSE);
  260. keypad(stdscr, TRUE);
  261. curs_set(FALSE); /* Hide cursor */
  262. timeout(1000); /* One second */
  263. }
  264. void
  265. exitcurses(void)
  266. {
  267. endwin(); /* Restore terminal */
  268. }
  269. /* Messages show up at the bottom */
  270. void
  271. printmsg(char *msg)
  272. {
  273. move(LINES - 1, 0);
  274. printw("%s\n", msg);
  275. }
  276. /* Display warning as a message */
  277. void
  278. printwarn(void)
  279. {
  280. printmsg(strerror(errno));
  281. }
  282. /* Kill curses and display error before exiting */
  283. void
  284. printerr(int ret, char *prefix)
  285. {
  286. exitcurses();
  287. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  288. exit(ret);
  289. }
  290. /* Clear the last line */
  291. void
  292. clearprompt(void)
  293. {
  294. printmsg("");
  295. }
  296. /* Print prompt on the last line */
  297. void
  298. printprompt(char *str)
  299. {
  300. clearprompt();
  301. printw(str);
  302. }
  303. /* Returns SEL_* if key is bound and 0 otherwise.
  304. * Also modifies the run and env pointers (used on SEL_{RUN,RUNARG}) */
  305. int
  306. nextsel(char **run, char **env)
  307. {
  308. int c, i;
  309. c = getch();
  310. if (c == -1)
  311. idle++;
  312. else
  313. idle = 0;
  314. for (i = 0; i < LEN(bindings); i++)
  315. if (c == bindings[i].sym) {
  316. *run = bindings[i].run;
  317. *env = bindings[i].env;
  318. return bindings[i].act;
  319. }
  320. return 0;
  321. }
  322. char *
  323. readln(void)
  324. {
  325. static char ln[LINE_MAX];
  326. timeout(-1);
  327. echo();
  328. curs_set(TRUE);
  329. memset(ln, 0, sizeof(ln));
  330. wgetnstr(stdscr, ln, sizeof(ln) - 1);
  331. noecho();
  332. curs_set(FALSE);
  333. timeout(1000);
  334. return ln[0] ? ln : NULL;
  335. }
  336. int
  337. canopendir(char *path)
  338. {
  339. DIR *dirp;
  340. dirp = opendir(path);
  341. if (dirp == NULL)
  342. return 0;
  343. closedir(dirp);
  344. return 1;
  345. }
  346. char *
  347. mkpath(char *dir, char *name, char *out, size_t n)
  348. {
  349. /* Handle absolute path */
  350. if (name[0] == '/')
  351. strlcpy(out, name, n);
  352. else {
  353. /* Handle root case */
  354. if (strcmp(dir, "/") == 0)
  355. snprintf(out, n, "/%s", name);
  356. else
  357. snprintf(out, n, "%s/%s", dir, name);
  358. }
  359. return out;
  360. }
  361. void
  362. printent(struct entry *ent, int active)
  363. {
  364. char name[PATH_MAX];
  365. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  366. char cm = 0;
  367. /* Copy name locally */
  368. strlcpy(name, ent->name, sizeof(name));
  369. if (S_ISDIR(ent->mode)) {
  370. cm = '/';
  371. maxlen--;
  372. } else if (S_ISLNK(ent->mode)) {
  373. cm = '@';
  374. maxlen--;
  375. } else if (S_ISSOCK(ent->mode)) {
  376. cm = '=';
  377. maxlen--;
  378. } else if (S_ISFIFO(ent->mode)) {
  379. cm = '|';
  380. maxlen--;
  381. } else if (ent->mode & S_IXUSR) {
  382. cm = '*';
  383. maxlen--;
  384. }
  385. /* No text wrapping in entries */
  386. if (strlen(name) > maxlen)
  387. name[maxlen] = '\0';
  388. if (cm == 0)
  389. printw("%s%s\n", active ? CURSR : EMPTY, name);
  390. else
  391. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  392. }
  393. int
  394. dentfill(char *path, struct entry **dents,
  395. int (*filter)(regex_t *, char *), regex_t *re)
  396. {
  397. char newpath[PATH_MAX];
  398. DIR *dirp;
  399. struct dirent *dp;
  400. struct stat sb;
  401. int r, n = 0;
  402. dirp = opendir(path);
  403. if (dirp == NULL)
  404. return 0;
  405. while ((dp = readdir(dirp)) != NULL) {
  406. /* Skip self and parent */
  407. if (strcmp(dp->d_name, ".") == 0 ||
  408. strcmp(dp->d_name, "..") == 0)
  409. continue;
  410. if (filter(re, dp->d_name) == 0)
  411. continue;
  412. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  413. strlcpy((*dents)[n].name, dp->d_name, sizeof((*dents)[n].name));
  414. /* Get mode flags */
  415. mkpath(path, dp->d_name, newpath, sizeof(newpath));
  416. r = lstat(newpath, &sb);
  417. if (r == -1)
  418. printerr(1, "lstat");
  419. (*dents)[n].mode = sb.st_mode;
  420. (*dents)[n].t = sb.st_mtime;
  421. n++;
  422. }
  423. /* Should never be null */
  424. r = closedir(dirp);
  425. if (r == -1)
  426. printerr(1, "closedir");
  427. return n;
  428. }
  429. void
  430. dentfree(struct entry *dents)
  431. {
  432. free(dents);
  433. }
  434. /* Return the position of the matching entry or 0 otherwise */
  435. int
  436. dentfind(struct entry *dents, int n, char *cwd, char *path)
  437. {
  438. char tmp[PATH_MAX];
  439. int i;
  440. if (path == NULL)
  441. return 0;
  442. for (i = 0; i < n; i++) {
  443. mkpath(cwd, dents[i].name, tmp, sizeof(tmp));
  444. DPRINTF_S(path);
  445. DPRINTF_S(tmp);
  446. if (strcmp(tmp, path) == 0)
  447. return i;
  448. }
  449. return 0;
  450. }
  451. int
  452. populate(char *path, char *oldpath, char *fltr)
  453. {
  454. regex_t re;
  455. int r;
  456. /* Can fail when permissions change while browsing */
  457. if (canopendir(path) == 0)
  458. return -1;
  459. /* Search filter */
  460. r = setfilter(&re, fltr);
  461. if (r != 0)
  462. return -1;
  463. dentfree(dents);
  464. ndents = 0;
  465. dents = NULL;
  466. ndents = dentfill(path, &dents, visible, &re);
  467. qsort(dents, ndents, sizeof(*dents), entrycmp);
  468. /* Find cur from history */
  469. cur = dentfind(dents, ndents, path, oldpath);
  470. return 0;
  471. }
  472. void
  473. redraw(char *path)
  474. {
  475. char cwd[PATH_MAX], cwdresolved[PATH_MAX];
  476. size_t ncols;
  477. int nlines, odd;
  478. int i;
  479. nlines = MIN(LINES - 4, ndents);
  480. /* Clean screen */
  481. erase();
  482. /* Strip trailing slashes */
  483. for (i = strlen(path) - 1; i > 0; i--)
  484. if (path[i] == '/')
  485. path[i] = '\0';
  486. else
  487. break;
  488. DPRINTF_D(cur);
  489. DPRINTF_S(path);
  490. /* No text wrapping in cwd line */
  491. ncols = COLS;
  492. if (ncols > PATH_MAX)
  493. ncols = PATH_MAX;
  494. strlcpy(cwd, path, ncols);
  495. cwd[ncols - strlen(CWD) - 1] = '\0';
  496. if (!realpath(cwd, cwdresolved)) {
  497. printmsg("Cannot resolve path");
  498. return;
  499. }
  500. printw(CWD "%s\n\n", cwdresolved);
  501. /* Print listing */
  502. odd = ISODD(nlines);
  503. if (cur < (nlines >> 1)) {
  504. for (i = 0; i < nlines; i++)
  505. printent(&dents[i], i == cur);
  506. } else if (cur >= ndents - (nlines >> 1)) {
  507. for (i = ndents - nlines; i < ndents; i++)
  508. printent(&dents[i], i == cur);
  509. } else {
  510. nlines >>= 1;
  511. for (i = cur - nlines; i < cur + nlines + odd; i++)
  512. printent(&dents[i], i == cur);
  513. }
  514. }
  515. void
  516. browse(char *ipath, char *ifilter)
  517. {
  518. char path[PATH_MAX], oldpath[PATH_MAX], newpath[PATH_MAX];
  519. char fltr[LINE_MAX];
  520. char *bin, *dir, *tmp, *run, *env;
  521. struct stat sb;
  522. regex_t re;
  523. int r, fd;
  524. strlcpy(path, ipath, sizeof(path));
  525. strlcpy(fltr, ifilter, sizeof(fltr));
  526. oldpath[0] = '\0';
  527. begin:
  528. r = populate(path, oldpath, fltr);
  529. if (r == -1) {
  530. printwarn();
  531. goto nochange;
  532. }
  533. for (;;) {
  534. redraw(path);
  535. nochange:
  536. switch (nextsel(&run, &env)) {
  537. case SEL_QUIT:
  538. dentfree(dents);
  539. return;
  540. case SEL_BACK:
  541. /* There is no going back */
  542. if (strcmp(path, "/") == 0 ||
  543. strcmp(path, ".") == 0 ||
  544. strchr(path, '/') == NULL)
  545. goto nochange;
  546. dir = xdirname(path);
  547. if (canopendir(dir) == 0) {
  548. printwarn();
  549. goto nochange;
  550. }
  551. /* Save history */
  552. strlcpy(oldpath, path, sizeof(oldpath));
  553. strlcpy(path, dir, sizeof(path));
  554. /* Reset filter */
  555. strlcpy(fltr, ifilter, sizeof(fltr));
  556. goto begin;
  557. case SEL_GOIN:
  558. /* Cannot descend in empty directories */
  559. if (ndents == 0)
  560. goto nochange;
  561. mkpath(path, dents[cur].name, newpath, sizeof(newpath));
  562. DPRINTF_S(newpath);
  563. /* Get path info */
  564. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  565. if (fd == -1) {
  566. printwarn();
  567. goto nochange;
  568. }
  569. r = fstat(fd, &sb);
  570. if (r == -1) {
  571. printwarn();
  572. close(fd);
  573. goto nochange;
  574. }
  575. close(fd);
  576. DPRINTF_U(sb.st_mode);
  577. switch (sb.st_mode & S_IFMT) {
  578. case S_IFDIR:
  579. if (canopendir(newpath) == 0) {
  580. printwarn();
  581. goto nochange;
  582. }
  583. strlcpy(path, newpath, sizeof(path));
  584. /* Reset filter */
  585. strlcpy(fltr, ifilter, sizeof(fltr));
  586. goto begin;
  587. case S_IFREG:
  588. /* If default mime opener is set, use it */
  589. if (opener) {
  590. char cmd[MAX_LEN];
  591. int status;
  592. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  593. opener, newpath);
  594. status = system(cmd);
  595. continue;
  596. }
  597. /* Try custom applications */
  598. bin = openwith(newpath);
  599. char *execvim = "vim";
  600. if (bin == NULL) {
  601. /* If a custom handler application is not set, open
  602. plain text files with vim, then try fallback_opener */
  603. FILE *fp;
  604. char cmd[MAX_LEN];
  605. int status;
  606. snprintf(cmd, MAX_LEN, "file \"%s\"", newpath);
  607. fp = popen(cmd, "r");
  608. if (fp == NULL)
  609. goto nochange;
  610. if (fgets(cmd, MAX_LEN, fp) == NULL) {
  611. pclose(fp);
  612. goto nochange;
  613. }
  614. pclose(fp);
  615. if (strstr(cmd, "ASCII text") != NULL)
  616. bin = execvim;
  617. else if (fallback_opener) {
  618. snprintf(cmd, MAX_LEN, "%s \"%s\" > /dev/null 2>&1",
  619. fallback_opener, newpath);
  620. status = system(cmd);
  621. continue;
  622. } else {
  623. printmsg("No association");
  624. goto nochange;
  625. }
  626. }
  627. exitcurses();
  628. spawn(bin, newpath, NULL);
  629. initcurses();
  630. continue;
  631. default:
  632. printmsg("Unsupported file");
  633. goto nochange;
  634. }
  635. case SEL_FLTR:
  636. /* Read filter */
  637. printprompt("filter: ");
  638. tmp = readln();
  639. if (tmp == NULL)
  640. tmp = ifilter;
  641. /* Check and report regex errors */
  642. r = setfilter(&re, tmp);
  643. if (r != 0)
  644. goto nochange;
  645. strlcpy(fltr, tmp, sizeof(fltr));
  646. DPRINTF_S(fltr);
  647. /* Save current */
  648. if (ndents > 0)
  649. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  650. goto begin;
  651. case SEL_NEXT:
  652. if (cur < ndents - 1)
  653. cur++;
  654. else if (ndents)
  655. /* Roll over, set cursor to first entry */
  656. cur = 0;
  657. break;
  658. case SEL_PREV:
  659. if (cur > 0)
  660. cur--;
  661. else if (ndents)
  662. /* Roll over, set cursor to last entry */
  663. cur = ndents - 1;
  664. break;
  665. case SEL_PGDN:
  666. if (cur < ndents - 1)
  667. cur += MIN((LINES - 4) / 2, ndents - 1 - cur);
  668. break;
  669. case SEL_PGUP:
  670. if (cur > 0)
  671. cur -= MIN((LINES - 4) / 2, cur);
  672. break;
  673. case SEL_HOME:
  674. cur = 0;
  675. break;
  676. case SEL_END:
  677. cur = ndents - 1;
  678. break;
  679. case SEL_CD:
  680. /* Read target dir */
  681. printprompt("chdir: ");
  682. tmp = readln();
  683. if (tmp == NULL) {
  684. clearprompt();
  685. goto nochange;
  686. }
  687. mkpath(path, tmp, newpath, sizeof(newpath));
  688. if (canopendir(newpath) == 0) {
  689. printwarn();
  690. goto nochange;
  691. }
  692. strlcpy(path, newpath, sizeof(path));
  693. /* Reset filter */
  694. strlcpy(fltr, ifilter, sizeof(fltr))
  695. DPRINTF_S(path);
  696. goto begin;
  697. case SEL_CDHOME:
  698. tmp = getenv("HOME");
  699. if (tmp == NULL) {
  700. clearprompt();
  701. goto nochange;
  702. }
  703. if (canopendir(tmp) == 0) {
  704. printwarn();
  705. goto nochange;
  706. }
  707. strlcpy(path, tmp, sizeof(path));
  708. /* Reset filter */
  709. strlcpy(fltr, ifilter, sizeof(fltr));
  710. DPRINTF_S(path);
  711. goto begin;
  712. case SEL_TOGGLEDOT:
  713. showhidden ^= 1;
  714. initfilter(showhidden, &ifilter);
  715. strlcpy(fltr, ifilter, sizeof(fltr));
  716. goto begin;
  717. case SEL_MTIME:
  718. mtimeorder = !mtimeorder;
  719. /* Save current */
  720. if (ndents > 0)
  721. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  722. goto begin;
  723. case SEL_REDRAW:
  724. /* Save current */
  725. if (ndents > 0)
  726. mkpath(path, dents[cur].name, oldpath, sizeof(oldpath));
  727. goto begin;
  728. case SEL_RUN:
  729. run = xgetenv(env, run);
  730. exitcurses();
  731. spawn(run, NULL, path);
  732. initcurses();
  733. /* Re-populate as directory content may have changed */
  734. goto begin;
  735. case SEL_RUNARG:
  736. run = xgetenv(env, run);
  737. exitcurses();
  738. spawn(run, dents[cur].name, path);
  739. initcurses();
  740. break;
  741. }
  742. /* Screensaver */
  743. if (idletimeout != 0 && idle == idletimeout) {
  744. idle = 0;
  745. exitcurses();
  746. spawn(idlecmd, NULL, NULL);
  747. initcurses();
  748. }
  749. }
  750. }
  751. void
  752. usage(char *argv0)
  753. {
  754. fprintf(stderr, "usage: %s [dir]\n", argv0);
  755. exit(1);
  756. }
  757. int
  758. main(int argc, char *argv[])
  759. {
  760. char cwd[PATH_MAX], *ipath;
  761. char *ifilter;
  762. if (argc > 2)
  763. usage(argv[0]);
  764. /* Confirm we are in a terminal */
  765. if (!isatty(0) || !isatty(1)) {
  766. fprintf(stderr, "stdin or stdout is not a tty\n");
  767. exit(1);
  768. }
  769. if (getuid() == 0)
  770. showhidden = 1;
  771. initfilter(showhidden, &ifilter);
  772. if (argv[1] != NULL) {
  773. ipath = argv[1];
  774. } else {
  775. ipath = getcwd(cwd, sizeof(cwd));
  776. if (ipath == NULL)
  777. ipath = "/";
  778. }
  779. /* Get the default desktop mime opener, if set */
  780. opener = getenv("NOICE_OPENER");
  781. /* Get the fallback desktop mime opener, if set */
  782. fallback_opener = getenv("NOICE_FALLBACK_OPENER");
  783. signal(SIGINT, SIG_IGN);
  784. /* Test initial path */
  785. if (canopendir(ipath) == 0) {
  786. fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
  787. exit(1);
  788. }
  789. /* Set locale before curses setup */
  790. setlocale(LC_ALL, "");
  791. initcurses();
  792. browse(ipath, ifilter);
  793. exitcurses();
  794. exit(0);
  795. }