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