My build of nnn with minor changes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

469 lignes
7.7 KiB

  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. { "\\.srt$", "less" },
  42. { "\\.txt$", "less" },
  43. { "\\.sh$", "sh" },
  44. { "^README$", "less" },
  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. char *
  72. openwith(char *file)
  73. {
  74. regex_t regex;
  75. char *bin = NULL;
  76. int i;
  77. for (i = 0; i < LEN(assocs); i++) {
  78. if (regcomp(&regex, assocs[i].regex,
  79. REG_NOSUB | REG_EXTENDED) != 0)
  80. continue;
  81. if (regexec(&regex, file, 0, NULL, 0) != REG_NOMATCH) {
  82. bin = assocs[i].bin;
  83. break;
  84. }
  85. }
  86. DPRINTF_S(bin);
  87. return bin;
  88. }
  89. int
  90. entrycmp(const void *va, const void *vb)
  91. {
  92. const struct entry *a, *b;
  93. a = (struct entry *)va;
  94. b = (struct entry *)vb;
  95. return strcmp(a->name, b->name);
  96. }
  97. void
  98. initcurses(void)
  99. {
  100. initscr();
  101. cbreak();
  102. noecho();
  103. nonl();
  104. intrflush(stdscr, FALSE);
  105. keypad(stdscr, TRUE);
  106. curs_set(FALSE); /* Hide cursor */
  107. }
  108. void
  109. exitcurses(void)
  110. {
  111. endwin(); /* Restore terminal */
  112. }
  113. /* Messages show up at the bottom */
  114. void
  115. printmsg(char *msg)
  116. {
  117. move(LINES - 1, 0);
  118. printw("%s\n", msg);
  119. }
  120. /* Display warning as a message */
  121. void
  122. printwarn(void)
  123. {
  124. printmsg(strerror(errno));
  125. }
  126. /* Kill curses and display error before exiting */
  127. void
  128. printerr(int ret, char *prefix)
  129. {
  130. exitcurses();
  131. printf("%s: %s\n", prefix, strerror(errno));
  132. exit(ret);
  133. }
  134. /*
  135. * Returns 0 normally
  136. * On movement it updates *cur
  137. * Returns 1 on quit
  138. * Returns 2 on go in
  139. * Returns 3 on go up
  140. */
  141. int
  142. nextsel(int *cur, int max)
  143. {
  144. int c;
  145. c = getch();
  146. switch (c) {
  147. case 'q':
  148. return 1;
  149. /* go up */
  150. case KEY_BACKSPACE:
  151. case KEY_LEFT:
  152. case 'h':
  153. return 2;
  154. /* go in */
  155. case KEY_ENTER:
  156. case '\r':
  157. case KEY_RIGHT:
  158. case 'l':
  159. return 3;
  160. /* next */
  161. case 'j':
  162. case KEY_DOWN:
  163. case CONTROL('N'):
  164. if (*cur < max - 1)
  165. (*cur)++;
  166. break;
  167. /* prev */
  168. case 'k':
  169. case KEY_UP:
  170. case CONTROL('P'):
  171. if (*cur > 0)
  172. (*cur)--;
  173. break;
  174. }
  175. return 0;
  176. }
  177. int
  178. testopendir(char *path)
  179. {
  180. DIR *dirp;
  181. dirp = opendir(path);
  182. if (dirp == NULL) {
  183. return 0;
  184. } else {
  185. closedir(dirp);
  186. return 1;
  187. }
  188. }
  189. void
  190. printent(struct entry *ent, int active)
  191. {
  192. char *name;
  193. unsigned int maxlen = COLS - strlen(CURSR) - 1;
  194. char cm = 0;
  195. /* Copy name locally */
  196. name = strdup(ent->name);
  197. if (name == NULL)
  198. printerr(1, "strdup name");
  199. if (S_ISDIR(ent->mode)) {
  200. cm = '/';
  201. maxlen--;
  202. } else if (S_ISLNK(ent->mode)) {
  203. cm = '@';
  204. maxlen--;
  205. }
  206. /* No text wrapping in entries */
  207. if (strlen(name) > maxlen)
  208. name[maxlen] = '\0';
  209. if (cm == 0)
  210. printw("%s%s\n", active ? CURSR : EMPTY, name);
  211. else
  212. printw("%s%s%c\n", active ? CURSR : EMPTY, name, cm);
  213. free(name);
  214. }
  215. void
  216. browse(const char *ipath)
  217. {
  218. DIR *dirp;
  219. int dfd;
  220. struct dirent *dp;
  221. struct entry *dents;
  222. int i, n, cur;
  223. int r, ret;
  224. char *path = strdup(ipath);
  225. char *cwd;
  226. struct stat sb;
  227. begin:
  228. /* Path should be a malloc(3)-ed string at all times */
  229. n = 0;
  230. cur = 0;
  231. dents = NULL;
  232. dirp = opendir(path);
  233. if (dirp == NULL) {
  234. printwarn();
  235. goto nochange;
  236. }
  237. while ((dp = readdir(dirp)) != NULL) {
  238. char *name;
  239. /* Skip self and parent */
  240. if (strcmp(dp->d_name, ".") == 0
  241. || strcmp(dp->d_name, "..") == 0)
  242. continue;
  243. /* Deep copy because readdir(3) reuses the entries */
  244. dents = realloc(dents, (n + 1) * sizeof(*dents));
  245. if (dents == NULL)
  246. printerr(1, "realloc");
  247. dents[n].name = strdup(dp->d_name);
  248. if (dents[n].name == NULL)
  249. printerr(1, "strdup");
  250. /* Get mode flags */
  251. asprintf(&name, "%s/%s", path, dents[n].name);
  252. r = lstat(name, &sb);
  253. free(name);
  254. if (r == -1)
  255. printerr(1, "stat");
  256. dents[n].mode = sb.st_mode;
  257. n++;
  258. }
  259. qsort(dents, n, sizeof(*dents), entrycmp);
  260. for (;;) {
  261. int nlines;
  262. int maxlen;
  263. int odd;
  264. redraw:
  265. nlines = MIN(LINES - 4, n);
  266. /* Clean screen */
  267. erase();
  268. /* Strip trailing slashes */
  269. for (i = strlen(path) - 1; i > -1; i--)
  270. if (path[i] == '/')
  271. path[i] = '\0';
  272. else
  273. break;
  274. DPRINTF_D(cur);
  275. DPRINTF_S(path);
  276. /* No text wrapping in cwd line */
  277. cwd = malloc(COLS * sizeof(char));
  278. strlcpy(cwd, path, COLS * sizeof(char));
  279. cwd[COLS - strlen(CWD) - 1] = '\0';
  280. /* Print cwd. If empty we are on the root. We store it
  281. * as an empty string so that when we navigate in /mnt
  282. * is doesn't come up as //mnt. */
  283. printw(CWD "%s%s\n\n",
  284. strcmp(cwd, "") == 0 ? "/" : "",
  285. cwd);
  286. /* Print listing */
  287. odd = ISODD(nlines);
  288. if (cur < nlines / 2) {
  289. for (i = 0; i < nlines; i++)
  290. printent(&dents[i], i == cur);
  291. } else if (cur >= n - nlines / 2) {
  292. for (i = n - nlines; i < n; i++)
  293. printent(&dents[i], i == cur);
  294. } else {
  295. for (i = cur - nlines / 2;
  296. i < cur + nlines / 2 + odd; i++)
  297. printent(&dents[i], i == cur);
  298. }
  299. nochange:
  300. ret = nextsel(&cur, n);
  301. if (ret == 1) {
  302. free(path);
  303. return;
  304. }
  305. if (ret == 2) {
  306. /* Handle root case */
  307. if (strcmp(path, "") == 0) {
  308. goto nochange;
  309. } else {
  310. char *dir, *tmp;
  311. dir = dirname(path);
  312. tmp = malloc(strlen(dir) + 1);
  313. strlcpy(tmp, dir, strlen(dir) + 1);
  314. free(path);
  315. path = tmp;
  316. goto out;
  317. }
  318. }
  319. if (ret == 3) {
  320. char *pathnew;
  321. char *name;
  322. char *bin;
  323. pid_t pid;
  324. int fd;
  325. /* Cannot descend in empty directories */
  326. if (n == 0)
  327. goto nochange;
  328. name = dents[cur].name;
  329. asprintf(&pathnew, "%s/%s", path, name);
  330. DPRINTF_S(name);
  331. DPRINTF_S(pathnew);
  332. /* Get path info */
  333. fd = open(pathnew, O_RDONLY | O_NONBLOCK);
  334. if (fd == -1) {
  335. printwarn();
  336. free(pathnew);
  337. goto nochange;
  338. }
  339. r = fstat(fd, &sb);
  340. close(fd);
  341. if (r == -1) {
  342. printwarn();
  343. free(pathnew);
  344. goto nochange;
  345. }
  346. DPRINTF_U(sb.st_mode);
  347. /* Directory */
  348. if (S_ISDIR(sb.st_mode)) {
  349. free(path);
  350. path = pathnew;
  351. goto out;
  352. }
  353. /* Regular file */
  354. if (S_ISREG(sb.st_mode)) {
  355. /* Open with */
  356. bin = openwith(name);
  357. if (bin == NULL) {
  358. printmsg("No association");
  359. free(pathnew);
  360. goto nochange;
  361. }
  362. exitcurses();
  363. /* Run program */
  364. pid = fork();
  365. if (pid == 0)
  366. execlp(bin, bin, pathnew, NULL);
  367. else
  368. waitpid(pid, NULL, 0);
  369. initcurses();
  370. free(pathnew);
  371. goto redraw;
  372. }
  373. /* All the rest */
  374. printmsg("Unsupported file");
  375. free(pathnew);
  376. goto nochange;
  377. }
  378. }
  379. out:
  380. for (i = 0; i < n; i++)
  381. free(dents[i].name);
  382. free(dents);
  383. r = closedir(dirp);
  384. if (r == -1)
  385. printerr(1, "closedir");
  386. goto begin;
  387. }
  388. int
  389. main(int argc, char *argv[])
  390. {
  391. char *ipath = argv[1] != NULL ? argv[1] : "/";
  392. /* Test initial path */
  393. if (!testopendir(ipath))
  394. printerr(1, ipath);
  395. /* Set locale before curses setup */
  396. setlocale(LC_ALL, "");
  397. initcurses();
  398. browse(ipath);
  399. exitcurses();
  400. return 0;
  401. }