aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-14 14:11:05 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-14 14:24:15 +0200
commit6e0b65303b01bb9669bf662364338ecf19e273a3 (patch)
tree0320823047fd3b13d152a66d5e99376924ca321a
parent5d9b2527bc17daea3ec9eed74d80a76ec89e0f51 (diff)
downloadrtty-6e0b65303b01bb9669bf662364338ecf19e273a3.tar.gz
rtty.c: Don't eat output after repeated typed command
-rw-r--r--Makefile4
-rw-r--r--rtty.c51
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;
}
}