From 10777e22600f6cea8fba064ebbc50e6123f88c45 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Thu, 8 Nov 2018 11:25:39 +0100 Subject: [PATCH] Revert "midicat: Move midi write its own routine." This reverts commit ea55b2c67d511691eb4acd8b634633b4c515ae7a. --- midicat/midicat.c | 92 +++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/midicat/midicat.c b/midicat/midicat.c index 8a053e0..2cc8bd7 100644 --- a/midicat/midicat.c +++ b/midicat/midicat.c @@ -21,59 +21,23 @@ #include #include "bsd-compat.h" -#define MIDI_BUFSZ 1024 - char usagestr[] = "usage: midicat [-d] [-i in-file] [-o out-file] " "[-q in-port] [-q out-port]\n"; -char *port0, *port1, *ifile, *ofile; -struct mio_hdl *ih, *oh; -unsigned char buf[MIDI_BUFSZ]; -int buf_used = 0; -int ifd = -1, ofd = -1; -int dump; - -static int -midi_flush(void) -{ - int i, n, sep; - - if (buf_used == 0) - return 1; - - if (ofile != NULL) { - n = write(ofd, buf, buf_used); - if (n != buf_used) { - fprintf(stderr, "%s: short write\n", ofile); - buf_used = 0; - return 0; - } - } else { - n = mio_write(oh, buf, buf_used); - if (n != buf_used) { - fprintf(stderr, "%s: port disconnected\n", - ih == oh ? port0 : port1); - buf_used = 0; - return 0; - } - } - - if (dump) { - for (i = 0; i < buf_used; i++) { - sep = (i % 16 == 15 || i == buf_used - 1) ? - '\n' : ' '; - fprintf(stderr, "%02x%c", buf[i], sep); - } - } - - buf_used = 0; - return 1; -} - int main(int argc, char **argv) { - int c, mode; +#define MIDI_BUFSZ 1024 + unsigned char buf[MIDI_BUFSZ]; + struct mio_hdl *ih, *oh; + char *port0, *port1, *ifile, *ofile; + int ifd, ofd; + int dump, c, i, len, n, sep, mode; + + dump = 0; + port0 = port1 = ifile = ofile = NULL; + ih = oh = NULL; + ifd = ofd = -1; while ((c = getopt(argc, argv, "di:o:q:")) != -1) { switch (c) { @@ -180,23 +144,39 @@ main(int argc, char **argv) /* transfer until end-of-file or error */ for (;;) { if (ifile != NULL) { - buf_used = read(ifd, buf, sizeof(buf)); - if (buf_used < 0) { + len = read(ifd, buf, sizeof(buf)); + if (len == 0) + break; + if (len < 0) { perror("stdin"); break; } - if (buf_used == 0) - break; - if (!midi_flush()) - break; } else { - buf_used = mio_read(ih, buf, sizeof(buf)); - if (buf_used == 0) { + len = mio_read(ih, buf, sizeof(buf)); + if (len == 0) { fprintf(stderr, "%s: disconnected\n", port0); break; } - if (!midi_flush()) + } + if (ofile != NULL) { + n = write(ofd, buf, len); + if (n != len) { + fprintf(stderr, "%s: short write\n", ofile); break; + } + } else { + n = mio_write(oh, buf, len); + if (n != len) { + fprintf(stderr, "%s: disconnected\n", port1); + break; + } + } + if (dump) { + for (i = 0; i < len; i++) { + sep = (i % 16 == 15 || i == len - 1) ? + '\n' : ' '; + fprintf(stderr, "%02x%c", buf[i], sep); + } } }