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