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