summaryrefslogtreecommitdiff
path: root/recordio.c
diff options
context:
space:
mode:
Diffstat (limited to 'recordio.c')
-rw-r--r--recordio.c121
1 files changed, 67 insertions, 54 deletions
diff --git a/recordio.c b/recordio.c
index 09277d9..a3ee03c 100644
--- a/recordio.c
+++ b/recordio.c
@@ -1,42 +1,40 @@
#include "sig.h"
-#include "substdio.h"
+#include "buffer.h"
#include "strerr.h"
#include "str.h"
#include "byte.h"
#include "readwrite.h"
#include "exit.h"
#include "fmt.h"
-#include "select.h"
+#include "iopause.h"
+#include "pathexec.h"
#define FATAL "recordio: fatal: "
-char pid[FMT_ULONG + 10];
+char pid[FMT_ULONG];
char recordbuf[512];
-substdio ssrecord = SUBSTDIO_FDBUF(write,2,recordbuf,sizeof recordbuf);
+buffer ssrecord = BUFFER_INIT(write,2,recordbuf,sizeof recordbuf);
-void record(buf,len,direction)
-char *buf;
-int len; /* 1...256 */
-char *direction;
+void record(char *buf,int len,char *direction) /* 1 <= len <= 256 */
{
int i;
while (len) {
- substdio_puts(&ssrecord,pid);
- substdio_puts(&ssrecord,direction);
+ buffer_puts(&ssrecord,pid);
+ buffer_puts(&ssrecord,direction);
i = byte_chr(buf,len,'\n');
- substdio_put(&ssrecord,buf,i);
+ buffer_put(&ssrecord,buf,i);
if (i == len) {
- substdio_puts(&ssrecord,"+\n");
- substdio_flush(&ssrecord);
+ buffer_puts(&ssrecord,"+\n");
+ buffer_flush(&ssrecord);
return;
}
- substdio_puts(&ssrecord," \n");
- substdio_flush(&ssrecord);
+ buffer_puts(&ssrecord," \n");
+ buffer_flush(&ssrecord);
buf += i + 1;
len -= i + 1;
}
@@ -52,41 +50,60 @@ char rightbuf[256];
int rightlen;
int rightpos;
-void doit(fdleft,fdright)
-int fdleft; /* copy from 0 to fdleft */
-int fdright; /* copy from fdright to 1 */
+void doit(int fdleft,int fdright) /* copy 0 -> fdleft, copy fdright -> 1 */
{
- fd_set rfds;
- fd_set wfds;
- int nfds;
+ struct taia stamp;
+ struct taia deadline;
+ iopause_fd x[4];
+ int xlen;
+ iopause_fd *io0;
+ iopause_fd *ioleft;
+ iopause_fd *io1;
+ iopause_fd *ioright;
int r;
- nfds = 2;
- if (nfds <= fdleft) nfds = fdleft + 1;
- if (nfds <= fdright) nfds = fdright + 1;
- /* XXX: will overflow select buf if too many fds are open */
-
for (;;) {
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
+ xlen = 0;
- if (leftstatus == 0) FD_SET(0,&rfds);
- if (leftstatus == 1) FD_SET(fdleft,&wfds);
+ io0 = 0;
+ if (leftstatus == 0) {
+ io0 = &x[xlen++];
+ io0->fd = 0;
+ io0->events = IOPAUSE_READ;
+ }
+ ioleft = 0;
+ if (leftstatus == 1) {
+ ioleft = &x[xlen++];
+ ioleft->fd = fdleft;
+ ioleft->events = IOPAUSE_WRITE;
+ }
- if (rightstatus == 0) FD_SET(fdright,&rfds);
- if (rightstatus == 1) FD_SET(1,&wfds);
+ ioright = 0;
+ if (rightstatus == 0) {
+ ioright = &x[xlen++];
+ ioright->fd = fdright;
+ ioright->events = IOPAUSE_READ;
+ }
+ io1 = 0;
+ if (rightstatus == 1) {
+ io1 = &x[xlen++];
+ io1->fd = 1;
+ io1->events = IOPAUSE_WRITE;
+ }
- if (select(nfds,&rfds,&wfds,(fd_set *) 0,(struct timeval *) 0) == -1)
- continue;
+ taia_now(&stamp);
+ taia_uint(&deadline,3600);
+ taia_add(&deadline,&stamp,&deadline);
+ iopause(x,xlen,&deadline,&stamp);
- if (FD_ISSET(0,&rfds)) {
+ if (io0 && io0->revents) {
r = read(0,leftbuf,sizeof leftbuf);
if (r <= 0) {
leftstatus = -1;
close(fdleft);
- substdio_puts(&ssrecord,pid);
- substdio_puts(&ssrecord," < [EOF]\n");
- substdio_flush(&ssrecord);
+ buffer_puts(&ssrecord,pid);
+ buffer_puts(&ssrecord," < [EOF]\n");
+ buffer_flush(&ssrecord);
}
else {
leftstatus = 1; leftpos = 0; leftlen = r;
@@ -94,28 +111,26 @@ int fdright; /* copy from fdright to 1 */
}
}
- if (FD_ISSET(fdleft,&wfds)) {
+ if (ioleft && ioleft->revents) {
r = write(fdleft,leftbuf + leftpos,leftlen - leftpos);
if (r == -1) break;
leftpos += r;
if (leftpos == leftlen) leftstatus = 0;
}
- if (FD_ISSET(fdright,&rfds)) {
+ if (ioright && ioright->revents) {
r = read(fdright,rightbuf,sizeof rightbuf);
if (r <= 0) {
- substdio_puts(&ssrecord,pid);
- substdio_puts(&ssrecord," > [EOF]\n");
- substdio_flush(&ssrecord);
+ buffer_puts(&ssrecord,pid);
+ buffer_puts(&ssrecord," > [EOF]\n");
+ buffer_flush(&ssrecord);
break;
}
- else {
- rightstatus = 1; rightpos = 0; rightlen = r;
- record(rightbuf,r," > ");
- }
+ rightstatus = 1; rightpos = 0; rightlen = r;
+ record(rightbuf,r," > ");
}
- if (FD_ISSET(1,&wfds)) {
+ if (io1 && io1->revents) {
r = write(1,rightbuf + rightpos,rightlen - rightpos);
if (r == -1) break;
rightpos += r;
@@ -126,14 +141,12 @@ int fdright; /* copy from fdright to 1 */
_exit(0);
}
-void main(argc,argv)
-int argc;
-char **argv;
+main(int argc,char **argv,char **envp)
{
int piin[2];
int piout[2];
- pid[fmt_ulong(pid,(unsigned long) getpid())] = 0;
+ pid[fmt_ulong(pid,getpid())] = 0;
if (argc < 2)
strerr_die1x(100,"recordio: usage: recordio program [ arg ... ]");
@@ -147,7 +160,7 @@ char **argv;
case -1:
strerr_die2sys(111,FATAL,"unable to fork: ");
case 0:
- sig_pipeignore();
+ sig_ignore(sig_pipe);
close(piin[0]);
close(piout[1]);
doit(piin[1],piout[0]);
@@ -160,6 +173,6 @@ char **argv;
if (fd_move(1,piout[1]) == -1)
strerr_die2sys(111,FATAL,"unable to move descriptors: ");
- execvp(argv[1],argv + 1);
+ pathexec_run(argv[1],argv + 1,envp);
strerr_die4sys(111,FATAL,"unable to run ",argv[1],": ");
}