xref: /dragonfly/contrib/cvs-1.12/src/buffer.h (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  * Copyright (C) 1996-2005 The Free Software Foundation, Inc.
3*86d7f5d3SJohn Marino  *
4*86d7f5d3SJohn Marino  * This program is free software; you can redistribute it and/or modify
5*86d7f5d3SJohn Marino  * it under the terms of the GNU General Public License as published by
6*86d7f5d3SJohn Marino  * the Free Software Foundation; either version 2, or (at your option)
7*86d7f5d3SJohn Marino  * any later version.
8*86d7f5d3SJohn Marino  *
9*86d7f5d3SJohn Marino  * This program is distributed in the hope that it will be useful,
10*86d7f5d3SJohn Marino  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*86d7f5d3SJohn Marino  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*86d7f5d3SJohn Marino  * GNU General Public License for more details.
13*86d7f5d3SJohn Marino  */
14*86d7f5d3SJohn Marino 
15*86d7f5d3SJohn Marino /* Declarations concerning the buffer data structure.  */
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
18*86d7f5d3SJohn Marino 
19*86d7f5d3SJohn Marino # include "getpagesize.h"
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino /*
22*86d7f5d3SJohn Marino  * We must read data from a child process and send it across the
23*86d7f5d3SJohn Marino  * network.  We do not want to block on writing to the network, so we
24*86d7f5d3SJohn Marino  * store the data from the child process in memory.  A BUFFER
25*86d7f5d3SJohn Marino  * structure holds the status of one communication, and uses a linked
26*86d7f5d3SJohn Marino  * list of buffer_data structures to hold data.
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino struct buffer;
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino typedef int (*type_buf_input) (void *, char *, size_t, size_t, size_t *);
32*86d7f5d3SJohn Marino typedef int (*type_buf_output) (void *, const char *, size_t, size_t *);
33*86d7f5d3SJohn Marino typedef int (*type_buf_flush) (void *);
34*86d7f5d3SJohn Marino typedef int (*type_buf_block) (void *, bool);
35*86d7f5d3SJohn Marino typedef int (*type_buf_get_fd) (void *);
36*86d7f5d3SJohn Marino typedef int (*type_buf_shutdown) (struct buffer *);
37*86d7f5d3SJohn Marino typedef void (*type_buf_memory_error) (struct buffer *);
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino struct buffer
40*86d7f5d3SJohn Marino {
41*86d7f5d3SJohn Marino     /* Data.  */
42*86d7f5d3SJohn Marino     struct buffer_data *data;
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino     /* Last buffer on data chain.  */
45*86d7f5d3SJohn Marino     struct buffer_data *last;
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino     /* Nonzero if the buffer is in nonblocking mode.  */
48*86d7f5d3SJohn Marino     bool nonblocking;
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino     /* Functions must be provided to transfer data in and out of the
51*86d7f5d3SJohn Marino        buffer.  Either the input or output field must be set, but not
52*86d7f5d3SJohn Marino        both.  */
53*86d7f5d3SJohn Marino 
54*86d7f5d3SJohn Marino     /* Read data into the buffer DATA.  There is room for up to SIZE
55*86d7f5d3SJohn Marino        bytes.  In blocking mode, wait until some input, at least NEED
56*86d7f5d3SJohn Marino        bytes, is available (NEED may be 0 but that is the same as NEED
57*86d7f5d3SJohn Marino        == 1).  In non-blocking mode return immediately no matter how
58*86d7f5d3SJohn Marino        much input is available; NEED is ignored. Return 0 on success,
59*86d7f5d3SJohn Marino        or -1 on end of file, or an errno code.  Set the number of
60*86d7f5d3SJohn Marino        bytes read in *GOT.
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino        If there are a nonzero number of bytes available, less than NEED,
63*86d7f5d3SJohn Marino        followed by end of file, just read those bytes and return 0.  */
64*86d7f5d3SJohn Marino     type_buf_input input;
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino     /* Write data.  This should write up to HAVE bytes from DATA.
67*86d7f5d3SJohn Marino        This should return 0 on success, or an errno code.  It should
68*86d7f5d3SJohn Marino        set the number of bytes written in *WROTE.  */
69*86d7f5d3SJohn Marino     type_buf_output output;
70*86d7f5d3SJohn Marino 
71*86d7f5d3SJohn Marino     /* Flush any data which may be buffered up after previous calls to
72*86d7f5d3SJohn Marino        OUTPUT.  This should return 0 on success, or an errno code.  */
73*86d7f5d3SJohn Marino     type_buf_flush flush;
74*86d7f5d3SJohn Marino 
75*86d7f5d3SJohn Marino     /* Change the blocking mode of the underlying communication
76*86d7f5d3SJohn Marino        stream.  If BLOCK is non-zero, it should be placed into
77*86d7f5d3SJohn Marino        blocking mode.  Otherwise, it should be placed into
78*86d7f5d3SJohn Marino        non-blocking mode.  This should return 0 on success, or an
79*86d7f5d3SJohn Marino        errno code.  */
80*86d7f5d3SJohn Marino     type_buf_block block;
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino     /* Return the file descriptor underlying this buffer, if any, or -1
83*86d7f5d3SJohn Marino      * otherwise.
84*86d7f5d3SJohn Marino      */
85*86d7f5d3SJohn Marino     type_buf_get_fd get_fd;
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino     /* Shut down the communication stream.  This does not mean that it
88*86d7f5d3SJohn Marino        should be closed.  It merely means that no more data will be
89*86d7f5d3SJohn Marino        read or written, and that any final processing that is
90*86d7f5d3SJohn Marino        appropriate should be done at this point.  This may be NULL.
91*86d7f5d3SJohn Marino        It should return 0 on success, or an errno code.  This entry
92*86d7f5d3SJohn Marino        point exists for the compression code.  */
93*86d7f5d3SJohn Marino     type_buf_shutdown shutdown;
94*86d7f5d3SJohn Marino 
95*86d7f5d3SJohn Marino     /* This field is passed to the INPUT, OUTPUT, and BLOCK functions.  */
96*86d7f5d3SJohn Marino     void *closure;
97*86d7f5d3SJohn Marino 
98*86d7f5d3SJohn Marino     /* Function to call if we can't allocate memory.  */
99*86d7f5d3SJohn Marino     type_buf_memory_error memory_error;
100*86d7f5d3SJohn Marino };
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino /* Data is stored in lists of these structures.  */
103*86d7f5d3SJohn Marino 
104*86d7f5d3SJohn Marino struct buffer_data
105*86d7f5d3SJohn Marino {
106*86d7f5d3SJohn Marino     /* Next buffer in linked list.  */
107*86d7f5d3SJohn Marino     struct buffer_data *next;
108*86d7f5d3SJohn Marino 
109*86d7f5d3SJohn Marino     /*
110*86d7f5d3SJohn Marino      * A pointer into the data area pointed to by the text field.  This
111*86d7f5d3SJohn Marino      * is where to find data that has not yet been written out.
112*86d7f5d3SJohn Marino      */
113*86d7f5d3SJohn Marino     char *bufp;
114*86d7f5d3SJohn Marino 
115*86d7f5d3SJohn Marino     /* The number of data bytes found at BUFP.  */
116*86d7f5d3SJohn Marino     size_t size;
117*86d7f5d3SJohn Marino 
118*86d7f5d3SJohn Marino     /*
119*86d7f5d3SJohn Marino      * Actual buffer.  This never changes after the structure is
120*86d7f5d3SJohn Marino      * allocated.  The buffer is BUFFER_DATA_SIZE bytes.
121*86d7f5d3SJohn Marino      */
122*86d7f5d3SJohn Marino     char *text;
123*86d7f5d3SJohn Marino };
124*86d7f5d3SJohn Marino 
125*86d7f5d3SJohn Marino /* The size we allocate for each buffer_data structure.  */
126*86d7f5d3SJohn Marino #define BUFFER_DATA_SIZE getpagesize ()
127*86d7f5d3SJohn Marino 
128*86d7f5d3SJohn Marino /* The type of a function passed as a memory error handler.  */
129*86d7f5d3SJohn Marino typedef void (*BUFMEMERRPROC) (struct buffer *);
130*86d7f5d3SJohn Marino 
131*86d7f5d3SJohn Marino struct buffer *buf_initialize (type_buf_input,
132*86d7f5d3SJohn Marino 				type_buf_output,
133*86d7f5d3SJohn Marino 				type_buf_flush,
134*86d7f5d3SJohn Marino 				type_buf_block,
135*86d7f5d3SJohn Marino 				type_buf_get_fd,
136*86d7f5d3SJohn Marino 				type_buf_shutdown,
137*86d7f5d3SJohn Marino 				type_buf_memory_error,
138*86d7f5d3SJohn Marino 				void *);
139*86d7f5d3SJohn Marino void buf_free (struct buffer *);
140*86d7f5d3SJohn Marino struct buffer *buf_nonio_initialize (void (*) (struct buffer *));
141*86d7f5d3SJohn Marino struct buffer *compress_buffer_initialize (struct buffer *, int, int,
142*86d7f5d3SJohn Marino 					   void (*) (struct buffer *));
143*86d7f5d3SJohn Marino struct buffer *packetizing_buffer_initialize
144*86d7f5d3SJohn Marino 	(struct buffer *, int (*) (void *, const char *, char *, size_t),
145*86d7f5d3SJohn Marino 	 int (*) (void *, const char *, char *, size_t, size_t *), void *,
146*86d7f5d3SJohn Marino 	 void (*) (struct buffer *));
147*86d7f5d3SJohn Marino int buf_empty (struct buffer *);
148*86d7f5d3SJohn Marino int buf_empty_p (struct buffer *);
149*86d7f5d3SJohn Marino void buf_output (struct buffer *, const char *, size_t);
150*86d7f5d3SJohn Marino void buf_output0 (struct buffer *, const char *);
151*86d7f5d3SJohn Marino void buf_append_char (struct buffer *, int);
152*86d7f5d3SJohn Marino int buf_send_output (struct buffer *);
153*86d7f5d3SJohn Marino int buf_flush (struct buffer *, bool);
154*86d7f5d3SJohn Marino int set_nonblock (struct buffer *);
155*86d7f5d3SJohn Marino int set_block (struct buffer *);
156*86d7f5d3SJohn Marino int buf_send_counted (struct buffer *);
157*86d7f5d3SJohn Marino int buf_send_special_count (struct buffer *, int);
158*86d7f5d3SJohn Marino void buf_append_data (struct buffer *, struct buffer_data *,
159*86d7f5d3SJohn Marino 		      struct buffer_data *);
160*86d7f5d3SJohn Marino void buf_append_buffer (struct buffer *, struct buffer *);
161*86d7f5d3SJohn Marino int buf_read_file (FILE *, long, struct buffer_data **, struct buffer_data **);
162*86d7f5d3SJohn Marino int buf_read_file_to_eof (FILE *, struct buffer_data **,
163*86d7f5d3SJohn Marino 			  struct buffer_data **);
164*86d7f5d3SJohn Marino int buf_input_data (struct buffer *, size_t *);
165*86d7f5d3SJohn Marino int buf_read_line (struct buffer *, char **, size_t *);
166*86d7f5d3SJohn Marino int buf_read_short_line (struct buffer *buf, char **line, size_t *lenp,
167*86d7f5d3SJohn Marino                          size_t max);
168*86d7f5d3SJohn Marino int buf_read_data (struct buffer *, size_t, char **, size_t *);
169*86d7f5d3SJohn Marino void buf_copy_lines (struct buffer *, struct buffer *, int);
170*86d7f5d3SJohn Marino int buf_copy_counted (struct buffer *, struct buffer *, int *);
171*86d7f5d3SJohn Marino int buf_chain_length (struct buffer_data *);
172*86d7f5d3SJohn Marino int buf_length (struct buffer *);
173*86d7f5d3SJohn Marino int buf_get_fd (struct buffer *);
174*86d7f5d3SJohn Marino int buf_shutdown (struct buffer *);
175*86d7f5d3SJohn Marino #ifdef PROXY_SUPPORT
176*86d7f5d3SJohn Marino void buf_copy_data (struct buffer *buf, struct buffer_data *data,
177*86d7f5d3SJohn Marino                     struct buffer_data *last);
178*86d7f5d3SJohn Marino #endif /* PROXY_SUPPORT */
179*86d7f5d3SJohn Marino void buf_free_data (struct buffer *);
180*86d7f5d3SJohn Marino 
181*86d7f5d3SJohn Marino #ifdef SERVER_FLOWCONTROL
182*86d7f5d3SJohn Marino int buf_count_mem (struct buffer *);
183*86d7f5d3SJohn Marino #endif /* SERVER_FLOWCONTROL */
184*86d7f5d3SJohn Marino 
185*86d7f5d3SJohn Marino struct buffer *
186*86d7f5d3SJohn Marino fd_buffer_initialize (int fd, pid_t child_pid, cvsroot_t *root, bool input,
187*86d7f5d3SJohn Marino                       void (*memory) (struct buffer *));
188*86d7f5d3SJohn Marino 
189*86d7f5d3SJohn Marino /* EWOULDBLOCK is not defined by POSIX, but some BSD systems will
190*86d7f5d3SJohn Marino    return it, rather than EAGAIN, for nonblocking writes.  */
191*86d7f5d3SJohn Marino # ifdef EWOULDBLOCK
192*86d7f5d3SJohn Marino #   define blocking_error(err) ((err) == EWOULDBLOCK || (err) == EAGAIN)
193*86d7f5d3SJohn Marino # else
194*86d7f5d3SJohn Marino #   define blocking_error(err) ((err) == EAGAIN)
195*86d7f5d3SJohn Marino # endif
196*86d7f5d3SJohn Marino #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
197