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