xref: /csrg-svn/sys/vax/stand/prf.c (revision 324)
1*324Sbill /*	prf.c	1.1	06/28/80 */
2*324Sbill 
3*324Sbill #include "../h/cons.h"
4*324Sbill #include "../h/mtpr.h"
5*324Sbill 
6*324Sbill /*
7*324Sbill  * Scaled down version of C Library printf.
8*324Sbill  * Only %s %u %d (==%u) %o %x %D %c are recognized.
9*324Sbill  * Used to print diagnostic information
10*324Sbill  * directly on console tty.
11*324Sbill  * Since it is not interrupt driven,
12*324Sbill  * all system activities are pretty much
13*324Sbill  * suspended.
14*324Sbill  * Printf should not be used for chit-chat.
15*324Sbill  */
16*324Sbill /*VARARGS1*/
17*324Sbill printf(fmt, x1)
18*324Sbill register char *fmt;
19*324Sbill unsigned x1;
20*324Sbill {
21*324Sbill 	register c;
22*324Sbill 	register unsigned int *adx;
23*324Sbill 	char *s;
24*324Sbill 
25*324Sbill 	adx = &x1;
26*324Sbill loop:
27*324Sbill 	while((c = *fmt++) != '%') {
28*324Sbill 		if(c == '\0')
29*324Sbill 			return;
30*324Sbill 		putchar(c);
31*324Sbill 	}
32*324Sbill 	c = *fmt++;
33*324Sbill 	if(c == 'X')
34*324Sbill 		printx((long)*adx);
35*324Sbill 	else if(c == 'd' || c == 'u' || c == 'o' || c == 'x')
36*324Sbill 		printn((long)*adx, c=='o'? 8: (c=='x'? 16:10));
37*324Sbill 	else if(c == 'c')
38*324Sbill 		putchar(*adx);
39*324Sbill 	else if(c == 's') {
40*324Sbill 		s = (char *)*adx;
41*324Sbill 		while(c = *s++)
42*324Sbill 			putchar(c);
43*324Sbill 	} else if (c == 'D') {
44*324Sbill 		printn(*(long *)adx, 10);
45*324Sbill 		adx += (sizeof(long) / sizeof(int)) - 1;
46*324Sbill 	}
47*324Sbill 	adx++;
48*324Sbill 	goto loop;
49*324Sbill }
50*324Sbill 
51*324Sbill printx(x)
52*324Sbill long x;
53*324Sbill {
54*324Sbill 	int i;
55*324Sbill 
56*324Sbill 	for (i = 0; i < 8; i++)
57*324Sbill 		putchar("0123456789ABCDEF"[(x>>((7-i)*4))&0xf]);
58*324Sbill }
59*324Sbill 
60*324Sbill /*
61*324Sbill  * Print an unsigned integer in base b.
62*324Sbill  */
63*324Sbill printn(n, b)
64*324Sbill long n;
65*324Sbill {
66*324Sbill 	register long a;
67*324Sbill 
68*324Sbill 	if (n<0) {	/* shouldn't happen */
69*324Sbill 		putchar('-');
70*324Sbill 		n = -n;
71*324Sbill 	}
72*324Sbill 	if(a = n/b)
73*324Sbill 		printn(a, b);
74*324Sbill 	putchar("0123456789ABCDEF"[(int)(n%b)]);
75*324Sbill }
76*324Sbill 
77*324Sbill /*
78*324Sbill  * Print a character on console.
79*324Sbill  * Attempts to save and restore device
80*324Sbill  * status.
81*324Sbill  * If the switches are 0, all
82*324Sbill  * printing is inhibited.
83*324Sbill  *
84*324Sbill  * Whether or not printing is inhibited,
85*324Sbill  * the last MSGBUFS characters
86*324Sbill  * are saved in msgbuf for inspection later.
87*324Sbill  */
88*324Sbill putchar(c)
89*324Sbill register c;
90*324Sbill {
91*324Sbill 	register s, timo;
92*324Sbill 
93*324Sbill 	timo = 30000;
94*324Sbill 	/*
95*324Sbill 	 * Try waiting for the console tty to come ready,
96*324Sbill 	 * otherwise give up after a reasonable time.
97*324Sbill 	 */
98*324Sbill 	while((mfpr(TXCS)&TXCS_RDY) == 0)
99*324Sbill 		if(--timo == 0)
100*324Sbill 			break;
101*324Sbill 	if(c == 0)
102*324Sbill 		return;
103*324Sbill 	s = mfpr(TXCS);
104*324Sbill 	mtpr(TXCS,0);
105*324Sbill 	mtpr(TXDB, c&0xff);
106*324Sbill 	if(c == '\n')
107*324Sbill 		putchar('\r');
108*324Sbill 	putchar(0);
109*324Sbill 	mtpr(TXCS, s);
110*324Sbill }
111*324Sbill 
112*324Sbill getchar()
113*324Sbill {
114*324Sbill 	register c;
115*324Sbill 
116*324Sbill 	while((mfpr(RXCS)&RXCS_DONE) == 0)
117*324Sbill 		;
118*324Sbill 	c = mfpr(RXDB)&0177;
119*324Sbill 	if (c=='\r')
120*324Sbill 		c = '\n';
121*324Sbill 	putchar(c);
122*324Sbill 	return(c);
123*324Sbill }
124*324Sbill 
125*324Sbill gets(buf)
126*324Sbill char	*buf;
127*324Sbill {
128*324Sbill register char *lp;
129*324Sbill register c;
130*324Sbill 
131*324Sbill 	lp = buf;
132*324Sbill 	for (;;) {
133*324Sbill 		c = getchar() & 0177;
134*324Sbill 		if (c>='A' && c<='Z')
135*324Sbill 			c -= 'A' - 'a';
136*324Sbill 		if (lp != buf && *(lp-1) == '\\') {
137*324Sbill 			lp--;
138*324Sbill 			if (c>='a' && c<='z') {
139*324Sbill 				c += 'A' - 'a';
140*324Sbill 				goto store;
141*324Sbill 			}
142*324Sbill 			switch ( c) {
143*324Sbill 			case '(':
144*324Sbill 				c = '{';
145*324Sbill 				break;
146*324Sbill 			case ')':
147*324Sbill 				c = '}';
148*324Sbill 				break;
149*324Sbill 			case '!':
150*324Sbill 				c = '|';
151*324Sbill 				break;
152*324Sbill 			case '^':
153*324Sbill 				c = '~';
154*324Sbill 				break;
155*324Sbill 			case '\'':
156*324Sbill 				c = '`';
157*324Sbill 				break;
158*324Sbill 			}
159*324Sbill 		}
160*324Sbill 	store:
161*324Sbill 		switch(c) {
162*324Sbill 		case '\n':
163*324Sbill 		case '\r':
164*324Sbill 			c = '\n';
165*324Sbill 			*lp++ = '\0';
166*324Sbill 			return;
167*324Sbill 		case '\b':
168*324Sbill 		case '#':
169*324Sbill 			lp--;
170*324Sbill 			if (lp < buf)
171*324Sbill 				lp = buf;
172*324Sbill 			continue;
173*324Sbill 		case '@':
174*324Sbill 			lp = buf;
175*324Sbill 			putchar('\n');
176*324Sbill 			continue;
177*324Sbill 		default:
178*324Sbill 			*lp++ = c;
179*324Sbill 		}
180*324Sbill 	}
181*324Sbill }
182