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