Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.100
      1  1.100    rillig /*	$NetBSD: io.c,v 1.100 2021/10/24 10:54:12 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 static char sccsid[] = "@(#)io.c	8.1 (Berkeley) 6/6/93";
     42   1.19     kamil #endif
     43   1.19     kamil 
     44    1.5     lukem #include <sys/cdefs.h>
     45   1.19     kamil #if defined(__NetBSD__)
     46  1.100    rillig __RCSID("$NetBSD: io.c,v 1.100 2021/10/24 10:54:12 rillig Exp $");
     47   1.19     kamil #elif defined(__FreeBSD__)
     48   1.19     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
     49   1.19     kamil #endif
     50    1.1       cgd 
     51    1.5     lukem #include <ctype.h>
     52  1.100    rillig #include <stdarg.h>
     53    1.1       cgd #include <stdio.h>
     54    1.1       cgd #include <string.h>
     55   1.22    rillig 
     56   1.19     kamil #include "indent.h"
     57    1.1       cgd 
     58   1.67    rillig static int paren_indent;
     59   1.94    rillig static bool suppress_blanklines;
     60    1.1       cgd 
     61   1.28    rillig static void
     62   1.28    rillig output_char(char ch)
     63   1.28    rillig {
     64   1.28    rillig     fputc(ch, output);
     65   1.36    rillig     debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
     66   1.28    rillig }
     67   1.28    rillig 
     68   1.28    rillig static void
     69   1.28    rillig output_range(const char *s, const char *e)
     70   1.28    rillig {
     71   1.28    rillig     fwrite(s, 1, (size_t)(e - s), output);
     72   1.36    rillig     debug_vis_range("output_range \"", s, e, "\"\n");
     73   1.28    rillig }
     74   1.28    rillig 
     75   1.29    rillig static inline void
     76   1.28    rillig output_string(const char *s)
     77   1.28    rillig {
     78   1.28    rillig     output_range(s, s + strlen(s));
     79   1.28    rillig }
     80   1.28    rillig 
     81   1.34    rillig static int
     82   1.34    rillig output_indent(int old_ind, int new_ind)
     83   1.34    rillig {
     84   1.34    rillig     int ind = old_ind;
     85   1.34    rillig 
     86   1.34    rillig     if (opt.use_tabs) {
     87   1.34    rillig 	int tabsize = opt.tabsize;
     88   1.34    rillig 	int n = new_ind / tabsize - ind / tabsize;
     89   1.34    rillig 	if (n > 0)
     90   1.34    rillig 	    ind -= ind % tabsize;
     91   1.34    rillig 	for (int i = 0; i < n; i++) {
     92   1.36    rillig 	    fputc('\t', output);
     93   1.34    rillig 	    ind += tabsize;
     94   1.34    rillig 	}
     95   1.34    rillig     }
     96   1.34    rillig 
     97   1.34    rillig     for (; ind < new_ind; ind++)
     98   1.67    rillig 	fputc(' ', output);
     99   1.34    rillig 
    100   1.36    rillig     debug_println("output_indent %d", ind);
    101   1.34    rillig     return ind;
    102   1.34    rillig }
    103   1.34    rillig 
    104   1.86    rillig static int
    105   1.86    rillig dump_line_label(void)
    106   1.86    rillig {
    107   1.86    rillig     int ind;
    108   1.86    rillig 
    109   1.86    rillig     while (lab.e > lab.s && is_hspace(lab.e[-1]))
    110   1.86    rillig 	lab.e--;
    111   1.86    rillig     *lab.e = '\0';
    112   1.86    rillig 
    113   1.86    rillig     ind = output_indent(0, compute_label_indent());
    114   1.86    rillig 
    115   1.86    rillig     if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0
    116   1.86    rillig 	    || strncmp(lab.s, "#endif", 6) == 0)) {
    117   1.86    rillig 	const char *s = lab.s;
    118   1.86    rillig 	if (lab.e[-1] == '\n')
    119   1.86    rillig 	    lab.e--;
    120   1.86    rillig 	do {
    121   1.86    rillig 	    output_char(*s++);
    122   1.86    rillig 	} while (s < lab.e && 'a' <= *s && *s <= 'z');
    123   1.86    rillig 
    124   1.86    rillig 	while (s < lab.e && is_hspace(*s))
    125   1.86    rillig 	    s++;
    126   1.86    rillig 
    127   1.86    rillig 	if (s < lab.e) {
    128   1.86    rillig 	    if (s[0] == '/' && s[1] == '*') {
    129   1.86    rillig 		output_char('\t');
    130   1.86    rillig 		output_range(s, lab.e);
    131   1.86    rillig 	    } else {
    132   1.86    rillig 		output_string("\t/* ");
    133   1.86    rillig 		output_range(s, lab.e);
    134   1.86    rillig 		output_string(" */");
    135   1.86    rillig 	    }
    136   1.86    rillig 	}
    137   1.86    rillig     } else
    138   1.86    rillig 	output_range(lab.s, lab.e);
    139   1.86    rillig     ind = indentation_after(ind, lab.s);
    140   1.86    rillig 
    141   1.86    rillig     ps.is_case_label = false;
    142   1.86    rillig     return ind;
    143   1.86    rillig }
    144   1.86    rillig 
    145   1.86    rillig static int
    146   1.86    rillig dump_line_code(int ind)
    147   1.86    rillig {
    148   1.86    rillig 
    149   1.86    rillig     int target_ind = compute_code_indent();
    150   1.86    rillig     for (int i = 0; i < ps.p_l_follow; i++) {
    151   1.86    rillig 	if (ps.paren_indents[i] >= 0) {
    152   1.86    rillig 	    int paren_ind = ps.paren_indents[i];
    153   1.86    rillig 	    /* XXX: the '+ 1' smells like an off-by-one error. */
    154   1.86    rillig 	    ps.paren_indents[i] = (short)-(paren_ind + target_ind + 1);
    155   1.86    rillig 	    debug_println(
    156   1.86    rillig 		"setting pi[%d] from %d to %d for column %d",
    157   1.86    rillig 		i, paren_ind, ps.paren_indents[i], target_ind + 1);
    158   1.86    rillig 	}
    159   1.86    rillig     }
    160   1.86    rillig 
    161   1.86    rillig     ind = output_indent(ind, target_ind);
    162   1.86    rillig     output_range(code.s, code.e);
    163   1.86    rillig     return indentation_after(ind, code.s);
    164   1.86    rillig }
    165   1.86    rillig 
    166   1.86    rillig static void
    167   1.86    rillig dump_line_comment(int ind)
    168   1.86    rillig {
    169   1.86    rillig     int target_ind = ps.com_ind;
    170   1.92    rillig     const char *p = com.s;
    171   1.86    rillig 
    172   1.86    rillig     target_ind += ps.comment_delta;
    173   1.86    rillig 
    174   1.86    rillig     /* consider original indentation in case this is a box comment */
    175   1.92    rillig     for (; *p == '\t'; p++)
    176   1.92    rillig 	target_ind += opt.tabsize;
    177   1.86    rillig 
    178   1.92    rillig     for (; target_ind < 0; p++) {
    179   1.92    rillig 	if (*p == ' ')
    180   1.92    rillig 	    target_ind++;
    181   1.92    rillig 	else if (*p == '\t')
    182   1.91    rillig 	    target_ind = next_tab(target_ind);
    183   1.92    rillig 	else {
    184   1.86    rillig 	    target_ind = 0;
    185   1.92    rillig 	    break;
    186   1.92    rillig 	}
    187   1.86    rillig     }
    188   1.86    rillig 
    189   1.86    rillig     /* if comment can't fit on this line, put it on next line */
    190   1.86    rillig     if (ind > target_ind) {
    191   1.86    rillig 	output_char('\n');
    192   1.86    rillig 	ind = 0;
    193   1.86    rillig 	ps.stats.lines++;
    194   1.86    rillig     }
    195   1.86    rillig 
    196   1.92    rillig     while (com.e > p && isspace((unsigned char)com.e[-1]))
    197   1.86    rillig 	com.e--;
    198   1.86    rillig 
    199   1.86    rillig     (void)output_indent(ind, target_ind);
    200   1.92    rillig     output_range(p, com.e);
    201   1.86    rillig 
    202   1.86    rillig     ps.comment_delta = ps.n_comment_delta;
    203   1.86    rillig     ps.stats.comment_lines++;
    204   1.86    rillig }
    205   1.86    rillig 
    206   1.26    rillig /*
    207   1.86    rillig  * Write a line of formatted source to the output file. The line consists of a
    208   1.86    rillig  * label, the code and the comment.
    209   1.26    rillig  */
    210    1.5     lukem void
    211   1.11       wiz dump_line(void)
    212   1.26    rillig {
    213   1.90    rillig     static bool first_line = true;
    214   1.19     kamil 
    215   1.60    rillig     if (ps.procname[0] != '\0') {
    216   1.19     kamil 	ps.ind_level = 0;
    217   1.60    rillig 	ps.procname[0] = '\0';
    218   1.19     kamil     }
    219   1.30    rillig 
    220   1.55    rillig     if (code.s == code.e && lab.s == lab.e && com.s == com.e) {
    221   1.94    rillig 	if (suppress_blanklines)
    222   1.94    rillig 	    suppress_blanklines = false;
    223   1.63    rillig 	else
    224   1.98    rillig 	    blank_lines_to_output++;
    225   1.77    rillig 
    226   1.30    rillig     } else if (!inhibit_formatting) {
    227   1.94    rillig 	suppress_blanklines = false;
    228   1.99    rillig 	if (blank_line_before && !first_line) {
    229   1.19     kamil 	    if (opt.swallow_optional_blanklines) {
    230   1.98    rillig 		if (blank_lines_to_output == 1)
    231   1.98    rillig 		    blank_lines_to_output = 0;
    232   1.30    rillig 	    } else {
    233   1.98    rillig 		if (blank_lines_to_output == 0)
    234   1.98    rillig 		    blank_lines_to_output = 1;
    235   1.19     kamil 	    }
    236   1.19     kamil 	}
    237   1.77    rillig 
    238   1.98    rillig 	for (; blank_lines_to_output > 0; blank_lines_to_output--)
    239   1.28    rillig 	    output_char('\n');
    240   1.77    rillig 
    241   1.19     kamil 	if (ps.ind_level == 0)
    242   1.67    rillig 	    ps.ind_stmt = false;	/* this is a class A kludge. don't do
    243   1.67    rillig 					 * additional statement indentation if
    244   1.67    rillig 					 * we are at bracket level 0 */
    245   1.19     kamil 
    246   1.55    rillig 	if (lab.e != lab.s || code.e != code.s)
    247   1.58    rillig 	    ps.stats.code_lines++;
    248   1.19     kamil 
    249   1.86    rillig 	int ind = 0;
    250   1.86    rillig 	if (lab.e != lab.s)
    251   1.86    rillig 	    ind = dump_line_label();
    252   1.86    rillig 	if (code.e != code.s)
    253   1.86    rillig 	    ind = dump_line_code(ind);
    254   1.86    rillig 	if (com.e != com.s)
    255   1.86    rillig 	    dump_line_comment(ind);
    256   1.77    rillig 
    257   1.19     kamil 	if (ps.use_ff)
    258   1.70    rillig 	    output_char('\f');
    259   1.19     kamil 	else
    260   1.28    rillig 	    output_char('\n');
    261   1.58    rillig 	ps.stats.lines++;
    262   1.77    rillig 
    263   1.88    rillig 	if (ps.just_saw_decl == 1 && opt.blanklines_after_decl) {
    264   1.99    rillig 	    blank_line_before = true;
    265   1.19     kamil 	    ps.just_saw_decl = 0;
    266   1.30    rillig 	} else
    267   1.99    rillig 	    blank_line_before = blank_line_after;
    268   1.99    rillig 	blank_line_after = false;
    269   1.19     kamil     }
    270   1.24    rillig 
    271   1.76    rillig     ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    272   1.76    rillig     ps.ind_stmt = ps.in_stmt && !ps.in_decl;
    273   1.19     kamil     ps.use_ff = false;
    274   1.60    rillig     ps.dumped_decl_indent = false;
    275   1.77    rillig 
    276   1.54    rillig     *(lab.e = lab.s) = '\0';	/* reset buffers */
    277   1.55    rillig     *(code.e = code.s) = '\0';
    278   1.52    rillig     *(com.e = com.s = com.buf + 1) = '\0';
    279   1.77    rillig 
    280   1.64    rillig     ps.ind_level = ps.ind_level_follow;
    281   1.19     kamil     ps.paren_level = ps.p_l_follow;
    282   1.77    rillig 
    283   1.36    rillig     if (ps.paren_level > 0) {
    284   1.67    rillig 	/* TODO: explain what negative indentation means */
    285   1.31    rillig 	paren_indent = -ps.paren_indents[ps.paren_level - 1];
    286   1.36    rillig 	debug_println("paren_indent is now %d", paren_indent);
    287   1.36    rillig     }
    288   1.77    rillig 
    289   1.90    rillig     first_line = false;
    290    1.1       cgd }
    291    1.1       cgd 
    292    1.5     lukem int
    293   1.39    rillig compute_code_indent(void)
    294    1.1       cgd {
    295   1.43    rillig     int target_ind = opt.indent_size * ps.ind_level;
    296   1.19     kamil 
    297   1.39    rillig     if (ps.paren_level != 0) {
    298   1.78    rillig 	if (!opt.lineup_to_parens) {
    299   1.49    rillig 	    if (2 * opt.continuation_indent == opt.indent_size)
    300   1.49    rillig 		target_ind += opt.continuation_indent;
    301   1.49    rillig 	    else
    302   1.49    rillig 		target_ind += opt.continuation_indent * ps.paren_level;
    303   1.77    rillig 
    304   1.78    rillig 	} else if (opt.lineup_to_parens_always) {
    305   1.39    rillig 	    /*
    306   1.39    rillig 	     * XXX: where does this '- 1' come from?  It looks strange but is
    307   1.39    rillig 	     * nevertheless needed for proper indentation, as demonstrated in
    308   1.39    rillig 	     * the test opt-lpl.0.
    309   1.39    rillig 	     */
    310   1.39    rillig 	    target_ind = paren_indent - 1;
    311   1.77    rillig 
    312   1.78    rillig 	} else {
    313   1.19     kamil 	    int w;
    314   1.31    rillig 	    int t = paren_indent;
    315    1.1       cgd 
    316   1.55    rillig 	    if ((w = 1 + indentation_after(t - 1, code.s) - opt.max_line_length) > 0
    317   1.55    rillig 		&& 1 + indentation_after(target_ind, code.s) <= opt.max_line_length) {
    318   1.19     kamil 		t -= w + 1;
    319   1.39    rillig 		if (t > target_ind + 1)
    320   1.39    rillig 		    target_ind = t - 1;
    321   1.30    rillig 	    } else
    322   1.39    rillig 		target_ind = t - 1;
    323   1.19     kamil 	}
    324   1.77    rillig 
    325   1.30    rillig     } else if (ps.ind_stmt)
    326   1.39    rillig 	target_ind += opt.continuation_indent;
    327   1.77    rillig 
    328   1.39    rillig     return target_ind;
    329    1.1       cgd }
    330    1.1       cgd 
    331    1.5     lukem int
    332   1.38    rillig compute_label_indent(void)
    333    1.1       cgd {
    334   1.75    rillig     if (ps.is_case_label)
    335   1.74    rillig 	return (int)(case_ind * (float)opt.indent_size);
    336   1.54    rillig     if (lab.s[0] == '#')
    337   1.67    rillig 	return 0;
    338   1.43    rillig     return opt.indent_size * (ps.ind_level - label_offset);
    339    1.1       cgd }
    340    1.1       cgd 
    341   1.53    rillig static void
    342   1.53    rillig skip_hspace(const char **pp)
    343   1.53    rillig {
    344   1.69    rillig     while (is_hspace(**pp))
    345   1.53    rillig 	(*pp)++;
    346   1.53    rillig }
    347   1.53    rillig 
    348   1.73    rillig static bool
    349   1.73    rillig skip_string(const char **pp, const char *s)
    350   1.73    rillig {
    351   1.73    rillig     size_t len = strlen(s);
    352   1.73    rillig     if (strncmp(*pp, s, len) == 0) {
    353   1.73    rillig 	*pp += len;
    354   1.73    rillig 	return true;
    355   1.73    rillig     }
    356   1.73    rillig     return false;
    357   1.73    rillig }
    358   1.73    rillig 
    359   1.53    rillig static void
    360   1.53    rillig parse_indent_comment(void)
    361   1.53    rillig {
    362   1.90    rillig     bool on;
    363   1.53    rillig 
    364   1.79    rillig     const char *p = inp.buf;
    365   1.53    rillig 
    366   1.53    rillig     skip_hspace(&p);
    367   1.73    rillig     if (!skip_string(&p, "/*"))
    368   1.53    rillig 	return;
    369   1.53    rillig     skip_hspace(&p);
    370   1.73    rillig     if (!skip_string(&p, "INDENT"))
    371   1.53    rillig 	return;
    372   1.53    rillig     skip_hspace(&p);
    373   1.77    rillig 
    374   1.73    rillig     if (*p == '*' || skip_string(&p, "ON"))
    375   1.90    rillig 	on = true;
    376   1.73    rillig     else if (skip_string(&p, "OFF"))
    377   1.90    rillig 	on = false;
    378   1.73    rillig     else
    379   1.53    rillig 	return;
    380   1.53    rillig 
    381   1.53    rillig     skip_hspace(&p);
    382   1.73    rillig     if (!skip_string(&p, "*/\n"))
    383   1.53    rillig 	return;
    384   1.53    rillig 
    385   1.55    rillig     if (com.s != com.e || lab.s != lab.e || code.s != code.e)
    386   1.53    rillig 	dump_line();
    387   1.53    rillig 
    388   1.90    rillig     inhibit_formatting = !on;
    389   1.90    rillig     if (on) {
    390   1.98    rillig 	blank_lines_to_output = 0;
    391   1.99    rillig 	blank_line_after = false;
    392   1.99    rillig 	blank_line_before = false;
    393   1.94    rillig 	suppress_blanklines = true;
    394   1.53    rillig     }
    395   1.53    rillig }
    396    1.1       cgd 
    397    1.1       cgd /*
    398    1.1       cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    399    1.5     lukem  *
    400    1.1       cgd  * All rights reserved
    401    1.1       cgd  */
    402    1.5     lukem void
    403   1.89    rillig inbuf_read_line(void)
    404   1.67    rillig {
    405   1.19     kamil     char *p;
    406   1.71    rillig     int ch;
    407   1.19     kamil     FILE *f = input;
    408   1.19     kamil 
    409   1.80    rillig     if (saved_inp_s != NULL) {	/* there is a partly filled input buffer left */
    410   1.80    rillig 	inp.s = saved_inp_s;	/* do not read anything, just switch buffers */
    411   1.80    rillig 	inp.e = saved_inp_e;
    412   1.80    rillig 	saved_inp_s = saved_inp_e = NULL;
    413   1.80    rillig 	debug_println("switched inp.s back to saved_inp_s");
    414   1.79    rillig 	if (inp.s < inp.e)
    415   1.19     kamil 	    return;		/* only return if there is really something in
    416    1.1       cgd 				 * this buffer */
    417   1.19     kamil     }
    418   1.77    rillig 
    419   1.79    rillig     for (p = inp.buf;;) {
    420   1.79    rillig 	if (p >= inp.l) {
    421   1.79    rillig 	    size_t size = (size_t)(inp.l - inp.buf) * 2 + 10;
    422   1.79    rillig 	    size_t offset = (size_t)(p - inp.buf);
    423   1.79    rillig 	    inp.buf = xrealloc(inp.buf, size);
    424   1.79    rillig 	    p = inp.buf + offset;
    425   1.79    rillig 	    inp.l = inp.buf + size - 2;
    426   1.19     kamil 	}
    427   1.77    rillig 
    428   1.71    rillig 	if ((ch = getc(f)) == EOF) {
    429   1.95    rillig 	    if (!inhibit_formatting) {
    430   1.95    rillig 		*p++ = ' ';
    431   1.95    rillig 		*p++ = '\n';
    432   1.95    rillig 	    }
    433   1.37    rillig 	    had_eof = true;
    434   1.37    rillig 	    break;
    435    1.1       cgd 	}
    436   1.77    rillig 
    437   1.71    rillig 	if (ch != '\0')
    438   1.71    rillig 	    *p++ = (char)ch;
    439   1.71    rillig 	if (ch == '\n')
    440   1.37    rillig 	    break;
    441   1.19     kamil     }
    442   1.77    rillig 
    443   1.79    rillig     inp.s = inp.buf;
    444   1.79    rillig     inp.e = p;
    445   1.77    rillig 
    446   1.89    rillig     if (p - inp.buf >= 3 && p[-3] == '*' && p[-2] == '/') {
    447   1.89    rillig 	if (strncmp(inp.buf, "/**INDENT**", 11) == 0)
    448   1.89    rillig 	    inbuf_read_line();	/* flush indent error message */
    449   1.53    rillig 	else
    450   1.53    rillig 	    parse_indent_comment();
    451   1.19     kamil     }
    452   1.77    rillig 
    453   1.96    rillig     if (inhibit_formatting)
    454   1.96    rillig 	output_range(inp.s, inp.e);
    455    1.1       cgd }
    456   1.19     kamil 
    457    1.1       cgd int
    458   1.40    rillig indentation_after_range(int ind, const char *start, const char *end)
    459    1.1       cgd {
    460   1.40    rillig     for (const char *p = start; *p != '\0' && p != end; ++p) {
    461   1.40    rillig 	if (*p == '\n' || *p == '\f')
    462   1.40    rillig 	    ind = 0;
    463   1.40    rillig 	else if (*p == '\t')
    464   1.91    rillig 	    ind = next_tab(ind);
    465   1.40    rillig 	else if (*p == '\b')
    466   1.40    rillig 	    --ind;
    467   1.40    rillig 	else
    468   1.40    rillig 	    ++ind;
    469   1.40    rillig     }
    470   1.40    rillig     return ind;
    471   1.40    rillig }
    472    1.1       cgd 
    473   1.40    rillig int
    474   1.40    rillig indentation_after(int ind, const char *s)
    475   1.40    rillig {
    476   1.40    rillig     return indentation_after_range(ind, s, NULL);
    477   1.40    rillig }
    478   1.19     kamil 
    479    1.5     lukem void
    480   1.20  christos diag(int level, const char *msg, ...)
    481    1.1       cgd {
    482   1.20  christos     va_list ap;
    483   1.20  christos     const char *s, *e;
    484   1.20  christos 
    485   1.60    rillig     if (level != 0)
    486   1.65    rillig 	found_err = true;
    487    1.1       cgd 
    488   1.19     kamil     if (output == stdout) {
    489   1.20  christos 	s = "/**INDENT** ";
    490   1.20  christos 	e = " */";
    491   1.20  christos     } else {
    492   1.20  christos 	s = e = "";
    493   1.19     kamil     }
    494    1.1       cgd 
    495   1.20  christos     va_start(ap, msg);
    496   1.20  christos     fprintf(stderr, "%s%s@%d: ", s, level == 0 ? "Warning" : "Error", line_no);
    497   1.20  christos     vfprintf(stderr, msg, ap);
    498   1.20  christos     fprintf(stderr, "%s\n", e);
    499   1.20  christos     va_end(ap);
    500    1.1       cgd }
    501