xref: /freebsd/usr.bin/indent/io.c (revision 02bde5cf13354d39157b9f43fb99ff0d34bf5b35)
170a3049eSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
59b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
69b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
79b50d902SRodney W. Grimes  * All rights reserved.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
38958d7c9fSPhilippe Charnier #if 0
39d0054952SPhilippe Charnier #ifndef lint
409b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
419b50d902SRodney W. Grimes #endif /* not lint */
42d0054952SPhilippe Charnier #endif
43d0054952SPhilippe Charnier 
44e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
45e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <ctype.h>
48958d7c9fSPhilippe Charnier #include <err.h>
49958d7c9fSPhilippe Charnier #include <stdio.h>
509b50d902SRodney W. Grimes #include <stdlib.h>
519b50d902SRodney W. Grimes #include <string.h>
529b50d902SRodney W. Grimes #include "indent_globs.h"
537916863dSJens Schweikhardt #include "indent.h"
549b50d902SRodney W. Grimes 
55*02bde5cfSKyle Evans /* Globals */
56*02bde5cfSKyle Evans int	found_err;
57*02bde5cfSKyle Evans int	n_real_blanklines;
58*02bde5cfSKyle Evans int	prefix_blankline_requested, postfix_blankline_requested;
59*02bde5cfSKyle Evans int	code_lines;
60*02bde5cfSKyle Evans int	had_eof;
61*02bde5cfSKyle Evans int	inhibit_formatting;
62*02bde5cfSKyle Evans int	suppress_blanklines;
63*02bde5cfSKyle Evans 
649b50d902SRodney W. Grimes int         comment_open;
657916863dSJens Schweikhardt static int  paren_target;
667916863dSJens Schweikhardt static int pad_output(int current, int target);
679b50d902SRodney W. Grimes 
687916863dSJens Schweikhardt void
697916863dSJens Schweikhardt dump_line(void)
709b50d902SRodney W. Grimes {				/* dump_line is the routine that actually
719b50d902SRodney W. Grimes 				 * effects the printing of the new source. It
729b50d902SRodney W. Grimes 				 * prints the label section, followed by the
739b50d902SRodney W. Grimes 				 * code section with the appropriate nesting
749b50d902SRodney W. Grimes 				 * level, followed by any comments */
7590af6a72SJuli Mallett     int cur_col,
767916863dSJens Schweikhardt                 target_col = 1;
777916863dSJens Schweikhardt     static int  not_first_line;
789b50d902SRodney W. Grimes 
799b50d902SRodney W. Grimes     if (ps.procname[0]) {
809b50d902SRodney W. Grimes 	ps.ind_level = 0;
819b50d902SRodney W. Grimes 	ps.procname[0] = 0;
829b50d902SRodney W. Grimes     }
839b50d902SRodney W. Grimes     if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
849b50d902SRodney W. Grimes 	if (suppress_blanklines > 0)
859b50d902SRodney W. Grimes 	    suppress_blanklines--;
869b50d902SRodney W. Grimes 	else {
879b50d902SRodney W. Grimes 	    ps.bl_line = true;
889b50d902SRodney W. Grimes 	    n_real_blanklines++;
899b50d902SRodney W. Grimes 	}
909b50d902SRodney W. Grimes     }
919b50d902SRodney W. Grimes     else if (!inhibit_formatting) {
929b50d902SRodney W. Grimes 	suppress_blanklines = 0;
939b50d902SRodney W. Grimes 	ps.bl_line = false;
949ef5c48bSBill Fumerola 	if (prefix_blankline_requested && not_first_line) {
957e53aaedSPiotr Pawel Stefaniak 	    if (opt.swallow_optional_blanklines) {
969b50d902SRodney W. Grimes 		if (n_real_blanklines == 1)
979b50d902SRodney W. Grimes 		    n_real_blanklines = 0;
989b50d902SRodney W. Grimes 	    }
999b50d902SRodney W. Grimes 	    else {
1009b50d902SRodney W. Grimes 		if (n_real_blanklines == 0)
1019b50d902SRodney W. Grimes 		    n_real_blanklines = 1;
1029b50d902SRodney W. Grimes 	    }
1039ef5c48bSBill Fumerola 	}
1049b50d902SRodney W. Grimes 	while (--n_real_blanklines >= 0)
1059b50d902SRodney W. Grimes 	    putc('\n', output);
1069b50d902SRodney W. Grimes 	n_real_blanklines = 0;
1079b50d902SRodney W. Grimes 	if (ps.ind_level == 0)
1089b50d902SRodney W. Grimes 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
1099b50d902SRodney W. Grimes 				 * additional statement indentation if we are
1109b50d902SRodney W. Grimes 				 * at bracket level 0 */
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	if (e_lab != s_lab || e_code != s_code)
1139b50d902SRodney W. Grimes 	    ++code_lines;	/* keep count of lines with code */
1149b50d902SRodney W. Grimes 
1159b50d902SRodney W. Grimes 
1169b50d902SRodney W. Grimes 	if (e_lab != s_lab) {	/* print lab, if any */
1179b50d902SRodney W. Grimes 	    if (comment_open) {
1189b50d902SRodney W. Grimes 		comment_open = 0;
1199b50d902SRodney W. Grimes 		fprintf(output, ".*/\n");
1209b50d902SRodney W. Grimes 	    }
1219b50d902SRodney W. Grimes 	    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1229b50d902SRodney W. Grimes 		e_lab--;
12309544f7dSPedro F. Giffuni 	    *e_lab = '\0';
1249b50d902SRodney W. Grimes 	    cur_col = pad_output(1, compute_label_target());
1259b50d902SRodney W. Grimes 	    if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
1269b50d902SRodney W. Grimes 				    || strncmp(s_lab, "#endif", 6) == 0)) {
12790af6a72SJuli Mallett 		char *s = s_lab;
1289b50d902SRodney W. Grimes 		if (e_lab[-1] == '\n') e_lab--;
1299b50d902SRodney W. Grimes 		do putc(*s++, output);
1309b50d902SRodney W. Grimes 		while (s < e_lab && 'a' <= *s && *s<='z');
1319b50d902SRodney W. Grimes 		while ((*s == ' ' || *s == '\t') && s < e_lab)
1329b50d902SRodney W. Grimes 		    s++;
1339b50d902SRodney W. Grimes 		if (s < e_lab)
1349b50d902SRodney W. Grimes 		    fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
1358c7e7698SDavid Malone 			    (int)(e_lab - s), s);
1369b50d902SRodney W. Grimes 	    }
1378c7e7698SDavid Malone 	    else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
1389b50d902SRodney W. Grimes 	    cur_col = count_spaces(cur_col, s_lab);
1399b50d902SRodney W. Grimes 	}
1409b50d902SRodney W. Grimes 	else
1419b50d902SRodney W. Grimes 	    cur_col = 1;	/* there is no label section */
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes 	ps.pcase = false;
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes 	if (s_code != e_code) {	/* print code section, if any */
14690af6a72SJuli Mallett 	    char *p;
1479b50d902SRodney W. Grimes 
1489b50d902SRodney W. Grimes 	    if (comment_open) {
1499b50d902SRodney W. Grimes 		comment_open = 0;
1509b50d902SRodney W. Grimes 		fprintf(output, ".*/\n");
1519b50d902SRodney W. Grimes 	    }
1529b50d902SRodney W. Grimes 	    target_col = compute_code_target();
1539b50d902SRodney W. Grimes 	    {
15490af6a72SJuli Mallett 		int i;
1559b50d902SRodney W. Grimes 
1569b50d902SRodney W. Grimes 		for (i = 0; i < ps.p_l_follow; i++)
1579b50d902SRodney W. Grimes 		    if (ps.paren_indents[i] >= 0)
1589b50d902SRodney W. Grimes 			ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
1599b50d902SRodney W. Grimes 	    }
1609b50d902SRodney W. Grimes 	    cur_col = pad_output(cur_col, target_col);
1619b50d902SRodney W. Grimes 	    for (p = s_code; p < e_code; p++)
1629b50d902SRodney W. Grimes 		if (*p == (char) 0200)
1639b50d902SRodney W. Grimes 		    fprintf(output, "%d", target_col * 7);
1649b50d902SRodney W. Grimes 		else
1659b50d902SRodney W. Grimes 		    putc(*p, output);
1669b50d902SRodney W. Grimes 	    cur_col = count_spaces(cur_col, s_code);
1679b50d902SRodney W. Grimes 	}
1681479f36dSPiotr Pawel Stefaniak 	if (s_com != e_com) {		/* print comment, if any */
16990af6a72SJuli Mallett 	    int target = ps.com_col;
17090af6a72SJuli Mallett 	    char *com_st = s_com;
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes 	    target += ps.comment_delta;
173458051a5SPedro F. Giffuni 	    while (*com_st == '\t')	/* consider original indentation in
174458051a5SPedro F. Giffuni 				     * case this is a box comment */
1757e53aaedSPiotr Pawel Stefaniak 		com_st++, target += opt.tabsize;
1769b50d902SRodney W. Grimes 	    while (target <= 0)
1779b50d902SRodney W. Grimes 		if (*com_st == ' ')
1789b50d902SRodney W. Grimes 		    target++, com_st++;
1797e53aaedSPiotr Pawel Stefaniak 		else if (*com_st == '\t') {
1807e53aaedSPiotr Pawel Stefaniak 		    target = opt.tabsize * (1 + (target - 1) / opt.tabsize) + 1;
1817e53aaedSPiotr Pawel Stefaniak 		    com_st++;
1827e53aaedSPiotr Pawel Stefaniak 		}
1839b50d902SRodney W. Grimes 		else
1849b50d902SRodney W. Grimes 		    target = 1;
185487ac9acSUlrich Spörlein 	    if (cur_col > target) {	/* if comment can't fit on this line,
1869b50d902SRodney W. Grimes 				     * put it on next line */
1879b50d902SRodney W. Grimes 		putc('\n', output);
1889b50d902SRodney W. Grimes 		cur_col = 1;
1899b50d902SRodney W. Grimes 		++ps.out_lines;
1909b50d902SRodney W. Grimes 	    }
191e1baf57eSPiotr Pawel Stefaniak 	    while (e_com > com_st && isspace((unsigned char)e_com[-1]))
1929b50d902SRodney W. Grimes 		e_com--;
19338359266SPedro F. Giffuni 	    (void)pad_output(cur_col, target);
1949b50d902SRodney W. Grimes 	    fwrite(com_st, e_com - com_st, 1, output);
1959b50d902SRodney W. Grimes 	    ps.comment_delta = ps.n_comment_delta;
1969b50d902SRodney W. Grimes 	    ++ps.com_lines;	/* count lines with comments */
1979b50d902SRodney W. Grimes 	}
1989b50d902SRodney W. Grimes 	if (ps.use_ff)
1999b50d902SRodney W. Grimes 	    putc('\014', output);
2009b50d902SRodney W. Grimes 	else
2019b50d902SRodney W. Grimes 	    putc('\n', output);
2029b50d902SRodney W. Grimes 	++ps.out_lines;
2037e53aaedSPiotr Pawel Stefaniak 	if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
2049b50d902SRodney W. Grimes 	    prefix_blankline_requested = 1;
2059b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
2069b50d902SRodney W. Grimes 	}
2079b50d902SRodney W. Grimes 	else
2089b50d902SRodney W. Grimes 	    prefix_blankline_requested = postfix_blankline_requested;
2099b50d902SRodney W. Grimes 	postfix_blankline_requested = 0;
2109b50d902SRodney W. Grimes     }
2119b50d902SRodney W. Grimes     ps.decl_on_line = ps.in_decl;	/* if we are in the middle of a
2129b50d902SRodney W. Grimes 					 * declaration, remember that fact for
2139b50d902SRodney W. Grimes 					 * proper comment indentation */
2149b50d902SRodney W. Grimes     ps.ind_stmt = ps.in_stmt & ~ps.in_decl;	/* next line should be
2159b50d902SRodney W. Grimes 						 * indented if we have not
2169b50d902SRodney W. Grimes 						 * completed this stmt and if
2179b50d902SRodney W. Grimes 						 * we are not in the middle of
2189b50d902SRodney W. Grimes 						 * a declaration */
2199b50d902SRodney W. Grimes     ps.use_ff = false;
2209b50d902SRodney W. Grimes     ps.dumped_decl_indent = 0;
2219b50d902SRodney W. Grimes     *(e_lab = s_lab) = '\0';	/* reset buffers */
2229b50d902SRodney W. Grimes     *(e_code = s_code) = '\0';
22369e66b43SPedro F. Giffuni     *(e_com = s_com = combuf + 1) = '\0';
2249b50d902SRodney W. Grimes     ps.ind_level = ps.i_l_follow;
2259b50d902SRodney W. Grimes     ps.paren_level = ps.p_l_follow;
2260bae80a3SPedro F. Giffuni     if (ps.paren_level > 0)
2279b50d902SRodney W. Grimes 	paren_target = -ps.paren_indents[ps.paren_level - 1];
2289b50d902SRodney W. Grimes     not_first_line = 1;
2299b50d902SRodney W. Grimes }
2309b50d902SRodney W. Grimes 
2317916863dSJens Schweikhardt int
2327916863dSJens Schweikhardt compute_code_target(void)
2339b50d902SRodney W. Grimes {
2347e53aaedSPiotr Pawel Stefaniak     int target_col = opt.ind_size * ps.ind_level + 1;
2359b50d902SRodney W. Grimes 
2369b50d902SRodney W. Grimes     if (ps.paren_level)
2377e53aaedSPiotr Pawel Stefaniak 	if (!opt.lineup_to_parens)
2387e53aaedSPiotr Pawel Stefaniak 	    target_col += opt.continuation_indent *
2397e53aaedSPiotr Pawel Stefaniak 		(2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
2407e53aaedSPiotr Pawel Stefaniak 	else if (opt.lineup_to_parens_always)
241aac30b75SPiotr Pawel Stefaniak 	    target_col = paren_target;
2429b50d902SRodney W. Grimes 	else {
24390af6a72SJuli Mallett 	    int w;
24490af6a72SJuli Mallett 	    int t = paren_target;
2459b50d902SRodney W. Grimes 
2467e53aaedSPiotr Pawel Stefaniak 	    if ((w = count_spaces(t, s_code) - opt.max_col) > 0
2477e53aaedSPiotr Pawel Stefaniak 		    && count_spaces(target_col, s_code) <= opt.max_col) {
2489b50d902SRodney W. Grimes 		t -= w + 1;
2499b50d902SRodney W. Grimes 		if (t > target_col)
2509b50d902SRodney W. Grimes 		    target_col = t;
2519b50d902SRodney W. Grimes 	    }
2529b50d902SRodney W. Grimes 	    else
2539b50d902SRodney W. Grimes 		target_col = t;
2549b50d902SRodney W. Grimes 	}
2559b50d902SRodney W. Grimes     else if (ps.ind_stmt)
2567e53aaedSPiotr Pawel Stefaniak 	target_col += opt.continuation_indent;
2579b50d902SRodney W. Grimes     return target_col;
2589b50d902SRodney W. Grimes }
2599b50d902SRodney W. Grimes 
2607916863dSJens Schweikhardt int
2617916863dSJens Schweikhardt compute_label_target(void)
2629b50d902SRodney W. Grimes {
2639b50d902SRodney W. Grimes     return
2647e53aaedSPiotr Pawel Stefaniak 	ps.pcase ? (int) (case_ind * opt.ind_size) + 1
2659b50d902SRodney W. Grimes 	: *s_lab == '#' ? 1
2667e53aaedSPiotr Pawel Stefaniak 	: opt.ind_size * (ps.ind_level - label_offset) + 1;
2679b50d902SRodney W. Grimes }
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes 
2709b50d902SRodney W. Grimes /*
2719b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
2729b50d902SRodney W. Grimes  *
2739b50d902SRodney W. Grimes  * All rights reserved
2749b50d902SRodney W. Grimes  *
2759b50d902SRodney W. Grimes  *
2769b50d902SRodney W. Grimes  * NAME: fill_buffer
2779b50d902SRodney W. Grimes  *
2789b50d902SRodney W. Grimes  * FUNCTION: Reads one block of input into input_buffer
2799b50d902SRodney W. Grimes  *
2809b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC 1/7/77 A
2819b50d902SRodney W. Grimes  * Willcox of CAC	Added check for switch back to partly full input
2829b50d902SRodney W. Grimes  * buffer from temporary buffer
2839b50d902SRodney W. Grimes  *
2849b50d902SRodney W. Grimes  */
2857916863dSJens Schweikhardt void
2867916863dSJens Schweikhardt fill_buffer(void)
2879b50d902SRodney W. Grimes {				/* this routine reads stuff from the input */
28890af6a72SJuli Mallett     char *p;
28990af6a72SJuli Mallett     int i;
29090af6a72SJuli Mallett     FILE *f = input;
2919b50d902SRodney W. Grimes 
292c917a54bSPedro F. Giffuni     if (bp_save != NULL) {	/* there is a partly filled input buffer left */
293e643b783SPedro F. Giffuni 	buf_ptr = bp_save;	/* do not read anything, just switch buffers */
2949b50d902SRodney W. Grimes 	buf_end = be_save;
295c917a54bSPedro F. Giffuni 	bp_save = be_save = NULL;
2969b50d902SRodney W. Grimes 	if (buf_ptr < buf_end)
2979b50d902SRodney W. Grimes 	    return;		/* only return if there is really something in
2989b50d902SRodney W. Grimes 				 * this buffer */
2999b50d902SRodney W. Grimes     }
3009b50d902SRodney W. Grimes     for (p = in_buffer;;) {
3019b50d902SRodney W. Grimes 	if (p >= in_buffer_limit) {
30290af6a72SJuli Mallett 	    int size = (in_buffer_limit - in_buffer) * 2 + 10;
30390af6a72SJuli Mallett 	    int offset = p - in_buffer;
3047916863dSJens Schweikhardt 	    in_buffer = realloc(in_buffer, size);
305d0054952SPhilippe Charnier 	    if (in_buffer == NULL)
306d0054952SPhilippe Charnier 		errx(1, "input line too long");
3079b50d902SRodney W. Grimes 	    p = in_buffer + offset;
3089b50d902SRodney W. Grimes 	    in_buffer_limit = in_buffer + size - 2;
3099b50d902SRodney W. Grimes 	}
3109b50d902SRodney W. Grimes 	if ((i = getc(f)) == EOF) {
3119b50d902SRodney W. Grimes 		*p++ = ' ';
3129b50d902SRodney W. Grimes 		*p++ = '\n';
3139b50d902SRodney W. Grimes 		had_eof = true;
3149b50d902SRodney W. Grimes 		break;
3159b50d902SRodney W. Grimes 	}
3169963ad6fSPiotr Pawel Stefaniak 	if (i != '\0')
3179b50d902SRodney W. Grimes 	    *p++ = i;
3189b50d902SRodney W. Grimes 	if (i == '\n')
3199b50d902SRodney W. Grimes 		break;
3209b50d902SRodney W. Grimes     }
3219b50d902SRodney W. Grimes     buf_ptr = in_buffer;
3229b50d902SRodney W. Grimes     buf_end = p;
3230bae80a3SPedro F. Giffuni     if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
3249b50d902SRodney W. Grimes 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
3259b50d902SRodney W. Grimes 	    fill_buffer();	/* flush indent error message */
3269b50d902SRodney W. Grimes 	else {
3279b50d902SRodney W. Grimes 	    int         com = 0;
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes 	    p = in_buffer;
3309b50d902SRodney W. Grimes 	    while (*p == ' ' || *p == '\t')
3319b50d902SRodney W. Grimes 		p++;
3329b50d902SRodney W. Grimes 	    if (*p == '/' && p[1] == '*') {
3339b50d902SRodney W. Grimes 		p += 2;
3349b50d902SRodney W. Grimes 		while (*p == ' ' || *p == '\t')
3359b50d902SRodney W. Grimes 		    p++;
3369b50d902SRodney W. Grimes 		if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
3379b50d902SRodney W. Grimes 			&& p[4] == 'N' && p[5] == 'T') {
3389b50d902SRodney W. Grimes 		    p += 6;
3399b50d902SRodney W. Grimes 		    while (*p == ' ' || *p == '\t')
3409b50d902SRodney W. Grimes 			p++;
3419b50d902SRodney W. Grimes 		    if (*p == '*')
3429b50d902SRodney W. Grimes 			com = 1;
3437916863dSJens Schweikhardt 		    else if (*p == 'O') {
3449b50d902SRodney W. Grimes 			if (*++p == 'N')
3459b50d902SRodney W. Grimes 			    p++, com = 1;
3469b50d902SRodney W. Grimes 			else if (*p == 'F' && *++p == 'F')
3479b50d902SRodney W. Grimes 			    p++, com = 2;
3487916863dSJens Schweikhardt 		    }
3499b50d902SRodney W. Grimes 		    while (*p == ' ' || *p == '\t')
3509b50d902SRodney W. Grimes 			p++;
3519b50d902SRodney W. Grimes 		    if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
3529b50d902SRodney W. Grimes 			if (s_com != e_com || s_lab != e_lab || s_code != e_code)
3539b50d902SRodney W. Grimes 			    dump_line();
3549b50d902SRodney W. Grimes 			if (!(inhibit_formatting = com - 1)) {
3559b50d902SRodney W. Grimes 			    n_real_blanklines = 0;
3569b50d902SRodney W. Grimes 			    postfix_blankline_requested = 0;
3579b50d902SRodney W. Grimes 			    prefix_blankline_requested = 0;
3589b50d902SRodney W. Grimes 			    suppress_blanklines = 1;
3599b50d902SRodney W. Grimes 			}
3609b50d902SRodney W. Grimes 		    }
3619b50d902SRodney W. Grimes 		}
3629b50d902SRodney W. Grimes 	    }
3639b50d902SRodney W. Grimes 	}
3649b50d902SRodney W. Grimes     }
3659b50d902SRodney W. Grimes     if (inhibit_formatting) {
3669b50d902SRodney W. Grimes 	p = in_buffer;
3679b50d902SRodney W. Grimes 	do
3689b50d902SRodney W. Grimes 	    putc(*p, output);
3699b50d902SRodney W. Grimes 	while (*p++ != '\n');
3709b50d902SRodney W. Grimes     }
3719b50d902SRodney W. Grimes }
3729b50d902SRodney W. Grimes 
3739b50d902SRodney W. Grimes /*
3749b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
3759b50d902SRodney W. Grimes  *
3769b50d902SRodney W. Grimes  * All rights reserved
3779b50d902SRodney W. Grimes  *
3789b50d902SRodney W. Grimes  *
3799b50d902SRodney W. Grimes  * NAME: pad_output
3809b50d902SRodney W. Grimes  *
3819b50d902SRodney W. Grimes  * FUNCTION: Writes tabs and spaces to move the current column up to the desired
3829b50d902SRodney W. Grimes  * position.
3839b50d902SRodney W. Grimes  *
3849b50d902SRodney W. Grimes  * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
3859b50d902SRodney W. Grimes  *
3869b50d902SRodney W. Grimes  * PARAMETERS: current		integer		The current column target
3879b50d902SRodney W. Grimes  * nteger		The desired column
3889b50d902SRodney W. Grimes  *
3899b50d902SRodney W. Grimes  * RETURNS: Integer value of the new column.  (If current >= target, no action is
3909b50d902SRodney W. Grimes  * taken, and current is returned.
3919b50d902SRodney W. Grimes  *
3929b50d902SRodney W. Grimes  * GLOBALS: None
3939b50d902SRodney W. Grimes  *
3949b50d902SRodney W. Grimes  * CALLS: write (sys)
3959b50d902SRodney W. Grimes  *
3969b50d902SRodney W. Grimes  * CALLED BY: dump_line
3979b50d902SRodney W. Grimes  *
3989b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
3999b50d902SRodney W. Grimes  *
4009b50d902SRodney W. Grimes  */
4017916863dSJens Schweikhardt static int
4027916863dSJens Schweikhardt pad_output(int current, int target)
4037916863dSJens Schweikhardt 			        /* writes tabs and blanks (if necessary) to
4049b50d902SRodney W. Grimes 				 * get the current output position up to the
4059b50d902SRodney W. Grimes 				 * target column */
4067916863dSJens Schweikhardt     /* current: the current column value */
4077916863dSJens Schweikhardt     /* target: position we want it at */
4089b50d902SRodney W. Grimes {
409fbdbd284SPiotr Pawel Stefaniak     int curr;			/* internal column pointer */
410fbdbd284SPiotr Pawel Stefaniak 
4119b50d902SRodney W. Grimes     if (current >= target)
4129b50d902SRodney W. Grimes 	return (current);	/* line is already long enough */
4139b50d902SRodney W. Grimes     curr = current;
4147e53aaedSPiotr Pawel Stefaniak     if (opt.use_tabs) {
415fbdbd284SPiotr Pawel Stefaniak 	int tcur;
416fbdbd284SPiotr Pawel Stefaniak 
4177e53aaedSPiotr Pawel Stefaniak 	while ((tcur = opt.tabsize * (1 + (curr - 1) / opt.tabsize) + 1) <= target) {
4189b50d902SRodney W. Grimes 	    putc('\t', output);
4199b50d902SRodney W. Grimes 	    curr = tcur;
4209b50d902SRodney W. Grimes 	}
421e3625e9cSJens Schweikhardt     }
4229b50d902SRodney W. Grimes     while (curr++ < target)
4239b50d902SRodney W. Grimes 	putc(' ', output);	/* pad with final blanks */
4241479f36dSPiotr Pawel Stefaniak 
4259b50d902SRodney W. Grimes     return (target);
4269b50d902SRodney W. Grimes }
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes /*
4299b50d902SRodney W. Grimes  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
4309b50d902SRodney W. Grimes  *
4319b50d902SRodney W. Grimes  * All rights reserved
4329b50d902SRodney W. Grimes  *
4339b50d902SRodney W. Grimes  *
4349b50d902SRodney W. Grimes  * NAME: count_spaces
4359b50d902SRodney W. Grimes  *
4369b50d902SRodney W. Grimes  * FUNCTION: Find out where printing of a given string will leave the current
4379b50d902SRodney W. Grimes  * character position on output.
4389b50d902SRodney W. Grimes  *
4399b50d902SRodney W. Grimes  * ALGORITHM: Run thru input string and add appropriate values to current
4409b50d902SRodney W. Grimes  * position.
4419b50d902SRodney W. Grimes  *
4429b50d902SRodney W. Grimes  * RETURNS: Integer value of position after printing "buffer" starting in column
4439b50d902SRodney W. Grimes  * "current".
4449b50d902SRodney W. Grimes  *
4459b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
4469b50d902SRodney W. Grimes  *
4479b50d902SRodney W. Grimes  */
4489b50d902SRodney W. Grimes int
44954d57555SPedro F. Giffuni count_spaces_until(int cur, char *buffer, char *end)
4509b50d902SRodney W. Grimes /*
4519b50d902SRodney W. Grimes  * this routine figures out where the character position will be after
4529b50d902SRodney W. Grimes  * printing the text in buffer starting at column "current"
4539b50d902SRodney W. Grimes  */
4549b50d902SRodney W. Grimes {
45590af6a72SJuli Mallett     char *buf;		/* used to look thru buffer */
4569b50d902SRodney W. Grimes 
45754d57555SPedro F. Giffuni     for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
4589b50d902SRodney W. Grimes 	switch (*buf) {
4599b50d902SRodney W. Grimes 
4609b50d902SRodney W. Grimes 	case '\n':
4619b50d902SRodney W. Grimes 	case 014:		/* form feed */
4629b50d902SRodney W. Grimes 	    cur = 1;
4639b50d902SRodney W. Grimes 	    break;
4649b50d902SRodney W. Grimes 
4659b50d902SRodney W. Grimes 	case '\t':
4667e53aaedSPiotr Pawel Stefaniak 	    cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;
4679b50d902SRodney W. Grimes 	    break;
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 	case 010:		/* backspace */
4709b50d902SRodney W. Grimes 	    --cur;
4719b50d902SRodney W. Grimes 	    break;
4729b50d902SRodney W. Grimes 
4739b50d902SRodney W. Grimes 	default:
4749b50d902SRodney W. Grimes 	    ++cur;
4759b50d902SRodney W. Grimes 	    break;
4769b50d902SRodney W. Grimes 	}			/* end of switch */
4779b50d902SRodney W. Grimes     }				/* end of for loop */
4789b50d902SRodney W. Grimes     return (cur);
4799b50d902SRodney W. Grimes }
4809b50d902SRodney W. Grimes 
48154d57555SPedro F. Giffuni int
48254d57555SPedro F. Giffuni count_spaces(int cur, char *buffer)
48354d57555SPedro F. Giffuni {
48454d57555SPedro F. Giffuni     return (count_spaces_until(cur, buffer, NULL));
48554d57555SPedro F. Giffuni }
48654d57555SPedro F. Giffuni 
4877916863dSJens Schweikhardt void
4888c7e7698SDavid Malone diag4(int level, const char *msg, int a, int b)
4899b50d902SRodney W. Grimes {
4909b50d902SRodney W. Grimes     if (level)
4919b50d902SRodney W. Grimes 	found_err = 1;
4929b50d902SRodney W. Grimes     if (output == stdout) {
4939b50d902SRodney W. Grimes 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
4949b50d902SRodney W. Grimes 	fprintf(stdout, msg, a, b);
4959b50d902SRodney W. Grimes 	fprintf(stdout, " */\n");
4969b50d902SRodney W. Grimes     }
4979b50d902SRodney W. Grimes     else {
4989b50d902SRodney W. Grimes 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
4999b50d902SRodney W. Grimes 	fprintf(stderr, msg, a, b);
5009b50d902SRodney W. Grimes 	fprintf(stderr, "\n");
5019b50d902SRodney W. Grimes     }
5029b50d902SRodney W. Grimes }
5039b50d902SRodney W. Grimes 
5047916863dSJens Schweikhardt void
5058c7e7698SDavid Malone diag3(int level, const char *msg, int a)
5067916863dSJens Schweikhardt {
5077916863dSJens Schweikhardt     if (level)
5087916863dSJens Schweikhardt 	found_err = 1;
5097916863dSJens Schweikhardt     if (output == stdout) {
5107916863dSJens Schweikhardt 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5117916863dSJens Schweikhardt 	fprintf(stdout, msg, a);
5127916863dSJens Schweikhardt 	fprintf(stdout, " */\n");
5137916863dSJens Schweikhardt     }
5147916863dSJens Schweikhardt     else {
5157916863dSJens Schweikhardt 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5167916863dSJens Schweikhardt 	fprintf(stderr, msg, a);
5177916863dSJens Schweikhardt 	fprintf(stderr, "\n");
5187916863dSJens Schweikhardt     }
5197916863dSJens Schweikhardt }
5207916863dSJens Schweikhardt 
5217916863dSJens Schweikhardt void
5228c7e7698SDavid Malone diag2(int level, const char *msg)
5237916863dSJens Schweikhardt {
5247916863dSJens Schweikhardt     if (level)
5257916863dSJens Schweikhardt 	found_err = 1;
5267916863dSJens Schweikhardt     if (output == stdout) {
5277916863dSJens Schweikhardt 	fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
528f7c0d4c7SDimitry Andric 	fprintf(stdout, "%s", msg);
5297916863dSJens Schweikhardt 	fprintf(stdout, " */\n");
5307916863dSJens Schweikhardt     }
5317916863dSJens Schweikhardt     else {
5327916863dSJens Schweikhardt 	fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
533f7c0d4c7SDimitry Andric 	fprintf(stderr, "%s", msg);
5347916863dSJens Schweikhardt 	fprintf(stderr, "\n");
5357916863dSJens Schweikhardt     }
5367916863dSJens Schweikhardt }
5377916863dSJens Schweikhardt 
538