xref: /freebsd/sys/ddb/db_output.c (revision 1e16f6098bf69794d9be2a7722a1ee1ac090508d)
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  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  * 	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
305b81b6b3SRodney W. Grimes 
315b81b6b3SRodney W. Grimes /*
325b81b6b3SRodney W. Grimes  * Printf and character output for debugger.
335b81b6b3SRodney W. Grimes  */
345b81b6b3SRodney W. Grimes 
35753960f7SDavid E. O'Brien #include <sys/cdefs.h>
36753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
37753960f7SDavid E. O'Brien 
38f540b106SGarrett Wollman #include <sys/param.h>
39f540b106SGarrett Wollman #include <sys/systm.h>
40ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
415ccbc3ccSBruce Evans 
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 */
613da6ef3cSBruce Evans db_expr_t	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)
643da6ef3cSBruce Evans db_expr_t	db_max_width = 79;		/* output line width */
651e16f609SJohn Baldwin static int	db_newlines;			/* # lines this page */
661e16f609SJohn Baldwin static int	db_maxlines = -1;		/* max lines per page */
671e16f609SJohn Baldwin static db_page_calloutfcn_t *db_page_callout = NULL;
681e16f609SJohn Baldwin static void	*db_page_callout_arg = NULL;
695b81b6b3SRodney W. Grimes 
7014e10f99SAlfred Perlstein static void db_putchar(int c, void *arg);
716ddbf1e2SGary Palmer 
725b81b6b3SRodney W. Grimes /*
735b81b6b3SRodney W. Grimes  * Force pending whitespace.
745b81b6b3SRodney W. Grimes  */
755b81b6b3SRodney W. Grimes void
765b81b6b3SRodney W. Grimes db_force_whitespace()
775b81b6b3SRodney W. Grimes {
785b81b6b3SRodney W. Grimes 	register int last_print, next_tab;
795b81b6b3SRodney W. Grimes 
805b81b6b3SRodney W. Grimes 	last_print = db_last_non_space;
815b81b6b3SRodney W. Grimes 	while (last_print < db_output_position) {
825b81b6b3SRodney W. Grimes 	    next_tab = NEXT_TAB(last_print);
835b81b6b3SRodney W. Grimes 	    if (next_tab <= db_output_position) {
845b81b6b3SRodney W. Grimes 		while (last_print < next_tab) { /* DON'T send a tab!!! */
855b81b6b3SRodney W. Grimes 			cnputc(' ');
865b81b6b3SRodney W. Grimes 			last_print++;
875b81b6b3SRodney W. Grimes 		}
885b81b6b3SRodney W. Grimes 	    }
895b81b6b3SRodney W. Grimes 	    else {
905b81b6b3SRodney W. Grimes 		cnputc(' ');
915b81b6b3SRodney W. Grimes 		last_print++;
925b81b6b3SRodney W. Grimes 	    }
935b81b6b3SRodney W. Grimes 	}
945b81b6b3SRodney W. Grimes 	db_last_non_space = db_output_position;
955b81b6b3SRodney W. Grimes }
965b81b6b3SRodney W. Grimes 
975b81b6b3SRodney W. Grimes /*
985b81b6b3SRodney W. Grimes  * Output character.  Buffer whitespace.
995b81b6b3SRodney W. Grimes  */
1006ddbf1e2SGary Palmer static void
1016ddbf1e2SGary Palmer db_putchar(c, arg)
1025b81b6b3SRodney W. Grimes 	int	c;		/* character to output */
1036ddbf1e2SGary Palmer 	void *	arg;
1045b81b6b3SRodney W. Grimes {
1051e16f609SJohn Baldwin 
1065b81b6b3SRodney W. Grimes 	if (c > ' ' && c <= '~') {
1075b81b6b3SRodney W. Grimes 	    /*
1085b81b6b3SRodney W. Grimes 	     * Printing character.
1095b81b6b3SRodney W. Grimes 	     * If we have spaces to print, print them first.
1105b81b6b3SRodney W. Grimes 	     * Use tabs if possible.
1115b81b6b3SRodney W. Grimes 	     */
1125b81b6b3SRodney W. Grimes 	    db_force_whitespace();
1135b81b6b3SRodney W. Grimes 	    cnputc(c);
1145b81b6b3SRodney W. Grimes 	    db_output_position++;
1155b81b6b3SRodney W. Grimes 	    db_last_non_space = db_output_position;
1165b81b6b3SRodney W. Grimes 	}
1175b81b6b3SRodney W. Grimes 	else if (c == '\n') {
1188a129caeSDavid Greenman 	    /* Newline */
1198a129caeSDavid Greenman 	    cnputc(c);
1208a129caeSDavid Greenman 	    db_output_position = 0;
1218a129caeSDavid Greenman 	    db_last_non_space = 0;
1228a129caeSDavid Greenman 	    db_check_interrupt();
1231e16f609SJohn Baldwin 	    if (db_maxlines > 0 && db_page_callout != NULL) {
1241e16f609SJohn Baldwin 		    db_newlines++;
1251e16f609SJohn Baldwin 		    if (db_newlines >= db_maxlines) {
1261e16f609SJohn Baldwin 			    db_maxlines = -1;
1271e16f609SJohn Baldwin 			    db_page_callout(db_page_callout_arg);
1281e16f609SJohn Baldwin 		    }
1291e16f609SJohn Baldwin 	    }
1308a129caeSDavid Greenman 	}
1318a129caeSDavid Greenman 	else if (c == '\r') {
1325b81b6b3SRodney W. Grimes 	    /* Return */
1335b81b6b3SRodney W. Grimes 	    cnputc(c);
1345b81b6b3SRodney W. Grimes 	    db_output_position = 0;
1355b81b6b3SRodney W. Grimes 	    db_last_non_space = 0;
1365b81b6b3SRodney W. Grimes 	    db_check_interrupt();
1375b81b6b3SRodney W. Grimes 	}
1385b81b6b3SRodney W. Grimes 	else if (c == '\t') {
1395b81b6b3SRodney W. Grimes 	    /* assume tabs every 8 positions */
1405b81b6b3SRodney W. Grimes 	    db_output_position = NEXT_TAB(db_output_position);
1415b81b6b3SRodney W. Grimes 	}
1425b81b6b3SRodney W. Grimes 	else if (c == ' ') {
1435b81b6b3SRodney W. Grimes 	    /* space */
1445b81b6b3SRodney W. Grimes 	    db_output_position++;
1455b81b6b3SRodney W. Grimes 	}
1465b81b6b3SRodney W. Grimes 	else if (c == '\007') {
1475b81b6b3SRodney W. Grimes 	    /* bell */
1485b81b6b3SRodney W. Grimes 	    cnputc(c);
1495b81b6b3SRodney W. Grimes 	}
1505b81b6b3SRodney W. Grimes 	/* other characters are assumed non-printing */
1515b81b6b3SRodney W. Grimes }
1525b81b6b3SRodney W. Grimes 
1535b81b6b3SRodney W. Grimes /*
1541e16f609SJohn Baldwin  * Register callout for providing a pager for output.
1551e16f609SJohn Baldwin  */
1561e16f609SJohn Baldwin void
1571e16f609SJohn Baldwin db_setup_paging(db_page_calloutfcn_t *callout, void *arg, int maxlines)
1581e16f609SJohn Baldwin {
1591e16f609SJohn Baldwin 
1601e16f609SJohn Baldwin 	db_page_callout = callout;
1611e16f609SJohn Baldwin 	db_page_callout_arg = arg;
1621e16f609SJohn Baldwin 	db_maxlines = maxlines;
1631e16f609SJohn Baldwin 	db_newlines = 0;
1641e16f609SJohn Baldwin }
1651e16f609SJohn Baldwin 
1661e16f609SJohn Baldwin /*
1671e16f609SJohn Baldwin  * A simple paging callout function.  If the argument is not null, it
1681e16f609SJohn Baldwin  * points to an integer that will be set to 1 if the user asks to quit.
1691e16f609SJohn Baldwin  */
1701e16f609SJohn Baldwin void
1711e16f609SJohn Baldwin db_simple_pager(void *arg)
1721e16f609SJohn Baldwin {
1731e16f609SJohn Baldwin 	int c;
1741e16f609SJohn Baldwin 
1751e16f609SJohn Baldwin 	db_printf("--More--\r");
1761e16f609SJohn Baldwin 	for (;;) {
1771e16f609SJohn Baldwin 		c = cngetc();
1781e16f609SJohn Baldwin 		switch (c) {
1791e16f609SJohn Baldwin 		case '\n':
1801e16f609SJohn Baldwin 			/* Just one more line. */
1811e16f609SJohn Baldwin 			db_setup_paging(db_simple_pager, arg, 1);
1821e16f609SJohn Baldwin 			return;
1831e16f609SJohn Baldwin 		case ' ':
1841e16f609SJohn Baldwin 			/* Another page. */
1851e16f609SJohn Baldwin 			db_setup_paging(db_simple_pager, arg,
1861e16f609SJohn Baldwin 			    DB_LINES_PER_PAGE);
1871e16f609SJohn Baldwin 			return;
1881e16f609SJohn Baldwin 		case 'q':
1891e16f609SJohn Baldwin 		case 'Q':
1901e16f609SJohn Baldwin 		case 'x':
1911e16f609SJohn Baldwin 		case 'X':
1921e16f609SJohn Baldwin 			/* Quit */
1931e16f609SJohn Baldwin 			if (arg != NULL) {
1941e16f609SJohn Baldwin 				*(int *)arg = 1;
1951e16f609SJohn Baldwin 				db_printf("\n");
1961e16f609SJohn Baldwin 				return;
1971e16f609SJohn Baldwin 			}
1981e16f609SJohn Baldwin #if 0
1991e16f609SJohn Baldwin 			/* FALLTHROUGH */
2001e16f609SJohn Baldwin 		default:
2011e16f609SJohn Baldwin 			cnputc('\007');
2021e16f609SJohn Baldwin #endif
2031e16f609SJohn Baldwin 		}
2041e16f609SJohn Baldwin 	}
2051e16f609SJohn Baldwin }
2061e16f609SJohn Baldwin 
2071e16f609SJohn Baldwin /*
2085b81b6b3SRodney W. Grimes  * Return output position
2095b81b6b3SRodney W. Grimes  */
2105b81b6b3SRodney W. Grimes int
2115b81b6b3SRodney W. Grimes db_print_position()
2125b81b6b3SRodney W. Grimes {
2135b81b6b3SRodney W. Grimes 	return (db_output_position);
2145b81b6b3SRodney W. Grimes }
2155b81b6b3SRodney W. Grimes 
2165b81b6b3SRodney W. Grimes /*
2175b81b6b3SRodney W. Grimes  * Printing
2185b81b6b3SRodney W. Grimes  */
219381fe1aaSGarrett Wollman void
220b9478d12SBruce Evans #if __STDC__
221381fe1aaSGarrett Wollman db_printf(const char *fmt, ...)
222b9478d12SBruce Evans #else
223b9478d12SBruce Evans db_printf(fmt)
224b9478d12SBruce Evans 	const char *fmt;
225b9478d12SBruce Evans #endif
2265b81b6b3SRodney W. Grimes {
2275b81b6b3SRodney W. Grimes 	va_list	listp;
228c7c34a24SBruce Evans 
229c7c34a24SBruce Evans 	va_start(listp, fmt);
230c7c34a24SBruce Evans 	kvprintf (fmt, db_putchar, NULL, db_radix, listp);
231c7c34a24SBruce Evans 	va_end(listp);
232c7c34a24SBruce Evans }
233c7c34a24SBruce Evans 
234c7c34a24SBruce Evans int db_indent;
235c7c34a24SBruce Evans 
236c7c34a24SBruce Evans void
237b9478d12SBruce Evans #if __STDC__
238c7c34a24SBruce Evans db_iprintf(const char *fmt,...)
239b9478d12SBruce Evans #else
240b9478d12SBruce Evans db_iprintf(fmt)
241b9478d12SBruce Evans 	const char *fmt;
242b9478d12SBruce Evans #endif
243c7c34a24SBruce Evans {
244c7c34a24SBruce Evans 	register int i;
245c7c34a24SBruce Evans 	va_list listp;
246c7c34a24SBruce Evans 
247c7c34a24SBruce Evans 	for (i = db_indent; i >= 8; i -= 8)
248c7c34a24SBruce Evans 		db_printf("\t");
249c7c34a24SBruce Evans 	while (--i >= 0)
250c7c34a24SBruce Evans 		db_printf(" ");
2515b81b6b3SRodney W. Grimes 	va_start(listp, fmt);
252791d77e0SPoul-Henning Kamp 	kvprintf (fmt, db_putchar, NULL, db_radix, listp);
2535b81b6b3SRodney W. Grimes 	va_end(listp);
2545b81b6b3SRodney W. Grimes }
2555b81b6b3SRodney W. Grimes 
2565b81b6b3SRodney W. Grimes /*
257572de915SRodney W. Grimes  * End line if too long.
258572de915SRodney W. Grimes  */
259572de915SRodney W. Grimes void
260572de915SRodney W. Grimes db_end_line()
261572de915SRodney W. Grimes {
262572de915SRodney W. Grimes 	if (db_output_position >= db_max_width)
263572de915SRodney W. Grimes 	    db_printf("\n");
264572de915SRodney W. Grimes }
265