remove BSD ifdefs
[unix-history] / usr / src / usr.bin / indent / io.c
CommitLineData
c0bc4ef7 1/*
30f48914
KB
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
b0627149
KB
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
30f48914
KB
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
b36fc510
KB
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
c0bc4ef7
DF
19 */
20
21#ifndef lint
0c8ee79d 22static char sccsid[] = "@(#)io.c 5.13 (Berkeley) %G%";
b0627149 23#endif /* not lint */
87ba9e01 24
95891db1 25#include "indent_globs.h"
30f48914 26#include <ctype.h>
87ba9e01
KM
27
28
1009bf5e
KM
29int comment_open;
30static paren_target;
87ba9e01 31
1009bf5e
KM
32dump_line()
33{ /* dump_line is the routine that actually
30f48914
KB
34 * effects the printing of the new source. It
35 * prints the label section, followed by the
36 * code section with the appropriate nesting
37 * level, followed by any comments */
1009bf5e 38 register int cur_col,
1009bf5e 39 target_col;
30f48914 40 static not_first_line;
87ba9e01 41
1009bf5e 42 if (ps.procname[0]) {
30f48914
KB
43 if (troff) {
44 if (comment_open) {
45 comment_open = 0;
46 fprintf(output, ".*/\n");
47 }
1009bf5e 48 fprintf(output, ".Pr \"%s\"\n", ps.procname);
30f48914 49 }
1009bf5e
KM
50 ps.ind_level = 0;
51 ps.procname[0] = 0;
52 }
53 if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
30f48914
KB
54 if (suppress_blanklines > 0)
55 suppress_blanklines--;
87ba9e01 56 else {
30f48914
KB
57 ps.bl_line = true;
58 n_real_blanklines++;
87ba9e01 59 }
87ba9e01 60 }
1009bf5e
KM
61 else if (!inhibit_formatting) {
62 suppress_blanklines = 0;
63 ps.bl_line = false;
30f48914 64 if (prefix_blankline_requested && not_first_line)
1009bf5e
KM
65 if (swallow_optional_blanklines) {
66 if (n_real_blanklines == 1)
67 n_real_blanklines = 0;
68 }
69 else {
70 if (n_real_blanklines == 0)
71 n_real_blanklines = 1;
72 }
73 while (--n_real_blanklines >= 0)
74 putc('\n', output);
75 n_real_blanklines = 0;
76 if (ps.ind_level == 0)
77 ps.ind_stmt = 0; /* this is a class A kludge. dont do
30f48914
KB
78 * additional statement indentation if we are
79 * at bracket level 0 */
1009bf5e
KM
80
81 if (e_lab != s_lab || e_code != s_code)
82 ++code_lines; /* keep count of lines with code */
83
84
85 if (e_lab != s_lab) { /* print lab, if any */
86 if (comment_open) {
87 comment_open = 0;
88 fprintf(output, ".*/\n");
89 }
90 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
91 e_lab--;
92 cur_col = pad_output(1, compute_label_target());
0c8ee79d
KB
93 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
94 || strncmp(s_lab, "#endif", 6) == 0)) {
95 register char *s = s_lab;
96 if (e_lab[-1] == '\n') e_lab--;
97 do putc(*s++, output);
98 while (s < e_lab && 'a' <= *s && *s<='z');
99 while ((*s == ' ' || *s == '\t') && s < e_lab)
100 s++;
101 if (s < e_lab)
102 fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
103 e_lab - s, s);
104 }
105 else fprintf(output, "%.*s", e_lab - s_lab, s_lab);
1009bf5e 106 cur_col = count_spaces(cur_col, s_lab);
87ba9e01 107 }
1009bf5e
KM
108 else
109 cur_col = 1; /* there is no label section */
110
111 ps.pcase = false;
112
113 if (s_code != e_code) { /* print code section, if any */
114 register char *p;
115
116 if (comment_open) {
117 comment_open = 0;
118 fprintf(output, ".*/\n");
119 }
120 target_col = compute_code_target();
121 {
122 register i;
123
124 for (i = 0; i < ps.p_l_follow; i++)
125 if (ps.paren_indents[i] >= 0)
126 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
127 }
128 cur_col = pad_output(cur_col, target_col);
129 for (p = s_code; p < e_code; p++)
30f48914 130 if (*p == (char) 0200)
1009bf5e
KM
131 fprintf(output, "%d", target_col * 7);
132 else
133 putc(*p, output);
134 cur_col = count_spaces(cur_col, s_code);
135 }
136 if (s_com != e_com)
137 if (troff) {
30f48914 138 int all_here = 0;
1009bf5e
KM
139 register char *p;
140
141 if (e_com[-1] == '/' && e_com[-2] == '*')
30f48914 142 e_com -= 2, all_here++;
1009bf5e
KM
143 while (e_com > s_com && e_com[-1] == ' ')
144 e_com--;
145 *e_com = 0;
146 p = s_com;
147 while (*p == ' ')
148 p++;
149 if (p[0] == '/' && p[1] == '*')
30f48914 150 p += 2, all_here++;
1009bf5e
KM
151 else if (p[0] == '*')
152 p += p[1] == '/' ? 2 : 1;
153 while (*p == ' ')
154 p++;
155 if (*p == 0)
156 goto inhibit_newline;
30f48914
KB
157 if (comment_open < 2 && ps.box_com) {
158 comment_open = 0;
159 fprintf(output, ".*/\n");
160 }
161 if (comment_open == 0) {
1009bf5e
KM
162 if ('a' <= *p && *p <= 'z')
163 *p = *p + 'A' - 'a';
30f48914
KB
164 if (e_com - p < 50 && all_here == 2) {
165 register char *follow = p;
166 fprintf(output, "\n.nr C! \\w\1");
167 while (follow < e_com) {
168 switch (*follow) {
169 case '\n':
170 putc(' ', output);
171 case 1:
172 break;
173 case '\\':
174 putc('\\', output);
175 default:
176 putc(*follow, output);
177 }
178 follow++;
179 }
180 putc(1, output);
1009bf5e 181 }
30f48914
KB
182 fprintf(output, "\n./* %dp %d %dp\n",
183 ps.com_col * 7,
184 (s_code != e_code || s_lab != e_lab) - ps.box_com,
185 target_col * 7);
1009bf5e 186 }
30f48914 187 comment_open = 1 + ps.box_com;
1009bf5e
KM
188 while (*p) {
189 if (*p == BACKSLASH)
190 putc(BACKSLASH, output);
191 putc(*p++, output);
192 }
193 }
194 else { /* print comment, if any */
195 register target = ps.com_col;
196 register char *com_st = s_com;
197
198 target += ps.comment_delta;
30f48914
KB
199 while (*com_st == '\t')
200 com_st++, target += 8; /* ? */
1009bf5e 201 while (target <= 0)
30f48914
KB
202 if (*com_st == ' ')
203 target++, com_st++;
204 else if (*com_st == '\t')
205 target = ((target - 1) & ~7) + 9, com_st++;
1009bf5e
KM
206 else
207 target = 1;
30f48914
KB
208 if (cur_col > target) { /* if comment cant fit on this line,
209 * put it on next line */
1009bf5e
KM
210 putc('\n', output);
211 cur_col = 1;
212 ++ps.out_lines;
213 }
30f48914
KB
214 while (e_com > com_st && isspace(e_com[-1]))
215 e_com--;
1009bf5e
KM
216 cur_col = pad_output(cur_col, target);
217 if (!ps.box_com) {
30f48914
KB
218 if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1))
219 if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
1009bf5e
KM
220 com_st[1] = '*';
221 else
222 fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
223 }
224 fwrite(com_st, e_com - com_st, 1, output);
225 ps.comment_delta = ps.n_comment_delta;
226 cur_col = count_spaces(cur_col, com_st);
227 ++ps.com_lines; /* count lines with comments */
228 }
229 if (ps.use_ff)
230 putc('\014', output);
231 else
232 putc('\n', output);
233inhibit_newline:
234 ++ps.out_lines;
235 if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
236 prefix_blankline_requested = 1;
237 ps.just_saw_decl = 0;
238 }
239 else
240 prefix_blankline_requested = postfix_blankline_requested;
241 postfix_blankline_requested = 0;
87ba9e01 242 }
1009bf5e 243 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
30f48914
KB
244 * declaration, remember that fact for
245 * proper comment indentation */
1009bf5e
KM
246 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
247 * indented if we have not
30f48914
KB
248 * completed this stmt and if
249 * we are not in the middle of
250 * a declaration */
1009bf5e
KM
251 ps.use_ff = false;
252 ps.dumped_decl_indent = 0;
253 *(e_lab = s_lab) = '\0'; /* reset buffers */
87ba9e01
KM
254 *(e_code = s_code) = '\0';
255 *(e_com = s_com) = '\0';
1009bf5e
KM
256 ps.ind_level = ps.i_l_follow;
257 ps.paren_level = ps.p_l_follow;
258 paren_target = -ps.paren_indents[ps.paren_level - 1];
30f48914 259 not_first_line = 1;
87ba9e01 260 return;
1a43f1ba 261}
87ba9e01 262
30f48914
KB
263compute_code_target()
264{
1009bf5e 265 register target_col = ps.ind_size * ps.ind_level + 1;
87ba9e01 266
1009bf5e
KM
267 if (ps.paren_level)
268 if (!lineup_to_parens)
269 target_col += continuation_indent * ps.paren_level;
270 else {
271 register w;
272 register t = paren_target;
273
274 if ((w = count_spaces(t, s_code) - max_col) > 0
30f48914 275 && count_spaces(target_col, s_code) <= max_col) {
1009bf5e
KM
276 t -= w + 1;
277 if (t > target_col)
278 target_col = t;
279 }
280 else
281 target_col = t;
282 }
283 else if (ps.ind_stmt)
284 target_col += continuation_indent;
285 return target_col;
286}
87ba9e01 287
1009bf5e
KM
288compute_label_target()
289{
290 return
30f48914 291 ps.pcase ? (int) (case_ind * ps.ind_size) + 1
1009bf5e 292 : *s_lab == '#' ? 1
30f48914 293 : ps.ind_size * (ps.ind_level - label_offset) + 1;
1009bf5e 294}
87ba9e01 295
87ba9e01 296
1009bf5e 297/*
30f48914
KB
298 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
299 *
300 * All rights reserved
301 *
302 *
303 * NAME: fill_buffer
304 *
305 * FUNCTION: Reads one block of input into input_buffer
306 *
307 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
308 * Willcox of CAC Added check for switch back to partly full input
309 * buffer from temporary buffer
310 *
0c8ee79d 311 */
1009bf5e
KM
312int
313fill_buffer()
314{ /* this routine reads stuff from the input */
1009bf5e
KM
315 register char *p;
316 register int i;
317 register FILE *f = input;
318
30f48914 319 if (bp_save != 0) { /* there is a partly filled input buffer left */
1009bf5e 320 buf_ptr = bp_save; /* dont read anything, just switch buffers */
87ba9e01
KM
321 buf_end = be_save;
322 bp_save = be_save = 0;
323 if (buf_ptr < buf_end)
30f48914
KB
324 return; /* only return if there is really something in
325 * this buffer */
87ba9e01 326 }
0c8ee79d
KB
327 for (p = in_buffer;;) {
328 if (p >= in_buffer_limit) {
329 register size = (in_buffer_limit - in_buffer) * 2 + 10;
330 register offset = p - in_buffer;
331 in_buffer = (char *) realloc(in_buffer, size);
332 if (in_buffer == 0)
333 err("input line too long");
334 p = in_buffer + offset;
335 in_buffer_limit = in_buffer + size - 2;
336 }
86591296
KB
337 if ((i = getc(f)) == EOF) {
338 *p++ = ' ';
339 *p++ = '\n';
340 had_eof = true;
341 break;
342 }
343 *p++ = i;
344 if (i == '\n')
345 break;
87ba9e01 346 }
0c8ee79d 347 buf_ptr = in_buffer;
1009bf5e 348 buf_end = p;
1a43f1ba 349 if (p[-2] == '/' && p[-3] == '*') {
1009bf5e
KM
350 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
351 fill_buffer(); /* flush indent error message */
352 else {
353 int com = 0;
354
355 p = in_buffer;
356 while (*p == ' ' || *p == '\t')
357 p++;
358 if (*p == '/' && p[1] == '*') {
359 p += 2;
360 while (*p == ' ' || *p == '\t')
361 p++;
362 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
30f48914 363 && p[4] == 'N' && p[5] == 'T') {
1009bf5e
KM
364 p += 6;
365 while (*p == ' ' || *p == '\t')
366 p++;
367 if (*p == '*')
368 com = 1;
369 else if (*p == 'O')
370 if (*++p == 'N')
371 p++, com = 1;
372 else if (*p == 'F' && *++p == 'F')
373 p++, com = 2;
374 while (*p == ' ' || *p == '\t')
375 p++;
376 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
377 if (s_com != e_com || s_lab != e_lab || s_code != e_code)
378 dump_line();
379 if (!(inhibit_formatting = com - 1)) {
380 n_real_blanklines = 0;
381 postfix_blankline_requested = 0;
382 prefix_blankline_requested = 0;
383 suppress_blanklines = 1;
384 }
385 }
386 }
387 }
388 }
389 }
390 if (inhibit_formatting) {
391 p = in_buffer;
392 do
393 putc(*p, output);
394 while (*p++ != '\n');
395 }
87ba9e01 396 return;
1a43f1ba 397}
87ba9e01 398
1009bf5e 399/*
30f48914
KB
400 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
401 *
402 * All rights reserved
403 *
404 *
405 * NAME: pad_output
406 *
407 * FUNCTION: Writes tabs and spaces to move the current column up to the desired
408 * position.
409 *
410 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
411 *
1009bf5e 412 * PARAMETERS: current integer The current column target
30f48914
KB
413 * nteger The desired column
414 *
415 * RETURNS: Integer value of the new column. (If current >= target, no action is
416 * taken, and current is returned.
417 *
418 * GLOBALS: None
419 *
420 * CALLS: write (sys)
421 *
422 * CALLED BY: dump_line
423 *
424 * HISTORY: initial coding November 1976 D A Willcox of CAC
425 *
0c8ee79d 426 */
30f48914
KB
427pad_output(current, target) /* writes tabs and blanks (if necessary) to
428 * get the current output position up to the
429 * target column */
1009bf5e
KM
430 int current; /* the current column value */
431 int target; /* position we want it at */
87ba9e01 432{
1009bf5e 433 register int curr; /* internal column pointer */
87ba9e01
KM
434 register int tcur;
435
1009bf5e
KM
436 if (troff)
437 fprintf(output, "\\h'|%dp'", (target - 1) * 7);
438 else {
439 if (current >= target)
440 return (current); /* line is already long enough */
441 curr = current;
442 while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
443 putc('\t', output);
87ba9e01
KM
444 curr = tcur;
445 }
1009bf5e
KM
446 while (curr++ < target)
447 putc(' ', output); /* pad with final blanks */
87ba9e01 448 }
87ba9e01 449 return (target);
1a43f1ba 450}
87ba9e01 451
1009bf5e 452/*
30f48914
KB
453 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
454 *
455 * All rights reserved
456 *
457 *
458 * NAME: count_spaces
459 *
1009bf5e 460 * FUNCTION: Find out where printing of a given string will leave the current
30f48914
KB
461 * character position on output.
462 *
1009bf5e 463 * ALGORITHM: Run thru input string and add appropriate values to current
30f48914
KB
464 * position.
465 *
466 * RETURNS: Integer value of position after printing "buffer" starting in column
467 * "current".
468 *
469 * HISTORY: initial coding November 1976 D A Willcox of CAC
470 *
0c8ee79d 471 */
1009bf5e
KM
472int
473count_spaces(current, buffer)
1009bf5e
KM
474/*
475 * this routine figures out where the character position will be after
30f48914 476 * printing the text in buffer starting at column "current"
1009bf5e
KM
477 */
478 int current;
479 char *buffer;
87ba9e01 480{
1009bf5e
KM
481 register char *buf; /* used to look thru buffer */
482 register int cur; /* current character counter */
87ba9e01
KM
483
484 cur = current;
485
486 for (buf = buffer; *buf != '\0'; ++buf) {
487 switch (*buf) {
488
30f48914
KB
489 case '\n':
490 case 014: /* form feed */
491 cur = 1;
492 break;
87ba9e01 493
30f48914
KB
494 case '\t':
495 cur = ((cur - 1) & tabmask) + tabsize + 1;
496 break;
87ba9e01 497
1a43f1ba 498 case 010: /* backspace */
30f48914
KB
499 --cur;
500 break;
87ba9e01 501
30f48914
KB
502 default:
503 ++cur;
504 break;
1009bf5e
KM
505 } /* end of switch */
506 } /* end of for loop */
87ba9e01 507 return (cur);
1a43f1ba 508}
87ba9e01 509
95891db1 510int found_err;
1a43f1ba 511/* VARARGS2 */
1009bf5e 512diag(level, msg, a, b)
87ba9e01 513{
95891db1
KB
514 if (level)
515 found_err = 1;
1009bf5e
KM
516 if (output == stdout) {
517 fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
518 fprintf(stdout, msg, a, b);
519 fprintf(stdout, " */\n");
87ba9e01 520 }
1009bf5e
KM
521 else {
522 fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
523 fprintf(stderr, msg, a, b);
524 fprintf(stderr, "\n");
87ba9e01 525 }
87ba9e01 526}
30f48914
KB
527
528writefdef(f, nm)
529 register struct fstate *f;
530{
531 fprintf(output, ".ds f%c %s\n.nr s%c %d\n",
532 nm, f->font, nm, f->size);
533}
534
535char *
536chfont(of, nf, s)
537 register struct fstate *of,
538 *nf;
539 char *s;
540{
541 if (of->font[0] != nf->font[0]
542 || of->font[1] != nf->font[1]) {
543 *s++ = '\\';
544 *s++ = 'f';
545 if (nf->font[1]) {
546 *s++ = '(';
547 *s++ = nf->font[0];
548 *s++ = nf->font[1];
549 }
550 else
551 *s++ = nf->font[0];
552 }
553 if (nf->size != of->size) {
554 *s++ = '\\';
555 *s++ = 's';
556 if (nf->size < of->size) {
557 *s++ = '-';
558 *s++ = '0' + of->size - nf->size;
559 }
560 else {
561 *s++ = '+';
562 *s++ = '0' + nf->size - of->size;
563 }
564 }
565 return s;
566}
567
568
30f48914
KB
569parsefont(f, s0)
570 register struct fstate *f;
571 char *s0;
572{
573 register char *s = s0;
574 int sizedelta = 0;
575 bzero(f, sizeof *f);
576 while (*s) {
577 if (isdigit(*s))
578 f->size = f->size * 10 + *s - '0';
579 else if (isupper(*s))
580 if (f->font[0])
581 f->font[1] = *s;
582 else
583 f->font[0] = *s;
584 else if (*s == 'c')
585 f->allcaps = 1;
586 else if (*s == '+')
587 sizedelta++;
588 else if (*s == '-')
589 sizedelta--;
590 else {
591 fprintf(stderr, "indent: bad font specification: %s\n", s0);
592 exit(1);
593 }
594 s++;
595 }
596 if (f->font[0] == 0)
597 f->font[0] = 'R';
598 if (bodyf.size == 0)
599 bodyf.size = 11;
600 if (f->size == 0)
601 f->size = bodyf.size + sizedelta;
602 else if (sizedelta > 0)
603 f->size += bodyf.size;
604 else
605 f->size = bodyf.size - f->size;
606}