bsd_ttyqueue.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-13 Shell init ver.
  9. */
  10. #include "bsd_porting.h"
  11. /*-
  12. * SPDX-License-Identifier: BSD-2-Clause
  13. *
  14. * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
  15. * All rights reserved.
  16. *
  17. * Portions of this software were developed under sponsorship from Snow
  18. * B.V., the Netherlands.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE.
  40. */
  41. #ifndef _SYS_TTYQUEUE_H_
  42. #define _SYS_TTYQUEUE_H_
  43. #ifndef __LWP_TERMINAL_H__
  44. #error "can only be included through <teminal.h>"
  45. #endif /* !__LWP_TERMINAL_H__ */
  46. struct lwp_tty;
  47. struct ttyinq_block;
  48. struct ttyoutq_block;
  49. struct uio;
  50. /* Data input queue. */
  51. struct ttyinq
  52. {
  53. struct ttyinq_block *ti_firstblock;
  54. struct ttyinq_block *ti_startblock;
  55. struct ttyinq_block *ti_reprintblock;
  56. struct ttyinq_block *ti_lastblock;
  57. unsigned int ti_begin;
  58. unsigned int ti_linestart;
  59. unsigned int ti_reprint;
  60. unsigned int ti_end;
  61. unsigned int ti_nblocks;
  62. unsigned int ti_quota;
  63. };
  64. #define TTYINQ_DATASIZE 128
  65. /* Data output queue. */
  66. struct ttyoutq
  67. {
  68. struct ttyoutq_block *to_firstblock;
  69. struct ttyoutq_block *to_lastblock;
  70. unsigned int to_begin;
  71. unsigned int to_end;
  72. unsigned int to_nblocks;
  73. unsigned int to_quota;
  74. };
  75. #define TTYOUTQ_DATASIZE (256 - sizeof(struct ttyoutq_block *))
  76. /* Input queue handling routines. */
  77. int ttyinq_setsize(struct ttyinq *ti, struct lwp_tty *tp, size_t len);
  78. void ttyinq_free(struct ttyinq *ti);
  79. int ttyinq_read_uio(struct ttyinq *ti, struct lwp_tty *tp, struct uio *uio,
  80. size_t readlen, size_t flushlen);
  81. size_t ttyinq_write(struct ttyinq *ti, const void *buf, size_t len, int quote);
  82. int ttyinq_write_nofrag(struct ttyinq *ti, const void *buf, size_t len,
  83. int quote);
  84. void ttyinq_canonicalize(struct ttyinq *ti);
  85. size_t ttyinq_findchar(struct ttyinq *ti, const char *breakc, size_t maxlen,
  86. char *lastc);
  87. void ttyinq_flush(struct ttyinq *ti);
  88. int ttyinq_peekchar(struct ttyinq *ti, char *c, int *quote);
  89. void ttyinq_unputchar(struct ttyinq *ti);
  90. void ttyinq_reprintpos_set(struct ttyinq *ti);
  91. void ttyinq_reprintpos_reset(struct ttyinq *ti);
  92. rt_inline size_t ttyinq_getsize(struct ttyinq *ti)
  93. {
  94. return (ti->ti_nblocks * TTYINQ_DATASIZE);
  95. }
  96. rt_inline size_t ttyinq_getallocatedsize(struct ttyinq *ti)
  97. {
  98. return (ti->ti_quota * TTYINQ_DATASIZE);
  99. }
  100. rt_inline size_t ttyinq_bytesleft(struct ttyinq *ti)
  101. {
  102. size_t len;
  103. /* Make sure the usage never exceeds the length. */
  104. len = ti->ti_nblocks * TTYINQ_DATASIZE;
  105. MPASS(len >= ti->ti_end);
  106. return (len - ti->ti_end);
  107. }
  108. rt_inline size_t ttyinq_bytescanonicalized(struct ttyinq *ti)
  109. {
  110. MPASS(ti->ti_begin <= ti->ti_linestart);
  111. return (ti->ti_linestart - ti->ti_begin);
  112. }
  113. rt_inline size_t ttyinq_bytesline(struct ttyinq *ti)
  114. {
  115. MPASS(ti->ti_linestart <= ti->ti_end);
  116. return (ti->ti_end - ti->ti_linestart);
  117. }
  118. /* Input buffer iteration. */
  119. typedef void ttyinq_line_iterator_t(void *data, char c, int flags);
  120. void ttyinq_line_iterate_from_linestart(struct ttyinq *ti,
  121. ttyinq_line_iterator_t *iterator,
  122. void *data);
  123. void ttyinq_line_iterate_from_reprintpos(struct ttyinq *ti,
  124. ttyinq_line_iterator_t *iterator,
  125. void *data);
  126. /* Output queue handling routines. */
  127. void ttyoutq_flush(struct ttyoutq *to);
  128. int ttyoutq_setsize(struct ttyoutq *to, struct lwp_tty *tp, size_t len);
  129. void ttyoutq_free(struct ttyoutq *to);
  130. size_t ttyoutq_read(struct ttyoutq *to, void *buf, size_t len);
  131. int ttyoutq_read_uio(struct ttyoutq *to, struct lwp_tty *tp, struct uio *uio);
  132. size_t ttyoutq_write(struct ttyoutq *to, const void *buf, size_t len);
  133. int ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t len);
  134. rt_inline size_t ttyoutq_getsize(struct ttyoutq *to)
  135. {
  136. return (to->to_nblocks * TTYOUTQ_DATASIZE);
  137. }
  138. rt_inline size_t ttyoutq_getallocatedsize(struct ttyoutq *to)
  139. {
  140. return (to->to_quota * TTYOUTQ_DATASIZE);
  141. }
  142. rt_inline size_t ttyoutq_bytesleft(struct ttyoutq *to)
  143. {
  144. size_t len;
  145. /* Make sure the usage never exceeds the length. */
  146. len = to->to_nblocks * TTYOUTQ_DATASIZE;
  147. MPASS(len >= to->to_end);
  148. return (len - to->to_end);
  149. }
  150. rt_inline size_t ttyoutq_bytesused(struct ttyoutq *to)
  151. {
  152. return (to->to_end - to->to_begin);
  153. }
  154. #endif /* !_SYS_TTYQUEUE_H_ */