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

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