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 12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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,SH,CD} otherwise
  171. */
  172. enum {
  173. SEL_QUIT = 1,
  174. SEL_BACK,
  175. SEL_GOIN,
  176. SEL_FLTR,
  177. SEL_SH,
  178. SEL_CD,
  179. };
  180. int
  181. nextsel(int *cur, int max)
  182. {
  183. int c;
  184. c = getch();
  185. switch (c) {
  186. case 'q':
  187. return SEL_QUIT;
  188. /* Back */
  189. case KEY_BACKSPACE:
  190. case KEY_LEFT:
  191. case 'h':
  192. return SEL_BACK;
  193. /* Inside */
  194. case KEY_ENTER:
  195. case '\r':
  196. case KEY_RIGHT:
  197. case 'l':
  198. return SEL_GOIN;
  199. /* Filter */
  200. case '/':
  201. case '&':
  202. return SEL_FLTR;
  203. /* Next */
  204. case 'j':
  205. case KEY_DOWN:
  206. case CONTROL('N'):
  207. if (*cur < max - 1)
  208. (*cur)++;
  209. break;
  210. /* Previous */
  211. case 'k':
  212. case KEY_UP:
  213. case CONTROL('P'):
  214. if (*cur > 0)
  215. (*cur)--;
  216. break;
  217. /* Page down */
  218. case KEY_NPAGE:
  219. case CONTROL('D'):
  220. if (*cur < max -1)
  221. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  222. break;
  223. /* Page up */
  224. case KEY_PPAGE:
  225. case CONTROL('U'):
  226. if (*cur > 0)
  227. (*cur) -= MIN((LINES - 4) / 2, *cur);
  228. break;
  229. case '!':
  230. return SEL_SH;
  231. case 'c':
  232. return SEL_CD;
  233. }
  234. return 0;
  235. }
  236. char *
  237. readln(void)
  238. {
  239. int c;
  240. int i = 0;
  241. char *ln = NULL;
  242. int y, x, x0;
  243. echo();
  244. curs_set(TRUE);
  245. /* Starting point */
  246. getyx(stdscr, y, x);
  247. x0 = x;
  248. while (c = getch()) {
  249. if (c == KEY_ENTER || c == '\r')
  250. break;
  251. if (c == KEY_BACKSPACE) {
  252. getyx(stdscr, y, x);
  253. if (x >= x0) {
  254. if (i > 0) {
  255. ln = realloc(ln, (i - 1) * sizeof(*ln));
  256. i--;
  257. } else {
  258. free(ln);
  259. ln = NULL;
  260. }
  261. move(y, x);
  262. printw("%c", ' ');
  263. move(y, x);
  264. } else {
  265. move(y, x0);
  266. }
  267. continue;
  268. }
  269. ln = realloc(ln, (i + 1) * sizeof(*ln));
  270. ln[i] = c;
  271. i++;
  272. }
  273. if (ln != NULL) {
  274. ln = realloc(ln, (i + 1) * sizeof(*ln));
  275. ln[i] = '\0';
  276. }
  277. curs_set(FALSE);
  278. noecho();
  279. return ln;
  280. }
  281. int
  282. testopendir(char *path)
  283. {
  284. DIR *dirp;
  285. dirp = opendir(path);
  286. if (dirp == NULL) {
  287. return 0;
  288. } else {
  289. closedir(dirp);
  290. return 1;
  291. }
  292. }
  293. void
  294. printent(struct entry *ent, int active)
  295. {
  296. char *name;
  297. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  298. char cm = 0;
  299. /* Copy name locally */
  300. name = strdup(ent->name);
  301. if (name == NULL)
  302. printerr(1, "strdup name");
  303. if (S_ISDIR(ent->mode)) {
  304. cm = '/';
  305. maxlen--;
  306. } else if (S_ISLNK(ent->mode)) {
  307. cm = '@';
  308. maxlen--;
  309. } else if (ent->mode & S_IXUSR) {
  310. cm = '*';
  311. maxlen--;
  312. }
  313. /* No text wrapping in entries */
  314. if (strlen(name) > maxlen)
  315. name[maxlen] = '\0';
  316. if (cm == 0)
  317. printw("%s%s\n", active ? CURSR : EMPTY, name);
  318. else
  319. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  320. free(name);
  321. }
  322. void
  323. browse(const char *ipath, const char *ifilter)
  324. {
  325. DIR *dirp;
  326. int dfd;
  327. struct dirent *dp;
  328. struct entry *dents;
  329. int i, n, cur;
  330. int r, ret;
  331. char *path = realpath(ipath, NULL);
  332. char *filter = strdup(ifilter);
  333. regex_t filter_re;
  334. char *cwd;
  335. struct stat sb;
  336. cur = 0;
  337. begin:
  338. /* Path and filter should be malloc(3)-ed strings at all times */
  339. n = 0;
  340. dents = NULL;
  341. dirp = opendir(path);
  342. if (dirp == NULL) {
  343. printwarn();
  344. goto nochange;
  345. } else {
  346. if (chdir(path) == -1)
  347. printwarn();
  348. }
  349. /* Search filter */
  350. r = setfilter(&filter_re, filter);
  351. if (r != 0)
  352. goto nochange;
  353. while ((dp = readdir(dirp)) != NULL) {
  354. char *name;
  355. /* Skip self and parent */
  356. if (strcmp(dp->d_name, ".") == 0
  357. || strcmp(dp->d_name, "..") == 0)
  358. continue;
  359. if (!visible(&filter_re, dp->d_name))
  360. continue;
  361. /* Deep copy because readdir(3) reuses the entries */
  362. dents = realloc(dents, (n + 1) * sizeof(*dents));
  363. if (dents == NULL)
  364. printerr(1, "realloc");
  365. dents[n].name = strdup(dp->d_name);
  366. if (dents[n].name == NULL)
  367. printerr(1, "strdup");
  368. /* Handle root case */
  369. if (strcmp(path, "/") == 0)
  370. asprintf(&name, "/%s", dents[n].name);
  371. else
  372. asprintf(&name, "%s/%s", path, dents[n].name);
  373. /* Get mode flags */
  374. r = lstat(name, &sb);
  375. free(name);
  376. if (r == -1)
  377. printerr(1, "stat");
  378. dents[n].mode = sb.st_mode;
  379. n++;
  380. }
  381. /* Make sure cur is in range */
  382. cur = MIN(cur, n - 1);
  383. qsort(dents, n, sizeof(*dents), entrycmp);
  384. for (;;) {
  385. int nlines;
  386. int maxlen;
  387. int odd;
  388. char *pathnew;
  389. char *name;
  390. char *bin;
  391. int fd;
  392. char *dir;
  393. char *tmp;
  394. regex_t re;
  395. struct history *hist;
  396. redraw:
  397. nlines = MIN(LINES - 4, n);
  398. /* Clean screen */
  399. erase();
  400. /* Strip trailing slashes */
  401. for (i = strlen(path) - 1; i > 0; i--)
  402. if (path[i] == '/')
  403. path[i] = '\0';
  404. else
  405. break;
  406. DPRINTF_D(cur);
  407. DPRINTF_S(path);
  408. /* No text wrapping in cwd line */
  409. cwd = malloc(COLS * sizeof(char));
  410. strlcpy(cwd, path, COLS * sizeof(char));
  411. cwd[COLS - strlen(CWD) - 1] = '\0';
  412. printw(CWD "%s\n\n", cwd);
  413. /* Print listing */
  414. odd = ISODD(nlines);
  415. if (cur < nlines / 2) {
  416. for (i = 0; i < nlines; i++)
  417. printent(&dents[i], i == cur);
  418. } else if (cur >= n - nlines / 2) {
  419. for (i = n - nlines; i < n; i++)
  420. printent(&dents[i], i == cur);
  421. } else {
  422. for (i = cur - nlines / 2;
  423. i < cur + nlines / 2 + odd; i++)
  424. printent(&dents[i], i == cur);
  425. }
  426. nochange:
  427. ret = nextsel(&cur, n);
  428. switch (ret) {
  429. case SEL_QUIT:
  430. free(path);
  431. free(filter);
  432. /* Forget history */
  433. while (!SLIST_EMPTY(&histhead)) {
  434. hist = SLIST_FIRST(&histhead);
  435. SLIST_REMOVE_HEAD(&histhead, entry);
  436. free(hist);
  437. }
  438. return;
  439. case SEL_BACK:
  440. /* There is no going back */
  441. if (strcmp(path, "/") == 0) {
  442. goto nochange;
  443. } else {
  444. dir = dirname(path);
  445. tmp = malloc(strlen(dir) + 1);
  446. strlcpy(tmp, dir, strlen(dir) + 1);
  447. free(path);
  448. path = tmp;
  449. free(filter);
  450. filter = strdup(ifilter); /* Reset filter */
  451. /* Recall history */
  452. hist = SLIST_FIRST(&histhead);
  453. if (hist != NULL) {
  454. cur = hist->pos;
  455. DPRINTF_D(hist->pos);
  456. SLIST_REMOVE_HEAD(&histhead, entry);
  457. free(hist);
  458. } else {
  459. cur = 0;
  460. }
  461. goto out;
  462. }
  463. case SEL_GOIN:
  464. /* Cannot descend in empty directories */
  465. if (n == 0)
  466. goto nochange;
  467. name = dents[cur].name;
  468. /* Handle root case */
  469. if (strcmp(path, "/") == 0)
  470. asprintf(&pathnew, "/%s", name);
  471. else
  472. asprintf(&pathnew, "%s/%s", path, name);
  473. DPRINTF_S(name);
  474. DPRINTF_S(pathnew);
  475. /* Get path info */
  476. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  477. if (fd == -1) {
  478. printwarn();
  479. free(pathnew);
  480. goto nochange;
  481. }
  482. r = fstat(fd, &sb);
  483. close(fd);
  484. if (r == -1) {
  485. printwarn();
  486. free(pathnew);
  487. goto nochange;
  488. }
  489. DPRINTF_U(sb.st_mode);
  490. /* Directory */
  491. if (S_ISDIR(sb.st_mode)) {
  492. free(path);
  493. path = pathnew;
  494. free(filter);
  495. filter = strdup(ifilter); /* Reset filter */
  496. /* Save history */
  497. hist = malloc(sizeof(struct history));
  498. hist->pos = cur;
  499. SLIST_INSERT_HEAD(&histhead, hist, entry);
  500. cur = 0;
  501. goto out;
  502. }
  503. /* Regular file */
  504. if (S_ISREG(sb.st_mode)) {
  505. /* Open with */
  506. bin = openwith(name);
  507. if (bin == NULL) {
  508. printmsg("No association");
  509. free(pathnew);
  510. goto nochange;
  511. }
  512. exitcurses();
  513. spawn(bin, pathnew);
  514. initcurses();
  515. free(pathnew);
  516. goto redraw;
  517. }
  518. /* All the rest */
  519. printmsg("Unsupported file");
  520. free(pathnew);
  521. goto nochange;
  522. case SEL_FLTR:
  523. /* Read filter */
  524. printmsg("");
  525. move(LINES - 1, 0);
  526. printw("filter: ");
  527. tmp = readln();
  528. if (tmp == NULL) {
  529. printmsg("");
  530. goto nochange;
  531. }
  532. r = setfilter(&re, tmp);
  533. if (r != 0) {
  534. free(tmp);
  535. goto nochange;
  536. }
  537. free(filter);
  538. filter = tmp;
  539. filter_re = re;
  540. DPRINTF_S(filter);
  541. cur = 0;
  542. goto out;
  543. case SEL_SH:
  544. exitcurses();
  545. spawn("/bin/sh", NULL);
  546. initcurses();
  547. break;
  548. case SEL_CD:
  549. /* Read target dir */
  550. printmsg("");
  551. move(LINES - 1, 0);
  552. printw("chdir: ");
  553. tmp = readln();
  554. if (tmp == NULL) {
  555. printmsg("");
  556. goto nochange;
  557. }
  558. if (testopendir(tmp) == 0) {
  559. printwarn();
  560. goto nochange;
  561. } else {
  562. free(path);
  563. path = realpath(tmp, NULL);
  564. free(tmp);
  565. free(filter);
  566. filter = strdup(ifilter); /* Reset filter */
  567. DPRINTF_S(path);
  568. cur = 0;
  569. goto out;
  570. }
  571. }
  572. }
  573. out:
  574. for (i = 0; i < n; i++)
  575. free(dents[i].name);
  576. free(dents);
  577. /* Should never be null */
  578. r = closedir(dirp);
  579. if (r == -1)
  580. printerr(1, "closedir");
  581. goto begin;
  582. }
  583. int
  584. main(int argc, char *argv[])
  585. {
  586. char cwd[PATH_MAX], *ipath;
  587. char *ifilter;
  588. if (getuid() == 0)
  589. ifilter = ".*";
  590. else
  591. ifilter = "^[^.].*"; /* Hide dotfiles */
  592. if (argv[1] != NULL) {
  593. ipath = argv[1];
  594. } else {
  595. ipath = getcwd(cwd, sizeof(cwd));
  596. if (ipath == NULL)
  597. ipath = "/";
  598. }
  599. /* Test initial path */
  600. if (!testopendir(ipath))
  601. printerr(1, ipath);
  602. /* Set locale before curses setup */
  603. setlocale(LC_ALL, "");
  604. initcurses();
  605. browse(ipath, ifilter);
  606. exitcurses();
  607. return 0;
  608. }