mirror of
https://github.com/ericonr/sndio.git
synced 2024-02-18 04:45:21 -06:00
midicat: Move midi write its own routine.
This commit is contained in:
parent
c9a6bf3db6
commit
ea55b2c67d
@ -21,23 +21,59 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "bsd-compat.h"
|
#include "bsd-compat.h"
|
||||||
|
|
||||||
|
#define MIDI_BUFSZ 1024
|
||||||
|
|
||||||
char usagestr[] = "usage: midicat [-d] [-i in-file] [-o out-file] "
|
char usagestr[] = "usage: midicat [-d] [-i in-file] [-o out-file] "
|
||||||
"[-q in-port] [-q out-port]\n";
|
"[-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
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
#define MIDI_BUFSZ 1024
|
int c, mode;
|
||||||
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) {
|
while ((c = getopt(argc, argv, "di:o:q:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
@ -144,40 +180,24 @@ main(int argc, char **argv)
|
|||||||
/* transfer until end-of-file or error */
|
/* transfer until end-of-file or error */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (ifile != NULL) {
|
if (ifile != NULL) {
|
||||||
len = read(ifd, buf, sizeof(buf));
|
buf_used = read(ifd, buf, sizeof(buf));
|
||||||
if (len == 0)
|
if (buf_used < 0) {
|
||||||
break;
|
|
||||||
if (len < 0) {
|
|
||||||
perror("stdin");
|
perror("stdin");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (buf_used == 0)
|
||||||
|
break;
|
||||||
|
if (!midi_flush())
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
len = mio_read(ih, buf, sizeof(buf));
|
buf_used = mio_read(ih, buf, sizeof(buf));
|
||||||
if (len == 0) {
|
if (buf_used == 0) {
|
||||||
fprintf(stderr, "%s: disconnected\n", port0);
|
fprintf(stderr, "%s: disconnected\n", port0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
if (!midi_flush())
|
||||||
if (ofile != NULL) {
|
|
||||||
n = write(ofd, buf, len);
|
|
||||||
if (n != len) {
|
|
||||||
fprintf(stderr, "%s: short write\n", ofile);
|
|
||||||
break;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* clean-up */
|
/* clean-up */
|
||||||
|
Loading…
Reference in New Issue
Block a user