1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/****************************************************************
Copyright 1990, 1991, 1993, 1994, 2000 by AT&T, Lucent Technologies and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T, Bell Laboratories,
Lucent or Bellcore or any of their entities not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
AT&T, Lucent and Bellcore disclaim all warranties with regard to
this software, including all implied warranties of
merchantability and fitness. In no event shall AT&T, Lucent or
Bellcore be liable for any special, indirect or consequential
damages or any damages whatsoever resulting from loss of use,
data or profits, whether in an action of contract, negligence or
other tortious action, arising out of or in connection with the
use or performance of this software.
****************************************************************/
#include "defs.h"
#include "names.h"
#include "output.h"
#ifndef KR_headers
#include "stdarg.h"
#endif
#define TOO_LONG_INDENT (2 * tab_size)
#define MAX_INDENT 44
#define MIN_INDENT 22
static int last_was_newline = 0;
int sharp_line = 0;
int indent = 0;
int in_comment = 0;
int in_define = 0;
extern int gflag1;
extern char filename[];
static void ind_printf Argdcl((int, FILE*, const char*, va_list));
static void
#ifdef KR_headers
write_indent(fp, use_indent, extra_indent, start, end)
FILE *fp;
int use_indent;
int extra_indent;
char *start;
char *end;
#else
write_indent(FILE *fp, int use_indent, int extra_indent, char *start, char *end)
#endif
{
int ind, tab;
if (sharp_line) {
fprintf(fp, "#line %ld \"%s\"\n", lineno, filename);
sharp_line = 0;
}
if (in_define == 1) {
in_define = 2;
use_indent = 0;
}
if (last_was_newline && use_indent) {
if (*start == '\n') do {
putc('\n', fp);
if (++start > end)
return;
}
while(*start == '\n');
ind = indent <= MAX_INDENT
? indent
: MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
tab = ind + extra_indent;
while (tab > 7) {
putc ('\t', fp);
tab -= 8;
} /* while */
while (tab-- > 0)
putc (' ', fp);
} /* if last_was_newline */
while (start <= end)
putc (*start++, fp);
} /* write_indent */
#ifdef KR_headers
/*VARARGS2*/
void
margin_printf (fp, a, b, c, d, e, f, g)
FILE *fp;
char *a;
long b, c, d, e, f, g;
{
ind_printf (0, fp, a, b, c, d, e, f, g);
} /* margin_printf */
/*VARARGS2*/
void
nice_printf (fp, a, b, c, d, e, f, g)
FILE *fp;
char *a;
long b, c, d, e, f, g;
{
ind_printf (1, fp, a, b, c, d, e, f, g);
} /* nice_printf */
#define SPRINTF(x,a,b,c,d,e,f,g) sprintf(x,a,b,c,d,e,f,g)
#else /* if (!defined(KR_HEADERS)) */
#define SPRINTF(x,a,b,c,d,e,f,g) vsprintf(x,a,ap)
void
margin_printf(FILE *fp, const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
ind_printf(0, fp, fmt, ap);
va_end(ap);
}
void
nice_printf(FILE *fp, const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
ind_printf(1, fp, fmt, ap);
va_end(ap);
}
#endif
#define max_line_len c_output_line_length
/* 74Number of characters allowed on an output
line. This assumes newlines are handled
nicely, i.e. a newline after a full text
line on a terminal is ignored */
/* output_buf holds the text of the next line to be printed. It gets
flushed when a newline is printed. next_slot points to the next
available location in the output buffer, i.e. where the next call to
nice_printf will have its output stored */
static char *output_buf;
static char *next_slot;
static char *string_start;
static char *word_start = NULL;
static int cursor_pos = 0;
static int In_string = 0;
void
np_init(Void)
{
next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
memset(output_buf, 0, MAX_OUTPUT_SIZE);
}
static char *
#ifdef KR_headers
adjust_pointer_in_string(pointer)
register char *pointer;
#else
adjust_pointer_in_string(register char *pointer)
#endif
{
register char *s, *s1, *se, *s0;
/* arrange not to break \002 */
s1 = string_start ? string_start : output_buf;
for(s = s1; s < pointer; s++) {
s0 = s1;
s1 = s;
if (*s == '\\') {
se = s++ + 4;
if (se > pointer)
break;
if (*s < '0' || *s > '7')
continue;
while(++s < se)
if (*s < '0' || *s > '7')
break;
--s;
}
}
return s0 - 1;
}
/* ANSI says strcpy's behavior is undefined for overlapping args,
* so we roll our own fwd_strcpy: */
static void
#ifdef KR_headers
fwd_strcpy(t, s)
register char *t;
register char *s;
#else
fwd_strcpy(register char *t, register char *s)
#endif
{ while(*t++ = *s++); }
/* isident -- true iff character could belong to a unit. C allows
letters, numbers and underscores in identifiers. This also doubles as
a check for numeric constants, since we include the decimal point and
minus sign. The minus has to be here, since the constant "10e-2"
cannot be broken up. The '.' also prevents structure references from
being broken, which is a quite acceptable side effect */
#define isident(x) (Tr[x] & 1)
#define isntident(x) (!Tr[x])
static void
#ifdef KR_headers
ind_printf (use_indent, fp, a, b, c, d, e, f, g)
int use_indent;
FILE *fp;
char *a;
long b, c, d, e, f, g;
#else
ind_printf (int use_indent, FILE *fp, const char *a, va_list ap)
#endif
{
extern int max_line_len;
extern FILEP c_file;
extern char tr_tab[]; /* in output.c */
register char *Tr = tr_tab;
int ch, cmax, inc, ind;
static int extra_indent, last_indent, set_cursor = 1;
cursor_pos += indent - last_indent;
last_indent = indent;
SPRINTF (next_slot, a, b, c, d, e, f, g);
if (fp != c_file) {
fprintf (fp,"%s", next_slot);
return;
} /* if fp != c_file */
do {
char *pointer;
/* The for loop will parse one output line */
if (set_cursor) {
ind = indent <= MAX_INDENT
? indent
: MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
cursor_pos = extra_indent;
if (use_indent)
cursor_pos += ind;
set_cursor = 0;
}
if (in_comment) {
cmax = max_line_len + 32; /* let comments be wider */
for (pointer = next_slot; *pointer && *pointer != '\n' &&
cursor_pos <= cmax; pointer++)
cursor_pos++;
}
else
for (pointer = next_slot; *pointer && *pointer != '\n' &&
cursor_pos <= max_line_len; pointer++) {
/* Update state variables here */
if (In_string) {
switch(*pointer) {
case '\\':
if (++cursor_pos > max_line_len) {
cursor_pos -= 2;
--pointer;
goto overflow;
}
++pointer;
break;
case '"':
In_string = 0;
word_start = 0;
}
}
else switch (*pointer) {
case '"':
if (cursor_pos + 5 > max_line_len) {
word_start = 0;
--pointer;
goto overflow;
}
In_string = 1;
string_start = word_start = pointer;
break;
case '\'':
if (pointer[1] == '\\')
if ((ch = pointer[2]) >= '0' && ch <= '7')
for(inc = 3; pointer[inc] != '\''
&& ++inc < 5;);
else
inc = 3;
else
inc = 2;
/*debug*/ if (pointer[inc] != '\'')
/*debug*/ fatalstr("Bad character constant %.10s",
pointer);
if ((cursor_pos += inc) > max_line_len) {
cursor_pos -= inc;
word_start = 0;
--pointer;
goto overflow;
}
word_start = pointer;
pointer += inc;
break;
case '\t':
cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
break;
default: {
/* HACK Assumes that all characters in an atomic C token will be written
at the same time. Must check for tokens first, since '-' is considered
part of an identifier; checking isident first would mean breaking up "->" */
if (word_start) {
if (isntident(*(unsigned char *)pointer))
word_start = NULL;
}
else if (isident(*(unsigned char *)pointer))
word_start = pointer;
break;
} /* default */
} /* switch */
cursor_pos++;
} /* for pointer = next_slot */
overflow:
if (*pointer == '\0') {
/* The output line is not complete, so break out and don't output
anything. The current line fragment will be stored in the buffer */
next_slot = pointer;
break;
} else {
char last_char;
int in_string0 = In_string;
/* If the line was too long, move pointer back to the character before
the current word. This allows line breaking on word boundaries. Make
sure that 80 character comment lines get broken up somehow. We assume
that any non-string 80 character identifier must be in a comment.
*/
if (*pointer == '\n')
in_define = 0;
else if (word_start && word_start > output_buf)
if (In_string)
if (string_start && pointer - string_start < 5)
pointer = string_start - 1;
else {
pointer = adjust_pointer_in_string(pointer);
string_start = 0;
}
else if (word_start == string_start
&& pointer - string_start >= 5) {
pointer = adjust_pointer_in_string(next_slot);
In_string = 1;
string_start = 0;
}
else
pointer = word_start - 1;
else if (cursor_pos > max_line_len) {
#ifndef ANSI_Libraries
extern char *strchr();
#endif
if (In_string) {
pointer = adjust_pointer_in_string(pointer);
if (string_start && pointer > string_start)
string_start = 0;
}
else if (strchr("&*+-/<=>|", *pointer)
&& strchr("!%&*+-/<=>^|", pointer[-1])) {
pointer -= 2;
if (strchr("<>", *pointer)) /* <<=, >>= */
pointer--;
}
else {
if (word_start)
while(isident(*(unsigned char *)pointer))
pointer++;
pointer--;
}
}
last_char = *pointer;
write_indent(fp, use_indent, extra_indent, output_buf, pointer);
next_slot = output_buf;
if (In_string && !string_start && Ansi == 1 && last_char != '\n')
*next_slot++ = '"';
fwd_strcpy(next_slot, pointer + 1);
/* insert a line break */
if (last_char == '\n') {
if (In_string)
last_was_newline = 0;
else {
last_was_newline = 1;
extra_indent = 0;
sharp_line = gflag1;
}
}
else {
extra_indent = TOO_LONG_INDENT;
if (In_string && !string_start) {
if (Ansi == 1) {
fprintf(fp, gflag1 ? "\"\\\n" : "\"\n");
use_indent = 1;
last_was_newline = 1;
}
else {
fprintf(fp, "\\\n");
last_was_newline = 0;
}
In_string = in_string0;
}
else {
if (in_define/* | gflag1*/)
putc('\\', fp);
putc ('\n', fp);
last_was_newline = 1;
}
} /* if *pointer != '\n' */
if (In_string && Ansi != 1 && !string_start)
cursor_pos = 0;
else
set_cursor = 1;
string_start = word_start = NULL;
} /* else */
} while (*next_slot);
} /* ind_printf */
|