chiark / gitweb /
New function `tv_addl' to add a literal to a time value.
[mLib] / selbuf.c
1 /* -*-c-*-
2  *
3  * $Id: selbuf.c,v 1.2 1999/05/17 20:36:50 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.2  1999/05/17 20:36:50  mdw
34  * Make the magical constants for the buffer flags uppercase.
35  *
36  * Revision 1.1  1999/05/14 21:01:15  mdw
37  * Integrated `select' handling bits from the background resolver project.
38  *
39  */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <unistd.h>
51
52 #include "lbuf.h"
53 #include "sel.h"
54 #include "selbuf.h"
55
56 /*----- Main code ---------------------------------------------------------*/
57
58 /* --- @selbuf_enable@ --- *
59  *
60  * Arguments:   @selbuf *b@ = pointer to buffer block
61  *
62  * Returns:     ---
63  *
64  * Use:         Enables a buffer for reading, and emits any queued lines
65  *              to the buffer's owner.
66  */
67
68 void selbuf_enable(selbuf *b)
69 {
70   if (!(b->b.f & LBUF_ENABLE)) {
71     b->b.f |= LBUF_ENABLE;
72     sel_addfile(&b->reader);
73     lbuf_flush(&b->b, 0, 0);
74   }
75 }
76
77 /* --- @selbuf_disable@ --- *
78  *
79  * Arguments:   @selbuf *b@ = pointer to a buffer block
80  *
81  * Returns:     ---
82  *
83  * Use:         Disables a buffer.  It won't be read from until it's
84  *              enabled again.
85  */
86
87 void selbuf_disable(selbuf *b)
88 {
89   if (b->b.f & LBUF_ENABLE) {
90     b->b.f &= ~LBUF_ENABLE;
91     sel_rmfile(&b->reader);
92   }
93 }
94
95 /* --- @selbuf_read@ --- *
96  *
97  * Arguments:   @int fd@ = file descriptor to read from
98  *              @int mode@ = what we can do to the file
99  *              @void *vp@ = pointer to buffer context
100  *
101  * Returns:     ---
102  *
103  * Use:         Acts on the result of a @select@ call.
104  */
105
106 static void selbuf_read(int fd, unsigned mode, void *vp)
107 {
108   selbuf *b = vp;
109   char *p;
110   size_t sz;
111   int n;
112
113   sz = lbuf_free(&b->b, &p);
114 again:
115   n = read(fd, p, sz);
116   if (n <= 0) {
117     switch (errno) {
118       case EINTR:
119         goto again;
120       case EAGAIN:
121 #if EAGAIN != EWOULDBLOCK
122       case EWOULDBLOCK:
123 #endif
124         return;
125       default:
126         lbuf_close(&b->b);
127     }
128   }
129   else
130     lbuf_flush(&b->b, p, n);
131 }
132
133 /* --- @selbuf_init@ --- *
134  *
135  * Arguments:   @selbuf *b@ = pointer to buffer block
136  *              @sel_state *s@ = pointer to select state to attach to
137  *              @int fd@ = file descriptor to listen to
138  *              @void (*func)(char *s, void *p)@ = function to call
139  *              @void *p@ = argument for function
140  *
141  * Returns:     ---
142  *
143  * Use:         Initializes a buffer block.
144  */
145
146 void selbuf_init(selbuf *b,
147                  sel_state *s,
148                  int fd,
149                  void (*func)(char */*s*/, void */*p*/),
150                  void *p)
151 {
152   lbuf_init(&b->b, func, p);
153   b->b.f &= ~LBUF_ENABLE;
154   sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
155   selbuf_enable(b);
156 }
157
158 /*----- That's all, folks -------------------------------------------------*/