io.c revision 1.115 1 /* $NetBSD: io.c,v 1.115 2021/11/05 21:39:12 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.115 2021/11/05 21:39:12 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 <ctype.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <string.h>
55
56 #include "indent.h"
57
58 static int paren_indent;
59 static bool suppress_blanklines;
60
61 static void
62 output_char(char ch)
63 {
64 fputc(ch, output);
65 debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
66 }
67
68 static void
69 output_range(const char *s, const char *e)
70 {
71 fwrite(s, 1, (size_t)(e - s), output);
72 debug_vis_range("output_range \"", s, e, "\"\n");
73 }
74
75 static inline void
76 output_string(const char *s)
77 {
78 output_range(s, s + strlen(s));
79 }
80
81 static int
82 output_indent(int old_ind, int new_ind)
83 {
84 int ind = old_ind;
85
86 if (opt.use_tabs) {
87 int tabsize = opt.tabsize;
88 int n = new_ind / tabsize - ind / tabsize;
89 if (n > 0)
90 ind -= ind % tabsize;
91 for (int i = 0; i < n; i++) {
92 fputc('\t', output);
93 ind += tabsize;
94 }
95 }
96
97 for (; ind < new_ind; ind++)
98 fputc(' ', output);
99
100 debug_println("output_indent %d", ind);
101 return ind;
102 }
103
104 static int
105 dump_line_label(void)
106 {
107 int ind;
108
109 while (lab.e > lab.s && ch_isblank(lab.e[-1]))
110 lab.e--;
111 *lab.e = '\0';
112
113 ind = output_indent(0, compute_label_indent());
114
115 if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0
116 || strncmp(lab.s, "#endif", 6) == 0)) {
117 const char *s = lab.s;
118 if (lab.e[-1] == '\n')
119 lab.e--;
120 do {
121 output_char(*s++);
122 } while (s < lab.e && 'a' <= *s && *s <= 'z');
123
124 while (s < lab.e && ch_isblank(*s))
125 s++;
126
127 if (s < lab.e) {
128 if (s[0] == '/' && s[1] == '*') {
129 output_char('\t');
130 output_range(s, lab.e);
131 } else {
132 output_string("\t/* ");
133 output_range(s, lab.e);
134 output_string(" */");
135 }
136 }
137 } else
138 output_range(lab.s, lab.e);
139 ind = ind_add(ind, lab.s, lab.e);
140
141 ps.is_case_label = false;
142 return ind;
143 }
144
145 static int
146 dump_line_code(int ind)
147 {
148
149 int target_ind = compute_code_indent();
150 for (int i = 0; i < ps.p_l_follow; i++) {
151 if (ps.paren_indents[i] >= 0) {
152 int paren_ind = ps.paren_indents[i];
153 ps.paren_indents[i] = (short)(-1 - (paren_ind + target_ind));
154 debug_println(
155 "setting paren_indents[%d] from %d to %d for column %d",
156 i, paren_ind, ps.paren_indents[i], target_ind + 1);
157 }
158 }
159
160 ind = output_indent(ind, target_ind);
161 output_range(code.s, code.e);
162 return ind_add(ind, code.s, code.e);
163 }
164
165 static void
166 dump_line_comment(int ind)
167 {
168 int target_ind = ps.com_ind;
169 const char *p = com.s;
170
171 target_ind += ps.comment_delta;
172
173 /* consider original indentation in case this is a box comment */
174 for (; *p == '\t'; p++)
175 target_ind += opt.tabsize;
176
177 for (; target_ind < 0; p++) {
178 if (*p == ' ')
179 target_ind++;
180 else if (*p == '\t')
181 target_ind = next_tab(target_ind);
182 else {
183 target_ind = 0;
184 break;
185 }
186 }
187
188 /* if comment can't fit on this line, put it on the next line */
189 if (ind > target_ind) {
190 output_char('\n');
191 ind = 0;
192 ps.stats.lines++;
193 }
194
195 while (com.e > p && isspace((unsigned char)com.e[-1]))
196 com.e--;
197
198 (void)output_indent(ind, target_ind);
199 output_range(p, com.e);
200
201 ps.comment_delta = ps.n_comment_delta;
202 ps.stats.comment_lines++;
203 }
204
205 /*
206 * Write a line of formatted source to the output file. The line consists of
207 * the label, the code and the comment.
208 */
209 static void
210 output_line(char line_terminator)
211 {
212 static bool first_line = true;
213
214 ps.procname[0] = '\0';
215
216 if (code.s == code.e && lab.s == lab.e && com.s == com.e) {
217 if (suppress_blanklines)
218 suppress_blanklines = false;
219 else
220 blank_lines_to_output++;
221
222 } else if (!inhibit_formatting) {
223 suppress_blanklines = false;
224 if (blank_line_before && !first_line) {
225 if (opt.swallow_optional_blanklines) {
226 if (blank_lines_to_output == 1)
227 blank_lines_to_output = 0;
228 } else {
229 if (blank_lines_to_output == 0)
230 blank_lines_to_output = 1;
231 }
232 }
233
234 for (; blank_lines_to_output > 0; blank_lines_to_output--)
235 output_char('\n');
236
237 if (ps.ind_level == 0)
238 ps.ind_stmt = false; /* this is a class A kludge. don't do
239 * additional statement indentation if
240 * we are at bracket level 0 */
241
242 if (lab.e != lab.s || code.e != code.s)
243 ps.stats.code_lines++;
244
245 int ind = 0;
246 if (lab.e != lab.s)
247 ind = dump_line_label();
248 if (code.e != code.s)
249 ind = dump_line_code(ind);
250 if (com.e != com.s)
251 dump_line_comment(ind);
252
253 output_char(line_terminator);
254 ps.stats.lines++;
255
256 if (ps.just_saw_decl == 1 && opt.blanklines_after_decl) {
257 blank_line_before = true;
258 ps.just_saw_decl = 0;
259 } else
260 blank_line_before = blank_line_after;
261 blank_line_after = false;
262 }
263
264 ps.decl_on_line = ps.in_decl; /* for proper comment indentation */
265 ps.ind_stmt = ps.in_stmt && !ps.in_decl;
266 ps.decl_indent_done = false;
267
268 *(lab.e = lab.s) = '\0'; /* reset buffers */
269 *(code.e = code.s) = '\0';
270 *(com.e = com.s = com.buf + 1) = '\0';
271
272 ps.ind_level = ps.ind_level_follow;
273 ps.paren_level = ps.p_l_follow;
274
275 if (ps.paren_level > 0) {
276 /* TODO: explain what negative indentation means */
277 paren_indent = -1 - ps.paren_indents[ps.paren_level - 1];
278 debug_println("paren_indent is now %d", paren_indent);
279 }
280
281 first_line = false;
282 }
283
284 void
285 dump_line(void)
286 {
287 output_line('\n');
288 }
289
290 void
291 dump_line_ff(void)
292 {
293 output_line('\f');
294 }
295
296 static int
297 compute_code_indent_lineup(int base_ind)
298 {
299 int ti = paren_indent;
300 int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
301 if (overflow < 0)
302 return ti;
303
304 if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
305 ti -= overflow + 2;
306 if (ti > base_ind)
307 return ti;
308 return base_ind;
309 }
310
311 return ti;
312 }
313
314 int
315 compute_code_indent(void)
316 {
317 int base_ind = ps.ind_level * opt.indent_size;
318
319 if (ps.paren_level == 0) {
320 if (ps.ind_stmt)
321 return base_ind + opt.continuation_indent;
322 return base_ind;
323 }
324
325 if (opt.lineup_to_parens) {
326 if (opt.lineup_to_parens_always)
327 return paren_indent;
328 return compute_code_indent_lineup(base_ind);
329 }
330
331 if (2 * opt.continuation_indent == opt.indent_size)
332 return base_ind + opt.continuation_indent;
333 else
334 return base_ind + opt.continuation_indent * ps.paren_level;
335 }
336
337 int
338 compute_label_indent(void)
339 {
340 if (ps.is_case_label)
341 return (int)(case_ind * (float)opt.indent_size);
342 if (lab.s[0] == '#')
343 return 0;
344 return opt.indent_size * (ps.ind_level - 2);
345 }
346
347 static void
348 skip_blank(const char **pp)
349 {
350 while (ch_isblank(**pp))
351 (*pp)++;
352 }
353
354 static bool
355 skip_string(const char **pp, const char *s)
356 {
357 size_t len = strlen(s);
358 if (strncmp(*pp, s, len) == 0) {
359 *pp += len;
360 return true;
361 }
362 return false;
363 }
364
365 static void
366 parse_indent_comment(void)
367 {
368 bool on;
369
370 const char *p = inp.buf;
371
372 skip_blank(&p);
373 if (!skip_string(&p, "/*"))
374 return;
375 skip_blank(&p);
376 if (!skip_string(&p, "INDENT"))
377 return;
378 skip_blank(&p);
379
380 if (*p == '*' || skip_string(&p, "ON"))
381 on = true;
382 else if (skip_string(&p, "OFF"))
383 on = false;
384 else
385 return;
386
387 skip_blank(&p);
388 if (!skip_string(&p, "*/\n"))
389 return;
390
391 if (com.s != com.e || lab.s != lab.e || code.s != code.e)
392 dump_line();
393
394 inhibit_formatting = !on;
395 if (on) {
396 blank_lines_to_output = 0;
397 blank_line_after = false;
398 blank_line_before = false;
399 suppress_blanklines = true;
400 }
401 }
402
403 /*
404 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
405 *
406 * All rights reserved
407 */
408 void
409 inbuf_read_line(void)
410 {
411 char *p;
412 int ch;
413 FILE *f = input;
414
415 if (saved_inp_s != NULL) { /* there is a partly filled input buffer left */
416 inp.s = saved_inp_s; /* do not read anything, just switch buffers */
417 inp.e = saved_inp_e;
418 saved_inp_s = saved_inp_e = NULL;
419 debug_println("switched inp.s back to saved_inp_s");
420 if (inp.s < inp.e)
421 return; /* only return if there is really something in
422 * this buffer */
423 }
424
425 for (p = inp.buf;;) {
426 if (p >= inp.l) {
427 size_t size = (size_t)(inp.l - inp.buf) * 2 + 10;
428 size_t offset = (size_t)(p - inp.buf);
429 inp.buf = xrealloc(inp.buf, size);
430 p = inp.buf + offset;
431 inp.l = inp.buf + size - 2;
432 }
433
434 if ((ch = getc(f)) == EOF) {
435 if (!inhibit_formatting) {
436 *p++ = ' ';
437 *p++ = '\n';
438 }
439 had_eof = true;
440 break;
441 }
442
443 if (ch != '\0')
444 *p++ = (char)ch;
445 if (ch == '\n')
446 break;
447 }
448
449 inp.s = inp.buf;
450 inp.e = p;
451
452 if (p - inp.s >= 3 && p[-3] == '*' && p[-2] == '/')
453 parse_indent_comment();
454
455 if (inhibit_formatting)
456 output_range(inp.s, inp.e);
457 }
458
459 int
460 ind_add(int ind, const char *start, const char *end)
461 {
462 for (const char *p = start; p != end; ++p) {
463 if (*p == '\n' || *p == '\f')
464 ind = 0;
465 else if (*p == '\t')
466 ind = next_tab(ind);
467 else if (*p == '\b')
468 --ind;
469 else
470 ++ind;
471 }
472 return ind;
473 }
474