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