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

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