2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 #include <ipxe/linebuf.h>
40 * Retrieve buffered-up line
42 * @v linebuf Line buffer
43 * @ret line Buffered line, or NULL if no line ready to read
45 char * buffered_line ( struct line_buffer *linebuf ) {
46 char *line = &linebuf->data[ linebuf->len ];
48 /* Fail unless we have a newly completed line to retrieve */
49 if ( ( linebuf->len == 0 ) || ( linebuf->consumed == 0 ) ||
50 ( *(--line) != '\0' ) )
53 /* Identify start of line */
54 while ( ( line > linebuf->data ) && ( line[-1] != '\0' ) )
61 * Discard line buffer contents
63 * @v linebuf Line buffer
65 void empty_line_buffer ( struct line_buffer *linebuf ) {
67 free ( linebuf->data );
70 linebuf->consumed = 0;
74 * Buffer up received data by lines
76 * @v linebuf Line buffer
77 * @v data New data to add
78 * @v len Length of new data to add
79 * @ret len Consumed length, or negative error number
81 * After calling line_buffer(), use buffered_line() to determine
82 * whether or not a complete line is available. Carriage returns and
83 * newlines will have been stripped, and the line will be
84 * NUL-terminated. This buffered line is valid only until the next
85 * call to line_buffer() (or to empty_line_buffer()).
87 * Note that line buffers use dynamically allocated storage; you
88 * should call empty_line_buffer() before freeing a @c struct @c
91 int line_buffer ( struct line_buffer *linebuf, const char *data, size_t len ) {
99 /* Search for line terminator */
100 if ( ( eol = memchr ( data, '\n', len ) ) ) {
101 consume = ( eol - data + 1 );
106 /* Reject any embedded NULs within the data to be consumed */
107 if ( memchr ( data, '\0', consume ) )
110 /* Reallocate data buffer and copy in new data */
111 new_len = ( linebuf->len + consume );
112 new_data = realloc ( linebuf->data, ( new_len + 1 ) );
115 memcpy ( ( new_data + linebuf->len ), data, consume );
116 new_data[new_len] = '\0';
117 linebuf->data = new_data;
118 linebuf->len = new_len;
120 /* If we have reached end of line, terminate the line */
123 /* Overwrite trailing LF (which must exist at this point) */
124 assert ( linebuf->len > 0 );
125 lf = &linebuf->data[ linebuf->len - 1 ];
126 assert ( *lf == '\n' );
129 /* Trim (and overwrite) trailing CR, if present */
130 if ( linebuf->len > 1 ) {
139 /* Record consumed length */
140 linebuf->consumed = consume;