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.0KB

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