My build of nnn with minor changes
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

586 行
9.7 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. { "\\.srt$", "less" },
  42. { "\\.txt$", "less" },
  43. { "\\.sh$", "sh" },
  44. { "^README$", "less" },
  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 1 on quit
  162. * Returns 2 on go in
  163. * Returns 3 on go up
  164. * Returns 4 on search
  165. */
  166. int
  167. nextsel(int *cur, int max)
  168. {
  169. int c;
  170. c = getch();
  171. switch (c) {
  172. case 'q':
  173. return 1;
  174. /* go up */
  175. case KEY_BACKSPACE:
  176. case KEY_LEFT:
  177. case 'h':
  178. return 2;
  179. /* go in */
  180. case KEY_ENTER:
  181. case '\r':
  182. case KEY_RIGHT:
  183. case 'l':
  184. return 3;
  185. /* search */
  186. case '/':
  187. case '&':
  188. return 4;
  189. /* next */
  190. case 'j':
  191. case KEY_DOWN:
  192. case CONTROL('N'):
  193. if (*cur < max - 1)
  194. (*cur)++;
  195. break;
  196. /* prev */
  197. case 'k':
  198. case KEY_UP:
  199. case CONTROL('P'):
  200. if (*cur > 0)
  201. (*cur)--;
  202. break;
  203. }
  204. return 0;
  205. }
  206. char *
  207. readln(void)
  208. {
  209. int c;
  210. int i = 0;
  211. char *ln = NULL;
  212. int y, x, x0;
  213. echo();
  214. curs_set(TRUE);
  215. /* Starting point */
  216. getyx(stdscr, y, x);
  217. x0 = x;
  218. while (c = getch()) {
  219. if (c == KEY_ENTER || c == '\r')
  220. break;
  221. if (c == KEY_BACKSPACE) {
  222. getyx(stdscr, y, x);
  223. if (x >= x0) {
  224. ln = realloc(ln, (i - 1) * sizeof(*ln));
  225. i--;
  226. move(y, x);
  227. printw("%c", ' ');
  228. move(y, x);
  229. } else {
  230. move(y, x0);
  231. }
  232. continue;
  233. }
  234. ln = realloc(ln, (i + 1) * sizeof(*ln));
  235. ln[i] = c;
  236. i++;
  237. }
  238. if (ln != NULL) {
  239. ln = realloc(ln, (i + 1) * sizeof(*ln));
  240. ln[i] = '\0';
  241. }
  242. curs_set(FALSE);
  243. noecho();
  244. return ln;
  245. }
  246. int
  247. testopendir(char *path)
  248. {
  249. DIR *dirp;
  250. dirp = opendir(path);
  251. if (dirp == NULL) {
  252. return 0;
  253. } else {
  254. closedir(dirp);
  255. return 1;
  256. }
  257. }
  258. void
  259. printent(struct entry *ent, int active)
  260. {
  261. char *name;
  262. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  263. char cm = 0;
  264. /* Copy name locally */
  265. name = strdup(ent->name);
  266. if (name == NULL)
  267. printerr(1, "strdup name");
  268. if (S_ISDIR(ent->mode)) {
  269. cm = '/';
  270. maxlen--;
  271. } else if (S_ISLNK(ent->mode)) {
  272. cm = '@';
  273. maxlen--;
  274. }
  275. /* No text wrapping in entries */
  276. if (strlen(name) > maxlen)
  277. name[maxlen] = '\0';
  278. if (cm == 0)
  279. printw("%s%s\n", active ? CURSR : EMPTY, name);
  280. else
  281. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  282. free(name);
  283. }
  284. void
  285. browse(const char *ipath, const char *ifilter)
  286. {
  287. DIR *dirp;
  288. int dfd;
  289. struct dirent *dp;
  290. struct entry *dents;
  291. int i, n, cur;
  292. int r, ret;
  293. char *path = strdup(ipath);
  294. char *filter = strdup(ifilter);
  295. regex_t filter_re;
  296. char *cwd;
  297. struct stat sb;
  298. begin:
  299. /* Path should be a malloc(3)-ed string at all times */
  300. n = 0;
  301. cur = 0;
  302. dents = NULL;
  303. dirp = opendir(path);
  304. if (dirp == NULL) {
  305. printwarn();
  306. goto nochange;
  307. }
  308. /* Search filter */
  309. r = setfilter(&filter_re, filter);
  310. if (r != 0)
  311. goto nochange;
  312. while ((dp = readdir(dirp)) != NULL) {
  313. char *name;
  314. /* Skip self and parent */
  315. if (strcmp(dp->d_name, ".") == 0
  316. || strcmp(dp->d_name, "..") == 0)
  317. continue;
  318. if (!visible(&filter_re, dp->d_name))
  319. continue;
  320. /* Deep copy because readdir(3) reuses the entries */
  321. dents = realloc(dents, (n + 1) * sizeof(*dents));
  322. if (dents == NULL)
  323. printerr(1, "realloc");
  324. dents[n].name = strdup(dp->d_name);
  325. if (dents[n].name == NULL)
  326. printerr(1, "strdup");
  327. /* Get mode flags */
  328. asprintf(&name, "%s/%s", path, dents[n].name);
  329. r = lstat(name, &sb);
  330. free(name);
  331. if (r == -1)
  332. printerr(1, "stat");
  333. dents[n].mode = sb.st_mode;
  334. n++;
  335. }
  336. qsort(dents, n, sizeof(*dents), entrycmp);
  337. for (;;) {
  338. int nlines;
  339. int maxlen;
  340. int odd;
  341. redraw:
  342. nlines = MIN(LINES - 4, n);
  343. /* Clean screen */
  344. erase();
  345. /* Strip trailing slashes */
  346. for (i = strlen(path) - 1; i > -1; i--)
  347. if (path[i] == '/')
  348. path[i] = '\0';
  349. else
  350. break;
  351. DPRINTF_D(cur);
  352. DPRINTF_S(path);
  353. /* No text wrapping in cwd line */
  354. cwd = malloc(COLS * sizeof(char));
  355. strlcpy(cwd, path, COLS * sizeof(char));
  356. cwd[COLS - strlen(CWD) - 1] = '\0';
  357. /* Print cwd. If empty we are on the root. We store it
  358. * as an empty string so that when we navigate in /mnt
  359. * is doesn't come up as //mnt. */
  360. printw(CWD "%s%s\n\n",
  361. strcmp(cwd, "") == 0 ? "/" : "",
  362. cwd);
  363. /* Print listing */
  364. odd = ISODD(nlines);
  365. if (cur < nlines / 2) {
  366. for (i = 0; i < nlines; i++)
  367. printent(&dents[i], i == cur);
  368. } else if (cur >= n - nlines / 2) {
  369. for (i = n - nlines; i < n; i++)
  370. printent(&dents[i], i == cur);
  371. } else {
  372. for (i = cur - nlines / 2;
  373. i < cur + nlines / 2 + odd; i++)
  374. printent(&dents[i], i == cur);
  375. }
  376. nochange:
  377. ret = nextsel(&cur, n);
  378. if (ret == 1) {
  379. free(path);
  380. return;
  381. }
  382. if (ret == 2) {
  383. /* Handle root case */
  384. if (strcmp(path, "") == 0) {
  385. goto nochange;
  386. } else {
  387. char *dir, *tmp;
  388. dir = dirname(path);
  389. tmp = malloc(strlen(dir) + 1);
  390. strlcpy(tmp, dir, strlen(dir) + 1);
  391. free(path);
  392. path = tmp;
  393. free(filter);
  394. filter = strdup(ifilter); /* Reset filter */
  395. goto out;
  396. }
  397. }
  398. if (ret == 3) {
  399. char *pathnew;
  400. char *name;
  401. char *bin;
  402. pid_t pid;
  403. int fd;
  404. /* Cannot descend in empty directories */
  405. if (n == 0)
  406. goto nochange;
  407. name = dents[cur].name;
  408. asprintf(&pathnew, "%s/%s", path, name);
  409. DPRINTF_S(name);
  410. DPRINTF_S(pathnew);
  411. /* Get path info */
  412. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  413. if (fd == -1) {
  414. printwarn();
  415. free(pathnew);
  416. goto nochange;
  417. }
  418. r = fstat(fd, &sb);
  419. close(fd);
  420. if (r == -1) {
  421. printwarn();
  422. free(pathnew);
  423. goto nochange;
  424. }
  425. DPRINTF_U(sb.st_mode);
  426. /* Directory */
  427. if (S_ISDIR(sb.st_mode)) {
  428. free(path);
  429. path = pathnew;
  430. free(filter);
  431. filter = strdup(ifilter); /* Reset filter */
  432. goto out;
  433. }
  434. /* Regular file */
  435. if (S_ISREG(sb.st_mode)) {
  436. /* Open with */
  437. bin = openwith(name);
  438. if (bin == NULL) {
  439. printmsg("No association");
  440. free(pathnew);
  441. goto nochange;
  442. }
  443. exitcurses();
  444. /* Run program */
  445. pid = fork();
  446. if (pid == 0)
  447. execlp(bin, bin, pathnew, NULL);
  448. else
  449. waitpid(pid, NULL, 0);
  450. initcurses();
  451. free(pathnew);
  452. goto redraw;
  453. }
  454. /* All the rest */
  455. printmsg("Unsupported file");
  456. free(pathnew);
  457. goto nochange;
  458. }
  459. if (ret == 4) {
  460. char *tmp;
  461. regex_t re;
  462. /* Read filter */
  463. move(LINES - 1, 0);
  464. printw("filter: ");
  465. tmp = readln();
  466. if (tmp == NULL) {
  467. printmsg("");
  468. goto nochange;
  469. }
  470. r = setfilter(&re, tmp);
  471. if (r != 0) {
  472. printmsg("");
  473. goto nochange;
  474. }
  475. free(filter);
  476. filter = tmp;
  477. filter_re = re;
  478. DPRINTF_S(filter);
  479. goto out;
  480. }
  481. }
  482. out:
  483. for (i = 0; i < n; i++)
  484. free(dents[i].name);
  485. free(dents);
  486. r = closedir(dirp);
  487. if (r == -1)
  488. printerr(1, "closedir");
  489. goto begin;
  490. }
  491. int
  492. main(int argc, char *argv[])
  493. {
  494. char *ipath = argv[1] != NULL ? argv[1] : "/";
  495. char *ifilter = "^[^.].*"; /* Hide dotfiles */
  496. /* Test initial path */
  497. if (!testopendir(ipath))
  498. printerr(1, ipath);
  499. /* Set locale before curses setup */
  500. setlocale(LC_ALL, "");
  501. initcurses();
  502. browse(ipath, ifilter);
  503. exitcurses();
  504. return 0;
  505. }