Home | History | Annotate | Line # | Download | only in kern
tty_subr.c revision 1.40.30.1
      1  1.40.30.1     skrll /*	$NetBSD: tty_subr.c,v 1.40.30.1 2017/08/28 17:53:07 skrll 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.40.30.1     skrll __KERNEL_RCSID(0, "$NetBSD: tty_subr.c,v 1.40.30.1 2017/08/28 17:53:07 skrll 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.35      yamt #include <sys/kmem.h>
     40        1.1   mycroft 
     41        1.1   mycroft /*
     42        1.1   mycroft  * At compile time, choose:
     43        1.1   mycroft  * There are two ways the TTY_QUOTE bit can be stored. If QBITS is
     44        1.1   mycroft  * defined we allocate an array of bits -- 1/8th as much memory but
     45       1.26       wiz  * setbit(), clrbit(), and isset() take more CPU. If QBITS is
     46        1.1   mycroft  * undefined, we just use an array of bytes.
     47       1.28     perry  *
     48        1.1   mycroft  * If TTY_QUOTE functionality isn't required by a line discipline,
     49        1.1   mycroft  * it can free c_cq and set it to NULL. This speeds things up,
     50        1.1   mycroft  * and also does not use any extra memory. This is useful for (say)
     51        1.1   mycroft  * a SLIP line discipline that wants a 32K ring buffer for data
     52        1.1   mycroft  * but doesn't need quoting.
     53        1.1   mycroft  */
     54        1.1   mycroft #define QBITS
     55        1.1   mycroft 
     56        1.1   mycroft #ifdef QBITS
     57        1.1   mycroft #define QMEM(n)		((((n)-1)/NBBY)+1)
     58        1.1   mycroft #else
     59        1.1   mycroft #define QMEM(n)		(n)
     60        1.1   mycroft #endif
     61        1.1   mycroft 
     62       1.12  christos #ifdef QBITS
     63       1.37       dsl static void	clrbits(u_char *, unsigned int, unsigned int);
     64       1.12  christos #endif
     65        1.1   mycroft 
     66        1.1   mycroft /*
     67        1.1   mycroft  * Initialize a particular clist. Ok, they are really ring buffers,
     68        1.1   mycroft  * of the specified length, with/without quoting support.
     69        1.1   mycroft  */
     70        1.1   mycroft int
     71       1.29   thorpej clalloc(struct clist *clp, int size, int quot)
     72        1.1   mycroft {
     73        1.1   mycroft 
     74       1.35      yamt 	clp->c_cs = kmem_zalloc(size, KM_SLEEP);
     75  1.40.30.1     skrll 	if (quot)
     76       1.35      yamt 		clp->c_cq = kmem_zalloc(QMEM(size), KM_SLEEP);
     77  1.40.30.1     skrll 	else
     78       1.35      yamt 		clp->c_cq = NULL;
     79        1.1   mycroft 
     80       1.35      yamt 	clp->c_cf = clp->c_cl = NULL;
     81        1.1   mycroft 	clp->c_ce = clp->c_cs + size;
     82        1.1   mycroft 	clp->c_cn = size;
     83        1.1   mycroft 	clp->c_cc = 0;
     84       1.30        ad 
     85        1.1   mycroft 	return (0);
     86        1.1   mycroft }
     87        1.1   mycroft 
     88        1.1   mycroft void
     89       1.29   thorpej clfree(struct clist *clp)
     90        1.1   mycroft {
     91       1.36  uebayasi 	if (clp->c_cs)
     92       1.35      yamt 		kmem_free(clp->c_cs, clp->c_cn);
     93       1.36  uebayasi 	if (clp->c_cq)
     94       1.35      yamt 		kmem_free(clp->c_cq, QMEM(clp->c_cn));
     95       1.35      yamt 	clp->c_cs = clp->c_cq = NULL;
     96       1.31        ad }
     97        1.1   mycroft 
     98        1.1   mycroft /*
     99        1.1   mycroft  * Get a character from a clist.
    100        1.1   mycroft  */
    101        1.1   mycroft int
    102       1.29   thorpej getc(struct clist *clp)
    103        1.1   mycroft {
    104       1.19  augustss 	int c = -1;
    105        1.1   mycroft 	int s;
    106        1.1   mycroft 
    107        1.1   mycroft 	s = spltty();
    108        1.1   mycroft 	if (clp->c_cc == 0)
    109        1.1   mycroft 		goto out;
    110        1.1   mycroft 
    111        1.1   mycroft 	c = *clp->c_cf & 0xff;
    112        1.1   mycroft 	if (clp->c_cq) {
    113        1.1   mycroft #ifdef QBITS
    114        1.1   mycroft 		if (isset(clp->c_cq, clp->c_cf - clp->c_cs) )
    115        1.1   mycroft 			c |= TTY_QUOTE;
    116        1.1   mycroft #else
    117        1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    118        1.1   mycroft 			c |= TTY_QUOTE;
    119        1.1   mycroft #endif
    120        1.1   mycroft 	}
    121       1.34  drochner 	*clp->c_cf = 0; /* wipe out to avoid information disclosure */
    122        1.1   mycroft 	if (++clp->c_cf == clp->c_ce)
    123        1.1   mycroft 		clp->c_cf = clp->c_cs;
    124        1.1   mycroft 	if (--clp->c_cc == 0)
    125        1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    126        1.1   mycroft out:
    127        1.1   mycroft 	splx(s);
    128        1.1   mycroft 	return c;
    129        1.1   mycroft }
    130        1.1   mycroft 
    131        1.1   mycroft /*
    132        1.1   mycroft  * Copy clist to buffer.
    133        1.1   mycroft  * Return number of bytes moved.
    134        1.1   mycroft  */
    135        1.1   mycroft int
    136       1.29   thorpej q_to_b(struct clist *clp, u_char *cp, int count)
    137        1.1   mycroft {
    138       1.19  augustss 	int cc;
    139        1.7   deraadt 	u_char *p = cp;
    140        1.1   mycroft 	int s;
    141        1.1   mycroft 
    142        1.1   mycroft 	s = spltty();
    143        1.1   mycroft 	/* optimize this while loop */
    144        1.1   mycroft 	while (count > 0 && clp->c_cc > 0) {
    145        1.1   mycroft 		cc = clp->c_cl - clp->c_cf;
    146        1.1   mycroft 		if (clp->c_cf >= clp->c_cl)
    147        1.1   mycroft 			cc = clp->c_ce - clp->c_cf;
    148        1.1   mycroft 		if (cc > count)
    149        1.1   mycroft 			cc = count;
    150       1.17     perry 		memcpy(p, clp->c_cf, cc);
    151        1.1   mycroft 		count -= cc;
    152        1.1   mycroft 		p += cc;
    153        1.1   mycroft 		clp->c_cc -= cc;
    154        1.1   mycroft 		clp->c_cf += cc;
    155        1.1   mycroft 		if (clp->c_cf == clp->c_ce)
    156        1.1   mycroft 			clp->c_cf = clp->c_cs;
    157        1.1   mycroft 	}
    158        1.1   mycroft 	if (clp->c_cc == 0)
    159        1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    160        1.1   mycroft 	splx(s);
    161        1.1   mycroft 	return p - cp;
    162        1.1   mycroft }
    163        1.1   mycroft 
    164        1.1   mycroft /*
    165        1.1   mycroft  * Return count of contiguous characters in clist.
    166        1.1   mycroft  * Stop counting if flag&character is non-null.
    167        1.1   mycroft  */
    168       1.12  christos int
    169       1.29   thorpej ndqb(struct clist *clp, int flag)
    170        1.1   mycroft {
    171        1.1   mycroft 	int count = 0;
    172       1.19  augustss 	int i;
    173       1.19  augustss 	int cc;
    174        1.1   mycroft 	int s;
    175        1.1   mycroft 
    176        1.1   mycroft 	s = spltty();
    177        1.2   deraadt 	if ((cc = clp->c_cc) == 0)
    178        1.1   mycroft 		goto out;
    179        1.1   mycroft 
    180        1.2   deraadt 	if (flag == 0) {
    181        1.2   deraadt 		count = clp->c_cl - clp->c_cf;
    182        1.9   deraadt 		if (count <= 0)
    183        1.2   deraadt 			count = clp->c_ce - clp->c_cf;
    184        1.2   deraadt 		goto out;
    185        1.2   deraadt 	}
    186        1.2   deraadt 
    187        1.1   mycroft 	i = clp->c_cf - clp->c_cs;
    188       1.10       cgd 	if (flag & TTY_QUOTE) {
    189       1.10       cgd 		while (cc-- > 0 && !(clp->c_cs[i++] & (flag & ~TTY_QUOTE) ||
    190       1.10       cgd 		    isset(clp->c_cq, i))) {
    191        1.2   deraadt 			count++;
    192        1.2   deraadt 			if (i == clp->c_cn)
    193        1.2   deraadt 				break;
    194        1.2   deraadt 		}
    195        1.2   deraadt 	} else {
    196       1.10       cgd 		while (cc-- > 0 && !(clp->c_cs[i++] & flag)) {
    197        1.2   deraadt 			count++;
    198        1.2   deraadt 			if (i == clp->c_cn)
    199        1.2   deraadt 				break;
    200        1.2   deraadt 		}
    201        1.1   mycroft 	}
    202        1.1   mycroft out:
    203        1.1   mycroft 	splx(s);
    204        1.1   mycroft 	return count;
    205        1.1   mycroft }
    206        1.1   mycroft 
    207        1.1   mycroft /*
    208        1.1   mycroft  * Flush count bytes from clist.
    209        1.1   mycroft  */
    210        1.1   mycroft void
    211       1.29   thorpej ndflush(struct clist *clp, int count)
    212        1.1   mycroft {
    213       1.19  augustss 	int cc;
    214        1.1   mycroft 	int s;
    215        1.1   mycroft 
    216        1.1   mycroft 	s = spltty();
    217        1.1   mycroft 	if (count == clp->c_cc) {
    218        1.1   mycroft 		clp->c_cc = 0;
    219        1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    220        1.1   mycroft 		goto out;
    221        1.1   mycroft 	}
    222        1.1   mycroft 	/* optimize this while loop */
    223        1.1   mycroft 	while (count > 0 && clp->c_cc > 0) {
    224        1.1   mycroft 		cc = clp->c_cl - clp->c_cf;
    225        1.1   mycroft 		if (clp->c_cf >= clp->c_cl)
    226        1.1   mycroft 			cc = clp->c_ce - clp->c_cf;
    227        1.1   mycroft 		if (cc > count)
    228        1.1   mycroft 			cc = count;
    229        1.1   mycroft 		count -= cc;
    230        1.1   mycroft 		clp->c_cc -= cc;
    231        1.1   mycroft 		clp->c_cf += cc;
    232        1.1   mycroft 		if (clp->c_cf == clp->c_ce)
    233        1.1   mycroft 			clp->c_cf = clp->c_cs;
    234        1.1   mycroft 	}
    235        1.1   mycroft 	if (clp->c_cc == 0)
    236        1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    237        1.1   mycroft out:
    238        1.1   mycroft 	splx(s);
    239        1.1   mycroft }
    240        1.1   mycroft 
    241        1.1   mycroft /*
    242        1.1   mycroft  * Put a character into the output queue.
    243        1.1   mycroft  */
    244        1.1   mycroft int
    245       1.29   thorpej putc(int c, struct clist *clp)
    246        1.1   mycroft {
    247       1.19  augustss 	int i;
    248        1.1   mycroft 	int s;
    249        1.1   mycroft 
    250        1.1   mycroft 	s = spltty();
    251        1.1   mycroft 	if (clp->c_cc == clp->c_cn)
    252        1.1   mycroft 		goto out;
    253        1.1   mycroft 
    254        1.1   mycroft 	if (clp->c_cc == 0) {
    255        1.1   mycroft 		if (!clp->c_cs) {
    256        1.1   mycroft #if defined(DIAGNOSTIC) || 1
    257       1.15  christos 			printf("putc: required clalloc\n");
    258        1.1   mycroft #endif
    259       1.40  christos 			if (clalloc(clp, clp->c_cn, 1)) {
    260        1.1   mycroft out:
    261        1.1   mycroft 				splx(s);
    262        1.1   mycroft 				return -1;
    263        1.1   mycroft 			}
    264        1.1   mycroft 		}
    265        1.1   mycroft 		clp->c_cf = clp->c_cl = clp->c_cs;
    266        1.1   mycroft 	}
    267        1.1   mycroft 
    268        1.1   mycroft 	*clp->c_cl = c & 0xff;
    269        1.1   mycroft 	i = clp->c_cl - clp->c_cs;
    270        1.1   mycroft 	if (clp->c_cq) {
    271        1.1   mycroft #ifdef QBITS
    272        1.1   mycroft 		if (c & TTY_QUOTE)
    273       1.28     perry 			setbit(clp->c_cq, i);
    274        1.1   mycroft 		else
    275        1.1   mycroft 			clrbit(clp->c_cq, i);
    276        1.1   mycroft #else
    277        1.1   mycroft 		q = clp->c_cq + i;
    278        1.1   mycroft 		*q = (c & TTY_QUOTE) ? 1 : 0;
    279        1.1   mycroft #endif
    280        1.1   mycroft 	}
    281        1.1   mycroft 	clp->c_cc++;
    282        1.1   mycroft 	clp->c_cl++;
    283        1.1   mycroft 	if (clp->c_cl == clp->c_ce)
    284        1.1   mycroft 		clp->c_cl = clp->c_cs;
    285        1.1   mycroft 	splx(s);
    286        1.1   mycroft 	return 0;
    287        1.1   mycroft }
    288        1.1   mycroft 
    289        1.1   mycroft #ifdef QBITS
    290        1.1   mycroft /*
    291        1.1   mycroft  * optimized version of
    292        1.1   mycroft  *
    293        1.1   mycroft  * for (i = 0; i < len; i++)
    294        1.1   mycroft  *	clrbit(cp, off + len);
    295        1.1   mycroft  */
    296       1.29   thorpej static void
    297       1.37       dsl clrbits(u_char *cp, unsigned int off, unsigned int len)
    298        1.1   mycroft {
    299       1.39       dsl 	unsigned int sbi, ebi;
    300       1.39       dsl 	u_char *scp, *ecp;
    301       1.39       dsl 	unsigned int end;
    302       1.39       dsl 	unsigned char mask;
    303        1.1   mycroft 
    304       1.39       dsl 	scp = cp + off / NBBY;
    305        1.1   mycroft 	sbi = off % NBBY;
    306       1.39       dsl 	end = off + len + NBBY - 1;
    307       1.39       dsl 	ecp = cp + end / NBBY - 1;
    308       1.39       dsl 	ebi = end % NBBY + 1;
    309       1.39       dsl 	if (scp >= ecp) {
    310       1.39       dsl 		mask = ((1 << len) - 1) << sbi;
    311       1.39       dsl 		*scp &= ~mask;
    312        1.1   mycroft 	} else {
    313       1.36  uebayasi 		mask = (1 << sbi) - 1;
    314       1.39       dsl 		*scp++ &= mask;
    315        1.1   mycroft 
    316       1.36  uebayasi 		mask = (1 << ebi) - 1;
    317       1.39       dsl 		*ecp &= ~mask;
    318        1.1   mycroft 
    319       1.39       dsl 		while (scp < ecp)
    320       1.39       dsl 			*scp++ = 0x00;
    321        1.1   mycroft 	}
    322        1.1   mycroft }
    323        1.1   mycroft #endif
    324        1.1   mycroft 
    325        1.1   mycroft /*
    326        1.1   mycroft  * Copy buffer to clist.
    327        1.1   mycroft  * Return number of bytes not transfered.
    328        1.1   mycroft  */
    329        1.1   mycroft int
    330       1.29   thorpej b_to_q(const u_char *cp, int count, struct clist *clp)
    331        1.1   mycroft {
    332       1.19  augustss 	int cc;
    333       1.19  augustss 	const u_char *p = cp;
    334       1.12  christos 	int s;
    335        1.1   mycroft 
    336        1.1   mycroft 	if (count <= 0)
    337        1.1   mycroft 		return 0;
    338        1.1   mycroft 
    339        1.1   mycroft 	s = spltty();
    340        1.1   mycroft 	if (clp->c_cc == clp->c_cn)
    341        1.1   mycroft 		goto out;
    342        1.1   mycroft 
    343        1.1   mycroft 	if (clp->c_cc == 0) {
    344        1.1   mycroft 		if (!clp->c_cs) {
    345        1.1   mycroft #if defined(DIAGNOSTIC) || 1
    346       1.15  christos 			printf("b_to_q: required clalloc\n");
    347        1.1   mycroft #endif
    348       1.40  christos 			if (clalloc(clp, clp->c_cn, 1))
    349        1.1   mycroft 				goto out;
    350        1.1   mycroft 		}
    351        1.1   mycroft 		clp->c_cf = clp->c_cl = clp->c_cs;
    352        1.1   mycroft 	}
    353        1.1   mycroft 
    354        1.1   mycroft 	/* optimize this while loop */
    355        1.1   mycroft 	while (count > 0 && clp->c_cc < clp->c_cn) {
    356        1.1   mycroft 		cc = clp->c_ce - clp->c_cl;
    357        1.1   mycroft 		if (clp->c_cf > clp->c_cl)
    358        1.1   mycroft 			cc = clp->c_cf - clp->c_cl;
    359        1.1   mycroft 		if (cc > count)
    360        1.1   mycroft 			cc = count;
    361       1.17     perry 		memcpy(clp->c_cl, p, cc);
    362        1.1   mycroft 		if (clp->c_cq) {
    363        1.1   mycroft #ifdef QBITS
    364        1.1   mycroft 			clrbits(clp->c_cq, clp->c_cl - clp->c_cs, cc);
    365        1.1   mycroft #else
    366       1.17     perry 			memset(clp->c_cl - clp->c_cs + clp->c_cq, 0, cc);
    367        1.1   mycroft #endif
    368        1.1   mycroft 		}
    369        1.1   mycroft 		p += cc;
    370        1.1   mycroft 		count -= cc;
    371        1.1   mycroft 		clp->c_cc += cc;
    372        1.1   mycroft 		clp->c_cl += cc;
    373        1.1   mycroft 		if (clp->c_cl == clp->c_ce)
    374        1.1   mycroft 			clp->c_cl = clp->c_cs;
    375        1.1   mycroft 	}
    376        1.1   mycroft out:
    377        1.1   mycroft 	splx(s);
    378        1.1   mycroft 	return count;
    379        1.1   mycroft }
    380        1.1   mycroft 
    381        1.1   mycroft static int cc;
    382        1.1   mycroft 
    383        1.1   mycroft /*
    384        1.1   mycroft  * Given a non-NULL pointer into the clist return the pointer
    385        1.1   mycroft  * to the next character in the list or return NULL if no more chars.
    386        1.1   mycroft  *
    387        1.1   mycroft  * Callers must not allow getc's to happen between firstc's and getc's
    388        1.1   mycroft  * so that the pointer becomes invalid.  Note that interrupts are NOT
    389        1.1   mycroft  * masked.
    390        1.1   mycroft  */
    391        1.7   deraadt u_char *
    392       1.29   thorpej nextc(struct clist *clp, u_char *cp, int *c)
    393        1.1   mycroft {
    394        1.1   mycroft 
    395        1.1   mycroft 	if (clp->c_cf == cp) {
    396        1.1   mycroft 		/*
    397        1.1   mycroft 		 * First time initialization.
    398        1.1   mycroft 		 */
    399        1.1   mycroft 		cc = clp->c_cc;
    400        1.1   mycroft 	}
    401        1.1   mycroft 	if (cc == 0 || cp == NULL)
    402        1.1   mycroft 		return NULL;
    403        1.1   mycroft 	if (--cc == 0)
    404        1.1   mycroft 		return NULL;
    405        1.1   mycroft 	if (++cp == clp->c_ce)
    406        1.1   mycroft 		cp = clp->c_cs;
    407        1.1   mycroft 	*c = *cp & 0xff;
    408        1.1   mycroft 	if (clp->c_cq) {
    409        1.1   mycroft #ifdef QBITS
    410        1.1   mycroft 		if (isset(clp->c_cq, cp - clp->c_cs))
    411        1.1   mycroft 			*c |= TTY_QUOTE;
    412        1.1   mycroft #else
    413        1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    414        1.1   mycroft 			*c |= TTY_QUOTE;
    415        1.1   mycroft #endif
    416        1.1   mycroft 	}
    417        1.1   mycroft 	return cp;
    418        1.1   mycroft }
    419        1.1   mycroft 
    420        1.1   mycroft /*
    421        1.1   mycroft  * Given a non-NULL pointer into the clist return the pointer
    422        1.1   mycroft  * to the first character in the list or return NULL if no more chars.
    423        1.1   mycroft  *
    424        1.1   mycroft  * Callers must not allow getc's to happen between firstc's and getc's
    425        1.1   mycroft  * so that the pointer becomes invalid.  Note that interrupts are NOT
    426        1.1   mycroft  * masked.
    427        1.1   mycroft  *
    428        1.1   mycroft  * *c is set to the NEXT character
    429        1.1   mycroft  */
    430        1.7   deraadt u_char *
    431       1.29   thorpej firstc(struct clist *clp, int *c)
    432        1.1   mycroft {
    433       1.19  augustss 	u_char *cp;
    434        1.1   mycroft 
    435        1.1   mycroft 	cc = clp->c_cc;
    436        1.1   mycroft 	if (cc == 0)
    437        1.1   mycroft 		return NULL;
    438        1.1   mycroft 	cp = clp->c_cf;
    439        1.1   mycroft 	*c = *cp & 0xff;
    440       1.36  uebayasi 	if (clp->c_cq) {
    441        1.1   mycroft #ifdef QBITS
    442        1.1   mycroft 		if (isset(clp->c_cq, cp - clp->c_cs))
    443        1.1   mycroft 			*c |= TTY_QUOTE;
    444        1.1   mycroft #else
    445        1.1   mycroft 		if (*(cp - clp->c_cs + clp->c_cq))
    446        1.1   mycroft 			*c |= TTY_QUOTE;
    447        1.1   mycroft #endif
    448        1.1   mycroft 	}
    449        1.1   mycroft 	return clp->c_cf;
    450        1.1   mycroft }
    451        1.1   mycroft 
    452        1.1   mycroft /*
    453        1.1   mycroft  * Remove the last character in the clist and return it.
    454        1.1   mycroft  */
    455        1.1   mycroft int
    456       1.29   thorpej unputc(struct clist *clp)
    457        1.1   mycroft {
    458        1.1   mycroft 	unsigned int c = -1;
    459        1.1   mycroft 	int s;
    460        1.1   mycroft 
    461        1.1   mycroft 	s = spltty();
    462        1.1   mycroft 	if (clp->c_cc == 0)
    463        1.1   mycroft 		goto out;
    464        1.1   mycroft 
    465        1.1   mycroft 	if (clp->c_cl == clp->c_cs)
    466        1.1   mycroft 		clp->c_cl = clp->c_ce - 1;
    467        1.1   mycroft 	else
    468        1.1   mycroft 		--clp->c_cl;
    469        1.1   mycroft 	clp->c_cc--;
    470        1.1   mycroft 
    471        1.1   mycroft 	c = *clp->c_cl & 0xff;
    472        1.1   mycroft 	if (clp->c_cq) {
    473        1.1   mycroft #ifdef QBITS
    474        1.1   mycroft 		if (isset(clp->c_cq, clp->c_cl - clp->c_cs))
    475        1.1   mycroft 			c |= TTY_QUOTE;
    476        1.1   mycroft #else
    477        1.1   mycroft 		if (*(clp->c_cf - clp->c_cs + clp->c_cq))
    478        1.5   deraadt 			c |= TTY_QUOTE;
    479        1.1   mycroft #endif
    480        1.1   mycroft 	}
    481        1.1   mycroft 	if (clp->c_cc == 0)
    482        1.7   deraadt 		clp->c_cf = clp->c_cl = (u_char *)0;
    483        1.1   mycroft out:
    484        1.1   mycroft 	splx(s);
    485        1.1   mycroft 	return c;
    486        1.1   mycroft }
    487        1.1   mycroft 
    488        1.1   mycroft /*
    489        1.1   mycroft  * Put the chars in the from queue on the end of the to queue.
    490        1.1   mycroft  */
    491        1.1   mycroft void
    492       1.29   thorpej catq(struct clist *from, struct clist *to)
    493        1.1   mycroft {
    494        1.1   mycroft 	int c;
    495        1.1   mycroft 
    496        1.1   mycroft 	while ((c = getc(from)) != -1)
    497        1.1   mycroft 		putc(c, to);
    498        1.1   mycroft }
    499