xref: /freebsd/sys/ddb/db_output.c (revision c7c34a24a3f07aa94c633605632dc562e8ad01cb)
15b81b6b3SRodney W. Grimes /*
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  *
26c7c34a24SBruce Evans  *	$Id: db_output.c,v 1.18 1996/05/08 04:28:35 gpalmer Exp $
275b81b6b3SRodney W. Grimes  */
280edf66ecSRodney W. Grimes 
295b81b6b3SRodney W. Grimes /*
305b81b6b3SRodney W. Grimes  * 	Author: David B. Golub, Carnegie Mellon University
315b81b6b3SRodney W. Grimes  *	Date:	7/90
325b81b6b3SRodney W. Grimes  */
335b81b6b3SRodney W. Grimes 
345b81b6b3SRodney W. Grimes /*
355b81b6b3SRodney W. Grimes  * Printf and character output for debugger.
365b81b6b3SRodney W. Grimes  */
375b81b6b3SRodney W. Grimes 
38f540b106SGarrett Wollman #include <sys/param.h>
39f540b106SGarrett Wollman #include <sys/systm.h>
405ccbc3ccSBruce Evans 
415ccbc3ccSBruce Evans #include <machine/cons.h>
42f540b106SGarrett Wollman #include <machine/stdarg.h>
435ccbc3ccSBruce Evans 
44f540b106SGarrett Wollman #include <ddb/ddb.h>
45058284fcSBruce Evans #include <ddb/db_output.h>
465b81b6b3SRodney W. Grimes 
475b81b6b3SRodney W. Grimes /*
485b81b6b3SRodney W. Grimes  *	Character output - tracks position in line.
495b81b6b3SRodney W. Grimes  *	To do this correctly, we should know how wide
505b81b6b3SRodney W. Grimes  *	the output device is - then we could zero
515b81b6b3SRodney W. Grimes  *	the line position when the output device wraps
525b81b6b3SRodney W. Grimes  *	around to the start of the next line.
535b81b6b3SRodney W. Grimes  *
545b81b6b3SRodney W. Grimes  *	Instead, we count the number of spaces printed
555b81b6b3SRodney W. Grimes  *	since the last printing character so that we
565b81b6b3SRodney W. Grimes  *	don't print trailing spaces.  This avoids most
575b81b6b3SRodney W. Grimes  *	of the wraparounds.
585b81b6b3SRodney W. Grimes  */
59f73a856dSPoul-Henning Kamp static int	db_output_position = 0;		/* output column */
60f73a856dSPoul-Henning Kamp static int	db_last_non_space = 0;		/* last non-space character */
615b81b6b3SRodney W. Grimes int	db_tab_stop_width = 8;		/* how wide are tab stops? */
625b81b6b3SRodney W. Grimes #define	NEXT_TAB(i) \
635b81b6b3SRodney W. Grimes 	((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width)
64c7c34a24SBruce Evans int	db_max_width = 79;		/* output line width */
655b81b6b3SRodney W. Grimes 
666ddbf1e2SGary Palmer static void db_putchar __P((int c, void *arg));
676ddbf1e2SGary Palmer 
685b81b6b3SRodney W. Grimes /*
695b81b6b3SRodney W. Grimes  * Force pending whitespace.
705b81b6b3SRodney W. Grimes  */
715b81b6b3SRodney W. Grimes void
725b81b6b3SRodney W. Grimes db_force_whitespace()
735b81b6b3SRodney W. Grimes {
745b81b6b3SRodney W. Grimes 	register int last_print, next_tab;
755b81b6b3SRodney W. Grimes 
765b81b6b3SRodney W. Grimes 	last_print = db_last_non_space;
775b81b6b3SRodney W. Grimes 	while (last_print < db_output_position) {
785b81b6b3SRodney W. Grimes 	    next_tab = NEXT_TAB(last_print);
795b81b6b3SRodney W. Grimes 	    if (next_tab <= db_output_position) {
805b81b6b3SRodney W. Grimes 		while (last_print < next_tab) { /* DON'T send a tab!!! */
815b81b6b3SRodney W. Grimes 			cnputc(' ');
825b81b6b3SRodney W. Grimes 			last_print++;
835b81b6b3SRodney W. Grimes 		}
845b81b6b3SRodney W. Grimes 	    }
855b81b6b3SRodney W. Grimes 	    else {
865b81b6b3SRodney W. Grimes 		cnputc(' ');
875b81b6b3SRodney W. Grimes 		last_print++;
885b81b6b3SRodney W. Grimes 	    }
895b81b6b3SRodney W. Grimes 	}
905b81b6b3SRodney W. Grimes 	db_last_non_space = db_output_position;
915b81b6b3SRodney W. Grimes }
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes /*
945b81b6b3SRodney W. Grimes  * Output character.  Buffer whitespace.
955b81b6b3SRodney W. Grimes  */
966ddbf1e2SGary Palmer static void
976ddbf1e2SGary Palmer db_putchar(c, arg)
985b81b6b3SRodney W. Grimes 	int	c;		/* character to output */
996ddbf1e2SGary Palmer 	void *	arg;
1005b81b6b3SRodney W. Grimes {
1015b81b6b3SRodney W. Grimes 	if (c > ' ' && c <= '~') {
1025b81b6b3SRodney W. Grimes 	    /*
1035b81b6b3SRodney W. Grimes 	     * Printing character.
1045b81b6b3SRodney W. Grimes 	     * If we have spaces to print, print them first.
1055b81b6b3SRodney W. Grimes 	     * Use tabs if possible.
1065b81b6b3SRodney W. Grimes 	     */
1075b81b6b3SRodney W. Grimes 	    db_force_whitespace();
1085b81b6b3SRodney W. Grimes 	    cnputc(c);
1095b81b6b3SRodney W. Grimes 	    db_output_position++;
1105b81b6b3SRodney W. Grimes 	    db_last_non_space = db_output_position;
1115b81b6b3SRodney W. Grimes 	}
1125b81b6b3SRodney W. Grimes 	else if (c == '\n') {
1138a129caeSDavid Greenman 	    /* Newline */
1148a129caeSDavid Greenman 	    cnputc(c);
1158a129caeSDavid Greenman 	    db_output_position = 0;
1168a129caeSDavid Greenman 	    db_last_non_space = 0;
1178a129caeSDavid Greenman 	    db_check_interrupt();
1188a129caeSDavid Greenman 	}
1198a129caeSDavid Greenman 	else if (c == '\r') {
1205b81b6b3SRodney W. Grimes 	    /* Return */
1215b81b6b3SRodney W. Grimes 	    cnputc(c);
1225b81b6b3SRodney W. Grimes 	    db_output_position = 0;
1235b81b6b3SRodney W. Grimes 	    db_last_non_space = 0;
1245b81b6b3SRodney W. Grimes 	    db_check_interrupt();
1255b81b6b3SRodney W. Grimes 	}
1265b81b6b3SRodney W. Grimes 	else if (c == '\t') {
1275b81b6b3SRodney W. Grimes 	    /* assume tabs every 8 positions */
1285b81b6b3SRodney W. Grimes 	    db_output_position = NEXT_TAB(db_output_position);
1295b81b6b3SRodney W. Grimes 	}
1305b81b6b3SRodney W. Grimes 	else if (c == ' ') {
1315b81b6b3SRodney W. Grimes 	    /* space */
1325b81b6b3SRodney W. Grimes 	    db_output_position++;
1335b81b6b3SRodney W. Grimes 	}
1345b81b6b3SRodney W. Grimes 	else if (c == '\007') {
1355b81b6b3SRodney W. Grimes 	    /* bell */
1365b81b6b3SRodney W. Grimes 	    cnputc(c);
1375b81b6b3SRodney W. Grimes 	}
1385b81b6b3SRodney W. Grimes 	/* other characters are assumed non-printing */
1395b81b6b3SRodney W. Grimes }
1405b81b6b3SRodney W. Grimes 
1415b81b6b3SRodney W. Grimes /*
1425b81b6b3SRodney W. Grimes  * Return output position
1435b81b6b3SRodney W. Grimes  */
1445b81b6b3SRodney W. Grimes int
1455b81b6b3SRodney W. Grimes db_print_position()
1465b81b6b3SRodney W. Grimes {
1475b81b6b3SRodney W. Grimes 	return (db_output_position);
1485b81b6b3SRodney W. Grimes }
1495b81b6b3SRodney W. Grimes 
1505b81b6b3SRodney W. Grimes /*
1515b81b6b3SRodney W. Grimes  * Printing
1525b81b6b3SRodney W. Grimes  */
153381fe1aaSGarrett Wollman void
154381fe1aaSGarrett Wollman db_printf(const char *fmt, ...)
1555b81b6b3SRodney W. Grimes {
1565b81b6b3SRodney W. Grimes 	va_list	listp;
157c7c34a24SBruce Evans 
158c7c34a24SBruce Evans 	va_start(listp, fmt);
159c7c34a24SBruce Evans 	kvprintf (fmt, db_putchar, NULL, db_radix, listp);
160c7c34a24SBruce Evans 	va_end(listp);
161c7c34a24SBruce Evans }
162c7c34a24SBruce Evans 
163c7c34a24SBruce Evans int db_indent;
164c7c34a24SBruce Evans 
165c7c34a24SBruce Evans void
166c7c34a24SBruce Evans db_iprintf(const char *fmt,...)
167c7c34a24SBruce Evans {
168c7c34a24SBruce Evans 	register int i;
169c7c34a24SBruce Evans 	va_list listp;
170c7c34a24SBruce Evans 
171c7c34a24SBruce Evans 	for (i = db_indent; i >= 8; i -= 8)
172c7c34a24SBruce Evans 		db_printf("\t");
173c7c34a24SBruce Evans 	while (--i >= 0)
174c7c34a24SBruce Evans 		db_printf(" ");
1755b81b6b3SRodney W. Grimes 	va_start(listp, fmt);
176791d77e0SPoul-Henning Kamp 	kvprintf (fmt, db_putchar, NULL, db_radix, listp);
1775b81b6b3SRodney W. Grimes 	va_end(listp);
1785b81b6b3SRodney W. Grimes }
1795b81b6b3SRodney W. Grimes 
1805b81b6b3SRodney W. Grimes /*
181572de915SRodney W. Grimes  * End line if too long.
182572de915SRodney W. Grimes  */
183572de915SRodney W. Grimes void
184572de915SRodney W. Grimes db_end_line()
185572de915SRodney W. Grimes {
186572de915SRodney W. Grimes 	if (db_output_position >= db_max_width)
187572de915SRodney W. Grimes 	    db_printf("\n");
188572de915SRodney W. Grimes }
189