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