blob: 56c28f04e81bef43216729c68f38ec8b64f40905 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/*
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
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010019 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
Harald Welte3fb0b6f2010-05-19 19:02:52 +020021 */
22
Sylvain Munaut12ba7782014-06-16 10:13:40 +020023#pragma once
Harald Welte3fb0b6f2010-05-19 19:02:52 +020024
25#include <sys/types.h>
26
27/* Create a new buffer. Memory will be allocated in chunks of the given
28 size. If the argument is 0, the library will supply a reasonable
29 default size suitable for buffering socket I/O. */
30struct buffer *buffer_new(void *ctx, size_t);
31
32/* Free all data in the buffer. */
33void buffer_reset(struct buffer *);
34
35/* This function first calls buffer_reset to release all buffered data.
36 Then it frees the struct buffer itself. */
37void buffer_free(struct buffer *);
38
39/* Add the given data to the end of the buffer. */
40extern void buffer_put(struct buffer *, const void *, size_t);
41/* Add a single character to the end of the buffer. */
Harald Welte7b74dda2014-03-11 10:31:19 +010042extern void buffer_putc(struct buffer *, unsigned char);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020043/* Add a NUL-terminated string to the end of the buffer. */
44extern void buffer_putstr(struct buffer *, const char *);
45
46/* Combine all accumulated (and unflushed) data inside the buffer into a
47 single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note
48 that this function does not alter the state of the buffer, so the data
49 is still inside waiting to be flushed. */
50char *buffer_getstr(struct buffer *);
51
52/* Returns 1 if there is no pending data in the buffer. Otherwise returns 0. */
53int buffer_empty(struct buffer *);
54
55typedef enum {
56 /* An I/O error occurred. The buffer should be destroyed and the
57 file descriptor should be closed. */
58 BUFFER_ERROR = -1,
59
60 /* The data was written successfully, and the buffer is now empty
61 (there is no pending data waiting to be flushed). */
62 BUFFER_EMPTY = 0,
63
64 /* There is pending data in the buffer waiting to be flushed. Please
65 try flushing the buffer when select indicates that the file descriptor
66 is writeable. */
67 BUFFER_PENDING = 1
68} buffer_status_t;
69
70/* Try to write this data to the file descriptor. Any data that cannot
71 be written immediately is added to the buffer queue. */
72extern buffer_status_t buffer_write(struct buffer *, int fd,
73 const void *, size_t);
74
75/* This function attempts to flush some (but perhaps not all) of
76 the queued data to the given file descriptor. */
77extern buffer_status_t buffer_flush_available(struct buffer *, int fd);
78
79/* The following 2 functions (buffer_flush_all and buffer_flush_window)
80 are for use in lib/vty.c only. They should not be used elsewhere. */
81
82/* Call buffer_flush_available repeatedly until either all data has been
83 flushed, or an I/O error has been encountered, or the operation would
84 block. */
85extern buffer_status_t buffer_flush_all(struct buffer *, int fd);
86
87/* Attempt to write enough data to the given fd to fill a window of the
88 given width and height (and remove the data written from the buffer).
89
90 If !no_more, then a message saying " --More-- " is appended.
91 If erase is true, then first overwrite the previous " --More-- " message
92 with spaces.
93
94 Any write error (including EAGAIN or EINTR) will cause this function
95 to return -1 (because the logic for handling the erase and more features
96 is too complicated to retry the write later).
97*/
98extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width,
99 int height, int erase, int no_more);