Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.55
      1  1.55    rillig /*	$NetBSD: io.c,v 1.55 2021/09/25 07:55:24 rillig Exp $	*/
      2   1.3       tls 
      3  1.19     kamil /*-
      4  1.19     kamil  * SPDX-License-Identifier: BSD-4-Clause
      5  1.19     kamil  *
      6  1.19     kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7   1.4       mrg  * Copyright (c) 1980, 1993
      8   1.4       mrg  *	The Regents of the University of California.  All rights reserved.
      9   1.1       cgd  * All rights reserved.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1       cgd  *    must display the following acknowledgement:
     21   1.1       cgd  *	This product includes software developed by the University of
     22   1.1       cgd  *	California, Berkeley and its contributors.
     23   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       cgd  *    may be used to endorse or promote products derived from this software
     25   1.1       cgd  *    without specific prior written permission.
     26   1.1       cgd  *
     27   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       cgd  * SUCH DAMAGE.
     38   1.1       cgd  */
     39   1.1       cgd 
     40  1.19     kamil #if 0
     41  1.19     kamil #ifndef lint
     42  1.19     kamil static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
     43  1.19     kamil #endif /* not lint */
     44  1.19     kamil #endif
     45  1.19     kamil 
     46   1.5     lukem #include <sys/cdefs.h>
     47   1.1       cgd #ifndef lint
     48  1.19     kamil #if defined(__NetBSD__)
     49  1.55    rillig __RCSID("$NetBSD: io.c,v 1.55 2021/09/25 07:55:24 rillig Exp $");
     50  1.19     kamil #elif defined(__FreeBSD__)
     51  1.19     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
     52  1.19     kamil #endif
     53   1.4       mrg #endif
     54   1.1       cgd 
     55   1.5     lukem #include <ctype.h>
     56   1.5     lukem #include <err.h>
     57   1.1       cgd #include <stdio.h>
     58   1.1       cgd #include <stdlib.h>
     59   1.1       cgd #include <string.h>
     60  1.20  christos #include <stdarg.h>
     61  1.22    rillig 
     62  1.19     kamil #include "indent.h"
     63   1.1       cgd 
     64  1.19     kamil int         comment_open;
     65  1.31    rillig static int  paren_indent;
     66   1.1       cgd 
     67  1.28    rillig static void
     68  1.28    rillig output_char(char ch)
     69  1.28    rillig {
     70  1.28    rillig     fputc(ch, output);
     71  1.36    rillig     debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
     72  1.28    rillig }
     73  1.28    rillig 
     74  1.28    rillig static void
     75  1.28    rillig output_range(const char *s, const char *e)
     76  1.28    rillig {
     77  1.28    rillig     fwrite(s, 1, (size_t)(e - s), output);
     78  1.36    rillig     debug_vis_range("output_range \"", s, e, "\"\n");
     79  1.28    rillig }
     80  1.28    rillig 
     81  1.29    rillig static inline void
     82  1.28    rillig output_string(const char *s)
     83  1.28    rillig {
     84  1.28    rillig     output_range(s, s + strlen(s));
     85  1.28    rillig }
     86  1.28    rillig 
     87  1.34    rillig static int
     88  1.34    rillig output_indent(int old_ind, int new_ind)
     89  1.34    rillig {
     90  1.34    rillig     int ind = old_ind;
     91  1.34    rillig 
     92  1.34    rillig     if (opt.use_tabs) {
     93  1.34    rillig 	int tabsize = opt.tabsize;
     94  1.34    rillig 	int n = new_ind / tabsize - ind / tabsize;
     95  1.34    rillig 	if (n > 0)
     96  1.34    rillig 	    ind -= ind % tabsize;
     97  1.34    rillig 	for (int i = 0; i < n; i++) {
     98  1.36    rillig 	    fputc('\t', output);
     99  1.34    rillig 	    ind += tabsize;
    100  1.34    rillig 	}
    101  1.34    rillig     }
    102  1.34    rillig 
    103  1.34    rillig     for (; ind < new_ind; ind++)
    104  1.36    rillig         fputc(' ', output);
    105  1.34    rillig 
    106  1.36    rillig     debug_println("output_indent %d", ind);
    107  1.34    rillig     return ind;
    108  1.34    rillig }
    109  1.34    rillig 
    110  1.26    rillig /*
    111  1.26    rillig  * dump_line is the routine that actually effects the printing of the new
    112  1.26    rillig  * source. It prints the label section, followed by the code section with
    113  1.26    rillig  * the appropriate nesting level, followed by any comments.
    114  1.26    rillig  */
    115   1.5     lukem void
    116  1.11       wiz dump_line(void)
    117  1.26    rillig {
    118  1.44    rillig     int cur_col;
    119  1.19     kamil     static int  not_first_line;
    120  1.19     kamil 
    121  1.19     kamil     if (ps.procname[0]) {
    122  1.19     kamil 	ps.ind_level = 0;
    123  1.19     kamil 	ps.procname[0] = 0;
    124  1.19     kamil     }
    125  1.30    rillig 
    126  1.55    rillig     if (code.s == code.e && lab.s == lab.e && com.s == com.e) {
    127  1.19     kamil 	if (suppress_blanklines > 0)
    128  1.19     kamil 	    suppress_blanklines--;
    129  1.19     kamil 	else {
    130  1.19     kamil 	    ps.bl_line = true;
    131  1.19     kamil 	    n_real_blanklines++;
    132  1.19     kamil 	}
    133  1.30    rillig     } else if (!inhibit_formatting) {
    134  1.19     kamil 	suppress_blanklines = 0;
    135  1.19     kamil 	ps.bl_line = false;
    136  1.19     kamil 	if (prefix_blankline_requested && not_first_line) {
    137  1.19     kamil 	    if (opt.swallow_optional_blanklines) {
    138  1.19     kamil 		if (n_real_blanklines == 1)
    139  1.19     kamil 		    n_real_blanklines = 0;
    140  1.30    rillig 	    } else {
    141  1.19     kamil 		if (n_real_blanklines == 0)
    142  1.19     kamil 		    n_real_blanklines = 1;
    143  1.19     kamil 	    }
    144  1.19     kamil 	}
    145  1.19     kamil 	while (--n_real_blanklines >= 0)
    146  1.28    rillig 	    output_char('\n');
    147  1.19     kamil 	n_real_blanklines = 0;
    148  1.19     kamil 	if (ps.ind_level == 0)
    149  1.19     kamil 	    ps.ind_stmt = 0;	/* this is a class A kludge. dont do
    150  1.19     kamil 				 * additional statement indentation if we are
    151  1.19     kamil 				 * at bracket level 0 */
    152  1.19     kamil 
    153  1.55    rillig 	if (lab.e != lab.s || code.e != code.s)
    154  1.19     kamil 	    ++code_lines;	/* keep count of lines with code */
    155  1.19     kamil 
    156  1.19     kamil 
    157  1.54    rillig 	if (lab.e != lab.s) {	/* print lab, if any */
    158  1.19     kamil 	    if (comment_open) {
    159  1.19     kamil 		comment_open = 0;
    160  1.28    rillig 		output_string(".*/\n");
    161  1.19     kamil 	    }
    162  1.54    rillig 	    while (lab.e > lab.s && (lab.e[-1] == ' ' || lab.e[-1] == '\t'))
    163  1.54    rillig 		lab.e--;
    164  1.54    rillig 	    *lab.e = '\0';
    165  1.38    rillig 	    cur_col = 1 + output_indent(0, compute_label_indent());
    166  1.54    rillig 	    if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0
    167  1.54    rillig 				    || strncmp(lab.s, "#endif", 6) == 0)) {
    168  1.54    rillig 		char *s = lab.s;
    169  1.54    rillig 		if (lab.e[-1] == '\n') lab.e--;
    170  1.25    rillig 		do {
    171  1.28    rillig 		    output_char(*s++);
    172  1.54    rillig 		} while (s < lab.e && 'a' <= *s && *s <= 'z');
    173  1.54    rillig 		while ((*s == ' ' || *s == '\t') && s < lab.e)
    174  1.19     kamil 		    s++;
    175  1.54    rillig 		if (s < lab.e) {
    176  1.28    rillig 		    if (s[0] == '/' && s[1] == '*') {
    177  1.28    rillig 			output_char('\t');
    178  1.54    rillig 			output_range(s, lab.e);
    179  1.28    rillig 		    } else {
    180  1.28    rillig 		        output_string("\t/* ");
    181  1.54    rillig 			output_range(s, lab.e);
    182  1.28    rillig 			output_string(" */");
    183  1.28    rillig 		    }
    184  1.28    rillig 		}
    185  1.28    rillig 	    } else
    186  1.54    rillig 	        output_range(lab.s, lab.e);
    187  1.54    rillig 	    cur_col = 1 + indentation_after(cur_col - 1, lab.s);
    188  1.30    rillig 	} else
    189  1.19     kamil 	    cur_col = 1;	/* there is no label section */
    190  1.19     kamil 
    191  1.19     kamil 	ps.pcase = false;
    192  1.19     kamil 
    193  1.55    rillig 	if (code.s != code.e) {	/* print code section, if any */
    194  1.19     kamil 	    if (comment_open) {
    195  1.19     kamil 		comment_open = 0;
    196  1.28    rillig 		output_string(".*/\n");
    197  1.19     kamil 	    }
    198  1.44    rillig 	    int target_col = 1 + compute_code_indent();
    199  1.19     kamil 	    {
    200  1.19     kamil 		int i;
    201  1.19     kamil 
    202  1.36    rillig 		for (i = 0; i < ps.p_l_follow; i++) {
    203  1.36    rillig 		    if (ps.paren_indents[i] >= 0) {
    204  1.36    rillig 			int ind = ps.paren_indents[i];
    205  1.36    rillig 			/*
    206  1.36    rillig 			 * XXX: this mix of 'indent' and 'column' smells like
    207  1.36    rillig 			 * an off-by-one error.
    208  1.36    rillig 			 */
    209  1.36    rillig 			ps.paren_indents[i] = -(ind + target_col);
    210  1.36    rillig 			debug_println(
    211  1.36    rillig 			    "setting pi[%d] from %d to %d for column %d",
    212  1.36    rillig 			    i, ind, ps.paren_indents[i], target_col);
    213  1.36    rillig 		    }
    214  1.36    rillig 		}
    215  1.19     kamil 	    }
    216  1.34    rillig 	    cur_col = 1 + output_indent(cur_col - 1, target_col - 1);
    217  1.55    rillig 	    output_range(code.s, code.e);
    218  1.55    rillig 	    cur_col = 1 + indentation_after(cur_col - 1, code.s);
    219  1.19     kamil 	}
    220  1.52    rillig 	if (com.s != com.e) {		/* print comment, if any */
    221  1.45    rillig 	    int target_col = ps.com_col;
    222  1.52    rillig 	    char *com_st = com.s;
    223  1.19     kamil 
    224  1.45    rillig 	    target_col += ps.comment_delta;
    225  1.19     kamil 	    while (*com_st == '\t')	/* consider original indentation in
    226  1.46    rillig 				 * case this is a box comment */
    227  1.45    rillig 		com_st++, target_col += opt.tabsize;
    228  1.45    rillig 	    while (target_col <= 0)
    229  1.19     kamil 		if (*com_st == ' ')
    230  1.45    rillig 		    target_col++, com_st++;
    231  1.19     kamil 		else if (*com_st == '\t') {
    232  1.45    rillig 		    target_col = opt.tabsize * (1 + (target_col - 1) / opt.tabsize) + 1;
    233  1.19     kamil 		    com_st++;
    234  1.30    rillig 		} else
    235  1.45    rillig 		    target_col = 1;
    236  1.45    rillig 	    if (cur_col > target_col) {	/* if comment can't fit on this line,
    237  1.46    rillig 				 * put it on next line */
    238  1.28    rillig 		output_char('\n');
    239  1.19     kamil 		cur_col = 1;
    240  1.19     kamil 		++ps.out_lines;
    241  1.19     kamil 	    }
    242  1.52    rillig 	    while (com.e > com_st && isspace((unsigned char)com.e[-1]))
    243  1.52    rillig 		com.e--;
    244  1.45    rillig 	    (void)output_indent(cur_col - 1, target_col - 1);
    245  1.52    rillig 	    output_range(com_st, com.e);
    246  1.19     kamil 	    ps.comment_delta = ps.n_comment_delta;
    247  1.19     kamil 	    ++ps.com_lines;	/* count lines with comments */
    248  1.19     kamil 	}
    249  1.19     kamil 	if (ps.use_ff)
    250  1.28    rillig 	    output_char('\014');
    251  1.19     kamil 	else
    252  1.28    rillig 	    output_char('\n');
    253  1.19     kamil 	++ps.out_lines;
    254  1.19     kamil 	if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
    255  1.19     kamil 	    prefix_blankline_requested = 1;
    256  1.19     kamil 	    ps.just_saw_decl = 0;
    257  1.30    rillig 	} else
    258  1.19     kamil 	    prefix_blankline_requested = postfix_blankline_requested;
    259  1.19     kamil 	postfix_blankline_requested = 0;
    260  1.19     kamil     }
    261  1.24    rillig 
    262  1.24    rillig     /* keep blank lines after '//' comments */
    263  1.52    rillig     if (com.e - com.s > 1 && com.s[1] == '/'
    264  1.50    rillig 	&& s_token < e_token && isspace((unsigned char)s_token[0]))
    265  1.28    rillig 	output_range(s_token, e_token);
    266  1.24    rillig 
    267  1.46    rillig     ps.decl_on_line = ps.in_decl; /* if we are in the middle of a declaration,
    268  1.46    rillig 				 * remember that fact for proper comment
    269  1.46    rillig 				 * indentation */
    270  1.46    rillig     ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be indented if
    271  1.46    rillig 				 * we have not completed this stmt and if we
    272  1.46    rillig 				 * are not in the middle of a declaration */
    273  1.19     kamil     ps.use_ff = false;
    274  1.19     kamil     ps.dumped_decl_indent = 0;
    275  1.54    rillig     *(lab.e = lab.s) = '\0';	/* reset buffers */
    276  1.55    rillig     *(code.e = code.s) = '\0';
    277  1.52    rillig     *(com.e = com.s = com.buf + 1) = '\0';
    278  1.19     kamil     ps.ind_level = ps.i_l_follow;
    279  1.19     kamil     ps.paren_level = ps.p_l_follow;
    280  1.36    rillig     if (ps.paren_level > 0) {
    281  1.36    rillig         /* TODO: explain what negative indentation means */
    282  1.31    rillig 	paren_indent = -ps.paren_indents[ps.paren_level - 1];
    283  1.36    rillig 	debug_println("paren_indent is now %d", paren_indent);
    284  1.36    rillig     }
    285  1.19     kamil     not_first_line = 1;
    286   1.1       cgd }
    287   1.1       cgd 
    288   1.5     lukem int
    289  1.39    rillig compute_code_indent(void)
    290   1.1       cgd {
    291  1.43    rillig     int target_ind = opt.indent_size * ps.ind_level;
    292  1.19     kamil 
    293  1.39    rillig     if (ps.paren_level != 0) {
    294  1.19     kamil 	if (!opt.lineup_to_parens)
    295  1.49    rillig 	    if (2 * opt.continuation_indent == opt.indent_size)
    296  1.49    rillig 		target_ind += opt.continuation_indent;
    297  1.49    rillig 	    else
    298  1.49    rillig 		target_ind += opt.continuation_indent * ps.paren_level;
    299  1.19     kamil 	else if (opt.lineup_to_parens_always)
    300  1.39    rillig 	    /*
    301  1.39    rillig 	     * XXX: where does this '- 1' come from?  It looks strange but is
    302  1.39    rillig 	     * nevertheless needed for proper indentation, as demonstrated in
    303  1.39    rillig 	     * the test opt-lpl.0.
    304  1.39    rillig 	     */
    305  1.39    rillig 	    target_ind = paren_indent - 1;
    306  1.19     kamil 	else {
    307  1.19     kamil 	    int w;
    308  1.31    rillig 	    int t = paren_indent;
    309   1.1       cgd 
    310  1.55    rillig 	    if ((w = 1 + indentation_after(t - 1, code.s) - opt.max_line_length) > 0
    311  1.55    rillig 		&& 1 + indentation_after(target_ind, code.s) <= opt.max_line_length) {
    312  1.19     kamil 		t -= w + 1;
    313  1.39    rillig 		if (t > target_ind + 1)
    314  1.39    rillig 		    target_ind = t - 1;
    315  1.30    rillig 	    } else
    316  1.39    rillig 		target_ind = t - 1;
    317  1.19     kamil 	}
    318  1.30    rillig     } else if (ps.ind_stmt)
    319  1.39    rillig 	target_ind += opt.continuation_indent;
    320  1.39    rillig     return target_ind;
    321   1.1       cgd }
    322   1.1       cgd 
    323   1.5     lukem int
    324  1.38    rillig compute_label_indent(void)
    325   1.1       cgd {
    326  1.38    rillig     if (ps.pcase)
    327  1.43    rillig 	return (int) (case_ind * opt.indent_size);
    328  1.54    rillig     if (lab.s[0] == '#')
    329  1.38    rillig         return 0;
    330  1.43    rillig     return opt.indent_size * (ps.ind_level - label_offset);
    331   1.1       cgd }
    332   1.1       cgd 
    333  1.53    rillig static void
    334  1.53    rillig skip_hspace(const char **pp)
    335  1.53    rillig {
    336  1.53    rillig     while (**pp == ' ' || **pp == '\t')
    337  1.53    rillig 	(*pp)++;
    338  1.53    rillig }
    339  1.53    rillig 
    340  1.53    rillig static void
    341  1.53    rillig parse_indent_comment(void)
    342  1.53    rillig {
    343  1.53    rillig     int on_off = 0;		/* 0 = keep, 1 = ON, 2 = OFF */
    344  1.53    rillig 
    345  1.53    rillig     const char *p = in_buffer;
    346  1.53    rillig 
    347  1.53    rillig     skip_hspace(&p);
    348  1.53    rillig 
    349  1.53    rillig     if (!(*p == '/' && p[1] == '*'))
    350  1.53    rillig 	return;
    351  1.53    rillig     p += 2;
    352  1.53    rillig 
    353  1.53    rillig     skip_hspace(&p);
    354  1.53    rillig 
    355  1.53    rillig     if (!(p[0] == 'I' && p[1] == 'N' && p[2] == 'D'
    356  1.53    rillig 	  && p[3] == 'E' && p[4] == 'N' && p[5] == 'T'))
    357  1.53    rillig 	return;
    358  1.53    rillig     p += 6;
    359  1.53    rillig 
    360  1.53    rillig     skip_hspace(&p);
    361  1.53    rillig 
    362  1.53    rillig     if (*p == '*')
    363  1.53    rillig 	on_off = 1;
    364  1.53    rillig     else if (*p == 'O') {
    365  1.53    rillig 	if (*++p == 'N')
    366  1.53    rillig 	    p++, on_off = 1;
    367  1.53    rillig 	else if (*p == 'F' && *++p == 'F')
    368  1.53    rillig 	    p++, on_off = 2;
    369  1.53    rillig     }
    370  1.53    rillig     if (on_off == 0)
    371  1.53    rillig 	return;
    372  1.53    rillig 
    373  1.53    rillig     skip_hspace(&p);
    374  1.53    rillig 
    375  1.53    rillig     if (!(p[0] == '*' && p[1] == '/' && p[2] == '\n'))
    376  1.53    rillig 	return;
    377  1.53    rillig 
    378  1.55    rillig     if (com.s != com.e || lab.s != lab.e || code.s != code.e)
    379  1.53    rillig 	dump_line();
    380  1.53    rillig 
    381  1.53    rillig     if (!(inhibit_formatting = on_off - 1)) {
    382  1.53    rillig 	n_real_blanklines = 0;
    383  1.53    rillig 	postfix_blankline_requested = 0;
    384  1.53    rillig 	prefix_blankline_requested = 0;
    385  1.53    rillig 	suppress_blanklines = 1;
    386  1.53    rillig     }
    387  1.53    rillig }
    388   1.1       cgd 
    389   1.1       cgd /*
    390   1.1       cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    391   1.5     lukem  *
    392   1.1       cgd  * All rights reserved
    393   1.5     lukem  *
    394  1.33    rillig  * FUNCTION: Reads one block of input into the input buffer
    395   1.1       cgd  */
    396   1.5     lukem void
    397  1.11       wiz fill_buffer(void)
    398   1.1       cgd {				/* this routine reads stuff from the input */
    399  1.19     kamil     char *p;
    400  1.19     kamil     int i;
    401  1.19     kamil     FILE *f = input;
    402  1.19     kamil 
    403  1.19     kamil     if (bp_save != NULL) {	/* there is a partly filled input buffer left */
    404  1.19     kamil 	buf_ptr = bp_save;	/* do not read anything, just switch buffers */
    405  1.19     kamil 	buf_end = be_save;
    406  1.19     kamil 	bp_save = be_save = NULL;
    407  1.47    rillig 	debug_println("switched buf_ptr back to bp_save");
    408  1.19     kamil 	if (buf_ptr < buf_end)
    409  1.19     kamil 	    return;		/* only return if there is really something in
    410   1.1       cgd 				 * this buffer */
    411  1.19     kamil     }
    412  1.19     kamil     for (p = in_buffer;;) {
    413  1.19     kamil 	if (p >= in_buffer_limit) {
    414  1.48    rillig 	    size_t size = (in_buffer_limit - in_buffer) * 2 + 10;
    415  1.48    rillig 	    size_t offset = p - in_buffer;
    416  1.19     kamil 	    in_buffer = realloc(in_buffer, size);
    417  1.19     kamil 	    if (in_buffer == NULL)
    418  1.19     kamil 		errx(1, "input line too long");
    419  1.19     kamil 	    p = in_buffer + offset;
    420  1.19     kamil 	    in_buffer_limit = in_buffer + size - 2;
    421  1.19     kamil 	}
    422  1.19     kamil 	if ((i = getc(f)) == EOF) {
    423  1.37    rillig 	    *p++ = ' ';
    424  1.37    rillig 	    *p++ = '\n';
    425  1.37    rillig 	    had_eof = true;
    426  1.37    rillig 	    break;
    427   1.1       cgd 	}
    428  1.19     kamil 	if (i != '\0')
    429  1.19     kamil 	    *p++ = i;
    430  1.19     kamil 	if (i == '\n')
    431  1.37    rillig 	    break;
    432  1.19     kamil     }
    433  1.19     kamil     buf_ptr = in_buffer;
    434  1.19     kamil     buf_end = p;
    435  1.19     kamil     if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
    436  1.19     kamil 	if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
    437  1.19     kamil 	    fill_buffer();	/* flush indent error message */
    438  1.53    rillig 	else
    439  1.53    rillig 	    parse_indent_comment();
    440  1.19     kamil     }
    441  1.19     kamil     if (inhibit_formatting) {
    442  1.19     kamil 	p = in_buffer;
    443  1.25    rillig 	do {
    444  1.28    rillig 	    output_char(*p);
    445  1.25    rillig 	} while (*p++ != '\n');
    446  1.19     kamil     }
    447   1.1       cgd }
    448  1.19     kamil 
    449   1.1       cgd int
    450  1.40    rillig indentation_after_range(int ind, const char *start, const char *end)
    451   1.1       cgd {
    452  1.40    rillig     for (const char *p = start; *p != '\0' && p != end; ++p) {
    453  1.40    rillig 	if (*p == '\n' || *p == '\f')
    454  1.40    rillig 	    ind = 0;
    455  1.40    rillig 	else if (*p == '\t')
    456  1.40    rillig 	    ind = opt.tabsize * (ind / opt.tabsize + 1);
    457  1.40    rillig 	else if (*p == '\b')
    458  1.40    rillig 	    --ind;
    459  1.40    rillig 	else
    460  1.40    rillig 	    ++ind;
    461  1.40    rillig     }
    462  1.40    rillig     return ind;
    463  1.40    rillig }
    464   1.1       cgd 
    465  1.40    rillig int
    466  1.40    rillig indentation_after(int ind, const char *s)
    467  1.40    rillig {
    468  1.40    rillig     return indentation_after_range(ind, s, NULL);
    469  1.40    rillig }
    470  1.19     kamil 
    471   1.5     lukem void
    472  1.20  christos diag(int level, const char *msg, ...)
    473   1.1       cgd {
    474  1.20  christos     va_list ap;
    475  1.20  christos     const char *s, *e;
    476  1.20  christos 
    477  1.19     kamil     if (level)
    478  1.19     kamil 	found_err = 1;
    479   1.1       cgd 
    480  1.19     kamil     if (output == stdout) {
    481  1.20  christos 	s = "/**INDENT** ";
    482  1.20  christos 	e = " */";
    483  1.20  christos     } else {
    484  1.20  christos 	s = e = "";
    485  1.19     kamil     }
    486   1.1       cgd 
    487  1.20  christos     va_start(ap, msg);
    488  1.20  christos     fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
    489  1.20  christos     vfprintf(stderr, msg, ap);
    490  1.20  christos     fprintf(stderr, "%s\n", e);
    491  1.20  christos     va_end(ap);
    492   1.1       cgd }
    493