rover.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. #define _XOPEN_SOURCE 700
  2. #define _XOPEN_SOURCE_EXTENDED
  3. #define _FILE_OFFSET_BITS 64
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <ctype.h>
  7. #include <wchar.h>
  8. #include <wctype.h>
  9. #include <string.h>
  10. #include <sys/types.h> /* pid_t, ... */
  11. #include <stdio.h>
  12. #include <limits.h> /* PATH_MAX */
  13. #include <locale.h> /* setlocale(), LC_ALL */
  14. #include <unistd.h> /* chdir(), getcwd(), read(), close(), ... */
  15. #include <dirent.h> /* DIR, struct dirent, opendir(), ... */
  16. #include <libgen.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h> /* open() */
  19. #include <sys/wait.h> /* waitpid() */
  20. #include <signal.h> /* struct sigaction, sigaction() */
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <curses.h>
  24. #include "config.h"
  25. /* This signal is not defined by POSIX, but should be
  26. present on all systems that have resizable terminals. */
  27. #ifndef SIGWINCH
  28. #define SIGWINCH 28
  29. #endif
  30. /* String buffers. */
  31. #define BUFLEN PATH_MAX
  32. static char BUF1[BUFLEN];
  33. static char BUF2[BUFLEN];
  34. static char INPUT[BUFLEN];
  35. static char CLIPBOARD[BUFLEN];
  36. static wchar_t WBUF[BUFLEN];
  37. /* Paths to external programs. */
  38. static char *user_shell;
  39. static char *user_pager;
  40. static char *user_editor;
  41. static char *user_open;
  42. /* Listing view parameters. */
  43. #define HEIGHT (LINES-4)
  44. #define STATUSPOS (COLS-16)
  45. /* Listing view flags. */
  46. #define SHOW_FILES 0x01u
  47. #define SHOW_DIRS 0x02u
  48. #define SHOW_HIDDEN 0x04u
  49. /* Marks parameters. */
  50. #define BULK_INIT 5
  51. #define BULK_THRESH 256
  52. /* Information associated to each entry in listing. */
  53. typedef struct Row {
  54. char *name;
  55. off_t size;
  56. mode_t mode;
  57. int islink;
  58. int marked;
  59. } Row;
  60. /* Dynamic array of marked entries. */
  61. typedef struct Marks {
  62. char dirpath[PATH_MAX];
  63. int bulk;
  64. int nentries;
  65. char **entries;
  66. } Marks;
  67. /* Line editing state. */
  68. typedef struct Edit {
  69. wchar_t buffer[BUFLEN+1];
  70. int left, right;
  71. } Edit;
  72. /* Each tab only stores the following information. */
  73. typedef struct Tab {
  74. int scroll;
  75. int esel;
  76. uint8_t flags;
  77. char cwd[PATH_MAX];
  78. } Tab;
  79. typedef struct Prog {
  80. off_t partial;
  81. off_t total;
  82. const char *msg;
  83. } Prog;
  84. /* Global state. */
  85. static struct Rover {
  86. int tab;
  87. int nfiles;
  88. Row *rows;
  89. WINDOW *window;
  90. Marks marks;
  91. Edit edit;
  92. int edit_scroll;
  93. volatile sig_atomic_t pending_usr1;
  94. volatile sig_atomic_t pending_winch;
  95. Prog prog;
  96. Tab tabs[10];
  97. } rover;
  98. /* Macros for accessing global state. */
  99. #define ENAME(I) rover.rows[I].name
  100. #define ESIZE(I) rover.rows[I].size
  101. #define EMODE(I) rover.rows[I].mode
  102. #define ISLINK(I) rover.rows[I].islink
  103. #define MARKED(I) rover.rows[I].marked
  104. #define SCROLL rover.tabs[rover.tab].scroll
  105. #define ESEL rover.tabs[rover.tab].esel
  106. #define FLAGS rover.tabs[rover.tab].flags
  107. #define CWD rover.tabs[rover.tab].cwd
  108. /* Helpers. */
  109. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  110. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  111. #define ISDIR(E) (strchr((E), '/') != NULL)
  112. /* Line Editing Macros. */
  113. #define EDIT_FULL(E) ((E).left == (E).right)
  114. #define EDIT_CAN_LEFT(E) ((E).left)
  115. #define EDIT_CAN_RIGHT(E) ((E).right < BUFLEN-1)
  116. #define EDIT_LEFT(E) (E).buffer[(E).right--] = (E).buffer[--(E).left]
  117. #define EDIT_RIGHT(E) (E).buffer[(E).left++] = (E).buffer[++(E).right]
  118. #define EDIT_INSERT(E, C) (E).buffer[(E).left++] = (C)
  119. #define EDIT_BACKSPACE(E) (E).left--
  120. #define EDIT_DELETE(E) (E).right++
  121. #define EDIT_CLEAR(E) do { (E).left = 0; (E).right = BUFLEN-1; } while(0)
  122. typedef enum EditStat {CONTINUE, CONFIRM, CANCEL} EditStat;
  123. typedef enum Color {DEFAULT, RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, WHITE, BLACK} Color;
  124. typedef int (*PROCESS)(const char *path);
  125. static void
  126. init_marks(Marks *marks)
  127. {
  128. strcpy(marks->dirpath, "");
  129. marks->bulk = BULK_INIT;
  130. marks->nentries = 0;
  131. marks->entries = calloc(marks->bulk, sizeof *marks->entries);
  132. }
  133. /* Unmark all entries. */
  134. static void
  135. mark_none(Marks *marks)
  136. {
  137. int i;
  138. strcpy(marks->dirpath, "");
  139. for (i = 0; i < marks->bulk && marks->nentries; i++)
  140. if (marks->entries[i]) {
  141. free(marks->entries[i]);
  142. marks->entries[i] = NULL;
  143. marks->nentries--;
  144. }
  145. if (marks->bulk > BULK_THRESH) {
  146. /* Reset bulk to free some memory. */
  147. free(marks->entries);
  148. marks->bulk = BULK_INIT;
  149. marks->entries = calloc(marks->bulk, sizeof *marks->entries);
  150. }
  151. }
  152. static void
  153. add_mark(Marks *marks, char *dirpath, char *entry)
  154. {
  155. int i;
  156. if (!strcmp(marks->dirpath, dirpath)) {
  157. /* Append mark to directory. */
  158. if (marks->nentries == marks->bulk) {
  159. /* Expand bulk to accomodate new entry. */
  160. int extra = marks->bulk / 2;
  161. marks->bulk += extra; /* bulk *= 1.5; */
  162. marks->entries = realloc(marks->entries,
  163. marks->bulk * sizeof *marks->entries);
  164. memset(&marks->entries[marks->nentries], 0,
  165. extra * sizeof *marks->entries);
  166. i = marks->nentries;
  167. } else {
  168. /* Search for empty slot (there must be one). */
  169. for (i = 0; i < marks->bulk; i++)
  170. if (!marks->entries[i])
  171. break;
  172. }
  173. } else {
  174. /* Directory changed. Discard old marks. */
  175. mark_none(marks);
  176. strcpy(marks->dirpath, dirpath);
  177. i = 0;
  178. }
  179. marks->entries[i] = malloc(strlen(entry) + 1);
  180. strcpy(marks->entries[i], entry);
  181. marks->nentries++;
  182. }
  183. static void
  184. del_mark(Marks *marks, char *entry)
  185. {
  186. int i;
  187. if (marks->nentries > 1) {
  188. for (i = 0; i < marks->bulk; i++)
  189. if (marks->entries[i] && !strcmp(marks->entries[i], entry))
  190. break;
  191. free(marks->entries[i]);
  192. marks->entries[i] = NULL;
  193. marks->nentries--;
  194. } else
  195. mark_none(marks);
  196. }
  197. static void
  198. free_marks(Marks *marks)
  199. {
  200. int i;
  201. for (i = 0; i < marks->bulk && marks->nentries; i++)
  202. if (marks->entries[i]) {
  203. free(marks->entries[i]);
  204. marks->nentries--;
  205. }
  206. free(marks->entries);
  207. }
  208. static void
  209. handle_usr1(int sig)
  210. {
  211. rover.pending_usr1 = 1;
  212. }
  213. static void
  214. handle_winch(int sig)
  215. {
  216. rover.pending_winch = 1;
  217. }
  218. static void
  219. enable_handlers()
  220. {
  221. struct sigaction sa;
  222. memset(&sa, 0, sizeof (struct sigaction));
  223. sa.sa_handler = handle_usr1;
  224. sigaction(SIGUSR1, &sa, NULL);
  225. sa.sa_handler = handle_winch;
  226. sigaction(SIGWINCH, &sa, NULL);
  227. }
  228. static void
  229. disable_handlers()
  230. {
  231. struct sigaction sa;
  232. memset(&sa, 0, sizeof (struct sigaction));
  233. sa.sa_handler = SIG_DFL;
  234. sigaction(SIGUSR1, &sa, NULL);
  235. sigaction(SIGWINCH, &sa, NULL);
  236. }
  237. static void reload();
  238. static void update_view();
  239. /* Handle any signals received since last call. */
  240. static void
  241. sync_signals()
  242. {
  243. if (rover.pending_usr1) {
  244. /* SIGUSR1 received: refresh directory listing. */
  245. reload();
  246. rover.pending_usr1 = 0;
  247. }
  248. if (rover.pending_winch) {
  249. /* SIGWINCH received: resize application accordingly. */
  250. delwin(rover.window);
  251. endwin();
  252. refresh();
  253. clear();
  254. rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
  255. if (HEIGHT < rover.nfiles && SCROLL + HEIGHT > rover.nfiles)
  256. SCROLL = ESEL - HEIGHT;
  257. update_view();
  258. rover.pending_winch = 0;
  259. }
  260. }
  261. /* This function must be used in place of getch().
  262. It handles signals while waiting for user input. */
  263. static int
  264. rover_getch()
  265. {
  266. int ch;
  267. while ((ch = getch()) == ERR)
  268. sync_signals();
  269. return ch;
  270. }
  271. /* This function must be used in place of get_wch().
  272. It handles signals while waiting for user input. */
  273. static int
  274. rover_get_wch(wint_t *wch)
  275. {
  276. wint_t ret;
  277. while ((ret = get_wch(wch)) == (wint_t) ERR)
  278. sync_signals();
  279. return ret;
  280. }
  281. /* Get user programs from the environment. */
  282. #define ROVER_ENV(dst, src) if ((dst = getenv("ROVER_" #src)) == NULL) \
  283. dst = getenv(#src);
  284. static void
  285. get_user_programs()
  286. {
  287. ROVER_ENV(user_shell, SHELL)
  288. ROVER_ENV(user_pager, PAGER)
  289. ROVER_ENV(user_editor, VISUAL)
  290. if (!user_editor)
  291. ROVER_ENV(user_editor, EDITOR)
  292. ROVER_ENV(user_open, OPEN)
  293. }
  294. /* Do a fork-exec to external program (e.g. $EDITOR). */
  295. static void
  296. spawn(char **args)
  297. {
  298. pid_t pid;
  299. int status;
  300. setenv("RVSEL", rover.nfiles ? ENAME(ESEL) : "", 1);
  301. pid = fork();
  302. if (pid > 0) {
  303. /* fork() succeeded. */
  304. disable_handlers();
  305. endwin();
  306. waitpid(pid, &status, 0);
  307. enable_handlers();
  308. kill(getpid(), SIGWINCH);
  309. } else if (pid == 0) {
  310. /* Child process. */
  311. execvp(args[0], args);
  312. }
  313. }
  314. static void
  315. shell_escaped_cat(char *buf, char *str, size_t n)
  316. {
  317. char *p = buf + strlen(buf);
  318. *p++ = '\'';
  319. for (n--; n; n--, str++) {
  320. switch (*str) {
  321. case '\'':
  322. if (n < 4)
  323. goto done;
  324. strcpy(p, "'\\''");
  325. n -= 4;
  326. p += 4;
  327. break;
  328. case '\0':
  329. goto done;
  330. default:
  331. *p = *str;
  332. p++;
  333. }
  334. }
  335. done:
  336. strncat(p, "'", n);
  337. }
  338. static int
  339. open_with_env(char *program, char *path)
  340. {
  341. if (program) {
  342. #ifdef RV_SHELL
  343. strncpy(BUF1, program, BUFLEN - 1);
  344. strncat(BUF1, " ", BUFLEN - strlen(program) - 1);
  345. shell_escaped_cat(BUF1, path, BUFLEN - strlen(program) - 2);
  346. spawn((char *[]) {RV_SHELL, "-c", BUF1, NULL});
  347. #else
  348. spawn((char *[]) {program, path, NULL});
  349. #endif
  350. return 1;
  351. }
  352. return 0;
  353. }
  354. /* Curses setup. */
  355. static void
  356. init_term()
  357. {
  358. setlocale(LC_ALL, "");
  359. initscr();
  360. cbreak(); /* Get one character at a time. */
  361. timeout(100); /* For getch(). */
  362. noecho();
  363. nonl(); /* No NL->CR/NL on output. */
  364. intrflush(stdscr, FALSE);
  365. keypad(stdscr, TRUE);
  366. curs_set(FALSE); /* Hide blinking cursor. */
  367. if (has_colors()) {
  368. short bg;
  369. start_color();
  370. #ifdef NCURSES_EXT_FUNCS
  371. use_default_colors();
  372. bg = -1;
  373. #else
  374. bg = COLOR_BLACK;
  375. #endif
  376. init_pair(RED, COLOR_RED, bg);
  377. init_pair(GREEN, COLOR_GREEN, bg);
  378. init_pair(YELLOW, COLOR_YELLOW, bg);
  379. init_pair(BLUE, COLOR_BLUE, bg);
  380. init_pair(CYAN, COLOR_CYAN, bg);
  381. init_pair(MAGENTA, COLOR_MAGENTA, bg);
  382. init_pair(WHITE, COLOR_WHITE, bg);
  383. init_pair(BLACK, COLOR_BLACK, bg);
  384. }
  385. atexit((void (*)(void)) endwin);
  386. enable_handlers();
  387. }
  388. /* Update the listing view. */
  389. static void
  390. update_view()
  391. {
  392. int i, j;
  393. int numsize;
  394. int ishidden;
  395. int marking;
  396. mvhline(0, 0, ' ', COLS);
  397. attr_on(A_BOLD, NULL);
  398. color_set(RVC_TABNUM, NULL);
  399. mvaddch(0, COLS - 2, rover.tab + '0');
  400. attr_off(A_BOLD, NULL);
  401. if (rover.marks.nentries) {
  402. numsize = snprintf(BUF1, BUFLEN, "%d", rover.marks.nentries);
  403. color_set(RVC_MARKS, NULL);
  404. mvaddstr(0, COLS - 3 - numsize, BUF1);
  405. } else
  406. numsize = -1;
  407. color_set(RVC_CWD, NULL);
  408. mbstowcs(WBUF, CWD, PATH_MAX);
  409. mvaddnwstr(0, 0, WBUF, COLS - 4 - numsize);
  410. wcolor_set(rover.window, RVC_BORDER, NULL);
  411. wborder(rover.window, 0, 0, 0, 0, 0, 0, 0, 0);
  412. ESEL = MAX(MIN(ESEL, rover.nfiles - 1), 0);
  413. /* Selection might not be visible, due to cursor wrapping or window
  414. shrinking. In that case, the scroll must be moved to make it visible. */
  415. if (rover.nfiles > HEIGHT) {
  416. SCROLL = MAX(MIN(SCROLL, ESEL), ESEL - HEIGHT + 1);
  417. SCROLL = MIN(MAX(SCROLL, 0), rover.nfiles - HEIGHT);
  418. } else
  419. SCROLL = 0;
  420. marking = !strcmp(CWD, rover.marks.dirpath);
  421. for (i = 0, j = SCROLL; i < HEIGHT && j < rover.nfiles; i++, j++) {
  422. ishidden = ENAME(j)[0] == '.';
  423. if (j == ESEL)
  424. wattr_on(rover.window, A_REVERSE, NULL);
  425. if (ISLINK(j))
  426. wcolor_set(rover.window, RVC_LINK, NULL);
  427. else if (ishidden)
  428. wcolor_set(rover.window, RVC_HIDDEN, NULL);
  429. else if (S_ISREG(EMODE(j))) {
  430. if (EMODE(j) & (S_IXUSR | S_IXGRP | S_IXOTH))
  431. wcolor_set(rover.window, RVC_EXEC, NULL);
  432. else
  433. wcolor_set(rover.window, RVC_REG, NULL);
  434. } else if (S_ISDIR(EMODE(j)))
  435. wcolor_set(rover.window, RVC_DIR, NULL);
  436. else if (S_ISCHR(EMODE(j)))
  437. wcolor_set(rover.window, RVC_CHR, NULL);
  438. else if (S_ISBLK(EMODE(j)))
  439. wcolor_set(rover.window, RVC_BLK, NULL);
  440. else if (S_ISFIFO(EMODE(j)))
  441. wcolor_set(rover.window, RVC_FIFO, NULL);
  442. else if (S_ISSOCK(EMODE(j)))
  443. wcolor_set(rover.window, RVC_SOCK, NULL);
  444. if (S_ISDIR(EMODE(j))) {
  445. mbstowcs(WBUF, ENAME(j), PATH_MAX);
  446. if (ISLINK(j))
  447. wcscat(WBUF, L"/");
  448. } else {
  449. char *suffix, *suffixes = "BKMGTPEZY";
  450. off_t human_size = ESIZE(j) * 10;
  451. int length = mbstowcs(NULL, ENAME(j), 0);
  452. for (suffix = suffixes; human_size >= 10240; suffix++)
  453. human_size = (human_size + 512) / 1024;
  454. if (*suffix == 'B')
  455. swprintf(WBUF, PATH_MAX, L"%s%*d %c", ENAME(j),
  456. (int) (COLS - length - 6),
  457. (int) human_size / 10, *suffix);
  458. else
  459. swprintf(WBUF, PATH_MAX, L"%s%*d.%d %c", ENAME(j),
  460. (int) (COLS - length - 8),
  461. (int) human_size / 10, (int) human_size % 10, *suffix);
  462. }
  463. mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
  464. mvwaddnwstr(rover.window, i + 1, 2, WBUF, COLS - 4);
  465. if (marking && MARKED(j)) {
  466. wcolor_set(rover.window, RVC_MARKS, NULL);
  467. mvwaddch(rover.window, i + 1, 1, RVS_MARK);
  468. } else
  469. mvwaddch(rover.window, i + 1, 1, ' ');
  470. if (j == ESEL)
  471. wattr_off(rover.window, A_REVERSE, NULL);
  472. }
  473. for (; i < HEIGHT; i++)
  474. mvwhline(rover.window, i + 1, 1, ' ', COLS - 2);
  475. if (rover.nfiles > HEIGHT) {
  476. int center, height;
  477. center = (SCROLL + HEIGHT / 2) * HEIGHT / rover.nfiles;
  478. height = (HEIGHT-1) * HEIGHT / rover.nfiles;
  479. if (!height) height = 1;
  480. wcolor_set(rover.window, RVC_SCROLLBAR, NULL);
  481. mvwvline(rover.window, center-height/2+1, COLS-1, RVS_SCROLLBAR, height);
  482. }
  483. BUF1[0] = FLAGS & SHOW_FILES ? 'F' : ' ';
  484. BUF1[1] = FLAGS & SHOW_DIRS ? 'D' : ' ';
  485. BUF1[2] = FLAGS & SHOW_HIDDEN ? 'H' : ' ';
  486. if (!rover.nfiles)
  487. strcpy(BUF2, "0/0");
  488. else
  489. snprintf(BUF2, BUFLEN, "%d/%d", ESEL + 1, rover.nfiles);
  490. snprintf(BUF1+3, BUFLEN-3, "%12s", BUF2);
  491. color_set(RVC_STATUS, NULL);
  492. mvaddstr(LINES - 1, STATUSPOS, BUF1);
  493. wrefresh(rover.window);
  494. }
  495. /* Show a message on the status bar. */
  496. static void
  497. message(Color color, char *fmt, ...)
  498. {
  499. int len, pos;
  500. va_list args;
  501. va_start(args, fmt);
  502. vsnprintf(BUF1, MIN(BUFLEN, STATUSPOS), fmt, args);
  503. va_end(args);
  504. len = strlen(BUF1);
  505. pos = (STATUSPOS - len) / 2;
  506. attr_on(A_BOLD, NULL);
  507. color_set(color, NULL);
  508. mvaddstr(LINES - 1, pos, BUF1);
  509. color_set(DEFAULT, NULL);
  510. attr_off(A_BOLD, NULL);
  511. }
  512. /* Clear message area, leaving only status info. */
  513. static void
  514. clear_message()
  515. {
  516. mvhline(LINES - 1, 0, ' ', STATUSPOS);
  517. }
  518. /* Comparison used to sort listing entries. */
  519. static int
  520. rowcmp(const void *a, const void *b)
  521. {
  522. int isdir1, isdir2, cmpdir;
  523. const Row *r1 = a;
  524. const Row *r2 = b;
  525. isdir1 = S_ISDIR(r1->mode);
  526. isdir2 = S_ISDIR(r2->mode);
  527. cmpdir = isdir2 - isdir1;
  528. return cmpdir ? cmpdir : strcoll(r1->name, r2->name);
  529. }
  530. /* Get all entries in current working directory. */
  531. static int
  532. ls(Row **rowsp, uint8_t flags)
  533. {
  534. DIR *dp;
  535. struct dirent *ep;
  536. struct stat statbuf;
  537. Row *rows;
  538. int i, n;
  539. if(!(dp = opendir("."))) return -1;
  540. n = -2; /* We don't want the entries "." and "..". */
  541. while (readdir(dp)) n++;
  542. rewinddir(dp);
  543. rows = malloc(n * sizeof *rows);
  544. i = 0;
  545. while ((ep = readdir(dp))) {
  546. if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
  547. continue;
  548. if (!(flags & SHOW_HIDDEN) && ep->d_name[0] == '.')
  549. continue;
  550. lstat(ep->d_name, &statbuf);
  551. rows[i].islink = S_ISLNK(statbuf.st_mode);
  552. stat(ep->d_name, &statbuf);
  553. if (S_ISDIR(statbuf.st_mode)) {
  554. if (flags & SHOW_DIRS) {
  555. rows[i].name = malloc(strlen(ep->d_name) + 2);
  556. strcpy(rows[i].name, ep->d_name);
  557. if (!rows[i].islink)
  558. strcat(rows[i].name, "/");
  559. rows[i].mode = statbuf.st_mode;
  560. i++;
  561. }
  562. } else if (flags & SHOW_FILES) {
  563. rows[i].name = malloc(strlen(ep->d_name) + 1);
  564. strcpy(rows[i].name, ep->d_name);
  565. rows[i].size = statbuf.st_size;
  566. rows[i].mode = statbuf.st_mode;
  567. i++;
  568. }
  569. }
  570. n = i; /* Ignore unused space in array caused by filters. */
  571. qsort(rows, n, sizeof (*rows), rowcmp);
  572. closedir(dp);
  573. *rowsp = rows;
  574. return n;
  575. }
  576. static void
  577. free_rows(Row **rowsp, int nfiles)
  578. {
  579. int i;
  580. for (i = 0; i < nfiles; i++)
  581. free((*rowsp)[i].name);
  582. free(*rowsp);
  583. *rowsp = NULL;
  584. }
  585. /* Change working directory to the path in CWD. */
  586. static void
  587. cd(int reset)
  588. {
  589. int i, j;
  590. message(CYAN, "Loading \"%s\"...", CWD);
  591. refresh();
  592. if (chdir(CWD) == -1) {
  593. getcwd(CWD, PATH_MAX-1);
  594. if (CWD[strlen(CWD)-1] != '/')
  595. strcat(CWD, "/");
  596. goto done;
  597. }
  598. if (reset) ESEL = SCROLL = 0;
  599. if (rover.nfiles)
  600. free_rows(&rover.rows, rover.nfiles);
  601. rover.nfiles = ls(&rover.rows, FLAGS);
  602. if (!strcmp(CWD, rover.marks.dirpath)) {
  603. for (i = 0; i < rover.nfiles; i++) {
  604. for (j = 0; j < rover.marks.bulk; j++)
  605. if (
  606. rover.marks.entries[j] &&
  607. !strcmp(rover.marks.entries[j], ENAME(i))
  608. )
  609. break;
  610. MARKED(i) = j < rover.marks.bulk;
  611. }
  612. } else
  613. for (i = 0; i < rover.nfiles; i++)
  614. MARKED(i) = 0;
  615. done:
  616. clear_message();
  617. update_view();
  618. }
  619. /* Select a target entry, if it is present. */
  620. static void
  621. try_to_sel(const char *target)
  622. {
  623. ESEL = 0;
  624. if (!ISDIR(target))
  625. while ((ESEL+1) < rover.nfiles && S_ISDIR(EMODE(ESEL)))
  626. ESEL++;
  627. while ((ESEL+1) < rover.nfiles && strcoll(ENAME(ESEL), target) < 0)
  628. ESEL++;
  629. }
  630. /* Reload CWD, but try to keep selection. */
  631. static void
  632. reload()
  633. {
  634. if (rover.nfiles) {
  635. strcpy(INPUT, ENAME(ESEL));
  636. cd(0);
  637. try_to_sel(INPUT);
  638. update_view();
  639. } else
  640. cd(1);
  641. }
  642. static off_t
  643. count_dir(const char *path)
  644. {
  645. DIR *dp;
  646. struct dirent *ep;
  647. struct stat statbuf;
  648. char subpath[PATH_MAX];
  649. off_t total;
  650. if(!(dp = opendir(path))) return 0;
  651. total = 0;
  652. while ((ep = readdir(dp))) {
  653. if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
  654. continue;
  655. snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
  656. lstat(subpath, &statbuf);
  657. if (S_ISDIR(statbuf.st_mode)) {
  658. strcat(subpath, "/");
  659. total += count_dir(subpath);
  660. } else
  661. total += statbuf.st_size;
  662. }
  663. closedir(dp);
  664. return total;
  665. }
  666. static off_t
  667. count_marked()
  668. {
  669. int i;
  670. char *entry;
  671. off_t total;
  672. struct stat statbuf;
  673. total = 0;
  674. chdir(rover.marks.dirpath);
  675. for (i = 0; i < rover.marks.bulk; i++) {
  676. entry = rover.marks.entries[i];
  677. if (entry) {
  678. if (ISDIR(entry)) {
  679. total += count_dir(entry);
  680. } else {
  681. lstat(entry, &statbuf);
  682. total += statbuf.st_size;
  683. }
  684. }
  685. }
  686. chdir(CWD);
  687. return total;
  688. }
  689. /* Recursively process a source directory using CWD as destination root.
  690. For each node (i.e. directory), do the following:
  691. 1. call pre(destination);
  692. 2. call proc() on every child leaf (i.e. files);
  693. 3. recurse into every child node;
  694. 4. call pos(source).
  695. E.g. to move directory /src/ (and all its contents) inside /dst/:
  696. strcpy(CWD, "/dst/");
  697. process_dir(adddir, movfile, deldir, "/src/"); */
  698. static int
  699. process_dir(PROCESS pre, PROCESS proc, PROCESS pos, const char *path)
  700. {
  701. int ret;
  702. DIR *dp;
  703. struct dirent *ep;
  704. struct stat statbuf;
  705. char subpath[PATH_MAX];
  706. ret = 0;
  707. if (pre) {
  708. char dstpath[PATH_MAX];
  709. strcpy(dstpath, CWD);
  710. strcat(dstpath, path + strlen(rover.marks.dirpath));
  711. ret |= pre(dstpath);
  712. }
  713. if(!(dp = opendir(path))) return -1;
  714. while ((ep = readdir(dp))) {
  715. if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
  716. continue;
  717. snprintf(subpath, PATH_MAX, "%s%s", path, ep->d_name);
  718. lstat(subpath, &statbuf);
  719. if (S_ISDIR(statbuf.st_mode)) {
  720. strcat(subpath, "/");
  721. ret |= process_dir(pre, proc, pos, subpath);
  722. } else
  723. ret |= proc(subpath);
  724. }
  725. closedir(dp);
  726. if (pos) ret |= pos(path);
  727. return ret;
  728. }
  729. /* Process all marked entries using CWD as destination root.
  730. All marked entries that are directories will be recursively processed.
  731. See process_dir() for details on the parameters. */
  732. static void
  733. process_marked(PROCESS pre, PROCESS proc, PROCESS pos,
  734. const char *msg_doing, const char *msg_done)
  735. {
  736. int i, ret;
  737. char *entry;
  738. char path[PATH_MAX];
  739. clear_message();
  740. message(CYAN, "%s...", msg_doing);
  741. refresh();
  742. rover.prog = (Prog) {0, count_marked(), msg_doing};
  743. for (i = 0; i < rover.marks.bulk; i++) {
  744. entry = rover.marks.entries[i];
  745. if (entry) {
  746. ret = 0;
  747. snprintf(path, PATH_MAX, "%s%s", rover.marks.dirpath, entry);
  748. if (ISDIR(entry)) {
  749. if (!strncmp(path, CWD, strlen(path)))
  750. ret = -1;
  751. else
  752. ret = process_dir(pre, proc, pos, path);
  753. } else
  754. ret = proc(path);
  755. if (!ret) {
  756. del_mark(&rover.marks, entry);
  757. reload();
  758. }
  759. }
  760. }
  761. rover.prog.total = 0;
  762. reload();
  763. if (!rover.marks.nentries)
  764. message(GREEN, "%s all marked entries.", msg_done);
  765. else
  766. message(RED, "Some errors occured while %s.", msg_doing);
  767. RV_ALERT();
  768. }
  769. static void
  770. update_progress(off_t delta)
  771. {
  772. int percent;
  773. if (!rover.prog.total) return;
  774. rover.prog.partial += delta;
  775. percent = (int) (rover.prog.partial * 100 / rover.prog.total);
  776. message(CYAN, "%s...%d%%", rover.prog.msg, percent);
  777. refresh();
  778. }
  779. /* Wrappers for file operations. */
  780. static int delfile(const char *path) {
  781. int ret;
  782. struct stat st;
  783. ret = lstat(path, &st);
  784. if (ret < 0) return ret;
  785. update_progress(st.st_size);
  786. return unlink(path);
  787. }
  788. static PROCESS deldir = rmdir;
  789. static int addfile(const char *path) {
  790. /* Using creat(2) because mknod(2) doesn't seem to be portable. */
  791. int ret;
  792. ret = creat(path, 0644);
  793. if (ret < 0) return ret;
  794. return close(ret);
  795. }
  796. static int cpyfile(const char *srcpath) {
  797. int src, dst, ret;
  798. size_t size;
  799. struct stat st;
  800. char buf[BUFSIZ];
  801. char dstpath[PATH_MAX];
  802. strcpy(dstpath, CWD);
  803. strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
  804. ret = lstat(srcpath, &st);
  805. if (ret < 0) return ret;
  806. if (S_ISLNK(st.st_mode)) {
  807. ret = readlink(srcpath, BUF1, BUFLEN-1);
  808. if (ret < 0) return ret;
  809. BUF1[ret] = '\0';
  810. ret = symlink(BUF1, dstpath);
  811. } else {
  812. ret = src = open(srcpath, O_RDONLY);
  813. if (ret < 0) return ret;
  814. ret = dst = creat(dstpath, st.st_mode);
  815. if (ret < 0) return ret;
  816. while ((size = read(src, buf, BUFSIZ)) > 0) {
  817. write(dst, buf, size);
  818. update_progress(size);
  819. sync_signals();
  820. }
  821. close(src);
  822. close(dst);
  823. ret = 0;
  824. }
  825. return ret;
  826. }
  827. static int adddir(const char *path) {
  828. int ret;
  829. struct stat st;
  830. ret = stat(CWD, &st);
  831. if (ret < 0) return ret;
  832. return mkdir(path, st.st_mode);
  833. }
  834. static int movfile(const char *srcpath) {
  835. int ret;
  836. struct stat st;
  837. char dstpath[PATH_MAX];
  838. strcpy(dstpath, CWD);
  839. strcat(dstpath, srcpath + strlen(rover.marks.dirpath));
  840. ret = rename(srcpath, dstpath);
  841. if (ret == 0) {
  842. ret = lstat(dstpath, &st);
  843. if (ret < 0) return ret;
  844. update_progress(st.st_size);
  845. } else if (errno == EXDEV) {
  846. ret = cpyfile(srcpath);
  847. if (ret < 0) return ret;
  848. ret = unlink(srcpath);
  849. }
  850. return ret;
  851. }
  852. static void
  853. start_line_edit(const char *init_input)
  854. {
  855. curs_set(TRUE);
  856. strncpy(INPUT, init_input, BUFLEN);
  857. rover.edit.left = mbstowcs(rover.edit.buffer, init_input, BUFLEN);
  858. rover.edit.right = BUFLEN - 1;
  859. rover.edit.buffer[BUFLEN] = L'\0';
  860. rover.edit_scroll = 0;
  861. }
  862. /* Read input and change editing state accordingly. */
  863. static EditStat
  864. get_line_edit()
  865. {
  866. wchar_t eraser, killer, wch;
  867. int ret, length;
  868. ret = rover_get_wch((wint_t *) &wch);
  869. erasewchar(&eraser);
  870. killwchar(&killer);
  871. if (ret == KEY_CODE_YES) {
  872. if (wch == KEY_ENTER) {
  873. curs_set(FALSE);
  874. return CONFIRM;
  875. } else if (wch == KEY_LEFT) {
  876. if (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
  877. } else if (wch == KEY_RIGHT) {
  878. if (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
  879. } else if (wch == KEY_UP) {
  880. while (EDIT_CAN_LEFT(rover.edit)) EDIT_LEFT(rover.edit);
  881. } else if (wch == KEY_DOWN) {
  882. while (EDIT_CAN_RIGHT(rover.edit)) EDIT_RIGHT(rover.edit);
  883. } else if (wch == KEY_BACKSPACE) {
  884. if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
  885. } else if (wch == KEY_DC) {
  886. if (EDIT_CAN_RIGHT(rover.edit)) EDIT_DELETE(rover.edit);
  887. }
  888. } else {
  889. if (wch == L'\r' || wch == L'\n') {
  890. curs_set(FALSE);
  891. return CONFIRM;
  892. } else if (wch == L'\t') {
  893. curs_set(FALSE);
  894. return CANCEL;
  895. } else if (wch == eraser) {
  896. if (EDIT_CAN_LEFT(rover.edit)) EDIT_BACKSPACE(rover.edit);
  897. } else if (wch == killer) {
  898. EDIT_CLEAR(rover.edit);
  899. clear_message();
  900. } else if (iswprint(wch)) {
  901. if (!EDIT_FULL(rover.edit)) EDIT_INSERT(rover.edit, wch);
  902. }
  903. }
  904. /* Encode edit contents in INPUT. */
  905. rover.edit.buffer[rover.edit.left] = L'\0';
  906. length = wcstombs(INPUT, rover.edit.buffer, BUFLEN);
  907. wcstombs(&INPUT[length], &rover.edit.buffer[rover.edit.right+1],
  908. BUFLEN-length);
  909. return CONTINUE;
  910. }
  911. /* Update line input on the screen. */
  912. static void
  913. update_input(const char *prompt, Color color)
  914. {
  915. int plen, ilen, maxlen;
  916. plen = strlen(prompt);
  917. ilen = mbstowcs(NULL, INPUT, 0);
  918. maxlen = STATUSPOS - plen - 2;
  919. if (ilen - rover.edit_scroll < maxlen)
  920. rover.edit_scroll = MAX(ilen - maxlen, 0);
  921. else if (rover.edit.left > rover.edit_scroll + maxlen - 1)
  922. rover.edit_scroll = rover.edit.left - maxlen;
  923. else if (rover.edit.left < rover.edit_scroll)
  924. rover.edit_scroll = MAX(rover.edit.left - maxlen, 0);
  925. color_set(RVC_PROMPT, NULL);
  926. mvaddstr(LINES - 1, 0, prompt);
  927. color_set(color, NULL);
  928. mbstowcs(WBUF, INPUT, COLS);
  929. mvaddnwstr(LINES - 1, plen, &WBUF[rover.edit_scroll], maxlen);
  930. mvaddch(LINES - 1, plen + MIN(ilen - rover.edit_scroll, maxlen + 1), ' ');
  931. color_set(DEFAULT, NULL);
  932. if (rover.edit_scroll)
  933. mvaddch(LINES - 1, plen - 1, '<');
  934. if (ilen > rover.edit_scroll + maxlen)
  935. mvaddch(LINES - 1, plen + maxlen, '>');
  936. move(LINES - 1, plen + rover.edit.left - rover.edit_scroll);
  937. }
  938. int
  939. main(int argc, char *argv[])
  940. {
  941. int i, ch;
  942. char *program;
  943. char *entry;
  944. const char *key;
  945. const char *clip_path;
  946. DIR *d;
  947. EditStat edit_stat;
  948. FILE *save_cwd_file = NULL;
  949. FILE *save_marks_file = NULL;
  950. FILE *clip_file;
  951. if (argc >= 2) {
  952. if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
  953. printf("rover %s\n", RV_VERSION);
  954. return 0;
  955. } else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
  956. printf(
  957. "Usage: rover [OPTIONS] [DIR [DIR [...]]]\n"
  958. " Browse current directory or the ones specified.\n\n"
  959. " or: rover -h|--help\n"
  960. " Print this help message and exit.\n\n"
  961. " or: rover -v|--version\n"
  962. " Print program version and exit.\n\n"
  963. "See rover(1) for more information.\n"
  964. "Rover homepage: <https://github.com/lecram/rover>.\n"
  965. );
  966. return 0;
  967. } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--save-cwd")) {
  968. if (argc > 2) {
  969. save_cwd_file = fopen(argv[2], "w");
  970. argc -= 2; argv += 2;
  971. } else {
  972. fprintf(stderr, "error: missing argument to %s\n", argv[1]);
  973. return 1;
  974. }
  975. } else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--save-marks")) {
  976. if (argc > 2) {
  977. save_marks_file = fopen(argv[2], "a");
  978. argc -= 2; argv += 2;
  979. } else {
  980. fprintf(stderr, "error: missing argument to %s\n", argv[1]);
  981. return 1;
  982. }
  983. }
  984. }
  985. get_user_programs();
  986. init_term();
  987. rover.nfiles = 0;
  988. for (i = 0; i < 10; i++) {
  989. rover.tabs[i].esel = rover.tabs[i].scroll = 0;
  990. rover.tabs[i].flags = RV_FLAGS;
  991. }
  992. strcpy(rover.tabs[0].cwd, getenv("HOME"));
  993. for (i = 1; i < argc && i < 10; i++) {
  994. if ((d = opendir(argv[i]))) {
  995. realpath(argv[i], rover.tabs[i].cwd);
  996. closedir(d);
  997. } else
  998. strcpy(rover.tabs[i].cwd, rover.tabs[0].cwd);
  999. }
  1000. getcwd(rover.tabs[i].cwd, PATH_MAX);
  1001. for (i++; i < 10; i++)
  1002. strcpy(rover.tabs[i].cwd, rover.tabs[i-1].cwd);
  1003. for (i = 0; i < 10; i++)
  1004. if (rover.tabs[i].cwd[strlen(rover.tabs[i].cwd) - 1] != '/')
  1005. strcat(rover.tabs[i].cwd, "/");
  1006. rover.tab = 1;
  1007. rover.window = subwin(stdscr, LINES - 2, COLS, 1, 0);
  1008. init_marks(&rover.marks);
  1009. cd(1);
  1010. strcpy(CLIPBOARD, CWD);
  1011. strcat(CLIPBOARD, ENAME(ESEL));
  1012. while (1) {
  1013. ch = rover_getch();
  1014. key = keyname(ch);
  1015. clear_message();
  1016. if (!strcmp(key, RVK_QUIT)) break;
  1017. else if (ch >= '0' && ch <= '9') {
  1018. rover.tab = ch - '0';
  1019. cd(0);
  1020. } else if (!strcmp(key, RVK_HELP)) {
  1021. spawn((char *[]) {"man", "rover", NULL});
  1022. } else if (!strcmp(key, RVK_DOWN)) {
  1023. if (!rover.nfiles) continue;
  1024. ESEL = MIN(ESEL + 1, rover.nfiles - 1);
  1025. update_view();
  1026. } else if (!strcmp(key, RVK_UP)) {
  1027. if (!rover.nfiles) continue;
  1028. ESEL = MAX(ESEL - 1, 0);
  1029. update_view();
  1030. } else if (!strcmp(key, RVK_JUMP_DOWN)) {
  1031. if (!rover.nfiles) continue;
  1032. ESEL = MIN(ESEL + RV_JUMP, rover.nfiles - 1);
  1033. if (rover.nfiles > HEIGHT)
  1034. SCROLL = MIN(SCROLL + RV_JUMP, rover.nfiles - HEIGHT);
  1035. update_view();
  1036. } else if (!strcmp(key, RVK_JUMP_UP)) {
  1037. if (!rover.nfiles) continue;
  1038. ESEL = MAX(ESEL - RV_JUMP, 0);
  1039. SCROLL = MAX(SCROLL - RV_JUMP, 0);
  1040. update_view();
  1041. } else if (!strcmp(key, RVK_JUMP_TOP)) {
  1042. if (!rover.nfiles) continue;
  1043. ESEL = 0;
  1044. update_view();
  1045. } else if (!strcmp(key, RVK_JUMP_BOTTOM)) {
  1046. if (!rover.nfiles) continue;
  1047. ESEL = rover.nfiles - 1;
  1048. update_view();
  1049. } else if (!strcmp(key, RVK_CD_DOWN)) {
  1050. if (!rover.nfiles || !S_ISDIR(EMODE(ESEL))) continue;
  1051. if (chdir(ENAME(ESEL)) == -1) {
  1052. message(RED, "Cannot access \"%s\".", ENAME(ESEL));
  1053. continue;
  1054. }
  1055. strcat(CWD, ENAME(ESEL));
  1056. cd(1);
  1057. } else if (!strcmp(key, RVK_CD_UP)) {
  1058. char *dirname, first;
  1059. if (!strcmp(CWD, "/")) continue;
  1060. CWD[strlen(CWD) - 1] = '\0';
  1061. dirname = strrchr(CWD, '/') + 1;
  1062. first = dirname[0];
  1063. dirname[0] = '\0';
  1064. cd(1);
  1065. dirname[0] = first;
  1066. dirname[strlen(dirname)] = '/';
  1067. try_to_sel(dirname);
  1068. dirname[0] = '\0';
  1069. if (rover.nfiles > HEIGHT)
  1070. SCROLL = ESEL - HEIGHT / 2;
  1071. update_view();
  1072. } else if (!strcmp(key, RVK_HOME)) {
  1073. strcpy(CWD, getenv("HOME"));
  1074. if (CWD[strlen(CWD) - 1] != '/')
  1075. strcat(CWD, "/");
  1076. cd(1);
  1077. } else if (!strcmp(key, RVK_TARGET)) {
  1078. char *bname, first;
  1079. int is_dir = S_ISDIR(EMODE(ESEL));
  1080. ssize_t len = readlink(ENAME(ESEL), BUF1, BUFLEN-1);
  1081. if (len == -1) continue;
  1082. BUF1[len] = '\0';
  1083. if (access(BUF1, F_OK) == -1) {
  1084. char *msg;
  1085. switch (errno) {
  1086. case EACCES:
  1087. msg = "Cannot access \"%s\".";
  1088. break;
  1089. case ENOENT:
  1090. msg = "\"%s\" does not exist.";
  1091. break;
  1092. default:
  1093. msg = "Cannot navigate to \"%s\".";
  1094. }
  1095. strcpy(BUF2, BUF1); /* message() uses BUF1. */
  1096. message(RED, msg, BUF2);
  1097. continue;
  1098. }
  1099. realpath(BUF1, CWD);
  1100. len = strlen(CWD);
  1101. if (CWD[len - 1] == '/')
  1102. CWD[len - 1] = '\0';
  1103. bname = strrchr(CWD, '/') + 1;
  1104. first = *bname;
  1105. *bname = '\0';
  1106. cd(1);
  1107. *bname = first;
  1108. if (is_dir)
  1109. strcat(CWD, "/");
  1110. try_to_sel(bname);
  1111. *bname = '\0';
  1112. update_view();
  1113. } else if (!strcmp(key, RVK_COPY_PATH)) {
  1114. clip_path = getenv("CLIP");
  1115. if (!clip_path) goto copy_path_fail;
  1116. clip_file = fopen(clip_path, "w");
  1117. if (!clip_file) goto copy_path_fail;
  1118. fprintf(clip_file, "%s%s\n", CWD, ENAME(ESEL));
  1119. fclose(clip_file);
  1120. goto copy_path_done;
  1121. copy_path_fail:
  1122. strcpy(CLIPBOARD, CWD);
  1123. strcat(CLIPBOARD, ENAME(ESEL));
  1124. copy_path_done:
  1125. ;
  1126. } else if (!strcmp(key, RVK_PASTE_PATH)) {
  1127. clip_path = getenv("CLIP");
  1128. if (!clip_path) goto paste_path_fail;
  1129. clip_file = fopen(clip_path, "r");
  1130. if (!clip_file) goto paste_path_fail;
  1131. fscanf(clip_file, "%s\n", CLIPBOARD);
  1132. fclose(clip_file);
  1133. paste_path_fail:
  1134. strcpy(BUF1, CLIPBOARD);
  1135. strcpy(CWD, dirname(BUF1));
  1136. if (strcmp(CWD, "/"))
  1137. strcat(CWD, "/");
  1138. cd(1);
  1139. strcpy(BUF1, CLIPBOARD);
  1140. try_to_sel(strstr(CLIPBOARD, basename(BUF1)));
  1141. update_view();
  1142. } else if (!strcmp(key, RVK_REFRESH)) {
  1143. reload();
  1144. } else if (!strcmp(key, RVK_SHELL)) {
  1145. program = user_shell;
  1146. if (program) {
  1147. #ifdef RV_SHELL
  1148. spawn((char *[]) {RV_SHELL, "-c", program, NULL});
  1149. #else
  1150. spawn((char *[]) {program, NULL});
  1151. #endif
  1152. reload();
  1153. }
  1154. } else if (!strcmp(key, RVK_VIEW)) {
  1155. if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
  1156. if (open_with_env(user_pager, ENAME(ESEL)))
  1157. cd(0);
  1158. } else if (!strcmp(key, RVK_EDIT)) {
  1159. if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
  1160. if (open_with_env(user_editor, ENAME(ESEL)))
  1161. cd(0);
  1162. } else if (!strcmp(key, RVK_OPEN)) {
  1163. if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
  1164. if (open_with_env(user_open, ENAME(ESEL)))
  1165. cd(0);
  1166. } else if (!strcmp(key, RVK_SEARCH)) {
  1167. int oldsel, oldscroll, length;
  1168. if (!rover.nfiles) continue;
  1169. oldsel = ESEL;
  1170. oldscroll = SCROLL;
  1171. start_line_edit("");
  1172. update_input(RVP_SEARCH, RED);
  1173. while ((edit_stat = get_line_edit()) == CONTINUE) {
  1174. int sel;
  1175. Color color = RED;
  1176. length = strlen(INPUT);
  1177. if (length) {
  1178. for (sel = 0; sel < rover.nfiles; sel++)
  1179. if (!strncmp(ENAME(sel), INPUT, length))
  1180. break;
  1181. if (sel < rover.nfiles) {
  1182. color = GREEN;
  1183. ESEL = sel;
  1184. if (rover.nfiles > HEIGHT) {
  1185. if (sel < 3)
  1186. SCROLL = 0;
  1187. else if (sel - 3 > rover.nfiles - HEIGHT)
  1188. SCROLL = rover.nfiles - HEIGHT;
  1189. else
  1190. SCROLL = sel - 3;
  1191. }
  1192. }
  1193. } else {
  1194. ESEL = oldsel;
  1195. SCROLL = oldscroll;
  1196. }
  1197. update_view();
  1198. update_input(RVP_SEARCH, color);
  1199. }
  1200. if (edit_stat == CANCEL) {
  1201. ESEL = oldsel;
  1202. SCROLL = oldscroll;
  1203. }
  1204. clear_message();
  1205. update_view();
  1206. } else if (!strcmp(key, RVK_TG_FILES)) {
  1207. FLAGS ^= SHOW_FILES;
  1208. reload();
  1209. } else if (!strcmp(key, RVK_TG_DIRS)) {
  1210. FLAGS ^= SHOW_DIRS;
  1211. reload();
  1212. } else if (!strcmp(key, RVK_TG_HIDDEN)) {
  1213. FLAGS ^= SHOW_HIDDEN;
  1214. reload();
  1215. } else if (!strcmp(key, RVK_NEW_FILE)) {
  1216. int ok = 0;
  1217. start_line_edit("");
  1218. update_input(RVP_NEW_FILE, RED);
  1219. while ((edit_stat = get_line_edit()) == CONTINUE) {
  1220. int length = strlen(INPUT);
  1221. ok = length;
  1222. for (i = 0; i < rover.nfiles; i++) {
  1223. if (
  1224. !strncmp(ENAME(i), INPUT, length) &&
  1225. (!strcmp(ENAME(i) + length, "") ||
  1226. !strcmp(ENAME(i) + length, "/"))
  1227. ) {
  1228. ok = 0;
  1229. break;
  1230. }
  1231. }
  1232. update_input(RVP_NEW_FILE, ok ? GREEN : RED);
  1233. }
  1234. clear_message();
  1235. if (edit_stat == CONFIRM) {
  1236. if (ok) {
  1237. if (addfile(INPUT) == 0) {
  1238. cd(1);
  1239. try_to_sel(INPUT);
  1240. update_view();
  1241. } else
  1242. message(RED, "Could not create \"%s\".", INPUT);
  1243. } else
  1244. message(RED, "\"%s\" already exists.", INPUT);
  1245. }
  1246. } else if (!strcmp(key, RVK_NEW_DIR)) {
  1247. int ok = 0;
  1248. start_line_edit("");
  1249. update_input(RVP_NEW_DIR, RED);
  1250. while ((edit_stat = get_line_edit()) == CONTINUE) {
  1251. int length = strlen(INPUT);
  1252. ok = length;
  1253. for (i = 0; i < rover.nfiles; i++) {
  1254. if (
  1255. !strncmp(ENAME(i), INPUT, length) &&
  1256. (!strcmp(ENAME(i) + length, "") ||
  1257. !strcmp(ENAME(i) + length, "/"))
  1258. ) {
  1259. ok = 0;
  1260. break;
  1261. }
  1262. }
  1263. update_input(RVP_NEW_DIR, ok ? GREEN : RED);
  1264. }
  1265. clear_message();
  1266. if (edit_stat == CONFIRM) {
  1267. if (ok) {
  1268. if (adddir(INPUT) == 0) {
  1269. cd(1);
  1270. strcat(INPUT, "/");
  1271. try_to_sel(INPUT);
  1272. update_view();
  1273. } else
  1274. message(RED, "Could not create \"%s/\".", INPUT);
  1275. } else
  1276. message(RED, "\"%s\" already exists.", INPUT);
  1277. }
  1278. } else if (!strcmp(key, RVK_RENAME)) {
  1279. int ok = 0;
  1280. char *last;
  1281. int isdir;
  1282. strcpy(INPUT, ENAME(ESEL));
  1283. last = INPUT + strlen(INPUT) - 1;
  1284. if ((isdir = *last == '/'))
  1285. *last = '\0';
  1286. start_line_edit(INPUT);
  1287. update_input(RVP_RENAME, RED);
  1288. while ((edit_stat = get_line_edit()) == CONTINUE) {
  1289. int length = strlen(INPUT);
  1290. ok = length;
  1291. for (i = 0; i < rover.nfiles; i++)
  1292. if (
  1293. !strncmp(ENAME(i), INPUT, length) &&
  1294. (!strcmp(ENAME(i) + length, "") ||
  1295. !strcmp(ENAME(i) + length, "/"))
  1296. ) {
  1297. ok = 0;
  1298. break;
  1299. }
  1300. update_input(RVP_RENAME, ok ? GREEN : RED);
  1301. }
  1302. clear_message();
  1303. if (edit_stat == CONFIRM) {
  1304. if (isdir)
  1305. strcat(INPUT, "/");
  1306. if (ok) {
  1307. if (!rename(ENAME(ESEL), INPUT) && MARKED(ESEL)) {
  1308. del_mark(&rover.marks, ENAME(ESEL));
  1309. add_mark(&rover.marks, CWD, INPUT);
  1310. }
  1311. cd(1);
  1312. try_to_sel(INPUT);
  1313. update_view();
  1314. } else
  1315. message(RED, "\"%s\" already exists.", INPUT);
  1316. }
  1317. } else if (!strcmp(key, RVK_TG_EXEC)) {
  1318. if (!rover.nfiles || S_ISDIR(EMODE(ESEL))) continue;
  1319. if (S_IXUSR & EMODE(ESEL))
  1320. EMODE(ESEL) &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
  1321. else
  1322. EMODE(ESEL) |= S_IXUSR | S_IXGRP | S_IXOTH ;
  1323. if (chmod(ENAME(ESEL), EMODE(ESEL))) {
  1324. message(RED, "Failed to change mode of \"%s\".", ENAME(ESEL));
  1325. } else {
  1326. message(GREEN, "Changed mode of \"%s\".", ENAME(ESEL));
  1327. update_view();
  1328. }
  1329. } else if (!strcmp(key, RVK_DELETE)) {
  1330. if (rover.nfiles) {
  1331. message(YELLOW, "Delete \"%s\"? (Y/n)", ENAME(ESEL));
  1332. if (rover_getch() == 'Y') {
  1333. const char *name = ENAME(ESEL);
  1334. int ret = ISDIR(ENAME(ESEL)) ? deldir(name) : delfile(name);
  1335. reload();
  1336. if (ret)
  1337. message(RED, "Could not delete \"%s\".", ENAME(ESEL));
  1338. } else
  1339. clear_message();
  1340. } else
  1341. message(RED, "No entry selected for deletion.");
  1342. } else if (!strcmp(key, RVK_TG_MARK)) {
  1343. if (MARKED(ESEL))
  1344. del_mark(&rover.marks, ENAME(ESEL));
  1345. else
  1346. add_mark(&rover.marks, CWD, ENAME(ESEL));
  1347. MARKED(ESEL) = !MARKED(ESEL);
  1348. ESEL = (ESEL + 1) % rover.nfiles;
  1349. update_view();
  1350. } else if (!strcmp(key, RVK_INVMARK)) {
  1351. for (i = 0; i < rover.nfiles; i++) {
  1352. if (MARKED(i))
  1353. del_mark(&rover.marks, ENAME(i));
  1354. else
  1355. add_mark(&rover.marks, CWD, ENAME(i));
  1356. MARKED(i) = !MARKED(i);
  1357. }
  1358. update_view();
  1359. } else if (!strcmp(key, RVK_MARKALL)) {
  1360. for (i = 0; i < rover.nfiles; i++)
  1361. if (!MARKED(i)) {
  1362. add_mark(&rover.marks, CWD, ENAME(i));
  1363. MARKED(i) = 1;
  1364. }
  1365. update_view();
  1366. } else if (!strcmp(key, RVK_MARK_DELETE)) {
  1367. if (rover.marks.nentries) {
  1368. message(YELLOW, "Delete all marked entries? (Y/n)");
  1369. if (rover_getch() == 'Y')
  1370. process_marked(NULL, delfile, deldir, "Deleting", "Deleted");
  1371. else
  1372. clear_message();
  1373. } else
  1374. message(RED, "No entries marked for deletion.");
  1375. } else if (!strcmp(key, RVK_MARK_COPY)) {
  1376. if (rover.marks.nentries) {
  1377. if (strcmp(CWD, rover.marks.dirpath))
  1378. process_marked(adddir, cpyfile, NULL, "Copying", "Copied");
  1379. else
  1380. message(RED, "Cannot copy to the same path.");
  1381. } else
  1382. message(RED, "No entries marked for copying.");
  1383. } else if (!strcmp(key, RVK_MARK_MOVE)) {
  1384. if (rover.marks.nentries) {
  1385. if (strcmp(CWD, rover.marks.dirpath))
  1386. process_marked(adddir, movfile, deldir, "Moving", "Moved");
  1387. else
  1388. message(RED, "Cannot move to the same path.");
  1389. } else
  1390. message(RED, "No entries marked for moving.");
  1391. }
  1392. }
  1393. if (rover.nfiles)
  1394. free_rows(&rover.rows, rover.nfiles);
  1395. delwin(rover.window);
  1396. if (save_cwd_file != NULL) {
  1397. fputs(CWD, save_cwd_file);
  1398. fclose(save_cwd_file);
  1399. }
  1400. if (save_marks_file != NULL) {
  1401. for (i = 0; i < rover.marks.bulk; i++) {
  1402. entry = rover.marks.entries[i];
  1403. if (entry)
  1404. fprintf(save_marks_file, "%s%s\n", rover.marks.dirpath, entry);
  1405. }
  1406. fclose(save_marks_file);
  1407. }
  1408. free_marks(&rover.marks);
  1409. return 0;
  1410. }