stream.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU 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, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <kern/stream.h>
  19. #include <kern/console.h>
  20. struct console_stream
  21. {
  22. struct stream base;
  23. cpu_flags_t flags;
  24. };
  25. static struct console_stream console_stream_storage;
  26. struct stream *console_stream;
  27. static void
  28. console_stream_write (struct stream *strm __unused,
  29. const void *data, uint32_t bytes)
  30. {
  31. console_puts_nolock (data, bytes);
  32. }
  33. static int32_t
  34. console_stream_read (struct stream *strm __unused,
  35. void *data, uint32_t bytes)
  36. {
  37. return ((int32_t)console_gets_nolock ((char *)data, bytes));
  38. }
  39. static void
  40. console_stream_lock (struct stream *strm)
  41. {
  42. _Auto cs = structof (strm, struct console_stream, base);
  43. console_lock (&cs->flags);
  44. }
  45. static void
  46. console_stream_unlock (struct stream *strm)
  47. {
  48. _Auto cs = structof (strm, struct console_stream, base);
  49. console_unlock (cs->flags);
  50. }
  51. static const struct stream_ops console_stream_ops =
  52. {
  53. .write = console_stream_write,
  54. .read = console_stream_read,
  55. .lock = console_stream_lock,
  56. .unlock = console_stream_unlock
  57. };
  58. static void
  59. string_stream_write (struct stream *strm, const void *data, uint32_t bytes)
  60. {
  61. _Auto ssp = (struct string_stream *) strm;
  62. if (ssp->cur >= ssp->size)
  63. return;
  64. size_t left = MIN (ssp->size - ssp->cur, bytes);
  65. memcpy (ssp->ptr + ssp->cur, data, left);
  66. ssp->cur += left;
  67. }
  68. static int32_t
  69. string_stream_read (struct stream *strm, void *out, uint32_t bytes)
  70. {
  71. _Auto ssp = (struct string_stream *) strm;
  72. if (ssp->cur >= ssp->size)
  73. return (0);
  74. size_t left = MIN (ssp->size - ssp->cur, bytes);
  75. memcpy (out, ssp->ptr + ssp->cur, left);
  76. ssp->cur += left;
  77. return ((int32_t)left);
  78. }
  79. static const struct stream_ops string_stream_ops =
  80. {
  81. .write = string_stream_write,
  82. .read = string_stream_read,
  83. };
  84. void string_stream_init (struct string_stream *strm, char *ptr, size_t size)
  85. {
  86. strm->ptr = ptr;
  87. strm->size = size;
  88. strm->cur = 0;
  89. strm->base.ops = &string_stream_ops;
  90. }
  91. static int __init
  92. stream_setup (void)
  93. {
  94. console_stream = &console_stream_storage.base;
  95. stream_init (console_stream, &console_stream_ops);
  96. return (0);
  97. }
  98. INIT_OP_DEFINE (stream_setup,
  99. INIT_OP_DEP (console_setup, true));