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