| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: selbuf.c,v 1.5 2002/01/13 13:33:15 mdw Exp $ |
| 4 | * |
| 5 | * Line-buffering select handler |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 14 | * mLib is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * mLib is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with mLib; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: selbuf.c,v $ |
| 33 | * Revision 1.5 2002/01/13 13:33:15 mdw |
| 34 | * Track interface change for @lbuf@. |
| 35 | * |
| 36 | * Revision 1.4 2000/06/17 10:38:14 mdw |
| 37 | * Add support for variable buffer sizes. |
| 38 | * |
| 39 | * Revision 1.3 1999/05/22 13:41:00 mdw |
| 40 | * Fix end-of-file detection and error handling. |
| 41 | * |
| 42 | * Revision 1.2 1999/05/17 20:36:50 mdw |
| 43 | * Make the magical constants for the buffer flags uppercase. |
| 44 | * |
| 45 | * Revision 1.1 1999/05/14 21:01:15 mdw |
| 46 | * Integrated `select' handling bits from the background resolver project. |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | /*----- Header files ------------------------------------------------------*/ |
| 51 | |
| 52 | #include <errno.h> |
| 53 | #include <stdio.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <string.h> |
| 56 | |
| 57 | #include <sys/types.h> |
| 58 | #include <sys/time.h> |
| 59 | #include <unistd.h> |
| 60 | |
| 61 | #include "lbuf.h" |
| 62 | #include "sel.h" |
| 63 | #include "selbuf.h" |
| 64 | |
| 65 | /*----- Main code ---------------------------------------------------------*/ |
| 66 | |
| 67 | /* --- @selbuf_enable@ --- * |
| 68 | * |
| 69 | * Arguments: @selbuf *b@ = pointer to buffer block |
| 70 | * |
| 71 | * Returns: --- |
| 72 | * |
| 73 | * Use: Enables a buffer for reading, and emits any queued lines |
| 74 | * to the buffer's owner. |
| 75 | */ |
| 76 | |
| 77 | void selbuf_enable(selbuf *b) |
| 78 | { |
| 79 | if (!(b->b.f & LBUF_ENABLE)) { |
| 80 | b->b.f |= LBUF_ENABLE; |
| 81 | sel_addfile(&b->reader); |
| 82 | lbuf_flush(&b->b, 0, 0); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* --- @selbuf_disable@ --- * |
| 87 | * |
| 88 | * Arguments: @selbuf *b@ = pointer to a buffer block |
| 89 | * |
| 90 | * Returns: --- |
| 91 | * |
| 92 | * Use: Disables a buffer. It won't be read from until it's |
| 93 | * enabled again. |
| 94 | */ |
| 95 | |
| 96 | void selbuf_disable(selbuf *b) |
| 97 | { |
| 98 | if (b->b.f & LBUF_ENABLE) { |
| 99 | b->b.f &= ~LBUF_ENABLE; |
| 100 | sel_rmfile(&b->reader); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* --- @selbuf_read@ --- * |
| 105 | * |
| 106 | * Arguments: @int fd@ = file descriptor to read from |
| 107 | * @int mode@ = what we can do to the file |
| 108 | * @void *vp@ = pointer to buffer context |
| 109 | * |
| 110 | * Returns: --- |
| 111 | * |
| 112 | * Use: Acts on the result of a @select@ call. |
| 113 | */ |
| 114 | |
| 115 | static void selbuf_read(int fd, unsigned mode, void *vp) |
| 116 | { |
| 117 | selbuf *b = vp; |
| 118 | char *p; |
| 119 | size_t sz; |
| 120 | int n; |
| 121 | |
| 122 | sz = lbuf_free(&b->b, &p); |
| 123 | n = read(fd, p, sz); |
| 124 | if (n == 0) |
| 125 | lbuf_close(&b->b); |
| 126 | else if (n > 0) |
| 127 | lbuf_flush(&b->b, p, n); |
| 128 | else switch (errno) { |
| 129 | case EINTR: |
| 130 | case EAGAIN: |
| 131 | #if EAGAIN != EWOULDBLOCK |
| 132 | case EWOULDBLOCK: |
| 133 | #endif |
| 134 | return; |
| 135 | default: |
| 136 | lbuf_close(&b->b); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* --- @selbuf_setsize@ --- * |
| 141 | * |
| 142 | * Arguments: @selbuf *b@ = pointer to buffer block |
| 143 | * @size_t sz@ = size of buffer |
| 144 | * |
| 145 | * Returns: --- |
| 146 | * |
| 147 | * Use: Sets the size of the buffer used for reading lines. |
| 148 | */ |
| 149 | |
| 150 | void selbuf_setsize(selbuf *b, size_t sz) |
| 151 | { |
| 152 | lbuf_setsize(&b->b, sz); |
| 153 | } |
| 154 | |
| 155 | /* --- @selbuf_init@ --- * |
| 156 | * |
| 157 | * Arguments: @selbuf *b@ = pointer to buffer block |
| 158 | * @sel_state *s@ = pointer to select state to attach to |
| 159 | * @int fd@ = file descriptor to listen to |
| 160 | * @lbuf_func *func@ = function to call |
| 161 | * @void *p@ = argument for function |
| 162 | * |
| 163 | * Returns: --- |
| 164 | * |
| 165 | * Use: Initializes a buffer block. |
| 166 | */ |
| 167 | |
| 168 | void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p) |
| 169 | { |
| 170 | lbuf_init(&b->b, func, p); |
| 171 | b->b.f &= ~LBUF_ENABLE; |
| 172 | sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b); |
| 173 | selbuf_enable(b); |
| 174 | } |
| 175 | |
| 176 | /* --- @selbuf_destroy@ --- * |
| 177 | * |
| 178 | * Arguments: @selbuf *b@ = pointer to buffer block |
| 179 | * |
| 180 | * Returns: --- |
| 181 | * |
| 182 | * Use: Deallocates a line buffer and frees any resources it owned. |
| 183 | */ |
| 184 | |
| 185 | void selbuf_destroy(selbuf *b) |
| 186 | { |
| 187 | selbuf_disable(b); |
| 188 | lbuf_destroy(&b->b); |
| 189 | } |
| 190 | |
| 191 | /*----- That's all, folks -------------------------------------------------*/ |