xref: /netbsd-src/sys/ddb/db_output.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: db_output.c,v 1.22 2000/03/30 11:31:27 augustss Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 
29 /*
30  * Printf and character output for debugger.
31  */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 
35 #include <machine/stdarg.h>
36 
37 #include <dev/cons.h>
38 
39 #include <machine/db_machdep.h>
40 
41 #include <ddb/db_command.h>
42 #include <ddb/db_output.h>
43 #include <ddb/db_interface.h>
44 #include <ddb/db_sym.h>
45 #include <ddb/db_extern.h>
46 
47 /*
48  *	Character output - tracks position in line.
49  *	To do this correctly, we should know how wide
50  *	the output device is - then we could zero
51  *	the line position when the output device wraps
52  *	around to the start of the next line.
53  *
54  *	Instead, we count the number of spaces printed
55  *	since the last printing character so that we
56  *	don't print trailing spaces.  This avoids most
57  *	of the wraparounds.
58  */
59 
60 #ifndef	DB_MAX_LINE
61 #define	DB_MAX_LINE		24	/* maximum line */
62 #define DB_MAX_WIDTH		80	/* maximum width */
63 #endif	DB_MAX_LINE
64 
65 #define DB_MIN_MAX_WIDTH	20	/* minimum max width */
66 #define DB_MIN_MAX_LINE		3	/* minimum max line */
67 #define CTRL(c)			((c) & 0xff)
68 
69 int	db_output_position = 0;		/* output column */
70 int	db_output_line = 0;		/* output line number */
71 int	db_last_non_space = 0;		/* last non-space character */
72 int	db_tab_stop_width = 8;		/* how wide are tab stops? */
73 int	db_max_line = DB_MAX_LINE;	/* output max lines */
74 int	db_max_width = DB_MAX_WIDTH;	/* output line width */
75 
76 static void db_more __P((void));
77 
78 /*
79  * Force pending whitespace.
80  */
81 void
82 db_force_whitespace()
83 {
84 	int last_print, next_tab;
85 
86 	last_print = db_last_non_space;
87 	while (last_print < db_output_position) {
88 	    next_tab = DB_NEXT_TAB(last_print);
89 	    if (next_tab <= db_output_position) {
90 		while (last_print < next_tab) { /* DON'T send a tab!!! */
91 			cnputc(' ');
92 			last_print++;
93 		}
94 	    }
95 	    else {
96 		cnputc(' ');
97 		last_print++;
98 	    }
99 	}
100 	db_last_non_space = db_output_position;
101 }
102 
103 static void
104 db_more()
105 {
106 	char *p;
107 	int quit_output = 0;
108 
109 	for (p = "--db_more--"; *p; p++)
110 	    cnputc(*p);
111 	switch(cngetc()) {
112 	case ' ':
113 	    db_output_line = 0;
114 	    break;
115 	case 'q':
116 	case CTRL('c'):
117 	    db_output_line = 0;
118 	    quit_output = 1;
119 	    break;
120 	default:
121 	    db_output_line--;
122 	    break;
123 	}
124 	p = "\b\b\b\b\b\b\b\b\b\b\b           \b\b\b\b\b\b\b\b\b\b\b";
125 	while (*p)
126 	    cnputc(*p++);
127 	if (quit_output) {
128 	    db_error(0);
129 	    /* NOTREACHED */
130 	}
131 }
132 
133 /*
134  * Output character.  Buffer whitespace.
135  */
136 void
137 db_putchar(c)
138 	int	c;		/* character to output */
139 {
140 	if (db_max_line >= DB_MIN_MAX_LINE && db_output_line >= db_max_line-1)
141 	    db_more();
142 	if (c > ' ' && c <= '~') {
143 	    /*
144 	     * Printing character.
145 	     * If we have spaces to print, print them first.
146 	     * Use tabs if possible.
147 	     */
148 	    db_force_whitespace();
149 	    cnputc(c);
150 	    db_output_position++;
151 	    if (db_max_width >= DB_MIN_MAX_WIDTH
152 		&& db_output_position >= db_max_width) {
153 		/* auto new line */
154 		cnputc('\n');
155 		db_output_position = 0;
156 		db_last_non_space = 0;
157 		db_output_line++;
158 	    }
159 	    db_last_non_space = db_output_position;
160 	}
161 	else if (c == '\n') {
162 	    /* Return */
163 	    cnputc(c);
164 	    db_output_position = 0;
165 	    db_last_non_space = 0;
166 	    db_output_line++;
167 	    db_check_interrupt();
168 	}
169 	else if (c == '\t') {
170 	    /* assume tabs every 8 positions */
171 	    db_output_position = DB_NEXT_TAB(db_output_position);
172 	}
173 	else if (c == ' ') {
174 	    /* space */
175 	    db_output_position++;
176 	}
177 	else if (c == '\007') {
178 	    /* bell */
179 	    cnputc(c);
180 	}
181 	/* other characters are assumed non-printing */
182 }
183 
184 /*
185  * Return output position
186  */
187 int
188 db_print_position()
189 {
190 	return (db_output_position);
191 }
192 
193 /*
194  * End line if too long.
195  */
196 void
197 db_end_line()
198 {
199 	if (db_output_position >= db_max_width)
200 	    db_printf("\n");
201 }
202