libosmovty  0.9.3
Osmocom VTY library
include/osmocom/vty/buffer.h
00001 /*
00002  * Buffering to output and input.
00003  * Copyright (C) 1998 Kunihiro Ishiguro
00004  *
00005  * This file is part of GNU Zebra.
00006  *
00007  * GNU Zebra is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation; either version 2, or (at your
00010  * option) any later version.
00011  *
00012  * GNU Zebra is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Zebra; see the file COPYING.  If not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 #pragma once
00024 
00025 #include <sys/types.h>
00026 
00027 /* Create a new buffer.  Memory will be allocated in chunks of the given
00028    size.  If the argument is 0, the library will supply a reasonable
00029    default size suitable for buffering socket I/O. */
00030 struct buffer *buffer_new(void *ctx, size_t);
00031 
00032 /* Free all data in the buffer. */
00033 void buffer_reset(struct buffer *);
00034 
00035 /* This function first calls buffer_reset to release all buffered data.
00036    Then it frees the struct buffer itself. */
00037 void buffer_free(struct buffer *);
00038 
00039 /* Add the given data to the end of the buffer. */
00040 extern void buffer_put(struct buffer *, const void *, size_t);
00041 /* Add a single character to the end of the buffer. */
00042 extern void buffer_putc(struct buffer *, unsigned char);
00043 /* Add a NUL-terminated string to the end of the buffer. */
00044 extern void buffer_putstr(struct buffer *, const char *);
00045 
00046 /* Combine all accumulated (and unflushed) data inside the buffer into a
00047    single NUL-terminated string allocated using XMALLOC(MTYPE_TMP).  Note
00048    that this function does not alter the state of the buffer, so the data
00049    is still inside waiting to be flushed. */
00050 char *buffer_getstr(struct buffer *);
00051 
00052 /* Returns 1 if there is no pending data in the buffer.  Otherwise returns 0. */
00053 int buffer_empty(struct buffer *);
00054 
00055 typedef enum {
00056         /* An I/O error occurred.  The buffer should be destroyed and the
00057            file descriptor should be closed. */
00058         BUFFER_ERROR = -1,
00059 
00060         /* The data was written successfully, and the buffer is now empty
00061            (there is no pending data waiting to be flushed). */
00062         BUFFER_EMPTY = 0,
00063 
00064         /* There is pending data in the buffer waiting to be flushed.  Please
00065            try flushing the buffer when select indicates that the file descriptor
00066            is writeable. */
00067         BUFFER_PENDING = 1
00068 } buffer_status_t;
00069 
00070 /* Try to write this data to the file descriptor.  Any data that cannot
00071    be written immediately is added to the buffer queue. */
00072 extern buffer_status_t buffer_write(struct buffer *, int fd,
00073                                     const void *, size_t);
00074 
00075 /* This function attempts to flush some (but perhaps not all) of
00076    the queued data to the given file descriptor. */
00077 extern buffer_status_t buffer_flush_available(struct buffer *, int fd);
00078 
00079 /* The following 2 functions (buffer_flush_all and buffer_flush_window)
00080    are for use in lib/vty.c only.  They should not be used elsewhere. */
00081 
00082 /* Call buffer_flush_available repeatedly until either all data has been
00083    flushed, or an I/O error has been encountered, or the operation would
00084    block. */
00085 extern buffer_status_t buffer_flush_all(struct buffer *, int fd);
00086 
00087 /* Attempt to write enough data to the given fd to fill a window of the
00088    given width and height (and remove the data written from the buffer).
00089 
00090    If !no_more, then a message saying " --More-- " is appended.
00091    If erase is true, then first overwrite the previous " --More-- " message
00092    with spaces.
00093 
00094    Any write error (including EAGAIN or EINTR) will cause this function
00095    to return -1 (because the logic for handling the erase and more features
00096    is too complicated to retry the write later).
00097 */
00098 extern buffer_status_t buffer_flush_window(struct buffer *, int fd, int width,
00099                                            int height, int erase, int no_more);