130296Ssam /*
230296Ssam  * Copyright (c) 1986 Regents of the University of California.
330296Ssam  * All rights reserved.  The Berkeley software License Agreement
430296Ssam  * specifies the terms and conditions for redistribution.
530296Ssam  *
6*41340Ssklower  *	@(#)kdb_output.c	7.3 (Berkeley) 05/03/90
730296Ssam  */
830110Ssam 
930110Ssam #include "../kdb/defs.h"
1030110Ssam 
11*41340Ssklower long	kdbmaxpos;
12*41340Ssklower int	kdbradix = 16;
1330110Ssam 
14*41340Ssklower char	kdbprintbuf[MAXLIN];
15*41340Ssklower char	*kdbprintptr = kdbprintbuf;
16*41340Ssklower char	*kdbdigitptr;
1730110Ssam 
kdbprintc(c)18*41340Ssklower kdbprintc(c)
1930110Ssam 	char c;
2030110Ssam {
2130110Ssam 	char d;
2230110Ssam 	register char *q;
2330110Ssam 	register posn, tabs, p;
2430110Ssam 
25*41340Ssklower 	if (kdbmkfault)
2630110Ssam 		return;
27*41340Ssklower 	if ((*kdbprintptr=c)==EOR) {
28*41340Ssklower 		tabs=0; posn=0; q=kdbprintbuf;
29*41340Ssklower 		for (p=0; p<kdbprintptr-kdbprintbuf; p++) {
30*41340Ssklower 			d=kdbprintbuf[p];
3130110Ssam 			if ((p&7)==0 && posn) {
3230110Ssam 				tabs++;
3330110Ssam 				posn=0;
3430110Ssam 			}
3530110Ssam 			if (d!=SP) {
3630110Ssam 				while (tabs>0)
3730110Ssam 					*q++=TB, tabs--;
3830110Ssam 				while (posn>0)
3930110Ssam 					*q++=SP, posn--;
4030110Ssam 				*q++=d;
4130110Ssam 			} else
4230110Ssam 				posn++;
4330110Ssam 		 }
4430110Ssam 		 *q++=EOR;
45*41340Ssklower 		 kdbwrite(kdbprintbuf,q-kdbprintbuf);
46*41340Ssklower 		 kdbprintptr=kdbprintbuf;
4730110Ssam 	} else if (c==TB) {
48*41340Ssklower 		*kdbprintptr++=SP;
49*41340Ssklower 		while ((kdbprintptr-kdbprintbuf)&7)
50*41340Ssklower 			*kdbprintptr++=SP;
5130110Ssam 	} else if (c)
52*41340Ssklower 		kdbprintptr++;
53*41340Ssklower 	if (kdbprintptr >= &kdbprintbuf[MAXLIN-9]) {
54*41340Ssklower 		kdbwrite(kdbprintbuf, kdbprintptr - kdbprintbuf);
55*41340Ssklower 		kdbprintptr = kdbprintbuf;
5630110Ssam 	}
5730110Ssam }
5830110Ssam 
kdbcharpos()59*41340Ssklower kdbcharpos()
6030110Ssam {
6130110Ssam 
62*41340Ssklower 	return (kdbprintptr-kdbprintbuf);
6330110Ssam }
6430110Ssam 
kdbflushbuf()65*41340Ssklower kdbflushbuf()
6630110Ssam {
6730110Ssam 
68*41340Ssklower 	if (kdbprintptr!=kdbprintbuf)
69*41340Ssklower 		kdbprintc(EOR);
7030110Ssam }
7130110Ssam 
7230110Ssam /* VARARGS1 */
kdbprintf(fmat,a1)73*41340Ssklower kdbprintf(fmat,a1)
7430110Ssam 	char *fmat, *a1;
7530110Ssam {
7630110Ssam 	char *fptr;
7730110Ssam 	register char *s;
7830110Ssam 	register long *dptr;
7930110Ssam 	register width, prec;
8030110Ssam 	char c, adj;
8130110Ssam 	int x, n;
8230110Ssam 	register long lx;
8330110Ssam 	char digits[64];
8430110Ssam 
8530110Ssam 	fptr = fmat; dptr = (long *)&a1;
8630110Ssam 	while (c = *fptr++) {
8730110Ssam 		if (c!='%') {
88*41340Ssklower 			kdbprintc(c);
8930110Ssam 			continue;
9030110Ssam 		}
9130110Ssam 		if (*fptr=='-') {
9230110Ssam 			adj='l'; fptr++;
9330110Ssam 		} else
9430110Ssam 			adj='r';
95*41340Ssklower 		width=kdbconvert(&fptr);
9630110Ssam 		if (*fptr=='.') {
97*41340Ssklower 			fptr++; prec=kdbconvert(&fptr);
9830110Ssam 		} else
9930110Ssam 			prec = -1;
100*41340Ssklower 		kdbdigitptr=digits;
10130296Ssam 		x = lx = *dptr++;
10230110Ssam 		s=0;
10330110Ssam 		switch (c = *fptr++) {
10430110Ssam 		case 'd':
105*41340Ssklower 			kdbprintnum((u_long)x, -10); break;
10630110Ssam 		case 'u':
107*41340Ssklower 			kdbprintnum((u_long)x, 10); break;
10830110Ssam 		case 'o':
109*41340Ssklower 			kdbprintnum((u_long)x, 8); break;
11030110Ssam 		case 'q':
111*41340Ssklower 			kdbprintnum((u_long)x, -8); break;
11230110Ssam 		case 'x':
113*41340Ssklower 			kdbprintnum((u_long)x, 16); break;
11430110Ssam 		case 'z':
115*41340Ssklower 			kdbprintnum((u_long)x, -16); break;
11630110Ssam 		case 'R':
117*41340Ssklower 			kdbprintnum((u_long)lx, kdbradix); break;
11830110Ssam 		case 'D':
119*41340Ssklower 			kdbprintnum((u_long)lx, -10); break;
12030110Ssam 		case 'U':
121*41340Ssklower 			kdbprintnum((u_long)lx, 10); break;
12230110Ssam 		case 'O':
123*41340Ssklower 			kdbprintnum((u_long)lx, 8); break;
12430110Ssam 		case 'Q':
125*41340Ssklower 			kdbprintnum((u_long)lx, -8); break;
12630110Ssam 		case 'X':
127*41340Ssklower 			kdbprintnum((u_long)lx, 16); break;
12830110Ssam 		case 'Z':
129*41340Ssklower 			kdbprintnum((u_long)lx, -16); break;
13030110Ssam 		case 'c':
131*41340Ssklower 			kdbprintc(x); break;
13230110Ssam 		case 's':
13330110Ssam 			s=(char *)lx; break;
13430110Ssam 		case 'm':
13530110Ssam 			break;
13630110Ssam 		case 'M':
13730110Ssam 			width=x; break;
13830110Ssam 		case 'T': case 't':
13930110Ssam 			if (c=='T')
14030110Ssam 				width=x;
14130110Ssam 			else
14230110Ssam 				dptr--;
14330110Ssam 			if (width)
144*41340Ssklower 				width -= kdbcharpos()%width;
14530110Ssam 			break;
14630110Ssam 		default:
147*41340Ssklower 			kdbprintc(c); dptr--;
14830110Ssam 			break;
14930110Ssam 		}
15030110Ssam 		if (s==0) {
151*41340Ssklower 			*kdbdigitptr=0; s=digits;
15230110Ssam 		}
15330110Ssam 		n=strlen(s);
15430110Ssam 		n=(prec<n && prec>=0 ? prec : n);
15530110Ssam 		width -= n;
15630110Ssam 		if (adj=='r')
15730110Ssam 			while (width-- > 0)
158*41340Ssklower 				kdbprintc(SP);
15930110Ssam 		while (n--)
160*41340Ssklower 			kdbprintc(*s++);
16130110Ssam 		while (width-- > 0)
162*41340Ssklower 			kdbprintc(SP);
163*41340Ssklower 		kdbdigitptr=digits;
16430110Ssam 	}
16530110Ssam }
16630110Ssam 
16730110Ssam static
kdbconvert(cp)168*41340Ssklower kdbconvert(cp)
16930110Ssam 	register char **cp;
17030110Ssam {
17130110Ssam 	register char c;
17230110Ssam 	int n;
17330110Ssam 
17430110Ssam 	n=0;
17530110Ssam 	while (((c = *(*cp)++)>='0') && c<='9')
17630110Ssam 		n=n*10+c-'0';
17730110Ssam 	(*cp)--;
17830110Ssam 	return (n);
17930110Ssam }
18030110Ssam 
18130110Ssam static
kdbprintnum(n,base)182*41340Ssklower kdbprintnum(n, base)
18330296Ssam 	register u_long n;
18430110Ssam {
18530110Ssam 	register char *dptr;
18630110Ssam 	char digs[15];
18730110Ssam 
18830110Ssam 	dptr=digs;
18930110Ssam 	if (base<0) {
19030110Ssam 		base = -base;
19130110Ssam 		if ((long)n<0) {
19230110Ssam 			n = -n;
193*41340Ssklower 			*kdbdigitptr++ = '-';
19430110Ssam 		}
19530110Ssam 	}
19630110Ssam 	while (n) {
19730110Ssam 		*dptr++ = n%base;
19830110Ssam 		n /= base;
19930110Ssam 	}
20030110Ssam 	if (dptr==digs)
20130110Ssam 		*dptr++=0;
20230110Ssam 	while (dptr!=digs) {
20330110Ssam 		n = *--dptr;
204*41340Ssklower 		*kdbdigitptr++ = (n+(n<=9 ? '0' : 'a'-10));
20530110Ssam 	}
20630110Ssam }
20730110Ssam 
kdbendline()208*41340Ssklower kdbendline()
20930110Ssam {
21030110Ssam 
211*41340Ssklower 	if (kdbmaxpos <= kdbcharpos())
212*41340Ssklower 		kdbprintf("\n");
21330110Ssam }
214