diff options
author | Daniel Hartmeier <dhartmei@cvs.openbsd.org> | 2003-09-02 22:44:07 +0000 |
---|---|---|
committer | Daniel Hartmeier <dhartmei@cvs.openbsd.org> | 2003-09-02 22:44:07 +0000 |
commit | 3bba28bf20ba83497ed9f3bc33d401fbdeecd827 (patch) | |
tree | af0531c8aeaecac6b6b714d86d53a3d39ebf8b53 /usr.bin | |
parent | 86aea4b71bc20b522c257b3adadd61defcde0f3a (diff) |
switch to dynamic fd_set and poll. patch entirely from millert@.
ok deraadt@, dhartmei@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/vi/cl/cl_read.c | 49 | ||||
-rw-r--r-- | usr.bin/vi/ex/ex_script.c | 76 |
2 files changed, 71 insertions, 54 deletions
diff --git a/usr.bin/vi/cl/cl_read.c b/usr.bin/vi/cl/cl_read.c index ab1bc97b0b7..8b3db162f65 100644 --- a/usr.bin/vi/cl/cl_read.c +++ b/usr.bin/vi/cl/cl_read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cl_read.c,v 1.11 2002/02/17 19:42:33 millert Exp $ */ +/* $OpenBSD: cl_read.c,v 1.12 2003/09/02 22:44:06 dhartmei Exp $ */ /*- * Copyright (c) 1993, 1994 @@ -146,7 +146,7 @@ cl_read(sp, flags, bp, blen, nrp, tp) CL_PRIVATE *clp; GS *gp; SCR *tsp; - fd_set rdfd; + fd_set *rdfd; input_t rval; int maxfd, nr, term_reset; @@ -154,6 +154,16 @@ cl_read(sp, flags, bp, blen, nrp, tp) clp = CLP(sp); term_reset = 0; + /* Allocate space for rdfd. */ + maxfd = STDIN_FILENO; + for (tsp = gp->dq.cqh_first; + tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) + if (F_ISSET(sp, SC_SCRIPT) && sp->script->sh_master > maxfd) + maxfd = sp->script->sh_master; + rdfd = (fd_set *)malloc(howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + if (rdfd == NULL) + goto err; + /* * 1: A read from a file or a pipe. In this case, the reads * never timeout regardless. This means that we can hang @@ -163,11 +173,13 @@ cl_read(sp, flags, bp, blen, nrp, tp) if (!F_ISSET(clp, CL_STDIN_TTY)) { switch (nr = read(STDIN_FILENO, bp, blen)) { case 0: + free(rdfd); return (INP_EOF); case -1: goto err; default: *nrp = nr; + free(rdfd); return (INP_OK); } /* NOTREACHED */ @@ -177,14 +189,16 @@ cl_read(sp, flags, bp, blen, nrp, tp) * 2: A read with an associated timeout, e.g., trying to complete * a map sequence. If input exists, we fall into #3. */ - FD_ZERO(&rdfd); - poll.tv_sec = 0; - poll.tv_usec = 0; if (tp != NULL) { - FD_SET(STDIN_FILENO, &rdfd); + memset(rdfd, 0, howmany(STDIN_FILENO + 1, NFDBITS) + * sizeof(fd_mask)); + poll.tv_sec = 0; + poll.tv_usec = 0; + FD_SET(STDIN_FILENO, rdfd); switch (select(STDIN_FILENO + 1, - &rdfd, NULL, NULL, tp == NULL ? &poll : tp)) { + rdfd, NULL, NULL, tp == NULL ? &poll : tp)) { case 0: + free(rdfd); return (INP_TIMEOUT); case -1: goto err; @@ -224,17 +238,13 @@ cl_read(sp, flags, bp, blen, nrp, tp) * the only way to keep from locking out scripting windows. */ if (F_ISSET(gp, G_SCRWIN)) { -loop: FD_ZERO(&rdfd); - FD_SET(STDIN_FILENO, &rdfd); - maxfd = STDIN_FILENO; +loop: memset(rdfd, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + FD_SET(STDIN_FILENO, rdfd); for (tsp = gp->dq.cqh_first; tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) - if (F_ISSET(sp, SC_SCRIPT)) { - FD_SET(sp->script->sh_master, &rdfd); - if (sp->script->sh_master > maxfd) - maxfd = sp->script->sh_master; - } - switch (select(maxfd + 1, &rdfd, NULL, NULL, NULL)) { + if (F_ISSET(sp, SC_SCRIPT)) + FD_SET(sp->script->sh_master, rdfd); + switch (select(maxfd + 1, rdfd, NULL, NULL, NULL)) { case 0: abort(); case -1: @@ -242,9 +252,11 @@ loop: FD_ZERO(&rdfd); default: break; } - if (!FD_ISSET(STDIN_FILENO, &rdfd)) { - if (sscr_input(sp)) + if (!FD_ISSET(STDIN_FILENO, rdfd)) { + if (sscr_input(sp)) { + free(rdfd); return (INP_ERR); + } goto loop; } } @@ -303,6 +315,7 @@ err: if (errno == EINTR) /* Restore the terminal state if it was modified. */ if (term_reset) (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &term1); + free(rdfd); return (rval); } diff --git a/usr.bin/vi/ex/ex_script.c b/usr.bin/vi/ex/ex_script.c index d1068448177..50692c2899e 100644 --- a/usr.bin/vi/ex/ex_script.c +++ b/usr.bin/vi/ex/ex_script.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ex_script.c,v 1.10 2003/04/17 02:22:56 itojun Exp $ */ +/* $OpenBSD: ex_script.c,v 1.11 2003/09/02 22:44:06 dhartmei Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -37,6 +37,7 @@ static const char sccsid[] = "@(#)ex_script.c 10.30 (Berkeley) 9/24/96"; #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */ #include <grp.h> #include <limits.h> +#include <poll.h> #include <stdlib.h> #include <string.h> #include <termios.h> @@ -211,32 +212,29 @@ static int sscr_getprompt(sp) SCR *sp; { - struct timeval tv; CHAR_T *endp, *p, *t, buf[1024]; SCRIPT *sc; - fd_set fdset; + struct pollfd pfd[1]; recno_t lline; size_t llen, len; u_int value; int nr; - FD_ZERO(&fdset); endp = buf; len = sizeof(buf); /* Wait up to a second for characters to read. */ - tv.tv_sec = 5; - tv.tv_usec = 0; sc = sp->script; - FD_SET(sc->sh_master, &fdset); - switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) { + pfd[0].fd = sc->sh_master; + pfd[0].events = POLLIN; + switch (poll(pfd, 1, 5 * 1000)) { case -1: /* Error or interrupt. */ - msgq(sp, M_SYSERR, "select"); + msgq(sp, M_SYSERR, "poll"); goto prompterr; case 0: /* Timeout */ msgq(sp, M_ERR, "Error: timed out"); goto prompterr; - case 1: /* Characters to read. */ + default: /* Characters to read. */ break; } @@ -272,15 +270,13 @@ more: len = sizeof(buf) - (endp - buf); goto more; /* Wait up 1/10 of a second to make sure that we got it all. */ - tv.tv_sec = 0; - tv.tv_usec = 100000; - switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) { + switch (poll(pfd, 1, 100)) { case -1: /* Error or interrupt. */ - msgq(sp, M_SYSERR, "select"); + msgq(sp, M_SYSERR, "poll"); goto prompterr; case 0: /* Timeout */ break; - case 1: /* Characters to read. */ + default: /* Characters to read. */ goto more; } @@ -381,31 +377,40 @@ sscr_input(sp) SCR *sp; { GS *gp; - struct timeval poll; - fd_set rdfd; - int maxfd; + struct timeval tv; + fd_set *rdfd; + int maxfd, nfd; gp = sp->gp; -loop: maxfd = 0; - FD_ZERO(&rdfd); - poll.tv_sec = 0; - poll.tv_usec = 0; + /* Allocate space for rdfd. */ + maxfd = STDIN_FILENO; + for (sp = gp->dq.cqh_first; sp != (void *)&gp->dq; sp = sp->q.cqe_next) + if (F_ISSET(sp, SC_SCRIPT) && sp->script->sh_master > maxfd) + maxfd = sp->script->sh_master; + rdfd = (fd_set *)malloc(howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + if (rdfd == NULL) { + msgq(sp, M_SYSERR, "malloc"); + return (1); + } + +loop: memset(rdfd, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); + tv.tv_sec = 0; + tv.tv_usec = 0; /* Set up the input mask. */ for (sp = gp->dq.cqh_first; sp != (void *)&gp->dq; sp = sp->q.cqe_next) - if (F_ISSET(sp, SC_SCRIPT)) { - FD_SET(sp->script->sh_master, &rdfd); - if (sp->script->sh_master > maxfd) - maxfd = sp->script->sh_master; - } + if (F_ISSET(sp, SC_SCRIPT)) + FD_SET(sp->script->sh_master, rdfd); /* Check for input. */ - switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) { + switch (select(maxfd + 1, rdfd, NULL, NULL, &tv)) { case -1: msgq(sp, M_SYSERR, "select"); + free(rdfd); return (1); case 0: + free(rdfd); return (0); default: break; @@ -414,8 +419,10 @@ loop: maxfd = 0; /* Read the input. */ for (sp = gp->dq.cqh_first; sp != (void *)&gp->dq; sp = sp->q.cqe_next) if (F_ISSET(sp, SC_SCRIPT) && - FD_ISSET(sp->script->sh_master, &rdfd) && sscr_insert(sp)) + FD_ISSET(sp->script->sh_master, rdfd) && sscr_insert(sp)) { + free(rdfd); return (1); + } goto loop; } @@ -430,7 +437,7 @@ sscr_insert(sp) struct timeval tv; CHAR_T *endp, *p, *t; SCRIPT *sc; - fd_set rdfd; + struct pollfd pfd[1]; recno_t lno; size_t blen, len, tlen; u_int value; @@ -481,12 +488,9 @@ more: switch (nr = read(sc->sh_master, endp, MINREAD)) { * confused the shell, or whatever. */ if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) { - tv.tv_sec = 0; - tv.tv_usec = 100000; - FD_ZERO(&rdfd); - FD_SET(sc->sh_master, &rdfd); - if (select(sc->sh_master + 1, - &rdfd, NULL, NULL, &tv) == 1) { + pfd[0].fd = sc->sh_master; + pfd[0].events = POLLIN; + if (poll(pfd, 1, 100) > 0) { memmove(bp, t, len); endp = bp + len; goto more; |