X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/1ef7279c324e278ecf75243ca651710db9ac708e..9fcce036ef8b14c12fc4482efc7c8cf75da3b31f:/selbuf.c?ds=inline diff --git a/selbuf.c b/selbuf.c index aafe711..05aec77 100644 --- a/selbuf.c +++ b/selbuf.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: selbuf.c,v 1.2 1999/05/17 20:36:50 mdw Exp $ + * $Id: selbuf.c,v 1.6 2004/04/08 01:36:13 mdw Exp $ * * Line-buffering select handler * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,29 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: selbuf.c,v $ - * Revision 1.2 1999/05/17 20:36:50 mdw - * Make the magical constants for the buffer flags uppercase. - * - * Revision 1.1 1999/05/14 21:01:15 mdw - * Integrated `select' handling bits from the background resolver project. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -111,23 +100,36 @@ static void selbuf_read(int fd, unsigned mode, void *vp) int n; sz = lbuf_free(&b->b, &p); -again: n = read(fd, p, sz); - if (n <= 0) { - switch (errno) { - case EINTR: - goto again; - case EAGAIN: + if (n == 0) + lbuf_close(&b->b); + else if (n > 0) + lbuf_flush(&b->b, p, n); + else switch (errno) { + case EINTR: + case EAGAIN: #if EAGAIN != EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #endif - return; - default: - lbuf_close(&b->b); - } + return; + default: + lbuf_close(&b->b); } - else - lbuf_flush(&b->b, p, n); +} + +/* --- @selbuf_setsize@ --- * + * + * Arguments: @selbuf *b@ = pointer to buffer block + * @size_t sz@ = size of buffer + * + * Returns: --- + * + * Use: Sets the size of the buffer used for reading lines. + */ + +void selbuf_setsize(selbuf *b, size_t sz) +{ + lbuf_setsize(&b->b, sz); } /* --- @selbuf_init@ --- * @@ -135,7 +137,7 @@ again: * Arguments: @selbuf *b@ = pointer to buffer block * @sel_state *s@ = pointer to select state to attach to * @int fd@ = file descriptor to listen to - * @void (*func)(char *s, void *p)@ = function to call + * @lbuf_func *func@ = function to call * @void *p@ = argument for function * * Returns: --- @@ -143,11 +145,7 @@ again: * Use: Initializes a buffer block. */ -void selbuf_init(selbuf *b, - sel_state *s, - int fd, - void (*func)(char */*s*/, void */*p*/), - void *p) +void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p) { lbuf_init(&b->b, func, p); b->b.f &= ~LBUF_ENABLE; @@ -155,4 +153,19 @@ void selbuf_init(selbuf *b, selbuf_enable(b); } +/* --- @selbuf_destroy@ --- * + * + * Arguments: @selbuf *b@ = pointer to buffer block + * + * Returns: --- + * + * Use: Deallocates a line buffer and frees any resources it owned. + */ + +void selbuf_destroy(selbuf *b) +{ + selbuf_disable(b); + lbuf_destroy(&b->b); +} + /*----- That's all, folks -------------------------------------------------*/