]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
d62a17ae | 2 | * Buffering to output and input. |
718e3744 | 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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #ifndef _ZEBRA_BUFFER_H | |
23 | #define _ZEBRA_BUFFER_H | |
24 | ||
5e244469 RW |
25 | #ifdef __cplusplus |
26 | extern "C" { | |
27 | #endif | |
28 | ||
9fc7ebf1 | 29 | /* Create a new buffer. Memory will be allocated in chunks of the given |
30 | size. If the argument is 0, the library will supply a reasonable | |
31 | default size suitable for buffering socket I/O. */ | |
d62a17ae | 32 | extern struct buffer *buffer_new(size_t); |
9fc7ebf1 | 33 | |
34 | /* Free all data in the buffer. */ | |
d62a17ae | 35 | extern void buffer_reset(struct buffer *); |
9fc7ebf1 | 36 | |
37 | /* This function first calls buffer_reset to release all buffered data. | |
38 | Then it frees the struct buffer itself. */ | |
d62a17ae | 39 | extern void buffer_free(struct buffer *); |
afb8b605 | 40 | |
9fc7ebf1 | 41 | /* Add the given data to the end of the buffer. */ |
d62a17ae | 42 | extern void buffer_put(struct buffer *, const void *, size_t); |
9fc7ebf1 | 43 | /* Add a single character to the end of the buffer. */ |
d7c0a89a | 44 | extern void buffer_putc(struct buffer *, uint8_t); |
9fc7ebf1 | 45 | /* Add a NUL-terminated string to the end of the buffer. */ |
d62a17ae | 46 | extern void buffer_putstr(struct buffer *, const char *); |
83eba583 DL |
47 | /* Add given data, inline-expanding \n to \r\n */ |
48 | extern void buffer_put_crlf(struct buffer *b, const void *p, size_t size); | |
9fc7ebf1 | 49 | |
afb8b605 | 50 | /* Combine all accumulated (and unflushed) data inside the buffer into a |
3b8b1855 | 51 | single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note |
52 | that this function does not alter the state of the buffer, so the data | |
53 | is still inside waiting to be flushed. */ | |
d62a17ae | 54 | char *buffer_getstr(struct buffer *); |
afb8b605 | 55 | |
9fc7ebf1 | 56 | /* Returns 1 if there is no pending data in the buffer. Otherwise returns 0. */ |
d62a17ae | 57 | int buffer_empty(struct buffer *); |
718e3744 | 58 | |
d62a17ae | 59 | typedef enum { |
60 | /* An I/O error occurred. The buffer should be destroyed and the | |
61 | file descriptor should be closed. */ | |
62 | BUFFER_ERROR = -1, | |
9fc7ebf1 | 63 | |
d62a17ae | 64 | /* The data was written successfully, and the buffer is now empty |
65 | (there is no pending data waiting to be flushed). */ | |
66 | BUFFER_EMPTY = 0, | |
9fc7ebf1 | 67 | |
d62a17ae | 68 | /* There is pending data in the buffer waiting to be flushed. Please |
69 | try flushing the buffer when select indicates that the file | |
70 | descriptor | |
71 | is writeable. */ | |
72 | BUFFER_PENDING = 1 | |
73 | } buffer_status_t; | |
9fc7ebf1 | 74 | |
75 | /* Try to write this data to the file descriptor. Any data that cannot | |
76 | be written immediately is added to the buffer queue. */ | |
d62a17ae | 77 | extern buffer_status_t buffer_write(struct buffer *, int fd, const void *, |
78 | size_t); | |
9fc7ebf1 | 79 | |
d62a17ae | 80 | /* This function attempts to flush some (but perhaps not all) of |
9fc7ebf1 | 81 | the queued data to the given file descriptor. */ |
82 | extern buffer_status_t buffer_flush_available(struct buffer *, int fd); | |
83 | ||
84 | /* The following 2 functions (buffer_flush_all and buffer_flush_window) | |
85 | are for use in lib/vty.c only. They should not be used elsewhere. */ | |
86 | ||
87 | /* Call buffer_flush_available repeatedly until either all data has been | |
88 | flushed, or an I/O error has been encountered, or the operation would | |
89 | block. */ | |
d62a17ae | 90 | extern buffer_status_t buffer_flush_all(struct buffer *, int fd); |
9fc7ebf1 | 91 | |
92 | /* Attempt to write enough data to the given fd to fill a window of the | |
93 | given width and height (and remove the data written from the buffer). | |
94 | ||
d62a17ae | 95 | If !no_more, then a message saying " --More-- " is appended. |
9fc7ebf1 | 96 | If erase is true, then first overwrite the previous " --More-- " message |
97 | with spaces. | |
98 | ||
99 | Any write error (including EAGAIN or EINTR) will cause this function | |
100 | to return -1 (because the logic for handling the erase and more features | |
101 | is too complicated to retry the write later). | |
102 | */ | |
d62a17ae | 103 | extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width, |
104 | int height, int erase, int no_more); | |
49ff6d9d | 105 | |
5e244469 RW |
106 | #ifdef __cplusplus |
107 | } | |
108 | #endif | |
109 | ||
718e3744 | 110 | #endif /* _ZEBRA_BUFFER_H */ |