My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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