xref: /openbsd/gnu/usr.bin/cvs/src/buffer.h (revision 5e617892)
1 /* Declarations concerning the buffer data structure.  */
2 
3 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
4 
5 /*
6  * We must read data from a child process and send it across the
7  * network.  We do not want to block on writing to the network, so we
8  * store the data from the child process in memory.  A BUFFER
9  * structure holds the status of one communication, and uses a linked
10  * list of buffer_data structures to hold data.
11  */
12 
13 struct buffer
14 {
15     /* Data.  */
16     struct buffer_data *data;
17 
18     /* Last buffer on data chain.  */
19     struct buffer_data *last;
20 
21     /* Nonzero if the buffer is in nonblocking mode.  */
22     int nonblocking;
23 
24     /* Functions must be provided to transfer data in and out of the
25        buffer.  Either the input or output field must be set, but not
26        both.  */
27 
28     /* Read data into the buffer DATA.  There is room for up to SIZE
29        bytes.  In blocking mode, wait until some input, at least NEED
30        bytes, is available (NEED may be 0 but that is the same as NEED
31        == 1).  In non-blocking mode return immediately no matter how
32        much input is available; NEED is ignored. Return 0 on success,
33        or -1 on end of file, or an errno code.  Set the number of
34        bytes read in *GOT.
35 
36        If there are a nonzero number of bytes available, less than NEED,
37        followed by end of file, just read those bytes and return 0.  */
38     int (*input) PROTO((void *closure, char *data, int need, int size,
39 			int *got));
40 
41     /* Write data.  This should write up to HAVE bytes from DATA.
42        This should return 0 on success, or an errno code.  It should
43        set the number of bytes written in *WROTE.  */
44     int (*output) PROTO((void *closure, const char *data, int have,
45 			 int *wrote));
46 
47     /* Flush any data which may be buffered up after previous calls to
48        OUTPUT.  This should return 0 on success, or an errno code.  */
49     int (*flush) PROTO((void *closure));
50 
51     /* Change the blocking mode of the underlying communication
52        stream.  If BLOCK is non-zero, it should be placed into
53        blocking mode.  Otherwise, it should be placed into
54        non-blocking mode.  This should return 0 on success, or an
55        errno code.  */
56     int (*block) PROTO ((void *closure, int block));
57 
58     /* Shut down the communication stream.  This does not mean that it
59        should be closed.  It merely means that no more data will be
60        read or written, and that any final processing that is
61        appropriate should be done at this point.  This may be NULL.
62        It should return 0 on success, or an errno code.  This entry
63        point exists for the compression code.  */
64     int (*shutdown) PROTO((void *closure));
65 
66     /* This field is passed to the INPUT, OUTPUT, and BLOCK functions.  */
67     void *closure;
68 
69     /* Function to call if we can't allocate memory.  */
70     void (*memory_error) PROTO((struct buffer *));
71 };
72 
73 /* Data is stored in lists of these structures.  */
74 
75 struct buffer_data
76 {
77     /* Next buffer in linked list.  */
78     struct buffer_data *next;
79 
80     /*
81      * A pointer into the data area pointed to by the text field.  This
82      * is where to find data that has not yet been written out.
83      */
84     char *bufp;
85 
86     /* The number of data bytes found at BUFP.  */
87     int size;
88 
89     /*
90      * Actual buffer.  This never changes after the structure is
91      * allocated.  The buffer is BUFFER_DATA_SIZE bytes.
92      */
93     char *text;
94 };
95 
96 /* The size we allocate for each buffer_data structure.  */
97 #define BUFFER_DATA_SIZE (4096)
98 
99 /* The type of a function passed as a memory error handler.  */
100 typedef void (*BUFMEMERRPROC) PROTO ((struct buffer *));
101 
102 extern struct buffer *buf_initialize PROTO((int (*) (void *, char *, int,
103 						     int, int *),
104 					    int (*) (void *, const char *,
105 						     int, int *),
106 					    int (*) (void *),
107 					    int (*) (void *, int),
108 					    int (*) (void *),
109 					    void (*) (struct buffer *),
110 					    void *));
111 extern void buf_free PROTO((struct buffer *));
112 extern struct buffer *buf_nonio_initialize PROTO((void (*) (struct buffer *)));
113 extern struct buffer *stdio_buffer_initialize
114   PROTO((FILE *, int, void (*) (struct buffer *)));
115 extern struct buffer *compress_buffer_initialize
116   PROTO((struct buffer *, int, int, void (*) (struct buffer *)));
117 extern struct buffer *packetizing_buffer_initialize
118   PROTO((struct buffer *, int (*) (void *, const char *, char *, int),
119 	 int (*) (void *, const char *, char *, int, int *), void *,
120 	 void (*) (struct buffer *)));
121 extern int buf_empty_p PROTO((struct buffer *));
122 extern void buf_output PROTO((struct buffer *, const char *, int));
123 extern void buf_output0 PROTO((struct buffer *, const char *));
124 extern void buf_append_char PROTO((struct buffer *, int));
125 extern int buf_send_output PROTO((struct buffer *));
126 extern int buf_flush PROTO((struct buffer *, int));
127 extern int set_nonblock PROTO((struct buffer *));
128 extern int set_block PROTO((struct buffer *));
129 extern int buf_send_counted PROTO((struct buffer *));
130 extern int buf_send_special_count PROTO((struct buffer *, int));
131 extern void buf_append_data PROTO((struct buffer *,
132 				   struct buffer_data *,
133 				   struct buffer_data *));
134 extern void buf_append_buffer PROTO((struct buffer *, struct buffer *));
135 extern int buf_read_file PROTO((FILE *, long, struct buffer_data **,
136 				struct buffer_data **));
137 extern int buf_read_file_to_eof PROTO((FILE *, struct buffer_data **,
138 				       struct buffer_data **));
139 extern int buf_input_data PROTO((struct buffer *, int *));
140 extern int buf_read_line PROTO((struct buffer *, char **, int *));
141 extern int buf_read_data PROTO((struct buffer *, int, char **, int *));
142 extern void buf_copy_lines PROTO((struct buffer *, struct buffer *, int));
143 extern int buf_copy_counted PROTO((struct buffer *, struct buffer *, int *));
144 extern int buf_chain_length PROTO((struct buffer_data *));
145 extern int buf_length PROTO((struct buffer *));
146 extern int buf_shutdown PROTO((struct buffer *));
147 
148 #ifdef SERVER_FLOWCONTROL
149 extern int buf_count_mem PROTO((struct buffer *));
150 #endif /* SERVER_FLOWCONTROL */
151 
152 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
153