My build of nnn with minor changes
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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