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