commit 9d56979dedbc9a3b279291ad07ab4ce277e8f5e7
parent fbd1f47fc5dc657c0bac33ac3d6f7c89495f4df9
Author: Christoph Lohmann <20h@r-36.net>
Date: Sun, 20 Dec 2020 11:44:12 +0100
Add reload function to catpoint.
Thanks to Karchnu <karchnu@karchnu.fr> for the initial patch!
Diffstat:
catpoint.c | | | 42 | +++++++++++++++++++++++++++++------------- |
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/catpoint.c b/catpoint.c
@@ -20,6 +20,7 @@ void
cleanup(int s)
{
int i;
+
for (i = 0; i<n; i++)
munmap(p[i], 0x1000);
@@ -28,6 +29,26 @@ cleanup(int s)
}
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
setsignal()
{
signal(SIGHUP, cleanup);
@@ -42,8 +63,7 @@ setsignal()
int
main(int argc, char *argv[])
{
- int c, i, fd;
- struct stat statbuf;
+ int c, i;
if (argc == 1)
errx(1, "usage: %s file ...", argv[0]);
@@ -57,17 +77,8 @@ main(int argc, char *argv[])
n = argc;
/* map files to mem */
- for (i = 0; argv[i] != NULL; i++) {
- fd = open(argv[i], O_RDONLY, 0);
- 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);
- }
+ for (i = 0; argv[i] != NULL; i++)
+ reload(argv, i);
/* init curses */
initscr();
@@ -83,6 +94,7 @@ main(int argc, char *argv[])
show:
/* display slide */
clear();
+ refresh();
printw("%s", p[i]);
again:
c = getch();
@@ -128,6 +140,10 @@ again:
case KEY_END:
i = argc - 1;
goto show;
+ /* reload */
+ case 'r':
+ reload(argv, i);
+ goto show;
default:
/* printf("key pressed = '%d'\n", c); */
goto again;