• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 FILE_LICENCE ( GPL2_OR_LATER );
20 
21 /**
22  * @file
23  *
24  * Line buffering
25  *
26  */
27 
28 #include <stdint.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <gpxe/linebuf.h>
33 
34 /**
35  * Retrieve buffered-up line
36  *
37  * @v linebuf		Line buffer
38  * @ret line		Buffered line, or NULL if no line ready to read
39  */
buffered_line(struct line_buffer * linebuf)40 char * buffered_line ( struct line_buffer *linebuf ) {
41 	return ( linebuf->ready ? linebuf->data : NULL );
42 }
43 
44 /**
45  * Discard line buffer contents
46  *
47  * @v linebuf		Line buffer
48  */
empty_line_buffer(struct line_buffer * linebuf)49 void empty_line_buffer ( struct line_buffer *linebuf ) {
50 	free ( linebuf->data );
51 	linebuf->data = NULL;
52 	linebuf->len = 0;
53 	linebuf->ready = 0;
54 }
55 
56 /**
57  * Buffer up received data by lines
58  *
59  * @v linebuf			Line buffer
60  * @v data			New data to add
61  * @v len			Length of new data to add
62  * @ret len			Consumed length, or negative error number
63  *
64  * After calling line_buffer(), use buffered_line() to determine
65  * whether or not a complete line is available.  Carriage returns and
66  * newlines will have been stripped, and the line will be
67  * NUL-terminated.  This buffered line is valid only until the next
68  * call to line_buffer() (or to empty_line_buffer()).
69  *
70  * Note that line buffers use dynamically allocated storage; you
71  * should call empty_line_buffer() before freeing a @c struct @c
72  * line_buffer.
73  */
line_buffer(struct line_buffer * linebuf,const char * data,size_t len)74 ssize_t line_buffer ( struct line_buffer *linebuf,
75 		      const char *data, size_t len ) {
76 	const char *eol;
77 	size_t consume;
78 	size_t new_len;
79 	char *new_data;
80 
81 	/* Free any completed line from previous iteration */
82 	if ( linebuf->ready )
83 		empty_line_buffer ( linebuf );
84 
85 	/* Search for line terminator */
86 	if ( ( eol = memchr ( data, '\n', len ) ) ) {
87 		consume = ( eol - data + 1 );
88 	} else {
89 		consume = len;
90 	}
91 
92 	/* Reallocate data buffer and copy in new data */
93 	new_len = ( linebuf->len + consume );
94 	new_data = realloc ( linebuf->data, ( new_len + 1 ) );
95 	if ( ! new_data )
96 		return -ENOMEM;
97 	memcpy ( ( new_data + linebuf->len ), data, consume );
98 	new_data[new_len] = '\0';
99 	linebuf->data = new_data;
100 	linebuf->len = new_len;
101 
102 	/* If we have reached end of line, trim the line and mark as ready */
103 	if ( eol ) {
104 		linebuf->data[--linebuf->len] = '\0'; /* trim NL */
105 		if ( linebuf->data[linebuf->len - 1] == '\r' )
106 			linebuf->data[--linebuf->len] = '\0'; /* trim CR */
107 		linebuf->ready = 1;
108 	}
109 
110 	return consume;
111 }
112