My build of nnn with minor changes
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

773 řádky
13 KiB

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <curses.h>
  8. #include <libgen.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <regex.h>
  12. #include <stdarg.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <signal.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "util.h"
  19. #ifdef DEBUG
  20. #define DEBUG_FD 8
  21. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  22. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  23. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  24. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  25. #else
  26. #define DPRINTF_D(x)
  27. #define DPRINTF_U(x)
  28. #define DPRINTF_S(x)
  29. #define DPRINTF_P(x)
  30. #endif /* DEBUG */
  31. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  32. #undef MIN
  33. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  34. #define ISODD(x) ((x) & 1)
  35. #define CONTROL(c) ((c) ^ 0x40)
  36. struct assoc {
  37. char *regex; /* Regex to match on filename */
  38. char *bin; /* Program */
  39. };
  40. /* Supported actions */
  41. enum action {
  42. SEL_QUIT = 1,
  43. SEL_BACK,
  44. SEL_GOIN,
  45. SEL_FLTR,
  46. SEL_NEXT,
  47. SEL_PREV,
  48. SEL_PGDN,
  49. SEL_PGUP,
  50. SEL_SH,
  51. SEL_CD,
  52. };
  53. struct key {
  54. int sym; /* Key pressed */
  55. enum action act; /* Action */
  56. };
  57. #include "config.h"
  58. struct entry {
  59. char *name;
  60. mode_t mode;
  61. };
  62. /*
  63. * Layout:
  64. * .---------
  65. * | cwd: /mnt/path
  66. * |
  67. * | file0
  68. * | file1
  69. * | > file2
  70. * | file3
  71. * | file4
  72. * ...
  73. * | filen
  74. * |
  75. * | Permission denied
  76. * '------
  77. */
  78. void printmsg(char *msg);
  79. void printwarn(void);
  80. void printerr(int ret, char *prefix);
  81. char *makepath(char *dir, char *name);
  82. #undef dprintf
  83. int
  84. dprintf(int fd, const char *fmt, ...)
  85. {
  86. char buf[BUFSIZ];
  87. int r;
  88. va_list ap;
  89. va_start(ap, fmt);
  90. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  91. if (r > 0)
  92. write(fd, buf, r);
  93. va_end(ap);
  94. return r;
  95. }
  96. void *
  97. xmalloc(size_t size)
  98. {
  99. void *p;
  100. p = malloc(size);
  101. if (p == NULL)
  102. printerr(1, "malloc");
  103. return p;
  104. }
  105. void *
  106. xrealloc(void *p, size_t size)
  107. {
  108. p = realloc(p, size);
  109. if (p == NULL)
  110. printerr(1, "realloc");
  111. return p;
  112. }
  113. char *
  114. xstrdup(const char *s)
  115. {
  116. char *p;
  117. p = strdup(s);
  118. if (p == NULL)
  119. printerr(1, "strdup");
  120. return p;
  121. }
  122. char *
  123. xdirname(const char *path)
  124. {
  125. char *p, *tmp;
  126. /* Some implementations of dirname(3) may modify `path' and some
  127. * return a pointer inside `path' and we cannot free(3) the
  128. * original string if we lose track of it. */
  129. tmp = xstrdup(path);
  130. p = dirname(tmp);
  131. if (p == NULL) {
  132. free(tmp);
  133. printerr(1, "dirname");
  134. }
  135. /* Make sure this is a malloc(3)-ed string */
  136. p = xstrdup(p);
  137. free(tmp);
  138. return p;
  139. }
  140. void
  141. spawn(const char *file, const char *arg, const char *dir)
  142. {
  143. pid_t pid;
  144. int status;
  145. pid = fork();
  146. if (pid == 0) {
  147. if (dir != NULL)
  148. chdir(dir);
  149. execlp(file, file, arg, NULL);
  150. _exit(1);
  151. } else {
  152. /* Ignore interruptions */
  153. while (waitpid(pid, &status, 0) == -1)
  154. DPRINTF_D(status);
  155. DPRINTF_D(pid);
  156. }
  157. }
  158. char *
  159. openwith(char *file)
  160. {
  161. regex_t regex;
  162. char *bin = NULL;
  163. int i;
  164. for (i = 0; i < LEN(assocs); i++) {
  165. if (regcomp(&regex, assocs[i].regex,
  166. REG_NOSUB | REG_EXTENDED) != 0)
  167. continue;
  168. if (regexec(&regex, file, 0, NULL, 0) == 0) {
  169. bin = assocs[i].bin;
  170. break;
  171. }
  172. }
  173. DPRINTF_S(bin);
  174. return bin;
  175. }
  176. int
  177. setfilter(regex_t *regex, char *filter)
  178. {
  179. char *errbuf;
  180. int r;
  181. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
  182. if (r != 0) {
  183. errbuf = xmalloc(COLS * sizeof(char));
  184. regerror(r, regex, errbuf, COLS * sizeof(char));
  185. printmsg(errbuf);
  186. free(errbuf);
  187. }
  188. return r;
  189. }
  190. int
  191. visible(regex_t *regex, char *file)
  192. {
  193. if (regexec(regex, file, 0, NULL, 0) == 0)
  194. return 1;
  195. return 0;
  196. }
  197. int
  198. entrycmp(const void *va, const void *vb)
  199. {
  200. const struct entry *a, *b;
  201. a = (struct entry *)va;
  202. b = (struct entry *)vb;
  203. return strcmp(a->name, b->name);
  204. }
  205. void
  206. initcurses(void)
  207. {
  208. initscr();
  209. cbreak();
  210. noecho();
  211. nonl();
  212. intrflush(stdscr, FALSE);
  213. keypad(stdscr, TRUE);
  214. curs_set(FALSE); /* Hide cursor */
  215. }
  216. void
  217. exitcurses(void)
  218. {
  219. endwin(); /* Restore terminal */
  220. }
  221. /* Messages show up at the bottom */
  222. void
  223. printmsg(char *msg)
  224. {
  225. move(LINES - 1, 0);
  226. printw("%s\n", msg);
  227. }
  228. /* Display warning as a message */
  229. void
  230. printwarn(void)
  231. {
  232. printmsg(strerror(errno));
  233. }
  234. /* Kill curses and display error before exiting */
  235. void
  236. printerr(int ret, char *prefix)
  237. {
  238. exitcurses();
  239. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  240. exit(ret);
  241. }
  242. /* Clear the last line */
  243. void
  244. clearprompt(void)
  245. {
  246. printmsg("");
  247. }
  248. /* Print prompt on the last line */
  249. void
  250. printprompt(char *str)
  251. {
  252. clearprompt();
  253. printw(str);
  254. }
  255. /*
  256. * Returns SEL_{QUIT,BACK,GOIN,FLTR,NEXT,PREV,PGDN,PGUP,SH,CD}
  257. * Returns 0 otherwise
  258. */
  259. int
  260. nextsel(void)
  261. {
  262. int c, i;
  263. c = getch();
  264. for (i = 0; i < LEN(bindings); i++) {
  265. if (c == bindings[i].sym)
  266. return bindings[i].act;
  267. }
  268. return 0;
  269. }
  270. char *
  271. readln(void)
  272. {
  273. int c;
  274. int i = 0;
  275. char *ln = NULL;
  276. int y, x, x0;
  277. echo();
  278. curs_set(TRUE);
  279. /* Starting point */
  280. getyx(stdscr, y, x);
  281. x0 = x;
  282. while ((c = getch()) != ERR) {
  283. if (c == KEY_ENTER || c == '\r')
  284. break;
  285. if (c == KEY_BACKSPACE) {
  286. getyx(stdscr, y, x);
  287. if (x >= x0) {
  288. i--;
  289. if (i > 0) {
  290. ln = xrealloc(ln, i * sizeof(*ln));
  291. } else {
  292. free(ln);
  293. ln = NULL;
  294. }
  295. move(y, x);
  296. printw("%c", ' ');
  297. move(y, x);
  298. } else {
  299. move(y, x0);
  300. }
  301. continue;
  302. }
  303. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  304. ln[i] = c;
  305. i++;
  306. }
  307. if (ln != NULL) {
  308. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  309. ln[i] = '\0';
  310. }
  311. curs_set(FALSE);
  312. noecho();
  313. return ln;
  314. }
  315. int
  316. canopendir(char *path)
  317. {
  318. DIR *dirp;
  319. dirp = opendir(path);
  320. if (dirp == NULL) {
  321. return 0;
  322. } else {
  323. closedir(dirp);
  324. return 1;
  325. }
  326. }
  327. void
  328. printent(struct entry *ent, int active)
  329. {
  330. char *name;
  331. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  332. char cm = 0;
  333. /* Copy name locally */
  334. name = xstrdup(ent->name);
  335. if (S_ISDIR(ent->mode)) {
  336. cm = '/';
  337. maxlen--;
  338. } else if (S_ISLNK(ent->mode)) {
  339. cm = '@';
  340. maxlen--;
  341. } else if (S_ISSOCK(ent->mode)) {
  342. cm = '=';
  343. maxlen--;
  344. } else if (S_ISFIFO(ent->mode)) {
  345. cm = '|';
  346. maxlen--;
  347. } else if (ent->mode & S_IXUSR) {
  348. cm = '*';
  349. maxlen--;
  350. }
  351. /* No text wrapping in entries */
  352. if (strlen(name) > maxlen)
  353. name[maxlen] = '\0';
  354. if (cm == 0)
  355. printw("%s%s\n", active ? CURSR : EMPTY, name);
  356. else
  357. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  358. free(name);
  359. }
  360. int
  361. dentfill(char *path, struct entry **dents,
  362. int (*filter)(regex_t *, char *), regex_t *re)
  363. {
  364. DIR *dirp;
  365. struct dirent *dp;
  366. struct stat sb;
  367. char *newpath;
  368. int r, n = 0;
  369. dirp = opendir(path);
  370. if (dirp == NULL)
  371. return 0;
  372. while ((dp = readdir(dirp)) != NULL) {
  373. /* Skip self and parent */
  374. if (strcmp(dp->d_name, ".") == 0
  375. || strcmp(dp->d_name, "..") == 0)
  376. continue;
  377. if (filter(re, dp->d_name) == 0)
  378. continue;
  379. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  380. (*dents)[n].name = xstrdup(dp->d_name);
  381. /* Get mode flags */
  382. newpath = makepath(path, dp->d_name);
  383. r = lstat(newpath, &sb);
  384. if (r == -1)
  385. printerr(1, "lstat");
  386. (*dents)[n].mode = sb.st_mode;
  387. n++;
  388. }
  389. /* Should never be null */
  390. r = closedir(dirp);
  391. if (r == -1)
  392. printerr(1, "closedir");
  393. return n;
  394. }
  395. void
  396. dentfree(struct entry *dents, int n)
  397. {
  398. int i;
  399. for (i = 0; i < n; i++)
  400. free(dents[i].name);
  401. free(dents);
  402. }
  403. char *
  404. makepath(char *dir, char *name)
  405. {
  406. char path[PATH_MAX];
  407. /* Handle absolute path */
  408. if (name[0] == '/') {
  409. strlcpy(path, name, sizeof(path));
  410. } else {
  411. /* Handle root case */
  412. if (strcmp(dir, "/") == 0) {
  413. strlcpy(path, "/", sizeof(path));
  414. strlcat(path, name, sizeof(path));
  415. } else {
  416. strlcpy(path, dir, sizeof(path));
  417. strlcat(path, "/", sizeof(path));
  418. strlcat(path, name, sizeof(path));
  419. }
  420. }
  421. return xstrdup(path);
  422. }
  423. /* Return the position of the matching entry or 0 otherwise */
  424. int
  425. dentfind(struct entry *dents, int n, char *cwd, char *path)
  426. {
  427. int i;
  428. char *tmp;
  429. if (path == NULL)
  430. return 0;
  431. for (i = 0; i < n; i++) {
  432. tmp = makepath(cwd, dents[i].name);
  433. DPRINTF_S(path);
  434. DPRINTF_S(tmp);
  435. if (strcmp(tmp, path) == 0) {
  436. free(tmp);
  437. return i;
  438. }
  439. free(tmp);
  440. }
  441. return 0;
  442. }
  443. void
  444. browse(const char *ipath, const char *ifilter)
  445. {
  446. struct entry *dents;
  447. int i, n, cur, r, fd;
  448. int nlines, odd;
  449. char *path = xstrdup(ipath);
  450. char *filter = xstrdup(ifilter);
  451. regex_t filter_re, re;
  452. char *cwd, *newpath, *oldpath = NULL;
  453. struct stat sb;
  454. char *name, *bin, *dir, *tmp;
  455. begin:
  456. /* Path and filter should be malloc(3)-ed strings at all times */
  457. n = 0;
  458. dents = NULL;
  459. if (canopendir(path) == 0) {
  460. printwarn();
  461. goto nochange;
  462. }
  463. /* Search filter */
  464. r = setfilter(&filter_re, filter);
  465. if (r != 0)
  466. goto nochange;
  467. n = dentfill(path, &dents, visible, &filter_re);
  468. qsort(dents, n, sizeof(*dents), entrycmp);
  469. /* Find cur from history */
  470. cur = dentfind(dents, n, path, oldpath);
  471. free(oldpath);
  472. oldpath = NULL;
  473. for (;;) {
  474. nlines = MIN(LINES - 4, n);
  475. /* Clean screen */
  476. erase();
  477. /* Strip trailing slashes */
  478. for (i = strlen(path) - 1; i > 0; i--)
  479. if (path[i] == '/')
  480. path[i] = '\0';
  481. else
  482. break;
  483. DPRINTF_D(cur);
  484. DPRINTF_S(path);
  485. /* No text wrapping in cwd line */
  486. cwd = xmalloc(COLS * sizeof(char));
  487. strlcpy(cwd, path, COLS * sizeof(char));
  488. cwd[COLS - strlen(CWD) - 1] = '\0';
  489. printw(CWD "%s\n\n", cwd);
  490. /* Print listing */
  491. odd = ISODD(nlines);
  492. if (cur < nlines / 2) {
  493. for (i = 0; i < nlines; i++)
  494. printent(&dents[i], i == cur);
  495. } else if (cur >= n - nlines / 2) {
  496. for (i = n - nlines; i < n; i++)
  497. printent(&dents[i], i == cur);
  498. } else {
  499. for (i = cur - nlines / 2;
  500. i < cur + nlines / 2 + odd; i++)
  501. printent(&dents[i], i == cur);
  502. }
  503. nochange:
  504. switch (nextsel()) {
  505. case SEL_QUIT:
  506. free(path);
  507. free(filter);
  508. dentfree(dents, n);
  509. return;
  510. case SEL_BACK:
  511. /* There is no going back */
  512. if (strcmp(path, "/") == 0 ||
  513. strcmp(path, ".") == 0 ||
  514. strchr(path, '/') == NULL)
  515. goto nochange;
  516. if (canopendir(path) == 0) {
  517. printwarn();
  518. goto nochange;
  519. }
  520. dir = xdirname(path);
  521. /* Save history */
  522. oldpath = path;
  523. path = dir;
  524. /* Reset filter */
  525. free(filter);
  526. filter = xstrdup(ifilter);
  527. goto out;
  528. case SEL_GOIN:
  529. /* Cannot descend in empty directories */
  530. if (n == 0)
  531. goto nochange;
  532. name = dents[cur].name;
  533. newpath = makepath(path, name);
  534. DPRINTF_S(newpath);
  535. /* Get path info */
  536. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  537. if (fd == -1) {
  538. printwarn();
  539. free(newpath);
  540. goto nochange;
  541. }
  542. r = fstat(fd, &sb);
  543. if (r == -1) {
  544. printwarn();
  545. close(fd);
  546. free(newpath);
  547. goto nochange;
  548. }
  549. close(fd);
  550. DPRINTF_U(sb.st_mode);
  551. switch (sb.st_mode & S_IFMT) {
  552. case S_IFDIR:
  553. if (canopendir(newpath) == 0) {
  554. printwarn();
  555. free(newpath);
  556. goto nochange;
  557. }
  558. free(path);
  559. path = newpath;
  560. /* Reset filter */
  561. free(filter);
  562. filter = xstrdup(ifilter);
  563. goto out;
  564. case S_IFREG:
  565. bin = openwith(name);
  566. if (bin == NULL) {
  567. printmsg("No association");
  568. free(newpath);
  569. goto nochange;
  570. }
  571. exitcurses();
  572. spawn(bin, newpath, NULL);
  573. initcurses();
  574. free(newpath);
  575. continue;
  576. default:
  577. printmsg("Unsupported file");
  578. goto nochange;
  579. }
  580. case SEL_FLTR:
  581. /* Read filter */
  582. printprompt("filter: ");
  583. tmp = readln();
  584. if (tmp == NULL) {
  585. clearprompt();
  586. goto nochange;
  587. }
  588. r = setfilter(&re, tmp);
  589. if (r != 0) {
  590. free(tmp);
  591. goto nochange;
  592. }
  593. free(filter);
  594. filter = tmp;
  595. filter_re = re;
  596. DPRINTF_S(filter);
  597. /* Save current */
  598. oldpath = makepath(path, dents[cur].name);
  599. goto out;
  600. case SEL_NEXT:
  601. if (cur < n - 1)
  602. cur++;
  603. break;
  604. case SEL_PREV:
  605. if (cur > 0)
  606. cur--;
  607. break;
  608. case SEL_PGDN:
  609. if (cur < n - 1)
  610. cur += MIN((LINES - 4) / 2, n - 1 - cur);
  611. break;
  612. case SEL_PGUP:
  613. if (cur > 0)
  614. cur -= MIN((LINES - 4) / 2, cur);
  615. break;
  616. case SEL_SH:
  617. exitcurses();
  618. spawn("/bin/sh", NULL, path);
  619. initcurses();
  620. break;
  621. case SEL_CD:
  622. /* Read target dir */
  623. printprompt("chdir: ");
  624. tmp = readln();
  625. if (tmp == NULL) {
  626. clearprompt();
  627. goto nochange;
  628. }
  629. newpath = makepath(path, tmp);
  630. free(tmp);
  631. if (canopendir(newpath) == 0) {
  632. free(newpath);
  633. printwarn();
  634. goto nochange;
  635. }
  636. free(path);
  637. path = newpath;
  638. free(filter);
  639. filter = xstrdup(ifilter); /* Reset filter */
  640. DPRINTF_S(path);
  641. goto out;
  642. }
  643. }
  644. out:
  645. dentfree(dents, n);
  646. goto begin;
  647. }
  648. int
  649. main(int argc, char *argv[])
  650. {
  651. char cwd[PATH_MAX], *ipath;
  652. char *ifilter;
  653. if (getuid() == 0)
  654. ifilter = ".";
  655. else
  656. ifilter = "^[^.]"; /* Hide dotfiles */
  657. if (argv[1] != NULL) {
  658. ipath = argv[1];
  659. } else {
  660. ipath = getcwd(cwd, sizeof(cwd));
  661. if (ipath == NULL)
  662. ipath = "/";
  663. }
  664. /* Test initial path */
  665. if (canopendir(ipath) == 0)
  666. printerr(1, ipath);
  667. /* Set locale before curses setup */
  668. setlocale(LC_ALL, "");
  669. initcurses();
  670. browse(ipath, ifilter);
  671. exitcurses();
  672. return 0;
  673. }