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