My build of nnn with minor changes
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

776 rindas
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. if (oldpath != NULL) {
  472. free(oldpath);
  473. oldpath = NULL;
  474. }
  475. for (;;) {
  476. redraw:
  477. nlines = MIN(LINES - 4, n);
  478. /* Clean screen */
  479. erase();
  480. /* Strip trailing slashes */
  481. for (i = strlen(path) - 1; i > 0; i--)
  482. if (path[i] == '/')
  483. path[i] = '\0';
  484. else
  485. break;
  486. DPRINTF_D(cur);
  487. DPRINTF_S(path);
  488. /* No text wrapping in cwd line */
  489. cwd = xmalloc(COLS * sizeof(char));
  490. strlcpy(cwd, path, COLS * sizeof(char));
  491. cwd[COLS - strlen(CWD) - 1] = '\0';
  492. printw(CWD "%s\n\n", cwd);
  493. /* Print listing */
  494. odd = ISODD(nlines);
  495. if (cur < nlines / 2) {
  496. for (i = 0; i < nlines; i++)
  497. printent(&dents[i], i == cur);
  498. } else if (cur >= n - nlines / 2) {
  499. for (i = n - nlines; i < n; i++)
  500. printent(&dents[i], i == cur);
  501. } else {
  502. for (i = cur - nlines / 2;
  503. i < cur + nlines / 2 + odd; i++)
  504. printent(&dents[i], i == cur);
  505. }
  506. nochange:
  507. switch (nextsel()) {
  508. case SEL_QUIT:
  509. free(path);
  510. free(filter);
  511. dentfree(dents, n);
  512. return;
  513. case SEL_BACK:
  514. /* There is no going back */
  515. if (strcmp(path, "/") == 0 ||
  516. strcmp(path, ".") == 0 ||
  517. strchr(path, '/') == NULL)
  518. goto nochange;
  519. if (canopendir(path) == 0) {
  520. printwarn();
  521. goto nochange;
  522. }
  523. dir = xdirname(path);
  524. /* Save history */
  525. oldpath = path;
  526. path = dir;
  527. /* Reset filter */
  528. free(filter);
  529. filter = xstrdup(ifilter);
  530. goto out;
  531. case SEL_GOIN:
  532. /* Cannot descend in empty directories */
  533. if (n == 0)
  534. goto nochange;
  535. name = dents[cur].name;
  536. newpath = makepath(path, name);
  537. DPRINTF_S(newpath);
  538. /* Get path info */
  539. fd = open(newpath, O_RDONLY | O_NONBLOCK);
  540. if (fd == -1) {
  541. printwarn();
  542. free(newpath);
  543. goto nochange;
  544. }
  545. r = fstat(fd, &sb);
  546. if (r == -1) {
  547. printwarn();
  548. close(fd);
  549. free(newpath);
  550. goto nochange;
  551. }
  552. close(fd);
  553. DPRINTF_U(sb.st_mode);
  554. switch (sb.st_mode & S_IFMT) {
  555. case S_IFDIR:
  556. if (canopendir(newpath) == 0) {
  557. printwarn();
  558. free(newpath);
  559. goto nochange;
  560. }
  561. free(path);
  562. path = newpath;
  563. /* Reset filter */
  564. free(filter);
  565. filter = xstrdup(ifilter);
  566. goto out;
  567. case S_IFREG:
  568. bin = openwith(name);
  569. if (bin == NULL) {
  570. printmsg("No association");
  571. free(newpath);
  572. goto nochange;
  573. }
  574. exitcurses();
  575. spawn(bin, newpath, NULL);
  576. initcurses();
  577. free(newpath);
  578. goto redraw;
  579. default:
  580. printmsg("Unsupported file");
  581. goto nochange;
  582. }
  583. case SEL_FLTR:
  584. /* Read filter */
  585. printprompt("filter: ");
  586. tmp = readln();
  587. if (tmp == NULL) {
  588. clearprompt();
  589. goto nochange;
  590. }
  591. r = setfilter(&re, tmp);
  592. if (r != 0) {
  593. free(tmp);
  594. goto nochange;
  595. }
  596. free(filter);
  597. filter = tmp;
  598. filter_re = re;
  599. DPRINTF_S(filter);
  600. /* Save current */
  601. oldpath = makepath(path, dents[cur].name);
  602. goto out;
  603. case SEL_NEXT:
  604. if (cur < n - 1)
  605. cur++;
  606. break;
  607. case SEL_PREV:
  608. if (cur > 0)
  609. cur--;
  610. break;
  611. case SEL_PGDN:
  612. if (cur < n - 1)
  613. cur += MIN((LINES - 4) / 2, n - 1 - cur);
  614. break;
  615. case SEL_PGUP:
  616. if (cur > 0)
  617. cur -= MIN((LINES - 4) / 2, cur);
  618. break;
  619. case SEL_SH:
  620. exitcurses();
  621. spawn("/bin/sh", NULL, path);
  622. initcurses();
  623. break;
  624. case SEL_CD:
  625. /* Read target dir */
  626. printprompt("chdir: ");
  627. tmp = readln();
  628. if (tmp == NULL) {
  629. clearprompt();
  630. goto nochange;
  631. }
  632. newpath = makepath(path, tmp);
  633. free(tmp);
  634. if (canopendir(newpath) == 0) {
  635. free(newpath);
  636. printwarn();
  637. goto nochange;
  638. }
  639. free(path);
  640. path = newpath;
  641. free(filter);
  642. filter = xstrdup(ifilter); /* Reset filter */
  643. DPRINTF_S(path);
  644. goto out;
  645. }
  646. }
  647. out:
  648. dentfree(dents, n);
  649. goto begin;
  650. }
  651. int
  652. main(int argc, char *argv[])
  653. {
  654. char cwd[PATH_MAX], *ipath;
  655. char *ifilter;
  656. if (getuid() == 0)
  657. ifilter = ".";
  658. else
  659. ifilter = "^[^.]"; /* Hide dotfiles */
  660. if (argv[1] != NULL) {
  661. ipath = argv[1];
  662. } else {
  663. ipath = getcwd(cwd, sizeof(cwd));
  664. if (ipath == NULL)
  665. ipath = "/";
  666. }
  667. /* Test initial path */
  668. if (canopendir(ipath) == 0)
  669. printerr(1, ipath);
  670. /* Set locale before curses setup */
  671. setlocale(LC_ALL, "");
  672. initcurses();
  673. browse(ipath, ifilter);
  674. exitcurses();
  675. return 0;
  676. }