io.c revision 1.173 1 1.173 rillig /* $NetBSD: io.c,v 1.173 2023/05/16 08:04:03 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.173 rillig __RCSID("$NetBSD: io.c,v 1.173 2023/05/16 08:04:03 rillig Exp $");
42 1.1 cgd
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <string.h>
45 1.22 rillig
46 1.19 kamil #include "indent.h"
47 1.1 cgd
48 1.143 rillig /*
49 1.154 rillig * The current line, ready to be split into tokens, terminated with '\n'. The
50 1.157 rillig * current read position is inp.s, and the invariant inp.s < inp.e holds.
51 1.143 rillig */
52 1.154 rillig static struct buffer inp;
53 1.173 rillig static struct buffer indent_off_text;
54 1.120 rillig
55 1.67 rillig static int paren_indent;
56 1.1 cgd
57 1.118 rillig
58 1.119 rillig const char *
59 1.119 rillig inp_p(void)
60 1.119 rillig {
61 1.166 rillig return inp.st;
62 1.119 rillig }
63 1.119 rillig
64 1.119 rillig const char *
65 1.123 rillig inp_line_start(void)
66 1.123 rillig {
67 1.157 rillig return inp.mem;
68 1.123 rillig }
69 1.123 rillig
70 1.118 rillig char
71 1.118 rillig inp_peek(void)
72 1.118 rillig {
73 1.166 rillig return *inp.st;
74 1.118 rillig }
75 1.118 rillig
76 1.118 rillig char
77 1.118 rillig inp_lookahead(size_t i)
78 1.118 rillig {
79 1.166 rillig return inp.st[i];
80 1.118 rillig }
81 1.118 rillig
82 1.118 rillig void
83 1.118 rillig inp_skip(void)
84 1.118 rillig {
85 1.166 rillig inp.st++;
86 1.166 rillig if ((size_t)(inp.st - inp.mem) >= inp.len)
87 1.118 rillig inp_read_line();
88 1.118 rillig }
89 1.118 rillig
90 1.118 rillig char
91 1.118 rillig inp_next(void)
92 1.118 rillig {
93 1.118 rillig char ch = inp_peek();
94 1.118 rillig inp_skip();
95 1.118 rillig return ch;
96 1.118 rillig }
97 1.118 rillig
98 1.138 rillig static void
99 1.137 rillig inp_read_next_line(FILE *f)
100 1.137 rillig {
101 1.166 rillig inp.st = inp.mem;
102 1.166 rillig inp.len = 0;
103 1.137 rillig
104 1.138 rillig for (;;) {
105 1.138 rillig int ch = getc(f);
106 1.138 rillig if (ch == EOF) {
107 1.173 rillig if (indent_enabled == indent_on) {
108 1.166 rillig buf_add_char(&inp, ' ');
109 1.166 rillig buf_add_char(&inp, '\n');
110 1.137 rillig }
111 1.137 rillig had_eof = true;
112 1.137 rillig break;
113 1.137 rillig }
114 1.137 rillig
115 1.137 rillig if (ch != '\0')
116 1.166 rillig buf_add_char(&inp, (char)ch);
117 1.137 rillig if (ch == '\n')
118 1.137 rillig break;
119 1.137 rillig }
120 1.137 rillig }
121 1.137 rillig
122 1.28 rillig static void
123 1.28 rillig output_char(char ch)
124 1.28 rillig {
125 1.28 rillig fputc(ch, output);
126 1.166 rillig debug_vis_range("output_char '", &ch, 1, "'\n");
127 1.28 rillig }
128 1.28 rillig
129 1.28 rillig static void
130 1.166 rillig output_range(const char *s, size_t len)
131 1.28 rillig {
132 1.166 rillig fwrite(s, 1, len, output);
133 1.166 rillig debug_vis_range("output_range \"", s, len, "\"\n");
134 1.28 rillig }
135 1.28 rillig
136 1.34 rillig static int
137 1.34 rillig output_indent(int old_ind, int new_ind)
138 1.34 rillig {
139 1.34 rillig int ind = old_ind;
140 1.34 rillig
141 1.34 rillig if (opt.use_tabs) {
142 1.34 rillig int tabsize = opt.tabsize;
143 1.34 rillig int n = new_ind / tabsize - ind / tabsize;
144 1.34 rillig if (n > 0)
145 1.34 rillig ind -= ind % tabsize;
146 1.34 rillig for (int i = 0; i < n; i++) {
147 1.36 rillig fputc('\t', output);
148 1.34 rillig ind += tabsize;
149 1.34 rillig }
150 1.34 rillig }
151 1.34 rillig
152 1.34 rillig for (; ind < new_ind; ind++)
153 1.67 rillig fputc(' ', output);
154 1.34 rillig
155 1.36 rillig debug_println("output_indent %d", ind);
156 1.34 rillig return ind;
157 1.34 rillig }
158 1.34 rillig
159 1.86 rillig static int
160 1.141 rillig output_line_label(void)
161 1.86 rillig {
162 1.86 rillig int ind;
163 1.86 rillig
164 1.166 rillig while (lab.len > 0 && ch_isblank(lab.mem[lab.len - 1]))
165 1.166 rillig lab.len--;
166 1.86 rillig
167 1.86 rillig ind = output_indent(0, compute_label_indent());
168 1.166 rillig output_range(lab.st, lab.len);
169 1.166 rillig ind = ind_add(ind, lab.st, lab.len);
170 1.86 rillig
171 1.86 rillig ps.is_case_label = false;
172 1.86 rillig return ind;
173 1.86 rillig }
174 1.86 rillig
175 1.86 rillig static int
176 1.141 rillig output_line_code(int ind)
177 1.86 rillig {
178 1.86 rillig
179 1.86 rillig int target_ind = compute_code_indent();
180 1.146 rillig for (int i = 0; i < ps.nparen; i++) {
181 1.171 rillig int paren_ind = ps.paren[i].indent;
182 1.171 rillig if (paren_ind >= 0) {
183 1.145 rillig ps.paren[i].indent = (short)(-1 - (paren_ind + target_ind));
184 1.86 rillig debug_println(
185 1.105 rillig "setting paren_indents[%d] from %d to %d for column %d",
186 1.145 rillig i, paren_ind, ps.paren[i].indent, target_ind + 1);
187 1.86 rillig }
188 1.86 rillig }
189 1.86 rillig
190 1.86 rillig ind = output_indent(ind, target_ind);
191 1.166 rillig output_range(code.st, code.len);
192 1.166 rillig return ind_add(ind, code.st, code.len);
193 1.86 rillig }
194 1.86 rillig
195 1.86 rillig static void
196 1.141 rillig output_line_comment(int ind)
197 1.86 rillig {
198 1.86 rillig int target_ind = ps.com_ind;
199 1.166 rillig const char *p = com.st;
200 1.86 rillig
201 1.86 rillig target_ind += ps.comment_delta;
202 1.86 rillig
203 1.86 rillig /* consider original indentation in case this is a box comment */
204 1.92 rillig for (; *p == '\t'; p++)
205 1.92 rillig target_ind += opt.tabsize;
206 1.86 rillig
207 1.92 rillig for (; target_ind < 0; p++) {
208 1.92 rillig if (*p == ' ')
209 1.92 rillig target_ind++;
210 1.92 rillig else if (*p == '\t')
211 1.91 rillig target_ind = next_tab(target_ind);
212 1.92 rillig else {
213 1.86 rillig target_ind = 0;
214 1.92 rillig break;
215 1.92 rillig }
216 1.86 rillig }
217 1.86 rillig
218 1.105 rillig /* if comment can't fit on this line, put it on the next line */
219 1.86 rillig if (ind > target_ind) {
220 1.86 rillig output_char('\n');
221 1.86 rillig ind = 0;
222 1.86 rillig }
223 1.86 rillig
224 1.166 rillig while (com.mem + com.len > p && ch_isspace(com.mem[com.len - 1]))
225 1.166 rillig com.len--;
226 1.86 rillig
227 1.86 rillig (void)output_indent(ind, target_ind);
228 1.166 rillig output_range(p, com.len - (size_t)(p - com.mem));
229 1.86 rillig
230 1.86 rillig ps.comment_delta = ps.n_comment_delta;
231 1.86 rillig }
232 1.86 rillig
233 1.26 rillig /*
234 1.105 rillig * Write a line of formatted source to the output file. The line consists of
235 1.105 rillig * the label, the code and the comment.
236 1.156 rillig *
237 1.156 rillig * Comments are written directly, bypassing this function.
238 1.26 rillig */
239 1.101 rillig static void
240 1.141 rillig output_complete_line(char line_terminator)
241 1.26 rillig {
242 1.156 rillig debug_printf("%s", __func__);
243 1.156 rillig debug_buffers();
244 1.156 rillig debug_println("%s", line_terminator == '\f' ? " form_feed" : "");
245 1.156 rillig
246 1.125 rillig ps.is_function_definition = false;
247 1.30 rillig
248 1.160 rillig if (ps.blank_line_after_decl && ps.declaration == decl_no) {
249 1.160 rillig ps.blank_line_after_decl = false;
250 1.166 rillig if (lab.len > 0 || code.len > 0 || com.len > 0)
251 1.160 rillig output_char('\n');
252 1.160 rillig }
253 1.160 rillig
254 1.173 rillig if (indent_enabled == indent_on) {
255 1.19 kamil if (ps.ind_level == 0)
256 1.130 rillig ps.in_stmt_cont = false; /* this is a class A kludge */
257 1.19 kamil
258 1.160 rillig if (opt.blank_line_after_decl && ps.declaration == decl_end
259 1.170 rillig && ps.tos > 1) {
260 1.160 rillig ps.declaration = decl_no;
261 1.160 rillig ps.blank_line_after_decl = true;
262 1.160 rillig }
263 1.160 rillig
264 1.86 rillig int ind = 0;
265 1.166 rillig if (lab.len > 0)
266 1.141 rillig ind = output_line_label();
267 1.166 rillig if (code.len > 0)
268 1.141 rillig ind = output_line_code(ind);
269 1.166 rillig if (com.len > 0)
270 1.141 rillig output_line_comment(ind);
271 1.77 rillig
272 1.101 rillig output_char(line_terminator);
273 1.19 kamil }
274 1.24 rillig
275 1.173 rillig if (indent_enabled == indent_last_off_line) {
276 1.173 rillig indent_enabled = indent_on;
277 1.173 rillig output_range(indent_off_text.st, indent_off_text.len);
278 1.173 rillig indent_off_text.len = 0;
279 1.173 rillig }
280 1.173 rillig
281 1.76 rillig ps.decl_on_line = ps.in_decl; /* for proper comment indentation */
282 1.131 rillig ps.in_stmt_cont = ps.in_stmt_or_decl && !ps.in_decl;
283 1.104 rillig ps.decl_indent_done = false;
284 1.169 rillig if (ps.extra_expr_indent == eei_last)
285 1.169 rillig ps.extra_expr_indent = eei_no;
286 1.77 rillig
287 1.166 rillig lab.len = 0;
288 1.166 rillig code.len = 0;
289 1.166 rillig com.len = 0;
290 1.77 rillig
291 1.64 rillig ps.ind_level = ps.ind_level_follow;
292 1.146 rillig ps.line_start_nparen = ps.nparen;
293 1.77 rillig
294 1.147 rillig if (ps.nparen > 0) {
295 1.67 rillig /* TODO: explain what negative indentation means */
296 1.147 rillig paren_indent = -1 - ps.paren[ps.nparen - 1].indent;
297 1.36 rillig debug_println("paren_indent is now %d", paren_indent);
298 1.36 rillig }
299 1.161 rillig
300 1.161 rillig ps.want_blank = false;
301 1.1 cgd }
302 1.1 cgd
303 1.101 rillig void
304 1.141 rillig output_line(void)
305 1.101 rillig {
306 1.141 rillig output_complete_line('\n');
307 1.101 rillig }
308 1.101 rillig
309 1.101 rillig void
310 1.141 rillig output_line_ff(void)
311 1.101 rillig {
312 1.141 rillig output_complete_line('\f');
313 1.101 rillig }
314 1.101 rillig
315 1.113 rillig static int
316 1.113 rillig compute_code_indent_lineup(int base_ind)
317 1.113 rillig {
318 1.162 rillig int ind = paren_indent;
319 1.166 rillig int overflow = ind_add(ind, code.st, code.len) - opt.max_line_length;
320 1.113 rillig if (overflow < 0)
321 1.162 rillig return ind;
322 1.113 rillig
323 1.166 rillig if (ind_add(base_ind, code.st, code.len) < opt.max_line_length) {
324 1.162 rillig ind -= overflow + 2;
325 1.162 rillig if (ind > base_ind)
326 1.162 rillig return ind;
327 1.113 rillig return base_ind;
328 1.113 rillig }
329 1.113 rillig
330 1.162 rillig return ind;
331 1.113 rillig }
332 1.113 rillig
333 1.5 lukem int
334 1.39 rillig compute_code_indent(void)
335 1.1 cgd {
336 1.110 rillig int base_ind = ps.ind_level * opt.indent_size;
337 1.19 kamil
338 1.146 rillig if (ps.line_start_nparen == 0) {
339 1.144 rillig if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
340 1.110 rillig return base_ind + opt.continuation_indent;
341 1.110 rillig return base_ind;
342 1.110 rillig }
343 1.77 rillig
344 1.110 rillig if (opt.lineup_to_parens) {
345 1.112 rillig if (opt.lineup_to_parens_always)
346 1.112 rillig return paren_indent;
347 1.113 rillig return compute_code_indent_lineup(base_ind);
348 1.110 rillig }
349 1.77 rillig
350 1.169 rillig if (ps.extra_expr_indent != eei_no)
351 1.168 rillig return base_ind + 2 * opt.continuation_indent;
352 1.168 rillig
353 1.110 rillig if (2 * opt.continuation_indent == opt.indent_size)
354 1.110 rillig return base_ind + opt.continuation_indent;
355 1.110 rillig else
356 1.146 rillig return base_ind + opt.continuation_indent * ps.line_start_nparen;
357 1.1 cgd }
358 1.1 cgd
359 1.5 lukem int
360 1.38 rillig compute_label_indent(void)
361 1.1 cgd {
362 1.75 rillig if (ps.is_case_label)
363 1.74 rillig return (int)(case_ind * (float)opt.indent_size);
364 1.166 rillig if (lab.st[0] == '#')
365 1.67 rillig return 0;
366 1.108 rillig return opt.indent_size * (ps.ind_level - 2);
367 1.1 cgd }
368 1.1 cgd
369 1.5 lukem void
370 1.116 rillig inp_read_line(void)
371 1.67 rillig {
372 1.173 rillig if (indent_enabled == indent_on)
373 1.173 rillig indent_off_text.len = 0;
374 1.173 rillig buf_add_chars(&indent_off_text, inp.mem, inp.len);
375 1.137 rillig inp_read_next_line(input);
376 1.173 rillig }
377 1.77 rillig
378 1.173 rillig void
379 1.173 rillig clear_indent_off_text(void)
380 1.173 rillig {
381 1.173 rillig indent_off_text.len = 0;
382 1.1 cgd }
383