Home | History | Annotate | Line # | Download | only in indent
io.c revision 1.128
      1  1.128  rillig /*	$NetBSD: io.c,v 1.128 2021/11/19 20:13:05 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.128  rillig __RCSID("$NetBSD: io.c,v 1.128 2021/11/19 20:13:05 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.121  rillig #include <assert.h>
     52    1.5   lukem #include <ctype.h>
     53    1.1     cgd #include <stdio.h>
     54  1.120  rillig #include <stdlib.h>
     55    1.1     cgd #include <string.h>
     56   1.22  rillig 
     57   1.19   kamil #include "indent.h"
     58    1.1     cgd 
     59  1.128  rillig static struct {
     60  1.124  rillig     struct buffer inp;		/* one line of input, ready to be split into
     61  1.124  rillig 				 * tokens; occasionally this buffer switches
     62  1.124  rillig 				 * to save_com_buf */
     63  1.124  rillig     char save_com_buf[5000];	/* input text is saved here when looking for
     64  1.124  rillig 				 * the brace after an if, while, etc */
     65  1.124  rillig     char *save_com_s;		/* start of the comment in save_com_buf */
     66  1.124  rillig     char *save_com_e;		/* end of the comment in save_com_buf */
     67  1.124  rillig 
     68  1.124  rillig     char *saved_inp_s;		/* saved value of inp.s when taking input from
     69  1.124  rillig 				 * save_com */
     70  1.128  rillig     char *saved_inp_e;		/* saved value of inp.e */
     71  1.124  rillig } inbuf;
     72  1.120  rillig 
     73   1.67  rillig static int paren_indent;
     74   1.94  rillig static bool suppress_blanklines;
     75    1.1     cgd 
     76  1.118  rillig 
     77  1.122  rillig void
     78  1.122  rillig inp_init(void)
     79  1.122  rillig {
     80  1.122  rillig     inbuf.inp.buf = xmalloc(10);
     81  1.122  rillig     inbuf.inp.l = inbuf.inp.buf + 8;
     82  1.122  rillig     inbuf.inp.s = inbuf.inp.buf;
     83  1.122  rillig     inbuf.inp.e = inbuf.inp.buf;
     84  1.122  rillig }
     85  1.122  rillig 
     86  1.119  rillig const char *
     87  1.119  rillig inp_p(void)
     88  1.119  rillig {
     89  1.119  rillig     return inbuf.inp.s;
     90  1.119  rillig }
     91  1.119  rillig 
     92  1.119  rillig const char *
     93  1.123  rillig inp_line_start(void)
     94  1.123  rillig {
     95  1.123  rillig     /*
     96  1.123  rillig      * The comment we're about to read usually comes from inp.buf, unless
     97  1.123  rillig      * it has been copied into save_com.
     98  1.123  rillig      *
     99  1.123  rillig      * XXX: ordered comparison between pointers from different objects
    100  1.123  rillig      * invokes undefined behavior (C99 6.5.8).
    101  1.123  rillig      */
    102  1.123  rillig     return inbuf.inp.s >= inbuf.save_com_buf &&
    103  1.123  rillig 	inbuf.inp.s < inbuf.save_com_buf + array_length(inbuf.save_com_buf)
    104  1.123  rillig 	? inbuf.save_com_buf : inbuf.inp.buf;
    105  1.123  rillig }
    106  1.123  rillig 
    107  1.123  rillig const char *
    108  1.119  rillig inp_line_end(void)
    109  1.119  rillig {
    110  1.119  rillig     return inbuf.inp.e;
    111  1.119  rillig }
    112  1.119  rillig 
    113  1.118  rillig char
    114  1.118  rillig inp_peek(void)
    115  1.118  rillig {
    116  1.118  rillig     return *inbuf.inp.s;
    117  1.118  rillig }
    118  1.118  rillig 
    119  1.118  rillig char
    120  1.118  rillig inp_lookahead(size_t i)
    121  1.118  rillig {
    122  1.118  rillig     return inbuf.inp.s[i];
    123  1.118  rillig }
    124  1.118  rillig 
    125  1.118  rillig void
    126  1.118  rillig inp_skip(void)
    127  1.118  rillig {
    128  1.118  rillig     inbuf.inp.s++;
    129  1.118  rillig     if (inbuf.inp.s >= inbuf.inp.e)
    130  1.118  rillig 	inp_read_line();
    131  1.118  rillig }
    132  1.118  rillig 
    133  1.118  rillig char
    134  1.118  rillig inp_next(void)
    135  1.118  rillig {
    136  1.118  rillig     char ch = inp_peek();
    137  1.118  rillig     inp_skip();
    138  1.118  rillig     return ch;
    139  1.118  rillig }
    140  1.118  rillig 
    141  1.120  rillig #ifdef debug
    142  1.120  rillig void
    143  1.120  rillig debug_inp(const char *prefix)
    144  1.120  rillig {
    145  1.120  rillig     debug_printf("%s:", prefix);
    146  1.120  rillig     debug_vis_range(" inp \"", inbuf.inp.s, inbuf.inp.e, "\"");
    147  1.120  rillig     if (inbuf.save_com_s != NULL)
    148  1.120  rillig 	debug_vis_range(" save_com \"",
    149  1.120  rillig 			inbuf.save_com_s, inbuf.save_com_e, "\"");
    150  1.120  rillig     if (inbuf.saved_inp_s != NULL)
    151  1.120  rillig 	debug_vis_range(" saved_inp \"",
    152  1.120  rillig 			inbuf.saved_inp_s, inbuf.saved_inp_e, "\"");
    153  1.120  rillig     debug_printf("\n");
    154  1.120  rillig }
    155  1.120  rillig #endif
    156  1.120  rillig 
    157  1.120  rillig static void
    158  1.120  rillig inp_comment_check_size(size_t n)
    159  1.120  rillig {
    160  1.120  rillig     if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <=
    161  1.120  rillig 	array_length(inbuf.save_com_buf))
    162  1.120  rillig 	return;
    163  1.120  rillig 
    164  1.120  rillig     diag(1, "Internal buffer overflow - "
    165  1.120  rillig 	    "Move big comment from right after if, while, or whatever");
    166  1.120  rillig     fflush(output);
    167  1.120  rillig     exit(1);
    168  1.120  rillig }
    169  1.120  rillig 
    170  1.120  rillig void
    171  1.121  rillig inp_comment_init_newline(void)
    172  1.121  rillig {
    173  1.121  rillig     if (inbuf.save_com_e != NULL)
    174  1.121  rillig 	return;
    175  1.121  rillig 
    176  1.121  rillig     inbuf.save_com_s = inbuf.save_com_buf;
    177  1.121  rillig     inbuf.save_com_s[0] = ' ';	/* see search_stmt_lbrace */
    178  1.121  rillig     inbuf.save_com_s[1] = ' ';	/* see search_stmt_lbrace */
    179  1.121  rillig     inbuf.save_com_e = &inbuf.save_com_s[2];
    180  1.121  rillig     debug_inp(__func__);
    181  1.121  rillig }
    182  1.121  rillig 
    183  1.121  rillig void
    184  1.121  rillig inp_comment_init_comment(void)
    185  1.121  rillig {
    186  1.121  rillig     if (inbuf.save_com_e != NULL)
    187  1.121  rillig 	return;
    188  1.121  rillig 
    189  1.121  rillig     /*
    190  1.121  rillig      * Copy everything from the start of the line, because
    191  1.121  rillig      * process_comment() will use that to calculate the original
    192  1.121  rillig      * indentation of a boxed comment.
    193  1.121  rillig      */
    194  1.121  rillig     /*
    195  1.121  rillig      * TODO: Don't store anything in the memory range [input.inp.buf,
    196  1.121  rillig      * input.inp.s), as that data can easily get lost.
    197  1.121  rillig      */
    198  1.121  rillig     /*
    199  1.126  rillig      * FIXME: The '4' below is completely wrong. For example, in the snippet
    200  1.121  rillig      * 'if(expr)/''*comment', the 'r)' of the code is not copied. If there
    201  1.121  rillig      * is an additional line break before the ')', memcpy tries to copy
    202  1.121  rillig      * (size_t)-1 bytes.
    203  1.126  rillig      *
    204  1.126  rillig      * The original author of this magic number doesn't remember its purpose
    205  1.126  rillig      * anymore, so there is no point in keeping it. The existing tests must
    206  1.126  rillig      * still pass though.
    207  1.121  rillig      */
    208  1.121  rillig     assert((size_t)(inbuf.inp.s - inbuf.inp.buf) >= 4);
    209  1.121  rillig     size_t line_len = (size_t)(inbuf.inp.s - inbuf.inp.buf) - 4;
    210  1.121  rillig     assert(line_len < array_length(inbuf.save_com_buf));
    211  1.121  rillig     memcpy(inbuf.save_com_buf, inbuf.inp.buf, line_len);
    212  1.121  rillig     inbuf.save_com_s = inbuf.save_com_buf + line_len;
    213  1.121  rillig     inbuf.save_com_s[0] = ' ';	/* see search_stmt_lbrace */
    214  1.121  rillig     inbuf.save_com_s[1] = ' ';	/* see search_stmt_lbrace */
    215  1.121  rillig     inbuf.save_com_e = &inbuf.save_com_s[2];
    216  1.121  rillig     debug_vis_range("search_stmt_comment: before save_com is \"",
    217  1.126  rillig 	inbuf.save_com_buf, inbuf.save_com_s, "\"\n");
    218  1.121  rillig     debug_vis_range("search_stmt_comment: save_com is \"",
    219  1.126  rillig 	inbuf.save_com_s, inbuf.save_com_e, "\"\n");
    220  1.121  rillig }
    221  1.121  rillig 
    222  1.121  rillig void
    223  1.122  rillig inp_comment_init_preproc(void)
    224  1.122  rillig {
    225  1.122  rillig     if (inbuf.save_com_e == NULL) {	/* if this is the first comment, we
    226  1.122  rillig 					 * must set up the buffer */
    227  1.122  rillig 	inbuf.save_com_s = inbuf.save_com_buf;
    228  1.122  rillig 	inbuf.save_com_e = inbuf.save_com_s;
    229  1.122  rillig     } else {
    230  1.122  rillig 	inp_comment_add_char('\n');	/* add newline between comments */
    231  1.122  rillig 	inp_comment_add_char(' ');
    232  1.122  rillig 	--line_no;
    233  1.122  rillig     }
    234  1.122  rillig }
    235  1.122  rillig 
    236  1.122  rillig void
    237  1.120  rillig inp_comment_add_char(char ch)
    238  1.120  rillig {
    239  1.120  rillig     inp_comment_check_size(1);
    240  1.120  rillig     *inbuf.save_com_e++ = ch;
    241  1.120  rillig }
    242  1.120  rillig 
    243  1.120  rillig void
    244  1.120  rillig inp_comment_add_range(const char *s, const char *e)
    245  1.120  rillig {
    246  1.120  rillig     size_t len = (size_t)(e - s);
    247  1.120  rillig     inp_comment_check_size(len);
    248  1.120  rillig     memcpy(inbuf.save_com_e, s, len);
    249  1.120  rillig     inbuf.save_com_e += len;
    250  1.120  rillig }
    251  1.120  rillig 
    252  1.121  rillig bool
    253  1.121  rillig inp_comment_complete_block(void)
    254  1.121  rillig {
    255  1.121  rillig     return inbuf.save_com_e[-2] == '*' && inbuf.save_com_e[-1] == '/';
    256  1.121  rillig }
    257  1.121  rillig 
    258  1.122  rillig bool
    259  1.122  rillig inp_comment_seen(void)
    260  1.122  rillig {
    261  1.122  rillig     return inbuf.save_com_e != NULL;
    262  1.122  rillig }
    263  1.122  rillig 
    264  1.122  rillig void
    265  1.122  rillig inp_comment_rtrim(void)
    266  1.122  rillig {
    267  1.122  rillig     while (inbuf.save_com_e > inbuf.save_com_s && ch_isblank(inbuf.save_com_e[-1]))
    268  1.122  rillig 	inbuf.save_com_e--;
    269  1.122  rillig }
    270  1.122  rillig 
    271  1.122  rillig void
    272  1.122  rillig inp_comment_rtrim_newline(void)
    273  1.122  rillig {
    274  1.122  rillig     while (inbuf.save_com_e > inbuf.save_com_s && inbuf.save_com_e[-1] == '\n')
    275  1.122  rillig 	inbuf.save_com_e--;
    276  1.122  rillig }
    277  1.122  rillig 
    278  1.120  rillig void
    279  1.120  rillig inp_from_comment(void)
    280  1.120  rillig {
    281  1.120  rillig     inbuf.saved_inp_s = inbuf.inp.s;
    282  1.120  rillig     inbuf.saved_inp_e = inbuf.inp.e;
    283  1.120  rillig 
    284  1.120  rillig     inbuf.inp.s = inbuf.save_com_s;	/* redirect lexi input to save_com_s */
    285  1.120  rillig     inbuf.inp.e = inbuf.save_com_e;
    286  1.128  rillig     inbuf.save_com_s = NULL;
    287  1.120  rillig     inbuf.save_com_e = NULL;
    288  1.120  rillig     debug_inp(__func__);
    289  1.120  rillig }
    290  1.118  rillig 
    291  1.122  rillig void
    292  1.122  rillig inp_comment_insert_lbrace(void)
    293  1.122  rillig {
    294  1.122  rillig     assert(inbuf.save_com_s[0] == ' ');	/* see inp_comment_init_newline */
    295  1.122  rillig     inbuf.save_com_s[0] = '{';
    296  1.122  rillig }
    297  1.121  rillig 
    298   1.28  rillig static void
    299   1.28  rillig output_char(char ch)
    300   1.28  rillig {
    301   1.28  rillig     fputc(ch, output);
    302   1.36  rillig     debug_vis_range("output_char '", &ch, &ch + 1, "'\n");
    303   1.28  rillig }
    304   1.28  rillig 
    305   1.28  rillig static void
    306   1.28  rillig output_range(const char *s, const char *e)
    307   1.28  rillig {
    308   1.28  rillig     fwrite(s, 1, (size_t)(e - s), output);
    309   1.36  rillig     debug_vis_range("output_range \"", s, e, "\"\n");
    310   1.28  rillig }
    311   1.28  rillig 
    312   1.29  rillig static inline void
    313   1.28  rillig output_string(const char *s)
    314   1.28  rillig {
    315   1.28  rillig     output_range(s, s + strlen(s));
    316   1.28  rillig }
    317   1.28  rillig 
    318   1.34  rillig static int
    319   1.34  rillig output_indent(int old_ind, int new_ind)
    320   1.34  rillig {
    321   1.34  rillig     int ind = old_ind;
    322   1.34  rillig 
    323   1.34  rillig     if (opt.use_tabs) {
    324   1.34  rillig 	int tabsize = opt.tabsize;
    325   1.34  rillig 	int n = new_ind / tabsize - ind / tabsize;
    326   1.34  rillig 	if (n > 0)
    327   1.34  rillig 	    ind -= ind % tabsize;
    328   1.34  rillig 	for (int i = 0; i < n; i++) {
    329   1.36  rillig 	    fputc('\t', output);
    330   1.34  rillig 	    ind += tabsize;
    331   1.34  rillig 	}
    332   1.34  rillig     }
    333   1.34  rillig 
    334   1.34  rillig     for (; ind < new_ind; ind++)
    335   1.67  rillig 	fputc(' ', output);
    336   1.34  rillig 
    337   1.36  rillig     debug_println("output_indent %d", ind);
    338   1.34  rillig     return ind;
    339   1.34  rillig }
    340   1.34  rillig 
    341   1.86  rillig static int
    342   1.86  rillig dump_line_label(void)
    343   1.86  rillig {
    344   1.86  rillig     int ind;
    345   1.86  rillig 
    346  1.107  rillig     while (lab.e > lab.s && ch_isblank(lab.e[-1]))
    347   1.86  rillig 	lab.e--;
    348   1.86  rillig     *lab.e = '\0';
    349   1.86  rillig 
    350   1.86  rillig     ind = output_indent(0, compute_label_indent());
    351   1.86  rillig 
    352   1.86  rillig     if (lab.s[0] == '#' && (strncmp(lab.s, "#else", 5) == 0
    353   1.86  rillig 	    || strncmp(lab.s, "#endif", 6) == 0)) {
    354   1.86  rillig 	const char *s = lab.s;
    355   1.86  rillig 	if (lab.e[-1] == '\n')
    356   1.86  rillig 	    lab.e--;
    357   1.86  rillig 	do {
    358   1.86  rillig 	    output_char(*s++);
    359   1.86  rillig 	} while (s < lab.e && 'a' <= *s && *s <= 'z');
    360   1.86  rillig 
    361  1.107  rillig 	while (s < lab.e && ch_isblank(*s))
    362   1.86  rillig 	    s++;
    363   1.86  rillig 
    364   1.86  rillig 	if (s < lab.e) {
    365   1.86  rillig 	    if (s[0] == '/' && s[1] == '*') {
    366   1.86  rillig 		output_char('\t');
    367   1.86  rillig 		output_range(s, lab.e);
    368   1.86  rillig 	    } else {
    369   1.86  rillig 		output_string("\t/* ");
    370   1.86  rillig 		output_range(s, lab.e);
    371   1.86  rillig 		output_string(" */");
    372   1.86  rillig 	    }
    373   1.86  rillig 	}
    374   1.86  rillig     } else
    375   1.86  rillig 	output_range(lab.s, lab.e);
    376  1.109  rillig     ind = ind_add(ind, lab.s, lab.e);
    377   1.86  rillig 
    378   1.86  rillig     ps.is_case_label = false;
    379   1.86  rillig     return ind;
    380   1.86  rillig }
    381   1.86  rillig 
    382   1.86  rillig static int
    383   1.86  rillig dump_line_code(int ind)
    384   1.86  rillig {
    385   1.86  rillig 
    386   1.86  rillig     int target_ind = compute_code_indent();
    387   1.86  rillig     for (int i = 0; i < ps.p_l_follow; i++) {
    388   1.86  rillig 	if (ps.paren_indents[i] >= 0) {
    389   1.86  rillig 	    int paren_ind = ps.paren_indents[i];
    390  1.115  rillig 	    ps.paren_indents[i] = (short)(-1 - (paren_ind + target_ind));
    391   1.86  rillig 	    debug_println(
    392  1.105  rillig 		"setting paren_indents[%d] from %d to %d for column %d",
    393   1.86  rillig 		i, paren_ind, ps.paren_indents[i], target_ind + 1);
    394   1.86  rillig 	}
    395   1.86  rillig     }
    396   1.86  rillig 
    397   1.86  rillig     ind = output_indent(ind, target_ind);
    398   1.86  rillig     output_range(code.s, code.e);
    399  1.109  rillig     return ind_add(ind, code.s, code.e);
    400   1.86  rillig }
    401   1.86  rillig 
    402   1.86  rillig static void
    403   1.86  rillig dump_line_comment(int ind)
    404   1.86  rillig {
    405   1.86  rillig     int target_ind = ps.com_ind;
    406   1.92  rillig     const char *p = com.s;
    407   1.86  rillig 
    408   1.86  rillig     target_ind += ps.comment_delta;
    409   1.86  rillig 
    410   1.86  rillig     /* consider original indentation in case this is a box comment */
    411   1.92  rillig     for (; *p == '\t'; p++)
    412   1.92  rillig 	target_ind += opt.tabsize;
    413   1.86  rillig 
    414   1.92  rillig     for (; target_ind < 0; p++) {
    415   1.92  rillig 	if (*p == ' ')
    416   1.92  rillig 	    target_ind++;
    417   1.92  rillig 	else if (*p == '\t')
    418   1.91  rillig 	    target_ind = next_tab(target_ind);
    419   1.92  rillig 	else {
    420   1.86  rillig 	    target_ind = 0;
    421   1.92  rillig 	    break;
    422   1.92  rillig 	}
    423   1.86  rillig     }
    424   1.86  rillig 
    425  1.105  rillig     /* if comment can't fit on this line, put it on the next line */
    426   1.86  rillig     if (ind > target_ind) {
    427   1.86  rillig 	output_char('\n');
    428   1.86  rillig 	ind = 0;
    429   1.86  rillig 	ps.stats.lines++;
    430   1.86  rillig     }
    431   1.86  rillig 
    432   1.92  rillig     while (com.e > p && isspace((unsigned char)com.e[-1]))
    433   1.86  rillig 	com.e--;
    434   1.86  rillig 
    435   1.86  rillig     (void)output_indent(ind, target_ind);
    436   1.92  rillig     output_range(p, com.e);
    437   1.86  rillig 
    438   1.86  rillig     ps.comment_delta = ps.n_comment_delta;
    439   1.86  rillig     ps.stats.comment_lines++;
    440   1.86  rillig }
    441   1.86  rillig 
    442   1.26  rillig /*
    443  1.105  rillig  * Write a line of formatted source to the output file. The line consists of
    444  1.105  rillig  * the label, the code and the comment.
    445   1.26  rillig  */
    446  1.101  rillig static void
    447  1.101  rillig output_line(char line_terminator)
    448   1.26  rillig {
    449   1.90  rillig     static bool first_line = true;
    450   1.19   kamil 
    451  1.125  rillig     ps.is_function_definition = false;
    452   1.30  rillig 
    453   1.55  rillig     if (code.s == code.e && lab.s == lab.e && com.s == com.e) {
    454   1.94  rillig 	if (suppress_blanklines)
    455   1.94  rillig 	    suppress_blanklines = false;
    456   1.63  rillig 	else
    457   1.98  rillig 	    blank_lines_to_output++;
    458   1.77  rillig 
    459   1.30  rillig     } else if (!inhibit_formatting) {
    460   1.94  rillig 	suppress_blanklines = false;
    461   1.99  rillig 	if (blank_line_before && !first_line) {
    462   1.19   kamil 	    if (opt.swallow_optional_blanklines) {
    463   1.98  rillig 		if (blank_lines_to_output == 1)
    464   1.98  rillig 		    blank_lines_to_output = 0;
    465   1.30  rillig 	    } else {
    466   1.98  rillig 		if (blank_lines_to_output == 0)
    467   1.98  rillig 		    blank_lines_to_output = 1;
    468   1.19   kamil 	    }
    469   1.19   kamil 	}
    470   1.77  rillig 
    471   1.98  rillig 	for (; blank_lines_to_output > 0; blank_lines_to_output--)
    472   1.28  rillig 	    output_char('\n');
    473   1.77  rillig 
    474   1.19   kamil 	if (ps.ind_level == 0)
    475   1.67  rillig 	    ps.ind_stmt = false;	/* this is a class A kludge. don't do
    476   1.67  rillig 					 * additional statement indentation if
    477   1.67  rillig 					 * we are at bracket level 0 */
    478   1.19   kamil 
    479   1.55  rillig 	if (lab.e != lab.s || code.e != code.s)
    480   1.58  rillig 	    ps.stats.code_lines++;
    481   1.19   kamil 
    482   1.86  rillig 	int ind = 0;
    483   1.86  rillig 	if (lab.e != lab.s)
    484   1.86  rillig 	    ind = dump_line_label();
    485   1.86  rillig 	if (code.e != code.s)
    486   1.86  rillig 	    ind = dump_line_code(ind);
    487   1.86  rillig 	if (com.e != com.s)
    488   1.86  rillig 	    dump_line_comment(ind);
    489   1.77  rillig 
    490  1.101  rillig 	output_char(line_terminator);
    491   1.58  rillig 	ps.stats.lines++;
    492   1.77  rillig 
    493   1.88  rillig 	if (ps.just_saw_decl == 1 && opt.blanklines_after_decl) {
    494   1.99  rillig 	    blank_line_before = true;
    495   1.19   kamil 	    ps.just_saw_decl = 0;
    496   1.30  rillig 	} else
    497   1.99  rillig 	    blank_line_before = blank_line_after;
    498   1.99  rillig 	blank_line_after = false;
    499   1.19   kamil     }
    500   1.24  rillig 
    501   1.76  rillig     ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
    502   1.76  rillig     ps.ind_stmt = ps.in_stmt && !ps.in_decl;
    503  1.104  rillig     ps.decl_indent_done = false;
    504   1.77  rillig 
    505   1.54  rillig     *(lab.e = lab.s) = '\0';	/* reset buffers */
    506   1.55  rillig     *(code.e = code.s) = '\0';
    507   1.52  rillig     *(com.e = com.s = com.buf + 1) = '\0';
    508   1.77  rillig 
    509   1.64  rillig     ps.ind_level = ps.ind_level_follow;
    510   1.19   kamil     ps.paren_level = ps.p_l_follow;
    511   1.77  rillig 
    512   1.36  rillig     if (ps.paren_level > 0) {
    513   1.67  rillig 	/* TODO: explain what negative indentation means */
    514  1.112  rillig 	paren_indent = -1 - ps.paren_indents[ps.paren_level - 1];
    515   1.36  rillig 	debug_println("paren_indent is now %d", paren_indent);
    516   1.36  rillig     }
    517   1.77  rillig 
    518   1.90  rillig     first_line = false;
    519    1.1     cgd }
    520    1.1     cgd 
    521  1.101  rillig void
    522  1.101  rillig dump_line(void)
    523  1.101  rillig {
    524  1.101  rillig     output_line('\n');
    525  1.101  rillig }
    526  1.101  rillig 
    527  1.101  rillig void
    528  1.101  rillig dump_line_ff(void)
    529  1.101  rillig {
    530  1.101  rillig     output_line('\f');
    531  1.101  rillig }
    532  1.101  rillig 
    533  1.113  rillig static int
    534  1.113  rillig compute_code_indent_lineup(int base_ind)
    535  1.113  rillig {
    536  1.113  rillig     int ti = paren_indent;
    537  1.113  rillig     int overflow = ind_add(ti, code.s, code.e) - opt.max_line_length;
    538  1.113  rillig     if (overflow < 0)
    539  1.113  rillig 	return ti;
    540  1.113  rillig 
    541  1.113  rillig     if (ind_add(base_ind, code.s, code.e) < opt.max_line_length) {
    542  1.113  rillig 	ti -= overflow + 2;
    543  1.113  rillig 	if (ti > base_ind)
    544  1.113  rillig 	    return ti;
    545  1.113  rillig 	return base_ind;
    546  1.113  rillig     }
    547  1.113  rillig 
    548  1.113  rillig     return ti;
    549  1.113  rillig }
    550  1.113  rillig 
    551    1.5   lukem int
    552   1.39  rillig compute_code_indent(void)
    553    1.1     cgd {
    554  1.110  rillig     int base_ind = ps.ind_level * opt.indent_size;
    555   1.19   kamil 
    556  1.110  rillig     if (ps.paren_level == 0) {
    557  1.110  rillig 	if (ps.ind_stmt)
    558  1.110  rillig 	    return base_ind + opt.continuation_indent;
    559  1.110  rillig 	return base_ind;
    560  1.110  rillig     }
    561   1.77  rillig 
    562  1.110  rillig     if (opt.lineup_to_parens) {
    563  1.112  rillig 	if (opt.lineup_to_parens_always)
    564  1.112  rillig 	    return paren_indent;
    565  1.113  rillig 	return compute_code_indent_lineup(base_ind);
    566  1.110  rillig     }
    567   1.77  rillig 
    568  1.110  rillig     if (2 * opt.continuation_indent == opt.indent_size)
    569  1.110  rillig 	return base_ind + opt.continuation_indent;
    570  1.110  rillig     else
    571  1.110  rillig 	return base_ind + opt.continuation_indent * ps.paren_level;
    572    1.1     cgd }
    573    1.1     cgd 
    574    1.5   lukem int
    575   1.38  rillig compute_label_indent(void)
    576    1.1     cgd {
    577   1.75  rillig     if (ps.is_case_label)
    578   1.74  rillig 	return (int)(case_ind * (float)opt.indent_size);
    579   1.54  rillig     if (lab.s[0] == '#')
    580   1.67  rillig 	return 0;
    581  1.108  rillig     return opt.indent_size * (ps.ind_level - 2);
    582    1.1     cgd }
    583    1.1     cgd 
    584   1.53  rillig static void
    585  1.107  rillig skip_blank(const char **pp)
    586   1.53  rillig {
    587  1.107  rillig     while (ch_isblank(**pp))
    588   1.53  rillig 	(*pp)++;
    589   1.53  rillig }
    590   1.53  rillig 
    591   1.73  rillig static bool
    592   1.73  rillig skip_string(const char **pp, const char *s)
    593   1.73  rillig {
    594   1.73  rillig     size_t len = strlen(s);
    595   1.73  rillig     if (strncmp(*pp, s, len) == 0) {
    596   1.73  rillig 	*pp += len;
    597   1.73  rillig 	return true;
    598   1.73  rillig     }
    599   1.73  rillig     return false;
    600   1.73  rillig }
    601   1.73  rillig 
    602   1.53  rillig static void
    603   1.53  rillig parse_indent_comment(void)
    604   1.53  rillig {
    605   1.90  rillig     bool on;
    606   1.53  rillig 
    607  1.117  rillig     const char *p = inbuf.inp.buf;
    608   1.53  rillig 
    609  1.107  rillig     skip_blank(&p);
    610   1.73  rillig     if (!skip_string(&p, "/*"))
    611   1.53  rillig 	return;
    612  1.107  rillig     skip_blank(&p);
    613   1.73  rillig     if (!skip_string(&p, "INDENT"))
    614   1.53  rillig 	return;
    615  1.107  rillig     skip_blank(&p);
    616   1.77  rillig 
    617   1.73  rillig     if (*p == '*' || skip_string(&p, "ON"))
    618   1.90  rillig 	on = true;
    619   1.73  rillig     else if (skip_string(&p, "OFF"))
    620   1.90  rillig 	on = false;
    621   1.73  rillig     else
    622   1.53  rillig 	return;
    623   1.53  rillig 
    624  1.107  rillig     skip_blank(&p);
    625   1.73  rillig     if (!skip_string(&p, "*/\n"))
    626   1.53  rillig 	return;
    627   1.53  rillig 
    628   1.55  rillig     if (com.s != com.e || lab.s != lab.e || code.s != code.e)
    629   1.53  rillig 	dump_line();
    630   1.53  rillig 
    631   1.90  rillig     inhibit_formatting = !on;
    632   1.90  rillig     if (on) {
    633   1.98  rillig 	blank_lines_to_output = 0;
    634   1.99  rillig 	blank_line_after = false;
    635   1.99  rillig 	blank_line_before = false;
    636   1.94  rillig 	suppress_blanklines = true;
    637   1.53  rillig     }
    638   1.53  rillig }
    639    1.1     cgd 
    640    1.1     cgd /*
    641    1.1     cgd  * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
    642    1.5   lukem  *
    643    1.1     cgd  * All rights reserved
    644    1.1     cgd  */
    645    1.5   lukem void
    646  1.116  rillig inp_read_line(void)
    647   1.67  rillig {
    648   1.19   kamil     char *p;
    649   1.71  rillig     int ch;
    650   1.19   kamil     FILE *f = input;
    651   1.19   kamil 
    652  1.117  rillig     if (inbuf.saved_inp_s != NULL) {	/* there is a partly filled input buffer left */
    653  1.117  rillig 	inbuf.inp.s = inbuf.saved_inp_s;	/* do not read anything, just switch buffers */
    654  1.117  rillig 	inbuf.inp.e = inbuf.saved_inp_e;
    655  1.117  rillig 	inbuf.saved_inp_s = inbuf.saved_inp_e = NULL;
    656   1.80  rillig 	debug_println("switched inp.s back to saved_inp_s");
    657  1.117  rillig 	if (inbuf.inp.s < inbuf.inp.e)
    658   1.19   kamil 	    return;		/* only return if there is really something in
    659    1.1     cgd 				 * this buffer */
    660   1.19   kamil     }
    661   1.77  rillig 
    662  1.117  rillig     for (p = inbuf.inp.buf;;) {
    663  1.117  rillig 	if (p >= inbuf.inp.l) {
    664  1.117  rillig 	    size_t size = (size_t)(inbuf.inp.l - inbuf.inp.buf) * 2 + 10;
    665  1.117  rillig 	    size_t offset = (size_t)(p - inbuf.inp.buf);
    666  1.117  rillig 	    inbuf.inp.buf = xrealloc(inbuf.inp.buf, size);
    667  1.117  rillig 	    p = inbuf.inp.buf + offset;
    668  1.117  rillig 	    inbuf.inp.l = inbuf.inp.buf + size - 2;
    669   1.19   kamil 	}
    670   1.77  rillig 
    671   1.71  rillig 	if ((ch = getc(f)) == EOF) {
    672   1.95  rillig 	    if (!inhibit_formatting) {
    673   1.95  rillig 		*p++ = ' ';
    674   1.95  rillig 		*p++ = '\n';
    675   1.95  rillig 	    }
    676   1.37  rillig 	    had_eof = true;
    677   1.37  rillig 	    break;
    678    1.1     cgd 	}
    679   1.77  rillig 
    680   1.71  rillig 	if (ch != '\0')
    681   1.71  rillig 	    *p++ = (char)ch;
    682   1.71  rillig 	if (ch == '\n')
    683   1.37  rillig 	    break;
    684   1.19   kamil     }
    685   1.77  rillig 
    686  1.117  rillig     inbuf.inp.s = inbuf.inp.buf;
    687  1.117  rillig     inbuf.inp.e = p;
    688   1.77  rillig 
    689  1.117  rillig     if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/')
    690  1.114  rillig 	parse_indent_comment();
    691   1.77  rillig 
    692   1.96  rillig     if (inhibit_formatting)
    693  1.117  rillig 	output_range(inbuf.inp.s, inbuf.inp.e);
    694    1.1     cgd }
    695   1.19   kamil 
    696    1.1     cgd int
    697  1.109  rillig ind_add(int ind, const char *start, const char *end)
    698    1.1     cgd {
    699  1.109  rillig     for (const char *p = start; p != end; ++p) {
    700   1.40  rillig 	if (*p == '\n' || *p == '\f')
    701   1.40  rillig 	    ind = 0;
    702   1.40  rillig 	else if (*p == '\t')
    703   1.91  rillig 	    ind = next_tab(ind);
    704   1.40  rillig 	else if (*p == '\b')
    705   1.40  rillig 	    --ind;
    706   1.40  rillig 	else
    707   1.40  rillig 	    ++ind;
    708   1.40  rillig     }
    709   1.40  rillig     return ind;
    710   1.40  rillig }
    711