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