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