My build of nnn with minor changes
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

634 lines
11 KiB

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <curses.h>
  7. #include <libgen.h>
  8. #include <locale.h>
  9. #include <regex.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #ifdef LINUX
  16. #include <bsd/string.h>
  17. #endif
  18. #include "queue.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. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  33. #define ISODD(x) ((x) & 1)
  34. #define CONTROL(c) ((c) ^ 0x40)
  35. struct assoc {
  36. char *regex; /* Regex to match on filename */
  37. char *bin; /* Program */
  38. };
  39. #include "config.h"
  40. struct entry {
  41. char *name;
  42. mode_t mode;
  43. };
  44. struct history {
  45. int pos;
  46. SLIST_ENTRY(history) entry;
  47. };
  48. SLIST_HEAD(histhead, history) histhead = SLIST_HEAD_INITIALIZER(histhead);
  49. #define CWD "cwd: "
  50. #define CURSR " > "
  51. #define EMPTY " "
  52. /*
  53. * Layout:
  54. * .---------
  55. * | cwd: /mnt/path
  56. * |
  57. * | file0
  58. * | file1
  59. * | > file2
  60. * | file3
  61. * | file4
  62. * ...
  63. * | filen
  64. * |
  65. * | Permission denied
  66. * '------
  67. */
  68. int die = 0;
  69. void printmsg(char *msg);
  70. void printwarn(void);
  71. void printerr(int ret, char *prefix);
  72. char *
  73. openwith(char *file)
  74. {
  75. regex_t regex;
  76. char *bin = NULL;
  77. int i;
  78. for (i = 0; i < LEN(assocs); i++) {
  79. if (regcomp(&regex, assocs[i].regex,
  80. REG_NOSUB | REG_EXTENDED) != 0)
  81. continue;
  82. if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
  83. bin = assocs[i].bin;
  84. break;
  85. }
  86. }
  87. DPRINTF_S(bin);
  88. return bin;
  89. }
  90. int
  91. setfilter(regex_t *regex, char *filter)
  92. {
  93. char *errbuf;
  94. int r;
  95. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
  96. if (r != 0) {
  97. errbuf = malloc(COLS * sizeof(char));
  98. regerror(r, regex, errbuf, COLS * sizeof(char));
  99. printmsg(errbuf);
  100. free(errbuf);
  101. }
  102. return r;
  103. }
  104. int
  105. visible(regex_t *regex, char *file)
  106. {
  107. if (regexec(regex, file, 0, NULL, 0) != REG_NOMATCH)
  108. return 1;
  109. return 0;
  110. }
  111. int
  112. entrycmp(const void *va, const void *vb)
  113. {
  114. const struct entry *a, *b;
  115. a = (struct entry *)va;
  116. b = (struct entry *)vb;
  117. return strcmp(a->name, b->name);
  118. }
  119. void
  120. initcurses(void)
  121. {
  122. initscr();
  123. cbreak();
  124. noecho();
  125. nonl();
  126. intrflush(stdscr, FALSE);
  127. keypad(stdscr, TRUE);
  128. curs_set(FALSE); /* Hide cursor */
  129. }
  130. void
  131. exitcurses(void)
  132. {
  133. endwin(); /* Restore terminal */
  134. }
  135. /* Messages show up at the bottom */
  136. void
  137. printmsg(char *msg)
  138. {
  139. move(LINES - 1, 0);
  140. printw("%s\n", msg);
  141. }
  142. /* Display warning as a message */
  143. void
  144. printwarn(void)
  145. {
  146. printmsg(strerror(errno));
  147. }
  148. /* Kill curses and display error before exiting */
  149. void
  150. printerr(int ret, char *prefix)
  151. {
  152. exitcurses();
  153. printf("%s: %s\n", prefix, strerror(errno));
  154. exit(ret);
  155. }
  156. /*
  157. * Returns 0 normally
  158. * On movement it updates *cur
  159. * Returns SEL_{QUIT,BACK,GOIN,FLTR} otherwise
  160. */
  161. #define SEL_QUIT 1
  162. #define SEL_BACK 2
  163. #define SEL_GOIN 3
  164. #define SEL_FLTR 4
  165. int
  166. nextsel(int *cur, int max)
  167. {
  168. int c;
  169. c = getch();
  170. switch (c) {
  171. case 'q':
  172. return SEL_QUIT;
  173. /* Back */
  174. case KEY_BACKSPACE:
  175. case KEY_LEFT:
  176. case 'h':
  177. return SEL_BACK;
  178. /* Inside */
  179. case KEY_ENTER:
  180. case '\r':
  181. case KEY_RIGHT:
  182. case 'l':
  183. return SEL_GOIN;
  184. /* Filter */
  185. case '/':
  186. case '&':
  187. return SEL_FLTR;
  188. /* Next */
  189. case 'j':
  190. case KEY_DOWN:
  191. case CONTROL('N'):
  192. if (*cur < max - 1)
  193. (*cur)++;
  194. break;
  195. /* Previous */
  196. case 'k':
  197. case KEY_UP:
  198. case CONTROL('P'):
  199. if (*cur > 0)
  200. (*cur)--;
  201. break;
  202. /* Page down */
  203. case KEY_NPAGE:
  204. case CONTROL('D'):
  205. if (*cur < max -1)
  206. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  207. break;
  208. /* Page up */
  209. case KEY_PPAGE:
  210. case CONTROL('U'):
  211. if (*cur > 0)
  212. (*cur) -= MIN((LINES - 4) / 2, *cur);
  213. break;
  214. }
  215. return 0;
  216. }
  217. char *
  218. readln(void)
  219. {
  220. int c;
  221. int i = 0;
  222. char *ln = NULL;
  223. int y, x, x0;
  224. echo();
  225. curs_set(TRUE);
  226. /* Starting point */
  227. getyx(stdscr, y, x);
  228. x0 = x;
  229. while (c = getch()) {
  230. if (c == KEY_ENTER || c == '\r')
  231. break;
  232. if (c == KEY_BACKSPACE) {
  233. getyx(stdscr, y, x);
  234. if (x >= x0) {
  235. ln = realloc(ln, (i - 1) * sizeof(*ln));
  236. i--;
  237. move(y, x);
  238. printw("%c", ' ');
  239. move(y, x);
  240. } else {
  241. move(y, x0);
  242. }
  243. continue;
  244. }
  245. ln = realloc(ln, (i + 1) * sizeof(*ln));
  246. ln[i] = c;
  247. i++;
  248. }
  249. if (ln != NULL) {
  250. ln = realloc(ln, (i + 1) * sizeof(*ln));
  251. ln[i] = '\0';
  252. }
  253. curs_set(FALSE);
  254. noecho();
  255. return ln;
  256. }
  257. int
  258. testopendir(char *path)
  259. {
  260. DIR *dirp;
  261. dirp = opendir(path);
  262. if (dirp == NULL) {
  263. return 0;
  264. } else {
  265. closedir(dirp);
  266. return 1;
  267. }
  268. }
  269. void
  270. printent(struct entry *ent, int active)
  271. {
  272. char *name;
  273. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  274. char cm = 0;
  275. /* Copy name locally */
  276. name = strdup(ent->name);
  277. if (name == NULL)
  278. printerr(1, "strdup name");
  279. if (S_ISDIR(ent->mode)) {
  280. cm = '/';
  281. maxlen--;
  282. } else if (S_ISLNK(ent->mode)) {
  283. cm = '@';
  284. maxlen--;
  285. }
  286. /* No text wrapping in entries */
  287. if (strlen(name) > maxlen)
  288. name[maxlen] = '\0';
  289. if (cm == 0)
  290. printw("%s%s\n", active ? CURSR : EMPTY, name);
  291. else
  292. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  293. free(name);
  294. }
  295. void
  296. browse(const char *ipath, const char *ifilter)
  297. {
  298. DIR *dirp;
  299. int dfd;
  300. struct dirent *dp;
  301. struct entry *dents;
  302. int i, n, cur;
  303. int r, ret;
  304. char *path = strdup(ipath);
  305. char *filter = strdup(ifilter);
  306. regex_t filter_re;
  307. char *cwd;
  308. struct stat sb;
  309. cur = 0;
  310. begin:
  311. /* Path and filter should be malloc(3)-ed strings at all times */
  312. n = 0;
  313. dents = NULL;
  314. dirp = opendir(path);
  315. if (dirp == NULL) {
  316. printwarn();
  317. goto nochange;
  318. }
  319. /* Search filter */
  320. r = setfilter(&filter_re, filter);
  321. if (r != 0)
  322. goto nochange;
  323. while ((dp = readdir(dirp)) != NULL) {
  324. char *name;
  325. /* Skip self and parent */
  326. if (strcmp(dp->d_name, ".") == 0
  327. || strcmp(dp->d_name, "..") == 0)
  328. continue;
  329. if (!visible(&filter_re, dp->d_name))
  330. continue;
  331. /* Deep copy because readdir(3) reuses the entries */
  332. dents = realloc(dents, (n + 1) * sizeof(*dents));
  333. if (dents == NULL)
  334. printerr(1, "realloc");
  335. dents[n].name = strdup(dp->d_name);
  336. if (dents[n].name == NULL)
  337. printerr(1, "strdup");
  338. /* Handle root case */
  339. if (strcmp(path, "/") == 0)
  340. asprintf(&name, "/%s", dents[n].name);
  341. else
  342. asprintf(&name, "%s/%s", path, dents[n].name);
  343. /* Get mode flags */
  344. r = lstat(name, &sb);
  345. free(name);
  346. if (r == -1)
  347. printerr(1, "stat");
  348. dents[n].mode = sb.st_mode;
  349. n++;
  350. }
  351. /* Make sure cur is in range */
  352. cur = MIN(cur, n - 1);
  353. qsort(dents, n, sizeof(*dents), entrycmp);
  354. for (;;) {
  355. int nlines;
  356. int maxlen;
  357. int odd;
  358. char *pathnew;
  359. char *name;
  360. char *bin;
  361. pid_t pid;
  362. int fd;
  363. char *dir;
  364. char *tmp;
  365. regex_t re;
  366. struct history *hist;
  367. int status;
  368. redraw:
  369. nlines = MIN(LINES - 4, n);
  370. /* Clean screen */
  371. erase();
  372. /* Strip trailing slashes */
  373. for (i = strlen(path) - 1; i > 0; i--)
  374. if (path[i] == '/')
  375. path[i] = '\0';
  376. else
  377. break;
  378. DPRINTF_D(cur);
  379. DPRINTF_S(path);
  380. /* No text wrapping in cwd line */
  381. cwd = malloc(COLS * sizeof(char));
  382. strlcpy(cwd, path, COLS * sizeof(char));
  383. cwd[COLS - strlen(CWD) - 1] = '\0';
  384. printw(CWD "%s\n\n", cwd);
  385. /* Print listing */
  386. odd = ISODD(nlines);
  387. if (cur < nlines / 2) {
  388. for (i = 0; i < nlines; i++)
  389. printent(&dents[i], i == cur);
  390. } else if (cur >= n - nlines / 2) {
  391. for (i = n - nlines; i < n; i++)
  392. printent(&dents[i], i == cur);
  393. } else {
  394. for (i = cur - nlines / 2;
  395. i < cur + nlines / 2 + odd; i++)
  396. printent(&dents[i], i == cur);
  397. }
  398. nochange:
  399. ret = nextsel(&cur, n);
  400. switch (ret) {
  401. case SEL_QUIT:
  402. free(path);
  403. free(filter);
  404. /* Forget history */
  405. while (!SLIST_EMPTY(&histhead)) {
  406. hist = SLIST_FIRST(&histhead);
  407. SLIST_REMOVE_HEAD(&histhead, entry);
  408. free(hist);
  409. }
  410. return;
  411. case SEL_BACK:
  412. /* There is no going back */
  413. if (strcmp(path, "/") == 0) {
  414. goto nochange;
  415. } else {
  416. dir = dirname(path);
  417. tmp = malloc(strlen(dir) + 1);
  418. strlcpy(tmp, dir, strlen(dir) + 1);
  419. free(path);
  420. path = tmp;
  421. free(filter);
  422. filter = strdup(ifilter); /* Reset filter */
  423. /* Recall history */
  424. hist = SLIST_FIRST(&histhead);
  425. if (hist != NULL) {
  426. cur = hist->pos;
  427. DPRINTF_D(hist->pos);
  428. SLIST_REMOVE_HEAD(&histhead, entry);
  429. free(hist);
  430. } else {
  431. cur = 0;
  432. }
  433. goto out;
  434. }
  435. case SEL_GOIN:
  436. /* Cannot descend in empty directories */
  437. if (n == 0)
  438. goto nochange;
  439. name = dents[cur].name;
  440. /* Handle root case */
  441. if (strcmp(path, "/") == 0)
  442. asprintf(&pathnew, "/%s", name);
  443. else
  444. asprintf(&pathnew, "%s/%s", path, name);
  445. DPRINTF_S(name);
  446. DPRINTF_S(pathnew);
  447. /* Get path info */
  448. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  449. if (fd == -1) {
  450. printwarn();
  451. free(pathnew);
  452. goto nochange;
  453. }
  454. r = fstat(fd, &sb);
  455. close(fd);
  456. if (r == -1) {
  457. printwarn();
  458. free(pathnew);
  459. goto nochange;
  460. }
  461. DPRINTF_U(sb.st_mode);
  462. /* Directory */
  463. if (S_ISDIR(sb.st_mode)) {
  464. free(path);
  465. path = pathnew;
  466. free(filter);
  467. filter = strdup(ifilter); /* Reset filter */
  468. /* Save history */
  469. hist = malloc(sizeof(struct history));
  470. hist->pos = cur;
  471. SLIST_INSERT_HEAD(&histhead, hist, entry);
  472. cur = 0;
  473. goto out;
  474. }
  475. /* Regular file */
  476. if (S_ISREG(sb.st_mode)) {
  477. /* Open with */
  478. bin = openwith(name);
  479. if (bin == NULL) {
  480. printmsg("No association");
  481. free(pathnew);
  482. goto nochange;
  483. }
  484. exitcurses();
  485. /* Run program */
  486. pid = fork();
  487. if (pid == 0) {
  488. execlp(bin, bin, pathnew, NULL);
  489. _exit(0);
  490. } else {
  491. /* Ignore interruptions */
  492. while (waitpid(pid, &status,
  493. 0) == -1)
  494. DPRINTF_D(status);
  495. DPRINTF_D(pid);
  496. }
  497. initcurses();
  498. free(pathnew);
  499. goto redraw;
  500. }
  501. /* All the rest */
  502. printmsg("Unsupported file");
  503. free(pathnew);
  504. goto nochange;
  505. case SEL_FLTR:
  506. /* Read filter */
  507. printmsg("");
  508. move(LINES - 1, 0);
  509. printw("filter: ");
  510. tmp = readln();
  511. if (tmp == NULL) {
  512. printmsg("");
  513. goto nochange;
  514. }
  515. r = setfilter(&re, tmp);
  516. if (r != 0) {
  517. free(tmp);
  518. goto nochange;
  519. }
  520. free(filter);
  521. filter = tmp;
  522. filter_re = re;
  523. DPRINTF_S(filter);
  524. cur = 0;
  525. goto out;
  526. }
  527. }
  528. out:
  529. for (i = 0; i < n; i++)
  530. free(dents[i].name);
  531. free(dents);
  532. /* Should never be null */
  533. r = closedir(dirp);
  534. if (r == -1)
  535. printerr(1, "closedir");
  536. goto begin;
  537. }
  538. int
  539. main(int argc, char *argv[])
  540. {
  541. char *ipath = argv[1] != NULL ? argv[1] : "/";
  542. char *ifilter = "^[^.].*"; /* Hide dotfiles */
  543. /* Test initial path */
  544. if (!testopendir(ipath))
  545. printerr(1, ipath);
  546. /* Set locale before curses setup */
  547. setlocale(LC_ALL, "");
  548. initcurses();
  549. browse(ipath, ifilter);
  550. exitcurses();
  551. return 0;
  552. }