Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

linebuf.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. return ( linebuf->ready ? linebuf->data : NULL );
  43. }
  44. /**
  45. * Discard line buffer contents
  46. *
  47. * @v linebuf Line buffer
  48. */
  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. * Buffer up received data by lines
  57. *
  58. * @v linebuf Line buffer
  59. * @v data New data to add
  60. * @v len Length of new data to add
  61. * @ret len Consumed length, or negative error number
  62. *
  63. * After calling line_buffer(), use buffered_line() to determine
  64. * whether or not a complete line is available. Carriage returns and
  65. * newlines will have been stripped, and the line will be
  66. * NUL-terminated. This buffered line is valid only until the next
  67. * call to line_buffer() (or to empty_line_buffer()).
  68. *
  69. * Note that line buffers use dynamically allocated storage; you
  70. * should call empty_line_buffer() before freeing a @c struct @c
  71. * line_buffer.
  72. */
  73. ssize_t line_buffer ( struct line_buffer *linebuf,
  74. const char *data, size_t len ) {
  75. const char *eol;
  76. size_t consume;
  77. size_t new_len;
  78. char *new_data;
  79. /* Free any completed line from previous iteration */
  80. if ( linebuf->ready )
  81. empty_line_buffer ( linebuf );
  82. /* Search for line terminator */
  83. if ( ( eol = memchr ( data, '\n', len ) ) ) {
  84. consume = ( eol - data + 1 );
  85. } else {
  86. consume = len;
  87. }
  88. /* Reallocate data buffer and copy in new data */
  89. new_len = ( linebuf->len + consume );
  90. new_data = realloc ( linebuf->data, ( new_len + 1 ) );
  91. if ( ! new_data )
  92. return -ENOMEM;
  93. memcpy ( ( new_data + linebuf->len ), data, consume );
  94. new_data[new_len] = '\0';
  95. linebuf->data = new_data;
  96. linebuf->len = new_len;
  97. /* If we have reached end of line, trim the line and mark as ready */
  98. if ( eol ) {
  99. linebuf->data[--linebuf->len] = '\0'; /* trim NL */
  100. if ( linebuf->data[linebuf->len - 1] == '\r' )
  101. linebuf->data[--linebuf->len] = '\0'; /* trim CR */
  102. linebuf->ready = 1;
  103. }
  104. return consume;
  105. }