io.c revision 1.206 1 /* $NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and 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
25 * without specific prior written permission.
26 *
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
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
37 * SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: io.c,v 1.206 2023/06/09 07:20:30 rillig Exp $");
42
43 #include <stdio.h>
44
45 #include "indent.h"
46
47 struct buffer inp;
48 const char *inp_p;
49
50 struct output_state out;
51 static int out_ind; /* width of the line that is being written */
52 static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
53 * single '\n', > 1 means there were (n
54 * - 1) blank lines above */
55 static int paren_indent;
56
57
58 static void
59 inp_read_next_line(FILE *f)
60 {
61 inp.len = 0;
62
63 for (;;) {
64 int ch = getc(f);
65 if (ch == EOF) {
66 if (indent_enabled == indent_on) {
67 buf_add_char(&inp, ' ');
68 buf_add_char(&inp, '\n');
69 }
70 had_eof = true;
71 break;
72 }
73
74 if (ch != '\0')
75 buf_add_char(&inp, (char)ch);
76 if (ch == '\n')
77 break;
78 }
79 inp_p = inp.s;
80 }
81
82 void
83 inp_read_line(void)
84 {
85 if (indent_enabled == indent_on)
86 out.indent_off_text.len = 0;
87 buf_add_chars(&out.indent_off_text, inp.s, inp.len);
88 inp_read_next_line(input);
89 }
90
91 void
92 inp_skip(void)
93 {
94 inp_p++;
95 if ((size_t)(inp_p - inp.s) >= inp.len)
96 inp_read_line();
97 }
98
99 char
100 inp_next(void)
101 {
102 char ch = inp_p[0];
103 inp_skip();
104 return ch;
105 }
106
107
108 static void
109 output_newline(void)
110 {
111 fputc('\n', output);
112 debug_println("output_newline");
113 wrote_newlines++;
114 out_ind = 0;
115 }
116
117 static void
118 output_range(const char *s, size_t len)
119 {
120 fwrite(s, 1, len, output);
121 debug_vis_range("output_range \"", s, len, "\"\n");
122 for (size_t i = 0; i < len; i++)
123 wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
124 out_ind = ind_add(out_ind, s, len);
125 }
126
127 static void
128 output_indent(int new_ind)
129 {
130 int ind = out_ind;
131
132 if (opt.use_tabs) {
133 int n = new_ind / opt.tabsize - ind / opt.tabsize;
134 if (n > 0) {
135 ind = ind - ind % opt.tabsize + n * opt.tabsize;
136 while (n-- > 0)
137 fputc('\t', output);
138 wrote_newlines = 0;
139 }
140 }
141
142 for (; ind < new_ind; ind++) {
143 fputc(' ', output);
144 wrote_newlines = 0;
145 }
146
147 debug_println("output_indent %d", ind);
148 out_ind = ind;
149 }
150
151 static bool
152 want_blank_line(void)
153 {
154 debug_println("%s: %s -> %s", __func__,
155 line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
156
157 if (ps.blank_line_after_decl && ps.declaration == decl_no) {
158 ps.blank_line_after_decl = false;
159 return true;
160 }
161 if (opt.blanklines_around_conditional_compilation) {
162 if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
163 return true;
164 if (out.prev_line_kind == lk_endif
165 && out.line_kind != lk_endif)
166 return true;
167 }
168 if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
169 && out.line_kind != lk_endif)
170 return true;
171 if (opt.blanklines_before_block_comments
172 && out.line_kind == lk_block_comment)
173 return true;
174 return false;
175 }
176
177 static bool
178 is_blank_line_optional(void)
179 {
180 if (out.prev_line_kind == lk_stmt_head)
181 return wrote_newlines >= 1;
182 if (ps.psyms.top >= 2)
183 return wrote_newlines >= 2;
184 return wrote_newlines >= 3;
185 }
186
187 static int
188 compute_case_label_indent(void)
189 {
190 int i = ps.psyms.top;
191 while (i > 0 && ps.psyms.sym[i] != psym_switch_expr)
192 i--;
193 float case_ind = (float)ps.psyms.ind_level[i] + opt.case_indent;
194 return (int)(case_ind * (float)opt.indent_size);
195 }
196
197 int
198 compute_label_indent(void)
199 {
200 if (out.line_kind == lk_case_or_default)
201 return compute_case_label_indent();
202 if (lab.s[0] == '#')
203 return 0;
204 return opt.indent_size * (ps.ind_level - 2);
205 }
206
207 static void
208 output_line_label(void)
209 {
210 output_indent(compute_label_indent());
211 output_range(lab.s, lab.len);
212 }
213
214 static int
215 compute_code_indent_lineup(int base_ind)
216 {
217 int ind = paren_indent;
218 int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
219 if (overflow < 0)
220 return ind;
221
222 if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
223 ind -= overflow + 2;
224 if (ind > base_ind)
225 return ind;
226 return base_ind;
227 }
228
229 return ind;
230 }
231
232 int
233 compute_code_indent(void)
234 {
235 int base_ind = ps.ind_level * opt.indent_size;
236
237 if (ps.line_start_nparen == 0) {
238 if (ps.psyms.top >= 1
239 && ps.psyms.sym[ps.psyms.top - 1] == psym_lbrace_enum)
240 return base_ind;
241 if (ps.in_stmt_cont)
242 return base_ind + opt.continuation_indent;
243 return base_ind;
244 }
245
246 if (opt.lineup_to_parens) {
247 if (opt.lineup_to_parens_always)
248 return paren_indent;
249 return compute_code_indent_lineup(base_ind);
250 }
251
252 if (ps.extra_expr_indent != eei_no)
253 return base_ind + 2 * opt.continuation_indent;
254
255 return base_ind + opt.continuation_indent * ps.line_start_nparen;
256 }
257
258 static void
259 output_line_code(void)
260 {
261 int target_ind = compute_code_indent();
262 for (int i = 0; i < ps.nparen; i++) {
263 int paren_ind = ps.paren[i].indent;
264 if (paren_ind >= 0) {
265 ps.paren[i].indent = -1 - (paren_ind + target_ind);
266 debug_println(
267 "setting paren_indents[%d] from %d to %d "
268 "for column %d",
269 i, paren_ind, ps.paren[i].indent, target_ind + 1);
270 }
271 }
272
273 if (lab.len > 0 && target_ind <= out_ind)
274 output_range(" ", 1);
275 output_indent(target_ind);
276 output_range(code.s, code.len);
277 }
278
279 static void
280 output_line_comment(void)
281 {
282 int target_ind = ps.com_ind + ps.comment_delta;
283 const char *p;
284
285 /* consider original indentation in case this is a box comment */
286 for (p = com.s; *p == '\t'; p++)
287 target_ind += opt.tabsize;
288
289 for (; target_ind < 0; p++) {
290 if (*p == ' ')
291 target_ind++;
292 else if (*p == '\t')
293 target_ind = next_tab(target_ind);
294 else {
295 target_ind = 0;
296 break;
297 }
298 }
299
300 if (out_ind > target_ind)
301 output_newline();
302
303 while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
304 com.len--;
305
306 output_indent(target_ind);
307 output_range(p, com.len - (size_t)(p - com.s));
308
309 ps.comment_delta = ps.n_comment_delta;
310 }
311
312 /*
313 * Write a line of formatted source to the output file. The line consists of
314 * the label, the code and the comment.
315 */
316 void
317 output_line(void)
318 {
319 debug_blank_line();
320 debug_printf("%s", __func__);
321 debug_buffers();
322
323 ps.is_function_definition = false;
324
325 if (indent_enabled == indent_on) {
326 if (lab.len == 0 && code.len == 0 && com.len == 0)
327 out.line_kind = lk_blank;
328
329 if (want_blank_line() && wrote_newlines < 2
330 && out.line_kind != lk_blank)
331 output_newline();
332
333 /* This kludge aligns function definitions correctly. */
334 if (ps.ind_level == 0)
335 ps.in_stmt_cont = false;
336
337 if (opt.blank_line_after_decl && ps.declaration == decl_end
338 && ps.psyms.top > 1) {
339 ps.declaration = decl_no;
340 ps.blank_line_after_decl = true;
341 }
342
343 if (opt.swallow_optional_blanklines
344 && out.line_kind == lk_blank
345 && is_blank_line_optional())
346 goto prepare_next_line;
347
348 if (lab.len > 0)
349 output_line_label();
350 if (code.len > 0)
351 output_line_code();
352 if (com.len > 0)
353 output_line_comment();
354
355 output_newline();
356 out.prev_line_kind = out.line_kind;
357 }
358
359 if (indent_enabled == indent_last_off_line) {
360 indent_enabled = indent_on;
361 output_range(out.indent_off_text.s, out.indent_off_text.len);
362 out.indent_off_text.len = 0;
363 }
364
365 prepare_next_line:
366 lab.len = 0;
367 code.len = 0;
368 com.len = 0;
369
370 ps.decl_on_line = ps.in_decl;
371 // XXX: don't reset in_stmt_cont here; see process_colon_question.
372 ps.in_stmt_cont = ps.in_stmt_or_decl
373 && !ps.in_decl && ps.block_init_level == 0;
374 ps.decl_indent_done = false;
375 if (ps.extra_expr_indent == eei_last)
376 ps.extra_expr_indent = eei_no;
377 if (!(ps.psyms.sym[ps.psyms.top] == psym_if_expr_stmt_else
378 && ps.nparen > 0))
379 ps.ind_level = ps.ind_level_follow;
380 ps.line_start_nparen = ps.nparen;
381 ps.want_blank = false;
382
383 if (ps.nparen > 0) {
384 /* TODO: explain what negative indentation means */
385 paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
386 debug_println("paren_indent is now %d", paren_indent);
387 }
388
389 out.line_kind = lk_other;
390 }
391