blob: 87cb3fde49edc0f4e20b95df30578b30b51ee819 [file] [log] [blame]
Harald Welte955049f2009-03-10 12:16:51 +00001/*
2 * Buffering to output and input.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_BUFFER_H
24#define _ZEBRA_BUFFER_H
25
26/* Create a new buffer. Memory will be allocated in chunks of the given
27 size. If the argument is 0, the library will supply a reasonable
28 default size suitable for buffering socket I/O. */
29struct buffer *buffer_new(size_t);
30
31/* Free all data in the buffer. */
32void buffer_reset(struct buffer *);
33
34/* This function first calls buffer_reset to release all buffered data.
35 Then it frees the struct buffer itself. */
36void buffer_free(struct buffer *);
37
38/* Add the given data to the end of the buffer. */
39extern void buffer_put(struct buffer *, const void *, size_t);
40/* Add a single character to the end of the buffer. */
41extern void buffer_putc(struct buffer *, u_char);
42/* Add a NUL-terminated string to the end of the buffer. */
43extern void buffer_putstr(struct buffer *, const char *);
44
45/* Combine all accumulated (and unflushed) data inside the buffer into a
46 single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note
47 that this function does not alter the state of the buffer, so the data
48 is still inside waiting to be flushed. */
49char *buffer_getstr(struct buffer *);
50
51/* Returns 1 if there is no pending data in the buffer. Otherwise returns 0. */
52int buffer_empty(struct buffer *);
53
54typedef enum {
55 /* An I/O error occurred. The buffer should be destroyed and the
56 file descriptor should be closed. */
57 BUFFER_ERROR = -1,
58
59 /* The data was written successfully, and the buffer is now empty
60 (there is no pending data waiting to be flushed). */
61 BUFFER_EMPTY = 0,
62
63 /* There is pending data in the buffer waiting to be flushed. Please
64 try flushing the buffer when select indicates that the file descriptor
65 is writeable. */
66 BUFFER_PENDING = 1
67} buffer_status_t;
68
69/* Try to write this data to the file descriptor. Any data that cannot
70 be written immediately is added to the buffer queue. */
71extern buffer_status_t buffer_write(struct buffer *, int fd,
72 const void *, size_t);
73
74/* This function attempts to flush some (but perhaps not all) of
75 the queued data to the given file descriptor. */
76extern buffer_status_t buffer_flush_available(struct buffer *, int fd);
77
78/* The following 2 functions (buffer_flush_all and buffer_flush_window)
79 are for use in lib/vty.c only. They should not be used elsewhere. */
80
81/* Call buffer_flush_available repeatedly until either all data has been
82 flushed, or an I/O error has been encountered, or the operation would
83 block. */
84extern buffer_status_t buffer_flush_all(struct buffer *, int fd);
85
86/* Attempt to write enough data to the given fd to fill a window of the
87 given width and height (and remove the data written from the buffer).
88
89 If !no_more, then a message saying " --More-- " is appended.
90 If erase is true, then first overwrite the previous " --More-- " message
91 with spaces.
92
93 Any write error (including EAGAIN or EINTR) will cause this function
94 to return -1 (because the logic for handling the erase and more features
95 is too complicated to retry the write later).
96*/
97extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width,
98 int height, int erase, int no_more);
99
100#endif /* _ZEBRA_BUFFER_H */