1994-05-25 09:21:21 +00:00
|
|
|
/*
|
1996-04-08 01:22:00 +00:00
|
|
|
* Copyright (c) 1994, David Greenman
|
|
|
|
* All rights reserved.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
1996-04-08 01:22:00 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-08-02 07:55:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clist support routines
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1995-08-28 09:19:25 +00:00
|
|
|
#include <sys/kernel.h>
|
1994-05-25 09:21:21 +00:00
|
|
|
#include <sys/systm.h>
|
1997-10-12 20:26:33 +00:00
|
|
|
#include <sys/malloc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/tty.h>
|
1994-05-25 09:21:21 +00:00
|
|
|
#include <sys/clist.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2002-03-19 21:25:46 +00:00
|
|
|
static void clist_init(void *);
|
1995-08-28 09:19:25 +00:00
|
|
|
SYSINIT(clist, SI_SUB_CLIST, SI_ORDER_FIRST, clist_init, NULL)
|
|
|
|
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct cblock *cfreelist = 0;
|
1994-05-27 12:28:05 +00:00
|
|
|
int cfreecount = 0;
|
1994-11-26 19:24:13 +00:00
|
|
|
static int cslushcount;
|
|
|
|
static int ctotcount;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
#ifndef INITIAL_CBLOCKS
|
1994-11-26 19:24:13 +00:00
|
|
|
#define INITIAL_CBLOCKS 50
|
1994-05-27 12:28:05 +00:00
|
|
|
#endif
|
|
|
|
|
2002-03-19 21:25:46 +00:00
|
|
|
static struct cblock *cblock_alloc(void);
|
|
|
|
static void cblock_alloc_cblocks(int number);
|
|
|
|
static void cblock_free(struct cblock *cblockp);
|
|
|
|
static void cblock_free_cblocks(int number);
|
1994-11-26 19:24:13 +00:00
|
|
|
|
1996-09-14 10:53:48 +00:00
|
|
|
#include "opt_ddb.h"
|
1995-11-20 12:15:45 +00:00
|
|
|
#ifdef DDB
|
1996-09-14 10:53:48 +00:00
|
|
|
#include <ddb/ddb.h>
|
1995-10-25 17:59:58 +00:00
|
|
|
|
1996-09-14 10:53:48 +00:00
|
|
|
DB_SHOW_COMMAND(cbstat, cbstat)
|
1994-05-27 12:28:05 +00:00
|
|
|
{
|
1999-07-01 19:46:36 +00:00
|
|
|
int cbsize = CBSIZE;
|
|
|
|
|
1994-11-26 19:24:13 +00:00
|
|
|
printf(
|
|
|
|
"tot = %d (active = %d, free = %d (reserved = %d, slush = %d))\n",
|
1999-07-01 19:46:36 +00:00
|
|
|
ctotcount * cbsize, ctotcount * cbsize - cfreecount, cfreecount,
|
|
|
|
cfreecount - cslushcount * cbsize, cslushcount * cbsize);
|
1994-05-27 12:28:05 +00:00
|
|
|
}
|
1995-11-20 12:15:45 +00:00
|
|
|
#endif /* DDB */
|
1994-05-27 12:28:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from init_main.c
|
|
|
|
*/
|
1995-08-28 09:19:25 +00:00
|
|
|
/* ARGSUSED*/
|
|
|
|
static void
|
1995-10-25 17:59:58 +00:00
|
|
|
clist_init(dummy)
|
|
|
|
void *dummy;
|
1994-05-27 12:28:05 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allocate an initial base set of cblocks as a 'slush'.
|
1994-11-26 19:24:13 +00:00
|
|
|
* We allocate non-slush cblocks with each initial ttyopen() and
|
|
|
|
* deallocate them with each ttyclose().
|
|
|
|
* We should adjust the slush allocation. This can't be done in
|
|
|
|
* the i/o routines because they are sometimes called from
|
|
|
|
* interrupt handlers when it may be unsafe to call malloc().
|
1994-05-27 12:28:05 +00:00
|
|
|
*/
|
1994-11-26 19:24:13 +00:00
|
|
|
cblock_alloc_cblocks(cslushcount = INITIAL_CBLOCKS);
|
1994-05-27 12:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a cblock from the cfreelist queue and return a pointer
|
|
|
|
* to it.
|
|
|
|
*/
|
1998-04-15 17:47:40 +00:00
|
|
|
static __inline struct cblock *
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_alloc()
|
|
|
|
{
|
|
|
|
struct cblock *cblockp;
|
|
|
|
|
|
|
|
cblockp = cfreelist;
|
1994-11-26 19:24:13 +00:00
|
|
|
if (cblockp == NULL)
|
|
|
|
panic("clist reservation botch");
|
1994-05-27 12:28:05 +00:00
|
|
|
cfreelist = cblockp->c_next;
|
|
|
|
cblockp->c_next = NULL;
|
|
|
|
cfreecount -= CBSIZE;
|
|
|
|
return (cblockp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a cblock to the cfreelist queue.
|
|
|
|
*/
|
1998-04-15 17:47:40 +00:00
|
|
|
static __inline void
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp)
|
|
|
|
struct cblock *cblockp;
|
|
|
|
{
|
1995-07-11 19:39:54 +00:00
|
|
|
if (isset(cblockp->c_quote, CBQSIZE * NBBY - 1))
|
|
|
|
bzero(cblockp->c_quote, sizeof cblockp->c_quote);
|
1994-05-27 12:28:05 +00:00
|
|
|
cblockp->c_next = cfreelist;
|
|
|
|
cfreelist = cblockp;
|
|
|
|
cfreecount += CBSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate some cblocks for the cfreelist queue.
|
|
|
|
*/
|
1994-11-26 19:24:13 +00:00
|
|
|
static void
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_alloc_cblocks(number)
|
|
|
|
int number;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
int i;
|
1995-07-11 19:39:54 +00:00
|
|
|
struct cblock *cbp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
for (i = 0; i < number; ++i) {
|
1997-03-05 16:45:01 +00:00
|
|
|
cbp = malloc(sizeof *cbp, M_TTYS, M_NOWAIT);
|
|
|
|
if (cbp == NULL) {
|
|
|
|
printf(
|
2003-01-21 08:56:16 +00:00
|
|
|
"cblock_alloc_cblocks: M_NOWAIT malloc failed, trying blocking malloc\n");
|
|
|
|
cbp = malloc(sizeof *cbp, M_TTYS, 0);
|
1997-03-05 16:45:01 +00:00
|
|
|
}
|
1995-07-11 19:39:54 +00:00
|
|
|
/*
|
|
|
|
* Freed cblocks have zero quotes and garbage elsewhere.
|
|
|
|
* Set the may-have-quote bit to force zeroing the quotes.
|
|
|
|
*/
|
|
|
|
setbit(cbp->c_quote, CBQSIZE * NBBY - 1);
|
|
|
|
cblock_free(cbp);
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
1994-11-26 19:24:13 +00:00
|
|
|
ctotcount += number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-01-01 18:49:04 +00:00
|
|
|
* Set the cblock allocation policy for a clist.
|
1995-07-11 19:39:54 +00:00
|
|
|
* Must be called in process context at spltty().
|
1994-11-26 19:24:13 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
clist_alloc_cblocks(clistp, ccmax, ccreserved)
|
|
|
|
struct clist *clistp;
|
|
|
|
int ccmax;
|
|
|
|
int ccreserved;
|
|
|
|
{
|
|
|
|
int dcbr;
|
|
|
|
|
1995-10-25 17:59:58 +00:00
|
|
|
/*
|
|
|
|
* Allow for wasted space at the head.
|
|
|
|
*/
|
|
|
|
if (ccmax != 0)
|
|
|
|
ccmax += CBSIZE - 1;
|
|
|
|
if (ccreserved != 0)
|
|
|
|
ccreserved += CBSIZE - 1;
|
|
|
|
|
1994-11-26 19:24:13 +00:00
|
|
|
clistp->c_cbmax = roundup(ccmax, CBSIZE) / CBSIZE;
|
|
|
|
dcbr = roundup(ccreserved, CBSIZE) / CBSIZE - clistp->c_cbreserved;
|
|
|
|
if (dcbr >= 0)
|
|
|
|
cblock_alloc_cblocks(dcbr);
|
|
|
|
else {
|
|
|
|
if (clistp->c_cbreserved + dcbr < clistp->c_cbcount)
|
|
|
|
dcbr = clistp->c_cbcount - clistp->c_cbreserved;
|
|
|
|
cblock_free_cblocks(-dcbr);
|
|
|
|
}
|
|
|
|
clistp->c_cbreserved += dcbr;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
1994-05-27 12:28:05 +00:00
|
|
|
* Free some cblocks from the cfreelist queue back to the
|
|
|
|
* system malloc pool.
|
|
|
|
*/
|
1994-11-26 19:24:13 +00:00
|
|
|
static void
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free_cblocks(number)
|
|
|
|
int number;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1994-11-26 19:24:13 +00:00
|
|
|
for (i = 0; i < number; ++i)
|
|
|
|
free(cblock_alloc(), M_TTYS);
|
|
|
|
ctotcount -= number;
|
1994-05-27 12:28:05 +00:00
|
|
|
}
|
|
|
|
|
1994-11-26 19:24:13 +00:00
|
|
|
/*
|
|
|
|
* Free the cblocks reserved for a clist.
|
|
|
|
* Must be called at spltty().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clist_free_cblocks(clistp)
|
|
|
|
struct clist *clistp;
|
|
|
|
{
|
|
|
|
if (clistp->c_cbcount != 0)
|
|
|
|
panic("freeing active clist cblocks");
|
|
|
|
cblock_free_cblocks(clistp->c_cbreserved);
|
|
|
|
clistp->c_cbmax = 0;
|
|
|
|
clistp->c_cbreserved = 0;
|
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a character from the head of a clist.
|
1994-05-25 09:21:21 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
getc(clistp)
|
|
|
|
struct clist *clistp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
int chr = -1;
|
|
|
|
int s;
|
|
|
|
struct cblock *cblockp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
s = spltty();
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/* If there are characters in the list, get one */
|
|
|
|
if (clistp->c_cc) {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
chr = (u_char)*clistp->c_cf;
|
1994-05-27 12:28:05 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
|
|
|
* If this char is quoted, set the flag.
|
|
|
|
*/
|
|
|
|
if (isset(cblockp->c_quote, clistp->c_cf - (char *)cblockp->c_info))
|
|
|
|
chr |= TTY_QUOTE;
|
1994-05-27 12:28:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Advance to next character.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
clistp->c_cf++;
|
|
|
|
clistp->c_cc--;
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If we have advanced the 'first' character pointer
|
|
|
|
* past the end of this cblock, advance to the next one.
|
|
|
|
* If there are no more characters, set the first and
|
|
|
|
* last pointers to NULL. In either case, free the
|
|
|
|
* current cblock.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if ((clistp->c_cf >= (char *)(cblockp+1)) || (clistp->c_cc == 0)) {
|
|
|
|
if (clistp->c_cc > 0) {
|
|
|
|
clistp->c_cf = cblockp->c_next->c_info;
|
|
|
|
} else {
|
|
|
|
clistp->c_cf = clistp->c_cl = NULL;
|
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp);
|
1994-11-26 19:24:13 +00:00
|
|
|
if (--clistp->c_cbcount >= clistp->c_cbreserved)
|
|
|
|
++cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
splx(s);
|
|
|
|
return (chr);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
|
|
|
* Copy 'amount' of chars, beginning at head of clist 'clistp' to
|
1994-05-27 12:28:05 +00:00
|
|
|
* destination linear buffer 'dest'. Return number of characters
|
|
|
|
* actually copied.
|
1994-05-25 09:21:21 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
q_to_b(clistp, dest, amount)
|
|
|
|
struct clist *clistp;
|
|
|
|
char *dest;
|
|
|
|
int amount;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
struct cblock *cblockp;
|
|
|
|
struct cblock *cblockn;
|
|
|
|
char *dest_orig = dest;
|
|
|
|
int numc;
|
|
|
|
int s;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
while (clistp && amount && (clistp->c_cc > 0)) {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
cblockn = cblockp + 1; /* pointer arithmetic! */
|
|
|
|
numc = min(amount, (char *)cblockn - clistp->c_cf);
|
|
|
|
numc = min(numc, clistp->c_cc);
|
|
|
|
bcopy(clistp->c_cf, dest, numc);
|
|
|
|
amount -= numc;
|
|
|
|
clistp->c_cf += numc;
|
|
|
|
clistp->c_cc -= numc;
|
|
|
|
dest += numc;
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If this cblock has been emptied, advance to the next
|
|
|
|
* one. If there are no more characters, set the first
|
|
|
|
* and last pointer to NULL. In either case, free the
|
|
|
|
* current cblock.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if ((clistp->c_cf >= (char *)cblockn) || (clistp->c_cc == 0)) {
|
|
|
|
if (clistp->c_cc > 0) {
|
|
|
|
clistp->c_cf = cblockp->c_next->c_info;
|
|
|
|
} else {
|
|
|
|
clistp->c_cf = clistp->c_cl = NULL;
|
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp);
|
1994-11-26 19:24:13 +00:00
|
|
|
if (--clistp->c_cbcount >= clistp->c_cbreserved)
|
|
|
|
++cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return (dest - dest_orig);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
|
|
|
* Flush 'amount' of chars, beginning at head of clist 'clistp'.
|
|
|
|
*/
|
1994-05-24 10:09:53 +00:00
|
|
|
void
|
1994-05-25 09:21:21 +00:00
|
|
|
ndflush(clistp, amount)
|
|
|
|
struct clist *clistp;
|
|
|
|
int amount;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
struct cblock *cblockp;
|
|
|
|
struct cblock *cblockn;
|
|
|
|
int numc;
|
|
|
|
int s;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
while (amount && (clistp->c_cc > 0)) {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
cblockn = cblockp + 1; /* pointer arithmetic! */
|
|
|
|
numc = min(amount, (char *)cblockn - clistp->c_cf);
|
|
|
|
numc = min(numc, clistp->c_cc);
|
|
|
|
amount -= numc;
|
|
|
|
clistp->c_cf += numc;
|
|
|
|
clistp->c_cc -= numc;
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If this cblock has been emptied, advance to the next
|
|
|
|
* one. If there are no more characters, set the first
|
|
|
|
* and last pointer to NULL. In either case, free the
|
|
|
|
* current cblock.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if ((clistp->c_cf >= (char *)cblockn) || (clistp->c_cc == 0)) {
|
|
|
|
if (clistp->c_cc > 0) {
|
|
|
|
clistp->c_cf = cblockp->c_next->c_info;
|
|
|
|
} else {
|
|
|
|
clistp->c_cf = clistp->c_cl = NULL;
|
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp);
|
1994-11-26 19:24:13 +00:00
|
|
|
if (--clistp->c_cbcount >= clistp->c_cbreserved)
|
|
|
|
++cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* Add a character to the end of a clist. Return -1 is no
|
|
|
|
* more clists, or 0 for success.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
|
|
|
putc(chr, clistp)
|
|
|
|
int chr;
|
|
|
|
struct clist *clistp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-09-25 19:34:02 +00:00
|
|
|
struct cblock *cblockp;
|
1994-05-25 09:21:21 +00:00
|
|
|
int s;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
if (clistp->c_cl == NULL) {
|
1995-10-31 19:00:00 +00:00
|
|
|
if (clistp->c_cbreserved < 1) {
|
|
|
|
splx(s);
|
1995-11-02 08:37:22 +00:00
|
|
|
printf("putc to a clist with no reserved cblocks\n");
|
|
|
|
return (-1); /* nothing done */
|
1995-10-31 19:00:00 +00:00
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
cblockp = cblock_alloc();
|
1994-11-26 19:24:13 +00:00
|
|
|
clistp->c_cbcount = 1;
|
|
|
|
clistp->c_cf = clistp->c_cl = cblockp->c_info;
|
|
|
|
clistp->c_cc = 0;
|
1994-05-25 09:21:21 +00:00
|
|
|
} else {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
|
|
|
|
if (((intptr_t)clistp->c_cl & CROUND) == 0) {
|
1994-05-27 12:28:05 +00:00
|
|
|
struct cblock *prev = (cblockp - 1);
|
1994-11-26 19:24:13 +00:00
|
|
|
|
|
|
|
if (clistp->c_cbcount >= clistp->c_cbreserved) {
|
|
|
|
if (clistp->c_cbcount >= clistp->c_cbmax
|
|
|
|
|| cslushcount <= 0) {
|
|
|
|
splx(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
--cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
1994-11-26 19:24:13 +00:00
|
|
|
cblockp = cblock_alloc();
|
|
|
|
clistp->c_cbcount++;
|
|
|
|
prev->c_next = cblockp;
|
|
|
|
clistp->c_cl = cblockp->c_info;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If this character is quoted, set the quote bit, if not, clear it.
|
|
|
|
*/
|
1995-07-11 19:39:54 +00:00
|
|
|
if (chr & TTY_QUOTE) {
|
1994-05-25 09:21:21 +00:00
|
|
|
setbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info);
|
1995-07-11 19:39:54 +00:00
|
|
|
/*
|
|
|
|
* Use one of the spare quote bits to record that something
|
|
|
|
* may be quoted.
|
|
|
|
*/
|
|
|
|
setbit(cblockp->c_quote, CBQSIZE * NBBY - 1);
|
|
|
|
} else
|
1994-05-27 12:28:05 +00:00
|
|
|
clrbit(cblockp->c_quote, clistp->c_cl - (char *)cblockp->c_info);
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
*clistp->c_cl++ = chr;
|
|
|
|
clistp->c_cc++;
|
|
|
|
|
|
|
|
splx(s);
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
1994-05-27 12:28:05 +00:00
|
|
|
* Copy data from linear buffer to clist chain. Return the
|
|
|
|
* number of characters not copied.
|
1994-05-25 09:21:21 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
b_to_q(src, amount, clistp)
|
|
|
|
char *src;
|
|
|
|
int amount;
|
|
|
|
struct clist *clistp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-09-25 19:34:02 +00:00
|
|
|
struct cblock *cblockp;
|
1994-05-27 12:28:05 +00:00
|
|
|
char *firstbyte, *lastbyte;
|
|
|
|
u_char startmask, endmask;
|
|
|
|
int startbit, endbit, num_between, numc;
|
1994-05-25 09:21:21 +00:00
|
|
|
int s;
|
|
|
|
|
1994-11-26 19:24:13 +00:00
|
|
|
/*
|
|
|
|
* Avoid allocating an initial cblock and then not using it.
|
|
|
|
* c_cc == 0 must imply c_cbount == 0.
|
|
|
|
*/
|
|
|
|
if (amount <= 0)
|
|
|
|
return (amount);
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
s = spltty();
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
1994-05-25 09:21:21 +00:00
|
|
|
* If there are no cblocks assigned to this clist yet,
|
|
|
|
* then get one.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if (clistp->c_cl == NULL) {
|
1995-10-31 19:00:00 +00:00
|
|
|
if (clistp->c_cbreserved < 1) {
|
|
|
|
splx(s);
|
1995-11-02 08:37:22 +00:00
|
|
|
printf("b_to_q to a clist with no reserved cblocks.\n");
|
|
|
|
return (amount); /* nothing done */
|
1995-10-31 19:00:00 +00:00
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
cblockp = cblock_alloc();
|
1994-11-26 19:24:13 +00:00
|
|
|
clistp->c_cbcount = 1;
|
|
|
|
clistp->c_cf = clistp->c_cl = cblockp->c_info;
|
|
|
|
clistp->c_cc = 0;
|
1994-05-25 09:21:21 +00:00
|
|
|
} else {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (amount) {
|
|
|
|
/*
|
|
|
|
* Get another cblock if needed.
|
|
|
|
*/
|
1998-07-15 02:32:35 +00:00
|
|
|
if (((intptr_t)clistp->c_cl & CROUND) == 0) {
|
1994-05-27 12:28:05 +00:00
|
|
|
struct cblock *prev = cblockp - 1;
|
1994-11-26 19:24:13 +00:00
|
|
|
|
|
|
|
if (clistp->c_cbcount >= clistp->c_cbreserved) {
|
|
|
|
if (clistp->c_cbcount >= clistp->c_cbmax
|
|
|
|
|| cslushcount <= 0) {
|
|
|
|
splx(s);
|
|
|
|
return (amount);
|
|
|
|
}
|
|
|
|
--cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
1994-11-26 19:24:13 +00:00
|
|
|
cblockp = cblock_alloc();
|
|
|
|
clistp->c_cbcount++;
|
|
|
|
prev->c_next = cblockp;
|
|
|
|
clistp->c_cl = cblockp->c_info;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy a chunk of the linear buffer up to the end
|
|
|
|
* of this cblock.
|
|
|
|
*/
|
1994-05-27 12:28:05 +00:00
|
|
|
numc = min(amount, (char *)(cblockp + 1) - clistp->c_cl);
|
1994-05-25 09:21:21 +00:00
|
|
|
bcopy(src, clistp->c_cl, numc);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
1995-07-11 19:39:54 +00:00
|
|
|
* Clear quote bits if they aren't known to be clear.
|
2001-02-06 11:21:58 +00:00
|
|
|
* The following could probably be made into a separate
|
1995-07-11 19:39:54 +00:00
|
|
|
* "bitzero()" routine, but why bother?
|
1994-05-25 09:21:21 +00:00
|
|
|
*/
|
1995-07-11 19:39:54 +00:00
|
|
|
if (isset(cblockp->c_quote, CBQSIZE * NBBY - 1)) {
|
|
|
|
startbit = clistp->c_cl - (char *)cblockp->c_info;
|
|
|
|
endbit = startbit + numc - 1;
|
|
|
|
|
|
|
|
firstbyte = (u_char *)cblockp->c_quote + (startbit / NBBY);
|
|
|
|
lastbyte = (u_char *)cblockp->c_quote + (endbit / NBBY);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate mask of bits to preserve in first and
|
|
|
|
* last bytes.
|
|
|
|
*/
|
|
|
|
startmask = NBBY - (startbit % NBBY);
|
|
|
|
startmask = 0xff >> startmask;
|
|
|
|
endmask = (endbit % NBBY);
|
|
|
|
endmask = 0xff << (endmask + 1);
|
|
|
|
|
|
|
|
if (firstbyte != lastbyte) {
|
|
|
|
*firstbyte &= startmask;
|
|
|
|
*lastbyte &= endmask;
|
|
|
|
|
|
|
|
num_between = lastbyte - firstbyte - 1;
|
|
|
|
if (num_between)
|
|
|
|
bzero(firstbyte + 1, num_between);
|
|
|
|
} else {
|
|
|
|
*firstbyte &= (startmask | endmask);
|
|
|
|
}
|
1994-05-27 12:28:05 +00:00
|
|
|
}
|
1994-05-25 09:21:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ...and update pointer for the next chunk.
|
|
|
|
*/
|
|
|
|
src += numc;
|
|
|
|
clistp->c_cl += numc;
|
|
|
|
clistp->c_cc += numc;
|
|
|
|
amount -= numc;
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If we go through the loop again, it's always
|
|
|
|
* for data in the next cblock, so by adding one (cblock),
|
|
|
|
* (which makes the pointer 1 beyond the end of this
|
|
|
|
* cblock) we prepare for the assignment of 'prev'
|
|
|
|
* above.
|
|
|
|
*/
|
|
|
|
cblockp += 1;
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return (amount);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* Get the next character in the clist. Store it at dst. Don't
|
|
|
|
* advance any clist pointers, but return a pointer to the next
|
|
|
|
* character position.
|
|
|
|
*/
|
1994-05-24 10:09:53 +00:00
|
|
|
char *
|
1994-05-25 09:21:21 +00:00
|
|
|
nextc(clistp, cp, dst)
|
|
|
|
struct clist *clistp;
|
|
|
|
char *cp;
|
|
|
|
int *dst;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
struct cblock *cblockp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
++cp;
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* See if the next character is beyond the end of
|
|
|
|
* the clist.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if (clistp->c_cc && (cp != clistp->c_cl)) {
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* If the next character is beyond the end of this
|
|
|
|
* cblock, advance to the next cblock.
|
|
|
|
*/
|
1998-07-15 02:32:35 +00:00
|
|
|
if (((intptr_t)cp & CROUND) == 0)
|
1994-05-25 09:21:21 +00:00
|
|
|
cp = ((struct cblock *)cp - 1)->c_next->c_info;
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)cp & ~CROUND);
|
1994-05-27 12:28:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the character. Set the quote flag if this character
|
|
|
|
* is quoted.
|
|
|
|
*/
|
|
|
|
*dst = (u_char)*cp | (isset(cblockp->c_quote, cp - (char *)cblockp->c_info) ? TTY_QUOTE : 0);
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* "Unput" a character from a clist.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
|
|
|
unputc(clistp)
|
|
|
|
struct clist *clistp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-25 09:21:21 +00:00
|
|
|
struct cblock *cblockp = 0, *cbp = 0;
|
|
|
|
int s;
|
|
|
|
int chr = -1;
|
|
|
|
|
|
|
|
|
|
|
|
s = spltty();
|
|
|
|
|
|
|
|
if (clistp->c_cc) {
|
|
|
|
--clistp->c_cc;
|
1994-05-27 12:28:05 +00:00
|
|
|
--clistp->c_cl;
|
1994-05-25 09:21:21 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
chr = (u_char)*clistp->c_cl;
|
1994-05-25 09:21:21 +00:00
|
|
|
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
1995-05-30 08:16:23 +00:00
|
|
|
* Set quote flag if this character was quoted.
|
1994-05-27 12:28:05 +00:00
|
|
|
*/
|
|
|
|
if (isset(cblockp->c_quote, (u_char *)clistp->c_cl - cblockp->c_info))
|
|
|
|
chr |= TTY_QUOTE;
|
|
|
|
|
1994-05-25 09:21:21 +00:00
|
|
|
/*
|
|
|
|
* If all of the characters have been unput in this
|
1994-05-27 12:28:05 +00:00
|
|
|
* cblock, then find the previous one and free this
|
1994-05-25 09:21:21 +00:00
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
if (clistp->c_cc && (clistp->c_cl <= (char *)cblockp->c_info)) {
|
1998-07-15 02:32:35 +00:00
|
|
|
cbp = (struct cblock *)((intptr_t)clistp->c_cf & ~CROUND);
|
1994-05-25 09:21:21 +00:00
|
|
|
|
|
|
|
while (cbp->c_next != cblockp)
|
|
|
|
cbp = cbp->c_next;
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* When the previous cblock is at the end, the 'last'
|
|
|
|
* pointer always points (invalidly) one past.
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
clistp->c_cl = (char *)(cbp+1);
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp);
|
1994-11-26 19:24:13 +00:00
|
|
|
if (--clistp->c_cbcount >= clistp->c_cbreserved)
|
|
|
|
++cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
cbp->c_next = NULL;
|
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
1994-05-25 09:21:21 +00:00
|
|
|
* If there are no more characters on the list, then
|
|
|
|
* free the last cblock.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
if ((clistp->c_cc == 0) && clistp->c_cl) {
|
1998-07-15 02:32:35 +00:00
|
|
|
cblockp = (struct cblock *)((intptr_t)clistp->c_cl & ~CROUND);
|
1994-05-27 12:28:05 +00:00
|
|
|
cblock_free(cblockp);
|
1994-11-26 19:24:13 +00:00
|
|
|
if (--clistp->c_cbcount >= clistp->c_cbreserved)
|
|
|
|
++cslushcount;
|
1994-05-25 09:21:21 +00:00
|
|
|
clistp->c_cf = clistp->c_cl = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return (chr);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
/*
|
|
|
|
* Move characters in source clist to destination clist,
|
|
|
|
* preserving quote bits.
|
|
|
|
*/
|
1994-05-24 10:09:53 +00:00
|
|
|
void
|
1994-05-25 09:21:21 +00:00
|
|
|
catq(src_clistp, dest_clistp)
|
|
|
|
struct clist *src_clistp, *dest_clistp;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-05-27 12:28:05 +00:00
|
|
|
int chr, s;
|
1994-05-25 09:21:21 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
s = spltty();
|
|
|
|
/*
|
|
|
|
* If the destination clist is empty (has no cblocks atttached),
|
1994-11-26 19:24:13 +00:00
|
|
|
* and there are no possible complications with the resource counters,
|
1994-05-27 12:28:05 +00:00
|
|
|
* then we simply assign the current clist to the destination.
|
|
|
|
*/
|
1994-11-26 19:24:13 +00:00
|
|
|
if (!dest_clistp->c_cf
|
|
|
|
&& src_clistp->c_cbcount <= src_clistp->c_cbmax
|
|
|
|
&& src_clistp->c_cbcount <= dest_clistp->c_cbmax) {
|
1994-05-27 12:28:05 +00:00
|
|
|
dest_clistp->c_cf = src_clistp->c_cf;
|
|
|
|
dest_clistp->c_cl = src_clistp->c_cl;
|
|
|
|
src_clistp->c_cf = src_clistp->c_cl = NULL;
|
|
|
|
|
|
|
|
dest_clistp->c_cc = src_clistp->c_cc;
|
|
|
|
src_clistp->c_cc = 0;
|
1994-11-26 19:24:13 +00:00
|
|
|
dest_clistp->c_cbcount = src_clistp->c_cbcount;
|
|
|
|
src_clistp->c_cbcount = 0;
|
1994-05-27 12:28:05 +00:00
|
|
|
|
|
|
|
splx(s);
|
|
|
|
return;
|
1994-05-25 09:21:21 +00:00
|
|
|
}
|
1994-11-26 19:24:13 +00:00
|
|
|
|
1994-05-27 12:28:05 +00:00
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX This should probably be optimized to more than one
|
|
|
|
* character at a time.
|
|
|
|
*/
|
|
|
|
while ((chr = getc(src_clistp)) != -1)
|
|
|
|
putc(chr, dest_clistp);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|