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