]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/buffer.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / buffer.h
index 65b8a8ca29675c545e35e5df74911d03466f9883..0c945a2acfea3fbea58ff97fabea08174d29fd72 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Buffering to output and input. 
+ * Buffering to output and input.
  * Copyright (C) 1998 Kunihiro Ishiguro
  *
  * This file is part of GNU Zebra.
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with GNU Zebra; see the file COPYING.  If not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #ifndef _ZEBRA_BUFFER_H
 #define _ZEBRA_BUFFER_H
 
-/* Buffer master. */
-struct buffer
-{
-  /* Data list. */
-  struct buffer_data *head;
-  struct buffer_data *tail;
-  
-  /* XXX: These unsigned longs should be size_t's */
-  /* Current allocated data. */
-  unsigned long alloc;
-
-  /* Size of each buffer_data chunk. */
-  unsigned long size;
-
-  /* For allocation. */
-  struct buffer_data *unused_head;
-  struct buffer_data *unused_tail;
-
-  /* Current total length of this buffer. */
-  unsigned long length;
-};
-
-/* Data container. */
-struct buffer_data
-{
-  struct buffer_data *next;
-  struct buffer_data *prev;
-
-  /* Current pointer. */
-  unsigned long cp;
-
-  /* Start pointer. */
-  unsigned long sp;
-
-  /* Actual data stream (variable length). */
-  unsigned char data[0];  /* real dimension is buffer->size */
-};
-
-/* Buffer prototypes. */
-struct buffer *buffer_new (size_t);
-int buffer_write (struct buffer *, const void *, size_t);
-void buffer_free (struct buffer *);
-char *buffer_getstr (struct buffer *);
-int buffer_putc (struct buffer *, u_char);
-int buffer_putstr (struct buffer *, const char *);
-void buffer_reset (struct buffer *);
-int buffer_flush_all (struct buffer *, int);
-int buffer_flush_vty_all (struct buffer *, int, int, int);
-int buffer_flush_window (struct buffer *, int, int, int, int, int);
-int buffer_empty (struct buffer *);
-
-/* buffer_flush_available attempts to flush the queued data to the given
-   file descriptor.  It returns 0 if the buffers are now empty (after
-   flushing), or 1 if more data remains on the buffer queue (must be flushed
-   later).  This function (unlike the other buffer_flush* functions) is
-   designed to work with non-blocking file descriptors. */
-int buffer_flush_available(struct buffer *, int fd);
+/* Create a new buffer.  Memory will be allocated in chunks of the given
+   size.  If the argument is 0, the library will supply a reasonable
+   default size suitable for buffering socket I/O. */
+extern struct buffer *buffer_new(size_t);
+
+/* Free all data in the buffer. */
+extern void buffer_reset(struct buffer *);
+
+/* This function first calls buffer_reset to release all buffered data.
+   Then it frees the struct buffer itself. */
+extern void buffer_free(struct buffer *);
+
+/* Add the given data to the end of the buffer. */
+extern void buffer_put(struct buffer *, const void *, size_t);
+/* Add a single character to the end of the buffer. */
+extern void buffer_putc(struct buffer *, uint8_t);
+/* Add a NUL-terminated string to the end of the buffer. */
+extern void buffer_putstr(struct buffer *, const char *);
+/* Add given data, inline-expanding \n to \r\n */
+extern void buffer_put_crlf(struct buffer *b, const void *p, size_t size);
+
+/* Combine all accumulated (and unflushed) data inside the buffer into a
+   single NUL-terminated string allocated using XMALLOC(MTYPE_TMP).  Note
+   that this function does not alter the state of the buffer, so the data
+   is still inside waiting to be flushed. */
+char *buffer_getstr(struct buffer *);
+
+/* Returns 1 if there is no pending data in the buffer.  Otherwise returns 0. */
+int buffer_empty(struct buffer *);
+
+typedef enum {
+       /* An I/O error occurred.  The buffer should be destroyed and the
+          file descriptor should be closed. */
+       BUFFER_ERROR = -1,
+
+       /* The data was written successfully, and the buffer is now empty
+          (there is no pending data waiting to be flushed). */
+       BUFFER_EMPTY = 0,
+
+       /* There is pending data in the buffer waiting to be flushed.  Please
+          try flushing the buffer when select indicates that the file
+          descriptor
+          is writeable. */
+       BUFFER_PENDING = 1
+} buffer_status_t;
+
+/* Try to write this data to the file descriptor.  Any data that cannot
+   be written immediately is added to the buffer queue. */
+extern buffer_status_t buffer_write(struct buffer *, int fd, const void *,
+                                   size_t);
+
+/* This function attempts to flush some (but perhaps not all) of
+   the queued data to the given file descriptor. */
+extern buffer_status_t buffer_flush_available(struct buffer *, int fd);
+
+/* The following 2 functions (buffer_flush_all and buffer_flush_window)
+   are for use in lib/vty.c only.  They should not be used elsewhere. */
+
+/* Call buffer_flush_available repeatedly until either all data has been
+   flushed, or an I/O error has been encountered, or the operation would
+   block. */
+extern buffer_status_t buffer_flush_all(struct buffer *, int fd);
+
+/* Attempt to write enough data to the given fd to fill a window of the
+   given width and height (and remove the data written from the buffer).
+
+   If !no_more, then a message saying " --More-- " is appended.
+   If erase is true, then first overwrite the previous " --More-- " message
+   with spaces.
+
+   Any write error (including EAGAIN or EINTR) will cause this function
+   to return -1 (because the logic for handling the erase and more features
+   is too complicated to retry the write later).
+*/
+extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width,
+                                          int height, int erase, int no_more);
 
 #endif /* _ZEBRA_BUFFER_H */