1 /* 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 * 26 * $Id: db_output.c,v 1.16 1996/01/15 22:39:35 phk Exp $ 27 */ 28 29 /* 30 * Author: David B. Golub, Carnegie Mellon University 31 * Date: 7/90 32 */ 33 34 /* 35 * Printf and character output for debugger. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 41 #include <machine/cons.h> 42 #include <machine/stdarg.h> 43 44 #include <ddb/ddb.h> 45 #include <ddb/db_output.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 static int db_output_position = 0; /* output column */ 60 static int db_last_non_space = 0; /* last non-space character */ 61 int db_tab_stop_width = 8; /* how wide are tab stops? */ 62 #define NEXT_TAB(i) \ 63 ((((i) + db_tab_stop_width) / db_tab_stop_width) * db_tab_stop_width) 64 int db_max_width = 80; /* output line width */ 65 66 /* 67 * Force pending whitespace. 68 */ 69 void 70 db_force_whitespace() 71 { 72 register int last_print, next_tab; 73 74 last_print = db_last_non_space; 75 while (last_print < db_output_position) { 76 next_tab = NEXT_TAB(last_print); 77 if (next_tab <= db_output_position) { 78 while (last_print < next_tab) { /* DON'T send a tab!!! */ 79 cnputc(' '); 80 last_print++; 81 } 82 } 83 else { 84 cnputc(' '); 85 last_print++; 86 } 87 } 88 db_last_non_space = db_output_position; 89 } 90 91 /* 92 * Output character. Buffer whitespace. 93 */ 94 void 95 db_putchar(c) 96 int c; /* character to output */ 97 { 98 if (c > ' ' && c <= '~') { 99 /* 100 * Printing character. 101 * If we have spaces to print, print them first. 102 * Use tabs if possible. 103 */ 104 db_force_whitespace(); 105 cnputc(c); 106 db_output_position++; 107 db_last_non_space = db_output_position; 108 } 109 else if (c == '\n') { 110 /* Newline */ 111 cnputc(c); 112 db_output_position = 0; 113 db_last_non_space = 0; 114 db_check_interrupt(); 115 } 116 else if (c == '\r') { 117 /* Return */ 118 cnputc(c); 119 db_output_position = 0; 120 db_last_non_space = 0; 121 db_check_interrupt(); 122 } 123 else if (c == '\t') { 124 /* assume tabs every 8 positions */ 125 db_output_position = NEXT_TAB(db_output_position); 126 } 127 else if (c == ' ') { 128 /* space */ 129 db_output_position++; 130 } 131 else if (c == '\007') { 132 /* bell */ 133 cnputc(c); 134 } 135 /* other characters are assumed non-printing */ 136 } 137 138 /* 139 * Return output position 140 */ 141 int 142 db_print_position() 143 { 144 return (db_output_position); 145 } 146 147 /* 148 * Printing 149 */ 150 void 151 db_printf(const char *fmt, ...) 152 { 153 va_list listp; 154 va_start(listp, fmt); 155 kvprintf (fmt, db_putchar, NULL, db_radix, listp); 156 va_end(listp); 157 } 158 159 /* 160 * End line if too long. 161 */ 162 void 163 db_end_line() 164 { 165 if (db_output_position >= db_max_width) 166 db_printf("\n"); 167 } 168 169