You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linebuf.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * Line buffering
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <ipxe/linebuf.h>
  35. /**
  36. * Retrieve buffered-up line
  37. *
  38. * @v linebuf Line buffer
  39. * @ret line Buffered line, or NULL if no line ready to read
  40. */
  41. char * buffered_line ( struct line_buffer *linebuf ) {
  42. char *line = &linebuf->data[ linebuf->len ];
  43. /* Fail unless we have a newly completed line to retrieve */
  44. if ( ( linebuf->len == 0 ) || ( linebuf->consumed == 0 ) ||
  45. ( *(--line) != '\0' ) )
  46. return NULL;
  47. /* Identify start of line */
  48. while ( ( line > linebuf->data ) && ( line[-1] != '\0' ) )
  49. line--;
  50. return line;
  51. }
  52. /**
  53. * Discard line buffer contents
  54. *
  55. * @v linebuf Line buffer
  56. */
  57. void empty_line_buffer ( struct line_buffer *linebuf ) {
  58. free ( linebuf->data );
  59. linebuf->data = NULL;
  60. linebuf->len = 0;
  61. linebuf->consumed = 0;
  62. }
  63. /**
  64. * Buffer up received data by lines
  65. *
  66. * @v linebuf Line buffer
  67. * @v data New data to add
  68. * @v len Length of new data to add
  69. * @ret len Consumed length, or negative error number
  70. *
  71. * After calling line_buffer(), use buffered_line() to determine
  72. * whether or not a complete line is available. Carriage returns and
  73. * newlines will have been stripped, and the line will be
  74. * NUL-terminated. This buffered line is valid only until the next
  75. * call to line_buffer() (or to empty_line_buffer()).
  76. *
  77. * Note that line buffers use dynamically allocated storage; you
  78. * should call empty_line_buffer() before freeing a @c struct @c
  79. * line_buffer.
  80. */
  81. int line_buffer ( struct line_buffer *linebuf, const char *data, size_t len ) {
  82. const char *eol;
  83. size_t consume;
  84. size_t new_len;
  85. char *new_data;
  86. char *lf;
  87. char *cr;
  88. /* Search for line terminator */
  89. if ( ( eol = memchr ( data, '\n', len ) ) ) {
  90. consume = ( eol - data + 1 );
  91. } else {
  92. consume = len;
  93. }
  94. /* Reject any embedded NULs within the data to be consumed */
  95. if ( memchr ( data, '\0', consume ) )
  96. return -EINVAL;
  97. /* Reallocate data buffer and copy in new data */
  98. new_len = ( linebuf->len + consume );
  99. new_data = realloc ( linebuf->data, ( new_len + 1 ) );
  100. if ( ! new_data )
  101. return -ENOMEM;
  102. memcpy ( ( new_data + linebuf->len ), data, consume );
  103. new_data[new_len] = '\0';
  104. linebuf->data = new_data;
  105. linebuf->len = new_len;
  106. /* If we have reached end of line, terminate the line */
  107. if ( eol ) {
  108. /* Overwrite trailing LF (which must exist at this point) */
  109. assert ( linebuf->len > 0 );
  110. lf = &linebuf->data[ linebuf->len - 1 ];
  111. assert ( *lf == '\n' );
  112. *lf = '\0';
  113. /* Trim (and overwrite) trailing CR, if present */
  114. if ( linebuf->len > 1 ) {
  115. cr = ( lf - 1 );
  116. if ( *cr == '\r' ) {
  117. linebuf->len--;
  118. *cr = '\0';
  119. }
  120. }
  121. }
  122. /* Record consumed length */
  123. linebuf->consumed = consume;
  124. return consume;
  125. }