My build of nnn with minor changes
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

noice.c 13 KiB

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