Add reload function to catpoint.

Thanks to Karchnu <karchnu@karchnu.fr> for the initial patch!
This commit is contained in:
Christoph Lohmann 2020-12-20 11:44:12 +01:00
parent fbd1f47fc5
commit 9d56979ded

View File

@ -20,6 +20,7 @@ void
cleanup(int s) cleanup(int s)
{ {
int i; int i;
for (i = 0; i<n; i++) for (i = 0; i<n; i++)
munmap(p[i], 0x1000); munmap(p[i], 0x1000);
@ -27,6 +28,26 @@ cleanup(int s)
exit(1); exit(1);
} }
void
reload(char **argv, int i)
{
struct stat statbuf;
int fd;
if (p[i] != NULL)
munmap(p[i], 0x1000);
fd = open(argv[i], O_RDONLY, 0);
if (fd < 0)
err(1, "open: %s", argv[i]);
if (fstat(fd, &statbuf) < 0)
err(1, "fstat: %s", argv[i]);
p[i] = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (p[i] == MAP_FAILED)
err(1, "mmap");
close(fd);
}
void void
setsignal() setsignal()
{ {
@ -42,8 +63,7 @@ setsignal()
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c, i, fd; int c, i;
struct stat statbuf;
if (argc == 1) if (argc == 1)
errx(1, "usage: %s file ...", argv[0]); errx(1, "usage: %s file ...", argv[0]);
@ -57,17 +77,8 @@ main(int argc, char *argv[])
n = argc; n = argc;
/* map files to mem */ /* map files to mem */
for (i = 0; argv[i] != NULL; i++) { for (i = 0; argv[i] != NULL; i++)
fd = open(argv[i], O_RDONLY, 0); reload(argv, i);
if (fd == -1)
err(1, "open: %s", argv[i]);
if (fstat(fd, &statbuf) < 0)
err(1, "fstat: %s", argv[i]);
p[i] = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (p[i] == MAP_FAILED)
err(1, "mmap");
close(fd);
}
/* init curses */ /* init curses */
initscr(); initscr();
@ -83,6 +94,7 @@ main(int argc, char *argv[])
show: show:
/* display slide */ /* display slide */
clear(); clear();
refresh();
printw("%s", p[i]); printw("%s", p[i]);
again: again:
c = getch(); c = getch();
@ -128,6 +140,10 @@ again:
case KEY_END: case KEY_END:
i = argc - 1; i = argc - 1;
goto show; goto show;
/* reload */
case 'r':
reload(argv, i);
goto show;
default: default:
/* printf("key pressed = '%d'\n", c); */ /* printf("key pressed = '%d'\n", c); */
goto again; goto again;