]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_subr.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1993, 1994 Theo de Raadt
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice unmodified, this list of conditions, and the following
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * We use the NetBSD based clist system, it is much more efficient than the
54 * old style clist stuff used by free bsd.
57 #include <sys/param.h>
58 #include <sys/systm.h>
60 #include <sys/ioctl.h>
62 #include <sys/malloc.h>
64 #include <machine/spl.h>
67 * At compile time, choose:
68 * There are two ways the TTY_QUOTE bit can be stored. If QBITS is
69 * defined we allocate an array of bits -- 1/8th as much memory but
70 * setbit(), clrbit(), and isset() take more cpu. If QBITS is
71 * undefined, we just use an array of bytes.
73 * If TTY_QUOTE functionality isn't required by a line discipline,
74 * it can free c_cq and set it to NULL. This speeds things up,
75 * and also does not use any extra memory. This is useful for (say)
76 * a SLIP line discipline that wants a 32K ring buffer for data
77 * but doesn't need quoting.
82 #define QMEM(n) ((((n)-1)/NBBY)+1)
97 * Initialize a particular clist. Ok, they are really ring buffers,
98 * of the specified length, with/without quoting support.
101 clalloc(clp
, size
, quot
)
107 MALLOC(clp
->c_cs
, u_char
*, size
, M_TTYS
, M_WAITOK
);
110 bzero(clp
->c_cs
, size
);
113 MALLOC(clp
->c_cq
, u_char
*, QMEM(size
), M_TTYS
, M_WAITOK
);
115 FREE(clp
->c_cs
, M_TTYS
);
118 bzero(clp
->c_cs
, QMEM(size
));
120 clp
->c_cq
= (u_char
*)0;
122 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
123 clp
->c_ce
= clp
->c_cs
+ size
;
134 FREE(clp
->c_cs
, M_TTYS
);
136 FREE(clp
->c_cq
, M_TTYS
);
137 clp
->c_cs
= clp
->c_cq
= (u_char
*)0;
142 * Get a character from a clist.
155 c
= *clp
->c_cf
& 0xff;
158 if (isset(clp
->c_cq
, clp
->c_cf
- clp
->c_cs
) )
161 if (*(clp
->c_cf
- clp
->c_cs
+ clp
->c_cq
))
165 if (++clp
->c_cf
== clp
->c_ce
)
166 clp
->c_cf
= clp
->c_cs
;
167 if (--clp
->c_cc
== 0)
168 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
175 * Copy clist to buffer.
176 * Return number of bytes moved.
179 q_to_b(clp
, cp
, count
)
189 /* optimize this while loop */
190 while (count
> 0 && clp
->c_cc
> 0) {
191 cc
= clp
->c_cl
- clp
->c_cf
;
192 if (clp
->c_cf
>= clp
->c_cl
)
193 cc
= clp
->c_ce
- clp
->c_cf
;
196 bcopy(clp
->c_cf
, p
, cc
);
201 if (clp
->c_cf
== clp
->c_ce
)
202 clp
->c_cf
= clp
->c_cs
;
205 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
211 * Return count of contiguous characters in clist.
212 * Stop counting if flag&character is non-null.
225 if ((cc
= clp
->c_cc
) == 0)
229 count
= clp
->c_cl
- clp
->c_cf
;
231 count
= clp
->c_ce
- clp
->c_cf
;
235 i
= clp
->c_cf
- clp
->c_cs
;
236 if (flag
& TTY_QUOTE
) {
237 while (cc
-- > 0 && !(clp
->c_cs
[i
++] & (flag
& ~TTY_QUOTE
) ||
238 isset(clp
->c_cq
, i
))) {
244 while (cc
-- > 0 && !(clp
->c_cs
[i
++] & flag
)) {
256 * Flush count bytes from clist.
267 if (count
== clp
->c_cc
) {
269 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
272 /* optimize this while loop */
273 while (count
> 0 && clp
->c_cc
> 0) {
274 cc
= clp
->c_cl
- clp
->c_cf
;
275 if (clp
->c_cf
>= clp
->c_cl
)
276 cc
= clp
->c_ce
- clp
->c_cf
;
282 if (clp
->c_cf
== clp
->c_ce
)
283 clp
->c_cf
= clp
->c_cs
;
286 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
292 * Put a character into the output queue.
303 if (clp
->c_cc
== 0) {
306 //printf("putc: required clalloc\n");
308 if(clalloc(clp
, 1024, 1)) {
314 clp
->c_cf
= clp
->c_cl
= clp
->c_cs
;
317 if (clp
->c_cc
== clp
->c_cn
)
320 *clp
->c_cl
= c
& 0xff;
321 i
= clp
->c_cl
- clp
->c_cs
;
325 setbit(clp
->c_cq
, i
);
327 clrbit(clp
->c_cq
, i
);
330 *q
= (c
& TTY_QUOTE
) ? 1 : 0;
335 if (clp
->c_cl
== clp
->c_ce
)
336 clp
->c_cl
= clp
->c_cs
;
343 * optimized version of
345 * for (i = 0; i < len; i++)
346 * clrbit(cp, off + len);
349 clrbits(cp
, off
, len
)
354 int sby
, sbi
, eby
, ebi
;
365 eby
= (off
+len
) / NBBY
;
366 ebi
= (off
+len
) % NBBY
;
368 mask
= ((1 << (ebi
- sbi
)) - 1) << sbi
;
377 for (i
= sby
; i
< eby
; i
++)
384 * Copy buffer to clist.
385 * Return number of bytes not transfered.
388 b_to_q(cp
, count
, clp
)
394 register u_char
*p
= cp
;
402 if (clp
->c_cc
== 0) {
405 printf("b_to_q: required clalloc\n");
407 if(clalloc(clp
, 1024, 1))
410 clp
->c_cf
= clp
->c_cl
= clp
->c_cs
;
413 if (clp
->c_cc
== clp
->c_cn
)
416 /* optimize this while loop */
417 while (count
> 0 && clp
->c_cc
< clp
->c_cn
) {
418 cc
= clp
->c_ce
- clp
->c_cl
;
419 if (clp
->c_cf
> clp
->c_cl
)
420 cc
= clp
->c_cf
- clp
->c_cl
;
423 bcopy(p
, clp
->c_cl
, cc
);
426 clrbits(clp
->c_cq
, clp
->c_cl
- clp
->c_cs
, cc
);
428 bzero(clp
->c_cl
- clp
->c_cs
+ clp
->c_cq
, cc
);
435 if (clp
->c_cl
== clp
->c_ce
)
436 clp
->c_cl
= clp
->c_cs
;
446 * Given a non-NULL pointer into the clist return the pointer
447 * to the next character in the list or return NULL if no more chars.
449 * Callers must not allow getc's to happen between firstc's and getc's
450 * so that the pointer becomes invalid. Note that interrupts are NOT
460 if (clp
->c_cf
== cp
) {
462 * First time initialization.
466 if (cc
== 0 || cp
== NULL
)
470 if (++cp
== clp
->c_ce
)
475 if (isset(clp
->c_cq
, cp
- clp
->c_cs
))
478 if (*(clp
->c_cf
- clp
->c_cs
+ clp
->c_cq
))
486 * Given a non-NULL pointer into the clist return the pointer
487 * to the first character in the list or return NULL if no more chars.
489 * Callers must not allow getc's to happen between firstc's and getc's
490 * so that the pointer becomes invalid. Note that interrupts are NOT
493 * *c is set to the NEXT character
509 if (isset(clp
->c_cq
, cp
- clp
->c_cs
))
512 if (*(cp
- clp
->c_cs
+ clp
->c_cq
))
520 * Remove the last character in the clist and return it.
533 if (clp
->c_cl
== clp
->c_cs
)
534 clp
->c_cl
= clp
->c_ce
- 1;
539 c
= *clp
->c_cl
& 0xff;
542 if (isset(clp
->c_cq
, clp
->c_cl
- clp
->c_cs
))
545 if (*(clp
->c_cf
- clp
->c_cs
+ clp
->c_cq
))
550 clp
->c_cf
= clp
->c_cl
= (u_char
*)0;
557 * Put the chars in the from queue on the end of the to queue.
561 struct clist
*from
, *to
;
565 while ((c
= getc(from
)) != -1)