1a7c91847Schristos /* CVS client logging buffer.
2a7c91847Schristos
3a7c91847Schristos This program is free software; you can redistribute it and/or modify
4a7c91847Schristos it under the terms of the GNU General Public License as published by
5a7c91847Schristos the Free Software Foundation; either version 2, or (at your option)
6a7c91847Schristos any later version.
7a7c91847Schristos
8a7c91847Schristos This program is distributed in the hope that it will be useful,
9a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
10a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11a7c91847Schristos GNU General Public License for more details. */
12*5a6c14c8Schristos #include <sys/cdefs.h>
13*5a6c14c8Schristos __RCSID("$NetBSD: ms-buffer.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
14a7c91847Schristos
15a7c91847Schristos #include <config.h>
16a7c91847Schristos
17a7c91847Schristos #include <stdio.h>
18a7c91847Schristos
19a7c91847Schristos #include "cvs.h"
20a7c91847Schristos #include "buffer.h"
21a7c91847Schristos #include "ms-buffer.h"
22a7c91847Schristos
23a7c91847Schristos #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
24a7c91847Schristos #ifdef PROXY_SUPPORT
25a7c91847Schristos
26a7c91847Schristos /* This structure is the closure field of a multi-source buffer. */
27a7c91847Schristos struct ms_buffer
28a7c91847Schristos {
29a7c91847Schristos /* Our buffer struct. */
30a7c91847Schristos struct buffer *buf;
31a7c91847Schristos
32a7c91847Schristos /* The underlying buffers. */
33a7c91847Schristos struct buffer *cur;
34a7c91847Schristos List *bufs;
35a7c91847Schristos
36a7c91847Schristos /* Whether we are in blocking mode or not. */
37a7c91847Schristos bool block;
38a7c91847Schristos };
39a7c91847Schristos
40a7c91847Schristos
41a7c91847Schristos
42a7c91847Schristos /* The block function for a multi-source buffer. */
43a7c91847Schristos static int
ms_buffer_block(void * closure,bool block)44a7c91847Schristos ms_buffer_block (void *closure, bool block)
45a7c91847Schristos {
46a7c91847Schristos struct ms_buffer *mb = closure;
47a7c91847Schristos
48a7c91847Schristos mb->block = block;
49a7c91847Schristos if (block)
50a7c91847Schristos return set_block (mb->cur);
51a7c91847Schristos else
52a7c91847Schristos return set_nonblock (mb->cur);
53a7c91847Schristos }
54a7c91847Schristos
55a7c91847Schristos
56a7c91847Schristos
57a7c91847Schristos /* The input function for a log buffer. */
58a7c91847Schristos static int
ms_buffer_input(void * closure,char * data,size_t need,size_t size,size_t * got)59a7c91847Schristos ms_buffer_input (void *closure, char *data, size_t need, size_t size,
60a7c91847Schristos size_t *got)
61a7c91847Schristos {
62a7c91847Schristos struct ms_buffer *mb = closure;
63a7c91847Schristos int status;
64a7c91847Schristos
65a7c91847Schristos assert (mb->cur->input);
66a7c91847Schristos status = (*mb->cur->input) (mb->cur->closure, data, need, size, got);
67a7c91847Schristos if (status == -1)
68a7c91847Schristos {
69a7c91847Schristos Node *p;
70a7c91847Schristos /* EOF. Set up the next buffer in line but return success and no
71a7c91847Schristos * data since our caller may have selected on the target to find
72a7c91847Schristos * ready data before calling us.
73a7c91847Schristos *
74a7c91847Schristos * If there are no more buffers, return EOF.
75a7c91847Schristos */
76a7c91847Schristos if (list_isempty (mb->bufs)) return -1;
77a7c91847Schristos buf_shutdown (mb->cur);
78a7c91847Schristos buf_free (mb->cur);
79a7c91847Schristos p = mb->bufs->list->next;
80a7c91847Schristos mb->cur = p->data;
81a7c91847Schristos p->delproc = NULL;
82a7c91847Schristos p->data = NULL;
83a7c91847Schristos delnode (p);
84a7c91847Schristos if (!buf_empty_p (mb->cur)) buf_append_buffer (mb->buf, mb->cur);
85a7c91847Schristos ms_buffer_block (closure, mb->block);
86a7c91847Schristos *got = 0;
87a7c91847Schristos status = 0;
88a7c91847Schristos }
89a7c91847Schristos
90a7c91847Schristos return status;
91a7c91847Schristos }
92a7c91847Schristos
93a7c91847Schristos
94a7c91847Schristos
95a7c91847Schristos /* Return the file descriptor underlying any child buffers. */
96a7c91847Schristos static int
ms_buffer_get_fd(void * closure)97a7c91847Schristos ms_buffer_get_fd (void *closure)
98a7c91847Schristos {
99a7c91847Schristos struct ms_buffer *mb = closure;
100a7c91847Schristos return buf_get_fd (mb->cur);
101a7c91847Schristos }
102a7c91847Schristos
103a7c91847Schristos
104a7c91847Schristos
105a7c91847Schristos /* The shutdown function for a multi-source buffer. */
106a7c91847Schristos static int
ms_buffer_shutdown(struct buffer * buf)107a7c91847Schristos ms_buffer_shutdown (struct buffer *buf)
108a7c91847Schristos {
109a7c91847Schristos struct ms_buffer *mb = buf->closure;
110a7c91847Schristos Node *p;
111a7c91847Schristos int err = 0;
112a7c91847Schristos
113a7c91847Schristos assert (mb->cur);
114a7c91847Schristos err += buf_shutdown (mb->cur);
115a7c91847Schristos buf_free (mb->cur);
116a7c91847Schristos for (p = mb->bufs->list->next; p != mb->bufs->list; p = p->next)
117a7c91847Schristos {
118a7c91847Schristos assert (p);
119a7c91847Schristos err += buf_shutdown (p->data);
120a7c91847Schristos }
121a7c91847Schristos
122a7c91847Schristos dellist (&mb->bufs);
123a7c91847Schristos return err;
124a7c91847Schristos }
125a7c91847Schristos
126a7c91847Schristos
127a7c91847Schristos
128a7c91847Schristos static void
delbuflist(Node * p)129a7c91847Schristos delbuflist (Node *p)
130a7c91847Schristos {
131a7c91847Schristos if (p->data)
132a7c91847Schristos buf_free (p->data);
133a7c91847Schristos }
134a7c91847Schristos
135a7c91847Schristos
136a7c91847Schristos
137a7c91847Schristos /* Create a multi-source buffer. This could easily be generalized to support
138a7c91847Schristos * any number of source buffers, but for now only two are necessary.
139a7c91847Schristos */
140a7c91847Schristos struct buffer *
ms_buffer_initialize(void (* memory)(struct buffer *),struct buffer * buf,struct buffer * buf2)141a7c91847Schristos ms_buffer_initialize (void (*memory) (struct buffer *),
142a7c91847Schristos struct buffer *buf, struct buffer *buf2/*, ...*/)
143a7c91847Schristos {
144a7c91847Schristos struct ms_buffer *mb = xmalloc (sizeof *mb);
145a7c91847Schristos struct buffer *retbuf;
146a7c91847Schristos Node *p;
147a7c91847Schristos
148a7c91847Schristos mb->block = false;
149a7c91847Schristos mb->cur = buf;
150a7c91847Schristos set_nonblock (buf);
151a7c91847Schristos mb->bufs = getlist ();
152a7c91847Schristos p = getnode ();
153a7c91847Schristos p->data = buf2;
154a7c91847Schristos p->delproc = delbuflist;
155a7c91847Schristos addnode (mb->bufs, p);
156a7c91847Schristos retbuf = buf_initialize (ms_buffer_input, NULL, NULL,
157a7c91847Schristos ms_buffer_block, ms_buffer_get_fd,
158a7c91847Schristos ms_buffer_shutdown, memory, mb);
159a7c91847Schristos if (!buf_empty_p (buf)) buf_append_buffer (retbuf, buf);
160a7c91847Schristos mb->buf = retbuf;
161a7c91847Schristos
162a7c91847Schristos return retbuf;
163a7c91847Schristos }
164a7c91847Schristos #endif /* PROXY_SUPPORT */
165a7c91847Schristos #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
166