From 6e0b65303b01bb9669bf662364338ecf19e273a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 14 Jul 2021 14:11:05 +0200 Subject: rtty.c: Don't eat output after repeated typed command --- Makefile | 4 ++-- rtty.c | 51 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 7197725..3720f8a 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ -install: - install ri rd /usr/local/bin +install: rtty + install rd rtty /usr/local/bin diff --git a/rtty.c b/rtty.c index 42c9324..88ef214 100644 --- a/rtty.c +++ b/rtty.c @@ -27,38 +27,61 @@ main() tv.tv_usec = 1; for(;;){ - /* Read from named pipe (out) and print on standard in. */ + /* + * User input is read from standard in and copied the + * input pipe. It is saved in bufin for later use. + */ + FD_SET(0, &rfds0); + if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ + nout = read(0, bufin, MAXBUF); + bufin[nout] = 0; + dprintf(fdin, "%s", bufin); + fflush(stdout); + } + + /* + * System output is read from the output pipe and copied + * to standard out for the user to see. + */ FD_SET(fdout, &rfds1); if(select(fdout+1, &rfds1, NULL, NULL, &tv) > 0){ nout = read(fdout, bufout, MAXBUF); if(!nout) continue; bufout[nout] = 0; - /* Ignore repetition of typed command. */ - for(bi = bufin, bo = bufout;;){ - if(*bo == '\n') + /* + * Bufin and bufout are copied to the temporary + * pointers bi and bo. Bo is incremented in order + * to skip any potential repetition of the command + * given by the user on standard in. (It is assumed + * that the typed command fits in bufin, i.e. is + * not larger than MAXBUF.) + */ + bi = bufin; + bo = bufout; + for(;;){ + if(*bo == '\n'){ + bo++; break; + } if(*bo == 13){ bo++; continue; } if(*bi != *bo){ - printf("%s", bufout); - fflush(stdout); + bo = bufout; break; } bi++; bo++; } - bufin[0] = 0; - } - /* Read from standard in and print on named pipe (in). */ - FD_SET(0, &rfds0); - if(select(0+1, &rfds0, NULL, NULL, &tv) > 0){ - nout = read(0, bufin, MAXBUF); - bufin[nout] = 0; - dprintf(fdin, "%s", bufin); + /* + * Bo is printed and bufin is cleared, so as not + * to perform the skip again, incorrectly. + */ + printf("%s", bo); fflush(stdout); + bufin[0] = 0; } } -- cgit v1.2.3