blob: c0245a7f60d2e722c5811ac6955898a9941b060d [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Buffering to output and input.
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_BUFFER_H
24#define _ZEBRA_BUFFER_H
25
26/* Buffer master. */
27struct buffer
28{
29 /* Data list. */
30 struct buffer_data *head;
31 struct buffer_data *tail;
paul2265d202004-11-08 15:43:21 +000032
33 /* XXX: These unsigned longs should be size_t's */
paul718e3742002-12-13 20:15:29 +000034 /* Current allocated data. */
35 unsigned long alloc;
36
ajs49ff6d92004-11-04 19:26:16 +000037 /* Size of each buffer_data chunk. */
paul718e3742002-12-13 20:15:29 +000038 unsigned long size;
39
40 /* For allocation. */
41 struct buffer_data *unused_head;
42 struct buffer_data *unused_tail;
43
44 /* Current total length of this buffer. */
45 unsigned long length;
46};
47
48/* Data container. */
49struct buffer_data
50{
paul718e3742002-12-13 20:15:29 +000051 struct buffer_data *next;
52 struct buffer_data *prev;
53
paul718e3742002-12-13 20:15:29 +000054 /* Current pointer. */
55 unsigned long cp;
56
57 /* Start pointer. */
58 unsigned long sp;
ajs49ff6d92004-11-04 19:26:16 +000059
60 /* Actual data stream (variable length). */
61 unsigned char data[0]; /* real dimension is buffer->size */
paul718e3742002-12-13 20:15:29 +000062};
63
64/* Buffer prototypes. */
65struct buffer *buffer_new (size_t);
paul9035efa2004-10-10 11:56:56 +000066int buffer_write (struct buffer *, const void *, size_t);
paul718e3742002-12-13 20:15:29 +000067void buffer_free (struct buffer *);
ajsafb8b602005-01-28 20:41:07 +000068
69/* Combine all accumulated (and unflushed) data inside the buffer into a
ajs3b8b1852005-01-29 18:19:13 +000070 single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note
71 that this function does not alter the state of the buffer, so the data
72 is still inside waiting to be flushed. */
paul718e3742002-12-13 20:15:29 +000073char *buffer_getstr (struct buffer *);
ajsafb8b602005-01-28 20:41:07 +000074
paul718e3742002-12-13 20:15:29 +000075int buffer_putc (struct buffer *, u_char);
paul9035efa2004-10-10 11:56:56 +000076int buffer_putstr (struct buffer *, const char *);
paul718e3742002-12-13 20:15:29 +000077void buffer_reset (struct buffer *);
78int buffer_flush_all (struct buffer *, int);
79int buffer_flush_vty_all (struct buffer *, int, int, int);
80int buffer_flush_window (struct buffer *, int, int, int, int, int);
81int buffer_empty (struct buffer *);
82
ajs49ff6d92004-11-04 19:26:16 +000083/* buffer_flush_available attempts to flush the queued data to the given
84 file descriptor. It returns 0 if the buffers are now empty (after
85 flushing), or 1 if more data remains on the buffer queue (must be flushed
86 later). This function (unlike the other buffer_flush* functions) is
87 designed to work with non-blocking file descriptors. */
88int buffer_flush_available(struct buffer *, int fd);
89
paul718e3742002-12-13 20:15:29 +000090#endif /* _ZEBRA_BUFFER_H */