io.c revision 1.34 1 /* $NetBSD: io.c,v 1.34 2021/03/13 00:26:56 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 #ifndef lint
42 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
43 #endif /* not lint */
44 #endif
45
46 #include <sys/cdefs.h>
47 #ifndef lint
48 #if defined(__NetBSD__)
49 __RCSID("$NetBSD: io.c,v 1.34 2021/03/13 00:26:56 rillig Exp $");
50 #elif defined(__FreeBSD__)
51 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
52 #endif
53 #endif
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdarg.h>
61
62 #include "indent.h"
63
64 int comment_open;
65 static int paren_indent;
66
67 static void
68 output_char(char ch)
69 {
70 fputc(ch, output);
71 }
72
73 static void
74 output_range(const char *s, const char *e)
75 {
76 fwrite(s, 1, (size_t)(e - s), output);
77 }
78
79 static inline void
80 output_string(const char *s)
81 {
82 output_range(s, s + strlen(s));
83 }
84
85 static void
86 output_int(int i)
87 {
88 fprintf(output, "%d", i);
89 }
90
91 static int
92 output_indent(int old_ind, int new_ind)
93 {
94 int ind = old_ind;
95
96 if (opt.use_tabs) {
97 int tabsize = opt.tabsize;
98 int n = new_ind / tabsize - ind / tabsize;
99 if (n > 0)
100 ind -= ind % tabsize;
101 for (int i = 0; i < n; i++) {
102 output_char('\t');
103 ind += tabsize;
104 }
105 }
106
107 for (; ind < new_ind; ind++)
108 output_char(' ');
109
110 return ind;
111 }
112
113 /*
114 * dump_line is the routine that actually effects the printing of the new
115 * source. It prints the label section, followed by the code section with
116 * the appropriate nesting level, followed by any comments.
117 */
118 void
119 dump_line(void)
120 {
121 int cur_col, target_col;
122 static int not_first_line;
123
124 if (ps.procname[0]) {
125 ps.ind_level = 0;
126 ps.procname[0] = 0;
127 }
128
129 if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
130 if (suppress_blanklines > 0)
131 suppress_blanklines--;
132 else {
133 ps.bl_line = true;
134 n_real_blanklines++;
135 }
136 } else if (!inhibit_formatting) {
137 suppress_blanklines = 0;
138 ps.bl_line = false;
139 if (prefix_blankline_requested && not_first_line) {
140 if (opt.swallow_optional_blanklines) {
141 if (n_real_blanklines == 1)
142 n_real_blanklines = 0;
143 } else {
144 if (n_real_blanklines == 0)
145 n_real_blanklines = 1;
146 }
147 }
148 while (--n_real_blanklines >= 0)
149 output_char('\n');
150 n_real_blanklines = 0;
151 if (ps.ind_level == 0)
152 ps.ind_stmt = 0; /* this is a class A kludge. dont do
153 * additional statement indentation if we are
154 * at bracket level 0 */
155
156 if (e_lab != s_lab || e_code != s_code)
157 ++code_lines; /* keep count of lines with code */
158
159
160 if (e_lab != s_lab) { /* print lab, if any */
161 if (comment_open) {
162 comment_open = 0;
163 output_string(".*/\n");
164 }
165 while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
166 e_lab--;
167 *e_lab = '\0';
168 cur_col = 1 + output_indent(0, compute_label_column() - 1);
169 if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
170 || strncmp(s_lab, "#endif", 6) == 0)) {
171 char *s = s_lab;
172 if (e_lab[-1] == '\n') e_lab--;
173 do {
174 output_char(*s++);
175 } while (s < e_lab && 'a' <= *s && *s <= 'z');
176 while ((*s == ' ' || *s == '\t') && s < e_lab)
177 s++;
178 if (s < e_lab) {
179 if (s[0] == '/' && s[1] == '*') {
180 output_char('\t');
181 output_range(s, e_lab);
182 } else {
183 output_string("\t/* ");
184 output_range(s, e_lab);
185 output_string(" */");
186 }
187 }
188 } else
189 output_range(s_lab, e_lab);
190 cur_col = count_spaces(cur_col, s_lab);
191 } else
192 cur_col = 1; /* there is no label section */
193
194 ps.pcase = false;
195
196 if (s_code != e_code) { /* print code section, if any */
197 char *p;
198
199 if (comment_open) {
200 comment_open = 0;
201 output_string(".*/\n");
202 }
203 target_col = compute_code_column();
204 {
205 int i;
206
207 for (i = 0; i < ps.p_l_follow; i++)
208 if (ps.paren_indents[i] >= 0)
209 ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
210 }
211 cur_col = 1 + output_indent(cur_col - 1, target_col - 1);
212 for (p = s_code; p < e_code; p++)
213 if (*p == (char) 0200)
214 output_int(target_col * 7);
215 else
216 output_char(*p);
217 cur_col = count_spaces(cur_col, s_code);
218 }
219 if (s_com != e_com) { /* print comment, if any */
220 int target = ps.com_col;
221 char *com_st = s_com;
222
223 target += ps.comment_delta;
224 while (*com_st == '\t') /* consider original indentation in
225 * case this is a box comment */
226 com_st++, target += opt.tabsize;
227 while (target <= 0)
228 if (*com_st == ' ')
229 target++, com_st++;
230 else if (*com_st == '\t') {
231 target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
232 com_st++;
233 } else
234 target = 1;
235 if (cur_col > target) { /* if comment can't fit on this line,
236 * put it on next line */
237 output_char('\n');
238 cur_col = 1;
239 ++ps.out_lines;
240 }
241 while (e_com > com_st && isspace((unsigned char)e_com[-1]))
242 e_com--;
243 (void)output_indent(cur_col - 1, target - 1);
244 output_range(com_st, e_com);
245 ps.comment_delta = ps.n_comment_delta;
246 ++ps.com_lines; /* count lines with comments */
247 }
248 if (ps.use_ff)
249 output_char('\014');
250 else
251 output_char('\n');
252 ++ps.out_lines;
253 if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
254 prefix_blankline_requested = 1;
255 ps.just_saw_decl = 0;
256 } else
257 prefix_blankline_requested = postfix_blankline_requested;
258 postfix_blankline_requested = 0;
259 }
260
261 /* keep blank lines after '//' comments */
262 if (e_com - s_com > 1 && s_com[1] == '/')
263 output_range(s_token, e_token);
264
265 ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
266 * declaration, remember that fact for
267 * proper comment indentation */
268 ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
269 * indented if we have not
270 * completed this stmt and if
271 * we are not in the middle of
272 * a declaration */
273 ps.use_ff = false;
274 ps.dumped_decl_indent = 0;
275 *(e_lab = s_lab) = '\0'; /* reset buffers */
276 *(e_code = s_code) = '\0';
277 *(e_com = s_com = combuf + 1) = '\0';
278 ps.ind_level = ps.i_l_follow;
279 ps.paren_level = ps.p_l_follow;
280 if (ps.paren_level > 0)
281 paren_indent = -ps.paren_indents[ps.paren_level - 1];
282 not_first_line = 1;
283 }
284
285 int
286 compute_code_column(void)
287 {
288 int target_col = opt.ind_size * ps.ind_level + 1;
289
290 if (ps.paren_level) {
291 if (!opt.lineup_to_parens)
292 target_col += opt.continuation_indent *
293 (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
294 else if (opt.lineup_to_parens_always)
295 target_col = paren_indent;
296 else {
297 int w;
298 int t = paren_indent;
299
300 if ((w = count_spaces(t, s_code) - opt.max_col) > 0
301 && count_spaces(target_col, s_code) <= opt.max_col) {
302 t -= w + 1;
303 if (t > target_col)
304 target_col = t;
305 } else
306 target_col = t;
307 }
308 } else if (ps.ind_stmt)
309 target_col += opt.continuation_indent;
310 return target_col;
311 }
312
313 int
314 compute_label_column(void)
315 {
316 return
317 ps.pcase ? (int) (case_ind * opt.ind_size) + 1
318 : *s_lab == '#' ? 1
319 : opt.ind_size * (ps.ind_level - label_offset) + 1;
320 }
321
322
323 /*
324 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
325 *
326 * All rights reserved
327 *
328 * FUNCTION: Reads one block of input into the input buffer
329 */
330 void
331 fill_buffer(void)
332 { /* this routine reads stuff from the input */
333 char *p;
334 int i;
335 FILE *f = input;
336
337 if (bp_save != NULL) { /* there is a partly filled input buffer left */
338 buf_ptr = bp_save; /* do not read anything, just switch buffers */
339 buf_end = be_save;
340 bp_save = be_save = NULL;
341 if (buf_ptr < buf_end)
342 return; /* only return if there is really something in
343 * this buffer */
344 }
345 for (p = in_buffer;;) {
346 if (p >= in_buffer_limit) {
347 int size = (in_buffer_limit - in_buffer) * 2 + 10;
348 int offset = p - in_buffer;
349 in_buffer = realloc(in_buffer, size);
350 if (in_buffer == NULL)
351 errx(1, "input line too long");
352 p = in_buffer + offset;
353 in_buffer_limit = in_buffer + size - 2;
354 }
355 if ((i = getc(f)) == EOF) {
356 *p++ = ' ';
357 *p++ = '\n';
358 had_eof = true;
359 break;
360 }
361 if (i != '\0')
362 *p++ = i;
363 if (i == '\n')
364 break;
365 }
366 buf_ptr = in_buffer;
367 buf_end = p;
368 if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
369 if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
370 fill_buffer(); /* flush indent error message */
371 else {
372 int com = 0;
373
374 p = in_buffer;
375 while (*p == ' ' || *p == '\t')
376 p++;
377 if (*p == '/' && p[1] == '*') {
378 p += 2;
379 while (*p == ' ' || *p == '\t')
380 p++;
381 if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
382 && p[4] == 'N' && p[5] == 'T') {
383 p += 6;
384 while (*p == ' ' || *p == '\t')
385 p++;
386 if (*p == '*')
387 com = 1;
388 else if (*p == 'O') {
389 if (*++p == 'N')
390 p++, com = 1;
391 else if (*p == 'F' && *++p == 'F')
392 p++, com = 2;
393 }
394 while (*p == ' ' || *p == '\t')
395 p++;
396 if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
397 if (s_com != e_com || s_lab != e_lab || s_code != e_code)
398 dump_line();
399 if (!(inhibit_formatting = com - 1)) {
400 n_real_blanklines = 0;
401 postfix_blankline_requested = 0;
402 prefix_blankline_requested = 0;
403 suppress_blanklines = 1;
404 }
405 }
406 }
407 }
408 }
409 }
410 if (inhibit_formatting) {
411 p = in_buffer;
412 do {
413 output_char(*p);
414 } while (*p++ != '\n');
415 }
416 }
417
418 /*
419 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
420 *
421 * All rights reserved
422 */
423 int
424 count_spaces_until(int col, const char *buffer, const char *end)
425 {
426 for (const char *p = buffer; *p != '\0' && p != end; ++p) {
427 switch (*p) {
428
429 case '\n':
430 case 014: /* form feed */
431 col = 1;
432 break;
433
434 case '\t':
435 col = 1 + opt.tabsize * ((col - 1) / opt.tabsize + 1);
436 break;
437
438 case 010: /* backspace */
439 --col;
440 break;
441
442 default:
443 ++col;
444 break;
445 } /* end of switch */
446 } /* end of for loop */
447 return col;
448 }
449
450 int
451 count_spaces(int col, const char *buffer)
452 {
453 return count_spaces_until(col, buffer, NULL);
454 }
455
456 void
457 diag(int level, const char *msg, ...)
458 {
459 va_list ap;
460 const char *s, *e;
461
462 if (level)
463 found_err = 1;
464
465 if (output == stdout) {
466 s = "/**INDENT** ";
467 e = " */";
468 } else {
469 s = e = "";
470 }
471
472 va_start(ap, msg);
473 fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
474 vfprintf(stderr, msg, ap);
475 fprintf(stderr, "%s\n", e);
476 va_end(ap);
477 }
478