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