My build of nnn with minor changes
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

noice.c 16 KiB

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