]> git.proxmox.com Git - mirror_frr.git/blob - lib/buffer.h
Merge pull request #13108 from opensourcerouting/clippy-string-warn
[mirror_frr.git] / lib / buffer.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Buffering to output and input.
4 * Copyright (C) 1998 Kunihiro Ishiguro
5 */
6
7 #ifndef _ZEBRA_BUFFER_H
8 #define _ZEBRA_BUFFER_H
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 /* Create a new buffer. Memory will be allocated in chunks of the given
15 size. If the argument is 0, the library will supply a reasonable
16 default size suitable for buffering socket I/O. */
17 extern struct buffer *buffer_new(size_t);
18
19 /* Free all data in the buffer. */
20 extern void buffer_reset(struct buffer *);
21
22 /* This function first calls buffer_reset to release all buffered data.
23 Then it frees the struct buffer itself. */
24 extern void buffer_free(struct buffer *);
25
26 /* Add the given data to the end of the buffer. */
27 extern void buffer_put(struct buffer *, const void *, size_t);
28 /* Add a single character to the end of the buffer. */
29 extern void buffer_putc(struct buffer *, uint8_t);
30 /* Add a NUL-terminated string to the end of the buffer. */
31 extern void buffer_putstr(struct buffer *, const char *);
32 /* Add given data, inline-expanding \n to \r\n */
33 extern void buffer_put_crlf(struct buffer *b, const void *p, size_t size);
34
35 /* Combine all accumulated (and unflushed) data inside the buffer into a
36 single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note
37 that this function does not alter the state of the buffer, so the data
38 is still inside waiting to be flushed. */
39 char *buffer_getstr(struct buffer *);
40
41 /* Returns 1 if there is no pending data in the buffer. Otherwise returns 0. */
42 int buffer_empty(struct buffer *);
43
44 typedef enum {
45 /* An I/O error occurred. The buffer should be destroyed and the
46 file descriptor should be closed. */
47 BUFFER_ERROR = -1,
48
49 /* The data was written successfully, and the buffer is now empty
50 (there is no pending data waiting to be flushed). */
51 BUFFER_EMPTY = 0,
52
53 /* There is pending data in the buffer waiting to be flushed. Please
54 try flushing the buffer when select indicates that the file
55 descriptor
56 is writeable. */
57 BUFFER_PENDING = 1
58 } buffer_status_t;
59
60 /* Try to write this data to the file descriptor. Any data that cannot
61 be written immediately is added to the buffer queue. */
62 extern buffer_status_t buffer_write(struct buffer *, int fd, const void *,
63 size_t);
64
65 /* This function attempts to flush some (but perhaps not all) of
66 the queued data to the given file descriptor. */
67 extern buffer_status_t buffer_flush_available(struct buffer *, int fd);
68
69 /* The following 2 functions (buffer_flush_all and buffer_flush_window)
70 are for use in lib/vty.c only. They should not be used elsewhere. */
71
72 /* Call buffer_flush_available repeatedly until either all data has been
73 flushed, or an I/O error has been encountered, or the operation would
74 block. */
75 extern buffer_status_t buffer_flush_all(struct buffer *, int fd);
76
77 /* Attempt to write enough data to the given fd to fill a window of the
78 given width and height (and remove the data written from the buffer).
79
80 If !no_more, then a message saying " --More-- " is appended.
81 If erase is true, then first overwrite the previous " --More-- " message
82 with spaces.
83
84 Any write error (including EAGAIN or EINTR) will cause this function
85 to return -1 (because the logic for handling the erase and more features
86 is too complicated to retry the write later).
87 */
88 extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width,
89 int height, int erase, int no_more);
90
91 #ifdef __cplusplus
92 }
93 #endif
94
95 #endif /* _ZEBRA_BUFFER_H */