xref: /original-bsd/usr.bin/pascal/pxp/pp.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)pp.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * pxp - Pascal execution profiler
14  *
15  * Bill Joy UCB
16  * Version 1.2 January 1979
17  */
18 
19 #include "0.h"
20 
21 #define noprint() nopflg
22 
23 int	pplev[3];	/* STAT, DECL, PRFN */
24 int	nopflg;
25 
26 setprint()
27 {
28 
29 	if (profile == 0) {
30 		if (table)
31 			nopflg = 1;
32 		else
33 			nopflg = 0;
34 		return;
35 	}
36 	nopflg = !all && nowcnt() == 0 || !opt('z');
37 }
38 
39 printon()
40 {
41 
42 	if (profile == 0) {
43 		if (table)
44 			nopflg = 1;
45 		return;
46 	}
47 	nopflg = 0;
48 }
49 
50 printoff()
51 {
52 
53 	nopflg = 1;
54 }
55 
56 ppkw(s)
57 	register char *s;
58 {
59 	register char *cp, i;
60 
61 	if (noprint())
62 		return;
63 	/*
64 	 * First real thing printed
65 	 * is always a keyword
66 	 * or includes an "id" (if a comment)
67 	 * (See ppnl below)
68 	 */
69 	hadsome = 1;
70 	if (underline) {
71 		for (cp = s; *cp; cp++)
72 			putchar('_');
73 		for (cp = s; *cp; cp++)
74 			putchar('\b');
75 	}
76 	printf(s);
77 }
78 
79 ppid(s)
80 	register char *s;
81 {
82 
83 	if (noprint())
84 		return;
85 	hadsome = 1;
86 	if (s == NIL)
87 		s = "{identifier}";
88 	printf(s);
89 }
90 
91 ppbra(s)
92 	char *s;
93 {
94 
95 	if (noprint())
96 		return;
97 	if (s != NIL)
98 		printf(s);
99 }
100 
101 ppsep(s)
102 	char *s;
103 {
104 
105 	if (noprint())
106 		return;
107 	printf(s);
108 }
109 
110 ppket(s)
111 	char *s;
112 {
113 
114 	if (noprint())
115 		return;
116 	if (s != NIL)
117 		printf(s);
118 }
119 
120 char	killsp;
121 
122 ppunspac()
123 {
124 
125 	killsp = 1;
126 }
127 
128 ppspac()
129 {
130 
131 	if (killsp) {
132 		killsp = 0;
133 		return;
134 	}
135 	if (noprint())
136 		return;
137 	putchar(' ');
138 }
139 
140 ppitem()
141 {
142 
143 	if (noprint())
144 		return;
145 	ppnl();
146 	indent();
147 }
148 
149 int	owenl, owenlb;
150 
151 ppsnlb()
152 {
153 
154 	if (nopflg)
155 		return;
156 	owenlb++;
157 }
158 
159 ppsnl()
160 {
161 
162 	if (nopflg)
163 		return;
164 	owenl++;
165 }
166 
167 pppay()
168 {
169 
170 	while (owenl || owenlb) {
171 		putchar('\n');
172 		if (owenlb) {
173 			putchar(' ');
174 			owenlb--;
175 		} else
176 			owenl--;
177 	}
178 }
179 
180 ppnl()
181 {
182 
183 	if (noprint())
184 		return;
185 	if (hadsome == 0)
186 		return;
187 	pppay();
188 	putchar('\n');
189 }
190 
191 indent()
192 {
193 	register i;
194 
195 	if (noprint())
196 		return;
197 	linopr();
198 	if (profile == 0) {
199 		indent1(pplev[PRFN] + pplev[DECL] + pplev[STAT]);
200 		return;
201 	}
202 	indent1(pplev[PRFN] + pplev[STAT]);
203 	switch (i = shudpcnt()) {
204 		case 1:
205 			printf("%7ld.", nowcnt());
206 			dashes('-');
207 			putchar('|');
208 			break;
209 		case 0:
210 		case -1:
211 			printf("        ");
212 			dashes(' ');
213 			putchar(i == 0 ? '|' : ' ');
214 			break;
215 	}
216 	indent1(pplev[DECL]);
217 }
218 
219 dashes(c)
220 	char c;
221 {
222 	register i;
223 
224 	for (i = unit - 1; i != 0; i--)
225 		putchar(c);
226 }
227 
228 indent1(in)
229 	int in;
230 {
231 	register i;
232 
233 	if (noprint())
234 		return;
235 	i = in;
236 	if (profile == 0)
237 		while (i >= 8) {
238 			putchar('\t');
239 			i -= 8;
240 		}
241 	while (i > 0) {
242 		putchar(' ');
243 		i--;
244 	}
245 }
246 
247 linopr()
248 {
249 
250 	if (noprint())
251 		return;
252 	if (profile) {
253 		if (line < 0)
254 			line = -line;
255 		printf("%6d  ", line);
256 	}
257 }
258 
259 indentlab()
260 {
261 
262 	indent1(pplev[PRFN]);
263 }
264 
265 ppop(s)
266 	char *s;
267 {
268 
269 	if (noprint())
270 		return;
271 	printf(s);
272 }
273 
274 ppnumb(s)
275 	char *s;
276 {
277 
278 	if (noprint())
279 		return;
280 	if (s == NIL)
281 		s = "{number}";
282 	printf(s);
283 }
284 
285 ppgoin(lv)
286 {
287 
288 	pplev[lv] += unit;
289 }
290 
291 ppgoout(lv)
292 {
293 
294 	pplev[lv] -= unit;
295 	if (pplev[lv] < 0)
296 		panic("pplev");
297 }
298 
299 ppstr(s)
300 	char *s;
301 {
302 	register char *cp;
303 
304 	if (noprint())
305 		return;
306 	if (s == NIL) {
307 		printf("{string}");
308 		return;
309 	}
310 	putchar('\'');
311 	cp = s;
312 	while (*cp) {
313 		putchar(*cp);
314 		if (*cp == '\'')
315 			putchar('\'');
316 		cp++;
317 	}
318 	putchar('\'');
319 }
320 
321 pplab(s)
322 	char *s;
323 {
324 
325 	if (noprint())
326 		return;
327 	if (s == NIL)
328 		s = "{integer label}";
329 	printf(s);
330 }
331 
332 int	outcol;
333 
334 
335 putchar(c)
336 	char c;
337 {
338 
339 	putc(c, stdout);
340 	if (ferror(stdout))
341 		outerr();
342 	switch (c) {
343 		case '\n':
344 			outcol = 0;
345 			flush();
346 			break;
347 		case '\t':
348 			outcol += 8;
349 			outcol &= ~07;
350 			break;
351 		case '\b':
352 			if (outcol)
353 				outcol--;
354 			break;
355 		default:
356 			outcol++;
357 		case '\f':
358 			break;
359 	}
360 }
361 
362 flush()
363 {
364 
365 	fflush(stdout);
366 	if (ferror(stdout))
367 		outerr();
368 }
369 
370 pptab()
371 {
372 	register int i;
373 
374 	if (noprint())
375 		return;
376 	i = pplev[PRFN] + profile ? 44 + unit : 28;
377 /*
378 	if (outcol > i + 8) {
379 		ppnl();
380 		i += 8;
381 	}
382 */
383 	do
384 		putchar('\t');
385 	while (outcol < i);
386 }
387 
388 outerr()
389 {
390 
391 	perror(stdoutn);
392 	pexit(DIED);
393 }
394