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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <dirent.h>
  7. #include <curses.h>
  8. #include <libgen.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <regex.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <signal.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include "queue.h"
  18. #include "util.h"
  19. #ifdef DEBUG
  20. #define DEBUG_FD 8
  21. #define DPRINTF_D(x) dprintf(DEBUG_FD, #x "=%d\n", x)
  22. #define DPRINTF_U(x) dprintf(DEBUG_FD, #x "=%u\n", x)
  23. #define DPRINTF_S(x) dprintf(DEBUG_FD, #x "=%s\n", x)
  24. #define DPRINTF_P(x) dprintf(DEBUG_FD, #x "=0x%p\n", x)
  25. #else
  26. #define DPRINTF_D(x)
  27. #define DPRINTF_U(x)
  28. #define DPRINTF_S(x)
  29. #define DPRINTF_P(x)
  30. #endif /* DEBUG */
  31. #define LEN(x) (sizeof(x) / sizeof(*(x)))
  32. #undef MIN
  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. /*
  51. * Layout:
  52. * .---------
  53. * | cwd: /mnt/path
  54. * |
  55. * | file0
  56. * | file1
  57. * | > file2
  58. * | file3
  59. * | file4
  60. * ...
  61. * | filen
  62. * |
  63. * | Permission denied
  64. * '------
  65. */
  66. void printmsg(char *msg);
  67. void printwarn(void);
  68. void printerr(int ret, char *prefix);
  69. void *
  70. xmalloc(size_t size)
  71. {
  72. void *p;
  73. p = malloc(size);
  74. if (p == NULL)
  75. printerr(1, "malloc");
  76. return p;
  77. }
  78. void *
  79. xrealloc(void *p, size_t size)
  80. {
  81. p = realloc(p, size);
  82. if (p == NULL)
  83. printerr(1, "realloc");
  84. return p;
  85. }
  86. char *
  87. xstrdup(const char *s)
  88. {
  89. char *p;
  90. p = strdup(s);
  91. if (p == NULL)
  92. printerr(1, "strdup");
  93. return p;
  94. }
  95. char *
  96. xrealpath(const char *path)
  97. {
  98. char *p;
  99. p = realpath(path, NULL);
  100. if (p == NULL)
  101. printerr(1, "realpath");
  102. return p;
  103. }
  104. char *
  105. xdirname(const char *path)
  106. {
  107. char *p, *tmp;
  108. /* Some implementations of dirname(3) may modify `path' and some
  109. * return a pointer inside `path` and we cannot free(3) the
  110. * original string if we lose track of it. */
  111. tmp = xstrdup(path);
  112. p = dirname(tmp);
  113. if (p == NULL) {
  114. free(tmp);
  115. printerr(1, "dirname");
  116. }
  117. /* Make sure this is a malloc(3)-ed string */
  118. p = xstrdup(p);
  119. free(tmp);
  120. return p;
  121. }
  122. void
  123. spawn(const char *file, const char *arg)
  124. {
  125. pid_t pid;
  126. int status;
  127. pid = fork();
  128. if (pid == 0) {
  129. execlp(file, file, arg, NULL);
  130. _exit(1);
  131. } else {
  132. /* Ignore interruptions */
  133. while (waitpid(pid, &status, 0) == -1)
  134. DPRINTF_D(status);
  135. DPRINTF_D(pid);
  136. }
  137. }
  138. char *
  139. openwith(char *file)
  140. {
  141. regex_t regex;
  142. char *bin = NULL;
  143. int i;
  144. for (i = 0; i < LEN(assocs); i++) {
  145. if (regcomp(&regex, assocs[i].regex,
  146. REG_NOSUB | REG_EXTENDED) != 0)
  147. continue;
  148. if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
  149. bin = assocs[i].bin;
  150. break;
  151. }
  152. }
  153. DPRINTF_S(bin);
  154. return bin;
  155. }
  156. int
  157. setfilter(regex_t *regex, char *filter)
  158. {
  159. char *errbuf;
  160. int r;
  161. r = regcomp(regex, filter, REG_NOSUB | REG_EXTENDED);
  162. if (r != 0) {
  163. errbuf = xmalloc(COLS * sizeof(char));
  164. regerror(r, regex, errbuf, COLS * sizeof(char));
  165. printmsg(errbuf);
  166. free(errbuf);
  167. }
  168. return r;
  169. }
  170. int
  171. visible(regex_t *regex, char *file)
  172. {
  173. if (regexec(regex, file, 0, NULL, 0) != REG_NOMATCH)
  174. return 1;
  175. return 0;
  176. }
  177. int
  178. entrycmp(const void *va, const void *vb)
  179. {
  180. const struct entry *a, *b;
  181. a = (struct entry *)va;
  182. b = (struct entry *)vb;
  183. return strcmp(a->name, b->name);
  184. }
  185. void
  186. initcurses(void)
  187. {
  188. initscr();
  189. cbreak();
  190. noecho();
  191. nonl();
  192. intrflush(stdscr, FALSE);
  193. keypad(stdscr, TRUE);
  194. curs_set(FALSE); /* Hide cursor */
  195. }
  196. void
  197. exitcurses(void)
  198. {
  199. endwin(); /* Restore terminal */
  200. }
  201. /* Messages show up at the bottom */
  202. void
  203. printmsg(char *msg)
  204. {
  205. move(LINES - 1, 0);
  206. printw("%s\n", msg);
  207. }
  208. /* Display warning as a message */
  209. void
  210. printwarn(void)
  211. {
  212. printmsg(strerror(errno));
  213. }
  214. /* Kill curses and display error before exiting */
  215. void
  216. printerr(int ret, char *prefix)
  217. {
  218. exitcurses();
  219. fprintf(stderr, "%s: %s\n", prefix, strerror(errno));
  220. exit(ret);
  221. }
  222. /* Clear the last line */
  223. void
  224. clearprompt(void)
  225. {
  226. printmsg("");
  227. }
  228. /* Print prompt on the last line */
  229. void
  230. printprompt(char *str)
  231. {
  232. clearprompt();
  233. printw(str);
  234. }
  235. /*
  236. * Returns 0 normally
  237. * On movement it updates *cur
  238. * Returns SEL_{QUIT,BACK,GOIN,FLTR,SH,CD} otherwise
  239. */
  240. enum {
  241. SEL_QUIT = 1,
  242. SEL_BACK,
  243. SEL_GOIN,
  244. SEL_FLTR,
  245. SEL_SH,
  246. SEL_CD,
  247. };
  248. int
  249. nextsel(int *cur, int max)
  250. {
  251. int c;
  252. c = getch();
  253. switch (c) {
  254. case 'q':
  255. return SEL_QUIT;
  256. /* Back */
  257. case KEY_BACKSPACE:
  258. case KEY_LEFT:
  259. case 'h':
  260. return SEL_BACK;
  261. /* Inside */
  262. case KEY_ENTER:
  263. case '\r':
  264. case KEY_RIGHT:
  265. case 'l':
  266. return SEL_GOIN;
  267. /* Filter */
  268. case '/':
  269. case '&':
  270. return SEL_FLTR;
  271. /* Next */
  272. case 'j':
  273. case KEY_DOWN:
  274. case CONTROL('N'):
  275. if (*cur < max - 1)
  276. (*cur)++;
  277. break;
  278. /* Previous */
  279. case 'k':
  280. case KEY_UP:
  281. case CONTROL('P'):
  282. if (*cur > 0)
  283. (*cur)--;
  284. break;
  285. /* Page down */
  286. case KEY_NPAGE:
  287. case CONTROL('D'):
  288. if (*cur < max -1)
  289. (*cur) += MIN((LINES - 4) / 2, max - 1 - *cur);
  290. break;
  291. /* Page up */
  292. case KEY_PPAGE:
  293. case CONTROL('U'):
  294. if (*cur > 0)
  295. (*cur) -= MIN((LINES - 4) / 2, *cur);
  296. break;
  297. case '!':
  298. return SEL_SH;
  299. case 'c':
  300. return SEL_CD;
  301. }
  302. return 0;
  303. }
  304. char *
  305. readln(void)
  306. {
  307. int c;
  308. int i = 0;
  309. char *ln = NULL;
  310. int y, x, x0;
  311. echo();
  312. curs_set(TRUE);
  313. /* Starting point */
  314. getyx(stdscr, y, x);
  315. x0 = x;
  316. while ((c = getch()) != ERR) {
  317. if (c == KEY_ENTER || c == '\r')
  318. break;
  319. if (c == KEY_BACKSPACE) {
  320. getyx(stdscr, y, x);
  321. if (x >= x0) {
  322. if (i > 0) {
  323. ln = xrealloc(ln, (i - 1) * sizeof(*ln));
  324. i--;
  325. } else {
  326. free(ln);
  327. ln = NULL;
  328. }
  329. move(y, x);
  330. printw("%c", ' ');
  331. move(y, x);
  332. } else {
  333. move(y, x0);
  334. }
  335. continue;
  336. }
  337. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  338. ln[i] = c;
  339. i++;
  340. }
  341. if (ln != NULL) {
  342. ln = xrealloc(ln, (i + 1) * sizeof(*ln));
  343. ln[i] = '\0';
  344. }
  345. curs_set(FALSE);
  346. noecho();
  347. return ln;
  348. }
  349. int
  350. canopendir(char *path)
  351. {
  352. DIR *dirp;
  353. dirp = opendir(path);
  354. if (dirp == NULL) {
  355. return 0;
  356. } else {
  357. closedir(dirp);
  358. return 1;
  359. }
  360. }
  361. void
  362. printent(struct entry *ent, int active)
  363. {
  364. char *name;
  365. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  366. char cm = 0;
  367. /* Copy name locally */
  368. name = xstrdup(ent->name);
  369. if (S_ISDIR(ent->mode)) {
  370. cm = '/';
  371. maxlen--;
  372. } else if (S_ISLNK(ent->mode)) {
  373. cm = '@';
  374. maxlen--;
  375. } else if (ent->mode & S_IXUSR) {
  376. cm = '*';
  377. maxlen--;
  378. }
  379. /* No text wrapping in entries */
  380. if (strlen(name) > maxlen)
  381. name[maxlen] = '\0';
  382. if (cm == 0)
  383. printw("%s%s\n", active ? CURSR : EMPTY, name);
  384. else
  385. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  386. free(name);
  387. }
  388. int
  389. dentfill(DIR *dirp, struct entry **dents,
  390. int (*filter)(regex_t *, char *), regex_t *re)
  391. {
  392. struct dirent *dp;
  393. struct stat sb;
  394. int n = 0;
  395. int r;
  396. while ((dp = readdir(dirp)) != NULL) {
  397. /* Skip self and parent */
  398. if (strcmp(dp->d_name, ".") == 0
  399. || strcmp(dp->d_name, "..") == 0)
  400. continue;
  401. if (filter(re, dp->d_name) == 0)
  402. continue;
  403. *dents = xrealloc(*dents, (n + 1) * sizeof(**dents));
  404. (*dents)[n].name = xstrdup(dp->d_name);
  405. /* Get mode flags */
  406. r = fstatat(dirfd(dirp), dp->d_name, &sb,
  407. AT_SYMLINK_NOFOLLOW);
  408. if (r == -1)
  409. printerr(1, "stat");
  410. (*dents)[n].mode = sb.st_mode;
  411. n++;
  412. }
  413. return n;
  414. }
  415. void
  416. dentfree(struct entry *dents, int n)
  417. {
  418. int i;
  419. for (i = 0; i < n; i++)
  420. free(dents[i].name);
  421. free(dents);
  422. }
  423. void
  424. pushhist(int pos)
  425. {
  426. struct history *hist;
  427. hist = xmalloc(sizeof(*hist));
  428. hist->pos = pos;
  429. SLIST_INSERT_HEAD(&histhead, hist, entry);
  430. }
  431. int
  432. pophist(void)
  433. {
  434. struct history *hist;
  435. int pos;
  436. /* Recall history */
  437. hist = SLIST_FIRST(&histhead);
  438. if (hist != NULL) {
  439. pos = hist->pos;
  440. SLIST_REMOVE_HEAD(&histhead, entry);
  441. free(hist);
  442. } else {
  443. pos = 0;
  444. }
  445. return pos;
  446. }
  447. void
  448. forgethist(void)
  449. {
  450. struct history *hist;
  451. while (SLIST_EMPTY(&histhead) == 0) {
  452. hist = SLIST_FIRST(&histhead);
  453. SLIST_REMOVE_HEAD(&histhead, entry);
  454. free(hist);
  455. }
  456. }
  457. void
  458. browse(const char *ipath, const char *ifilter)
  459. {
  460. DIR *dirp;
  461. struct entry *dents;
  462. int i, n, cur;
  463. int r, ret;
  464. char *path = xrealpath(ipath);
  465. char *filter = xstrdup(ifilter);
  466. regex_t filter_re;
  467. char *cwd;
  468. struct stat sb;
  469. cur = 0;
  470. begin:
  471. /* Path and filter should be malloc(3)-ed strings at all times */
  472. n = 0;
  473. dents = NULL;
  474. dirp = opendir(path);
  475. if (dirp == NULL) {
  476. printwarn();
  477. goto nochange;
  478. } else {
  479. if (chdir(path) == -1)
  480. printwarn();
  481. }
  482. /* Search filter */
  483. r = setfilter(&filter_re, filter);
  484. if (r != 0)
  485. goto nochange;
  486. n = dentfill(dirp, &dents, visible, &filter_re);
  487. /* Make sure cur is in range */
  488. cur = MIN(cur, n - 1);
  489. qsort(dents, n, sizeof(*dents), entrycmp);
  490. for (;;) {
  491. int nlines;
  492. int odd;
  493. char *name;
  494. char *bin;
  495. char *dir;
  496. char *tmp;
  497. regex_t re;
  498. redraw:
  499. nlines = MIN(LINES - 4, n);
  500. /* Clean screen */
  501. erase();
  502. DPRINTF_D(cur);
  503. DPRINTF_S(path);
  504. /* No text wrapping in cwd line */
  505. cwd = xmalloc(COLS * sizeof(char));
  506. strlcpy(cwd, path, COLS * sizeof(char));
  507. cwd[COLS - strlen(CWD) - 1] = '\0';
  508. printw(CWD "%s\n\n", cwd);
  509. /* Print listing */
  510. odd = ISODD(nlines);
  511. if (cur < nlines / 2) {
  512. for (i = 0; i < nlines; i++)
  513. printent(&dents[i], i == cur);
  514. } else if (cur >= n - nlines / 2) {
  515. for (i = n - nlines; i < n; i++)
  516. printent(&dents[i], i == cur);
  517. } else {
  518. for (i = cur - nlines / 2;
  519. i < cur + nlines / 2 + odd; i++)
  520. printent(&dents[i], i == cur);
  521. }
  522. nochange:
  523. ret = nextsel(&cur, n);
  524. switch (ret) {
  525. case SEL_QUIT:
  526. free(path);
  527. free(filter);
  528. forgethist();
  529. dentfree(dents, n);
  530. return;
  531. case SEL_BACK:
  532. /* There is no going back */
  533. if (strcmp(path, "/") == 0)
  534. goto nochange;
  535. if (canopendir(path) == 0) {
  536. printwarn();
  537. goto nochange;
  538. }
  539. dir = xdirname(path);
  540. free(path);
  541. path = dir;
  542. /* Reset filter */
  543. free(filter);
  544. filter = xstrdup(ifilter);
  545. /* Recall history */
  546. cur = pophist();
  547. goto out;
  548. case SEL_GOIN:
  549. /* Cannot descend in empty directories */
  550. if (n == 0)
  551. goto nochange;
  552. name = dents[cur].name;
  553. DPRINTF_S(name);
  554. /* Get path info */
  555. r = fstatat(dirfd(dirp), name, &sb, 0);
  556. if (r == -1) {
  557. printwarn();
  558. goto nochange;
  559. }
  560. DPRINTF_U(sb.st_mode);
  561. switch (sb.st_mode & S_IFMT) {
  562. case S_IFDIR:
  563. if (canopendir(path) == 0) {
  564. printwarn();
  565. goto nochange;
  566. }
  567. free(path);
  568. path = xrealpath(name);
  569. /* Reset filter */
  570. free(filter);
  571. filter = xstrdup(ifilter);
  572. /* Remember history */
  573. pushhist(cur);
  574. cur = 0;
  575. goto out;
  576. case S_IFREG:
  577. bin = openwith(name);
  578. if (bin == NULL) {
  579. printmsg("No association");
  580. goto nochange;
  581. }
  582. exitcurses();
  583. spawn(bin, name);
  584. initcurses();
  585. goto redraw;
  586. default:
  587. printmsg("Unsupported file");
  588. goto nochange;
  589. }
  590. case SEL_FLTR:
  591. /* Read filter */
  592. printprompt("filter: ");
  593. tmp = readln();
  594. if (tmp == NULL) {
  595. clearprompt();
  596. goto nochange;
  597. }
  598. r = setfilter(&re, tmp);
  599. if (r != 0) {
  600. free(tmp);
  601. goto nochange;
  602. }
  603. free(filter);
  604. filter = tmp;
  605. filter_re = re;
  606. DPRINTF_S(filter);
  607. cur = 0;
  608. goto out;
  609. case SEL_SH:
  610. exitcurses();
  611. spawn("/bin/sh", NULL);
  612. initcurses();
  613. break;
  614. case SEL_CD:
  615. /* Read target dir */
  616. printprompt("chdir: ");
  617. tmp = readln();
  618. if (tmp == NULL) {
  619. clearprompt();
  620. goto nochange;
  621. }
  622. if (canopendir(tmp) == 0) {
  623. printwarn();
  624. goto nochange;
  625. }
  626. free(path);
  627. path = xrealpath(tmp);
  628. free(tmp);
  629. free(filter);
  630. filter = xstrdup(ifilter); /* Reset filter */
  631. forgethist();
  632. DPRINTF_S(path);
  633. cur = 0;
  634. goto out;
  635. }
  636. }
  637. out:
  638. dentfree(dents, n);
  639. /* Should never be null */
  640. r = closedir(dirp);
  641. if (r == -1)
  642. printerr(1, "closedir");
  643. goto begin;
  644. }
  645. int
  646. main(int argc, char *argv[])
  647. {
  648. char cwd[PATH_MAX], *ipath;
  649. char *ifilter;
  650. if (getuid() == 0)
  651. ifilter = ".*";
  652. else
  653. ifilter = "^[^.].*"; /* Hide dotfiles */
  654. if (argv[1] != NULL) {
  655. ipath = argv[1];
  656. } else {
  657. ipath = getcwd(cwd, sizeof(cwd));
  658. if (ipath == NULL)
  659. ipath = "/";
  660. }
  661. /* Test initial path */
  662. if (canopendir(ipath) == 0)
  663. printerr(1, ipath);
  664. /* Set locale before curses setup */
  665. setlocale(LC_ALL, "");
  666. initcurses();
  667. browse(ipath, ifilter);
  668. exitcurses();
  669. return 0;
  670. }