#ident "$Header: buffer.c 1.5 89/08/07 $"

/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*
 * $Log:	buffer.c,v $
 */

#include "crash.h"
#include <sys/types.h>
#include <sys/buf.h>
#include <sys/sysmacros.h>

#define INKERNEL
#include <sys/var.h>

int nbuf;
struct buf *bufp;
struct buf *readbuf();

buffer_init()
{
	struct var v;

	readv(search("buf"), &bufp, sizeof bufp);
	readv(search("v"), &v, sizeof v);
	nbuf = v.v_buf;

}

Bufhdr()
{

	struct buf *bb, *b = bufp;
	register int j;
	char *arg;
	int count, slt;

	if (live)
		buffer_init();

	printf("BUF   ADDR   COUNT  MAJ MIN BLKNO    VNODE  TYPE  FLAGS\n");
						
	if ((arg = token()) == NULL) {		/* print the works */
		for (j = nbuf; j > 0; b++, j--) {
			if ((bb = readbuf(b)) == NULL)
				continue;
			if (bb->b_flags & B_INVAL)
				continue;
			printf("%-3d %#6x", nbuf - j, b);
			prbufhdr(bb);
		}
	} else if (memcmp("0x", arg, 2) == 0) {/* buffer address supplied */
		printf("    %#6x", atoi(arg));
		if( (bb = readbuf(atoi(arg))) != NULL)
			prbufhdr(bb);
	} else {				/* selective print */
		slt = atoi(arg);
		b += slt;
		count = atoi(token());
		if (!count) count = 1;
		count = ((slt + count) > nbuf) ? (nbuf - slt) : count;
		for (j = count; j > 0; b++, j--, slt++) {
			if ((bb = readbuf(b)) == NULL)
				continue;
			printf("%-3d %#6x", slt, b);
			prbufhdr(bb);
		}
	}	
}

prbufhdr(b)
register struct buf *b;
{

	register char	ch;

	printf(" %-6d %#6x %-6d %#6x",
	        b->b_bcount, b->b_devp, 
		b->b_blkno, b->b_vp);

	switch(b->b_iotype) {
	case (B_FILIO):
		printf("  %s  ", "fil");
		break;
	case (B_RAWIO):
		printf("  %s  ", "raw ");
		break;
	case (B_PTEIO):
		printf("  %s  ", "pte ");
		break;
	case (B_PTBIO):
		printf("  %s  ", "ptb ");
		break;
	}
	printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
	b->b_flags & B_WRITE ? "wrt " : "",
	b->b_flags & B_READ ? "rd " : "",
	b->b_flags & B_ERROR ? "err " : "",
	b->b_flags & B_PHYS ? "phy " : "",
	b->b_flags & B_AGE ? "age ": "",
	b->b_flags & B_ASYNC ? "asy ": "",
	b->b_flags & B_DELWRI ? "del ": "",
	b->b_flags & B_TAPE ? "tap ": "",
	b->b_flags & B_UAREA ? "uar ": "",
	b->b_flags & B_PAGET ? "pag ": "",
	b->b_flags & B_DIRTY ? "dir ": "",
	b->b_flags & B_PGIN ? "pgi ": "",
	b->b_flags & B_INVAL ? "inv ": "",
	b->b_flags & B_HEAD ? "hed ": "",
	b->b_flags & B_CALL ? "call ": "",
	b->b_flags & B_IOCTL ? "ioc ": "",
	b->b_flags & B_REALLOC ? "rlc ": "",
	b->b_flags & B_NOTREF ? "nrf ": "",
	b->b_flags & B_NOCLR ? "ncl ": "");

	return;

}

buffer()
{
}

struct buf *
readbuf(bb)
	struct buf *bb;
{

	static struct buf bufbuf;

	if (readv(bb, &bufbuf, sizeof bufbuf) != sizeof bufbuf) {
		printf("read error on buf\n");
		return NULL;
	}
	return &bufbuf;

}

