1 /* 2 * Copyright (c) 1986 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)kdb_output.c 7.2 (Berkeley) 12/15/86 7 */ 8 9 #include "../kdb/defs.h" 10 11 long maxpos; 12 int radix = 16; 13 14 char printbuf[MAXLIN]; 15 char *printptr = printbuf; 16 char *digitptr; 17 18 printc(c) 19 char c; 20 { 21 char d; 22 register char *q; 23 register posn, tabs, p; 24 25 if (mkfault) 26 return; 27 if ((*printptr=c)==EOR) { 28 tabs=0; posn=0; q=printbuf; 29 for (p=0; p<printptr-printbuf; p++) { 30 d=printbuf[p]; 31 if ((p&7)==0 && posn) { 32 tabs++; 33 posn=0; 34 } 35 if (d!=SP) { 36 while (tabs>0) 37 *q++=TB, tabs--; 38 while (posn>0) 39 *q++=SP, posn--; 40 *q++=d; 41 } else 42 posn++; 43 } 44 *q++=EOR; 45 kdbwrite(printbuf,q-printbuf); 46 printptr=printbuf; 47 } else if (c==TB) { 48 *printptr++=SP; 49 while ((printptr-printbuf)&7) 50 *printptr++=SP; 51 } else if (c) 52 printptr++; 53 if (printptr >= &printbuf[MAXLIN-9]) { 54 kdbwrite(printbuf, printptr - printbuf); 55 printptr = printbuf; 56 } 57 } 58 59 charpos() 60 { 61 62 return (printptr-printbuf); 63 } 64 65 flushbuf() 66 { 67 68 if (printptr!=printbuf) 69 printc(EOR); 70 } 71 72 /* VARARGS1 */ 73 printf(fmat,a1) 74 char *fmat, *a1; 75 { 76 char *fptr; 77 register char *s; 78 register long *dptr; 79 register width, prec; 80 char c, adj; 81 int x, n; 82 register long lx; 83 char digits[64]; 84 85 fptr = fmat; dptr = (long *)&a1; 86 while (c = *fptr++) { 87 if (c!='%') { 88 printc(c); 89 continue; 90 } 91 if (*fptr=='-') { 92 adj='l'; fptr++; 93 } else 94 adj='r'; 95 width=convert(&fptr); 96 if (*fptr=='.') { 97 fptr++; prec=convert(&fptr); 98 } else 99 prec = -1; 100 digitptr=digits; 101 x = lx = *dptr++; 102 s=0; 103 switch (c = *fptr++) { 104 case 'd': 105 printnum((u_long)x, -10); break; 106 case 'u': 107 printnum((u_long)x, 10); break; 108 case 'o': 109 printnum((u_long)x, 8); break; 110 case 'q': 111 printnum((u_long)x, -8); break; 112 case 'x': 113 printnum((u_long)x, 16); break; 114 case 'z': 115 printnum((u_long)x, -16); break; 116 case 'R': 117 printnum((u_long)lx, radix); break; 118 case 'D': 119 printnum((u_long)lx, -10); break; 120 case 'U': 121 printnum((u_long)lx, 10); break; 122 case 'O': 123 printnum((u_long)lx, 8); break; 124 case 'Q': 125 printnum((u_long)lx, -8); break; 126 case 'X': 127 printnum((u_long)lx, 16); break; 128 case 'Z': 129 printnum((u_long)lx, -16); break; 130 case 'c': 131 printc(x); break; 132 case 's': 133 s=(char *)lx; break; 134 case 'm': 135 break; 136 case 'M': 137 width=x; break; 138 case 'T': case 't': 139 if (c=='T') 140 width=x; 141 else 142 dptr--; 143 if (width) 144 width -= charpos()%width; 145 break; 146 default: 147 printc(c); dptr--; 148 break; 149 } 150 if (s==0) { 151 *digitptr=0; s=digits; 152 } 153 n=strlen(s); 154 n=(prec<n && prec>=0 ? prec : n); 155 width -= n; 156 if (adj=='r') 157 while (width-- > 0) 158 printc(SP); 159 while (n--) 160 printc(*s++); 161 while (width-- > 0) 162 printc(SP); 163 digitptr=digits; 164 } 165 } 166 167 static 168 convert(cp) 169 register char **cp; 170 { 171 register char c; 172 int n; 173 174 n=0; 175 while (((c = *(*cp)++)>='0') && c<='9') 176 n=n*10+c-'0'; 177 (*cp)--; 178 return (n); 179 } 180 181 static 182 printnum(n, base) 183 register u_long n; 184 { 185 register char *dptr; 186 char digs[15]; 187 188 dptr=digs; 189 if (base<0) { 190 base = -base; 191 if ((long)n<0) { 192 n = -n; 193 *digitptr++ = '-'; 194 } 195 } 196 while (n) { 197 *dptr++ = n%base; 198 n /= base; 199 } 200 if (dptr==digs) 201 *dptr++=0; 202 while (dptr!=digs) { 203 n = *--dptr; 204 *digitptr++ = (n+(n<=9 ? '0' : 'a'-10)); 205 } 206 } 207 208 endline() 209 { 210 211 if (maxpos <= charpos()) 212 printf("\n"); 213 } 214