io.c revision 1.198 1 /* $NetBSD: io.c,v 1.198 2023/06/05 12:06:51 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.198 2023/06/05 12:06:51 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 void
59 inp_skip(void)
60 {
61 inp_p++;
62 if ((size_t)(inp_p - inp.s) >= inp.len)
63 inp_read_line();
64 }
65
66 char
67 inp_next(void)
68 {
69 char ch = inp_p[0];
70 inp_skip();
71 return ch;
72 }
73
74 static void
75 inp_read_next_line(FILE *f)
76 {
77 inp.len = 0;
78
79 for (;;) {
80 int ch = getc(f);
81 if (ch == EOF) {
82 if (indent_enabled == indent_on) {
83 buf_add_char(&inp, ' ');
84 buf_add_char(&inp, '\n');
85 }
86 had_eof = true;
87 break;
88 }
89
90 if (ch != '\0')
91 buf_add_char(&inp, (char)ch);
92 if (ch == '\n')
93 break;
94 }
95 inp_p = inp.s;
96 }
97
98 static void
99 output_newline(void)
100 {
101 fputc('\n', output);
102 debug_println("output_newline");
103 wrote_newlines++;
104 out_ind = 0;
105 }
106
107 static void
108 output_range(const char *s, size_t len)
109 {
110 fwrite(s, 1, len, output);
111 debug_vis_range("output_range \"", s, len, "\"\n");
112 for (size_t i = 0; i < len; i++)
113 wrote_newlines = s[i] == '\n' ? wrote_newlines + 1 : 0;
114 out_ind = ind_add(out_ind, s, len);
115 }
116
117 static void
118 output_indent(int new_ind)
119 {
120 int ind = out_ind;
121
122 if (opt.use_tabs) {
123 int tabsize = opt.tabsize;
124 int n = new_ind / tabsize - ind / tabsize;
125 if (n > 0)
126 ind -= ind % tabsize;
127 for (int i = 0; i < n; i++) {
128 fputc('\t', output);
129 ind += tabsize;
130 wrote_newlines = 0;
131 }
132 }
133
134 for (; ind < new_ind; ind++) {
135 fputc(' ', output);
136 wrote_newlines = 0;
137 }
138
139 debug_println("output_indent %d", ind);
140 out_ind = ind;
141 }
142
143 static bool
144 want_blank_line(void)
145 {
146 debug_println("%s: %s -> %s", __func__,
147 line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
148
149 if (ps.blank_line_after_decl && ps.declaration == decl_no) {
150 ps.blank_line_after_decl = false;
151 return true;
152 }
153 if (opt.blanklines_around_conditional_compilation) {
154 if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
155 return true;
156 if (out.prev_line_kind == lk_endif
157 && out.line_kind != lk_endif)
158 return true;
159 }
160 if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
161 && out.line_kind != lk_endif)
162 return true;
163 if (opt.blanklines_before_block_comments
164 && out.line_kind == lk_block_comment)
165 return true;
166 return false;
167 }
168
169 static bool
170 is_blank_line_optional(void)
171 {
172 if (out.prev_line_kind == lk_stmt_head)
173 return wrote_newlines >= 1;
174 if (ps.tos >= 2)
175 return wrote_newlines >= 2;
176 return wrote_newlines >= 3;
177 }
178
179 static void
180 output_line_label(void)
181 {
182 output_indent(compute_label_indent());
183 output_range(lab.s, lab.len);
184 }
185
186 static void
187 output_line_code(void)
188 {
189 int target_ind = compute_code_indent();
190 for (int i = 0; i < ps.nparen; i++) {
191 int paren_ind = ps.paren[i].indent;
192 if (paren_ind >= 0) {
193 ps.paren[i].indent = -1 - (paren_ind + target_ind);
194 debug_println(
195 "setting paren_indents[%d] from %d to %d "
196 "for column %d",
197 i, paren_ind, ps.paren[i].indent, target_ind + 1);
198 }
199 }
200
201 if (lab.len > 0 && target_ind <= out_ind)
202 output_range(" ", 1);
203 output_indent(target_ind);
204 output_range(code.s, code.len);
205 }
206
207 static void
208 output_line_comment(void)
209 {
210 int target_ind = ps.com_ind + ps.comment_delta;
211 const char *p;
212
213 /* consider original indentation in case this is a box comment */
214 for (p = com.s; *p == '\t'; p++)
215 target_ind += opt.tabsize;
216
217 for (; target_ind < 0; p++) {
218 if (*p == ' ')
219 target_ind++;
220 else if (*p == '\t')
221 target_ind = next_tab(target_ind);
222 else {
223 target_ind = 0;
224 break;
225 }
226 }
227
228 if (out_ind > target_ind)
229 output_newline();
230
231 while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
232 com.len--;
233
234 output_indent(target_ind);
235 output_range(p, com.len - (size_t)(p - com.s));
236
237 ps.comment_delta = ps.n_comment_delta;
238 }
239
240 /*
241 * Write a line of formatted source to the output file. The line consists of
242 * the label, the code and the comment.
243 */
244 void
245 output_line(void)
246 {
247 debug_blank_line();
248 debug_printf("%s", __func__);
249 debug_buffers();
250
251 ps.is_function_definition = false;
252
253 if (indent_enabled == indent_on) {
254 if (lab.len == 0 && code.len == 0 && com.len == 0)
255 out.line_kind = lk_blank;
256
257 if (want_blank_line() && wrote_newlines < 2
258 && out.line_kind != lk_blank)
259 output_newline();
260
261 /* This kludge aligns function definitions correctly. */
262 if (ps.ind_level == 0)
263 ps.in_stmt_cont = false;
264
265 if (opt.blank_line_after_decl && ps.declaration == decl_end
266 && ps.tos > 1) {
267 ps.declaration = decl_no;
268 ps.blank_line_after_decl = true;
269 }
270
271 if (opt.swallow_optional_blanklines
272 && out.line_kind == lk_blank
273 && is_blank_line_optional())
274 goto dont_write_line;
275
276 if (lab.len > 0)
277 output_line_label();
278 if (code.len > 0)
279 output_line_code();
280 if (com.len > 0)
281 output_line_comment();
282
283 output_newline();
284 out.prev_line_kind = out.line_kind;
285 }
286
287 if (indent_enabled == indent_last_off_line) {
288 indent_enabled = indent_on;
289 output_range(out.indent_off_text.s, out.indent_off_text.len);
290 out.indent_off_text.len = 0;
291 }
292
293 dont_write_line:
294 ps.decl_on_line = ps.in_decl; /* for proper comment indentation */
295 ps.in_stmt_cont = ps.in_stmt_or_decl
296 && !ps.in_decl && ps.block_init_level == 0;
297 ps.decl_indent_done = false;
298 if (ps.extra_expr_indent == eei_last)
299 ps.extra_expr_indent = eei_no;
300
301 lab.len = 0;
302 code.len = 0;
303 com.len = 0;
304
305 ps.ind_level = ps.ind_level_follow;
306 ps.line_start_nparen = ps.nparen;
307
308 if (ps.nparen > 0) {
309 /* TODO: explain what negative indentation means */
310 paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
311 debug_println("paren_indent is now %d", paren_indent);
312 }
313
314 ps.want_blank = false;
315 out.line_kind = lk_other;
316 }
317
318 static int
319 compute_code_indent_lineup(int base_ind)
320 {
321 int ind = paren_indent;
322 int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
323 if (overflow < 0)
324 return ind;
325
326 if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
327 ind -= overflow + 2;
328 if (ind > base_ind)
329 return ind;
330 return base_ind;
331 }
332
333 return ind;
334 }
335
336 int
337 compute_code_indent(void)
338 {
339 int base_ind = ps.ind_level * opt.indent_size;
340
341 if (ps.line_start_nparen == 0) {
342 if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
343 return base_ind;
344 if (ps.in_stmt_cont)
345 return base_ind + opt.continuation_indent;
346 return base_ind;
347 }
348
349 if (opt.lineup_to_parens) {
350 if (opt.lineup_to_parens_always)
351 return paren_indent;
352 return compute_code_indent_lineup(base_ind);
353 }
354
355 if (ps.extra_expr_indent != eei_no)
356 return base_ind + 2 * opt.continuation_indent;
357
358 if (2 * opt.continuation_indent == opt.indent_size)
359 return base_ind + opt.continuation_indent;
360 else
361 return base_ind +
362 opt.continuation_indent * ps.line_start_nparen;
363 }
364
365 int
366 compute_label_indent(void)
367 {
368 if (out.line_kind == lk_case_or_default)
369 return (int)(case_ind * (float)opt.indent_size);
370 if (lab.s[0] == '#')
371 return 0;
372 return opt.indent_size * (ps.ind_level - 2);
373 }
374
375 void
376 inp_read_line(void)
377 {
378 if (indent_enabled == indent_on)
379 out.indent_off_text.len = 0;
380 buf_add_chars(&out.indent_off_text, inp.s, inp.len);
381 inp_read_next_line(input);
382 }
383