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