summaryrefslogtreecommitdiff
path: root/src/ms-buffer.c
blob: 5bf49f5c2e75003d9c10f2050af041478902516e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* CVS client logging buffer.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program 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 General Public License for more details.  */

#include <config.h>

#include <stdio.h>

#include "cvs.h"
#include "buffer.h"
#include "ms-buffer.h"

#if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
#ifdef PROXY_SUPPORT

/* This structure is the closure field of a multi-source buffer.  */
struct ms_buffer
{
    /* Our buffer struct.  */
    struct buffer *buf;

    /* The underlying buffers.  */
    struct buffer *cur;
    List *bufs;

    /* Whether we are in blocking mode or not.  */
    bool block;
};



/* The block function for a multi-source buffer.  */
static int
ms_buffer_block (void *closure, bool block)
{
    struct ms_buffer *mb = closure;

    mb->block = block;
    if (block)
	return set_block (mb->cur);
    else
	return set_nonblock (mb->cur);
}



/* The input function for a log buffer.  */
static int
ms_buffer_input (void *closure, char *data, size_t need, size_t size,
		 size_t *got)
{
    struct ms_buffer *mb = closure;
    int status;

    assert (mb->cur->input);
    status = (*mb->cur->input) (mb->cur->closure, data, need, size, got);
    if (status == -1)
    {
	Node *p;
	/* EOF.  Set up the next buffer in line but return success and no
	 * data since our caller may have selected on the target to find
	 * ready data before calling us.
	 *
	 * If there are no more buffers, return EOF.
	 */
	if (list_isempty (mb->bufs)) return -1;
	buf_shutdown (mb->cur);
	buf_free (mb->cur);
	p = mb->bufs->list->next;
	mb->cur = p->data;
	p->delproc = NULL;
	p->data = NULL;
	delnode (p);
	if (!buf_empty_p (mb->cur)) buf_append_buffer (mb->buf, mb->cur);
	ms_buffer_block (closure, mb->block);
	*got = 0;
	status = 0;
    }

    return status;
}



/* Return the file descriptor underlying any child buffers.  */
static int
ms_buffer_get_fd (void *closure)
{
    struct ms_buffer *mb = closure;
    return buf_get_fd (mb->cur);
}



/* The shutdown function for a multi-source buffer.  */
static int
ms_buffer_shutdown (struct buffer *buf)
{
    struct ms_buffer *mb = buf->closure;
    Node *p;
    int err = 0;

    assert (mb->cur);
    err += buf_shutdown (mb->cur);
    buf_free (mb->cur);
    for (p = mb->bufs->list->next; p != mb->bufs->list; p = p->next)
    {
	assert (p);
	err += buf_shutdown (p->data);
    }

    dellist (&mb->bufs);
    return err;
}



static void
delbuflist (Node *p)
{
    if (p->data)
	buf_free (p->data);
}



/* Create a multi-source buffer.  This could easily be generalized to support
 * any number of source buffers, but for now only two are necessary.
 */
struct buffer *
ms_buffer_initialize (void (*memory) (struct buffer *),
		      struct buffer *buf, struct buffer *buf2/*, ...*/)
{
    struct ms_buffer *mb = xmalloc (sizeof *mb);
    struct buffer *retbuf;
    Node *p;

    mb->block = false;
    mb->cur = buf;
    set_nonblock (buf);
    mb->bufs = getlist ();
    p = getnode ();
    p->data = buf2;
    p->delproc = delbuflist;
    addnode (mb->bufs, p);
    retbuf = buf_initialize (ms_buffer_input, NULL, NULL,
			     ms_buffer_block, ms_buffer_get_fd,
			     ms_buffer_shutdown, memory, mb);
    if (!buf_empty_p (buf)) buf_append_buffer (retbuf, buf);
    mb->buf = retbuf;

    return retbuf;
}
#endif /* PROXY_SUPPORT */
#endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */