#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/malloc.h>
#define QBITS
#ifdef QBITS
#define QMEM(n) ((((n)-1)/NBBY)+1)
#else
#define QMEM(n) (n)
#endif
void
cinit(void)
{
}
int
clalloc(struct clist *clp, int size, int quot)
{
MALLOC_ZONE(clp->c_cs, u_char *, size, M_TTYS, M_WAITOK);
if (!clp->c_cs)
return (-1);
bzero(clp->c_cs, size);
if(quot) {
MALLOC_ZONE(clp->c_cq, u_char *, QMEM(size), M_TTYS, M_WAITOK);
if (!clp->c_cq) {
FREE_ZONE(clp->c_cs, size, M_TTYS);
return (-1);
}
bzero(clp->c_cs, QMEM(size));
} else
clp->c_cq = (u_char *)0;
clp->c_cf = clp->c_cl = (u_char *)0;
clp->c_ce = clp->c_cs + size;
clp->c_cn = size;
clp->c_cc = 0;
return (0);
}
void
clfree(struct clist *clp)
{
if(clp->c_cs)
FREE_ZONE(clp->c_cs, clp->c_cn, M_TTYS);
if(clp->c_cq)
FREE_ZONE(clp->c_cq, QMEM(clp->c_cn), M_TTYS);
clp->c_cs = clp->c_cq = (u_char *)0;
}
int
getc(struct clist *clp)
{
int c = -1;
if (clp->c_cc == 0)
goto out;
c = *clp->c_cf & 0xff;
if (clp->c_cq) {
#ifdef QBITS
if (isset(clp->c_cq, clp->c_cf - clp->c_cs) )
c |= TTY_QUOTE;
#else
if (*(clp->c_cf - clp->c_cs + clp->c_cq))
c |= TTY_QUOTE;
#endif
}
if (++clp->c_cf == clp->c_ce)
clp->c_cf = clp->c_cs;
if (--clp->c_cc == 0)
clp->c_cf = clp->c_cl = (u_char *)0;
out:
return c;
}
int
q_to_b(struct clist *clp, u_char *cp, int count)
{
int cc;
u_char *p = cp;
while (count > 0 && clp->c_cc > 0) {
cc = clp->c_cl - clp->c_cf;
if (clp->c_cf >= clp->c_cl)
cc = clp->c_ce - clp->c_cf;
if (cc > count)
cc = count;
bcopy(clp->c_cf, p, cc);
count -= cc;
p += cc;
clp->c_cc -= cc;
clp->c_cf += cc;
if (clp->c_cf == clp->c_ce)
clp->c_cf = clp->c_cs;
}
if (clp->c_cc == 0)
clp->c_cf = clp->c_cl = (u_char *)0;
return p - cp;
}
int
ndqb(struct clist *clp, int flag)
{
int count = 0;
register int i;
register int cc;
if ((cc = clp->c_cc) == 0)
goto out;
if (flag == 0) {
count = clp->c_cl - clp->c_cf;
if (count <= 0)
count = clp->c_ce - clp->c_cf;
goto out;
}
i = clp->c_cf - clp->c_cs;
if (flag & TTY_QUOTE) {
while (cc-- > 0 && !(clp->c_cs[i++] & (flag & ~TTY_QUOTE) ||
isset(clp->c_cq, i))) {
count++;
if (i == clp->c_cn)
break;
}
} else {
while (cc-- > 0 && !(clp->c_cs[i++] & flag)) {
count++;
if (i == clp->c_cn)
break;
}
}
out:
return count;
}
void
ndflush(struct clist *clp, int count)
{
int cc;
if (count == clp->c_cc) {
clp->c_cc = 0;
clp->c_cf = clp->c_cl = (u_char *)0;
return;
}
while (count > 0 && clp->c_cc > 0) {
cc = clp->c_cl - clp->c_cf;
if (clp->c_cf >= clp->c_cl)
cc = clp->c_ce - clp->c_cf;
if (cc > count)
cc = count;
count -= cc;
clp->c_cc -= cc;
clp->c_cf += cc;
if (clp->c_cf == clp->c_ce)
clp->c_cf = clp->c_cs;
}
if (clp->c_cc == 0)
clp->c_cf = clp->c_cl = (u_char *)0;
}
int
putc(int c, struct clist *clp)
{
register int i;
if (clp->c_cc == 0) {
if (!clp->c_cs) {
#if DIAGNOSTIC
#endif
if(clalloc(clp, 1024, 1)) {
out:
return -1;
}
}
clp->c_cf = clp->c_cl = clp->c_cs;
}
if (clp->c_cc == clp->c_cn)
goto out;
*clp->c_cl = c & 0xff;
i = clp->c_cl - clp->c_cs;
if (clp->c_cq) {
#ifdef QBITS
if (c & TTY_QUOTE)
setbit(clp->c_cq, i);
else
clrbit(clp->c_cq, i);
#else
q = clp->c_cq + i;
*q = (c & TTY_QUOTE) ? 1 : 0;
#endif
}
clp->c_cc++;
clp->c_cl++;
if (clp->c_cl == clp->c_ce)
clp->c_cl = clp->c_cs;
return 0;
}
#ifdef QBITS
void
clrbits(u_char *cp, int off, int len)
{
int sby, sbi, eby, ebi;
register int i;
u_char mask;
if(len==1) {
clrbit(cp, off);
return;
}
sby = off / NBBY;
sbi = off % NBBY;
eby = (off+len) / NBBY;
ebi = (off+len) % NBBY;
if (sby == eby) {
mask = ((1 << (ebi - sbi)) - 1) << sbi;
cp[sby] &= ~mask;
} else {
mask = (1<<sbi) - 1;
cp[sby++] &= mask;
mask = (1<<ebi) - 1;
if (mask)
cp[eby] &= ~mask;
for (i = sby; i < eby; i++)
cp[i] = 0x00;
}
}
#endif
int
b_to_q(const u_char *cp, int count, struct clist *clp)
{
int cc;
const u_char *p = cp;
if (count <= 0)
return 0;
if (clp->c_cc == 0) {
if (!clp->c_cs) {
#if DIAGNOSTIC
printf("b_to_q: required clalloc\n");
#endif
if(clalloc(clp, 1024, 1))
goto out;
}
clp->c_cf = clp->c_cl = clp->c_cs;
}
if (clp->c_cc == clp->c_cn)
goto out;
while (count > 0 && clp->c_cc < clp->c_cn) {
cc = clp->c_ce - clp->c_cl;
if (clp->c_cf > clp->c_cl)
cc = clp->c_cf - clp->c_cl;
if (cc > count)
cc = count;
bcopy(p, clp->c_cl, cc);
if (clp->c_cq) {
#ifdef QBITS
clrbits(clp->c_cq, clp->c_cl - clp->c_cs, cc);
#else
bzero(clp->c_cl - clp->c_cs + clp->c_cq, cc);
#endif
}
p += cc;
count -= cc;
clp->c_cc += cc;
clp->c_cl += cc;
if (clp->c_cl == clp->c_ce)
clp->c_cl = clp->c_cs;
}
out:
return count;
}
static int cc;
u_char *
nextc(struct clist *clp, u_char *cp, int *c)
{
if (clp->c_cf == cp) {
cc = clp->c_cc;
}
if (cc == 0 || cp == NULL)
return NULL;
if (--cc == 0)
return NULL;
if (++cp == clp->c_ce)
cp = clp->c_cs;
*c = *cp & 0xff;
if (clp->c_cq) {
#ifdef QBITS
if (isset(clp->c_cq, cp - clp->c_cs))
*c |= TTY_QUOTE;
#else
if (*(clp->c_cf - clp->c_cs + clp->c_cq))
*c |= TTY_QUOTE;
#endif
}
return cp;
}
u_char *
firstc(struct clist *clp, int *c)
{
u_char *cp;
cc = clp->c_cc;
if (cc == 0)
return NULL;
cp = clp->c_cf;
*c = *cp & 0xff;
if(clp->c_cq) {
#ifdef QBITS
if (isset(clp->c_cq, cp - clp->c_cs))
*c |= TTY_QUOTE;
#else
if (*(cp - clp->c_cs + clp->c_cq))
*c |= TTY_QUOTE;
#endif
}
return clp->c_cf;
}
int
unputc(struct clist *clp)
{
unsigned int c = -1;
if (clp->c_cc == 0)
goto out;
if (clp->c_cl == clp->c_cs)
clp->c_cl = clp->c_ce - 1;
else
--clp->c_cl;
clp->c_cc--;
c = *clp->c_cl & 0xff;
if (clp->c_cq) {
#ifdef QBITS
if (isset(clp->c_cq, clp->c_cl - clp->c_cs))
c |= TTY_QUOTE;
#else
if (*(clp->c_cf - clp->c_cs + clp->c_cq))
c |= TTY_QUOTE;
#endif
}
if (clp->c_cc == 0)
clp->c_cf = clp->c_cl = (u_char *)0;
out:
return c;
}
void
catq(struct clist *from, struct clist *to)
{
int c;
while ((c = getc(from)) != -1)
putc(c, to);
}