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