My build of nnn with minor changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

noice.c 10 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /* Page down */
  205. case KEY_NPAGE:
  206. case CONTROL('D'):
  207. if (*cur < max -1)
  208. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  209. break;
  210. /* Page up */
  211. case KEY_PPAGE:
  212. case CONTROL('U'):
  213. if (*cur > 0)
  214. (*cur) -= MIN((LINES - 4) / 2, *cur);
  215. break;
  216. }
  217. return 0;
  218. }
  219. char *
  220. readln(void)
  221. {
  222. int c;
  223. int i = 0;
  224. char *ln = NULL;
  225. int y, x, x0;
  226. echo();
  227. curs_set(TRUE);
  228. /* Starting point */
  229. getyx(stdscr, y, x);
  230. x0 = x;
  231. while (c = getch()) {
  232. if (c == KEY_ENTER || c == '\r')
  233. break;
  234. if (c == KEY_BACKSPACE) {
  235. getyx(stdscr, y, x);
  236. if (x >= x0) {
  237. ln = realloc(ln, (i - 1) * sizeof(*ln));
  238. i--;
  239. move(y, x);
  240. printw("%c", ' ');
  241. move(y, x);
  242. } else {
  243. move(y, x0);
  244. }
  245. continue;
  246. }
  247. ln = realloc(ln, (i + 1) * sizeof(*ln));
  248. ln[i] = c;
  249. i++;
  250. }
  251. if (ln != NULL) {
  252. ln = realloc(ln, (i + 1) * sizeof(*ln));
  253. ln[i] = '\0';
  254. }
  255. curs_set(FALSE);
  256. noecho();
  257. return ln;
  258. }
  259. int
  260. testopendir(char *path)
  261. {
  262. DIR *dirp;
  263. dirp = opendir(path);
  264. if (dirp == NULL) {
  265. return 0;
  266. } else {
  267. closedir(dirp);
  268. return 1;
  269. }
  270. }
  271. void
  272. printent(struct entry *ent, int active)
  273. {
  274. char *name;
  275. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  276. char cm = 0;
  277. /* Copy name locally */
  278. name = strdup(ent->name);
  279. if (name == NULL)
  280. printerr(1, "strdup name");
  281. if (S_ISDIR(ent->mode)) {
  282. cm = '/';
  283. maxlen--;
  284. } else if (S_ISLNK(ent->mode)) {
  285. cm = '@';
  286. maxlen--;
  287. }
  288. /* No text wrapping in entries */
  289. if (strlen(name) > maxlen)
  290. name[maxlen] = '\0';
  291. if (cm == 0)
  292. printw("%s%s\n", active ? CURSR : EMPTY, name);
  293. else
  294. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  295. free(name);
  296. }
  297. void
  298. browse(const char *ipath, const char *ifilter)
  299. {
  300. DIR *dirp;
  301. int dfd;
  302. struct dirent *dp;
  303. struct entry *dents;
  304. int i, n, cur;
  305. int r, ret;
  306. char *path = strdup(ipath);
  307. char *filter = strdup(ifilter);
  308. regex_t filter_re;
  309. char *cwd;
  310. struct stat sb;
  311. begin:
  312. /* Path and filter should be malloc(3)-ed strings at all times */
  313. n = 0;
  314. cur = 0;
  315. dents = NULL;
  316. dirp = opendir(path);
  317. if (dirp == NULL) {
  318. printwarn();
  319. goto nochange;
  320. }
  321. /* Search filter */
  322. r = setfilter(&filter_re, filter);
  323. if (r != 0)
  324. goto nochange;
  325. while ((dp = readdir(dirp)) != NULL) {
  326. char *name;
  327. /* Skip self and parent */
  328. if (strcmp(dp->d_name, ".") == 0
  329. || strcmp(dp->d_name, "..") == 0)
  330. continue;
  331. if (!visible(&filter_re, dp->d_name))
  332. continue;
  333. /* Deep copy because readdir(3) reuses the entries */
  334. dents = realloc(dents, (n + 1) * sizeof(*dents));
  335. if (dents == NULL)
  336. printerr(1, "realloc");
  337. dents[n].name = strdup(dp->d_name);
  338. if (dents[n].name == NULL)
  339. printerr(1, "strdup");
  340. /* Handle root case */
  341. if (strcmp(path, "/") == 0)
  342. asprintf(&name, "/%s", dents[n].name);
  343. else
  344. asprintf(&name, "%s/%s", path, dents[n].name);
  345. /* Get mode flags */
  346. r = lstat(name, &sb);
  347. free(name);
  348. if (r == -1)
  349. printerr(1, "stat");
  350. dents[n].mode = sb.st_mode;
  351. n++;
  352. }
  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. redraw:
  367. nlines = MIN(LINES - 4, n);
  368. /* Clean screen */
  369. erase();
  370. /* Strip trailing slashes */
  371. for (i = strlen(path) - 1; i > 0; i--)
  372. if (path[i] == '/')
  373. path[i] = '\0';
  374. else
  375. break;
  376. DPRINTF_D(cur);
  377. DPRINTF_S(path);
  378. /* No text wrapping in cwd line */
  379. cwd = malloc(COLS * sizeof(char));
  380. strlcpy(cwd, path, COLS * sizeof(char));
  381. cwd[COLS - strlen(CWD) - 1] = '\0';
  382. printw(CWD "%s\n\n", cwd);
  383. /* Print listing */
  384. odd = ISODD(nlines);
  385. if (cur < nlines / 2) {
  386. for (i = 0; i < nlines; i++)
  387. printent(&dents[i], i == cur);
  388. } else if (cur >= n - nlines / 2) {
  389. for (i = n - nlines; i < n; i++)
  390. printent(&dents[i], i == cur);
  391. } else {
  392. for (i = cur - nlines / 2;
  393. i < cur + nlines / 2 + odd; i++)
  394. printent(&dents[i], i == cur);
  395. }
  396. nochange:
  397. ret = nextsel(&cur, n);
  398. switch (ret) {
  399. case SEL_QUIT:
  400. free(path);
  401. free(filter);
  402. return;
  403. case SEL_BACK:
  404. /* There is no going back */
  405. if (strcmp(path, "/") == 0) {
  406. goto nochange;
  407. } else {
  408. dir = dirname(path);
  409. tmp = malloc(strlen(dir) + 1);
  410. strlcpy(tmp, dir, strlen(dir) + 1);
  411. free(path);
  412. path = tmp;
  413. free(filter);
  414. filter = strdup(ifilter); /* Reset filter */
  415. goto out;
  416. }
  417. case SEL_GOIN:
  418. /* Cannot descend in empty directories */
  419. if (n == 0)
  420. goto nochange;
  421. name = dents[cur].name;
  422. /* Handle root case */
  423. if (strcmp(path, "/") == 0)
  424. asprintf(&pathnew, "/%s", name);
  425. else
  426. asprintf(&pathnew, "%s/%s", path, name);
  427. DPRINTF_S(name);
  428. DPRINTF_S(pathnew);
  429. /* Get path info */
  430. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  431. if (fd == -1) {
  432. printwarn();
  433. free(pathnew);
  434. goto nochange;
  435. }
  436. r = fstat(fd, &sb);
  437. close(fd);
  438. if (r == -1) {
  439. printwarn();
  440. free(pathnew);
  441. goto nochange;
  442. }
  443. DPRINTF_U(sb.st_mode);
  444. /* Directory */
  445. if (S_ISDIR(sb.st_mode)) {
  446. free(path);
  447. path = pathnew;
  448. free(filter);
  449. filter = strdup(ifilter); /* Reset filter */
  450. goto out;
  451. }
  452. /* Regular file */
  453. if (S_ISREG(sb.st_mode)) {
  454. /* Open with */
  455. bin = openwith(name);
  456. if (bin == NULL) {
  457. printmsg("No association");
  458. free(pathnew);
  459. goto nochange;
  460. }
  461. exitcurses();
  462. /* Run program */
  463. pid = fork();
  464. if (pid == 0)
  465. execlp(bin, bin, pathnew, NULL);
  466. else
  467. waitpid(pid, NULL, 0);
  468. initcurses();
  469. free(pathnew);
  470. goto redraw;
  471. }
  472. /* All the rest */
  473. printmsg("Unsupported file");
  474. free(pathnew);
  475. goto nochange;
  476. case SEL_FLTR:
  477. /* Read filter */
  478. printmsg("");
  479. move(LINES - 1, 0);
  480. printw("filter: ");
  481. tmp = readln();
  482. if (tmp == NULL) {
  483. printmsg("");
  484. goto nochange;
  485. }
  486. r = setfilter(&re, tmp);
  487. if (r != 0) {
  488. free(tmp);
  489. goto nochange;
  490. }
  491. free(filter);
  492. filter = tmp;
  493. filter_re = re;
  494. DPRINTF_S(filter);
  495. goto out;
  496. }
  497. }
  498. out:
  499. for (i = 0; i < n; i++)
  500. free(dents[i].name);
  501. free(dents);
  502. /* Should never be null */
  503. r = closedir(dirp);
  504. if (r == -1)
  505. printerr(1, "closedir");
  506. goto begin;
  507. }
  508. int
  509. main(int argc, char *argv[])
  510. {
  511. char *ipath = argv[1] != NULL ? argv[1] : "/";
  512. char *ifilter = "^[^.].*"; /* Hide dotfiles */
  513. /* Test initial path */
  514. if (!testopendir(ipath))
  515. printerr(1, ipath);
  516. /* Set locale before curses setup */
  517. setlocale(LC_ALL, "");
  518. initcurses();
  519. browse(ipath, ifilter);
  520. exitcurses();
  521. return 0;
  522. }