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