Sat Mar 13 13:55:42 2021 UTC ()
indent: rename local variable in dump_line

This clarifies that the variable names a column, not an indentation.


(rillig)
diff -r1.44 -r1.45 src/usr.bin/indent/io.c

cvs diff -r1.44 -r1.45 src/usr.bin/indent/io.c (switch to unified diff)

--- src/usr.bin/indent/io.c 2021/03/13 13:54:01 1.44
+++ src/usr.bin/indent/io.c 2021/03/13 13:55:42 1.45
@@ -1,471 +1,471 @@ @@ -1,471 +1,471 @@
1/* $NetBSD: io.c,v 1.44 2021/03/13 13:54:01 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.45 2021/03/13 13:55:42 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
42static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 42static 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.44 2021/03/13 13:54:01 rillig Exp $"); 49__RCSID("$NetBSD: io.c,v 1.45 2021/03/13 13:55:42 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
64int comment_open; 64int comment_open;
65static int paren_indent; 65static int paren_indent;
66 66
67static void 67static void
68output_char(char ch) 68output_char(char ch)
69{ 69{
70 fputc(ch, output); 70 fputc(ch, output);
71 debug_vis_range("output_char '", &ch, &ch + 1, "'\n"); 71 debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
72} 72}
73 73
74static void 74static void
75output_range(const char *s, const char *e) 75output_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 debug_vis_range("output_range \"", s, e, "\"\n"); 78 debug_vis_range("output_range \"", s, e, "\"\n");
79} 79}
80 80
81static inline void 81static inline void
82output_string(const char *s) 82output_string(const char *s)
83{ 83{
84 output_range(s, s + strlen(s)); 84 output_range(s, s + strlen(s));
85} 85}
86 86
87static int 87static int
88output_indent(int old_ind, int new_ind) 88output_indent(int old_ind, int new_ind)
89{ 89{
90 int ind = old_ind; 90 int ind = old_ind;
91 91
92 if (opt.use_tabs) { 92 if (opt.use_tabs) {
93 int tabsize = opt.tabsize; 93 int tabsize = opt.tabsize;
94 int n = new_ind / tabsize - ind / tabsize; 94 int n = new_ind / tabsize - ind / tabsize;
95 if (n > 0) 95 if (n > 0)
96 ind -= ind % tabsize; 96 ind -= ind % tabsize;
97 for (int i = 0; i < n; i++) { 97 for (int i = 0; i < n; i++) {
98 fputc('\t', output); 98 fputc('\t', output);
99 ind += tabsize; 99 ind += tabsize;
100 } 100 }
101 } 101 }
102 102
103 for (; ind < new_ind; ind++) 103 for (; ind < new_ind; ind++)
104 fputc(' ', output); 104 fputc(' ', output);
105 105
106 debug_println("output_indent %d", ind); 106 debug_println("output_indent %d", ind);
107 return ind; 107 return ind;
108} 108}
109 109
110/* 110/*
111 * dump_line is the routine that actually effects the printing of the new 111 * dump_line is the routine that actually effects the printing of the new
112 * source. It prints the label section, followed by the code section with 112 * source. It prints the label section, followed by the code section with
113 * the appropriate nesting level, followed by any comments. 113 * the appropriate nesting level, followed by any comments.
114 */ 114 */
115void 115void
116dump_line(void) 116dump_line(void)
117{ 117{
118 int cur_col; 118 int cur_col;
119 static int not_first_line; 119 static int not_first_line;
120 120
121 if (ps.procname[0]) { 121 if (ps.procname[0]) {
122 ps.ind_level = 0; 122 ps.ind_level = 0;
123 ps.procname[0] = 0; 123 ps.procname[0] = 0;
124 } 124 }
125 125
126 if (s_code == e_code && s_lab == e_lab && s_com == e_com) { 126 if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
127 if (suppress_blanklines > 0) 127 if (suppress_blanklines > 0)
128 suppress_blanklines--; 128 suppress_blanklines--;
129 else { 129 else {
130 ps.bl_line = true; 130 ps.bl_line = true;
131 n_real_blanklines++; 131 n_real_blanklines++;
132 } 132 }
133 } else if (!inhibit_formatting) { 133 } else if (!inhibit_formatting) {
134 suppress_blanklines = 0; 134 suppress_blanklines = 0;
135 ps.bl_line = false; 135 ps.bl_line = false;
136 if (prefix_blankline_requested && not_first_line) { 136 if (prefix_blankline_requested && not_first_line) {
137 if (opt.swallow_optional_blanklines) { 137 if (opt.swallow_optional_blanklines) {
138 if (n_real_blanklines == 1) 138 if (n_real_blanklines == 1)
139 n_real_blanklines = 0; 139 n_real_blanklines = 0;
140 } else { 140 } else {
141 if (n_real_blanklines == 0) 141 if (n_real_blanklines == 0)
142 n_real_blanklines = 1; 142 n_real_blanklines = 1;
143 } 143 }
144 } 144 }
145 while (--n_real_blanklines >= 0) 145 while (--n_real_blanklines >= 0)
146 output_char('\n'); 146 output_char('\n');
147 n_real_blanklines = 0; 147 n_real_blanklines = 0;
148 if (ps.ind_level == 0) 148 if (ps.ind_level == 0)
149 ps.ind_stmt = 0; /* this is a class A kludge. dont do 149 ps.ind_stmt = 0; /* this is a class A kludge. dont do
150 * additional statement indentation if we are 150 * additional statement indentation if we are
151 * at bracket level 0 */ 151 * at bracket level 0 */
152 152
153 if (e_lab != s_lab || e_code != s_code) 153 if (e_lab != s_lab || e_code != s_code)
154 ++code_lines; /* keep count of lines with code */ 154 ++code_lines; /* keep count of lines with code */
155 155
156 156
157 if (e_lab != s_lab) { /* print lab, if any */ 157 if (e_lab != s_lab) { /* print lab, if any */
158 if (comment_open) { 158 if (comment_open) {
159 comment_open = 0; 159 comment_open = 0;
160 output_string(".*/\n"); 160 output_string(".*/\n");
161 } 161 }
162 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) 162 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
163 e_lab--; 163 e_lab--;
164 *e_lab = '\0'; 164 *e_lab = '\0';
165 cur_col = 1 + output_indent(0, compute_label_indent()); 165 cur_col = 1 + output_indent(0, compute_label_indent());
166 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0 166 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
167 || strncmp(s_lab, "#endif", 6) == 0)) { 167 || strncmp(s_lab, "#endif", 6) == 0)) {
168 char *s = s_lab; 168 char *s = s_lab;
169 if (e_lab[-1] == '\n') e_lab--; 169 if (e_lab[-1] == '\n') e_lab--;
170 do { 170 do {
171 output_char(*s++); 171 output_char(*s++);
172 } while (s < e_lab && 'a' <= *s && *s <= 'z'); 172 } while (s < e_lab && 'a' <= *s && *s <= 'z');
173 while ((*s == ' ' || *s == '\t') && s < e_lab) 173 while ((*s == ' ' || *s == '\t') && s < e_lab)
174 s++; 174 s++;
175 if (s < e_lab) { 175 if (s < e_lab) {
176 if (s[0] == '/' && s[1] == '*') { 176 if (s[0] == '/' && s[1] == '*') {
177 output_char('\t'); 177 output_char('\t');
178 output_range(s, e_lab); 178 output_range(s, e_lab);
179 } else { 179 } else {
180 output_string("\t/* "); 180 output_string("\t/* ");
181 output_range(s, e_lab); 181 output_range(s, e_lab);
182 output_string(" */"); 182 output_string(" */");
183 } 183 }
184 } 184 }
185 } else 185 } else
186 output_range(s_lab, e_lab); 186 output_range(s_lab, e_lab);
187 cur_col = 1 + indentation_after(cur_col - 1, s_lab); 187 cur_col = 1 + indentation_after(cur_col - 1, s_lab);
188 } else 188 } else
189 cur_col = 1; /* there is no label section */ 189 cur_col = 1; /* there is no label section */
190 190
191 ps.pcase = false; 191 ps.pcase = false;
192 192
193 if (s_code != e_code) { /* print code section, if any */ 193 if (s_code != e_code) { /* print code section, if any */
194 if (comment_open) { 194 if (comment_open) {
195 comment_open = 0; 195 comment_open = 0;
196 output_string(".*/\n"); 196 output_string(".*/\n");
197 } 197 }
198 int target_col = 1 + compute_code_indent(); 198 int target_col = 1 + compute_code_indent();
199 { 199 {
200 int i; 200 int i;
201 201
202 for (i = 0; i < ps.p_l_follow; i++) { 202 for (i = 0; i < ps.p_l_follow; i++) {
203 if (ps.paren_indents[i] >= 0) { 203 if (ps.paren_indents[i] >= 0) {
204 int ind = ps.paren_indents[i]; 204 int ind = ps.paren_indents[i];
205 /* 205 /*
206 * XXX: this mix of 'indent' and 'column' smells like 206 * XXX: this mix of 'indent' and 'column' smells like
207 * an off-by-one error. 207 * an off-by-one error.
208 */ 208 */
209 ps.paren_indents[i] = -(ind + target_col); 209 ps.paren_indents[i] = -(ind + target_col);
210 debug_println( 210 debug_println(
211 "setting pi[%d] from %d to %d for column %d", 211 "setting pi[%d] from %d to %d for column %d",
212 i, ind, ps.paren_indents[i], target_col); 212 i, ind, ps.paren_indents[i], target_col);
213 } 213 }
214 } 214 }
215 } 215 }
216 cur_col = 1 + output_indent(cur_col - 1, target_col - 1); 216 cur_col = 1 + output_indent(cur_col - 1, target_col - 1);
217 output_range(s_code, e_code); 217 output_range(s_code, e_code);
218 cur_col = 1 + indentation_after(cur_col - 1, s_code); 218 cur_col = 1 + indentation_after(cur_col - 1, s_code);
219 } 219 }
220 if (s_com != e_com) { /* print comment, if any */ 220 if (s_com != e_com) { /* print comment, if any */
221 int target = ps.com_col; 221 int target_col = ps.com_col;
222 char *com_st = s_com; 222 char *com_st = s_com;
223 223
224 target += ps.comment_delta; 224 target_col += ps.comment_delta;
225 while (*com_st == '\t') /* consider original indentation in 225 while (*com_st == '\t') /* consider original indentation in
226 * case this is a box comment */ 226 * case this is a box comment */
227 com_st++, target += opt.tabsize; 227 com_st++, target_col += opt.tabsize;
228 while (target <= 0) 228 while (target_col <= 0)
229 if (*com_st == ' ') 229 if (*com_st == ' ')
230 target++, com_st++; 230 target_col++, com_st++;
231 else if (*com_st == '\t') { 231 else if (*com_st == '\t') {
232 target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1; 232 target_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
233 com_st++; 233 com_st++;
234 } else 234 } else
235 target = 1; 235 target_col = 1;
236 if (cur_col > target) { /* if comment can't fit on this line, 236 if (cur_col > target_col) { /* if comment can't fit on this line,
237 * put it on next line */ 237 * put it on next line */
238 output_char('\n'); 238 output_char('\n');
239 cur_col = 1; 239 cur_col = 1;
240 ++ps.out_lines; 240 ++ps.out_lines;
241 } 241 }
242 while (e_com > com_st && isspace((unsigned char)e_com[-1])) 242 while (e_com > com_st && isspace((unsigned char)e_com[-1]))
243 e_com--; 243 e_com--;
244 (void)output_indent(cur_col - 1, target - 1); 244 (void)output_indent(cur_col - 1, target_col - 1);
245 output_range(com_st, e_com); 245 output_range(com_st, e_com);
246 ps.comment_delta = ps.n_comment_delta; 246 ps.comment_delta = ps.n_comment_delta;
247 ++ps.com_lines; /* count lines with comments */ 247 ++ps.com_lines; /* count lines with comments */
248 } 248 }
249 if (ps.use_ff) 249 if (ps.use_ff)
250 output_char('\014'); 250 output_char('\014');
251 else 251 else
252 output_char('\n'); 252 output_char('\n');
253 ++ps.out_lines; 253 ++ps.out_lines;
254 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) { 254 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
255 prefix_blankline_requested = 1; 255 prefix_blankline_requested = 1;
256 ps.just_saw_decl = 0; 256 ps.just_saw_decl = 0;
257 } else 257 } else
258 prefix_blankline_requested = postfix_blankline_requested; 258 prefix_blankline_requested = postfix_blankline_requested;
259 postfix_blankline_requested = 0; 259 postfix_blankline_requested = 0;
260 } 260 }
261 261
262 /* keep blank lines after '//' comments */ 262 /* keep blank lines after '//' comments */
263 if (e_com - s_com > 1 && s_com[1] == '/') 263 if (e_com - s_com > 1 && s_com[1] == '/')
264 output_range(s_token, e_token); 264 output_range(s_token, e_token);
265 265
266 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a 266 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
267 * declaration, remember that fact for 267 * declaration, remember that fact for
268 * proper comment indentation */ 268 * proper comment indentation */
269 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be 269 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
270 * indented if we have not 270 * indented if we have not
271 * completed this stmt and if 271 * completed this stmt and if
272 * we are not in the middle of 272 * we are not in the middle of
273 * a declaration */ 273 * a declaration */
274 ps.use_ff = false; 274 ps.use_ff = false;
275 ps.dumped_decl_indent = 0; 275 ps.dumped_decl_indent = 0;
276 *(e_lab = s_lab) = '\0'; /* reset buffers */ 276 *(e_lab = s_lab) = '\0'; /* reset buffers */
277 *(e_code = s_code) = '\0'; 277 *(e_code = s_code) = '\0';
278 *(e_com = s_com = combuf + 1) = '\0'; 278 *(e_com = s_com = combuf + 1) = '\0';
279 ps.ind_level = ps.i_l_follow; 279 ps.ind_level = ps.i_l_follow;
280 ps.paren_level = ps.p_l_follow; 280 ps.paren_level = ps.p_l_follow;
281 if (ps.paren_level > 0) { 281 if (ps.paren_level > 0) {
282 /* TODO: explain what negative indentation means */ 282 /* TODO: explain what negative indentation means */
283 paren_indent = -ps.paren_indents[ps.paren_level - 1]; 283 paren_indent = -ps.paren_indents[ps.paren_level - 1];
284 debug_println("paren_indent is now %d", paren_indent); 284 debug_println("paren_indent is now %d", paren_indent);
285 } 285 }
286 not_first_line = 1; 286 not_first_line = 1;
287} 287}
288 288
289int 289int
290compute_code_indent(void) 290compute_code_indent(void)
291{ 291{
292 int target_ind = opt.indent_size * ps.ind_level; 292 int target_ind = opt.indent_size * ps.ind_level;
293 293
294 if (ps.paren_level != 0) { 294 if (ps.paren_level != 0) {
295 if (!opt.lineup_to_parens) 295 if (!opt.lineup_to_parens)
296 target_ind += opt.continuation_indent * 296 target_ind += opt.continuation_indent *
297 (2 * opt.continuation_indent == opt.indent_size ? 1 : ps.paren_level); 297 (2 * opt.continuation_indent == opt.indent_size ? 1 : ps.paren_level);
298 else if (opt.lineup_to_parens_always) 298 else if (opt.lineup_to_parens_always)
299 /* 299 /*
300 * XXX: where does this '- 1' come from? It looks strange but is 300 * XXX: where does this '- 1' come from? It looks strange but is
301 * nevertheless needed for proper indentation, as demonstrated in 301 * nevertheless needed for proper indentation, as demonstrated in
302 * the test opt-lpl.0. 302 * the test opt-lpl.0.
303 */ 303 */
304 target_ind = paren_indent - 1; 304 target_ind = paren_indent - 1;
305 else { 305 else {
306 int w; 306 int w;
307 int t = paren_indent; 307 int t = paren_indent;
308 308
309 if ((w = 1 + indentation_after(t - 1, s_code) - opt.max_line_length) > 0 309 if ((w = 1 + indentation_after(t - 1, s_code) - opt.max_line_length) > 0
310 && 1 + indentation_after(target_ind, s_code) <= opt.max_line_length) { 310 && 1 + indentation_after(target_ind, s_code) <= opt.max_line_length) {
311 t -= w + 1; 311 t -= w + 1;
312 if (t > target_ind + 1) 312 if (t > target_ind + 1)
313 target_ind = t - 1; 313 target_ind = t - 1;
314 } else 314 } else
315 target_ind = t - 1; 315 target_ind = t - 1;
316 } 316 }
317 } else if (ps.ind_stmt) 317 } else if (ps.ind_stmt)
318 target_ind += opt.continuation_indent; 318 target_ind += opt.continuation_indent;
319 return target_ind; 319 return target_ind;
320} 320}
321 321
322int 322int
323compute_label_indent(void) 323compute_label_indent(void)
324{ 324{
325 if (ps.pcase) 325 if (ps.pcase)
326 return (int) (case_ind * opt.indent_size); 326 return (int) (case_ind * opt.indent_size);
327 if (s_lab[0] == '#') 327 if (s_lab[0] == '#')
328 return 0; 328 return 0;
329 return opt.indent_size * (ps.ind_level - label_offset); 329 return opt.indent_size * (ps.ind_level - label_offset);
330} 330}
331 331
332 332
333/* 333/*
334 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois 334 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
335 * 335 *
336 * All rights reserved 336 * All rights reserved
337 * 337 *
338 * FUNCTION: Reads one block of input into the input buffer 338 * FUNCTION: Reads one block of input into the input buffer
339 */ 339 */
340void 340void
341fill_buffer(void) 341fill_buffer(void)
342{ /* this routine reads stuff from the input */ 342{ /* this routine reads stuff from the input */
343 char *p; 343 char *p;
344 int i; 344 int i;
345 FILE *f = input; 345 FILE *f = input;
346 346
347 if (bp_save != NULL) { /* there is a partly filled input buffer left */ 347 if (bp_save != NULL) { /* there is a partly filled input buffer left */
348 buf_ptr = bp_save; /* do not read anything, just switch buffers */ 348 buf_ptr = bp_save; /* do not read anything, just switch buffers */
349 buf_end = be_save; 349 buf_end = be_save;
350 bp_save = be_save = NULL; 350 bp_save = be_save = NULL;
351 if (buf_ptr < buf_end) 351 if (buf_ptr < buf_end)
352 return; /* only return if there is really something in 352 return; /* only return if there is really something in
353 * this buffer */ 353 * this buffer */
354 } 354 }
355 for (p = in_buffer;;) { 355 for (p = in_buffer;;) {
356 if (p >= in_buffer_limit) { 356 if (p >= in_buffer_limit) {
357 int size = (in_buffer_limit - in_buffer) * 2 + 10; 357 int size = (in_buffer_limit - in_buffer) * 2 + 10;
358 int offset = p - in_buffer; 358 int offset = p - in_buffer;
359 in_buffer = realloc(in_buffer, size); 359 in_buffer = realloc(in_buffer, size);
360 if (in_buffer == NULL) 360 if (in_buffer == NULL)
361 errx(1, "input line too long"); 361 errx(1, "input line too long");
362 p = in_buffer + offset; 362 p = in_buffer + offset;
363 in_buffer_limit = in_buffer + size - 2; 363 in_buffer_limit = in_buffer + size - 2;
364 } 364 }
365 if ((i = getc(f)) == EOF) { 365 if ((i = getc(f)) == EOF) {
366 *p++ = ' '; 366 *p++ = ' ';
367 *p++ = '\n'; 367 *p++ = '\n';
368 had_eof = true; 368 had_eof = true;
369 break; 369 break;
370 } 370 }
371 if (i != '\0') 371 if (i != '\0')
372 *p++ = i; 372 *p++ = i;
373 if (i == '\n') 373 if (i == '\n')
374 break; 374 break;
375 } 375 }
376 buf_ptr = in_buffer; 376 buf_ptr = in_buffer;
377 buf_end = p; 377 buf_end = p;
378 if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') { 378 if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
379 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0) 379 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
380 fill_buffer(); /* flush indent error message */ 380 fill_buffer(); /* flush indent error message */
381 else { 381 else {
382 int com = 0; 382 int com = 0;
383 383
384 p = in_buffer; 384 p = in_buffer;
385 while (*p == ' ' || *p == '\t') 385 while (*p == ' ' || *p == '\t')
386 p++; 386 p++;
387 if (*p == '/' && p[1] == '*') { 387 if (*p == '/' && p[1] == '*') {
388 p += 2; 388 p += 2;
389 while (*p == ' ' || *p == '\t') 389 while (*p == ' ' || *p == '\t')
390 p++; 390 p++;
391 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E' 391 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
392 && p[4] == 'N' && p[5] == 'T') { 392 && p[4] == 'N' && p[5] == 'T') {
393 p += 6; 393 p += 6;
394 while (*p == ' ' || *p == '\t') 394 while (*p == ' ' || *p == '\t')
395 p++; 395 p++;
396 if (*p == '*') 396 if (*p == '*')
397 com = 1; 397 com = 1;
398 else if (*p == 'O') { 398 else if (*p == 'O') {
399 if (*++p == 'N') 399 if (*++p == 'N')
400 p++, com = 1; 400 p++, com = 1;
401 else if (*p == 'F' && *++p == 'F') 401 else if (*p == 'F' && *++p == 'F')
402 p++, com = 2; 402 p++, com = 2;
403 } 403 }
404 while (*p == ' ' || *p == '\t') 404 while (*p == ' ' || *p == '\t')
405 p++; 405 p++;
406 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) { 406 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
407 if (s_com != e_com || s_lab != e_lab || s_code != e_code) 407 if (s_com != e_com || s_lab != e_lab || s_code != e_code)
408 dump_line(); 408 dump_line();
409 if (!(inhibit_formatting = com - 1)) { 409 if (!(inhibit_formatting = com - 1)) {
410 n_real_blanklines = 0; 410 n_real_blanklines = 0;
411 postfix_blankline_requested = 0; 411 postfix_blankline_requested = 0;
412 prefix_blankline_requested = 0; 412 prefix_blankline_requested = 0;
413 suppress_blanklines = 1; 413 suppress_blanklines = 1;
414 } 414 }
415 } 415 }
416 } 416 }
417 } 417 }
418 } 418 }
419 } 419 }
420 if (inhibit_formatting) { 420 if (inhibit_formatting) {
421 p = in_buffer; 421 p = in_buffer;
422 do { 422 do {
423 output_char(*p); 423 output_char(*p);
424 } while (*p++ != '\n'); 424 } while (*p++ != '\n');
425 } 425 }
426} 426}
427 427
428int 428int
429indentation_after_range(int ind, const char *start, const char *end) 429indentation_after_range(int ind, const char *start, const char *end)
430{ 430{
431 for (const char *p = start; *p != '\0' && p != end; ++p) { 431 for (const char *p = start; *p != '\0' && p != end; ++p) {
432 if (*p == '\n' || *p == '\f') 432 if (*p == '\n' || *p == '\f')
433 ind = 0; 433 ind = 0;
434 else if (*p == '\t') 434 else if (*p == '\t')
435 ind = opt.tabsize * (ind / opt.tabsize + 1); 435 ind = opt.tabsize * (ind / opt.tabsize + 1);
436 else if (*p == '\b') 436 else if (*p == '\b')
437 --ind; 437 --ind;
438 else 438 else
439 ++ind; 439 ++ind;
440 } 440 }
441 return ind; 441 return ind;
442} 442}
443 443
444int 444int
445indentation_after(int ind, const char *s) 445indentation_after(int ind, const char *s)
446{ 446{
447 return indentation_after_range(ind, s, NULL); 447 return indentation_after_range(ind, s, NULL);
448} 448}
449 449
450void 450void
451diag(int level, const char *msg, ...) 451diag(int level, const char *msg, ...)
452{ 452{
453 va_list ap; 453 va_list ap;
454 const char *s, *e; 454 const char *s, *e;
455 455
456 if (level) 456 if (level)
457 found_err = 1; 457 found_err = 1;
458 458
459 if (output == stdout) { 459 if (output == stdout) {
460 s = "/**INDENT** "; 460 s = "/**INDENT** ";
461 e = " */"; 461 e = " */";
462 } else { 462 } else {
463 s = e = ""; 463 s = e = "";
464 } 464 }
465 465
466 va_start(ap, msg); 466 va_start(ap, msg);
467 fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no); 467 fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
468 vfprintf(stderr, msg, ap); 468 vfprintf(stderr, msg, ap);
469 fprintf(stderr, "%s\n", e); 469 fprintf(stderr, "%s\n", e);
470 va_end(ap); 470 va_end(ap);
471} 471}