Home | History | Annotate | Line # | Download | only in kern
tty_subr.c revision 1.28.4.1
      1  1.28.4.1      yamt /*	$NetBSD: tty_subr.c,v 1.28.4.1 2006/06/21 15:09:39 yamt Exp $	*/
      2       1.8       cgd 
      3       1.1   mycroft /*
      4       1.7   deraadt  * Copyright (c) 1993, 1994 Theo de Raadt
      5       1.1   mycroft  * All rights reserved.
      6       1.1   mycroft  *
      7       1.1   mycroft  * Per Lindqvist <pgd (at) compuram.bbt.se> supplied an almost fully working
      8       1.1   mycroft  * set of true clist functions that this is very loosely based on.
      9       1.1   mycroft  *
     10       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.1   mycroft  * modification, are permitted provided that the following conditions
     12       1.1   mycroft  * are met:
     13       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.1   mycroft  *
     19       1.7   deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.7   deraadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.7   deraadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.7   deraadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.7   deraadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.7   deraadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.7   deraadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.7   deraadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.7   deraadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.7   deraadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1   mycroft  */
     30      1.21     lukem 
     31      1.21     lukem #include <sys/cdefs.h>
     32  1.28.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: tty_subr.c,v 1.28.4.1 2006/06/21 15:09:39 yamt Exp $");
     33       1.1   mycroft 
     34       1.3   mycroft #include <sys/param.h>
     35       1.3   mycroft #include <sys/systm.h>
     36       1.3   mycroft #include <sys/buf.h>
     37       1.3   mycroft #include <sys/ioctl.h>
     38       1.3   mycroft #include <sys/tty.h>
     39       1.3   mycroft #include <sys/malloc.h>
     40      1.24   thorpej 
     41      1.24   thorpej MALLOC_DEFINE(M_TTYS, "ttys", "allocated tty structures");
     42       1.1   mycroft 
     43       1.1   mycroft /*
     44       1.1   mycroft  * At compile time, choose:
     45       1.1   mycroft  * There are two ways the TTY_QUOTE bit can be stored. If QBITS is
     46       1.1   mycroft  * defined we allocate an array of bits -- 1/8th as much memory but
     47      1.26       wiz  * setbit(), clrbit(), and isset() take more CPU. If QBITS is
     48       1.1   mycroft  * undefined, we just use an array of bytes.
     49      1.28     perry  *
     50       1.1   mycroft  * If TTY_QUOTE functionality isn't required by a line discipline,
     51       1.1   mycroft  * it can free c_cq and set it to NULL. This speeds things up,
     52       1.1   mycroft  * and also does not use any extra memory. This is useful for (say)
     53       1.1   mycroft  * a SLIP line discipline that wants a 32K ring buffer for data
     54       1.1   mycroft  * but doesn't need quoting.
     55       1.1   mycroft  */
     56       1.1   mycroft #define QBITS
     57       1.1   mycroft 
     58       1.1   mycroft #ifdef QBITS
     59       1.1   mycroft #define QMEM(n)		((((n)-1)/NBBY)+1)
     60       1.1   mycroft #else
     61       1.1   mycroft #define QMEM(n)		(n)
     62       1.1   mycroft #endif
     63       1.1   mycroft 
     64      1.12  christos #ifdef QBITS
     65  1.28.4.1      yamt static void	clrbits(u_char *, int, int);
     66      1.12  christos #endif
     67       1.1   mycroft 
     68       1.1   mycroft /*
     69       1.1   mycroft  * Initialize a particular clist. Ok, they are really ring buffers,
     70       1.1   mycroft  * of the specified length, with/without quoting support.
     71       1.1   mycroft  */
     72       1.1   mycroft int
     73  1.28.4.1      yamt clalloc(struct clist *clp, int size, int quot)
     74       1.1   mycroft {
     75       1.1   mycroft 
     76      1.20   thorpej 	clp->c_cs = malloc(size, M_TTYS, M_WAITOK);
     77       1.1   mycroft 	if (!clp->c_cs)
     78       1.1   mycroft 		return (-1);
     79      1.17     perry 	memset(clp->c_cs, 0, size);
     80       1.1   mycroft 
     81       1.1   mycroft 	if(quot) {
     82      1.20   thorpej 		clp->c_cq = malloc(QMEM(size), M_TTYS, M_WAITOK);
     83       1.1   mycroft 		if (!clp->c_cq) {
     84      1.20   thorpej 			free(clp->c_cs, M_TTYS);
     85       1.1   mycroft 			return (-1);
     86       1.1   mycroft 		}
     87      1.22        ad 		memset(clp->c_cq, 0, QMEM(size));
     88       1.1   mycroft 	} else
     89       1.7   deraadt 		clp->c_cq = (u_char *)0;
     90       1.1   mycroft 
     91       1.7   deraadt 	clp->c_cf = clp->c_cl = (u_char *)0;
     92       1.1   mycroft 	clp->c_ce = clp->c_cs + size;
     93       1.1   mycroft 	clp->c_cn = size;
     94       1.1   mycroft 	clp->c_cc = 0;
     95       1.1   mycroft 	return (0);
     96       1.1   mycroft }
     97       1.1   mycroft 
     98       1.1   mycroft void
     99  1.28.4.1      yamt clfree(struct clist *clp)
    100       1.1   mycroft {
    101       1.1   mycroft 	if(clp->c_cs)
    102      1.20   thorpej 		free(clp->c_cs, M_TTYS);
    103       1.1   mycroft 	if(clp->c_cq)
    104      1.20   thorpej 		free(clp->c_cq, M_TTYS);
    105       1.7   deraadt 	clp->c_cs = clp->c_cq = (u_char *)0;
    106       1.1   mycroft }
    107       1.1   mycroft 
    108       1.1   mycroft 
    109       1.1   mycroft /*
    110       1.1   mycroft  * Get a character from a clist.
    111       1.1   mycroft  */
    112       1.1   mycroft int
    113  1.28.4.1      yamt getc(struct clist *clp)
    114       1.1   mycroft {
    115      1.19  augustss 	int c = -1;
    116       1.1   mycroft 	int s;
    117       1.1   mycroft 
    118       1.1   mycroft 	s = spltty();
    119       1.1   mycroft 	if (clp->c_cc == 0)
    120       1.1   mycroft 		goto out;
    121       1.1   mycroft 
    122       1.1   mycroft 	c = *clp->c_cf & 0xff;
    123       1.1   mycroft 	if (clp->c_cq) {
    124       1.1   mycroft #ifdef QBITS
    125       1.1   mycroft 		if (isset(clp->c_cq, clp->c_cf - clp->c_cs) )
    126       1.1   mycroft 			c |= TTY_QUOTE;
    127       1.1   mycroft #else
    128       1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    129       1.1   mycroft 			c |= TTY_QUOTE;
    130       1.1   mycroft #endif
    131       1.1   mycroft 	}
    132       1.1   mycroft 	if (++clp->c_cf == clp->c_ce)
    133       1.1   mycroft 		clp->c_cf = clp->c_cs;
    134       1.1   mycroft 	if (--clp->c_cc == 0)
    135       1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    136       1.1   mycroft out:
    137       1.1   mycroft 	splx(s);
    138       1.1   mycroft 	return c;
    139       1.1   mycroft }
    140       1.1   mycroft 
    141       1.1   mycroft /*
    142       1.1   mycroft  * Copy clist to buffer.
    143       1.1   mycroft  * Return number of bytes moved.
    144       1.1   mycroft  */
    145       1.1   mycroft int
    146  1.28.4.1      yamt q_to_b(struct clist *clp, u_char *cp, int count)
    147       1.1   mycroft {
    148      1.19  augustss 	int cc;
    149       1.7   deraadt 	u_char *p = cp;
    150       1.1   mycroft 	int s;
    151       1.1   mycroft 
    152       1.1   mycroft 	s = spltty();
    153       1.1   mycroft 	/* optimize this while loop */
    154       1.1   mycroft 	while (count > 0 && clp->c_cc > 0) {
    155       1.1   mycroft 		cc = clp->c_cl - clp->c_cf;
    156       1.1   mycroft 		if (clp->c_cf >= clp->c_cl)
    157       1.1   mycroft 			cc = clp->c_ce - clp->c_cf;
    158       1.1   mycroft 		if (cc > count)
    159       1.1   mycroft 			cc = count;
    160      1.17     perry 		memcpy(p, clp->c_cf, cc);
    161       1.1   mycroft 		count -= cc;
    162       1.1   mycroft 		p += cc;
    163       1.1   mycroft 		clp->c_cc -= cc;
    164       1.1   mycroft 		clp->c_cf += cc;
    165       1.1   mycroft 		if (clp->c_cf == clp->c_ce)
    166       1.1   mycroft 			clp->c_cf = clp->c_cs;
    167       1.1   mycroft 	}
    168       1.1   mycroft 	if (clp->c_cc == 0)
    169       1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    170       1.1   mycroft 	splx(s);
    171       1.1   mycroft 	return p - cp;
    172       1.1   mycroft }
    173       1.1   mycroft 
    174       1.1   mycroft /*
    175       1.1   mycroft  * Return count of contiguous characters in clist.
    176       1.1   mycroft  * Stop counting if flag&character is non-null.
    177       1.1   mycroft  */
    178      1.12  christos int
    179  1.28.4.1      yamt ndqb(struct clist *clp, int flag)
    180       1.1   mycroft {
    181       1.1   mycroft 	int count = 0;
    182      1.19  augustss 	int i;
    183      1.19  augustss 	int cc;
    184       1.1   mycroft 	int s;
    185       1.1   mycroft 
    186       1.1   mycroft 	s = spltty();
    187       1.2   deraadt 	if ((cc = clp->c_cc) == 0)
    188       1.1   mycroft 		goto out;
    189       1.1   mycroft 
    190       1.2   deraadt 	if (flag == 0) {
    191       1.2   deraadt 		count = clp->c_cl - clp->c_cf;
    192       1.9   deraadt 		if (count <= 0)
    193       1.2   deraadt 			count = clp->c_ce - clp->c_cf;
    194       1.2   deraadt 		goto out;
    195       1.2   deraadt 	}
    196       1.2   deraadt 
    197       1.1   mycroft 	i = clp->c_cf - clp->c_cs;
    198      1.10       cgd 	if (flag & TTY_QUOTE) {
    199      1.10       cgd 		while (cc-- > 0 && !(clp->c_cs[i++] & (flag & ~TTY_QUOTE) ||
    200      1.10       cgd 		    isset(clp->c_cq, i))) {
    201       1.2   deraadt 			count++;
    202       1.2   deraadt 			if (i == clp->c_cn)
    203       1.2   deraadt 				break;
    204       1.2   deraadt 		}
    205       1.2   deraadt 	} else {
    206      1.10       cgd 		while (cc-- > 0 && !(clp->c_cs[i++] & flag)) {
    207       1.2   deraadt 			count++;
    208       1.2   deraadt 			if (i == clp->c_cn)
    209       1.2   deraadt 				break;
    210       1.2   deraadt 		}
    211       1.1   mycroft 	}
    212       1.1   mycroft out:
    213       1.1   mycroft 	splx(s);
    214       1.1   mycroft 	return count;
    215       1.1   mycroft }
    216       1.1   mycroft 
    217       1.1   mycroft /*
    218       1.1   mycroft  * Flush count bytes from clist.
    219       1.1   mycroft  */
    220       1.1   mycroft void
    221  1.28.4.1      yamt ndflush(struct clist *clp, int count)
    222       1.1   mycroft {
    223      1.19  augustss 	int cc;
    224       1.1   mycroft 	int s;
    225       1.1   mycroft 
    226       1.1   mycroft 	s = spltty();
    227       1.1   mycroft 	if (count == clp->c_cc) {
    228       1.1   mycroft 		clp->c_cc = 0;
    229       1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    230       1.1   mycroft 		goto out;
    231       1.1   mycroft 	}
    232       1.1   mycroft 	/* optimize this while loop */
    233       1.1   mycroft 	while (count > 0 && clp->c_cc > 0) {
    234       1.1   mycroft 		cc = clp->c_cl - clp->c_cf;
    235       1.1   mycroft 		if (clp->c_cf >= clp->c_cl)
    236       1.1   mycroft 			cc = clp->c_ce - clp->c_cf;
    237       1.1   mycroft 		if (cc > count)
    238       1.1   mycroft 			cc = count;
    239       1.1   mycroft 		count -= cc;
    240       1.1   mycroft 		clp->c_cc -= cc;
    241       1.1   mycroft 		clp->c_cf += cc;
    242       1.1   mycroft 		if (clp->c_cf == clp->c_ce)
    243       1.1   mycroft 			clp->c_cf = clp->c_cs;
    244       1.1   mycroft 	}
    245       1.1   mycroft 	if (clp->c_cc == 0)
    246       1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    247       1.1   mycroft out:
    248       1.1   mycroft 	splx(s);
    249       1.1   mycroft }
    250       1.1   mycroft 
    251       1.1   mycroft /*
    252       1.1   mycroft  * Put a character into the output queue.
    253       1.1   mycroft  */
    254       1.1   mycroft int
    255  1.28.4.1      yamt putc(int c, struct clist *clp)
    256       1.1   mycroft {
    257      1.19  augustss 	int i;
    258       1.1   mycroft 	int s;
    259       1.1   mycroft 
    260       1.1   mycroft 	s = spltty();
    261       1.1   mycroft 	if (clp->c_cc == clp->c_cn)
    262       1.1   mycroft 		goto out;
    263       1.1   mycroft 
    264       1.1   mycroft 	if (clp->c_cc == 0) {
    265       1.1   mycroft 		if (!clp->c_cs) {
    266       1.1   mycroft #if defined(DIAGNOSTIC) || 1
    267      1.15  christos 			printf("putc: required clalloc\n");
    268       1.1   mycroft #endif
    269       1.1   mycroft 			if(clalloc(clp, 1024, 1)) {
    270       1.1   mycroft out:
    271       1.1   mycroft 				splx(s);
    272       1.1   mycroft 				return -1;
    273       1.1   mycroft 			}
    274       1.1   mycroft 		}
    275       1.1   mycroft 		clp->c_cf = clp->c_cl = clp->c_cs;
    276       1.1   mycroft 	}
    277       1.1   mycroft 
    278       1.1   mycroft 	*clp->c_cl = c & 0xff;
    279       1.1   mycroft 	i = clp->c_cl - clp->c_cs;
    280       1.1   mycroft 	if (clp->c_cq) {
    281       1.1   mycroft #ifdef QBITS
    282       1.1   mycroft 		if (c & TTY_QUOTE)
    283      1.28     perry 			setbit(clp->c_cq, i);
    284       1.1   mycroft 		else
    285       1.1   mycroft 			clrbit(clp->c_cq, i);
    286       1.1   mycroft #else
    287       1.1   mycroft 		q = clp->c_cq + i;
    288       1.1   mycroft 		*q = (c & TTY_QUOTE) ? 1 : 0;
    289       1.1   mycroft #endif
    290       1.1   mycroft 	}
    291       1.1   mycroft 	clp->c_cc++;
    292       1.1   mycroft 	clp->c_cl++;
    293       1.1   mycroft 	if (clp->c_cl == clp->c_ce)
    294       1.1   mycroft 		clp->c_cl = clp->c_cs;
    295       1.1   mycroft 	splx(s);
    296       1.1   mycroft 	return 0;
    297       1.1   mycroft }
    298       1.1   mycroft 
    299       1.1   mycroft #ifdef QBITS
    300       1.1   mycroft /*
    301       1.1   mycroft  * optimized version of
    302       1.1   mycroft  *
    303       1.1   mycroft  * for (i = 0; i < len; i++)
    304       1.1   mycroft  *	clrbit(cp, off + len);
    305       1.1   mycroft  */
    306  1.28.4.1      yamt static void
    307  1.28.4.1      yamt clrbits(u_char *cp, int off, int len)
    308       1.1   mycroft {
    309       1.1   mycroft 	int sby, sbi, eby, ebi;
    310      1.19  augustss 	int i;
    311       1.7   deraadt 	u_char mask;
    312       1.1   mycroft 
    313       1.1   mycroft 	if(len==1) {
    314       1.1   mycroft 		clrbit(cp, off);
    315       1.1   mycroft 		return;
    316       1.1   mycroft 	}
    317       1.1   mycroft 
    318       1.1   mycroft 	sby = off / NBBY;
    319       1.1   mycroft 	sbi = off % NBBY;
    320       1.1   mycroft 	eby = (off+len) / NBBY;
    321       1.1   mycroft 	ebi = (off+len) % NBBY;
    322       1.1   mycroft 	if (sby == eby) {
    323       1.1   mycroft 		mask = ((1 << (ebi - sbi)) - 1) << sbi;
    324       1.1   mycroft 		cp[sby] &= ~mask;
    325       1.1   mycroft 	} else {
    326       1.1   mycroft 		mask = (1<<sbi) - 1;
    327       1.1   mycroft 		cp[sby++] &= mask;
    328       1.1   mycroft 
    329       1.1   mycroft 		mask = (1<<ebi) - 1;
    330       1.1   mycroft 		cp[eby] &= ~mask;
    331       1.1   mycroft 
    332       1.1   mycroft 		for (i = sby; i < eby; i++)
    333       1.1   mycroft 			cp[i] = 0x00;
    334       1.1   mycroft 	}
    335       1.1   mycroft }
    336       1.1   mycroft #endif
    337       1.1   mycroft 
    338       1.1   mycroft /*
    339       1.1   mycroft  * Copy buffer to clist.
    340       1.1   mycroft  * Return number of bytes not transfered.
    341       1.1   mycroft  */
    342       1.1   mycroft int
    343  1.28.4.1      yamt b_to_q(const u_char *cp, int count, struct clist *clp)
    344       1.1   mycroft {
    345      1.19  augustss 	int cc;
    346      1.19  augustss 	const u_char *p = cp;
    347      1.12  christos 	int s;
    348       1.1   mycroft 
    349       1.1   mycroft 	if (count <= 0)
    350       1.1   mycroft 		return 0;
    351       1.1   mycroft 
    352       1.1   mycroft 	s = spltty();
    353       1.1   mycroft 	if (clp->c_cc == clp->c_cn)
    354       1.1   mycroft 		goto out;
    355       1.1   mycroft 
    356       1.1   mycroft 	if (clp->c_cc == 0) {
    357       1.1   mycroft 		if (!clp->c_cs) {
    358       1.1   mycroft #if defined(DIAGNOSTIC) || 1
    359      1.15  christos 			printf("b_to_q: required clalloc\n");
    360       1.1   mycroft #endif
    361       1.1   mycroft 			if(clalloc(clp, 1024, 1))
    362       1.1   mycroft 				goto out;
    363       1.1   mycroft 		}
    364       1.1   mycroft 		clp->c_cf = clp->c_cl = clp->c_cs;
    365       1.1   mycroft 	}
    366       1.1   mycroft 
    367       1.1   mycroft 	/* optimize this while loop */
    368       1.1   mycroft 	while (count > 0 && clp->c_cc < clp->c_cn) {
    369       1.1   mycroft 		cc = clp->c_ce - clp->c_cl;
    370       1.1   mycroft 		if (clp->c_cf > clp->c_cl)
    371       1.1   mycroft 			cc = clp->c_cf - clp->c_cl;
    372       1.1   mycroft 		if (cc > count)
    373       1.1   mycroft 			cc = count;
    374      1.17     perry 		memcpy(clp->c_cl, p, cc);
    375       1.1   mycroft 		if (clp->c_cq) {
    376       1.1   mycroft #ifdef QBITS
    377       1.1   mycroft 			clrbits(clp->c_cq, clp->c_cl - clp->c_cs, cc);
    378       1.1   mycroft #else
    379      1.17     perry 			memset(clp->c_cl - clp->c_cs + clp->c_cq, 0, cc);
    380       1.1   mycroft #endif
    381       1.1   mycroft 		}
    382       1.1   mycroft 		p += cc;
    383       1.1   mycroft 		count -= cc;
    384       1.1   mycroft 		clp->c_cc += cc;
    385       1.1   mycroft 		clp->c_cl += cc;
    386       1.1   mycroft 		if (clp->c_cl == clp->c_ce)
    387       1.1   mycroft 			clp->c_cl = clp->c_cs;
    388       1.1   mycroft 	}
    389       1.1   mycroft out:
    390       1.1   mycroft 	splx(s);
    391       1.1   mycroft 	return count;
    392       1.1   mycroft }
    393       1.1   mycroft 
    394       1.1   mycroft static int cc;
    395       1.1   mycroft 
    396       1.1   mycroft /*
    397       1.1   mycroft  * Given a non-NULL pointer into the clist return the pointer
    398       1.1   mycroft  * to the next character in the list or return NULL if no more chars.
    399       1.1   mycroft  *
    400       1.1   mycroft  * Callers must not allow getc's to happen between firstc's and getc's
    401       1.1   mycroft  * so that the pointer becomes invalid.  Note that interrupts are NOT
    402       1.1   mycroft  * masked.
    403       1.1   mycroft  */
    404       1.7   deraadt u_char *
    405  1.28.4.1      yamt nextc(struct clist *clp, u_char *cp, int *c)
    406       1.1   mycroft {
    407       1.1   mycroft 
    408       1.1   mycroft 	if (clp->c_cf == cp) {
    409       1.1   mycroft 		/*
    410       1.1   mycroft 		 * First time initialization.
    411       1.1   mycroft 		 */
    412       1.1   mycroft 		cc = clp->c_cc;
    413       1.1   mycroft 	}
    414       1.1   mycroft 	if (cc == 0 || cp == NULL)
    415       1.1   mycroft 		return NULL;
    416       1.1   mycroft 	if (--cc == 0)
    417       1.1   mycroft 		return NULL;
    418       1.1   mycroft 	if (++cp == clp->c_ce)
    419       1.1   mycroft 		cp = clp->c_cs;
    420       1.1   mycroft 	*c = *cp & 0xff;
    421       1.1   mycroft 	if (clp->c_cq) {
    422       1.1   mycroft #ifdef QBITS
    423       1.1   mycroft 		if (isset(clp->c_cq, cp - clp->c_cs))
    424       1.1   mycroft 			*c |= TTY_QUOTE;
    425       1.1   mycroft #else
    426       1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    427       1.1   mycroft 			*c |= TTY_QUOTE;
    428       1.1   mycroft #endif
    429       1.1   mycroft 	}
    430       1.1   mycroft 	return cp;
    431       1.1   mycroft }
    432       1.1   mycroft 
    433       1.1   mycroft /*
    434       1.1   mycroft  * Given a non-NULL pointer into the clist return the pointer
    435       1.1   mycroft  * to the first character in the list or return NULL if no more chars.
    436       1.1   mycroft  *
    437       1.1   mycroft  * Callers must not allow getc's to happen between firstc's and getc's
    438       1.1   mycroft  * so that the pointer becomes invalid.  Note that interrupts are NOT
    439       1.1   mycroft  * masked.
    440       1.1   mycroft  *
    441       1.1   mycroft  * *c is set to the NEXT character
    442       1.1   mycroft  */
    443       1.7   deraadt u_char *
    444  1.28.4.1      yamt firstc(struct clist *clp, int *c)
    445       1.1   mycroft {
    446      1.19  augustss 	u_char *cp;
    447       1.1   mycroft 
    448       1.1   mycroft 	cc = clp->c_cc;
    449       1.1   mycroft 	if (cc == 0)
    450       1.1   mycroft 		return NULL;
    451       1.1   mycroft 	cp = clp->c_cf;
    452       1.1   mycroft 	*c = *cp & 0xff;
    453       1.1   mycroft 	if(clp->c_cq) {
    454       1.1   mycroft #ifdef QBITS
    455       1.1   mycroft 		if (isset(clp->c_cq, cp - clp->c_cs))
    456       1.1   mycroft 			*c |= TTY_QUOTE;
    457       1.1   mycroft #else
    458       1.1   mycroft 		if (*(cp - clp->c_cs + clp->c_cq))
    459       1.1   mycroft 			*c |= TTY_QUOTE;
    460       1.1   mycroft #endif
    461       1.1   mycroft 	}
    462       1.1   mycroft 	return clp->c_cf;
    463       1.1   mycroft }
    464       1.1   mycroft 
    465       1.1   mycroft /*
    466       1.1   mycroft  * Remove the last character in the clist and return it.
    467       1.1   mycroft  */
    468       1.1   mycroft int
    469  1.28.4.1      yamt unputc(struct clist *clp)
    470       1.1   mycroft {
    471       1.1   mycroft 	unsigned int c = -1;
    472       1.1   mycroft 	int s;
    473       1.1   mycroft 
    474       1.1   mycroft 	s = spltty();
    475       1.1   mycroft 	if (clp->c_cc == 0)
    476       1.1   mycroft 		goto out;
    477       1.1   mycroft 
    478       1.1   mycroft 	if (clp->c_cl == clp->c_cs)
    479       1.1   mycroft 		clp->c_cl = clp->c_ce - 1;
    480       1.1   mycroft 	else
    481       1.1   mycroft 		--clp->c_cl;
    482       1.1   mycroft 	clp->c_cc--;
    483       1.1   mycroft 
    484       1.1   mycroft 	c = *clp->c_cl & 0xff;
    485       1.1   mycroft 	if (clp->c_cq) {
    486       1.1   mycroft #ifdef QBITS
    487       1.1   mycroft 		if (isset(clp->c_cq, clp->c_cl - clp->c_cs))
    488       1.1   mycroft 			c |= TTY_QUOTE;
    489       1.1   mycroft #else
    490       1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    491       1.5   deraadt 			c |= TTY_QUOTE;
    492       1.1   mycroft #endif
    493       1.1   mycroft 	}
    494       1.1   mycroft 	if (clp->c_cc == 0)
    495       1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    496       1.1   mycroft out:
    497       1.1   mycroft 	splx(s);
    498       1.1   mycroft 	return c;
    499       1.1   mycroft }
    500       1.1   mycroft 
    501       1.1   mycroft /*
    502       1.1   mycroft  * Put the chars in the from queue on the end of the to queue.
    503       1.1   mycroft  */
    504       1.1   mycroft void
    505  1.28.4.1      yamt catq(struct clist *from, struct clist *to)
    506       1.1   mycroft {
    507       1.1   mycroft 	int c;
    508       1.1   mycroft 
    509       1.1   mycroft 	while ((c = getc(from)) != -1)
    510       1.1   mycroft 		putc(c, to);
    511       1.1   mycroft }
    512