1 /* $NetBSD: io.c,v 1.14 2009/04/12 11:09:49 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
34 * Copyright (c) 1985 Sun Microsystems, Inc.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/cdefs.h>
69 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
71 __RCSID("$NetBSD: io.c,v 1.14 2009/04/12 11:09:49 lukem Exp $");
81 #include "indent_globs.h"
84 static int paren_target
;
88 { /* dump_line is the routine that actually
89 * effects the printing of the new source. It
90 * prints the label section, followed by the
91 * code section with the appropriate nesting
92 * level, followed by any comments */
93 int cur_col
, target_col
;
94 static int not_first_line
;
101 fprintf(output
, ".*/\n");
103 fprintf(output
, ".Pr \"%s\"\n", ps
.procname
);
108 if (s_code
== e_code
&& s_lab
== e_lab
&& s_com
== e_com
) {
109 if (suppress_blanklines
> 0)
110 suppress_blanklines
--;
116 if (!inhibit_formatting
) {
117 suppress_blanklines
= 0;
119 if (prefix_blankline_requested
&& not_first_line
) {
120 if (swallow_optional_blanklines
) {
121 if (n_real_blanklines
== 1)
122 n_real_blanklines
= 0;
124 if (n_real_blanklines
== 0)
125 n_real_blanklines
= 1;
128 while (--n_real_blanklines
>= 0)
130 n_real_blanklines
= 0;
131 if (ps
.ind_level
== 0)
132 ps
.ind_stmt
= 0; /* this is a class A
134 * additional statement
135 * indentation if we are
136 * at bracket level 0 */
138 if (e_lab
!= s_lab
|| e_code
!= s_code
)
139 ++code_lines
; /* keep count of lines with
143 if (e_lab
!= s_lab
) { /* print lab, if any */
146 fprintf(output
, ".*/\n");
148 while (e_lab
> s_lab
&& (e_lab
[-1] == ' ' || e_lab
[-1] == '\t'))
150 cur_col
= pad_output(1, compute_label_target());
151 if (s_lab
[0] == '#' && (strncmp(s_lab
, "#else", 5) == 0
152 || strncmp(s_lab
, "#endif", 6) == 0)) {
154 if (e_lab
[-1] == '\n')
158 while (s
< e_lab
&& 'a' <= *s
&& *s
<= 'z');
159 while ((*s
== ' ' || *s
== '\t') && s
< e_lab
)
162 fprintf(output
, s
[0] == '/' && s
[1] == '*' ? "\t%.*s" : "\t/* %.*s */",
163 (int)(e_lab
- s
), s
);
165 fprintf(output
, "%.*s", (int)(e_lab
- s_lab
), s_lab
);
166 cur_col
= count_spaces(cur_col
, s_lab
);
168 cur_col
= 1; /* there is no label section */
172 if (s_code
!= e_code
) { /* print code section, if any */
177 fprintf(output
, ".*/\n");
179 target_col
= compute_code_target();
183 for (i
= 0; i
< ps
.p_l_follow
; i
++)
184 if (ps
.paren_indents
[i
] >= 0)
185 ps
.paren_indents
[i
] = -(ps
.paren_indents
[i
] + target_col
);
187 cur_col
= pad_output(cur_col
, target_col
);
188 for (p
= s_code
; p
< e_code
; p
++)
189 if (*p
== (char) 0200)
190 fprintf(output
, "%d", target_col
* 7);
193 cur_col
= count_spaces(cur_col
, s_code
);
195 if (s_com
!= e_com
) {
200 if (e_com
[-1] == '/' && e_com
[-2] == '*')
201 e_com
-= 2, all_here
++;
202 while (e_com
> s_com
&& e_com
[-1] == ' ')
208 if (p
[0] == '/' && p
[1] == '*')
212 p
+= p
[1] == '/' ? 2 : 1;
216 goto inhibit_newline
;
217 if (comment_open
< 2 && ps
.box_com
) {
219 fprintf(output
, ".*/\n");
221 if (comment_open
== 0) {
222 if ('a' <= *p
&& *p
<= 'z')
224 if (e_com
- p
< 50 && all_here
== 2) {
226 fprintf(output
, "\n.nr C! \\w\1");
227 while (follow
< e_com
) {
236 putc(*follow
, output
);
242 fprintf(output
, "\n./* %dp %d %dp\n",
244 (s_code
!= e_code
|| s_lab
!= e_lab
) - ps
.box_com
,
247 comment_open
= 1 + ps
.box_com
;
250 putc(BACKSLASH
, output
);
253 } else { /* print comment, if any */
254 int target
= ps
.com_col
;
255 char *com_st
= s_com
;
257 target
+= ps
.comment_delta
;
258 while (*com_st
== '\t')
259 com_st
++, target
+= 8; /* ? */
265 target
= ((target
- 1) & ~7) + 9, com_st
++;
268 if (cur_col
> target
) { /* if comment cant fit
269 * on this line, put it
275 while (e_com
> com_st
276 && isspace((unsigned char)e_com
[-1]))
278 cur_col
= pad_output(cur_col
, target
);
280 if (star_comment_cont
282 || e_com
<= com_st
+ 1)) {
285 && e_com
> com_st
+ 1)
297 e_com
- com_st
, 1, output
);
298 ps
.comment_delta
= ps
.n_comment_delta
;
299 cur_col
= count_spaces(cur_col
, com_st
);
300 ++ps
.com_lines
; /* count lines with
305 putc('\014', output
);
310 if (ps
.just_saw_decl
== 1 && blanklines_after_declarations
) {
311 prefix_blankline_requested
= 1;
312 ps
.just_saw_decl
= 0;
314 prefix_blankline_requested
= postfix_blankline_requested
;
315 postfix_blankline_requested
= 0;
317 ps
.decl_on_line
= ps
.in_decl
; /* if we are in the middle of a
318 * declaration, remember that fact for
319 * proper comment indentation */
320 ps
.ind_stmt
= ps
.in_stmt
& ~ps
.in_decl
; /* next line should be
321 * indented if we have not
322 * completed this stmt and if
323 * we are not in the middle of
326 ps
.dumped_decl_indent
= 0;
327 *(e_lab
= s_lab
) = '\0';/* reset buffers */
328 *(e_code
= s_code
) = '\0';
329 *(e_com
= s_com
) = '\0';
330 ps
.ind_level
= ps
.i_l_follow
;
331 ps
.paren_level
= ps
.p_l_follow
;
332 paren_target
= -ps
.paren_indents
[ps
.paren_level
- 1];
337 compute_code_target(void)
339 int target_col
= ps
.ind_size
* ps
.ind_level
+ 1;
341 if (ps
.paren_level
) {
342 if (!lineup_to_parens
)
343 target_col
+= continuation_indent
* ps
.paren_level
;
346 int t
= paren_target
;
348 if ((w
= count_spaces(t
, s_code
) - max_col
) > 0
349 && count_spaces(target_col
, s_code
) <= max_col
) {
358 target_col
+= continuation_indent
;
363 compute_label_target(void)
366 ps
.pcase
? (int) (case_ind
* ps
.ind_size
) + 1
368 : ps
.ind_size
* (ps
.ind_level
- label_offset
) + 1;
373 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
375 * All rights reserved
380 * FUNCTION: Reads one block of input into input_buffer
382 * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A
383 * Willcox of CAC Added check for switch back to partly full input
384 * buffer from temporary buffer
389 { /* this routine reads stuff from the input */
395 if (bp_save
!= 0) { /* there is a partly filled input buffer left */
396 buf_ptr
= bp_save
; /* dont read anything, just switch
399 bp_save
= be_save
= 0;
400 if (buf_ptr
< buf_end
)
401 return; /* only return if there is really something in
404 for (p
= in_buffer
;;) {
405 if (p
>= in_buffer_limit
) {
406 int size
= (in_buffer_limit
- in_buffer
) * 2 + 10;
407 int offset
= p
- in_buffer
;
408 n
= (char *) realloc(in_buffer
, size
);
410 errx(1, "input line too long");
412 p
= in_buffer
+ offset
;
413 in_buffer_limit
= in_buffer
+ size
- 2;
415 if ((i
= getc(f
)) == EOF
) {
427 if (p
[-2] == '/' && p
[-3] == '*') {
428 if (in_buffer
[3] == 'I' && strncmp(in_buffer
, "/**INDENT**", 11) == 0)
429 fill_buffer(); /* flush indent error message */
434 while (*p
== ' ' || *p
== '\t')
436 if (*p
== '/' && p
[1] == '*') {
438 while (*p
== ' ' || *p
== '\t')
440 if (p
[0] == 'I' && p
[1] == 'N' && p
[2] == 'D' && p
[3] == 'E'
441 && p
[4] == 'N' && p
[5] == 'T') {
443 while (*p
== ' ' || *p
== '\t')
452 if (*p
== 'F' && *++p
== 'F')
456 while (*p
== ' ' || *p
== '\t')
458 if (p
[0] == '*' && p
[1] == '/' && p
[2] == '\n' && com
) {
459 if (s_com
!= e_com
|| s_lab
!= e_lab
|| s_code
!= e_code
)
461 if (!(inhibit_formatting
= com
- 1)) {
462 n_real_blanklines
= 0;
463 postfix_blankline_requested
= 0;
464 prefix_blankline_requested
= 0;
465 suppress_blanklines
= 1;
472 if (inhibit_formatting
) {
476 while (*p
++ != '\n');
480 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
482 * All rights reserved
487 * FUNCTION: Writes tabs and spaces to move the current column up to the desired
490 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
492 * PARAMETERS: current integer The current column target
493 * target integer The desired column
495 * RETURNS: Integer value of the new column. (If current >= target, no action is
496 * taken, and current is returned.
502 * CALLED BY: dump_line
504 * HISTORY: initial coding November 1976 D A Willcox of CAC
508 pad_output(int current
, int target
)
510 int curr
; /* internal column pointer */
514 fprintf(output
, "\\h'|%dp'", (target
- 1) * 7);
516 if (current
>= target
)
517 return (current
); /* line is already long enough */
519 while ((tcur
= ((curr
- 1) & tabmask
) + tabsize
+ 1) <= target
) {
523 while (curr
++ < target
)
524 putc(' ', output
); /* pad with final blanks */
529 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
531 * All rights reserved
536 * FUNCTION: Find out where printing of a given string will leave the current
537 * character position on output.
539 * ALGORITHM: Run thru input string and add appropriate values to current
542 * RETURNS: Integer value of position after printing "buffer" starting in column
545 * HISTORY: initial coding November 1976 D A Willcox of CAC
549 count_spaces(int current
, char *buffer
)
551 * this routine figures out where the character position will be after
552 * printing the text in buffer starting at column "current"
555 char *buf
; /* used to look thru buffer */
556 int cur
; /* current character counter */
560 for (buf
= buffer
; *buf
!= '\0'; ++buf
) {
564 case 014: /* form feed */
569 cur
= ((cur
- 1) & tabmask
) + tabsize
+ 1;
572 case 010: /* backspace */
579 } /* end of switch */
580 } /* end of for loop */
588 diag(int level
, const char *msg
, ...)
596 if (output
== stdout
) {
597 fprintf(stdout
, "/**INDENT** %s@%d: ", level
== 0 ? "Warning" : "Error", line_no
);
598 vfprintf(stdout
, msg
, ap
);
599 fprintf(stdout
, " */\n");
601 fprintf(stderr
, "%s@%d: ", level
== 0 ? "Warning" : "Error", line_no
);
602 vfprintf(stdout
, msg
, ap
);
603 fprintf(stderr
, "\n");
609 writefdef(struct fstate
*f
, int nm
)
611 fprintf(output
, ".ds f%c %s\n.nr s%c %d\n",
612 nm
, f
->font
, nm
, f
->size
);
616 chfont(struct fstate
*of
, struct fstate
*nf
, char *s
)
618 if (of
->font
[0] != nf
->font
[0]
619 || of
->font
[1] != nf
->font
[1]) {
629 if (nf
->size
!= of
->size
) {
632 if (nf
->size
< of
->size
) {
634 *s
++ = '0' + of
->size
- nf
->size
;
637 *s
++ = '0' + nf
->size
- of
->size
;
645 parsefont(struct fstate
*f
, const char *s0
)
649 memset(f
, 0, sizeof *f
);
651 if (isdigit((unsigned char)*s
))
652 f
->size
= f
->size
* 10 + *s
- '0';
654 if (isupper((unsigned char)*s
)) {
669 fprintf(stderr
, "indent: bad font specification: %s\n", s0
);
679 f
->size
= bodyf
.size
+ sizedelta
;
682 f
->size
+= bodyf
.size
;
684 f
->size
= bodyf
.size
- f
->size
;