Home | History | Annotate | Line # | Download | only in indent
indent.c revision 1.156
      1  1.156  rillig /*	$NetBSD: indent.c,v 1.156 2021/10/25 00:54:37 rillig Exp $	*/
      2    1.4     tls 
      3   1.25   kamil /*-
      4   1.25   kamil  * SPDX-License-Identifier: BSD-4-Clause
      5   1.25   kamil  *
      6   1.25   kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7   1.25   kamil  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
      8    1.5     mrg  * Copyright (c) 1980, 1993
      9    1.5     mrg  *	The Regents of the University of California.  All rights reserved.
     10   1.15     agc  *
     11   1.15     agc  * Redistribution and use in source and binary forms, with or without
     12   1.15     agc  * modification, are permitted provided that the following conditions
     13   1.15     agc  * are met:
     14   1.15     agc  * 1. Redistributions of source code must retain the above copyright
     15   1.15     agc  *    notice, this list of conditions and the following disclaimer.
     16   1.15     agc  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.15     agc  *    notice, this list of conditions and the following disclaimer in the
     18   1.15     agc  *    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.25   kamil #if 0
     41   1.25   kamil static char sccsid[] = "@(#)indent.c	5.17 (Berkeley) 6/7/93";
     42   1.25   kamil #endif
     43    1.1     cgd 
     44   1.25   kamil #include <sys/cdefs.h>
     45   1.25   kamil #if defined(__NetBSD__)
     46  1.156  rillig __RCSID("$NetBSD: indent.c,v 1.156 2021/10/25 00:54:37 rillig Exp $");
     47   1.25   kamil #elif defined(__FreeBSD__)
     48   1.25   kamil __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
     49   1.25   kamil #endif
     50    1.1     cgd 
     51    1.1     cgd #include <sys/param.h>
     52   1.25   kamil #if HAVE_CAPSICUM
     53   1.25   kamil #include <sys/capsicum.h>
     54   1.25   kamil #include <capsicum_helpers.h>
     55   1.25   kamil #endif
     56   1.82  rillig #include <ctype.h>
     57    1.6   lukem #include <err.h>
     58    1.6   lukem #include <errno.h>
     59    1.1     cgd #include <fcntl.h>
     60    1.1     cgd #include <stdio.h>
     61    1.1     cgd #include <stdlib.h>
     62    1.1     cgd #include <string.h>
     63   1.82  rillig #include <unistd.h>
     64   1.29  rillig 
     65   1.25   kamil #include "indent.h"
     66   1.25   kamil 
     67   1.71  rillig struct options opt = {
     68  1.122  rillig     .brace_same_line = true,
     69   1.86  rillig     .comment_delimiter_on_blankline = true,
     70   1.86  rillig     .cuddle_else = true,
     71   1.86  rillig     .comment_column = 33,
     72   1.86  rillig     .decl_indent = 16,
     73   1.86  rillig     .else_if = true,
     74   1.86  rillig     .function_brace_split = true,
     75   1.86  rillig     .format_col1_comments = true,
     76   1.86  rillig     .format_block_comments = true,
     77   1.86  rillig     .indent_parameters = true,
     78   1.86  rillig     .indent_size = 8,
     79   1.86  rillig     .local_decl_indent = -1,
     80   1.86  rillig     .lineup_to_parens = true,
     81   1.86  rillig     .procnames_start_line = true,
     82   1.86  rillig     .star_comment_cont = true,
     83   1.86  rillig     .tabsize = 8,
     84   1.86  rillig     .max_line_length = 78,
     85   1.86  rillig     .use_tabs = true,
     86   1.71  rillig };
     87   1.71  rillig 
     88   1.27   joerg struct parser_state ps;
     89   1.27   joerg 
     90   1.63  rillig struct buffer lab;
     91   1.65  rillig struct buffer code;
     92   1.63  rillig struct buffer com;
     93   1.67  rillig struct buffer token;
     94   1.27   joerg 
     95  1.124  rillig struct buffer inp;
     96   1.86  rillig 
     97   1.86  rillig char sc_buf[sc_size];
     98   1.86  rillig char *save_com;
     99   1.88  rillig static char *sc_end;		/* pointer into save_com buffer */
    100   1.86  rillig 
    101  1.125  rillig char *saved_inp_s;
    102  1.125  rillig char *saved_inp_e;
    103   1.86  rillig 
    104   1.86  rillig bool found_err;
    105  1.140  rillig int blank_lines_to_output;
    106  1.142  rillig bool blank_line_before;
    107  1.142  rillig bool blank_line_after;
    108   1.86  rillig bool break_comma;
    109   1.86  rillig float case_ind;
    110   1.86  rillig bool had_eof;
    111   1.86  rillig int line_no;
    112   1.86  rillig bool inhibit_formatting;
    113   1.27   joerg 
    114   1.88  rillig static int ifdef_level;
    115   1.88  rillig static struct parser_state state_stack[5];
    116   1.27   joerg 
    117   1.86  rillig FILE *input;
    118   1.86  rillig FILE *output;
    119   1.27   joerg 
    120   1.88  rillig static const char *in_name = "Standard Input";
    121   1.88  rillig static const char *out_name = "Standard Output";
    122   1.88  rillig static const char *backup_suffix = ".BAK";
    123   1.88  rillig static char bakfile[MAXPATHLEN] = "";
    124    1.1     cgd 
    125   1.40  rillig #if HAVE_CAPSICUM
    126   1.40  rillig static void
    127   1.40  rillig init_capsicum(void)
    128    1.1     cgd {
    129   1.25   kamil     cap_rights_t rights;
    130   1.40  rillig 
    131   1.40  rillig     /* Restrict input/output descriptors and enter Capsicum sandbox. */
    132   1.40  rillig     cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
    133   1.40  rillig     if (caph_rights_limit(fileno(output), &rights) < 0)
    134   1.40  rillig 	err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
    135   1.40  rillig     cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
    136   1.40  rillig     if (caph_rights_limit(fileno(input), &rights) < 0)
    137   1.40  rillig 	err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
    138   1.40  rillig     if (caph_enter() < 0)
    139   1.40  rillig 	err(EXIT_FAILURE, "unable to enter capability mode");
    140   1.40  rillig }
    141   1.41  rillig #endif
    142   1.41  rillig 
    143  1.148  rillig void
    144  1.148  rillig diag(int level, const char *msg, ...)
    145  1.148  rillig {
    146  1.148  rillig     va_list ap;
    147  1.148  rillig 
    148  1.148  rillig     if (level != 0)
    149  1.148  rillig 	found_err = true;
    150  1.148  rillig 
    151  1.148  rillig     va_start(ap, msg);
    152  1.148  rillig     fprintf(stderr, "%s: %s:%d: ",
    153  1.148  rillig 	level == 0 ? "warning" : "error", in_name, line_no);
    154  1.148  rillig     vfprintf(stderr, msg, ap);
    155  1.148  rillig     fprintf(stderr, "\n");
    156  1.148  rillig     va_end(ap);
    157  1.148  rillig }
    158  1.148  rillig 
    159   1.41  rillig static void
    160  1.104  rillig search_brace_newline(bool *force_nl)
    161   1.91  rillig {
    162   1.91  rillig     if (sc_end == NULL) {
    163   1.91  rillig 	save_com = sc_buf;
    164   1.91  rillig 	save_com[0] = save_com[1] = ' ';
    165   1.91  rillig 	sc_end = &save_com[2];
    166   1.91  rillig     }
    167   1.91  rillig     *sc_end++ = '\n';
    168   1.91  rillig 
    169  1.147  rillig     line_no++;
    170  1.147  rillig 
    171   1.91  rillig     /*
    172   1.92  rillig      * We may have inherited a force_nl == true from the previous token (like
    173   1.92  rillig      * a semicolon). But once we know that a newline has been scanned in this
    174   1.92  rillig      * loop, force_nl should be false.
    175   1.91  rillig      *
    176   1.92  rillig      * However, the force_nl == true must be preserved if newline is never
    177   1.92  rillig      * scanned in this loop, so this assignment cannot be done earlier.
    178   1.91  rillig      */
    179  1.104  rillig     *force_nl = false;
    180   1.91  rillig }
    181   1.91  rillig 
    182   1.91  rillig static void
    183  1.104  rillig search_brace_comment(bool *comment_buffered)
    184   1.91  rillig {
    185   1.91  rillig     if (sc_end == NULL) {
    186   1.91  rillig 	/*
    187   1.91  rillig 	 * Copy everything from the start of the line, because
    188   1.92  rillig 	 * process_comment() will use that to calculate original indentation
    189   1.92  rillig 	 * of a boxed comment.
    190   1.91  rillig 	 */
    191  1.124  rillig 	memcpy(sc_buf, inp.buf, (size_t)(inp.s - inp.buf) - 4);
    192  1.124  rillig 	save_com = sc_buf + (inp.s - inp.buf - 4);
    193   1.91  rillig 	save_com[0] = save_com[1] = ' ';
    194   1.91  rillig 	sc_end = &save_com[2];
    195   1.91  rillig     }
    196  1.119  rillig 
    197  1.104  rillig     *comment_buffered = true;
    198   1.92  rillig     *sc_end++ = '/';		/* copy in start of comment */
    199   1.91  rillig     *sc_end++ = '*';
    200  1.119  rillig 
    201   1.92  rillig     for (;;) {			/* loop until the end of the comment */
    202  1.100  rillig 	*sc_end++ = inbuf_next();
    203  1.124  rillig 	if (sc_end[-1] == '*' && *inp.s == '/')
    204   1.92  rillig 	    break;		/* we are at end of comment */
    205   1.91  rillig 	if (sc_end >= &save_com[sc_size]) {	/* check for temp buffer
    206   1.91  rillig 						 * overflow */
    207   1.91  rillig 	    diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
    208   1.91  rillig 	    fflush(output);
    209   1.91  rillig 	    exit(1);
    210   1.91  rillig 	}
    211   1.91  rillig     }
    212  1.119  rillig 
    213   1.92  rillig     *sc_end++ = '/';		/* add ending slash */
    214   1.99  rillig     inbuf_skip();		/* get past / in buffer */
    215   1.91  rillig }
    216   1.91  rillig 
    217   1.91  rillig static bool
    218   1.91  rillig search_brace_lbrace(void)
    219   1.91  rillig {
    220   1.91  rillig     /*
    221   1.92  rillig      * Put KNF-style lbraces before the buffered up tokens and jump out of
    222  1.117  rillig      * this loop in order to avoid copying the token again.
    223   1.91  rillig      */
    224  1.122  rillig     if (sc_end != NULL && opt.brace_same_line) {
    225   1.91  rillig 	save_com[0] = '{';
    226   1.91  rillig 	/*
    227   1.92  rillig 	 * Originally the lbrace may have been alone on its own line, but it
    228   1.92  rillig 	 * will be moved into "the else's line", so if there was a newline
    229   1.92  rillig 	 * resulting from the "{" before, it must be scanned now and ignored.
    230   1.91  rillig 	 */
    231  1.124  rillig 	while (isspace((unsigned char)*inp.s)) {
    232   1.99  rillig 	    inbuf_skip();
    233  1.124  rillig 	    if (*inp.s == '\n')
    234   1.91  rillig 		break;
    235   1.91  rillig 	}
    236   1.91  rillig 	return true;
    237   1.91  rillig     }
    238   1.91  rillig     return false;
    239   1.91  rillig }
    240   1.91  rillig 
    241   1.91  rillig static bool
    242  1.156  rillig search_brace_other(lexer_symbol lsym, bool *force_nl,
    243   1.91  rillig     bool comment_buffered, bool last_else)
    244   1.91  rillig {
    245   1.91  rillig     bool remove_newlines;
    246   1.91  rillig 
    247   1.91  rillig     remove_newlines =
    248   1.91  rillig 	    /* "} else" */
    249  1.156  rillig 	    (lsym == lsym_else && code.e != code.s && code.e[-1] == '}')
    250   1.91  rillig 	    /* "else if" */
    251  1.156  rillig 	    || (lsym == lsym_if && last_else && opt.else_if);
    252   1.91  rillig     if (remove_newlines)
    253  1.104  rillig 	*force_nl = false;
    254  1.119  rillig 
    255   1.91  rillig     if (sc_end == NULL) {	/* ignore buffering if comment wasn't saved
    256   1.91  rillig 				 * up */
    257   1.91  rillig 	ps.search_brace = false;
    258   1.91  rillig 	return false;
    259   1.91  rillig     }
    260  1.119  rillig 
    261  1.120  rillig     while (sc_end > save_com && isblank((unsigned char)sc_end[-1]))
    262   1.91  rillig 	sc_end--;
    263  1.119  rillig 
    264   1.91  rillig     if (opt.swallow_optional_blanklines ||
    265   1.91  rillig 	(!comment_buffered && remove_newlines)) {
    266  1.104  rillig 	*force_nl = !remove_newlines;
    267   1.91  rillig 	while (sc_end > save_com && sc_end[-1] == '\n') {
    268   1.91  rillig 	    sc_end--;
    269   1.91  rillig 	}
    270   1.91  rillig     }
    271  1.119  rillig 
    272  1.117  rillig     if (*force_nl) {		/* if we should insert a nl here, put it into
    273   1.91  rillig 				 * the buffer */
    274  1.104  rillig 	*force_nl = false;
    275   1.91  rillig 	--line_no;		/* this will be re-increased when the newline
    276   1.91  rillig 				 * is read from the buffer */
    277   1.91  rillig 	*sc_end++ = '\n';
    278   1.91  rillig 	*sc_end++ = ' ';
    279  1.117  rillig 	if (opt.verbose)	/* warn if the line was not already broken */
    280   1.91  rillig 	    diag(0, "Line broken");
    281   1.91  rillig     }
    282  1.119  rillig 
    283  1.135  rillig     /* XXX: buffer overflow? This is essentially a strcpy. */
    284   1.91  rillig     for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
    285   1.91  rillig 	*sc_end++ = *t_ptr;
    286   1.91  rillig     return true;
    287   1.91  rillig }
    288   1.91  rillig 
    289   1.91  rillig static void
    290   1.91  rillig switch_buffer(void)
    291   1.91  rillig {
    292   1.91  rillig     ps.search_brace = false;	/* stop looking for start of stmt */
    293  1.139  rillig     saved_inp_s = inp.s;	/* save current input buffer */
    294  1.125  rillig     saved_inp_e = inp.e;
    295  1.124  rillig     inp.s = save_com;		/* fix so that subsequent calls to lexi will
    296   1.91  rillig 				 * take tokens out of save_com */
    297   1.91  rillig     *sc_end++ = ' ';		/* add trailing blank, just in case */
    298  1.124  rillig     inp.e = sc_end;
    299   1.91  rillig     sc_end = NULL;
    300  1.124  rillig     debug_println("switched inp.s to save_com");
    301   1.91  rillig }
    302   1.91  rillig 
    303   1.91  rillig static void
    304  1.156  rillig search_brace_lookahead(lexer_symbol *lsym)
    305   1.91  rillig {
    306  1.156  rillig     if (*lsym == lsym_eof)
    307  1.115  rillig 	return;
    308  1.115  rillig 
    309   1.91  rillig     /*
    310  1.149  rillig      * The only intended purpose of calling lexi() below is to categorize the
    311  1.149  rillig      * next token in order to decide whether to continue buffering forthcoming
    312  1.149  rillig      * tokens. Once the buffering is over, lexi() will be called again
    313  1.149  rillig      * elsewhere on all of the tokens - this time for normal processing.
    314  1.115  rillig      *
    315  1.149  rillig      * Calling it for this purpose is a bug, because lexi() also changes the
    316  1.149  rillig      * parser state and discards leading whitespace, which is needed mostly
    317  1.149  rillig      * for comment-related considerations.
    318  1.115  rillig      *
    319  1.149  rillig      * Work around the former problem by giving lexi() a copy of the current
    320  1.149  rillig      * parser state and discard it if the call turned out to be just a
    321  1.149  rillig      * lookahead.
    322  1.115  rillig      *
    323  1.115  rillig      * Work around the latter problem by copying all whitespace characters
    324  1.115  rillig      * into the buffer so that the later lexi() call will read them.
    325   1.91  rillig      */
    326  1.115  rillig     if (sc_end != NULL) {
    327  1.124  rillig 	while (is_hspace(*inp.s)) {
    328  1.124  rillig 	    *sc_end++ = *inp.s++;
    329  1.115  rillig 	    if (sc_end >= &save_com[sc_size])
    330  1.115  rillig 		errx(1, "input too long");
    331   1.91  rillig 	}
    332  1.124  rillig 	if (inp.s >= inp.e)
    333  1.133  rillig 	    inbuf_read_line();
    334  1.115  rillig     }
    335   1.91  rillig 
    336  1.115  rillig     struct parser_state transient_state;
    337  1.115  rillig     transient_state = ps;
    338  1.156  rillig     *lsym = lexi(&transient_state);	/* read another token */
    339  1.156  rillig     if (*lsym != lsym_newline && *lsym != lsym_form_feed &&
    340  1.156  rillig 	*lsym != lsym_comment && !transient_state.search_brace) {
    341  1.115  rillig 	ps = transient_state;
    342   1.91  rillig     }
    343   1.91  rillig }
    344   1.91  rillig 
    345   1.91  rillig static void
    346  1.156  rillig search_brace(lexer_symbol *lsym, bool *force_nl,
    347  1.104  rillig     bool *comment_buffered, bool *last_else)
    348   1.41  rillig {
    349   1.41  rillig     while (ps.search_brace) {
    350  1.156  rillig 	switch (*lsym) {
    351  1.156  rillig 	case lsym_newline:
    352  1.104  rillig 	    search_brace_newline(force_nl);
    353   1.59  rillig 	    break;
    354  1.156  rillig 	case lsym_form_feed:
    355   1.41  rillig 	    break;
    356  1.156  rillig 	case lsym_comment:
    357  1.104  rillig 	    search_brace_comment(comment_buffered);
    358   1.41  rillig 	    break;
    359  1.156  rillig 	case lsym_lbrace:
    360   1.91  rillig 	    if (search_brace_lbrace())
    361  1.115  rillig 		goto switch_buffer;
    362   1.41  rillig 	    /* FALLTHROUGH */
    363   1.41  rillig 	default:		/* it is the start of a normal statement */
    364  1.156  rillig 	    if (!search_brace_other(*lsym, force_nl,
    365  1.104  rillig 		    *comment_buffered, *last_else))
    366   1.41  rillig 		return;
    367  1.149  rillig     switch_buffer:
    368   1.91  rillig 	    switch_buffer();
    369   1.41  rillig 	}
    370  1.156  rillig 	search_brace_lookahead(lsym);
    371   1.41  rillig     }
    372   1.40  rillig 
    373  1.104  rillig     *last_else = false;
    374   1.41  rillig }
    375    1.1     cgd 
    376   1.53  rillig static void
    377   1.72  rillig buf_init(struct buffer *buf)
    378   1.72  rillig {
    379  1.123  rillig     size_t size = 200;
    380  1.123  rillig     buf->buf = xmalloc(size);
    381   1.86  rillig     buf->buf[0] = ' ';		/* allow accessing buf->e[-1] */
    382   1.72  rillig     buf->buf[1] = '\0';
    383   1.72  rillig     buf->s = buf->buf + 1;
    384   1.72  rillig     buf->e = buf->s;
    385  1.123  rillig     buf->l = buf->buf + size - 5;	/* safety margin */
    386   1.72  rillig }
    387   1.72  rillig 
    388  1.110  rillig static size_t
    389  1.110  rillig buf_len(const struct buffer *buf)
    390  1.110  rillig {
    391  1.110  rillig     return (size_t)(buf->e - buf->s);
    392  1.110  rillig }
    393  1.110  rillig 
    394   1.81  rillig void
    395   1.81  rillig buf_expand(struct buffer *buf, size_t desired_size)
    396   1.81  rillig {
    397  1.110  rillig     size_t nsize = (size_t)(buf->l - buf->s) + 400 + desired_size;
    398  1.110  rillig     size_t len = buf_len(buf);
    399   1.81  rillig     buf->buf = xrealloc(buf->buf, nsize);
    400  1.110  rillig     buf->e = buf->buf + len + 1;
    401   1.81  rillig     buf->l = buf->buf + nsize - 5;
    402   1.81  rillig     buf->s = buf->buf + 1;
    403   1.81  rillig }
    404   1.81  rillig 
    405   1.72  rillig static void
    406  1.112  rillig buf_reserve(struct buffer *buf, size_t n)
    407  1.112  rillig {
    408  1.112  rillig     if (buf->e + n >= buf->l)
    409  1.112  rillig 	buf_expand(buf, n);
    410  1.112  rillig }
    411  1.112  rillig 
    412  1.112  rillig static void
    413  1.112  rillig buf_add_char(struct buffer *buf, char ch)
    414  1.112  rillig {
    415  1.112  rillig     buf_reserve(buf, 1);
    416  1.112  rillig     *buf->e++ = ch;
    417  1.112  rillig }
    418  1.112  rillig 
    419  1.112  rillig static void
    420  1.112  rillig buf_add_buf(struct buffer *buf, const struct buffer *add)
    421  1.112  rillig {
    422  1.112  rillig     size_t len = buf_len(add);
    423  1.112  rillig     buf_reserve(buf, len);
    424  1.112  rillig     memcpy(buf->e, add->s, len);
    425  1.112  rillig     buf->e += len;
    426  1.112  rillig }
    427  1.112  rillig 
    428  1.112  rillig static void
    429  1.112  rillig buf_terminate(struct buffer *buf)
    430  1.112  rillig {
    431  1.112  rillig     buf_reserve(buf, 1);
    432  1.112  rillig     *buf->e = '\0';
    433  1.112  rillig }
    434  1.112  rillig 
    435  1.112  rillig static void
    436  1.112  rillig buf_reset(struct buffer *buf)
    437  1.112  rillig {
    438  1.112  rillig     buf->e = buf->s;
    439  1.112  rillig }
    440  1.112  rillig 
    441  1.112  rillig static void
    442   1.53  rillig main_init_globals(void)
    443   1.40  rillig {
    444   1.83  rillig     found_err = false;
    445    1.1     cgd 
    446  1.156  rillig     ps.s_sym[0] = psym_stmt;
    447  1.116  rillig     ps.last_nl = true;
    448  1.156  rillig     ps.last_token = lsym_semicolon;
    449   1.72  rillig     buf_init(&com);
    450   1.72  rillig     buf_init(&lab);
    451   1.72  rillig     buf_init(&code);
    452   1.72  rillig     buf_init(&token);
    453  1.119  rillig 
    454   1.73  rillig     opt.else_if = true;		/* XXX: redundant? */
    455   1.25   kamil 
    456  1.124  rillig     inp.buf = xmalloc(10);
    457  1.124  rillig     inp.l = inp.buf + 8;
    458  1.124  rillig     inp.s = inp.e = inp.buf;
    459   1.25   kamil     line_no = 1;
    460  1.121  rillig     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
    461  1.119  rillig 
    462  1.134  rillig     ps.init_or_struct = false;
    463   1.25   kamil     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
    464  1.119  rillig     ps.is_case_label = false;
    465   1.25   kamil 
    466   1.25   kamil     sc_end = NULL;
    467  1.125  rillig     saved_inp_s = NULL;
    468  1.125  rillig     saved_inp_e = NULL;
    469   1.25   kamil 
    470   1.25   kamil     output = NULL;
    471   1.25   kamil 
    472   1.53  rillig     const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
    473   1.53  rillig     if (suffix != NULL)
    474   1.88  rillig 	backup_suffix = suffix;
    475   1.53  rillig }
    476   1.53  rillig 
    477  1.131  rillig /*
    478  1.131  rillig  * Copy the input file to the backup file, then make the backup file the input
    479  1.131  rillig  * and the original input file the output.
    480  1.131  rillig  */
    481  1.131  rillig static void
    482  1.131  rillig bakcopy(void)
    483  1.131  rillig {
    484  1.131  rillig     ssize_t n;
    485  1.131  rillig     int bak_fd;
    486  1.131  rillig     char buff[8 * 1024];
    487  1.131  rillig 
    488  1.131  rillig     const char *last_slash = strrchr(in_name, '/');
    489  1.131  rillig     snprintf(bakfile, sizeof(bakfile), "%s%s",
    490  1.149  rillig 	last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
    491  1.131  rillig 
    492  1.131  rillig     /* copy in_name to backup file */
    493  1.131  rillig     bak_fd = creat(bakfile, 0600);
    494  1.131  rillig     if (bak_fd < 0)
    495  1.131  rillig 	err(1, "%s", bakfile);
    496  1.131  rillig 
    497  1.131  rillig     while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
    498  1.131  rillig 	if (write(bak_fd, buff, (size_t)n) != n)
    499  1.131  rillig 	    err(1, "%s", bakfile);
    500  1.131  rillig     if (n < 0)
    501  1.131  rillig 	err(1, "%s", in_name);
    502  1.131  rillig 
    503  1.131  rillig     close(bak_fd);
    504  1.131  rillig     (void)fclose(input);
    505  1.131  rillig 
    506  1.131  rillig     /* re-open backup file as the input file */
    507  1.131  rillig     input = fopen(bakfile, "r");
    508  1.131  rillig     if (input == NULL)
    509  1.131  rillig 	err(1, "%s", bakfile);
    510  1.131  rillig     /* now the original input file will be the output */
    511  1.131  rillig     output = fopen(in_name, "w");
    512  1.131  rillig     if (output == NULL) {
    513  1.131  rillig 	unlink(bakfile);
    514  1.131  rillig 	err(1, "%s", in_name);
    515  1.131  rillig     }
    516  1.131  rillig }
    517  1.131  rillig 
    518   1.53  rillig static void
    519   1.53  rillig main_parse_command_line(int argc, char **argv)
    520   1.53  rillig {
    521   1.53  rillig     int i;
    522   1.53  rillig     const char *profile_name = NULL;
    523    1.1     cgd 
    524   1.25   kamil     for (i = 1; i < argc; ++i)
    525   1.25   kamil 	if (strcmp(argv[i], "-npro") == 0)
    526   1.25   kamil 	    break;
    527   1.25   kamil 	else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
    528   1.97  rillig 	    profile_name = argv[i] + 2;	/* non-empty -P (set profile) */
    529   1.25   kamil     if (i >= argc)
    530   1.98  rillig 	load_profiles(profile_name);
    531    1.1     cgd 
    532   1.25   kamil     for (i = 1; i < argc; ++i) {
    533   1.80  rillig 	if (argv[i][0] == '-') {
    534  1.114  rillig 	    set_option(argv[i], "Command line");
    535   1.80  rillig 
    536   1.80  rillig 	} else if (input == NULL) {
    537   1.80  rillig 	    in_name = argv[i];
    538   1.80  rillig 	    input = fopen(in_name, "r");
    539   1.80  rillig 	    if (input == NULL)
    540   1.80  rillig 		err(1, "%s", in_name);
    541   1.80  rillig 
    542   1.80  rillig 	} else if (output == NULL) {
    543   1.80  rillig 	    out_name = argv[i];
    544   1.80  rillig 	    if (strcmp(in_name, out_name) == 0)
    545   1.80  rillig 		errx(1, "input and output files must be different");
    546   1.80  rillig 	    output = fopen(out_name, "w");
    547   1.80  rillig 	    if (output == NULL)
    548   1.80  rillig 		err(1, "%s", out_name);
    549    1.1     cgd 
    550   1.80  rillig 	} else
    551   1.25   kamil 	    errx(1, "unknown parameter: %s", argv[i]);
    552   1.80  rillig     }
    553   1.80  rillig 
    554   1.25   kamil     if (input == NULL)
    555   1.25   kamil 	input = stdin;
    556   1.25   kamil     if (output == NULL) {
    557   1.25   kamil 	if (input == stdin)
    558   1.25   kamil 	    output = stdout;
    559   1.25   kamil 	else {
    560   1.25   kamil 	    out_name = in_name;
    561   1.25   kamil 	    bakcopy();
    562    1.1     cgd 	}
    563   1.25   kamil     }
    564   1.25   kamil 
    565   1.57  rillig     if (opt.comment_column <= 1)
    566   1.57  rillig 	opt.comment_column = 2;	/* don't put normal comments before column 2 */
    567   1.51  rillig     if (opt.block_comment_max_line_length <= 0)
    568   1.51  rillig 	opt.block_comment_max_line_length = opt.max_line_length;
    569  1.100  rillig     if (opt.local_decl_indent < 0)	/* if not specified by user, set this */
    570   1.25   kamil 	opt.local_decl_indent = opt.decl_indent;
    571   1.57  rillig     if (opt.decl_comment_column <= 0)	/* if not specified by user, set this */
    572   1.57  rillig 	opt.decl_comment_column = opt.ljust_decl
    573   1.57  rillig 	    ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
    574   1.57  rillig 	    : opt.comment_column;
    575   1.25   kamil     if (opt.continuation_indent == 0)
    576   1.57  rillig 	opt.continuation_indent = opt.indent_size;
    577   1.53  rillig }
    578   1.53  rillig 
    579   1.53  rillig static void
    580   1.53  rillig main_prepare_parsing(void)
    581   1.53  rillig {
    582  1.133  rillig     inbuf_read_line();
    583   1.25   kamil 
    584  1.156  rillig     parse(psym_semicolon);
    585   1.53  rillig 
    586  1.106  rillig     int ind = 0;
    587  1.137  rillig     for (const char *p = inp.s;; p++) {
    588   1.53  rillig 	if (*p == ' ')
    589  1.106  rillig 	    ind++;
    590   1.53  rillig 	else if (*p == '\t')
    591  1.136  rillig 	    ind = next_tab(ind);
    592   1.53  rillig 	else
    593   1.53  rillig 	    break;
    594   1.25   kamil     }
    595  1.137  rillig 
    596  1.106  rillig     if (ind >= opt.indent_size)
    597  1.107  rillig 	ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
    598   1.53  rillig }
    599   1.25   kamil 
    600  1.131  rillig static void
    601  1.131  rillig indent_declaration(int cur_decl_ind, bool tabs_to_var)
    602  1.131  rillig {
    603  1.137  rillig     int ind = (int)buf_len(&code);
    604  1.131  rillig     char *orig_code_e = code.e;
    605  1.131  rillig 
    606  1.131  rillig     /*
    607  1.131  rillig      * get the tab math right for indentations that are not multiples of
    608  1.131  rillig      * tabsize
    609  1.131  rillig      */
    610  1.131  rillig     if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
    611  1.137  rillig 	ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
    612  1.131  rillig 	cur_decl_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
    613  1.131  rillig     }
    614  1.131  rillig 
    615  1.131  rillig     if (tabs_to_var) {
    616  1.137  rillig 	for (int next; (next = next_tab(ind)) <= cur_decl_ind; ind = next)
    617  1.131  rillig 	    buf_add_char(&code, '\t');
    618  1.131  rillig     }
    619  1.131  rillig 
    620  1.137  rillig     for (; ind < cur_decl_ind; ind++)
    621  1.131  rillig 	buf_add_char(&code, ' ');
    622  1.131  rillig 
    623  1.131  rillig     if (code.e == orig_code_e && ps.want_blank) {
    624  1.131  rillig 	*code.e++ = ' ';
    625  1.131  rillig 	ps.want_blank = false;
    626  1.131  rillig     }
    627  1.131  rillig }
    628  1.131  rillig 
    629   1.60  rillig static void __attribute__((__noreturn__))
    630   1.54  rillig process_end_of_file(void)
    631   1.54  rillig {
    632   1.65  rillig     if (lab.s != lab.e || code.s != code.e || com.s != com.e)
    633   1.54  rillig 	dump_line();
    634   1.54  rillig 
    635   1.54  rillig     if (ps.tos > 1)		/* check for balanced braces */
    636   1.54  rillig 	diag(1, "Stuff missing from end of file");
    637   1.54  rillig 
    638   1.54  rillig     if (opt.verbose) {
    639   1.54  rillig 	printf("There were %d output lines and %d comments\n",
    640   1.86  rillig 	    ps.stats.lines, ps.stats.comments);
    641   1.54  rillig 	printf("(Lines with comments)/(Lines with code): %6.3f\n",
    642   1.86  rillig 	    (1.0 * ps.stats.comment_lines) / ps.stats.code_lines);
    643   1.54  rillig     }
    644   1.54  rillig 
    645   1.54  rillig     fflush(output);
    646   1.83  rillig     exit(found_err ? EXIT_FAILURE : EXIT_SUCCESS);
    647   1.54  rillig }
    648   1.54  rillig 
    649   1.54  rillig static void
    650  1.156  rillig process_comment_in_code(lexer_symbol lsym, bool *force_nl)
    651   1.54  rillig {
    652  1.104  rillig     if (*force_nl &&
    653  1.156  rillig 	lsym != lsym_semicolon &&
    654  1.156  rillig 	(lsym != lsym_lbrace || !opt.brace_same_line)) {
    655   1.54  rillig 
    656   1.54  rillig 	/* we should force a broken line here */
    657   1.54  rillig 	if (opt.verbose)
    658   1.54  rillig 	    diag(0, "Line broken");
    659   1.54  rillig 	dump_line();
    660  1.102  rillig 	ps.want_blank = false;	/* don't insert blank at line start */
    661  1.104  rillig 	*force_nl = false;
    662   1.54  rillig     }
    663   1.54  rillig 
    664  1.118  rillig     /* add an extra level of indentation; turned off again by a ';' or '}' */
    665  1.118  rillig     ps.in_stmt = true;
    666  1.118  rillig 
    667  1.118  rillig     if (com.s != com.e) {	/* a comment embedded in a line */
    668  1.112  rillig 	buf_add_char(&code, ' ');
    669  1.112  rillig 	buf_add_buf(&code, &com);
    670  1.112  rillig 	buf_add_char(&code, ' ');
    671  1.112  rillig 	buf_terminate(&code);
    672  1.112  rillig 	buf_reset(&com);
    673   1.54  rillig 	ps.want_blank = false;
    674   1.54  rillig     }
    675   1.54  rillig }
    676   1.54  rillig 
    677   1.54  rillig static void
    678   1.54  rillig process_form_feed(void)
    679   1.54  rillig {
    680  1.145  rillig     dump_line_ff();
    681   1.54  rillig     ps.want_blank = false;
    682   1.54  rillig }
    683   1.54  rillig 
    684   1.54  rillig static void
    685   1.54  rillig process_newline(void)
    686   1.54  rillig {
    687  1.156  rillig     if (ps.last_token == lsym_comma && ps.p_l_follow == 0 && !ps.block_init &&
    688  1.141  rillig 	!opt.break_after_comma && break_comma &&
    689  1.141  rillig 	com.s == com.e)
    690  1.141  rillig 	goto stay_in_line;
    691  1.141  rillig 
    692  1.141  rillig     dump_line();
    693  1.141  rillig     ps.want_blank = false;
    694  1.141  rillig 
    695  1.141  rillig stay_in_line:
    696  1.116  rillig     ++line_no;
    697   1.54  rillig }
    698   1.54  rillig 
    699   1.94  rillig static bool
    700   1.94  rillig want_blank_before_lparen(void)
    701   1.94  rillig {
    702   1.95  rillig     if (!ps.want_blank)
    703   1.95  rillig 	return false;
    704  1.156  rillig     if (ps.last_token == lsym_rparen_or_rbracket)
    705   1.96  rillig 	return false;
    706  1.156  rillig     if (ps.last_token != lsym_ident && ps.last_token != lsym_funcname)
    707   1.95  rillig 	return true;
    708   1.95  rillig     if (opt.proc_calls_space)
    709   1.95  rillig 	return true;
    710   1.95  rillig     if (ps.keyword == kw_sizeof)
    711   1.95  rillig 	return opt.blank_after_sizeof;
    712   1.95  rillig     return ps.keyword != kw_0 && ps.keyword != kw_offsetof;
    713   1.94  rillig }
    714   1.94  rillig 
    715   1.54  rillig static void
    716  1.105  rillig process_lparen_or_lbracket(int decl_ind, bool tabs_to_var, bool sp_sw)
    717   1.54  rillig {
    718  1.146  rillig     if (++ps.p_l_follow == array_length(ps.paren_indents)) {
    719   1.54  rillig 	diag(0, "Reached internal limit of %zu unclosed parens",
    720  1.146  rillig 	    array_length(ps.paren_indents));
    721   1.54  rillig 	ps.p_l_follow--;
    722   1.54  rillig     }
    723  1.119  rillig 
    724   1.93  rillig     if (token.s[0] == '(' && ps.in_decl
    725   1.93  rillig 	&& !ps.block_init && !ps.dumped_decl_indent &&
    726   1.54  rillig 	ps.procname[0] == '\0' && ps.paren_level == 0) {
    727   1.54  rillig 	/* function pointer declarations */
    728  1.105  rillig 	indent_declaration(decl_ind, tabs_to_var);
    729   1.54  rillig 	ps.dumped_decl_indent = true;
    730   1.94  rillig     } else if (want_blank_before_lparen())
    731   1.65  rillig 	*code.e++ = ' ';
    732   1.54  rillig     ps.want_blank = false;
    733   1.67  rillig     *code.e++ = token.s[0];
    734   1.58  rillig 
    735   1.54  rillig     ps.paren_indents[ps.p_l_follow - 1] =
    736  1.103  rillig 	(short)indentation_after_range(0, code.s, code.e);
    737  1.135  rillig     debug_println("paren_indents[%d] is now %d",
    738   1.58  rillig 	ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
    739   1.58  rillig 
    740  1.128  rillig     if (sp_sw && ps.p_l_follow == 1 && opt.extra_expr_indent
    741   1.58  rillig 	    && ps.paren_indents[0] < 2 * opt.indent_size) {
    742  1.103  rillig 	ps.paren_indents[0] = (short)(2 * opt.indent_size);
    743  1.135  rillig 	debug_println("paren_indents[0] is now %d", ps.paren_indents[0]);
    744   1.58  rillig     }
    745  1.119  rillig 
    746  1.134  rillig     if (ps.init_or_struct && *token.s == '(' && ps.tos <= 2) {
    747   1.54  rillig 	/*
    748   1.86  rillig 	 * this is a kluge to make sure that declarations will be aligned
    749   1.86  rillig 	 * right if proc decl has an explicit type on it, i.e. "int a(x) {..."
    750   1.54  rillig 	 */
    751  1.156  rillig 	parse(psym_semicolon);	/* I said this was a kluge... */
    752  1.134  rillig 	ps.init_or_struct = false;
    753   1.54  rillig     }
    754  1.119  rillig 
    755   1.54  rillig     /* parenthesized type following sizeof or offsetof is not a cast */
    756   1.89  rillig     if (ps.keyword == kw_offsetof || ps.keyword == kw_sizeof)
    757   1.54  rillig 	ps.not_cast_mask |= 1 << ps.p_l_follow;
    758   1.54  rillig }
    759   1.54  rillig 
    760   1.54  rillig static void
    761  1.156  rillig process_rparen_or_rbracket(bool *sp_sw, bool *force_nl, stmt_head hd)
    762   1.54  rillig {
    763   1.73  rillig     if ((ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) != 0) {
    764  1.144  rillig 	ps.next_unary = true;
    765   1.54  rillig 	ps.cast_mask &= (1 << ps.p_l_follow) - 1;
    766   1.54  rillig 	ps.want_blank = opt.space_after_cast;
    767   1.54  rillig     } else
    768   1.54  rillig 	ps.want_blank = true;
    769   1.54  rillig     ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
    770   1.54  rillig 
    771   1.54  rillig     if (--ps.p_l_follow < 0) {
    772   1.54  rillig 	ps.p_l_follow = 0;
    773   1.67  rillig 	diag(0, "Extra %c", *token.s);
    774   1.54  rillig     }
    775   1.54  rillig 
    776   1.65  rillig     if (code.e == code.s)	/* if the paren starts the line */
    777   1.54  rillig 	ps.paren_level = ps.p_l_follow;	/* then indent it */
    778   1.54  rillig 
    779   1.67  rillig     *code.e++ = token.s[0];
    780   1.54  rillig 
    781  1.135  rillig     if (*sp_sw && ps.p_l_follow == 0) {	/* check for end of if (...), or some
    782  1.135  rillig 					 * such */
    783  1.104  rillig 	*sp_sw = false;
    784  1.104  rillig 	*force_nl = true;	/* must force newline after if */
    785  1.144  rillig 	ps.next_unary = true;
    786  1.102  rillig 	ps.in_stmt = false;	/* don't use stmt continuation indentation */
    787   1.54  rillig 
    788  1.156  rillig 	parse_hd(hd);		/* let parser worry about if, or whatever */
    789   1.54  rillig     }
    790  1.122  rillig 
    791  1.122  rillig     /*
    792  1.122  rillig      * This should ensure that constructs such as main(){...} and int[]{...}
    793  1.122  rillig      * have their braces put in the right place.
    794  1.122  rillig      */
    795  1.122  rillig     ps.search_brace = opt.brace_same_line;
    796   1.54  rillig }
    797   1.54  rillig 
    798   1.54  rillig static void
    799  1.105  rillig process_unary_op(int decl_ind, bool tabs_to_var)
    800   1.54  rillig {
    801   1.54  rillig     if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
    802   1.54  rillig 	ps.procname[0] == '\0' && ps.paren_level == 0) {
    803   1.54  rillig 	/* pointer declarations */
    804  1.132  rillig 	indent_declaration(decl_ind - (int)buf_len(&token), tabs_to_var);
    805   1.54  rillig 	ps.dumped_decl_indent = true;
    806   1.54  rillig     } else if (ps.want_blank)
    807   1.65  rillig 	*code.e++ = ' ';
    808   1.54  rillig 
    809  1.112  rillig     buf_add_buf(&code, &token);
    810   1.54  rillig     ps.want_blank = false;
    811   1.54  rillig }
    812   1.54  rillig 
    813   1.54  rillig static void
    814   1.54  rillig process_binary_op(void)
    815   1.54  rillig {
    816   1.54  rillig     if (ps.want_blank)
    817  1.112  rillig 	buf_add_char(&code, ' ');
    818  1.112  rillig     buf_add_buf(&code, &token);
    819   1.54  rillig     ps.want_blank = true;
    820   1.54  rillig }
    821   1.54  rillig 
    822   1.54  rillig static void
    823   1.54  rillig process_postfix_op(void)
    824   1.54  rillig {
    825   1.67  rillig     *code.e++ = token.s[0];
    826   1.67  rillig     *code.e++ = token.s[1];
    827   1.54  rillig     ps.want_blank = true;
    828   1.54  rillig }
    829   1.54  rillig 
    830   1.54  rillig static void
    831  1.152  rillig process_question(int *quest_level)
    832   1.54  rillig {
    833  1.152  rillig     (*quest_level)++;
    834   1.54  rillig     if (ps.want_blank)
    835   1.65  rillig 	*code.e++ = ' ';
    836   1.65  rillig     *code.e++ = '?';
    837   1.54  rillig     ps.want_blank = true;
    838   1.54  rillig }
    839   1.54  rillig 
    840   1.54  rillig static void
    841  1.152  rillig process_colon(int *quest_level, bool *force_nl, bool *seen_case)
    842   1.54  rillig {
    843  1.152  rillig     if (*quest_level > 0) {	/* part of a '?:' operator */
    844  1.152  rillig 	--*quest_level;
    845   1.54  rillig 	if (ps.want_blank)
    846   1.65  rillig 	    *code.e++ = ' ';
    847   1.65  rillig 	*code.e++ = ':';
    848   1.54  rillig 	ps.want_blank = true;
    849   1.54  rillig 	return;
    850   1.54  rillig     }
    851  1.113  rillig 
    852  1.149  rillig     if (ps.init_or_struct) {	/* bit-field */
    853   1.65  rillig 	*code.e++ = ':';
    854   1.54  rillig 	ps.want_blank = false;
    855   1.54  rillig 	return;
    856   1.54  rillig     }
    857   1.54  rillig 
    858  1.113  rillig     buf_add_buf(&lab, &code);	/* 'case' or 'default' or named label */
    859  1.112  rillig     buf_add_char(&lab, ':');
    860  1.112  rillig     buf_terminate(&lab);
    861  1.112  rillig     buf_reset(&code);
    862  1.112  rillig 
    863  1.113  rillig     ps.in_stmt = false;
    864  1.113  rillig     ps.is_case_label = *seen_case;
    865  1.113  rillig     *force_nl = *seen_case;
    866  1.105  rillig     *seen_case = false;
    867   1.54  rillig     ps.want_blank = false;
    868   1.54  rillig }
    869   1.54  rillig 
    870   1.54  rillig static void
    871  1.152  rillig process_semicolon(bool *seen_case, int *quest_level, int decl_ind,
    872  1.156  rillig     bool tabs_to_var, bool *sp_sw, stmt_head hd, bool *force_nl)
    873   1.54  rillig {
    874   1.79  rillig     if (ps.decl_nest == 0)
    875  1.135  rillig 	ps.init_or_struct = false;
    876  1.105  rillig     *seen_case = false;		/* these will only need resetting in an error */
    877  1.152  rillig     *quest_level = 0;
    878  1.156  rillig     if (ps.last_token == lsym_rparen_or_rbracket)
    879   1.73  rillig 	ps.in_parameter_declaration = false;
    880   1.54  rillig     ps.cast_mask = 0;
    881   1.54  rillig     ps.not_cast_mask = 0;
    882   1.73  rillig     ps.block_init = false;
    883   1.54  rillig     ps.block_init_level = 0;
    884   1.54  rillig     ps.just_saw_decl--;
    885   1.54  rillig 
    886   1.65  rillig     if (ps.in_decl && code.s == code.e && !ps.block_init &&
    887   1.54  rillig 	!ps.dumped_decl_indent && ps.paren_level == 0) {
    888   1.54  rillig 	/* indent stray semicolons in declarations */
    889  1.105  rillig 	indent_declaration(decl_ind - 1, tabs_to_var);
    890   1.54  rillig 	ps.dumped_decl_indent = true;
    891   1.54  rillig     }
    892   1.54  rillig 
    893  1.135  rillig     ps.in_decl = ps.decl_nest > 0;	/* if we were in a first level
    894  1.102  rillig 					 * structure declaration, we aren't
    895  1.102  rillig 					 * anymore */
    896   1.54  rillig 
    897  1.156  rillig     if ((!*sp_sw || hd != hd_for) && ps.p_l_follow > 0) {
    898   1.54  rillig 
    899   1.54  rillig 	/*
    900  1.135  rillig 	 * There were unbalanced parens in the statement. It is a bit
    901  1.135  rillig 	 * complicated, because the semicolon might be in a for statement.
    902   1.54  rillig 	 */
    903   1.54  rillig 	diag(1, "Unbalanced parens");
    904   1.54  rillig 	ps.p_l_follow = 0;
    905  1.104  rillig 	if (*sp_sw) {		/* this is a check for an if, while, etc. with
    906   1.54  rillig 				 * unbalanced parens */
    907  1.104  rillig 	    *sp_sw = false;
    908  1.156  rillig 	    parse_hd(hd);	/* don't lose the 'if', or whatever */
    909   1.54  rillig 	}
    910   1.54  rillig     }
    911   1.65  rillig     *code.e++ = ';';
    912   1.54  rillig     ps.want_blank = true;
    913   1.86  rillig     ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the middle of a
    914   1.86  rillig 					 * stmt */
    915   1.54  rillig 
    916  1.104  rillig     if (!*sp_sw) {		/* if not if for (;;) */
    917  1.156  rillig 	parse(psym_semicolon);	/* let parser know about end of stmt */
    918  1.104  rillig 	*force_nl = true;	/* force newline after an end of stmt */
    919   1.54  rillig     }
    920   1.54  rillig }
    921   1.54  rillig 
    922   1.54  rillig static void
    923  1.156  rillig process_lbrace(bool *force_nl, bool *sp_sw, stmt_head hd,
    924  1.105  rillig     int *di_stack, int di_stack_cap, int *decl_ind)
    925   1.54  rillig {
    926  1.102  rillig     ps.in_stmt = false;		/* don't indent the {} */
    927  1.119  rillig 
    928   1.54  rillig     if (!ps.block_init)
    929  1.104  rillig 	*force_nl = true;	/* force other stuff on same line as '{' onto
    930   1.54  rillig 				 * new line */
    931   1.54  rillig     else if (ps.block_init_level <= 0)
    932   1.54  rillig 	ps.block_init_level = 1;
    933   1.54  rillig     else
    934   1.54  rillig 	ps.block_init_level++;
    935   1.54  rillig 
    936   1.65  rillig     if (code.s != code.e && !ps.block_init) {
    937  1.122  rillig 	if (!opt.brace_same_line) {
    938   1.54  rillig 	    dump_line();
    939   1.54  rillig 	    ps.want_blank = false;
    940  1.134  rillig 	} else if (ps.in_parameter_declaration && !ps.init_or_struct) {
    941   1.79  rillig 	    ps.ind_level_follow = 0;
    942   1.86  rillig 	    if (opt.function_brace_split) {	/* dump the line prior to the
    943   1.86  rillig 						 * brace ... */
    944   1.54  rillig 		dump_line();
    945   1.54  rillig 		ps.want_blank = false;
    946   1.54  rillig 	    } else		/* add a space between the decl and brace */
    947   1.54  rillig 		ps.want_blank = true;
    948   1.54  rillig 	}
    949   1.54  rillig     }
    950  1.119  rillig 
    951   1.54  rillig     if (ps.in_parameter_declaration)
    952  1.142  rillig 	blank_line_before = false;
    953   1.54  rillig 
    954   1.86  rillig     if (ps.p_l_follow > 0) {	/* check for preceding unbalanced parens */
    955   1.54  rillig 	diag(1, "Unbalanced parens");
    956   1.54  rillig 	ps.p_l_follow = 0;
    957  1.104  rillig 	if (*sp_sw) {		/* check for unclosed if, for, etc. */
    958  1.104  rillig 	    *sp_sw = false;
    959  1.156  rillig 	    parse_hd(hd);
    960   1.79  rillig 	    ps.ind_level = ps.ind_level_follow;
    961   1.54  rillig 	}
    962   1.54  rillig     }
    963  1.119  rillig 
    964   1.65  rillig     if (code.s == code.e)
    965  1.118  rillig 	ps.ind_stmt = false;	/* don't indent the '{' itself */
    966  1.134  rillig     if (ps.in_decl && ps.init_or_struct) {
    967  1.105  rillig 	di_stack[ps.decl_nest] = *decl_ind;
    968   1.79  rillig 	if (++ps.decl_nest == di_stack_cap) {
    969   1.54  rillig 	    diag(0, "Reached internal limit of %d struct levels",
    970   1.86  rillig 		di_stack_cap);
    971   1.79  rillig 	    ps.decl_nest--;
    972   1.54  rillig 	}
    973   1.54  rillig     } else {
    974   1.86  rillig 	ps.decl_on_line = false;	/* we can't be in the middle of a
    975   1.86  rillig 					 * declaration, so don't do special
    976   1.86  rillig 					 * indentation of comments */
    977  1.128  rillig 	if (opt.blanklines_after_decl_at_top && ps.in_parameter_declaration)
    978  1.142  rillig 	    blank_line_after = true;
    979   1.73  rillig 	ps.in_parameter_declaration = false;
    980   1.54  rillig 	ps.in_decl = false;
    981   1.54  rillig     }
    982  1.119  rillig 
    983  1.105  rillig     *decl_ind = 0;
    984  1.156  rillig     parse(psym_lbrace);
    985  1.116  rillig     if (ps.want_blank)
    986   1.65  rillig 	*code.e++ = ' ';
    987   1.54  rillig     ps.want_blank = false;
    988   1.65  rillig     *code.e++ = '{';
    989   1.54  rillig     ps.just_saw_decl = 0;
    990   1.54  rillig }
    991   1.54  rillig 
    992   1.54  rillig static void
    993  1.105  rillig process_rbrace(bool *sp_sw, int *decl_ind, const int *di_stack)
    994   1.54  rillig {
    995  1.156  rillig     if (ps.s_sym[ps.tos] == psym_decl && !ps.block_init) {
    996  1.156  rillig 	/* semicolons can be omitted in declarations */
    997  1.156  rillig 	parse(psym_semicolon);
    998  1.156  rillig     }
    999  1.119  rillig 
   1000   1.73  rillig     if (ps.p_l_follow != 0) {	/* check for unclosed if, for, else. */
   1001   1.54  rillig 	diag(1, "Unbalanced parens");
   1002   1.54  rillig 	ps.p_l_follow = 0;
   1003  1.104  rillig 	*sp_sw = false;
   1004   1.54  rillig     }
   1005  1.119  rillig 
   1006   1.54  rillig     ps.just_saw_decl = 0;
   1007   1.54  rillig     ps.block_init_level--;
   1008  1.119  rillig 
   1009   1.65  rillig     if (code.s != code.e && !ps.block_init) {	/* '}' must be first on line */
   1010   1.54  rillig 	if (opt.verbose)
   1011   1.54  rillig 	    diag(0, "Line broken");
   1012   1.54  rillig 	dump_line();
   1013   1.54  rillig     }
   1014  1.119  rillig 
   1015   1.65  rillig     *code.e++ = '}';
   1016   1.54  rillig     ps.want_blank = true;
   1017   1.54  rillig     ps.in_stmt = ps.ind_stmt = false;
   1018  1.119  rillig 
   1019   1.79  rillig     if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
   1020  1.105  rillig 	*decl_ind = di_stack[--ps.decl_nest];
   1021  1.151  rillig 	if (ps.decl_nest == 0 && !ps.in_parameter_declaration) {
   1022   1.54  rillig 	    ps.just_saw_decl = 2;
   1023  1.151  rillig 	    *decl_ind = ps.ind_level == 0
   1024  1.151  rillig 		? opt.decl_indent : opt.local_decl_indent;
   1025  1.151  rillig 	}
   1026   1.54  rillig 	ps.in_decl = true;
   1027   1.54  rillig     }
   1028  1.119  rillig 
   1029  1.142  rillig     blank_line_before = false;
   1030  1.156  rillig     parse(psym_rbrace);
   1031   1.54  rillig     ps.search_brace = opt.cuddle_else
   1032  1.156  rillig 	&& ps.s_sym[ps.tos] == psym_if_expr_stmt
   1033  1.143  rillig 	&& ps.s_ind_level[ps.tos] >= ps.ind_level;
   1034  1.119  rillig 
   1035   1.79  rillig     if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
   1036  1.142  rillig 	blank_line_after = true;
   1037   1.54  rillig }
   1038   1.54  rillig 
   1039   1.54  rillig static void
   1040  1.127  rillig process_keyword_do(bool *force_nl, bool *last_else)
   1041   1.54  rillig {
   1042   1.54  rillig     ps.in_stmt = false;
   1043  1.119  rillig 
   1044  1.127  rillig     if (code.e != code.s) {	/* make sure this starts a line */
   1045  1.127  rillig 	if (opt.verbose)
   1046  1.127  rillig 	    diag(0, "Line broken");
   1047  1.127  rillig 	dump_line();
   1048  1.127  rillig 	ps.want_blank = false;
   1049  1.127  rillig     }
   1050  1.119  rillig 
   1051  1.127  rillig     *force_nl = true;		/* following stuff must go onto new line */
   1052  1.127  rillig     *last_else = false;
   1053  1.156  rillig     parse(psym_do);
   1054  1.127  rillig }
   1055  1.119  rillig 
   1056  1.127  rillig static void
   1057  1.127  rillig process_keyword_else(bool *force_nl, bool *last_else)
   1058  1.127  rillig {
   1059  1.127  rillig     ps.in_stmt = false;
   1060  1.119  rillig 
   1061  1.127  rillig     if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) {
   1062  1.127  rillig 	if (opt.verbose)
   1063  1.127  rillig 	    diag(0, "Line broken");
   1064  1.127  rillig 	dump_line();		/* make sure this starts a line */
   1065  1.127  rillig 	ps.want_blank = false;
   1066   1.54  rillig     }
   1067  1.127  rillig 
   1068  1.127  rillig     *force_nl = true;		/* following stuff must go onto new line */
   1069  1.127  rillig     *last_else = true;
   1070  1.156  rillig     parse(psym_else);
   1071   1.54  rillig }
   1072   1.54  rillig 
   1073   1.54  rillig static void
   1074  1.132  rillig process_decl(int *decl_ind, bool *tabs_to_var)
   1075   1.54  rillig {
   1076  1.156  rillig     parse(psym_decl);		/* let the parser worry about indentation */
   1077  1.119  rillig 
   1078  1.156  rillig     if (ps.last_token == lsym_rparen_or_rbracket && ps.tos <= 1) {
   1079   1.65  rillig 	if (code.s != code.e) {
   1080   1.54  rillig 	    dump_line();
   1081   1.73  rillig 	    ps.want_blank = false;
   1082   1.54  rillig 	}
   1083   1.54  rillig     }
   1084  1.119  rillig 
   1085   1.79  rillig     if (ps.in_parameter_declaration && opt.indent_parameters &&
   1086   1.79  rillig 	ps.decl_nest == 0) {
   1087   1.79  rillig 	ps.ind_level = ps.ind_level_follow = 1;
   1088   1.73  rillig 	ps.ind_stmt = false;
   1089   1.54  rillig     }
   1090  1.119  rillig 
   1091  1.134  rillig     ps.init_or_struct = /* maybe */ true;
   1092  1.156  rillig     ps.in_decl = ps.decl_on_line = ps.last_token != lsym_typedef;
   1093  1.134  rillig     if (ps.decl_nest <= 0)
   1094   1.54  rillig 	ps.just_saw_decl = 2;
   1095  1.119  rillig 
   1096  1.142  rillig     blank_line_before = false;
   1097   1.54  rillig 
   1098  1.132  rillig     int len = (int)buf_len(&token) + 1;
   1099   1.82  rillig     int ind = ps.ind_level == 0 || ps.decl_nest > 0
   1100  1.149  rillig 	? opt.decl_indent	/* global variable or local member */
   1101  1.149  rillig 	: opt.local_decl_indent;	/* local variable */
   1102  1.132  rillig     *decl_ind = ind > 0 ? ind : len;
   1103  1.132  rillig     *tabs_to_var = opt.use_tabs && ind > 0;
   1104   1.54  rillig }
   1105   1.54  rillig 
   1106   1.54  rillig static void
   1107  1.156  rillig process_ident(lexer_symbol lsym, int decl_ind, bool tabs_to_var,
   1108  1.156  rillig     bool *sp_sw, bool *force_nl, stmt_head hd)
   1109   1.54  rillig {
   1110   1.54  rillig     if (ps.in_decl) {
   1111  1.156  rillig 	if (lsym == lsym_funcname) {
   1112   1.54  rillig 	    ps.in_decl = false;
   1113   1.65  rillig 	    if (opt.procnames_start_line && code.s != code.e) {
   1114   1.65  rillig 		*code.e = '\0';
   1115   1.54  rillig 		dump_line();
   1116   1.54  rillig 	    } else if (ps.want_blank) {
   1117   1.65  rillig 		*code.e++ = ' ';
   1118   1.54  rillig 	    }
   1119   1.54  rillig 	    ps.want_blank = false;
   1120  1.119  rillig 
   1121   1.54  rillig 	} else if (!ps.block_init && !ps.dumped_decl_indent &&
   1122   1.86  rillig 	    ps.paren_level == 0) {	/* if we are in a declaration, we must
   1123   1.86  rillig 					 * indent identifier */
   1124  1.105  rillig 	    indent_declaration(decl_ind, tabs_to_var);
   1125   1.54  rillig 	    ps.dumped_decl_indent = true;
   1126   1.54  rillig 	    ps.want_blank = false;
   1127   1.54  rillig 	}
   1128  1.119  rillig 
   1129  1.104  rillig     } else if (*sp_sw && ps.p_l_follow == 0) {
   1130  1.104  rillig 	*sp_sw = false;
   1131  1.104  rillig 	*force_nl = true;
   1132  1.144  rillig 	ps.next_unary = true;
   1133   1.54  rillig 	ps.in_stmt = false;
   1134  1.156  rillig 	parse_hd(hd);
   1135   1.54  rillig     }
   1136   1.54  rillig }
   1137   1.54  rillig 
   1138   1.54  rillig static void
   1139  1.111  rillig copy_token(void)
   1140   1.54  rillig {
   1141   1.54  rillig     if (ps.want_blank)
   1142  1.112  rillig 	buf_add_char(&code, ' ');
   1143  1.112  rillig     buf_add_buf(&code, &token);
   1144   1.54  rillig }
   1145   1.54  rillig 
   1146   1.54  rillig static void
   1147   1.54  rillig process_string_prefix(void)
   1148   1.54  rillig {
   1149  1.112  rillig     copy_token();
   1150   1.54  rillig     ps.want_blank = false;
   1151   1.54  rillig }
   1152   1.54  rillig 
   1153   1.54  rillig static void
   1154   1.54  rillig process_period(void)
   1155   1.54  rillig {
   1156   1.85  rillig     if (code.e[-1] == ',')
   1157   1.85  rillig 	*code.e++ = ' ';
   1158  1.102  rillig     *code.e++ = '.';
   1159  1.102  rillig     ps.want_blank = false;
   1160   1.54  rillig }
   1161   1.54  rillig 
   1162   1.54  rillig static void
   1163  1.105  rillig process_comma(int decl_ind, bool tabs_to_var, bool *force_nl)
   1164   1.54  rillig {
   1165  1.135  rillig     ps.want_blank = code.s != code.e;	/* only put blank after comma if comma
   1166   1.86  rillig 					 * does not start the line */
   1167  1.119  rillig 
   1168   1.54  rillig     if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
   1169   1.54  rillig 	!ps.dumped_decl_indent && ps.paren_level == 0) {
   1170   1.54  rillig 	/* indent leading commas and not the actual identifiers */
   1171  1.105  rillig 	indent_declaration(decl_ind - 1, tabs_to_var);
   1172   1.54  rillig 	ps.dumped_decl_indent = true;
   1173   1.54  rillig     }
   1174  1.119  rillig 
   1175   1.65  rillig     *code.e++ = ',';
   1176  1.119  rillig 
   1177   1.54  rillig     if (ps.p_l_follow == 0) {
   1178   1.54  rillig 	if (ps.block_init_level <= 0)
   1179   1.73  rillig 	    ps.block_init = false;
   1180   1.87  rillig 	if (break_comma && (opt.break_after_comma ||
   1181  1.149  rillig 		indentation_after_range(compute_code_indent(), code.s, code.e)
   1182  1.149  rillig 		>= opt.max_line_length - opt.tabsize))
   1183  1.104  rillig 	    *force_nl = true;
   1184   1.54  rillig     }
   1185   1.54  rillig }
   1186   1.54  rillig 
   1187  1.112  rillig /* move the whole line to the 'label' buffer */
   1188   1.54  rillig static void
   1189  1.115  rillig read_preprocessing_line(void)
   1190   1.54  rillig {
   1191  1.129  rillig     enum {
   1192  1.129  rillig 	PLAIN, STR, CHR, COMM
   1193  1.129  rillig     } state;
   1194  1.129  rillig 
   1195  1.112  rillig     buf_add_char(&lab, '#');
   1196   1.54  rillig 
   1197  1.129  rillig     state = PLAIN;
   1198  1.115  rillig     int com_start = 0, com_end = 0;
   1199  1.115  rillig 
   1200  1.124  rillig     while (is_hspace(*inp.s))
   1201  1.115  rillig 	inbuf_skip();
   1202  1.115  rillig 
   1203  1.129  rillig     while (*inp.s != '\n' || (state == COMM && !had_eof)) {
   1204  1.115  rillig 	buf_reserve(&lab, 2);
   1205  1.115  rillig 	*lab.e++ = inbuf_next();
   1206  1.115  rillig 	switch (lab.e[-1]) {
   1207  1.115  rillig 	case '\\':
   1208  1.129  rillig 	    if (state != COMM)
   1209  1.115  rillig 		*lab.e++ = inbuf_next();
   1210  1.115  rillig 	    break;
   1211  1.115  rillig 	case '/':
   1212  1.129  rillig 	    if (*inp.s == '*' && state == PLAIN) {
   1213  1.129  rillig 		state = COMM;
   1214  1.124  rillig 		*lab.e++ = *inp.s++;
   1215  1.115  rillig 		com_start = (int)buf_len(&lab) - 2;
   1216  1.115  rillig 	    }
   1217  1.115  rillig 	    break;
   1218  1.115  rillig 	case '"':
   1219  1.129  rillig 	    if (state == STR)
   1220  1.129  rillig 		state = PLAIN;
   1221  1.129  rillig 	    else if (state == PLAIN)
   1222  1.129  rillig 		state = STR;
   1223  1.115  rillig 	    break;
   1224  1.115  rillig 	case '\'':
   1225  1.129  rillig 	    if (state == CHR)
   1226  1.129  rillig 		state = PLAIN;
   1227  1.129  rillig 	    else if (state == PLAIN)
   1228  1.129  rillig 		state = CHR;
   1229  1.115  rillig 	    break;
   1230  1.115  rillig 	case '*':
   1231  1.129  rillig 	    if (*inp.s == '/' && state == COMM) {
   1232  1.129  rillig 		state = PLAIN;
   1233  1.124  rillig 		*lab.e++ = *inp.s++;
   1234  1.115  rillig 		com_end = (int)buf_len(&lab);
   1235   1.54  rillig 	    }
   1236  1.115  rillig 	    break;
   1237   1.54  rillig 	}
   1238  1.115  rillig     }
   1239   1.54  rillig 
   1240  1.115  rillig     while (lab.e > lab.s && is_hspace(lab.e[-1]))
   1241  1.115  rillig 	lab.e--;
   1242  1.125  rillig     if (lab.e - lab.s == com_end && saved_inp_s == NULL) {
   1243  1.115  rillig 	/* comment on preprocessor line */
   1244  1.115  rillig 	if (sc_end == NULL) {	/* if this is the first comment, we must set
   1245  1.115  rillig 				 * up the buffer */
   1246  1.115  rillig 	    save_com = sc_buf;
   1247  1.115  rillig 	    sc_end = save_com;
   1248  1.115  rillig 	} else {
   1249  1.115  rillig 	    *sc_end++ = '\n';	/* add newline between comments */
   1250  1.115  rillig 	    *sc_end++ = ' ';
   1251  1.115  rillig 	    --line_no;
   1252  1.115  rillig 	}
   1253  1.115  rillig 	if (sc_end - save_com + com_end - com_start > sc_size)
   1254  1.115  rillig 	    errx(1, "input too long");
   1255  1.115  rillig 	memmove(sc_end, lab.s + com_start, (size_t)(com_end - com_start));
   1256  1.115  rillig 	sc_end += com_end - com_start;
   1257  1.115  rillig 	lab.e = lab.s + com_start;
   1258  1.101  rillig 	while (lab.e > lab.s && is_hspace(lab.e[-1]))
   1259   1.63  rillig 	    lab.e--;
   1260  1.125  rillig 	saved_inp_s = inp.s;	/* save current input buffer */
   1261  1.125  rillig 	saved_inp_e = inp.e;
   1262  1.124  rillig 	inp.s = save_com;	/* fix so that subsequent calls to lexi will
   1263   1.58  rillig 				 * take tokens out of save_com */
   1264  1.115  rillig 	*sc_end++ = ' ';	/* add trailing blank, just in case */
   1265  1.124  rillig 	inp.e = sc_end;
   1266  1.115  rillig 	sc_end = NULL;
   1267  1.124  rillig 	debug_println("switched inp.s to save_com");
   1268   1.54  rillig     }
   1269  1.115  rillig     buf_terminate(&lab);
   1270  1.115  rillig }
   1271  1.115  rillig 
   1272  1.115  rillig static void
   1273  1.115  rillig process_preprocessing(void)
   1274  1.115  rillig {
   1275  1.115  rillig     if (com.s != com.e || lab.s != lab.e || code.s != code.e)
   1276  1.115  rillig 	dump_line();
   1277  1.115  rillig 
   1278  1.115  rillig     read_preprocessing_line();
   1279  1.115  rillig 
   1280  1.115  rillig     ps.is_case_label = false;
   1281   1.54  rillig 
   1282   1.86  rillig     if (strncmp(lab.s, "#if", 3) == 0) {	/* also ifdef, ifndef */
   1283  1.146  rillig 	if ((size_t)ifdef_level < array_length(state_stack))
   1284   1.54  rillig 	    state_stack[ifdef_level++] = ps;
   1285   1.88  rillig 	else
   1286   1.54  rillig 	    diag(1, "#if stack overflow");
   1287  1.119  rillig 
   1288   1.86  rillig     } else if (strncmp(lab.s, "#el", 3) == 0) {	/* else, elif */
   1289   1.54  rillig 	if (ifdef_level <= 0)
   1290   1.63  rillig 	    diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
   1291   1.88  rillig 	else
   1292   1.54  rillig 	    ps = state_stack[ifdef_level - 1];
   1293  1.119  rillig 
   1294   1.63  rillig     } else if (strncmp(lab.s, "#endif", 6) == 0) {
   1295   1.54  rillig 	if (ifdef_level <= 0)
   1296   1.54  rillig 	    diag(1, "Unmatched #endif");
   1297   1.54  rillig 	else
   1298   1.54  rillig 	    ifdef_level--;
   1299  1.119  rillig 
   1300   1.54  rillig     } else {
   1301   1.63  rillig 	if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
   1302   1.63  rillig 	    strncmp(lab.s + 1, "error", 5) != 0 &&
   1303   1.63  rillig 	    strncmp(lab.s + 1, "line", 4) != 0 &&
   1304   1.63  rillig 	    strncmp(lab.s + 1, "undef", 5) != 0 &&
   1305   1.63  rillig 	    strncmp(lab.s + 1, "define", 6) != 0 &&
   1306   1.63  rillig 	    strncmp(lab.s + 1, "include", 7) != 0) {
   1307   1.54  rillig 	    diag(1, "Unrecognized cpp directive");
   1308   1.54  rillig 	    return;
   1309   1.54  rillig 	}
   1310   1.54  rillig     }
   1311  1.119  rillig 
   1312   1.54  rillig     if (opt.blanklines_around_conditional_compilation) {
   1313  1.142  rillig 	blank_line_after = true;
   1314  1.140  rillig 	blank_lines_to_output = 0;
   1315   1.54  rillig     } else {
   1316  1.142  rillig 	blank_line_after = false;
   1317  1.142  rillig 	blank_line_before = false;
   1318   1.54  rillig     }
   1319   1.54  rillig 
   1320   1.54  rillig     /*
   1321   1.54  rillig      * subsequent processing of the newline character will cause the line to
   1322   1.54  rillig      * be printed
   1323   1.54  rillig      */
   1324   1.54  rillig }
   1325   1.54  rillig 
   1326   1.60  rillig static void __attribute__((__noreturn__))
   1327   1.53  rillig main_loop(void)
   1328   1.53  rillig {
   1329  1.150  rillig     bool force_nl = false;	/* when true, code must be broken */
   1330   1.75  rillig     bool last_else = false;	/* true iff last keyword was an else */
   1331  1.150  rillig     int decl_ind = 0;		/* current indentation for declarations */
   1332   1.86  rillig     int di_stack[20];		/* a stack of structure indentation levels */
   1333  1.150  rillig     bool tabs_to_var = false;	/* true if using tabs to indent to var name */
   1334  1.150  rillig     bool sp_sw = false;		/* when true, we are in the expression of
   1335   1.53  rillig 				 * if(...), while(...), etc. */
   1336  1.156  rillig     stmt_head hd = hd_0;	/* the type of statement for if (...),
   1337  1.135  rillig 				 * for (...), etc */
   1338  1.152  rillig     int quest_level = 0;	/* when this is positive, we have seen a '?'
   1339  1.152  rillig 				 * without the matching ':' in a '?:'
   1340  1.152  rillig 				 * expression */
   1341  1.150  rillig     bool seen_case = false;	/* set to true when we see a 'case', so we
   1342   1.53  rillig 				 * know what to do with the following colon */
   1343   1.53  rillig 
   1344   1.79  rillig     di_stack[ps.decl_nest = 0] = 0;
   1345    1.1     cgd 
   1346   1.59  rillig     for (;;) {			/* this is the main loop.  it will go until we
   1347    1.1     cgd 				 * reach eof */
   1348   1.75  rillig 	bool comment_buffered = false;
   1349    1.1     cgd 
   1350  1.156  rillig 	lexer_symbol lsym = lexi(&ps);	/* Read the next token.  The actual
   1351  1.150  rillig 					 * characters read are stored in
   1352  1.150  rillig 					 * "token". */
   1353    1.1     cgd 
   1354   1.25   kamil 	/*
   1355  1.118  rillig 	 * Move newlines and comments following an if (), while (), else, etc.
   1356  1.118  rillig 	 * up to the start of the following stmt to a buffer. This allows
   1357  1.118  rillig 	 * proper handling of both kinds of brace placement (-br, -bl) and
   1358  1.118  rillig 	 * cuddling "else" (-ce).
   1359   1.25   kamil 	 */
   1360  1.156  rillig 	search_brace(&lsym, &force_nl, &comment_buffered, &last_else);
   1361   1.25   kamil 
   1362  1.156  rillig 	if (lsym == lsym_eof) {
   1363   1.54  rillig 	    process_end_of_file();
   1364   1.60  rillig 	    /* NOTREACHED */
   1365   1.54  rillig 	}
   1366   1.25   kamil 
   1367  1.156  rillig 	if (lsym == lsym_newline || lsym == lsym_form_feed ||
   1368  1.156  rillig 		lsym == lsym_preprocessing)
   1369  1.130  rillig 	    force_nl = false;
   1370  1.156  rillig 	else if (lsym != lsym_comment)
   1371  1.156  rillig 	    process_comment_in_code(lsym, &force_nl);
   1372   1.25   kamil 
   1373  1.112  rillig 	buf_reserve(&code, 3);	/* space for 2 characters plus '\0' */
   1374    1.1     cgd 
   1375  1.156  rillig 	switch (lsym) {
   1376   1.25   kamil 
   1377  1.156  rillig 	case lsym_form_feed:
   1378   1.54  rillig 	    process_form_feed();
   1379   1.25   kamil 	    break;
   1380   1.25   kamil 
   1381  1.156  rillig 	case lsym_newline:
   1382   1.54  rillig 	    process_newline();
   1383   1.25   kamil 	    break;
   1384   1.25   kamil 
   1385  1.156  rillig 	case lsym_lparen_or_lbracket:
   1386  1.105  rillig 	    process_lparen_or_lbracket(decl_ind, tabs_to_var, sp_sw);
   1387   1.25   kamil 	    break;
   1388   1.25   kamil 
   1389  1.156  rillig 	case lsym_rparen_or_rbracket:
   1390  1.156  rillig 	    process_rparen_or_rbracket(&sp_sw, &force_nl, hd);
   1391   1.25   kamil 	    break;
   1392   1.25   kamil 
   1393  1.156  rillig 	case lsym_unary_op:
   1394  1.105  rillig 	    process_unary_op(decl_ind, tabs_to_var);
   1395   1.25   kamil 	    break;
   1396   1.25   kamil 
   1397  1.156  rillig 	case lsym_binary_op:
   1398   1.54  rillig 	    process_binary_op();
   1399   1.25   kamil 	    break;
   1400   1.25   kamil 
   1401  1.156  rillig 	case lsym_postfix_op:
   1402   1.54  rillig 	    process_postfix_op();
   1403   1.25   kamil 	    break;
   1404   1.25   kamil 
   1405  1.156  rillig 	case lsym_question:
   1406  1.152  rillig 	    process_question(&quest_level);
   1407   1.25   kamil 	    break;
   1408   1.25   kamil 
   1409  1.156  rillig 	case lsym_case_label:	/* got word 'case' or 'default' */
   1410  1.116  rillig 	    seen_case = true;
   1411  1.111  rillig 	    goto copy_token;
   1412   1.25   kamil 
   1413  1.156  rillig 	case lsym_colon:
   1414  1.152  rillig 	    process_colon(&quest_level, &force_nl, &seen_case);
   1415   1.25   kamil 	    break;
   1416   1.25   kamil 
   1417  1.156  rillig 	case lsym_semicolon:
   1418  1.152  rillig 	    process_semicolon(&seen_case, &quest_level, decl_ind, tabs_to_var,
   1419  1.156  rillig 		&sp_sw, hd, &force_nl);
   1420   1.25   kamil 	    break;
   1421   1.25   kamil 
   1422  1.156  rillig 	case lsym_lbrace:
   1423  1.156  rillig 	    process_lbrace(&force_nl, &sp_sw, hd, di_stack,
   1424  1.146  rillig 		(int)array_length(di_stack), &decl_ind);
   1425   1.25   kamil 	    break;
   1426   1.25   kamil 
   1427  1.156  rillig 	case lsym_rbrace:
   1428  1.105  rillig 	    process_rbrace(&sp_sw, &decl_ind, di_stack);
   1429   1.25   kamil 	    break;
   1430   1.25   kamil 
   1431  1.156  rillig 	case lsym_switch:
   1432  1.156  rillig 	    sp_sw = true;	/* the interesting stuff is done after the
   1433  1.156  rillig 				 * expressions are scanned */
   1434  1.156  rillig 	    hd = hd_switch;	/* remember the type of header for later use
   1435  1.156  rillig 				 * by parser */
   1436  1.111  rillig 	    goto copy_token;
   1437   1.25   kamil 
   1438  1.156  rillig 	case lsym_for:
   1439  1.156  rillig 	    sp_sw = true;
   1440  1.156  rillig 	    hd = hd_for;
   1441  1.154  rillig 	    goto copy_token;
   1442   1.25   kamil 
   1443  1.156  rillig 	case lsym_if:
   1444  1.154  rillig 	    sp_sw = true;
   1445  1.156  rillig 	    hd = hd_if;
   1446  1.154  rillig 	    goto copy_token;
   1447  1.154  rillig 
   1448  1.156  rillig 	case lsym_while:
   1449  1.154  rillig 	    sp_sw = true;
   1450  1.156  rillig 	    hd = hd_while;
   1451  1.111  rillig 	    goto copy_token;
   1452   1.25   kamil 
   1453  1.156  rillig 	case lsym_do:
   1454  1.153  rillig 	    process_keyword_do(&force_nl, &last_else);
   1455  1.153  rillig 	    goto copy_token;
   1456  1.153  rillig 
   1457  1.156  rillig 	case lsym_else:
   1458  1.153  rillig 	    process_keyword_else(&force_nl, &last_else);
   1459  1.111  rillig 	    goto copy_token;
   1460   1.25   kamil 
   1461  1.156  rillig 	case lsym_typedef:
   1462  1.156  rillig 	case lsym_storage_class:
   1463  1.142  rillig 	    blank_line_before = false;
   1464  1.111  rillig 	    goto copy_token;
   1465   1.25   kamil 
   1466  1.156  rillig 	case lsym_tag:
   1467   1.25   kamil 	    if (ps.p_l_follow > 0)
   1468  1.111  rillig 		goto copy_token;
   1469   1.25   kamil 	    /* FALLTHROUGH */
   1470  1.156  rillig 	case lsym_type:
   1471  1.105  rillig 	    process_decl(&decl_ind, &tabs_to_var);
   1472  1.111  rillig 	    goto copy_token;
   1473   1.25   kamil 
   1474  1.156  rillig 	case lsym_funcname:
   1475  1.156  rillig 	case lsym_ident:	/* an identifier, constant or string */
   1476  1.156  rillig 	    process_ident(lsym, decl_ind, tabs_to_var, &sp_sw, &force_nl, hd);
   1477  1.111  rillig     copy_token:
   1478  1.111  rillig 	    copy_token();
   1479  1.156  rillig 	    if (lsym != lsym_funcname)
   1480   1.25   kamil 		ps.want_blank = true;
   1481   1.25   kamil 	    break;
   1482   1.25   kamil 
   1483  1.156  rillig 	case lsym_string_prefix:
   1484   1.54  rillig 	    process_string_prefix();
   1485   1.25   kamil 	    break;
   1486    1.1     cgd 
   1487  1.156  rillig 	case lsym_period:
   1488   1.54  rillig 	    process_period();
   1489   1.25   kamil 	    break;
   1490   1.25   kamil 
   1491  1.156  rillig 	case lsym_comma:
   1492  1.105  rillig 	    process_comma(decl_ind, tabs_to_var, &force_nl);
   1493   1.25   kamil 	    break;
   1494   1.25   kamil 
   1495  1.156  rillig 	case lsym_preprocessing:	/* the initial '#' */
   1496   1.54  rillig 	    process_preprocessing();
   1497   1.54  rillig 	    break;
   1498  1.119  rillig 
   1499  1.156  rillig 	case lsym_comment:	/* the initial '/' '*' or '//' of a comment */
   1500   1.56  rillig 	    process_comment();
   1501   1.25   kamil 	    break;
   1502   1.30  rillig 
   1503   1.30  rillig 	default:
   1504   1.30  rillig 	    break;
   1505   1.64  rillig 	}
   1506   1.25   kamil 
   1507   1.65  rillig 	*code.e = '\0';
   1508  1.156  rillig 	if (lsym != lsym_comment && lsym != lsym_newline &&
   1509  1.156  rillig 		lsym != lsym_preprocessing)
   1510  1.156  rillig 	    ps.last_token = lsym;
   1511   1.53  rillig     }
   1512   1.53  rillig }
   1513   1.53  rillig 
   1514   1.53  rillig int
   1515   1.53  rillig main(int argc, char **argv)
   1516   1.53  rillig {
   1517   1.53  rillig     main_init_globals();
   1518   1.53  rillig     main_parse_command_line(argc, argv);
   1519   1.53  rillig #if HAVE_CAPSICUM
   1520   1.53  rillig     init_capsicum();
   1521   1.53  rillig #endif
   1522   1.53  rillig     main_prepare_parsing();
   1523   1.53  rillig     main_loop();
   1524   1.25   kamil }
   1525    1.6   lukem 
   1526   1.48  rillig #ifdef debug
   1527   1.48  rillig void
   1528   1.48  rillig debug_printf(const char *fmt, ...)
   1529   1.48  rillig {
   1530   1.48  rillig     FILE *f = output == stdout ? stderr : stdout;
   1531   1.48  rillig     va_list ap;
   1532   1.48  rillig 
   1533   1.48  rillig     va_start(ap, fmt);
   1534   1.48  rillig     vfprintf(f, fmt, ap);
   1535   1.48  rillig     va_end(ap);
   1536   1.48  rillig }
   1537   1.48  rillig 
   1538   1.48  rillig void
   1539   1.48  rillig debug_println(const char *fmt, ...)
   1540   1.48  rillig {
   1541   1.48  rillig     FILE *f = output == stdout ? stderr : stdout;
   1542   1.48  rillig     va_list ap;
   1543   1.48  rillig 
   1544   1.48  rillig     va_start(ap, fmt);
   1545   1.48  rillig     vfprintf(f, fmt, ap);
   1546   1.48  rillig     va_end(ap);
   1547   1.48  rillig     fprintf(f, "\n");
   1548   1.48  rillig }
   1549   1.48  rillig 
   1550   1.48  rillig void
   1551   1.48  rillig debug_vis_range(const char *prefix, const char *s, const char *e,
   1552   1.86  rillig     const char *suffix)
   1553   1.48  rillig {
   1554   1.48  rillig     debug_printf("%s", prefix);
   1555   1.48  rillig     for (const char *p = s; p < e; p++) {
   1556   1.48  rillig 	if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
   1557   1.48  rillig 	    debug_printf("%c", *p);
   1558   1.48  rillig 	else if (*p == '\n')
   1559   1.48  rillig 	    debug_printf("\\n");
   1560   1.48  rillig 	else if (*p == '\t')
   1561   1.48  rillig 	    debug_printf("\\t");
   1562   1.48  rillig 	else
   1563   1.48  rillig 	    debug_printf("\\x%02x", *p);
   1564   1.48  rillig     }
   1565   1.48  rillig     debug_printf("%s", suffix);
   1566   1.48  rillig }
   1567   1.48  rillig #endif
   1568   1.68  rillig 
   1569   1.68  rillig static void *
   1570   1.68  rillig nonnull(void *p)
   1571   1.68  rillig {
   1572   1.68  rillig     if (p == NULL)
   1573   1.68  rillig 	err(EXIT_FAILURE, NULL);
   1574   1.68  rillig     return p;
   1575   1.68  rillig }
   1576   1.68  rillig 
   1577   1.68  rillig void *
   1578   1.68  rillig xmalloc(size_t size)
   1579   1.68  rillig {
   1580   1.68  rillig     return nonnull(malloc(size));
   1581   1.68  rillig }
   1582   1.68  rillig 
   1583   1.68  rillig void *
   1584   1.68  rillig xrealloc(void *p, size_t new_size)
   1585   1.68  rillig {
   1586   1.68  rillig     return nonnull(realloc(p, new_size));
   1587   1.68  rillig }
   1588   1.68  rillig 
   1589   1.68  rillig char *
   1590   1.68  rillig xstrdup(const char *s)
   1591   1.68  rillig {
   1592   1.68  rillig     return nonnull(strdup(s));
   1593   1.68  rillig }
   1594