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

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