2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
7 * Redistribution and use in source and binary forms are permitted
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
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.
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.
33 { /* dump_line is the routine
34 that actually effects the
35 printing of the new source.
36 It prints the label section,
37 followed by the code section
38 with the appropriate nesting
39 level, followed by any
41 register int cur_col
, target_col
;
42 static not_first_line
;
51 fprintf(output
, ".*/\n");
53 fprintf(output
, ".Pr \"%s\"\n", ps
.procname
);
58 if (s_code
== e_code
&& s_lab
== e_lab
&& s_com
== e_com
)
60 if (suppress_blanklines
> 0)
61 suppress_blanklines
--;
67 } else if (!inhibit_formatting
)
69 suppress_blanklines
= 0;
71 if (prefix_blankline_requested
&& not_first_line
)
74 if (n_real_blanklines
== 1)
75 n_real_blanklines
= 0;
78 if (n_real_blanklines
== 0)
79 n_real_blanklines
= 1;
81 while (--n_real_blanklines
>= 0)
83 n_real_blanklines
= 0;
84 if (ps
.ind_level
== 0)
85 ps
.ind_stmt
= 0; /* this is a class A kludge.
86 dont do additional statement
87 indentation if we are at
90 if (e_lab
!= s_lab
|| e_code
!= s_code
)
91 ++code_lines
; /* keep count of lines with
96 { /* print lab, if any */
100 fprintf(output
, ".*/\n");
102 while (e_lab
> s_lab
&& (e_lab
[-1] == ' ' || e_lab
[-1] == '\t'))
104 cur_col
= pad_output(1, label_target());
105 fprintf(output
, "%.*s", (int)(e_lab
- s_lab
), s_lab
);
106 cur_col
= count_spaces(cur_col
, s_lab
);
108 cur_col
= 1; /* there is no label section */
112 if (s_code
!= e_code
)
113 { /* print code section, if any */
119 fprintf(output
, ".*/\n");
121 target_col
= code_target();
125 for (i
= 0; i
< ps
.p_l_follow
; i
++)
126 if (ps
.paren_indents
[i
] >= 0)
127 ps
.paren_indents
[i
] = -(ps
.paren_indents
[i
] + target_col
);
129 cur_col
= pad_output(cur_col
, target_col
);
130 for (p
= s_code
; p
< e_code
; p
++)
131 if (*p
== (char) 0200)
132 fprintf(output
, "%d", target_col
* 7);
135 cur_col
= count_spaces(cur_col
, s_code
);
143 if (e_com
[-1] == '/' && e_com
[-2] == '*')
144 e_com
-= 2, all_here
++;
145 while (e_com
> s_com
&& e_com
[-1] == ' ')
151 if (p
[0] == '/' && p
[1] == '*')
153 else if (p
[0] == '*')
154 p
+= p
[1] == '/' ? 2 : 1;
158 goto inhibit_newline
;
159 if (comment_open
< 2 && ps
.box_com
)
162 fprintf(output
, ".*/\n");
164 if (comment_open
== 0)
166 if ('a' <= *p
&& *p
<= 'z')
168 if (e_com
- p
< 50 && all_here
== 2)
170 register char *follow
= p
;
171 fprintf(output
, "\n.nr C! \\w\1");
172 while (follow
< e_com
)
183 putc(*follow
, output
);
189 fprintf(output
, "\n./* %dp %d %dp\n",
191 (s_code
!= e_code
|| s_lab
!= e_lab
) - ps
.box_com
,
194 comment_open
= 1 + ps
.box_com
;
198 putc(BACKSLASH
, output
);
202 { /* print comment, if any */
203 register target
= ps
.com_col
;
204 register char *com_st
= s_com
;
206 target
+= ps
.comment_delta
;
207 while (*com_st
== '\t')
208 com_st
++, target
+= 8; /* ? */
212 else if (*com_st
== '\t')
213 target
= ((target
- 1) & ~7) + 9, com_st
++;
216 if (cur_col
> target
)
217 { /* if comment cant fit on this
218 line, put it on next line */
223 while (e_com
> com_st
&& isspace(e_com
[-1]))
225 cur_col
= pad_output(cur_col
, target
);
228 if (star_comment_cont
&& (com_st
[1] != '*' || e_com
<= com_st
+ 1))
229 if (com_st
[1] == ' ' && com_st
[0] == ' ' && e_com
> com_st
+ 1)
232 fwrite(" * ", com_st
[0] == '\t' ? 2 : com_st
[0] == '*' ? 1 : 3, 1, output
);
234 fwrite(com_st
, (int)(e_com
- com_st
), 1, output
);
235 ps
.comment_delta
= ps
.n_comment_delta
;
236 cur_col
= count_spaces(cur_col
, com_st
);
237 ++ps
.com_lines
; /* count lines with comments */
240 putc('\014', output
);
245 if (ps
.just_saw_decl
== 1 && bl_aft_decl
)
247 prefix_blankline_requested
= 1;
248 ps
.just_saw_decl
= 0;
250 prefix_blankline_requested
= postfix_blankline_requested
;
251 postfix_blankline_requested
= 0;
253 ps
.decl_on_line
= ps
.in_decl
; /* if we are in the middle of a
254 declaration, remember that
255 fact for proper comment
257 ps
.ind_stmt
= ps
.in_stmt
& ~ps
.in_decl
; /* next line should be
264 ps
.dumped_decl_indent
= 0;
265 *(e_lab
= s_lab
) = '\0'; /* reset buffers */
266 *(e_code
= s_code
) = '\0';
267 *(e_com
= s_com
) = '\0';
268 ps
.ind_level
= ps
.i_l_follow
;
269 ps
.paren_level
= ps
.p_l_follow
;
270 paren_target
= -ps
.paren_indents
[ps
.paren_level
- 1];
277 register target_col
= ps
.ind_size
* ps
.ind_level
+ 1;
280 if (!lineup_to_parens
)
281 target_col
+= continuation_indent
* ps
.paren_level
;
285 register t
= paren_target
;
287 if ((w
= count_spaces(t
, s_code
) - max_col
) > 0
288 && count_spaces(target_col
, s_code
) <= max_col
)
296 else if (ps
.ind_stmt
)
297 target_col
+= continuation_indent
;
304 ps
.pcase
? (int) (case_ind
* ps
.ind_size
) + 1
306 : ps
.ind_size
* (ps
.ind_level
- label_offset
) + 1;
311 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
313 * All rights reserved
318 * FUNCTION: Reads one block of input into input_buffer
320 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
321 * Willcox of CAC Added check for switch back to partly full input
322 * buffer from temporary buffer
327 { /* this routine reads stuff
331 register FILE *f
= input
;
334 { /* there is a partly filled
336 buf_ptr
= bp_save
; /* dont read anything, just
339 bp_save
= be_save
= 0;
340 if (buf_ptr
< buf_end
)
341 return; /* only return if there is
342 really something in this
345 for (p
= buf_ptr
= in_buffer
;;)
347 if ((i
= getc(f
)) == EOF
)
359 if (p
[-2] == '/' && p
[-3] == '*')
361 if (in_buffer
[3] == 'I' && strncmp(in_buffer
, "/**INDENT**", 11) == 0)
362 fill_buffer(); /* flush indent error message */
368 while (*p
== ' ' || *p
== '\t')
370 if (*p
== '/' && p
[1] == '*')
373 while (*p
== ' ' || *p
== '\t')
375 if (p
[0] == 'I' && p
[1] == 'N' && p
[2] == 'D' && p
[3] == 'E'
376 && p
[4] == 'N' && p
[5] == 'T')
379 while (*p
== ' ' || *p
== '\t')
386 else if (*p
== 'F' && *++p
== 'F')
388 while (*p
== ' ' || *p
== '\t')
390 if (p
[0] == '*' && p
[1] == '/' && p
[2] == '\n' && com
)
392 if (s_com
!= e_com
|| s_lab
!= e_lab
|| s_code
!= e_code
)
394 if (!(inhibit_formatting
= com
- 1))
396 n_real_blanklines
= 0;
397 postfix_blankline_requested
= 0;
398 prefix_blankline_requested
= 0;
399 suppress_blanklines
= 1;
406 if (inhibit_formatting
)
411 while (*p
++ != '\n');
417 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
419 * All rights reserved
424 * FUNCTION: Writes tabs and spaces to move the current column up to the desired
427 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
429 * PARAMETERS: current integer The current column target
430 * nteger The desired column
432 * RETURNS: Integer value of the new column. (If current >= target, no action
433 * is taken, and current is returned.
439 * CALLED BY: dump_line
441 * HISTORY: initial coding November 1976 D A Willcox of CAC
444 int pad_output(current
, target
) /* writes tabs and blanks (if
445 necessary) to get the
446 current output position up
447 to the target column */
448 int current
; /* the current column value */
449 int target
; /* position we want it at */
451 register int curr
; /* internal column pointer */
455 fprintf(output
, "\\h'|%dp'", (target
- 1) * 7);
458 if (current
>= target
)
459 return (current
); /* line is already long enough */
461 while ((tcur
= ((curr
- 1) & tabmask
) + tabsize
+ 1) <= target
)
466 while (curr
++ < target
)
467 putc(' ', output
); /* pad with final blanks */
473 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
475 * All rights reserved
480 * FUNCTION: Find out where printing of a given string will leave the current
481 * character position on output.
483 * ALGORITHM: Run thru input string and add appropriate values to current
486 * RETURNS: Integer value of position after printing "buffer" starting in column
489 * HISTORY: initial coding November 1976 D A Willcox of CAC
493 count_spaces(current
, buffer
)
495 * this routine figures out where the character position will be after
496 * printing the text in buffer starting at column "current"
501 register char *buf
; /* used to look thru buffer */
502 register int cur
; /* current character counter */
506 for (buf
= buffer
; *buf
!= '\0'; ++buf
)
512 case 014: /* form feed */
517 cur
= ((cur
- 1) & tabmask
) + tabsize
+ 1;
520 case '\b': /* this is a backspace */
527 } /* end of switch */
528 } /* end of for loop */
533 void diag(level
, msg
, a
, b
)
540 if (output
== stdout
)
542 fprintf(stdout
, "/**INDENT** %s@%d: ", level
== 0 ? "Warning" : "Error", line_no
);
543 fprintf(stdout
, msg
, a
, b
);
544 fprintf(stdout
, " */\n");
547 fprintf(stderr
, "%s@%d: ", level
== 0 ? "Warning" : "Error", line_no
);
548 fprintf(stderr
, msg
, a
, b
);
549 fprintf(stderr
, "\n");
553 void writefdef(f
, nm
)
554 register struct fstate
*f
;
557 fprintf(output
, ".ds f%c %s\n.nr s%c %d\n",
558 nm
, f
->font
, nm
, f
->size
);
563 register struct fstate
*of
, *nf
;
566 if (of
->font
[0] != nf
->font
[0]
567 || of
->font
[1] != nf
->font
[1])
579 if (nf
->size
!= of
->size
)
583 if (nf
->size
< of
->size
)
586 *s
++ = (char)'0' + of
->size
- nf
->size
;
590 *s
++ = (char)'0' + nf
->size
- of
->size
;
597 void parsefont(f
, s0
)
598 register struct fstate
*f
;
601 register char *s
= s0
;
603 memset(f
, '\0', sizeof *f
);
607 f
->size
= f
->size
* 10 + *s
- '0';
608 else if (isupper(*s
))
621 fprintf(stderr
, "indent: bad font specification: %s\n", s0
);
631 f
->size
= bodyf
.size
+ sizedelta
;
632 else if (sizedelta
> 0)
633 f
->size
+= bodyf
.size
;
635 f
->size
= bodyf
.size
- f
->size
;