freebsd-dev/sys/ddb/db_output.c
Poul-Henning Kamp 791d77e0dd Get rid of two and a half printf in the kernel.
Add more features to the one remaining to handle the job:
	+	signed quantity.
	#	alternate format
	-	left padding
	*	read width as next arg.
	n	numeric in (argument specified) default radix.

Fix the DDB debugger to use these.
Use vprintf in debug routine in pcvt.

The warnings from gcc may become more wrong and  intolerable because
of this.

Warning:  I have not checked the entire source for unsupported or
changed constructs, but generally belive that there are only a few.

Suggested by: bde
1996-01-15 22:41:03 +00:00

193 lines
4.4 KiB
C

/*
* Mach Operating System
* Copyright (c) 1991,1990 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*
* $Id: db_output.c,v 1.15 1995/12/10 19:08:03 bde Exp $
*/
/*
* Author: David B. Golub, Carnegie Mellon University
* Date: 7/90
*/
/*
* Printf and character output for debugger.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <machine/cons.h>
#include <machine/stdarg.h>
#include <ddb/ddb.h>
#include <ddb/db_output.h>
/*
* Character output - tracks position in line.
* To do this correctly, we should know how wide
* the output device is - then we could zero
* the line position when the output device wraps
* around to the start of the next line.
*
* Instead, we count the number of spaces printed
* since the last printing character so that we
* don't print trailing spaces. This avoids most
* of the wraparounds.
*/
static int db_output_position = 0; /* output column */
static int db_last_non_space = 0; /* last non-space character */
int db_tab_stop_width = 8; /* how wide are tab stops? */
#define NEXT_TAB(i) \
((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
int db_max_width = 80; /* output line width */
static char *db_ksprintn __P((u_long ul, int base, int *lenp));
static void db_printf_guts __P((const char *, va_list));
/*
* Force pending whitespace.
*/
void
db_force_whitespace()
{
register int last_print, next_tab;
last_print = db_last_non_space;
while (last_print < db_output_position) {
next_tab = NEXT_TAB(last_print);
if (next_tab <= db_output_position) {
while (last_print < next_tab) { /* DON'T send a tab!!! */
cnputc(' ');
last_print++;
}
}
else {
cnputc(' ');
last_print++;
}
}
db_last_non_space = db_output_position;
}
/*
* Output character. Buffer whitespace.
*/
void
db_putchar(c)
int c; /* character to output */
{
if (c > ' ' && c <= '~') {
/*
* Printing character.
* If we have spaces to print, print them first.
* Use tabs if possible.
*/
db_force_whitespace();
cnputc(c);
db_output_position++;
db_last_non_space = db_output_position;
}
else if (c == '\n') {
/* Newline */
cnputc(c);
db_output_position = 0;
db_last_non_space = 0;
db_check_interrupt();
}
else if (c == '\r') {
/* Return */
cnputc(c);
db_output_position = 0;
db_last_non_space = 0;
db_check_interrupt();
}
else if (c == '\t') {
/* assume tabs every 8 positions */
db_output_position = NEXT_TAB(db_output_position);
}
else if (c == ' ') {
/* space */
db_output_position++;
}
else if (c == '\007') {
/* bell */
cnputc(c);
}
/* other characters are assumed non-printing */
}
/*
* Return output position
*/
int
db_print_position()
{
return (db_output_position);
}
/*
* Printing
*/
void
db_printf(const char *fmt, ...)
{
va_list listp;
va_start(listp, fmt);
kvprintf (fmt, db_putchar, NULL, db_radix, listp);
va_end(listp);
}
/*
* End line if too long.
*/
void
db_end_line()
{
if (db_output_position >= db_max_width)
db_printf("\n");
}
/*
* Put a number (base <= 16) in a buffer in reverse order; return an
* optional length and a pointer to the NULL terminated (preceded?)
* buffer.
*/
static char *
db_ksprintn(ul, base, lenp)
register u_long ul;
register int base, *lenp;
{ /* A long in base 8, plus NULL. */
static char buf[sizeof(long) * NBBY / 3 + 2];
register char *p;
p = buf;
do {
*++p = "0123456789abcdef"[ul % base];
} while (ul /= base);
if (lenp)
*lenp = p - buf;
return (p);
}