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

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