aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-14 10:23:03 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-14 10:31:31 +0200
commit99334c581349f27551f30082de9b0b8a7b9274af (patch)
tree597123f0123a584c936b066edd52c53e37daa813
parent99e20949b97cb04e03c7dffb80c9599a71497c08 (diff)
downloadrtty-99334c581349f27551f30082de9b0b8a7b9274af.tar.gz
rin.c: Improve
-rw-r--r--rin.c66
1 files changed, 46 insertions, 20 deletions
diff --git a/rin.c b/rin.c
index 72463d8..8d89bc6 100644
--- a/rin.c
+++ b/rin.c
@@ -1,34 +1,60 @@
+#include <curses.h>
#include <err.h>
#include <fcntl.h>
-#include <signal.h>
-#include <termios.h>
#include <unistd.h>
+#include <sys/select.h>
int
main()
{
- int c, n, o, t;
- struct termios orig, term;
+ fd_set rfds;
+ int c, in, n, out, startx, y;
+ struct timeval tv;
+ WINDOW *w;
- t = 0;
+ w = initscr();
+ raw();
+ noecho();
+ timeout(1);
- if((o = open("/var/tmp/r.in", O_RDWR)) == -1)
+ if((in = open("/var/tmp/r.in", O_RDWR)) == -1)
err(1, "/var/tmp/r.in");
+ if((out = open("/var/tmp/r.out", O_RDWR)) == -1)
+ err(1, "/var/tmp/r.out");
- /* Enter "raw" terminal mode. */
- tcgetattr(t, &orig);
- tcgetattr(t, &term);
- term.c_lflag &= ~(ICANON|ECHO|ISIG);
- tcsetattr(t, TCSANOW, &term);
-
- while((n = read(t, &c, 1)) > 0){
- if(c == 26) /* ^Z */
- break;
- if(c != '\n')
- write(t, &c, 1);
- write(o, &c, 1);
+ tv.tv_sec = 0;
+ tv.tv_usec = 1;
+ FD_ZERO(&rfds);
+
+ startx = 0;
+
+ for(;;){
+ FD_SET(out, &rfds);
+ if(select(out+1, &rfds, NULL, NULL, &tv) > 0){
+ read(out, &c, 1);
+ printw("%c", c);
+
+ startx = -1;
+ }
+ if((c = getch()) != ERR){
+ if(startx == -1)
+ startx = getcurx(w);
+ if(c == 26) /* ^Z */
+ break;
+ if(c == 21){ /* ^U */
+move: y = getcury(w);
+ move(y, startx);
+ continue;
+ }
+ if(c == '\n')
+ goto move;
+ else
+ printw("%c", c);
+ write(in, &c, 1);
+ }
}
- /* Restore original terminal mode. */
- tcsetattr(t, TCSANOW, &orig);
+ echo();
+ noraw();
+ endwin();
}