xref: /freebsd/usr.bin/indent/io.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1985 Sun Microsystems, Inc.
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "indent_globs.h"
45 #include "indent.h"
46 
47 /* Globals */
48 int	found_err;
49 int	n_real_blanklines;
50 int	prefix_blankline_requested, postfix_blankline_requested;
51 int	code_lines;
52 int	had_eof;
53 int	inhibit_formatting;
54 int	suppress_blanklines;
55 
56 int         comment_open;
57 static int  paren_target;
58 static int pad_output(int current, int target);
59 
60 void
dump_line(void)61 dump_line(void)
62 {				/* dump_line is the routine that actually
63 				 * effects the printing of the new source. It
64 				 * prints the label section, followed by the
65 				 * code section with the appropriate nesting
66 				 * level, followed by any comments */
67     int cur_col,
68                 target_col = 1;
69     static int  not_first_line;
70 
71     if (ps.procname[0]) {
72 	ps.ind_level = 0;
73 	ps.procname[0] = 0;
74     }
75     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
76 	if (suppress_blanklines > 0)
77 	    suppress_blanklines--;
78 	else {
79 	    ps.bl_line = true;
80 	    n_real_blanklines++;
81 	}
82     }
83     else if (!inhibit_formatting) {
84 	suppress_blanklines = 0;
85 	ps.bl_line = false;
86 	if (prefix_blankline_requested && not_first_line) {
87 	    if (opt.swallow_optional_blanklines) {
88 		if (n_real_blanklines == 1)
89 		    n_real_blanklines = 0;
90 	    }
91 	    else {
92 		if (n_real_blanklines == 0)
93 		    n_real_blanklines = 1;
94 	    }
95 	}
96 	while (--n_real_blanklines >= 0)
97 	    putc('\n', output);
98 	n_real_blanklines = 0;
99 	if (ps.ind_level == 0)
100 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
101 				 * additional statement indentation if we are
102 				 * at bracket level 0 */
103 
104 	if (e_lab != s_lab || e_code != s_code)
105 	    ++code_lines;	/* keep count of lines with code */
106 
107 
108 	if (e_lab != s_lab) {	/* print lab, if any */
109 	    if (comment_open) {
110 		comment_open = 0;
111 		fprintf(output, ".*/\n");
112 	    }
113 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
114 		e_lab--;
115 	    *e_lab = '\0';
116 	    cur_col = pad_output(1, compute_label_target());
117 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
118 				    || strncmp(s_lab, "#endif", 6) == 0)) {
119 		char *s = s_lab;
120 		if (e_lab[-1] == '\n') e_lab--;
121 		do putc(*s++, output);
122 		while (s < e_lab && 'a' <= *s && *s<='z');
123 		while ((*s == ' ' || *s == '\t') && s < e_lab)
124 		    s++;
125 		if (s < e_lab)
126 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
127 			    (int)(e_lab - s), s);
128 	    }
129 	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
130 	    cur_col = count_spaces(cur_col, s_lab);
131 	}
132 	else
133 	    cur_col = 1;	/* there is no label section */
134 
135 	ps.pcase = false;
136 
137 	if (s_code != e_code) {	/* print code section, if any */
138 	    char *p;
139 
140 	    if (comment_open) {
141 		comment_open = 0;
142 		fprintf(output, ".*/\n");
143 	    }
144 	    target_col = compute_code_target();
145 	    {
146 		int i;
147 
148 		for (i = 0; i < ps.p_l_follow; i++)
149 		    if (ps.paren_indents[i] >= 0)
150 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
151 	    }
152 	    cur_col = pad_output(cur_col, target_col);
153 	    for (p = s_code; p < e_code; p++)
154 		if (*p == (char) 0200)
155 		    fprintf(output, "%d", target_col * 7);
156 		else
157 		    putc(*p, output);
158 	    cur_col = count_spaces(cur_col, s_code);
159 	}
160 	if (s_com != e_com) {		/* print comment, if any */
161 	    int target = ps.com_col;
162 	    char *com_st = s_com;
163 
164 	    target += ps.comment_delta;
165 	    while (*com_st == '\t')	/* consider original indentation in
166 				     * case this is a box comment */
167 		com_st++, target += opt.tabsize;
168 	    while (target <= 0)
169 		if (*com_st == ' ')
170 		    target++, com_st++;
171 		else if (*com_st == '\t') {
172 		    target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
173 		    com_st++;
174 		}
175 		else
176 		    target = 1;
177 	    if (cur_col > target) {	/* if comment can't fit on this line,
178 				     * put it on next line */
179 		putc('\n', output);
180 		cur_col = 1;
181 		++ps.out_lines;
182 	    }
183 	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
184 		e_com--;
185 	    (void)pad_output(cur_col, target);
186 	    fwrite(com_st, e_com - com_st, 1, output);
187 	    ps.comment_delta = ps.n_comment_delta;
188 	    ++ps.com_lines;	/* count lines with comments */
189 	}
190 	if (ps.use_ff)
191 	    putc('\014', output);
192 	else
193 	    putc('\n', output);
194 	++ps.out_lines;
195 	if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
196 	    prefix_blankline_requested = 1;
197 	    ps.just_saw_decl = 0;
198 	}
199 	else
200 	    prefix_blankline_requested = postfix_blankline_requested;
201 	postfix_blankline_requested = 0;
202     }
203     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
204 					 * declaration, remember that fact for
205 					 * proper comment indentation */
206     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
207 						 * indented if we have not
208 						 * completed this stmt and if
209 						 * we are not in the middle of
210 						 * a declaration */
211     ps.use_ff = false;
212     ps.dumped_decl_indent = 0;
213     *(e_lab = s_lab) = '\0';	/* reset buffers */
214     *(e_code = s_code) = '\0';
215     *(e_com = s_com = combuf + 1) = '\0';
216     ps.ind_level = ps.i_l_follow;
217     ps.paren_level = ps.p_l_follow;
218     if (ps.paren_level > 0)
219 	paren_target = -ps.paren_indents[ps.paren_level - 1];
220     not_first_line = 1;
221 }
222 
223 int
compute_code_target(void)224 compute_code_target(void)
225 {
226     int target_col = opt.ind_size * ps.ind_level + 1;
227 
228     if (ps.paren_level)
229 	if (!opt.lineup_to_parens)
230 	    target_col += opt.continuation_indent *
231 		(2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
232 	else if (opt.lineup_to_parens_always)
233 	    target_col = paren_target;
234 	else {
235 	    int w;
236 	    int t = paren_target;
237 
238 	    if ((w = count_spaces(t, s_code) - opt.max_col) > 0
239 		    && count_spaces(target_col, s_code) <= opt.max_col) {
240 		t -= w + 1;
241 		if (t > target_col)
242 		    target_col = t;
243 	    }
244 	    else
245 		target_col = t;
246 	}
247     else if (ps.ind_stmt)
248 	target_col += opt.continuation_indent;
249     return target_col;
250 }
251 
252 int
compute_label_target(void)253 compute_label_target(void)
254 {
255     return
256 	ps.pcase ? (int) (case_ind * opt.ind_size) + 1
257 	: *s_lab == '#' ? 1
258 	: opt.ind_size * (ps.ind_level - label_offset) + 1;
259 }
260 
261 
262 /*
263  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
264  *
265  * All rights reserved
266  *
267  *
268  * NAME: fill_buffer
269  *
270  * FUNCTION: Reads one block of input into input_buffer
271  *
272  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
273  * Willcox of CAC	Added check for switch back to partly full input
274  * buffer from temporary buffer
275  *
276  */
277 void
fill_buffer(void)278 fill_buffer(void)
279 {				/* this routine reads stuff from the input */
280     char *p;
281     int i;
282     FILE *f = input;
283 
284     if (bp_save != NULL) {	/* there is a partly filled input buffer left */
285 	buf_ptr = bp_save;	/* do not read anything, just switch buffers */
286 	buf_end = be_save;
287 	bp_save = be_save = NULL;
288 	if (buf_ptr < buf_end)
289 	    return;		/* only return if there is really something in
290 				 * this buffer */
291     }
292     for (p = in_buffer;;) {
293 	if (p >= in_buffer_limit) {
294 	    int size = (in_buffer_limit - in_buffer) * 2 + 10;
295 	    int offset = p - in_buffer;
296 	    in_buffer = realloc(in_buffer, size);
297 	    if (in_buffer == NULL)
298 		errx(1, "input line too long");
299 	    p = in_buffer + offset;
300 	    in_buffer_limit = in_buffer + size - 2;
301 	}
302 	if ((i = getc(f)) == EOF) {
303 		*p++ = ' ';
304 		*p++ = '\n';
305 		had_eof = true;
306 		break;
307 	}
308 	if (i != '\0')
309 	    *p++ = i;
310 	if (i == '\n')
311 		break;
312     }
313     buf_ptr = in_buffer;
314     buf_end = p;
315     if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
316 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
317 	    fill_buffer();	/* flush indent error message */
318 	else {
319 	    int         com = 0;
320 
321 	    p = in_buffer;
322 	    while (*p == ' ' || *p == '\t')
323 		p++;
324 	    if (*p == '/' && p[1] == '*') {
325 		p += 2;
326 		while (*p == ' ' || *p == '\t')
327 		    p++;
328 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
329 			&& p[4] == 'N' && p[5] == 'T') {
330 		    p += 6;
331 		    while (*p == ' ' || *p == '\t')
332 			p++;
333 		    if (*p == '*')
334 			com = 1;
335 		    else if (*p == 'O') {
336 			if (*++p == 'N')
337 			    p++, com = 1;
338 			else if (*p == 'F' && *++p == 'F')
339 			    p++, com = 2;
340 		    }
341 		    while (*p == ' ' || *p == '\t')
342 			p++;
343 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
344 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
345 			    dump_line();
346 			if (!(inhibit_formatting = com - 1)) {
347 			    n_real_blanklines = 0;
348 			    postfix_blankline_requested = 0;
349 			    prefix_blankline_requested = 0;
350 			    suppress_blanklines = 1;
351 			}
352 		    }
353 		}
354 	    }
355 	}
356     }
357     if (inhibit_formatting) {
358 	p = in_buffer;
359 	do
360 	    putc(*p, output);
361 	while (*p++ != '\n');
362     }
363 }
364 
365 /*
366  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
367  *
368  * All rights reserved
369  *
370  *
371  * NAME: pad_output
372  *
373  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
374  * position.
375  *
376  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
377  *
378  * PARAMETERS: current		integer		The current column target
379  * nteger		The desired column
380  *
381  * RETURNS: Integer value of the new column.  (If current >= target, no action is
382  * taken, and current is returned.
383  *
384  * GLOBALS: None
385  *
386  * CALLS: write (sys)
387  *
388  * CALLED BY: dump_line
389  *
390  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
391  *
392  */
393 static int
pad_output(int current,int target)394 pad_output(int current, int target)
395 			        /* writes tabs and blanks (if necessary) to
396 				 * get the current output position up to the
397 				 * target column */
398     /* current: the current column value */
399     /* target: position we want it at */
400 {
401     int curr;			/* internal column pointer */
402 
403     if (current >= target)
404 	return (current);	/* line is already long enough */
405     curr = current;
406     if (opt.use_tabs) {
407 	int tcur;
408 
409 	while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
410 	    putc('\t', output);
411 	    curr = tcur;
412 	}
413     }
414     while (curr++ < target)
415 	putc(' ', output);	/* pad with final blanks */
416 
417     return (target);
418 }
419 
420 /*
421  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
422  *
423  * All rights reserved
424  *
425  *
426  * NAME: count_spaces
427  *
428  * FUNCTION: Find out where printing of a given string will leave the current
429  * character position on output.
430  *
431  * ALGORITHM: Run thru input string and add appropriate values to current
432  * position.
433  *
434  * RETURNS: Integer value of position after printing "buffer" starting in column
435  * "current".
436  *
437  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
438  *
439  */
440 int
count_spaces_until(int cur,char * buffer,char * end)441 count_spaces_until(int cur, char *buffer, char *end)
442 /*
443  * this routine figures out where the character position will be after
444  * printing the text in buffer starting at column "current"
445  */
446 {
447     char *buf;		/* used to look thru buffer */
448 
449     for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
450 	switch (*buf) {
451 
452 	case '\n':
453 	case 014:		/* form feed */
454 	    cur = 1;
455 	    break;
456 
457 	case '\t':
458 	    cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;
459 	    break;
460 
461 	case 010:		/* backspace */
462 	    --cur;
463 	    break;
464 
465 	default:
466 	    ++cur;
467 	    break;
468 	}			/* end of switch */
469     }				/* end of for loop */
470     return (cur);
471 }
472 
473 int
count_spaces(int cur,char * buffer)474 count_spaces(int cur, char *buffer)
475 {
476     return (count_spaces_until(cur, buffer, NULL));
477 }
478 
479 void
diag4(int level,const char * msg,int a,int b)480 diag4(int level, const char *msg, int a, int b)
481 {
482     if (level)
483 	found_err = 1;
484     if (output == stdout) {
485 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
486 	fprintf(stdout, msg, a, b);
487 	fprintf(stdout, " */\n");
488     }
489     else {
490 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
491 	fprintf(stderr, msg, a, b);
492 	fprintf(stderr, "\n");
493     }
494 }
495 
496 void
diag3(int level,const char * msg,int a)497 diag3(int level, const char *msg, int a)
498 {
499     if (level)
500 	found_err = 1;
501     if (output == stdout) {
502 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
503 	fprintf(stdout, msg, a);
504 	fprintf(stdout, " */\n");
505     }
506     else {
507 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
508 	fprintf(stderr, msg, a);
509 	fprintf(stderr, "\n");
510     }
511 }
512 
513 void
diag2(int level,const char * msg)514 diag2(int level, const char *msg)
515 {
516     if (level)
517 	found_err = 1;
518     if (output == stdout) {
519 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
520 	fprintf(stdout, "%s", msg);
521 	fprintf(stdout, " */\n");
522     }
523     else {
524 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
525 	fprintf(stderr, "%s", msg);
526 	fprintf(stderr, "\n");
527     }
528 }
529 
530