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ů.
 
 
 
 
 
 

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