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