| @@ -1,545 +1,545 @@ | | | @@ -1,545 +1,545 @@ |
1 | /* $NetBSD: io.c,v 1.28 2021/03/12 19:11:29 rillig Exp $ */ | | 1 | /* $NetBSD: io.c,v 1.29 2021/03/12 19:14:18 rillig Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * SPDX-License-Identifier: BSD-4-Clause | | 4 | * SPDX-License-Identifier: BSD-4-Clause |
5 | * | | 5 | * |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. |
7 | * Copyright (c) 1980, 1993 | | 7 | * Copyright (c) 1980, 1993 |
8 | * The Regents of the University of California. All rights reserved. | | 8 | * The Regents of the University of California. All rights reserved. |
9 | * All rights reserved. | | 9 | * All rights reserved. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. | | 15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright | | 16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the | | 17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. | | 18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. All advertising materials mentioning features or use of this software | | 19 | * 3. All advertising materials mentioning features or use of this software |
20 | * must display the following acknowledgement: | | 20 | * must display the following acknowledgement: |
21 | * This product includes software developed by the University of | | 21 | * This product includes software developed by the University of |
22 | * California, Berkeley and its contributors. | | 22 | * California, Berkeley and its contributors. |
23 | * 4. Neither the name of the University nor the names of its contributors | | 23 | * 4. Neither the name of the University nor the names of its contributors |
24 | * may be used to endorse or promote products derived from this software | | 24 | * may be used to endorse or promote products derived from this software |
25 | * without specific prior written permission. | | 25 | * without specific prior written permission. |
26 | * | | 26 | * |
27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37 | * SUCH DAMAGE. | | 37 | * SUCH DAMAGE. |
38 | */ | | 38 | */ |
39 | | | 39 | |
40 | #if 0 | | 40 | #if 0 |
41 | #ifndef lint | | 41 | #ifndef lint |
42 | static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; | | 42 | static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; |
43 | #endif /* not lint */ | | 43 | #endif /* not lint */ |
44 | #endif | | 44 | #endif |
45 | | | 45 | |
46 | #include <sys/cdefs.h> | | 46 | #include <sys/cdefs.h> |
47 | #ifndef lint | | 47 | #ifndef lint |
48 | #if defined(__NetBSD__) | | 48 | #if defined(__NetBSD__) |
49 | __RCSID("$NetBSD: io.c,v 1.28 2021/03/12 19:11:29 rillig Exp $"); | | 49 | __RCSID("$NetBSD: io.c,v 1.29 2021/03/12 19:14:18 rillig Exp $"); |
50 | #elif defined(__FreeBSD__) | | 50 | #elif defined(__FreeBSD__) |
51 | __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); | | 51 | __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); |
52 | #endif | | 52 | #endif |
53 | #endif | | 53 | #endif |
54 | | | 54 | |
55 | #include <ctype.h> | | 55 | #include <ctype.h> |
56 | #include <err.h> | | 56 | #include <err.h> |
57 | #include <stdio.h> | | 57 | #include <stdio.h> |
58 | #include <stdlib.h> | | 58 | #include <stdlib.h> |
59 | #include <string.h> | | 59 | #include <string.h> |
60 | #include <stdarg.h> | | 60 | #include <stdarg.h> |
61 | | | 61 | |
62 | #include "indent.h" | | 62 | #include "indent.h" |
63 | | | 63 | |
64 | int comment_open; | | 64 | int comment_open; |
65 | static int paren_target; | | 65 | static int paren_target; |
66 | static int pad_output(int current, int target); | | 66 | static int pad_output(int current, int target); |
67 | | | 67 | |
68 | static void | | 68 | static void |
69 | output_char(char ch) | | 69 | output_char(char ch) |
70 | { | | 70 | { |
71 | fputc(ch, output); | | 71 | fputc(ch, output); |
72 | } | | 72 | } |
73 | | | 73 | |
74 | static void | | 74 | static void |
75 | output_range(const char *s, const char *e) | | 75 | output_range(const char *s, const char *e) |
76 | { | | 76 | { |
77 | fwrite(s, 1, (size_t)(e - s), output); | | 77 | fwrite(s, 1, (size_t)(e - s), output); |
78 | } | | 78 | } |
79 | | | 79 | |
80 | static void | | 80 | static inline void |
81 | output_string(const char *s) | | 81 | output_string(const char *s) |
82 | { | | 82 | { |
83 | output_range(s, s + strlen(s)); | | 83 | output_range(s, s + strlen(s)); |
84 | } | | 84 | } |
85 | | | 85 | |
86 | static void | | 86 | static void |
87 | output_int(int i) | | 87 | output_int(int i) |
88 | { | | 88 | { |
89 | fprintf(output, "%d", i); | | 89 | fprintf(output, "%d", i); |
90 | } | | 90 | } |
91 | | | 91 | |
92 | /* | | 92 | /* |
93 | * dump_line is the routine that actually effects the printing of the new | | 93 | * dump_line is the routine that actually effects the printing of the new |
94 | * source. It prints the label section, followed by the code section with | | 94 | * source. It prints the label section, followed by the code section with |
95 | * the appropriate nesting level, followed by any comments. | | 95 | * the appropriate nesting level, followed by any comments. |
96 | */ | | 96 | */ |
97 | void | | 97 | void |
98 | dump_line(void) | | 98 | dump_line(void) |
99 | { | | 99 | { |
100 | int cur_col, target_col; | | 100 | int cur_col, target_col; |
101 | static int not_first_line; | | 101 | static int not_first_line; |
102 | | | 102 | |
103 | if (ps.procname[0]) { | | 103 | if (ps.procname[0]) { |
104 | ps.ind_level = 0; | | 104 | ps.ind_level = 0; |
105 | ps.procname[0] = 0; | | 105 | ps.procname[0] = 0; |
106 | } | | 106 | } |
107 | if (s_code == e_code && s_lab == e_lab && s_com == e_com) { | | 107 | if (s_code == e_code && s_lab == e_lab && s_com == e_com) { |
108 | if (suppress_blanklines > 0) | | 108 | if (suppress_blanklines > 0) |
109 | suppress_blanklines--; | | 109 | suppress_blanklines--; |
110 | else { | | 110 | else { |
111 | ps.bl_line = true; | | 111 | ps.bl_line = true; |
112 | n_real_blanklines++; | | 112 | n_real_blanklines++; |
113 | } | | 113 | } |
114 | } | | 114 | } |
115 | else if (!inhibit_formatting) { | | 115 | else if (!inhibit_formatting) { |
116 | suppress_blanklines = 0; | | 116 | suppress_blanklines = 0; |
117 | ps.bl_line = false; | | 117 | ps.bl_line = false; |
118 | if (prefix_blankline_requested && not_first_line) { | | 118 | if (prefix_blankline_requested && not_first_line) { |
119 | if (opt.swallow_optional_blanklines) { | | 119 | if (opt.swallow_optional_blanklines) { |
120 | if (n_real_blanklines == 1) | | 120 | if (n_real_blanklines == 1) |
121 | n_real_blanklines = 0; | | 121 | n_real_blanklines = 0; |
122 | } | | 122 | } |
123 | else { | | 123 | else { |
124 | if (n_real_blanklines == 0) | | 124 | if (n_real_blanklines == 0) |
125 | n_real_blanklines = 1; | | 125 | n_real_blanklines = 1; |
126 | } | | 126 | } |
127 | } | | 127 | } |
128 | while (--n_real_blanklines >= 0) | | 128 | while (--n_real_blanklines >= 0) |
129 | output_char('\n'); | | 129 | output_char('\n'); |
130 | n_real_blanklines = 0; | | 130 | n_real_blanklines = 0; |
131 | if (ps.ind_level == 0) | | 131 | if (ps.ind_level == 0) |
132 | ps.ind_stmt = 0; /* this is a class A kludge. dont do | | 132 | ps.ind_stmt = 0; /* this is a class A kludge. dont do |
133 | * additional statement indentation if we are | | 133 | * additional statement indentation if we are |
134 | * at bracket level 0 */ | | 134 | * at bracket level 0 */ |
135 | | | 135 | |
136 | if (e_lab != s_lab || e_code != s_code) | | 136 | if (e_lab != s_lab || e_code != s_code) |
137 | ++code_lines; /* keep count of lines with code */ | | 137 | ++code_lines; /* keep count of lines with code */ |
138 | | | 138 | |
139 | | | 139 | |
140 | if (e_lab != s_lab) { /* print lab, if any */ | | 140 | if (e_lab != s_lab) { /* print lab, if any */ |
141 | if (comment_open) { | | 141 | if (comment_open) { |
142 | comment_open = 0; | | 142 | comment_open = 0; |
143 | output_string(".*/\n"); | | 143 | output_string(".*/\n"); |
144 | } | | 144 | } |
145 | while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) | | 145 | while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) |
146 | e_lab--; | | 146 | e_lab--; |
147 | *e_lab = '\0'; | | 147 | *e_lab = '\0'; |
148 | cur_col = pad_output(1, compute_label_target()); | | 148 | cur_col = pad_output(1, compute_label_target()); |
149 | if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 | | 149 | if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 |
150 | || strncmp(s_lab, "#endif", 6) == 0)) { | | 150 | || strncmp(s_lab, "#endif", 6) == 0)) { |
151 | char *s = s_lab; | | 151 | char *s = s_lab; |
152 | if (e_lab[-1] == '\n') e_lab--; | | 152 | if (e_lab[-1] == '\n') e_lab--; |
153 | do { | | 153 | do { |
154 | output_char(*s++); | | 154 | output_char(*s++); |
155 | } while (s < e_lab && 'a' <= *s && *s <= 'z'); | | 155 | } while (s < e_lab && 'a' <= *s && *s <= 'z'); |
156 | while ((*s == ' ' || *s == '\t') && s < e_lab) | | 156 | while ((*s == ' ' || *s == '\t') && s < e_lab) |
157 | s++; | | 157 | s++; |
158 | if (s < e_lab) { | | 158 | if (s < e_lab) { |
159 | if (s[0] == '/' && s[1] == '*') { | | 159 | if (s[0] == '/' && s[1] == '*') { |
160 | output_char('\t'); | | 160 | output_char('\t'); |
161 | output_range(s, e_lab); | | 161 | output_range(s, e_lab); |
162 | } else { | | 162 | } else { |
163 | output_string("\t/* "); | | 163 | output_string("\t/* "); |
164 | output_range(s, e_lab); | | 164 | output_range(s, e_lab); |
165 | output_string(" */"); | | 165 | output_string(" */"); |
166 | } | | 166 | } |
167 | } | | 167 | } |
168 | } else | | 168 | } else |
169 | output_range(s_lab, e_lab); | | 169 | output_range(s_lab, e_lab); |
170 | cur_col = count_spaces(cur_col, s_lab); | | 170 | cur_col = count_spaces(cur_col, s_lab); |
171 | } | | 171 | } |
172 | else | | 172 | else |
173 | cur_col = 1; /* there is no label section */ | | 173 | cur_col = 1; /* there is no label section */ |
174 | | | 174 | |
175 | ps.pcase = false; | | 175 | ps.pcase = false; |
176 | | | 176 | |
177 | if (s_code != e_code) { /* print code section, if any */ | | 177 | if (s_code != e_code) { /* print code section, if any */ |
178 | char *p; | | 178 | char *p; |
179 | | | 179 | |
180 | if (comment_open) { | | 180 | if (comment_open) { |
181 | comment_open = 0; | | 181 | comment_open = 0; |
182 | output_string(".*/\n"); | | 182 | output_string(".*/\n"); |
183 | } | | 183 | } |
184 | target_col = compute_code_target(); | | 184 | target_col = compute_code_target(); |
185 | { | | 185 | { |
186 | int i; | | 186 | int i; |
187 | | | 187 | |
188 | for (i = 0; i < ps.p_l_follow; i++) | | 188 | for (i = 0; i < ps.p_l_follow; i++) |
189 | if (ps.paren_indents[i] >= 0) | | 189 | if (ps.paren_indents[i] >= 0) |
190 | ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); | | 190 | ps.paren_indents[i] = -(ps.paren_indents[i] + target_col); |
191 | } | | 191 | } |
192 | cur_col = pad_output(cur_col, target_col); | | 192 | cur_col = pad_output(cur_col, target_col); |
193 | for (p = s_code; p < e_code; p++) | | 193 | for (p = s_code; p < e_code; p++) |
194 | if (*p == (char) 0200) | | 194 | if (*p == (char) 0200) |
195 | output_int(target_col * 7); | | 195 | output_int(target_col * 7); |
196 | else | | 196 | else |
197 | output_char(*p); | | 197 | output_char(*p); |
198 | cur_col = count_spaces(cur_col, s_code); | | 198 | cur_col = count_spaces(cur_col, s_code); |
199 | } | | 199 | } |
200 | if (s_com != e_com) { /* print comment, if any */ | | 200 | if (s_com != e_com) { /* print comment, if any */ |
201 | int target = ps.com_col; | | 201 | int target = ps.com_col; |
202 | char *com_st = s_com; | | 202 | char *com_st = s_com; |
203 | | | 203 | |
204 | target += ps.comment_delta; | | 204 | target += ps.comment_delta; |
205 | while (*com_st == '\t') /* consider original indentation in | | 205 | while (*com_st == '\t') /* consider original indentation in |
206 | * case this is a box comment */ | | 206 | * case this is a box comment */ |
207 | com_st++, target += opt.tabsize; | | 207 | com_st++, target += opt.tabsize; |
208 | while (target <= 0) | | 208 | while (target <= 0) |
209 | if (*com_st == ' ') | | 209 | if (*com_st == ' ') |
210 | target++, com_st++; | | 210 | target++, com_st++; |
211 | else if (*com_st == '\t') { | | 211 | else if (*com_st == '\t') { |
212 | target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1; | | 212 | target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1; |
213 | com_st++; | | 213 | com_st++; |
214 | } | | 214 | } |
215 | else | | 215 | else |
216 | target = 1; | | 216 | target = 1; |
217 | if (cur_col > target) { /* if comment can't fit on this line, | | 217 | if (cur_col > target) { /* if comment can't fit on this line, |
218 | * put it on next line */ | | 218 | * put it on next line */ |
219 | output_char('\n'); | | 219 | output_char('\n'); |
220 | cur_col = 1; | | 220 | cur_col = 1; |
221 | ++ps.out_lines; | | 221 | ++ps.out_lines; |
222 | } | | 222 | } |
223 | while (e_com > com_st && isspace((unsigned char)e_com[-1])) | | 223 | while (e_com > com_st && isspace((unsigned char)e_com[-1])) |
224 | e_com--; | | 224 | e_com--; |
225 | (void)pad_output(cur_col, target); | | 225 | (void)pad_output(cur_col, target); |
226 | output_range(com_st, e_com); | | 226 | output_range(com_st, e_com); |
227 | ps.comment_delta = ps.n_comment_delta; | | 227 | ps.comment_delta = ps.n_comment_delta; |
228 | ++ps.com_lines; /* count lines with comments */ | | 228 | ++ps.com_lines; /* count lines with comments */ |
229 | } | | 229 | } |
230 | if (ps.use_ff) | | 230 | if (ps.use_ff) |
231 | output_char('\014'); | | 231 | output_char('\014'); |
232 | else | | 232 | else |
233 | output_char('\n'); | | 233 | output_char('\n'); |
234 | ++ps.out_lines; | | 234 | ++ps.out_lines; |
235 | if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { | | 235 | if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { |
236 | prefix_blankline_requested = 1; | | 236 | prefix_blankline_requested = 1; |
237 | ps.just_saw_decl = 0; | | 237 | ps.just_saw_decl = 0; |
238 | } | | 238 | } |
239 | else | | 239 | else |
240 | prefix_blankline_requested = postfix_blankline_requested; | | 240 | prefix_blankline_requested = postfix_blankline_requested; |
241 | postfix_blankline_requested = 0; | | 241 | postfix_blankline_requested = 0; |
242 | } | | 242 | } |
243 | | | 243 | |
244 | /* keep blank lines after '//' comments */ | | 244 | /* keep blank lines after '//' comments */ |
245 | if (e_com - s_com > 1 && s_com[1] == '/') | | 245 | if (e_com - s_com > 1 && s_com[1] == '/') |
246 | output_range(s_token, e_token); | | 246 | output_range(s_token, e_token); |
247 | | | 247 | |
248 | ps.decl_on_line = ps.in_decl; /* if we are in the middle of a | | 248 | ps.decl_on_line = ps.in_decl; /* if we are in the middle of a |
249 | * declaration, remember that fact for | | 249 | * declaration, remember that fact for |
250 | * proper comment indentation */ | | 250 | * proper comment indentation */ |
251 | ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be | | 251 | ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be |
252 | * indented if we have not | | 252 | * indented if we have not |
253 | * completed this stmt and if | | 253 | * completed this stmt and if |
254 | * we are not in the middle of | | 254 | * we are not in the middle of |
255 | * a declaration */ | | 255 | * a declaration */ |
256 | ps.use_ff = false; | | 256 | ps.use_ff = false; |
257 | ps.dumped_decl_indent = 0; | | 257 | ps.dumped_decl_indent = 0; |
258 | *(e_lab = s_lab) = '\0'; /* reset buffers */ | | 258 | *(e_lab = s_lab) = '\0'; /* reset buffers */ |
259 | *(e_code = s_code) = '\0'; | | 259 | *(e_code = s_code) = '\0'; |
260 | *(e_com = s_com = combuf + 1) = '\0'; | | 260 | *(e_com = s_com = combuf + 1) = '\0'; |
261 | ps.ind_level = ps.i_l_follow; | | 261 | ps.ind_level = ps.i_l_follow; |
262 | ps.paren_level = ps.p_l_follow; | | 262 | ps.paren_level = ps.p_l_follow; |
263 | if (ps.paren_level > 0) | | 263 | if (ps.paren_level > 0) |
264 | paren_target = -ps.paren_indents[ps.paren_level - 1]; | | 264 | paren_target = -ps.paren_indents[ps.paren_level - 1]; |
265 | not_first_line = 1; | | 265 | not_first_line = 1; |
266 | } | | 266 | } |
267 | | | 267 | |
268 | int | | 268 | int |
269 | compute_code_target(void) | | 269 | compute_code_target(void) |
270 | { | | 270 | { |
271 | int target_col = opt.ind_size * ps.ind_level + 1; | | 271 | int target_col = opt.ind_size * ps.ind_level + 1; |
272 | | | 272 | |
273 | if (ps.paren_level) | | 273 | if (ps.paren_level) |
274 | if (!opt.lineup_to_parens) | | 274 | if (!opt.lineup_to_parens) |
275 | target_col += opt.continuation_indent * | | 275 | target_col += opt.continuation_indent * |
276 | (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); | | 276 | (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level); |
277 | else if (opt.lineup_to_parens_always) | | 277 | else if (opt.lineup_to_parens_always) |
278 | target_col = paren_target; | | 278 | target_col = paren_target; |
279 | else { | | 279 | else { |
280 | int w; | | 280 | int w; |
281 | int t = paren_target; | | 281 | int t = paren_target; |
282 | | | 282 | |
283 | if ((w = count_spaces(t, s_code) - opt.max_col) > 0 | | 283 | if ((w = count_spaces(t, s_code) - opt.max_col) > 0 |
284 | && count_spaces(target_col, s_code) <= opt.max_col) { | | 284 | && count_spaces(target_col, s_code) <= opt.max_col) { |
285 | t -= w + 1; | | 285 | t -= w + 1; |
286 | if (t > target_col) | | 286 | if (t > target_col) |
287 | target_col = t; | | 287 | target_col = t; |
288 | } | | 288 | } |
289 | else | | 289 | else |
290 | target_col = t; | | 290 | target_col = t; |
291 | } | | 291 | } |
292 | else if (ps.ind_stmt) | | 292 | else if (ps.ind_stmt) |
293 | target_col += opt.continuation_indent; | | 293 | target_col += opt.continuation_indent; |
294 | return target_col; | | 294 | return target_col; |
295 | } | | 295 | } |
296 | | | 296 | |
297 | int | | 297 | int |
298 | compute_label_target(void) | | 298 | compute_label_target(void) |
299 | { | | 299 | { |
300 | return | | 300 | return |
301 | ps.pcase ? (int) (case_ind * opt.ind_size) + 1 | | 301 | ps.pcase ? (int) (case_ind * opt.ind_size) + 1 |
302 | : *s_lab == '#' ? 1 | | 302 | : *s_lab == '#' ? 1 |
303 | : opt.ind_size * (ps.ind_level - label_offset) + 1; | | 303 | : opt.ind_size * (ps.ind_level - label_offset) + 1; |
304 | } | | 304 | } |
305 | | | 305 | |
306 | | | 306 | |
307 | /* | | 307 | /* |
308 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois | | 308 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois |
309 | * | | 309 | * |
310 | * All rights reserved | | 310 | * All rights reserved |
311 | * | | 311 | * |
312 | * | | 312 | * |
313 | * NAME: fill_buffer | | 313 | * NAME: fill_buffer |
314 | * | | 314 | * |
315 | * FUNCTION: Reads one block of input into input_buffer | | 315 | * FUNCTION: Reads one block of input into input_buffer |
316 | * | | 316 | * |
317 | * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A | | 317 | * HISTORY: initial coding November 1976 D A Willcox of CAC 1/7/77 A |
318 | * Willcox of CAC Added check for switch back to partly full input | | 318 | * Willcox of CAC Added check for switch back to partly full input |
319 | * buffer from temporary buffer | | 319 | * buffer from temporary buffer |
320 | * | | 320 | * |
321 | */ | | 321 | */ |
322 | void | | 322 | void |
323 | fill_buffer(void) | | 323 | fill_buffer(void) |
324 | { /* this routine reads stuff from the input */ | | 324 | { /* this routine reads stuff from the input */ |
325 | char *p; | | 325 | char *p; |
326 | int i; | | 326 | int i; |
327 | FILE *f = input; | | 327 | FILE *f = input; |
328 | | | 328 | |
329 | if (bp_save != NULL) { /* there is a partly filled input buffer left */ | | 329 | if (bp_save != NULL) { /* there is a partly filled input buffer left */ |
330 | buf_ptr = bp_save; /* do not read anything, just switch buffers */ | | 330 | buf_ptr = bp_save; /* do not read anything, just switch buffers */ |
331 | buf_end = be_save; | | 331 | buf_end = be_save; |
332 | bp_save = be_save = NULL; | | 332 | bp_save = be_save = NULL; |
333 | if (buf_ptr < buf_end) | | 333 | if (buf_ptr < buf_end) |
334 | return; /* only return if there is really something in | | 334 | return; /* only return if there is really something in |
335 | * this buffer */ | | 335 | * this buffer */ |
336 | } | | 336 | } |
337 | for (p = in_buffer;;) { | | 337 | for (p = in_buffer;;) { |
338 | if (p >= in_buffer_limit) { | | 338 | if (p >= in_buffer_limit) { |
339 | int size = (in_buffer_limit - in_buffer) * 2 + 10; | | 339 | int size = (in_buffer_limit - in_buffer) * 2 + 10; |
340 | int offset = p - in_buffer; | | 340 | int offset = p - in_buffer; |
341 | in_buffer = realloc(in_buffer, size); | | 341 | in_buffer = realloc(in_buffer, size); |
342 | if (in_buffer == NULL) | | 342 | if (in_buffer == NULL) |
343 | errx(1, "input line too long"); | | 343 | errx(1, "input line too long"); |
344 | p = in_buffer + offset; | | 344 | p = in_buffer + offset; |
345 | in_buffer_limit = in_buffer + size - 2; | | 345 | in_buffer_limit = in_buffer + size - 2; |
346 | } | | 346 | } |
347 | if ((i = getc(f)) == EOF) { | | 347 | if ((i = getc(f)) == EOF) { |
348 | *p++ = ' '; | | 348 | *p++ = ' '; |
349 | *p++ = '\n'; | | 349 | *p++ = '\n'; |
350 | had_eof = true; | | 350 | had_eof = true; |
351 | break; | | 351 | break; |
352 | } | | 352 | } |
353 | if (i != '\0') | | 353 | if (i != '\0') |
354 | *p++ = i; | | 354 | *p++ = i; |
355 | if (i == '\n') | | 355 | if (i == '\n') |
356 | break; | | 356 | break; |
357 | } | | 357 | } |
358 | buf_ptr = in_buffer; | | 358 | buf_ptr = in_buffer; |
359 | buf_end = p; | | 359 | buf_end = p; |
360 | if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') { | | 360 | if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') { |
361 | if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) | | 361 | if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) |
362 | fill_buffer(); /* flush indent error message */ | | 362 | fill_buffer(); /* flush indent error message */ |
363 | else { | | 363 | else { |
364 | int com = 0; | | 364 | int com = 0; |
365 | | | 365 | |
366 | p = in_buffer; | | 366 | p = in_buffer; |
367 | while (*p == ' ' || *p == '\t') | | 367 | while (*p == ' ' || *p == '\t') |
368 | p++; | | 368 | p++; |
369 | if (*p == '/' && p[1] == '*') { | | 369 | if (*p == '/' && p[1] == '*') { |
370 | p += 2; | | 370 | p += 2; |
371 | while (*p == ' ' || *p == '\t') | | 371 | while (*p == ' ' || *p == '\t') |
372 | p++; | | 372 | p++; |
373 | if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' | | 373 | if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' |
374 | && p[4] == 'N' && p[5] == 'T') { | | 374 | && p[4] == 'N' && p[5] == 'T') { |
375 | p += 6; | | 375 | p += 6; |
376 | while (*p == ' ' || *p == '\t') | | 376 | while (*p == ' ' || *p == '\t') |
377 | p++; | | 377 | p++; |
378 | if (*p == '*') | | 378 | if (*p == '*') |
379 | com = 1; | | 379 | com = 1; |
380 | else if (*p == 'O') { | | 380 | else if (*p == 'O') { |
381 | if (*++p == 'N') | | 381 | if (*++p == 'N') |
382 | p++, com = 1; | | 382 | p++, com = 1; |
383 | else if (*p == 'F' && *++p == 'F') | | 383 | else if (*p == 'F' && *++p == 'F') |
384 | p++, com = 2; | | 384 | p++, com = 2; |
385 | } | | 385 | } |
386 | while (*p == ' ' || *p == '\t') | | 386 | while (*p == ' ' || *p == '\t') |
387 | p++; | | 387 | p++; |
388 | if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { | | 388 | if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { |
389 | if (s_com != e_com || s_lab != e_lab || s_code != e_code) | | 389 | if (s_com != e_com || s_lab != e_lab || s_code != e_code) |
390 | dump_line(); | | 390 | dump_line(); |
391 | if (!(inhibit_formatting = com - 1)) { | | 391 | if (!(inhibit_formatting = com - 1)) { |
392 | n_real_blanklines = 0; | | 392 | n_real_blanklines = 0; |
393 | postfix_blankline_requested = 0; | | 393 | postfix_blankline_requested = 0; |
394 | prefix_blankline_requested = 0; | | 394 | prefix_blankline_requested = 0; |
395 | suppress_blanklines = 1; | | 395 | suppress_blanklines = 1; |
396 | } | | 396 | } |
397 | } | | 397 | } |
398 | } | | 398 | } |
399 | } | | 399 | } |
400 | } | | 400 | } |
401 | } | | 401 | } |
402 | if (inhibit_formatting) { | | 402 | if (inhibit_formatting) { |
403 | p = in_buffer; | | 403 | p = in_buffer; |
404 | do { | | 404 | do { |
405 | output_char(*p); | | 405 | output_char(*p); |
406 | } while (*p++ != '\n'); | | 406 | } while (*p++ != '\n'); |
407 | } | | 407 | } |
408 | } | | 408 | } |
409 | | | 409 | |
410 | /* | | 410 | /* |
411 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois | | 411 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois |
412 | * | | 412 | * |
413 | * All rights reserved | | 413 | * All rights reserved |
414 | * | | 414 | * |
415 | * | | 415 | * |
416 | * NAME: pad_output | | 416 | * NAME: pad_output |
417 | * | | 417 | * |
418 | * FUNCTION: Writes tabs and spaces to move the current column up to the desired | | 418 | * FUNCTION: Writes tabs and spaces to move the current column up to the desired |
419 | * position. | | 419 | * position. |
420 | * | | 420 | * |
421 | * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. | | 421 | * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf. |
422 | * | | 422 | * |
423 | * PARAMETERS: current integer The current column target | | 423 | * PARAMETERS: current integer The current column target |
424 | * nteger The desired column | | 424 | * nteger The desired column |
425 | * | | 425 | * |
426 | * RETURNS: Integer value of the new column. (If current >= target, no action is | | 426 | * RETURNS: Integer value of the new column. (If current >= target, no action is |
427 | * taken, and current is returned. | | 427 | * taken, and current is returned. |
428 | * | | 428 | * |
429 | * GLOBALS: None | | 429 | * GLOBALS: None |
430 | * | | 430 | * |
431 | * CALLS: write (sys) | | 431 | * CALLS: write (sys) |
432 | * | | 432 | * |
433 | * CALLED BY: dump_line | | 433 | * CALLED BY: dump_line |
434 | * | | 434 | * |
435 | * HISTORY: initial coding November 1976 D A Willcox of CAC | | 435 | * HISTORY: initial coding November 1976 D A Willcox of CAC |
436 | * | | 436 | * |
437 | */ | | 437 | */ |
438 | static int | | 438 | static int |
439 | pad_output(int current, int target) | | 439 | pad_output(int current, int target) |
440 | /* writes tabs and blanks (if necessary) to | | 440 | /* writes tabs and blanks (if necessary) to |
441 | * get the current output position up to the | | 441 | * get the current output position up to the |
442 | * target column */ | | 442 | * target column */ |
443 | /* current: the current column value */ | | 443 | /* current: the current column value */ |
444 | /* target: position we want it at */ | | 444 | /* target: position we want it at */ |
445 | { | | 445 | { |
446 | int curr; /* internal column pointer */ | | 446 | int curr; /* internal column pointer */ |
447 | | | 447 | |
448 | if (current >= target) | | 448 | if (current >= target) |
449 | return current; /* line is already long enough */ | | 449 | return current; /* line is already long enough */ |
450 | curr = current; | | 450 | curr = current; |
451 | if (opt.use_tabs) { | | 451 | if (opt.use_tabs) { |
452 | int tcur; | | 452 | int tcur; |
453 | | | 453 | |
454 | while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) { | | 454 | while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) { |
455 | output_char('\t'); | | 455 | output_char('\t'); |
456 | curr = tcur; | | 456 | curr = tcur; |
457 | } | | 457 | } |
458 | } | | 458 | } |
459 | while (curr++ < target) | | 459 | while (curr++ < target) |
460 | output_char(' '); /* pad with final blanks */ | | 460 | output_char(' '); /* pad with final blanks */ |
461 | | | 461 | |
462 | return target; | | 462 | return target; |
463 | } | | 463 | } |
464 | | | 464 | |
465 | /* | | 465 | /* |
466 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois | | 466 | * Copyright (C) 1976 by the Board of Trustees of the University of Illinois |
467 | * | | 467 | * |
468 | * All rights reserved | | 468 | * All rights reserved |
469 | * | | 469 | * |
470 | * | | 470 | * |
471 | * NAME: count_spaces | | 471 | * NAME: count_spaces |
472 | * | | 472 | * |
473 | * FUNCTION: Find out where printing of a given string will leave the current | | 473 | * FUNCTION: Find out where printing of a given string will leave the current |
474 | * character position on output. | | 474 | * character position on output. |
475 | * | | 475 | * |
476 | * ALGORITHM: Run thru input string and add appropriate values to current | | 476 | * ALGORITHM: Run thru input string and add appropriate values to current |
477 | * position. | | 477 | * position. |
478 | * | | 478 | * |
479 | * RETURNS: Integer value of position after printing "buffer" starting in column | | 479 | * RETURNS: Integer value of position after printing "buffer" starting in column |
480 | * "current". | | 480 | * "current". |
481 | * | | 481 | * |
482 | * HISTORY: initial coding November 1976 D A Willcox of CAC | | 482 | * HISTORY: initial coding November 1976 D A Willcox of CAC |
483 | * | | 483 | * |
484 | */ | | 484 | */ |
485 | int | | 485 | int |
486 | count_spaces_until(int cur, char *buffer, char *end) | | 486 | count_spaces_until(int cur, char *buffer, char *end) |
487 | /* | | 487 | /* |
488 | * this routine figures out where the character position will be after | | 488 | * this routine figures out where the character position will be after |
489 | * printing the text in buffer starting at column "current" | | 489 | * printing the text in buffer starting at column "current" |
490 | */ | | 490 | */ |
491 | { | | 491 | { |
492 | char *buf; /* used to look thru buffer */ | | 492 | char *buf; /* used to look thru buffer */ |
493 | | | 493 | |
494 | for (buf = buffer; *buf != '\0' && buf != end; ++buf) { | | 494 | for (buf = buffer; *buf != '\0' && buf != end; ++buf) { |
495 | switch (*buf) { | | 495 | switch (*buf) { |
496 | | | 496 | |
497 | case '\n': | | 497 | case '\n': |
498 | case 014: /* form feed */ | | 498 | case 014: /* form feed */ |
499 | cur = 1; | | 499 | cur = 1; |
500 | break; | | 500 | break; |
501 | | | 501 | |
502 | case '\t': | | 502 | case '\t': |
503 | cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1; | | 503 | cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1; |
504 | break; | | 504 | break; |
505 | | | 505 | |
506 | case 010: /* backspace */ | | 506 | case 010: /* backspace */ |
507 | --cur; | | 507 | --cur; |
508 | break; | | 508 | break; |
509 | | | 509 | |
510 | default: | | 510 | default: |
511 | ++cur; | | 511 | ++cur; |
512 | break; | | 512 | break; |
513 | } /* end of switch */ | | 513 | } /* end of switch */ |
514 | } /* end of for loop */ | | 514 | } /* end of for loop */ |
515 | return cur; | | 515 | return cur; |
516 | } | | 516 | } |
517 | | | 517 | |
518 | int | | 518 | int |
519 | count_spaces(int cur, char *buffer) | | 519 | count_spaces(int cur, char *buffer) |
520 | { | | 520 | { |
521 | return count_spaces_until(cur, buffer, NULL); | | 521 | return count_spaces_until(cur, buffer, NULL); |
522 | } | | 522 | } |
523 | | | 523 | |
524 | void | | 524 | void |
525 | diag(int level, const char *msg, ...) | | 525 | diag(int level, const char *msg, ...) |
526 | { | | 526 | { |
527 | va_list ap; | | 527 | va_list ap; |
528 | const char *s, *e; | | 528 | const char *s, *e; |
529 | | | 529 | |
530 | if (level) | | 530 | if (level) |
531 | found_err = 1; | | 531 | found_err = 1; |
532 | | | 532 | |
533 | if (output == stdout) { | | 533 | if (output == stdout) { |
534 | s = "/**INDENT** "; | | 534 | s = "/**INDENT** "; |
535 | e = " */"; | | 535 | e = " */"; |
536 | } else { | | 536 | } else { |
537 | s = e = ""; | | 537 | s = e = ""; |
538 | } | | 538 | } |
539 | | | 539 | |
540 | va_start(ap, msg); | | 540 | va_start(ap, msg); |
541 | fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no); | | 541 | fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no); |
542 | vfprintf(stderr, msg, ap); | | 542 | vfprintf(stderr, msg, ap); |
543 | fprintf(stderr, "%s\n", e); | | 543 | fprintf(stderr, "%s\n", e); |
544 | va_end(ap); | | 544 | va_end(ap); |
545 | } | | 545 | } |