xref: /freebsd/usr.bin/indent/indent.c (revision 02bde5cf13354d39157b9f43fb99ff0d34bf5b35)
170a3049eSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
59b50d902SRodney W. Grimes  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
69b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
79b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
38958d7c9fSPhilippe Charnier #if 0
39d0054952SPhilippe Charnier #ifndef lint
409b50d902SRodney W. Grimes static char sccsid[] = "@(#)indent.c	5.17 (Berkeley) 6/7/93";
419b50d902SRodney W. Grimes #endif /* not lint */
42d0054952SPhilippe Charnier #endif
43d0054952SPhilippe Charnier 
44e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
45e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/param.h>
48d36899d1SConrad Meyer #include <sys/capsicum.h>
497672a014SMariusz Zaborski #include <capsicum_helpers.h>
50958d7c9fSPhilippe Charnier #include <err.h>
5110cc720cSConrad Meyer #include <errno.h>
529b50d902SRodney W. Grimes #include <fcntl.h>
539b50d902SRodney W. Grimes #include <unistd.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
569b50d902SRodney W. Grimes #include <string.h>
577916863dSJens Schweikhardt #include <ctype.h>
589b50d902SRodney W. Grimes #include "indent_globs.h"
599b50d902SRodney W. Grimes #include "indent_codes.h"
607916863dSJens Schweikhardt #include "indent.h"
617916863dSJens Schweikhardt 
62*02bde5cfSKyle Evans /* Globals */
63*02bde5cfSKyle Evans FILE	*input, *output;
64*02bde5cfSKyle Evans char	*labbuf, *s_lab, *e_lab, *l_lab;
65*02bde5cfSKyle Evans char	*codebuf, *s_code, *e_code, *l_code;
66*02bde5cfSKyle Evans char	*combuf, *s_com, *e_com, *l_com;
67*02bde5cfSKyle Evans char	*tokenbuf, *s_token, *e_token, *l_token;
68*02bde5cfSKyle Evans char	*in_buffer, *in_buffer_limit;
69*02bde5cfSKyle Evans char	*buf_ptr, *buf_end;
70*02bde5cfSKyle Evans 
71*02bde5cfSKyle Evans char	 sc_buf[sc_size];
72*02bde5cfSKyle Evans 
73*02bde5cfSKyle Evans char	*save_com, *sc_end;
74*02bde5cfSKyle Evans char	*bp_save;
75*02bde5cfSKyle Evans char	*be_save;
76*02bde5cfSKyle Evans 
77*02bde5cfSKyle Evans struct options		opt;
78*02bde5cfSKyle Evans int	 line_no;
79*02bde5cfSKyle Evans 
80*02bde5cfSKyle Evans struct parser_state	ps;
81*02bde5cfSKyle Evans int	 ifdef_level;
82*02bde5cfSKyle Evans struct parser_state	state_stack[5];
83*02bde5cfSKyle Evans struct parser_state	match_state[5];
84*02bde5cfSKyle Evans 
85*02bde5cfSKyle Evans 
867916863dSJens Schweikhardt static void bakcopy(void);
8711c49893SPedro F. Giffuni static void indent_declaration(int, int);
889b50d902SRodney W. Grimes 
898c7e7698SDavid Malone const char *in_name = "Standard Input";	/* will always point to name of input
909b50d902SRodney W. Grimes 					 * file */
918c7e7698SDavid Malone const char *out_name = "Standard Output";	/* will always point to name
929b50d902SRodney W. Grimes 						 * of output file */
93c6841b07SKyle Evans const char *simple_backup_suffix = ".BAK";	/* Suffix to use for backup
94c6841b07SKyle Evans 						 * files */
959b50d902SRodney W. Grimes char        bakfile[MAXPATHLEN] = "";
969b50d902SRodney W. Grimes 
977916863dSJens Schweikhardt int
987916863dSJens Schweikhardt main(int argc, char **argv)
999b50d902SRodney W. Grimes {
10010cc720cSConrad Meyer     cap_rights_t rights;
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes     int         dec_ind;	/* current indentation for declarations */
1039b50d902SRodney W. Grimes     int         di_stack[20];	/* a stack of structure indentation levels */
1049b50d902SRodney W. Grimes     int         force_nl;	/* when true, code must be broken */
1057916863dSJens Schweikhardt     int         hd_type = 0;	/* used to store type of stmt for if (...),
1069b50d902SRodney W. Grimes 				 * for (...), etc */
10790af6a72SJuli Mallett     int		i;		/* local loop counter */
1089b50d902SRodney W. Grimes     int         scase;		/* set to true when we see a case, so we will
1099b50d902SRodney W. Grimes 				 * know what to do with the following colon */
110dc51023cSPhilippe Charnier     int         sp_sw;		/* when true, we are in the expression of
1119b50d902SRodney W. Grimes 				 * if(...), while(...), etc. */
1129b50d902SRodney W. Grimes     int         squest;		/* when this is positive, we have seen a ?
1139b50d902SRodney W. Grimes 				 * without the matching : in a <c>?<s>:<s>
1149b50d902SRodney W. Grimes 				 * construct */
1158c7e7698SDavid Malone     const char *t_ptr;		/* used for copying tokens */
116e3625e9cSJens Schweikhardt     int		tabs_to_var;	/* true if using tabs to indent to var name */
1179b50d902SRodney W. Grimes     int         type_code;	/* the type of token, returned by lexi */
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes     int         last_else = 0;	/* true iff last keyword was an else */
12086adac04SPiotr Pawel Stefaniak     const char *profile_name = NULL;
121c6841b07SKyle Evans     const char *envval = NULL;
12263c3f226SPiotr Pawel Stefaniak     struct parser_state transient_state; /* a copy for lookup */
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes     /*-----------------------------------------------*\
1259b50d902SRodney W. Grimes     |		      INITIALIZATION		      |
1269b50d902SRodney W. Grimes     \*-----------------------------------------------*/
1279b50d902SRodney W. Grimes 
128d0054952SPhilippe Charnier     found_err = 0;
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes     ps.p_stack[0] = stmt;	/* this is the parser's stack */
1319b50d902SRodney W. Grimes     ps.last_nl = true;		/* this is true if the last thing scanned was
1329b50d902SRodney W. Grimes 				 * a newline */
1339b50d902SRodney W. Grimes     ps.last_token = semicolon;
1349b50d902SRodney W. Grimes     combuf = (char *) malloc(bufsize);
135d0054952SPhilippe Charnier     if (combuf == NULL)
136d0054952SPhilippe Charnier 	err(1, NULL);
1379b50d902SRodney W. Grimes     labbuf = (char *) malloc(bufsize);
138d0054952SPhilippe Charnier     if (labbuf == NULL)
139d0054952SPhilippe Charnier 	err(1, NULL);
1409b50d902SRodney W. Grimes     codebuf = (char *) malloc(bufsize);
141d0054952SPhilippe Charnier     if (codebuf == NULL)
142d0054952SPhilippe Charnier 	err(1, NULL);
1439b50d902SRodney W. Grimes     tokenbuf = (char *) malloc(bufsize);
144d0054952SPhilippe Charnier     if (tokenbuf == NULL)
145d0054952SPhilippe Charnier 	err(1, NULL);
146a6bcfda4SPedro F. Giffuni     alloc_typenames();
14701c66110SPiotr Pawel Stefaniak     init_constant_tt();
1489b50d902SRodney W. Grimes     l_com = combuf + bufsize - 5;
1499b50d902SRodney W. Grimes     l_lab = labbuf + bufsize - 5;
1509b50d902SRodney W. Grimes     l_code = codebuf + bufsize - 5;
1519b50d902SRodney W. Grimes     l_token = tokenbuf + bufsize - 5;
1529b50d902SRodney W. Grimes     combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
1539b50d902SRodney W. Grimes 						 * comment buffers */
1549b50d902SRodney W. Grimes     combuf[1] = codebuf[1] = labbuf[1] = '\0';
1557e53aaedSPiotr Pawel Stefaniak     opt.else_if = 1;		/* Default else-if special processing to on */
1569b50d902SRodney W. Grimes     s_lab = e_lab = labbuf + 1;
1579b50d902SRodney W. Grimes     s_code = e_code = codebuf + 1;
1589b50d902SRodney W. Grimes     s_com = e_com = combuf + 1;
1599b50d902SRodney W. Grimes     s_token = e_token = tokenbuf + 1;
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes     in_buffer = (char *) malloc(10);
162d0054952SPhilippe Charnier     if (in_buffer == NULL)
163d0054952SPhilippe Charnier 	err(1, NULL);
1649b50d902SRodney W. Grimes     in_buffer_limit = in_buffer + 8;
1659b50d902SRodney W. Grimes     buf_ptr = buf_end = in_buffer;
1669b50d902SRodney W. Grimes     line_no = 1;
1679b50d902SRodney W. Grimes     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
1689b50d902SRodney W. Grimes     sp_sw = force_nl = false;
1699b50d902SRodney W. Grimes     ps.in_or_st = false;
1709b50d902SRodney W. Grimes     ps.bl_line = true;
1719b50d902SRodney W. Grimes     dec_ind = 0;
1729b50d902SRodney W. Grimes     di_stack[ps.dec_nest = 0] = 0;
1739b50d902SRodney W. Grimes     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes     scase = ps.pcase = false;
1769b50d902SRodney W. Grimes     squest = 0;
177c917a54bSPedro F. Giffuni     sc_end = NULL;
178c917a54bSPedro F. Giffuni     bp_save = NULL;
179c917a54bSPedro F. Giffuni     be_save = NULL;
1809b50d902SRodney W. Grimes 
181c917a54bSPedro F. Giffuni     output = NULL;
182488633c7SPhilippe Charnier     tabs_to_var = 0;
1839b50d902SRodney W. Grimes 
184c6841b07SKyle Evans     envval = getenv("SIMPLE_BACKUP_SUFFIX");
185c6841b07SKyle Evans     if (envval)
186c6841b07SKyle Evans         simple_backup_suffix = envval;
187c6841b07SKyle Evans 
1889b50d902SRodney W. Grimes     /*--------------------------------------------------*\
1899b50d902SRodney W. Grimes     |   		COMMAND LINE SCAN		 |
1909b50d902SRodney W. Grimes     \*--------------------------------------------------*/
1919b50d902SRodney W. Grimes 
1929b50d902SRodney W. Grimes #ifdef undef
1939b50d902SRodney W. Grimes     max_col = 78;		/* -l78 */
1949b50d902SRodney W. Grimes     lineup_to_parens = 1;	/* -lp */
195aac30b75SPiotr Pawel Stefaniak     lineup_to_parens_always = 0;	/* -nlpl */
1969b50d902SRodney W. Grimes     ps.ljust_decl = 0;		/* -ndj */
1979b50d902SRodney W. Grimes     ps.com_ind = 33;		/* -c33 */
1989b50d902SRodney W. Grimes     star_comment_cont = 1;	/* -sc */
1999b50d902SRodney W. Grimes     ps.ind_size = 8;		/* -i8 */
2009b50d902SRodney W. Grimes     verbose = 0;
2019b50d902SRodney W. Grimes     ps.decl_indent = 16;	/* -di16 */
20288ce0e7fSBruce Evans     ps.local_decl_indent = -1;	/* if this is not set to some nonnegative value
20388ce0e7fSBruce Evans 				 * by an arg, we will set this equal to
20488ce0e7fSBruce Evans 				 * ps.decl_ind */
2059b50d902SRodney W. Grimes     ps.indent_parameters = 1;	/* -ip */
2069b50d902SRodney W. Grimes     ps.decl_com_ind = 0;	/* if this is not set to some positive value
2079b50d902SRodney W. Grimes 				 * by an arg, we will set this equal to
2089b50d902SRodney W. Grimes 				 * ps.com_ind */
2099b50d902SRodney W. Grimes     btype_2 = 1;		/* -br */
2109b50d902SRodney W. Grimes     cuddle_else = 1;		/* -ce */
2119b50d902SRodney W. Grimes     ps.unindent_displace = 0;	/* -d0 */
2129b50d902SRodney W. Grimes     ps.case_indent = 0;		/* -cli0 */
213a5e1cac0SDavid E. O'Brien     format_block_comments = 1;	/* -fcb */
2149b50d902SRodney W. Grimes     format_col1_comments = 1;	/* -fc1 */
2159b50d902SRodney W. Grimes     procnames_start_line = 1;	/* -psl */
2169b50d902SRodney W. Grimes     proc_calls_space = 0;	/* -npcs */
2179b50d902SRodney W. Grimes     comment_delimiter_on_blankline = 1;	/* -cdb */
2189b50d902SRodney W. Grimes     ps.leave_comma = 1;		/* -nbc */
2199b50d902SRodney W. Grimes #endif
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i)
2229b50d902SRodney W. Grimes 	if (strcmp(argv[i], "-npro") == 0)
2239b50d902SRodney W. Grimes 	    break;
22486adac04SPiotr Pawel Stefaniak 	else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
22586adac04SPiotr Pawel Stefaniak 	    profile_name = argv[i];	/* non-empty -P (set profile) */
2269b50d902SRodney W. Grimes     set_defaults();
2279b50d902SRodney W. Grimes     if (i >= argc)
22886adac04SPiotr Pawel Stefaniak 	set_profile(profile_name);
2299b50d902SRodney W. Grimes 
2309b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i) {
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes 	/*
2339b50d902SRodney W. Grimes 	 * look thru args (if any) for changes to defaults
2349b50d902SRodney W. Grimes 	 */
2359b50d902SRodney W. Grimes 	if (argv[i][0] != '-') {/* no flag on parameter */
236f5f8c098SKevin Lo 	    if (input == NULL) {	/* we must have the input file */
2379b50d902SRodney W. Grimes 		in_name = argv[i];	/* remember name of input file */
2389b50d902SRodney W. Grimes 		input = fopen(in_name, "r");
239f5f8c098SKevin Lo 		if (input == NULL)	/* check for open error */
2400c4d24a7SKris Kennaway 			err(1, "%s", in_name);
2419b50d902SRodney W. Grimes 		continue;
2429b50d902SRodney W. Grimes 	    }
243f5f8c098SKevin Lo 	    else if (output == NULL) {	/* we have the output file */
2449b50d902SRodney W. Grimes 		out_name = argv[i];	/* remember name of output file */
2459b50d902SRodney W. Grimes 		if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
2469b50d902SRodney W. Grimes 							 * the file */
247958d7c9fSPhilippe Charnier 		    errx(1, "input and output files must be different");
2489b50d902SRodney W. Grimes 		}
2499b50d902SRodney W. Grimes 		output = fopen(out_name, "w");
250f5f8c098SKevin Lo 		if (output == NULL)	/* check for create error */
2510c4d24a7SKris Kennaway 			err(1, "%s", out_name);
2529b50d902SRodney W. Grimes 		continue;
2539b50d902SRodney W. Grimes 	    }
254958d7c9fSPhilippe Charnier 	    errx(1, "unknown parameter: %s", argv[i]);
2559b50d902SRodney W. Grimes 	}
2569b50d902SRodney W. Grimes 	else
2579b50d902SRodney W. Grimes 	    set_option(argv[i]);
2589b50d902SRodney W. Grimes     }				/* end of for */
259f5f8c098SKevin Lo     if (input == NULL)
26018251d71SPeter Hawkins 	input = stdin;
261f5f8c098SKevin Lo     if (output == NULL) {
2621479f36dSPiotr Pawel Stefaniak 	if (input == stdin)
2639b50d902SRodney W. Grimes 	    output = stdout;
2649b50d902SRodney W. Grimes 	else {
2659b50d902SRodney W. Grimes 	    out_name = in_name;
2669b50d902SRodney W. Grimes 	    bakcopy();
2679b50d902SRodney W. Grimes 	}
2689ef5c48bSBill Fumerola     }
26910cc720cSConrad Meyer 
27010cc720cSConrad Meyer     /* Restrict input/output descriptors and enter Capsicum sandbox. */
27110cc720cSConrad Meyer     cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
272377421dfSMariusz Zaborski     if (caph_rights_limit(fileno(output), &rights) < 0)
27310cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
27410cc720cSConrad Meyer     cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
275377421dfSMariusz Zaborski     if (caph_rights_limit(fileno(input), &rights) < 0)
27610cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
2777672a014SMariusz Zaborski     if (caph_enter() < 0)
27810cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to enter capability mode");
27910cc720cSConrad Meyer 
2807e53aaedSPiotr Pawel Stefaniak     if (opt.com_ind <= 1)
2817e53aaedSPiotr Pawel Stefaniak 	opt.com_ind = 2;	/* don't put normal comments before column 2 */
2827e53aaedSPiotr Pawel Stefaniak     if (opt.block_comment_max_col <= 0)
2837e53aaedSPiotr Pawel Stefaniak 	opt.block_comment_max_col = opt.max_col;
2847e53aaedSPiotr Pawel Stefaniak     if (opt.local_decl_indent < 0) /* if not specified by user, set this */
2857e53aaedSPiotr Pawel Stefaniak 	opt.local_decl_indent = opt.decl_indent;
2867e53aaedSPiotr Pawel Stefaniak     if (opt.decl_com_ind <= 0)	/* if not specified by user, set this */
2877e53aaedSPiotr Pawel Stefaniak 	opt.decl_com_ind = opt.ljust_decl ? (opt.com_ind <= 10 ? 2 : opt.com_ind - 8) : opt.com_ind;
2887e53aaedSPiotr Pawel Stefaniak     if (opt.continuation_indent == 0)
2897e53aaedSPiotr Pawel Stefaniak 	opt.continuation_indent = opt.ind_size;
2909b50d902SRodney W. Grimes     fill_buffer();		/* get first batch of stuff into input buffer */
2919b50d902SRodney W. Grimes 
2929b50d902SRodney W. Grimes     parse(semicolon);
2939b50d902SRodney W. Grimes     {
29490af6a72SJuli Mallett 	char *p = buf_ptr;
29590af6a72SJuli Mallett 	int col = 1;
2969b50d902SRodney W. Grimes 
2979b50d902SRodney W. Grimes 	while (1) {
2989b50d902SRodney W. Grimes 	    if (*p == ' ')
2999b50d902SRodney W. Grimes 		col++;
3009b50d902SRodney W. Grimes 	    else if (*p == '\t')
3017e53aaedSPiotr Pawel Stefaniak 		col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1;
3029b50d902SRodney W. Grimes 	    else
3039b50d902SRodney W. Grimes 		break;
3049b50d902SRodney W. Grimes 	    p++;
3059b50d902SRodney W. Grimes 	}
3067e53aaedSPiotr Pawel Stefaniak 	if (col > opt.ind_size)
3077e53aaedSPiotr Pawel Stefaniak 	    ps.ind_level = ps.i_l_follow = col / opt.ind_size;
3089b50d902SRodney W. Grimes     }
3099b50d902SRodney W. Grimes 
3109b50d902SRodney W. Grimes     /*
3119b50d902SRodney W. Grimes      * START OF MAIN LOOP
3129b50d902SRodney W. Grimes      */
3139b50d902SRodney W. Grimes 
3149b50d902SRodney W. Grimes     while (1) {			/* this is the main loop.  it will go until we
3159b50d902SRodney W. Grimes 				 * reach eof */
316ec5ac89eSPiotr Pawel Stefaniak 	int comment_buffered = false;
3179b50d902SRodney W. Grimes 
31863c3f226SPiotr Pawel Stefaniak 	type_code = lexi(&ps);	/* lexi reads one token.  The actual
3199b50d902SRodney W. Grimes 				 * characters read are stored in "token". lexi
3209b50d902SRodney W. Grimes 				 * returns a code indicating the type of token */
3219b50d902SRodney W. Grimes 
3229b50d902SRodney W. Grimes 	/*
323ec5ac89eSPiotr Pawel Stefaniak 	 * The following code moves newlines and comments following an if (),
324ec5ac89eSPiotr Pawel Stefaniak 	 * while (), else, etc. up to the start of the following stmt to
325ec5ac89eSPiotr Pawel Stefaniak 	 * a buffer. This allows proper handling of both kinds of brace
326ec5ac89eSPiotr Pawel Stefaniak 	 * placement (-br, -bl) and cuddling "else" (-ce).
3279b50d902SRodney W. Grimes 	 */
3289b50d902SRodney W. Grimes 
329ec5ac89eSPiotr Pawel Stefaniak 	while (ps.search_brace) {
3309b50d902SRodney W. Grimes 	    switch (type_code) {
3319b50d902SRodney W. Grimes 	    case newline:
332ec5ac89eSPiotr Pawel Stefaniak 		if (sc_end == NULL) {
3333c51c3cfSPiotr Pawel Stefaniak 		    save_com = sc_buf;
3349b50d902SRodney W. Grimes 		    save_com[0] = save_com[1] = ' ';
335ec5ac89eSPiotr Pawel Stefaniak 		    sc_end = &save_com[2];
3369b50d902SRodney W. Grimes 		}
337ec5ac89eSPiotr Pawel Stefaniak 		*sc_end++ = '\n';
338ec5ac89eSPiotr Pawel Stefaniak 		/*
339ec5ac89eSPiotr Pawel Stefaniak 		 * We may have inherited a force_nl == true from the previous
340ec5ac89eSPiotr Pawel Stefaniak 		 * token (like a semicolon). But once we know that a newline
341ec5ac89eSPiotr Pawel Stefaniak 		 * has been scanned in this loop, force_nl should be false.
342ec5ac89eSPiotr Pawel Stefaniak 		 *
343ec5ac89eSPiotr Pawel Stefaniak 		 * However, the force_nl == true must be preserved if newline
344ec5ac89eSPiotr Pawel Stefaniak 		 * is never scanned in this loop, so this assignment cannot be
345ec5ac89eSPiotr Pawel Stefaniak 		 * done earlier.
346ec5ac89eSPiotr Pawel Stefaniak 		 */
347ec5ac89eSPiotr Pawel Stefaniak 		force_nl = false;
348ec5ac89eSPiotr Pawel Stefaniak 	    case form_feed:
349ec5ac89eSPiotr Pawel Stefaniak 		break;
350ec5ac89eSPiotr Pawel Stefaniak 	    case comment:
351ec5ac89eSPiotr Pawel Stefaniak 		if (sc_end == NULL) {
3523c51c3cfSPiotr Pawel Stefaniak 		    /*
3533c51c3cfSPiotr Pawel Stefaniak 		     * Copy everything from the start of the line, because
3543c51c3cfSPiotr Pawel Stefaniak 		     * pr_comment() will use that to calculate original
3553c51c3cfSPiotr Pawel Stefaniak 		     * indentation of a boxed comment.
3563c51c3cfSPiotr Pawel Stefaniak 		     */
3573c51c3cfSPiotr Pawel Stefaniak 		    memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
3583c51c3cfSPiotr Pawel Stefaniak 		    save_com = sc_buf + (buf_ptr - in_buffer - 4);
359ec5ac89eSPiotr Pawel Stefaniak 		    save_com[0] = save_com[1] = ' ';
360ec5ac89eSPiotr Pawel Stefaniak 		    sc_end = &save_com[2];
3619b50d902SRodney W. Grimes 		}
362ec5ac89eSPiotr Pawel Stefaniak 		comment_buffered = true;
3639b50d902SRodney W. Grimes 		*sc_end++ = '/';	/* copy in start of comment */
3649b50d902SRodney W. Grimes 		*sc_end++ = '*';
3659b50d902SRodney W. Grimes 		for (;;) {	/* loop until we get to the end of the comment */
3669b50d902SRodney W. Grimes 		    *sc_end = *buf_ptr++;
3679b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
3689b50d902SRodney W. Grimes 			fill_buffer();
3699b50d902SRodney W. Grimes 		    if (*sc_end++ == '*' && *buf_ptr == '/')
3709b50d902SRodney W. Grimes 			break;	/* we are at end of comment */
371ec5ac89eSPiotr Pawel Stefaniak 		    if (sc_end >= &save_com[sc_size]) {	/* check for temp buffer
3729b50d902SRodney W. Grimes 							 * overflow */
373d0054952SPhilippe Charnier 			diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
3749b50d902SRodney W. Grimes 			fflush(output);
3759b50d902SRodney W. Grimes 			exit(1);
3769b50d902SRodney W. Grimes 		    }
3779b50d902SRodney W. Grimes 		}
3789b50d902SRodney W. Grimes 		*sc_end++ = '/';	/* add ending slash */
3799b50d902SRodney W. Grimes 		if (++buf_ptr >= buf_end)	/* get past / in buffer */
3809b50d902SRodney W. Grimes 		    fill_buffer();
3819b50d902SRodney W. Grimes 		break;
382ec5ac89eSPiotr Pawel Stefaniak 	    case lbrace:
383ec5ac89eSPiotr Pawel Stefaniak 		/*
384ec5ac89eSPiotr Pawel Stefaniak 		 * Put KNF-style lbraces before the buffered up tokens and
385ec5ac89eSPiotr Pawel Stefaniak 		 * jump out of this loop in order to avoid copying the token
386ec5ac89eSPiotr Pawel Stefaniak 		 * again under the default case of the switch below.
387ec5ac89eSPiotr Pawel Stefaniak 		 */
3887e53aaedSPiotr Pawel Stefaniak 		if (sc_end != NULL && opt.btype_2) {
389ec5ac89eSPiotr Pawel Stefaniak 		    save_com[0] = '{';
390ec5ac89eSPiotr Pawel Stefaniak 		    /*
391ec5ac89eSPiotr Pawel Stefaniak 		     * Originally the lbrace may have been alone on its own
392ec5ac89eSPiotr Pawel Stefaniak 		     * line, but it will be moved into "the else's line", so
393ec5ac89eSPiotr Pawel Stefaniak 		     * if there was a newline resulting from the "{" before,
394ec5ac89eSPiotr Pawel Stefaniak 		     * it must be scanned now and ignored.
395ec5ac89eSPiotr Pawel Stefaniak 		     */
396e1baf57eSPiotr Pawel Stefaniak 		    while (isspace((unsigned char)*buf_ptr)) {
397ec5ac89eSPiotr Pawel Stefaniak 			if (++buf_ptr >= buf_end)
398ec5ac89eSPiotr Pawel Stefaniak 			    fill_buffer();
399ec5ac89eSPiotr Pawel Stefaniak 			if (*buf_ptr == '\n')
400ec5ac89eSPiotr Pawel Stefaniak 			    break;
4019b50d902SRodney W. Grimes 		    }
402ec5ac89eSPiotr Pawel Stefaniak 		    goto sw_buffer;
403ec5ac89eSPiotr Pawel Stefaniak 		}
404ec5ac89eSPiotr Pawel Stefaniak 		/* FALLTHROUGH */
405dc51023cSPhilippe Charnier 	    default:		/* it is the start of a normal statement */
406ec5ac89eSPiotr Pawel Stefaniak 		{
407ec5ac89eSPiotr Pawel Stefaniak 		    int remove_newlines;
4089b50d902SRodney W. Grimes 
409ec5ac89eSPiotr Pawel Stefaniak 		    remove_newlines =
410ec5ac89eSPiotr Pawel Stefaniak 			/* "} else" */
411ec5ac89eSPiotr Pawel Stefaniak 			(type_code == sp_nparen && *token == 'e' &&
412ec5ac89eSPiotr Pawel Stefaniak 			    e_code != s_code && e_code[-1] == '}')
413ec5ac89eSPiotr Pawel Stefaniak 			/* "else if" */
414ec5ac89eSPiotr Pawel Stefaniak 			|| (type_code == sp_paren && *token == 'i' &&
4157e53aaedSPiotr Pawel Stefaniak 			    last_else && opt.else_if);
416ec5ac89eSPiotr Pawel Stefaniak 		    if (remove_newlines)
417ec5ac89eSPiotr Pawel Stefaniak 			force_nl = false;
418ec5ac89eSPiotr Pawel Stefaniak 		    if (sc_end == NULL) {	/* ignore buffering if
419ec5ac89eSPiotr Pawel Stefaniak 						 * comment wasn't saved up */
4209b50d902SRodney W. Grimes 			ps.search_brace = false;
4219b50d902SRodney W. Grimes 			goto check_type;
4229b50d902SRodney W. Grimes 		    }
423e1baf57eSPiotr Pawel Stefaniak 		    while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
424ec5ac89eSPiotr Pawel Stefaniak 			sc_end--;
425ec5ac89eSPiotr Pawel Stefaniak 		    }
4267e53aaedSPiotr Pawel Stefaniak 		    if (opt.swallow_optional_blanklines ||
427ec5ac89eSPiotr Pawel Stefaniak 			(!comment_buffered && remove_newlines)) {
428ec5ac89eSPiotr Pawel Stefaniak 			force_nl = !remove_newlines;
429ec5ac89eSPiotr Pawel Stefaniak 			while (sc_end > save_com && sc_end[-1] == '\n') {
430ec5ac89eSPiotr Pawel Stefaniak 			    sc_end--;
431ec5ac89eSPiotr Pawel Stefaniak 			}
432ec5ac89eSPiotr Pawel Stefaniak 		    }
433ec5ac89eSPiotr Pawel Stefaniak 		    if (force_nl) {	/* if we should insert a nl here, put
434ec5ac89eSPiotr Pawel Stefaniak 					 * it into the buffer */
4359b50d902SRodney W. Grimes 			force_nl = false;
436ec5ac89eSPiotr Pawel Stefaniak 			--line_no;	/* this will be re-increased when the
437ec5ac89eSPiotr Pawel Stefaniak 					 * newline is read from the buffer */
4389b50d902SRodney W. Grimes 			*sc_end++ = '\n';
4399b50d902SRodney W. Grimes 			*sc_end++ = ' ';
4407e53aaedSPiotr Pawel Stefaniak 			if (opt.verbose) /* print error msg if the line was
441ec5ac89eSPiotr Pawel Stefaniak 					 * not already broken */
4427916863dSJens Schweikhardt 			    diag2(0, "Line broken");
4439b50d902SRodney W. Grimes 		    }
4449b50d902SRodney W. Grimes 		    for (t_ptr = token; *t_ptr; ++t_ptr)
445ec5ac89eSPiotr Pawel Stefaniak 			*sc_end++ = *t_ptr;
4469b50d902SRodney W. Grimes 
4479b50d902SRodney W. Grimes 	    sw_buffer:
4489b50d902SRodney W. Grimes 		    ps.search_brace = false;	/* stop looking for start of
4499b50d902SRodney W. Grimes 						 * stmt */
4509b50d902SRodney W. Grimes 		    bp_save = buf_ptr;	/* save current input buffer */
4519b50d902SRodney W. Grimes 		    be_save = buf_end;
4529b50d902SRodney W. Grimes 		    buf_ptr = save_com;	/* fix so that subsequent calls to
4539b50d902SRodney W. Grimes 					 * lexi will take tokens out of
4549b50d902SRodney W. Grimes 					 * save_com */
4559b50d902SRodney W. Grimes 		    *sc_end++ = ' ';/* add trailing blank, just in case */
4569b50d902SRodney W. Grimes 		    buf_end = sc_end;
457c917a54bSPedro F. Giffuni 		    sc_end = NULL;
4589b50d902SRodney W. Grimes 		    break;
459ec5ac89eSPiotr Pawel Stefaniak 		}
4609b50d902SRodney W. Grimes 	    }			/* end of switch */
46163c3f226SPiotr Pawel Stefaniak 	    /*
46263c3f226SPiotr Pawel Stefaniak 	     * We must make this check, just in case there was an unexpected
46363c3f226SPiotr Pawel Stefaniak 	     * EOF.
46463c3f226SPiotr Pawel Stefaniak 	     */
46563c3f226SPiotr Pawel Stefaniak 	    if (type_code != 0) {
46663c3f226SPiotr Pawel Stefaniak 		/*
46763c3f226SPiotr Pawel Stefaniak 		 * The only intended purpose of calling lexi() below is to
46863c3f226SPiotr Pawel Stefaniak 		 * categorize the next token in order to decide whether to
46963c3f226SPiotr Pawel Stefaniak 		 * continue buffering forthcoming tokens. Once the buffering
47063c3f226SPiotr Pawel Stefaniak 		 * is over, lexi() will be called again elsewhere on all of
47163c3f226SPiotr Pawel Stefaniak 		 * the tokens - this time for normal processing.
47263c3f226SPiotr Pawel Stefaniak 		 *
47363c3f226SPiotr Pawel Stefaniak 		 * Calling it for this purpose is a bug, because lexi() also
47463c3f226SPiotr Pawel Stefaniak 		 * changes the parser state and discards leading whitespace,
47563c3f226SPiotr Pawel Stefaniak 		 * which is needed mostly for comment-related considerations.
47663c3f226SPiotr Pawel Stefaniak 		 *
47763c3f226SPiotr Pawel Stefaniak 		 * Work around the former problem by giving lexi() a copy of
47863c3f226SPiotr Pawel Stefaniak 		 * the current parser state and discard it if the call turned
47963c3f226SPiotr Pawel Stefaniak 		 * out to be just a look ahead.
48063c3f226SPiotr Pawel Stefaniak 		 *
48163c3f226SPiotr Pawel Stefaniak 		 * Work around the latter problem by copying all whitespace
48263c3f226SPiotr Pawel Stefaniak 		 * characters into the buffer so that the later lexi() call
48363c3f226SPiotr Pawel Stefaniak 		 * will read them.
48463c3f226SPiotr Pawel Stefaniak 		 */
48563c3f226SPiotr Pawel Stefaniak 		if (sc_end != NULL) {
48663c3f226SPiotr Pawel Stefaniak 		    while (*buf_ptr == ' ' || *buf_ptr == '\t') {
48763c3f226SPiotr Pawel Stefaniak 			*sc_end++ = *buf_ptr++;
48863c3f226SPiotr Pawel Stefaniak 			if (sc_end >= &save_com[sc_size]) {
48933c24642SPiotr Pawel Stefaniak 			    errx(1, "input too long");
49063c3f226SPiotr Pawel Stefaniak 			}
49163c3f226SPiotr Pawel Stefaniak 		    }
49263c3f226SPiotr Pawel Stefaniak 		    if (buf_ptr >= buf_end) {
49363c3f226SPiotr Pawel Stefaniak 			fill_buffer();
49463c3f226SPiotr Pawel Stefaniak 		    }
49563c3f226SPiotr Pawel Stefaniak 		}
49663c3f226SPiotr Pawel Stefaniak 		transient_state = ps;
49763c3f226SPiotr Pawel Stefaniak 		type_code = lexi(&transient_state);	/* read another token */
49863c3f226SPiotr Pawel Stefaniak 		if (type_code != newline && type_code != form_feed &&
49963c3f226SPiotr Pawel Stefaniak 		    type_code != comment && !transient_state.search_brace) {
50063c3f226SPiotr Pawel Stefaniak 		    ps = transient_state;
50163c3f226SPiotr Pawel Stefaniak 		}
50263c3f226SPiotr Pawel Stefaniak 	    }
5039b50d902SRodney W. Grimes 	}			/* end of while (search_brace) */
5049b50d902SRodney W. Grimes 	last_else = 0;
5059b50d902SRodney W. Grimes check_type:
5069b50d902SRodney W. Grimes 	if (type_code == 0) {	/* we got eof */
5079b50d902SRodney W. Grimes 	    if (s_lab != e_lab || s_code != e_code
5089b50d902SRodney W. Grimes 		    || s_com != e_com)	/* must dump end of line */
5099b50d902SRodney W. Grimes 		dump_line();
5109b50d902SRodney W. Grimes 	    if (ps.tos > 1)	/* check for balanced braces */
511d0054952SPhilippe Charnier 		diag2(1, "Stuff missing from end of file");
5129b50d902SRodney W. Grimes 
5137e53aaedSPiotr Pawel Stefaniak 	    if (opt.verbose) {
5149b50d902SRodney W. Grimes 		printf("There were %d output lines and %d comments\n",
5159b50d902SRodney W. Grimes 		       ps.out_lines, ps.out_coms);
5169b50d902SRodney W. Grimes 		printf("(Lines with comments)/(Lines with code): %6.3f\n",
5179b50d902SRodney W. Grimes 		       (1.0 * ps.com_lines) / code_lines);
5189b50d902SRodney W. Grimes 	    }
5199b50d902SRodney W. Grimes 	    fflush(output);
5209b50d902SRodney W. Grimes 	    exit(found_err);
5219b50d902SRodney W. Grimes 	}
5229b50d902SRodney W. Grimes 	if (
5239b50d902SRodney W. Grimes 		(type_code != comment) &&
5249b50d902SRodney W. Grimes 		(type_code != newline) &&
5259b50d902SRodney W. Grimes 		(type_code != preesc) &&
5269b50d902SRodney W. Grimes 		(type_code != form_feed)) {
5279b50d902SRodney W. Grimes 	    if (force_nl &&
5289b50d902SRodney W. Grimes 		    (type_code != semicolon) &&
5297e53aaedSPiotr Pawel Stefaniak 		    (type_code != lbrace || !opt.btype_2)) {
5309b50d902SRodney W. Grimes 		/* we should force a broken line here */
5317e53aaedSPiotr Pawel Stefaniak 		if (opt.verbose)
5327916863dSJens Schweikhardt 		    diag2(0, "Line broken");
5339b50d902SRodney W. Grimes 		dump_line();
5349b50d902SRodney W. Grimes 		ps.want_blank = false;	/* dont insert blank at line start */
5359b50d902SRodney W. Grimes 		force_nl = false;
5369b50d902SRodney W. Grimes 	    }
5379b50d902SRodney W. Grimes 	    ps.in_stmt = true;	/* turn on flag which causes an extra level of
5389b50d902SRodney W. Grimes 				 * indentation. this is turned off by a ; or
5399b50d902SRodney W. Grimes 				 * '}' */
5409b50d902SRodney W. Grimes 	    if (s_com != e_com) {	/* the turkey has embedded a comment
5419b50d902SRodney W. Grimes 					 * in a line. fix it */
5429de29bfbSPiotr Pawel Stefaniak 		int len = e_com - s_com;
5439de29bfbSPiotr Pawel Stefaniak 
5449de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_CODE(len + 3);
5459b50d902SRodney W. Grimes 		*e_code++ = ' ';
5469de29bfbSPiotr Pawel Stefaniak 		memcpy(e_code, s_com, len);
5479de29bfbSPiotr Pawel Stefaniak 		e_code += len;
5489b50d902SRodney W. Grimes 		*e_code++ = ' ';
5499b50d902SRodney W. Grimes 		*e_code = '\0';	/* null terminate code sect */
5509b50d902SRodney W. Grimes 		ps.want_blank = false;
5519b50d902SRodney W. Grimes 		e_com = s_com;
5529b50d902SRodney W. Grimes 	    }
5539b50d902SRodney W. Grimes 	}
5549b50d902SRodney W. Grimes 	else if (type_code != comment)	/* preserve force_nl thru a comment */
5559b50d902SRodney W. Grimes 	    force_nl = false;	/* cancel forced newline after newline, form
5569b50d902SRodney W. Grimes 				 * feed, etc */
5579b50d902SRodney W. Grimes 
5589b50d902SRodney W. Grimes 
5599b50d902SRodney W. Grimes 
5609b50d902SRodney W. Grimes 	/*-----------------------------------------------------*\
5619b50d902SRodney W. Grimes 	|	   do switch on type of token scanned		|
5629b50d902SRodney W. Grimes 	\*-----------------------------------------------------*/
5639de29bfbSPiotr Pawel Stefaniak 	CHECK_SIZE_CODE(3);	/* maximum number of increments of e_code
5649de29bfbSPiotr Pawel Stefaniak 				 * before the next CHECK_SIZE_CODE or
5659de29bfbSPiotr Pawel Stefaniak 				 * dump_line() is 2. After that there's the
5669de29bfbSPiotr Pawel Stefaniak 				 * final increment for the null character. */
5679b50d902SRodney W. Grimes 	switch (type_code) {	/* now, decide what to do with the token */
5689b50d902SRodney W. Grimes 
5699b50d902SRodney W. Grimes 	case form_feed:	/* found a form feed in line */
5709b50d902SRodney W. Grimes 	    ps.use_ff = true;	/* a form feed is treated much like a newline */
5719b50d902SRodney W. Grimes 	    dump_line();
5729b50d902SRodney W. Grimes 	    ps.want_blank = false;
5739b50d902SRodney W. Grimes 	    break;
5749b50d902SRodney W. Grimes 
5759b50d902SRodney W. Grimes 	case newline:
5769b50d902SRodney W. Grimes 	    if (ps.last_token != comma || ps.p_l_follow > 0
5777e53aaedSPiotr Pawel Stefaniak 		    || !opt.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
5789b50d902SRodney W. Grimes 		dump_line();
5799b50d902SRodney W. Grimes 		ps.want_blank = false;
5809b50d902SRodney W. Grimes 	    }
5819b50d902SRodney W. Grimes 	    ++line_no;		/* keep track of input line number */
5829b50d902SRodney W. Grimes 	    break;
5839b50d902SRodney W. Grimes 
5849b50d902SRodney W. Grimes 	case lparen:		/* got a '(' or '[' */
58593567e87SPiotr Pawel Stefaniak 	    /* count parens to make Healy happy */
58693567e87SPiotr Pawel Stefaniak 	    if (++ps.p_l_follow == nitems(ps.paren_indents)) {
58793567e87SPiotr Pawel Stefaniak 		diag3(0, "Reached internal limit of %d unclosed parens",
58893567e87SPiotr Pawel Stefaniak 		    nitems(ps.paren_indents));
58993567e87SPiotr Pawel Stefaniak 		ps.p_l_follow--;
59093567e87SPiotr Pawel Stefaniak 	    }
59101a206e6SPiotr Pawel Stefaniak 	    if (*token == '[')
59201a206e6SPiotr Pawel Stefaniak 		/* not a function pointer declaration or a function call */;
59301a206e6SPiotr Pawel Stefaniak 	    else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
594e95d1e3cSPiotr Pawel Stefaniak 		ps.procname[0] == '\0' && ps.paren_level == 0) {
59511c49893SPedro F. Giffuni 		/* function pointer declarations */
59611c49893SPedro F. Giffuni 		indent_declaration(dec_ind, tabs_to_var);
59711c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
5989b50d902SRodney W. Grimes 	    }
59901a206e6SPiotr Pawel Stefaniak 	    else if (ps.want_blank &&
600b06c2eb7SPiotr Pawel Stefaniak 		    ((ps.last_token != ident && ps.last_token != funcname) ||
6017e53aaedSPiotr Pawel Stefaniak 		    opt.proc_calls_space ||
602b06c2eb7SPiotr Pawel Stefaniak 		    /* offsetof (1) is never allowed a space; sizeof (2) gets
603b06c2eb7SPiotr Pawel Stefaniak 		     * one iff -bs; all other keywords (>2) always get a space
604b06c2eb7SPiotr Pawel Stefaniak 		     * before lparen */
6057e53aaedSPiotr Pawel Stefaniak 			ps.keyword + opt.Bill_Shannon > 2))
606b06c2eb7SPiotr Pawel Stefaniak 		*e_code++ = ' ';
607b06c2eb7SPiotr Pawel Stefaniak 	    ps.want_blank = false;
6089b50d902SRodney W. Grimes 	    *e_code++ = token[0];
6099d4264fbSPiotr Pawel Stefaniak 	    ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
6107e53aaedSPiotr Pawel Stefaniak 	    if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
6117e53aaedSPiotr Pawel Stefaniak 		    && ps.paren_indents[0] < 2 * opt.ind_size)
6127e53aaedSPiotr Pawel Stefaniak 		ps.paren_indents[0] = 2 * opt.ind_size;
6139b50d902SRodney W. Grimes 	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
6149b50d902SRodney W. Grimes 		/*
6159b50d902SRodney W. Grimes 		 * this is a kluge to make sure that declarations will be
6169b50d902SRodney W. Grimes 		 * aligned right if proc decl has an explicit type on it, i.e.
6179b50d902SRodney W. Grimes 		 * "int a(x) {..."
6189b50d902SRodney W. Grimes 		 */
6199b50d902SRodney W. Grimes 		parse(semicolon);	/* I said this was a kluge... */
6209b50d902SRodney W. Grimes 		ps.in_or_st = false;	/* turn off flag for structure decl or
6219b50d902SRodney W. Grimes 					 * initialization */
6229b50d902SRodney W. Grimes 	    }
623b4939677SPedro F. Giffuni 	    /* parenthesized type following sizeof or offsetof is not a cast */
624b4939677SPedro F. Giffuni 	    if (ps.keyword == 1 || ps.keyword == 2)
625b4939677SPedro F. Giffuni 		ps.not_cast_mask |= 1 << ps.p_l_follow;
6269b50d902SRodney W. Grimes 	    break;
6279b50d902SRodney W. Grimes 
6289b50d902SRodney W. Grimes 	case rparen:		/* got a ')' or ']' */
629b4939677SPedro F. Giffuni 	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
6309b50d902SRodney W. Grimes 		ps.last_u_d = true;
6319b50d902SRodney W. Grimes 		ps.cast_mask &= (1 << ps.p_l_follow) - 1;
6327e53aaedSPiotr Pawel Stefaniak 		ps.want_blank = opt.space_after_cast;
633a5e1cac0SDavid E. O'Brien 	    } else
634a5e1cac0SDavid E. O'Brien 		ps.want_blank = true;
635b4939677SPedro F. Giffuni 	    ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
6369b50d902SRodney W. Grimes 	    if (--ps.p_l_follow < 0) {
6379b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
6387916863dSJens Schweikhardt 		diag3(0, "Extra %c", *token);
6399b50d902SRodney W. Grimes 	    }
6409b50d902SRodney W. Grimes 	    if (e_code == s_code)	/* if the paren starts the line */
6419b50d902SRodney W. Grimes 		ps.paren_level = ps.p_l_follow;	/* then indent it */
6429b50d902SRodney W. Grimes 
6439b50d902SRodney W. Grimes 	    *e_code++ = token[0];
6449b50d902SRodney W. Grimes 
6459b50d902SRodney W. Grimes 	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
6469b50d902SRodney W. Grimes 							 * (...), or some such */
6479b50d902SRodney W. Grimes 		sp_sw = false;
6489b50d902SRodney W. Grimes 		force_nl = true;/* must force newline after if */
6499b50d902SRodney W. Grimes 		ps.last_u_d = true;	/* inform lexi that a following
6509b50d902SRodney W. Grimes 					 * operator is unary */
6519b50d902SRodney W. Grimes 		ps.in_stmt = false;	/* dont use stmt continuation
6529b50d902SRodney W. Grimes 					 * indentation */
6539b50d902SRodney W. Grimes 
6549b50d902SRodney W. Grimes 		parse(hd_type);	/* let parser worry about if, or whatever */
6559b50d902SRodney W. Grimes 	    }
6567e53aaedSPiotr Pawel Stefaniak 	    ps.search_brace = opt.btype_2; /* this should ensure that
6577e53aaedSPiotr Pawel Stefaniak 					 * constructs such as main(){...}
6587e53aaedSPiotr Pawel Stefaniak 					 * and int[]{...} have their braces
6597e53aaedSPiotr Pawel Stefaniak 					 * put in the right place */
6609b50d902SRodney W. Grimes 	    break;
6619b50d902SRodney W. Grimes 
6629b50d902SRodney W. Grimes 	case unary_op:		/* this could be any unary operation */
663e95d1e3cSPiotr Pawel Stefaniak 	    if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
664e95d1e3cSPiotr Pawel Stefaniak 		ps.procname[0] == '\0' && ps.paren_level == 0) {
66511c49893SPedro F. Giffuni 		/* pointer declarations */
6661479f36dSPiotr Pawel Stefaniak 
6671479f36dSPiotr Pawel Stefaniak 		/*
6681479f36dSPiotr Pawel Stefaniak 		 * if this is a unary op in a declaration, we should indent
6691479f36dSPiotr Pawel Stefaniak 		 * this token
6701479f36dSPiotr Pawel Stefaniak 		 */
67111c49893SPedro F. Giffuni 		for (i = 0; token[i]; ++i)
67211c49893SPedro F. Giffuni 		    /* find length of token */;
67311c49893SPedro F. Giffuni 		indent_declaration(dec_ind - i, tabs_to_var);
67411c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
67511c49893SPedro F. Giffuni 	    }
67611c49893SPedro F. Giffuni 	    else if (ps.want_blank)
67711c49893SPedro F. Giffuni 		*e_code++ = ' ';
6789b50d902SRodney W. Grimes 
6799de29bfbSPiotr Pawel Stefaniak 	    {
6809de29bfbSPiotr Pawel Stefaniak 		int len = e_token - s_token;
6819de29bfbSPiotr Pawel Stefaniak 
6829de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_CODE(len);
6839de29bfbSPiotr Pawel Stefaniak 		memcpy(e_code, token, len);
6849de29bfbSPiotr Pawel Stefaniak 		e_code += len;
6859b50d902SRodney W. Grimes 	    }
6869b50d902SRodney W. Grimes 	    ps.want_blank = false;
6879b50d902SRodney W. Grimes 	    break;
6889b50d902SRodney W. Grimes 
6899b50d902SRodney W. Grimes 	case binary_op:	/* any binary operation */
6909de29bfbSPiotr Pawel Stefaniak 	    {
6919de29bfbSPiotr Pawel Stefaniak 		int len = e_token - s_token;
6929de29bfbSPiotr Pawel Stefaniak 
6939de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_CODE(len + 1);
6949b50d902SRodney W. Grimes 		if (ps.want_blank)
6959b50d902SRodney W. Grimes 		    *e_code++ = ' ';
6969de29bfbSPiotr Pawel Stefaniak 		memcpy(e_code, token, len);
6979de29bfbSPiotr Pawel Stefaniak 		e_code += len;
6989b50d902SRodney W. Grimes 	    }
6999b50d902SRodney W. Grimes 	    ps.want_blank = true;
7009b50d902SRodney W. Grimes 	    break;
7019b50d902SRodney W. Grimes 
7029b50d902SRodney W. Grimes 	case postop:		/* got a trailing ++ or -- */
7039b50d902SRodney W. Grimes 	    *e_code++ = token[0];
7049b50d902SRodney W. Grimes 	    *e_code++ = token[1];
7059b50d902SRodney W. Grimes 	    ps.want_blank = true;
7069b50d902SRodney W. Grimes 	    break;
7079b50d902SRodney W. Grimes 
7089b50d902SRodney W. Grimes 	case question:		/* got a ? */
7099b50d902SRodney W. Grimes 	    squest++;		/* this will be used when a later colon
7109b50d902SRodney W. Grimes 				 * appears so we can distinguish the
7119b50d902SRodney W. Grimes 				 * <c>?<n>:<n> construct */
7129b50d902SRodney W. Grimes 	    if (ps.want_blank)
7139b50d902SRodney W. Grimes 		*e_code++ = ' ';
7149b50d902SRodney W. Grimes 	    *e_code++ = '?';
7159b50d902SRodney W. Grimes 	    ps.want_blank = true;
7169b50d902SRodney W. Grimes 	    break;
7179b50d902SRodney W. Grimes 
7189b50d902SRodney W. Grimes 	case casestmt:		/* got word 'case' or 'default' */
7199b50d902SRodney W. Grimes 	    scase = true;	/* so we can process the later colon properly */
7209b50d902SRodney W. Grimes 	    goto copy_id;
7219b50d902SRodney W. Grimes 
7229b50d902SRodney W. Grimes 	case colon:		/* got a ':' */
7239b50d902SRodney W. Grimes 	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
7249b50d902SRodney W. Grimes 		--squest;
7259b50d902SRodney W. Grimes 		if (ps.want_blank)
7269b50d902SRodney W. Grimes 		    *e_code++ = ' ';
7279b50d902SRodney W. Grimes 		*e_code++ = ':';
7289b50d902SRodney W. Grimes 		ps.want_blank = true;
7299b50d902SRodney W. Grimes 		break;
7309b50d902SRodney W. Grimes 	    }
7316953f51aSAndriy Gapon 	    if (ps.in_or_st) {
7329b50d902SRodney W. Grimes 		*e_code++ = ':';
7339b50d902SRodney W. Grimes 		ps.want_blank = false;
7349b50d902SRodney W. Grimes 		break;
7359b50d902SRodney W. Grimes 	    }
7369b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* seeing a label does not imply we are in a
7379b50d902SRodney W. Grimes 				 * stmt */
7389de29bfbSPiotr Pawel Stefaniak 	    /*
7399de29bfbSPiotr Pawel Stefaniak 	     * turn everything so far into a label
7409de29bfbSPiotr Pawel Stefaniak 	     */
7419de29bfbSPiotr Pawel Stefaniak 	    {
7429de29bfbSPiotr Pawel Stefaniak 		int len = e_code - s_code;
7439de29bfbSPiotr Pawel Stefaniak 
7449de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_LAB(len + 3);
7459de29bfbSPiotr Pawel Stefaniak 		memcpy(e_lab, s_code, len);
7469de29bfbSPiotr Pawel Stefaniak 		e_lab += len;
7479b50d902SRodney W. Grimes 		*e_lab++ = ':';
7489b50d902SRodney W. Grimes 		*e_lab = '\0';
7499de29bfbSPiotr Pawel Stefaniak 		e_code = s_code;
7509de29bfbSPiotr Pawel Stefaniak 	    }
7519b50d902SRodney W. Grimes 	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by
7529b50d902SRodney W. Grimes 						 * dump_line to decide how to
7539b50d902SRodney W. Grimes 						 * indent the label. force_nl
7549b50d902SRodney W. Grimes 						 * will force a case n: to be
7559b50d902SRodney W. Grimes 						 * on a line by itself */
7569b50d902SRodney W. Grimes 	    scase = false;
7579b50d902SRodney W. Grimes 	    ps.want_blank = false;
7589b50d902SRodney W. Grimes 	    break;
7599b50d902SRodney W. Grimes 
7609b50d902SRodney W. Grimes 	case semicolon:	/* got a ';' */
7619b4009b4SPedro F. Giffuni 	    if (ps.dec_nest == 0)
7629b4009b4SPedro F. Giffuni 		ps.in_or_st = false;/* we are not in an initialization or
7639b4009b4SPedro F. Giffuni 				     * structure declaration */
7649d5abbddSJens Schweikhardt 	    scase = false;	/* these will only need resetting in an error */
7659b50d902SRodney W. Grimes 	    squest = 0;
766a3abcad0SPiotr Pawel Stefaniak 	    if (ps.last_token == rparen)
7679b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
7689b50d902SRodney W. Grimes 	    ps.cast_mask = 0;
769b4939677SPedro F. Giffuni 	    ps.not_cast_mask = 0;
7709b50d902SRodney W. Grimes 	    ps.block_init = 0;
7719b50d902SRodney W. Grimes 	    ps.block_init_level = 0;
7729b50d902SRodney W. Grimes 	    ps.just_saw_decl--;
7739b50d902SRodney W. Grimes 
77411c49893SPedro F. Giffuni 	    if (ps.in_decl && s_code == e_code && !ps.block_init &&
7759d4264fbSPiotr Pawel Stefaniak 		!ps.dumped_decl_indent && ps.paren_level == 0) {
77611c49893SPedro F. Giffuni 		/* indent stray semicolons in declarations */
77711c49893SPedro F. Giffuni 		indent_declaration(dec_ind - 1, tabs_to_var);
77811c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
7799b50d902SRodney W. Grimes 	    }
7809b50d902SRodney W. Grimes 
7819b50d902SRodney W. Grimes 	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
7829b50d902SRodney W. Grimes 						 * structure declaration, we
7839b50d902SRodney W. Grimes 						 * arent any more */
7849b50d902SRodney W. Grimes 
7859b50d902SRodney W. Grimes 	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
7869b50d902SRodney W. Grimes 
7879b50d902SRodney W. Grimes 		/*
7889b50d902SRodney W. Grimes 		 * This should be true iff there were unbalanced parens in the
7899b50d902SRodney W. Grimes 		 * stmt.  It is a bit complicated, because the semicolon might
7909b50d902SRodney W. Grimes 		 * be in a for stmt
7919b50d902SRodney W. Grimes 		 */
7927916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
7939b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
7949d5abbddSJens Schweikhardt 		if (sp_sw) {	/* this is a check for an if, while, etc. with
7959b50d902SRodney W. Grimes 				 * unbalanced parens */
7969b50d902SRodney W. Grimes 		    sp_sw = false;
7979b50d902SRodney W. Grimes 		    parse(hd_type);	/* dont lose the if, or whatever */
7989b50d902SRodney W. Grimes 		}
7999b50d902SRodney W. Grimes 	    }
8009b50d902SRodney W. Grimes 	    *e_code++ = ';';
8019b50d902SRodney W. Grimes 	    ps.want_blank = true;
8029b50d902SRodney W. Grimes 	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
8039b50d902SRodney W. Grimes 						 * middle of a stmt */
8049b50d902SRodney W. Grimes 
8059b50d902SRodney W. Grimes 	    if (!sp_sw) {	/* if not if for (;;) */
8069b50d902SRodney W. Grimes 		parse(semicolon);	/* let parser know about end of stmt */
8079d5abbddSJens Schweikhardt 		force_nl = true;/* force newline after an end of stmt */
8089b50d902SRodney W. Grimes 	    }
8099b50d902SRodney W. Grimes 	    break;
8109b50d902SRodney W. Grimes 
8119b50d902SRodney W. Grimes 	case lbrace:		/* got a '{' */
8129b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* dont indent the {} */
8139b50d902SRodney W. Grimes 	    if (!ps.block_init)
8149b50d902SRodney W. Grimes 		force_nl = true;/* force other stuff on same line as '{' onto
8159b50d902SRodney W. Grimes 				 * new line */
8169b50d902SRodney W. Grimes 	    else if (ps.block_init_level <= 0)
8179b50d902SRodney W. Grimes 		ps.block_init_level = 1;
8189b50d902SRodney W. Grimes 	    else
8199b50d902SRodney W. Grimes 		ps.block_init_level++;
8209b50d902SRodney W. Grimes 
8219b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {
8227e53aaedSPiotr Pawel Stefaniak 		if (!opt.btype_2) {
8239b50d902SRodney W. Grimes 		    dump_line();
8249b50d902SRodney W. Grimes 		    ps.want_blank = false;
8259b50d902SRodney W. Grimes 		}
8269b50d902SRodney W. Grimes 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
8279b50d902SRodney W. Grimes 		    ps.i_l_follow = 0;
8287e53aaedSPiotr Pawel Stefaniak 		    if (opt.function_brace_split) { /* dump the line prior
8297e53aaedSPiotr Pawel Stefaniak 				 * to the brace ... */
8309b50d902SRodney W. Grimes 			dump_line();
8319b50d902SRodney W. Grimes 			ps.want_blank = false;
832e3625e9cSJens Schweikhardt 		    } else	/* add a space between the decl and brace */
833e3625e9cSJens Schweikhardt 			ps.want_blank = true;
8349b50d902SRodney W. Grimes 		}
8359b50d902SRodney W. Grimes 	    }
8369b50d902SRodney W. Grimes 	    if (ps.in_parameter_declaration)
8379b50d902SRodney W. Grimes 		prefix_blankline_requested = 0;
8389b50d902SRodney W. Grimes 
839d7d97eb0SJeroen Ruigrok van der Werven 	    if (ps.p_l_follow > 0) {	/* check for preceding unbalanced
8409b50d902SRodney W. Grimes 					 * parens */
8417916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
8429b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
8439b50d902SRodney W. Grimes 		if (sp_sw) {	/* check for unclosed if, for, etc. */
8449b50d902SRodney W. Grimes 		    sp_sw = false;
8459b50d902SRodney W. Grimes 		    parse(hd_type);
8469b50d902SRodney W. Grimes 		    ps.ind_level = ps.i_l_follow;
8479b50d902SRodney W. Grimes 		}
8489b50d902SRodney W. Grimes 	    }
8499b50d902SRodney W. Grimes 	    if (s_code == e_code)
8509b50d902SRodney W. Grimes 		ps.ind_stmt = false;	/* dont put extra indentation on line
8519b50d902SRodney W. Grimes 					 * with '{' */
8529b50d902SRodney W. Grimes 	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
8539b50d902SRodney W. Grimes 						 * declaration or an init */
854370d0cf9SPiotr Pawel Stefaniak 		di_stack[ps.dec_nest] = dec_ind;
855370d0cf9SPiotr Pawel Stefaniak 		if (++ps.dec_nest == nitems(di_stack)) {
856370d0cf9SPiotr Pawel Stefaniak 		    diag3(0, "Reached internal limit of %d struct levels",
857370d0cf9SPiotr Pawel Stefaniak 			nitems(di_stack));
858370d0cf9SPiotr Pawel Stefaniak 		    ps.dec_nest--;
859370d0cf9SPiotr Pawel Stefaniak 		}
8609b50d902SRodney W. Grimes 		/* ?		dec_ind = 0; */
8619b50d902SRodney W. Grimes 	    }
8629b50d902SRodney W. Grimes 	    else {
863487ac9acSUlrich Spörlein 		ps.decl_on_line = false;	/* we can't be in the middle of
864487ac9acSUlrich Spörlein 						 * a declaration, so don't do
8659b50d902SRodney W. Grimes 						 * special indentation of
8669b50d902SRodney W. Grimes 						 * comments */
8677e53aaedSPiotr Pawel Stefaniak 		if (opt.blanklines_after_declarations_at_proctop
8689b50d902SRodney W. Grimes 			&& ps.in_parameter_declaration)
8699b50d902SRodney W. Grimes 		    postfix_blankline_requested = 1;
8709b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
871a3abcad0SPiotr Pawel Stefaniak 		ps.in_decl = false;
8729b50d902SRodney W. Grimes 	    }
8739b50d902SRodney W. Grimes 	    dec_ind = 0;
8749b50d902SRodney W. Grimes 	    parse(lbrace);	/* let parser know about this */
8759b50d902SRodney W. Grimes 	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at
8769b50d902SRodney W. Grimes 				 * start of line */
8779b50d902SRodney W. Grimes 		*e_code++ = ' ';
8789b50d902SRodney W. Grimes 	    ps.want_blank = false;
8799b50d902SRodney W. Grimes 	    *e_code++ = '{';
8809b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
8819b50d902SRodney W. Grimes 	    break;
8829b50d902SRodney W. Grimes 
8839b50d902SRodney W. Grimes 	case rbrace:		/* got a '}' */
8849b50d902SRodney W. Grimes 	    if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
8859b50d902SRodney W. Grimes 								 * omitted in
8869b50d902SRodney W. Grimes 								 * declarations */
8879b50d902SRodney W. Grimes 		parse(semicolon);
8889b50d902SRodney W. Grimes 	    if (ps.p_l_follow) {/* check for unclosed if, for, else. */
8897916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
8909b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
8919b50d902SRodney W. Grimes 		sp_sw = false;
8929b50d902SRodney W. Grimes 	    }
8939b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
8949b50d902SRodney W. Grimes 	    ps.block_init_level--;
8959b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
8969b50d902SRodney W. Grimes 							 * line */
8977e53aaedSPiotr Pawel Stefaniak 		if (opt.verbose)
8987916863dSJens Schweikhardt 		    diag2(0, "Line broken");
8999b50d902SRodney W. Grimes 		dump_line();
9009b50d902SRodney W. Grimes 	    }
9019b50d902SRodney W. Grimes 	    *e_code++ = '}';
9029b50d902SRodney W. Grimes 	    ps.want_blank = true;
9039b50d902SRodney W. Grimes 	    ps.in_stmt = ps.ind_stmt = false;
9049b50d902SRodney W. Grimes 	    if (ps.dec_nest > 0) {	/* we are in multi-level structure
9059b50d902SRodney W. Grimes 					 * declaration */
9069b50d902SRodney W. Grimes 		dec_ind = di_stack[--ps.dec_nest];
9079b50d902SRodney W. Grimes 		if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
9089b50d902SRodney W. Grimes 		    ps.just_saw_decl = 2;
9099b50d902SRodney W. Grimes 		ps.in_decl = true;
9109b50d902SRodney W. Grimes 	    }
9119b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
9129b50d902SRodney W. Grimes 	    parse(rbrace);	/* let parser know about this */
9137e53aaedSPiotr Pawel Stefaniak 	    ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == ifhead
9149b50d902SRodney W. Grimes 		&& ps.il[ps.tos] >= ps.ind_level;
9157e53aaedSPiotr Pawel Stefaniak 	    if (ps.tos <= 1 && opt.blanklines_after_procs && ps.dec_nest <= 0)
9169b50d902SRodney W. Grimes 		postfix_blankline_requested = 1;
9179b50d902SRodney W. Grimes 	    break;
9189b50d902SRodney W. Grimes 
9199b50d902SRodney W. Grimes 	case swstmt:		/* got keyword "switch" */
9209b50d902SRodney W. Grimes 	    sp_sw = true;
9219b50d902SRodney W. Grimes 	    hd_type = swstmt;	/* keep this for when we have seen the
9229b50d902SRodney W. Grimes 				 * expression */
9239b50d902SRodney W. Grimes 	    goto copy_id;	/* go move the token into buffer */
9249b50d902SRodney W. Grimes 
9259b50d902SRodney W. Grimes 	case sp_paren:		/* token is if, while, for */
9269b50d902SRodney W. Grimes 	    sp_sw = true;	/* the interesting stuff is done after the
9279b50d902SRodney W. Grimes 				 * expression is scanned */
9289b50d902SRodney W. Grimes 	    hd_type = (*token == 'i' ? ifstmt :
9299b50d902SRodney W. Grimes 		       (*token == 'w' ? whilestmt : forstmt));
9309b50d902SRodney W. Grimes 
9319b50d902SRodney W. Grimes 	    /*
9329b50d902SRodney W. Grimes 	     * remember the type of header for later use by parser
9339b50d902SRodney W. Grimes 	     */
9349b50d902SRodney W. Grimes 	    goto copy_id;	/* copy the token into line */
9359b50d902SRodney W. Grimes 
9369b50d902SRodney W. Grimes 	case sp_nparen:	/* got else, do */
9379b50d902SRodney W. Grimes 	    ps.in_stmt = false;
9389b50d902SRodney W. Grimes 	    if (*token == 'e') {
9397e53aaedSPiotr Pawel Stefaniak 		if (e_code != s_code && (!opt.cuddle_else || e_code[-1] != '}')) {
9407e53aaedSPiotr Pawel Stefaniak 		    if (opt.verbose)
9417916863dSJens Schweikhardt 			diag2(0, "Line broken");
9429b50d902SRodney W. Grimes 		    dump_line();/* make sure this starts a line */
9439b50d902SRodney W. Grimes 		    ps.want_blank = false;
9449b50d902SRodney W. Grimes 		}
9459b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
9469b50d902SRodney W. Grimes 		last_else = 1;
9479b50d902SRodney W. Grimes 		parse(elselit);
9489b50d902SRodney W. Grimes 	    }
9499b50d902SRodney W. Grimes 	    else {
9509b50d902SRodney W. Grimes 		if (e_code != s_code) {	/* make sure this starts a line */
9517e53aaedSPiotr Pawel Stefaniak 		    if (opt.verbose)
9527916863dSJens Schweikhardt 			diag2(0, "Line broken");
9539b50d902SRodney W. Grimes 		    dump_line();
9549b50d902SRodney W. Grimes 		    ps.want_blank = false;
9559b50d902SRodney W. Grimes 		}
9569b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
9579b50d902SRodney W. Grimes 		last_else = 0;
9589b50d902SRodney W. Grimes 		parse(dolit);
9599b50d902SRodney W. Grimes 	    }
9609b50d902SRodney W. Grimes 	    goto copy_id;	/* move the token into line */
9619b50d902SRodney W. Grimes 
9623bbaa755SPiotr Pawel Stefaniak 	case type_def:
963f171328eSPedro F. Giffuni 	case storage:
964f171328eSPedro F. Giffuni 	    prefix_blankline_requested = 0;
965f171328eSPedro F. Giffuni 	    goto copy_id;
966f171328eSPedro F. Giffuni 
967f9287a9dSPiotr Pawel Stefaniak 	case structure:
968f9287a9dSPiotr Pawel Stefaniak 	    if (ps.p_l_follow > 0)
969f9287a9dSPiotr Pawel Stefaniak 		goto copy_id;
970f171328eSPedro F. Giffuni 	case decl:		/* we have a declaration type (int, etc.) */
9719b50d902SRodney W. Grimes 	    parse(decl);	/* let parser worry about indentation */
9729b50d902SRodney W. Grimes 	    if (ps.last_token == rparen && ps.tos <= 1) {
9739b50d902SRodney W. Grimes 		if (s_code != e_code) {
9749b50d902SRodney W. Grimes 		    dump_line();
9759b50d902SRodney W. Grimes 		    ps.want_blank = 0;
9769b50d902SRodney W. Grimes 		}
9779b50d902SRodney W. Grimes 	    }
9787e53aaedSPiotr Pawel Stefaniak 	    if (ps.in_parameter_declaration && opt.indent_parameters && ps.dec_nest == 0) {
9799b50d902SRodney W. Grimes 		ps.ind_level = ps.i_l_follow = 1;
9809b50d902SRodney W. Grimes 		ps.ind_stmt = 0;
9819b50d902SRodney W. Grimes 	    }
9829b50d902SRodney W. Grimes 	    ps.in_or_st = true;	/* this might be a structure or initialization
9839b50d902SRodney W. Grimes 				 * declaration */
9843bbaa755SPiotr Pawel Stefaniak 	    ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
9859b50d902SRodney W. Grimes 	    if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
9869b50d902SRodney W. Grimes 		ps.just_saw_decl = 2;
9879b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
9889b50d902SRodney W. Grimes 	    for (i = 0; token[i++];);	/* get length of token */
9899b50d902SRodney W. Grimes 
99088ce0e7fSBruce Evans 	    if (ps.ind_level == 0 || ps.dec_nest > 0) {
99188ce0e7fSBruce Evans 		/* global variable or struct member in local variable */
9927e53aaedSPiotr Pawel Stefaniak 		dec_ind = opt.decl_indent > 0 ? opt.decl_indent : i;
9937e53aaedSPiotr Pawel Stefaniak 		tabs_to_var = (opt.use_tabs ? opt.decl_indent > 0 : 0);
99488ce0e7fSBruce Evans 	    } else {
99588ce0e7fSBruce Evans 		/* local variable */
9967e53aaedSPiotr Pawel Stefaniak 		dec_ind = opt.local_decl_indent > 0 ? opt.local_decl_indent : i;
9977e53aaedSPiotr Pawel Stefaniak 		tabs_to_var = (opt.use_tabs ? opt.local_decl_indent > 0 : 0);
99888ce0e7fSBruce Evans 	    }
9999b50d902SRodney W. Grimes 	    goto copy_id;
10009b50d902SRodney W. Grimes 
1001a3abcad0SPiotr Pawel Stefaniak 	case funcname:
10029b50d902SRodney W. Grimes 	case ident:		/* got an identifier or constant */
10039522d0b0SPiotr Pawel Stefaniak 	    if (ps.in_decl) {
10049522d0b0SPiotr Pawel Stefaniak 		if (type_code == funcname) {
10059522d0b0SPiotr Pawel Stefaniak 		    ps.in_decl = false;
10067e53aaedSPiotr Pawel Stefaniak 		    if (opt.procnames_start_line && s_code != e_code) {
10079522d0b0SPiotr Pawel Stefaniak 			*e_code = '\0';
10089522d0b0SPiotr Pawel Stefaniak 			dump_line();
10099522d0b0SPiotr Pawel Stefaniak 		    }
10109522d0b0SPiotr Pawel Stefaniak 		    else if (ps.want_blank) {
10119522d0b0SPiotr Pawel Stefaniak 			*e_code++ = ' ';
10129522d0b0SPiotr Pawel Stefaniak 		    }
10139522d0b0SPiotr Pawel Stefaniak 		    ps.want_blank = false;
10149522d0b0SPiotr Pawel Stefaniak 		}
10159522d0b0SPiotr Pawel Stefaniak 		else if (!ps.block_init && !ps.dumped_decl_indent &&
10169522d0b0SPiotr Pawel Stefaniak 		    ps.paren_level == 0) { /* if we are in a declaration, we
10179522d0b0SPiotr Pawel Stefaniak 					    * must indent identifier */
101811c49893SPedro F. Giffuni 		    indent_declaration(dec_ind, tabs_to_var);
101911c49893SPedro F. Giffuni 		    ps.dumped_decl_indent = true;
1020e024a090SBruce Evans 		    ps.want_blank = false;
10219b50d902SRodney W. Grimes 		}
10229b50d902SRodney W. Grimes 	    }
10239b50d902SRodney W. Grimes 	    else if (sp_sw && ps.p_l_follow == 0) {
10249b50d902SRodney W. Grimes 		sp_sw = false;
10259b50d902SRodney W. Grimes 		force_nl = true;
10269b50d902SRodney W. Grimes 		ps.last_u_d = true;
10279b50d902SRodney W. Grimes 		ps.in_stmt = false;
10289b50d902SRodney W. Grimes 		parse(hd_type);
10299b50d902SRodney W. Grimes 	    }
10309b50d902SRodney W. Grimes     copy_id:
10319de29bfbSPiotr Pawel Stefaniak 	    {
10329de29bfbSPiotr Pawel Stefaniak 		int len = e_token - s_token;
10339de29bfbSPiotr Pawel Stefaniak 
10349de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_CODE(len + 1);
10359b50d902SRodney W. Grimes 		if (ps.want_blank)
10369b50d902SRodney W. Grimes 		    *e_code++ = ' ';
10379de29bfbSPiotr Pawel Stefaniak 		memcpy(e_code, s_token, len);
10389de29bfbSPiotr Pawel Stefaniak 		e_code += len;
10399b50d902SRodney W. Grimes 	    }
1040a3abcad0SPiotr Pawel Stefaniak 	    if (type_code != funcname)
10419b50d902SRodney W. Grimes 		ps.want_blank = true;
10429b50d902SRodney W. Grimes 	    break;
10439b50d902SRodney W. Grimes 
1044350fcdd5SPedro F. Giffuni 	case strpfx:
10459de29bfbSPiotr Pawel Stefaniak 	    {
10469de29bfbSPiotr Pawel Stefaniak 		int len = e_token - s_token;
10479de29bfbSPiotr Pawel Stefaniak 
10489de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_CODE(len + 1);
1049350fcdd5SPedro F. Giffuni 		if (ps.want_blank)
1050350fcdd5SPedro F. Giffuni 		    *e_code++ = ' ';
10519de29bfbSPiotr Pawel Stefaniak 		memcpy(e_code, token, len);
10529de29bfbSPiotr Pawel Stefaniak 		e_code += len;
1053350fcdd5SPedro F. Giffuni 	    }
1054350fcdd5SPedro F. Giffuni 	    ps.want_blank = false;
1055350fcdd5SPedro F. Giffuni 	    break;
1056350fcdd5SPedro F. Giffuni 
10579b50d902SRodney W. Grimes 	case period:		/* treat a period kind of like a binary
10589b50d902SRodney W. Grimes 				 * operation */
10599b50d902SRodney W. Grimes 	    *e_code++ = '.';	/* move the period into line */
10609b50d902SRodney W. Grimes 	    ps.want_blank = false;	/* dont put a blank after a period */
10619b50d902SRodney W. Grimes 	    break;
10629b50d902SRodney W. Grimes 
10639b50d902SRodney W. Grimes 	case comma:
10649b50d902SRodney W. Grimes 	    ps.want_blank = (s_code != e_code);	/* only put blank after comma
10659b50d902SRodney W. Grimes 						 * if comma does not start the
10669b50d902SRodney W. Grimes 						 * line */
1067e95d1e3cSPiotr Pawel Stefaniak 	    if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
10689d4264fbSPiotr Pawel Stefaniak 		!ps.dumped_decl_indent && ps.paren_level == 0) {
106911c49893SPedro F. Giffuni 		/* indent leading commas and not the actual identifiers */
107011c49893SPedro F. Giffuni 		indent_declaration(dec_ind - 1, tabs_to_var);
107111c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
10729b50d902SRodney W. Grimes 	    }
10739b50d902SRodney W. Grimes 	    *e_code++ = ',';
10749b50d902SRodney W. Grimes 	    if (ps.p_l_follow == 0) {
10759b50d902SRodney W. Grimes 		if (ps.block_init_level <= 0)
10769b50d902SRodney W. Grimes 		    ps.block_init = 0;
10777e53aaedSPiotr Pawel Stefaniak 		if (break_comma && (!opt.leave_comma ||
10785f35ea69SPiotr Pawel Stefaniak 		    count_spaces_until(compute_code_target(), s_code, e_code) >
10797e53aaedSPiotr Pawel Stefaniak 		    opt.max_col - opt.tabsize))
10809b50d902SRodney W. Grimes 		    force_nl = true;
10819b50d902SRodney W. Grimes 	    }
10829b50d902SRodney W. Grimes 	    break;
10839b50d902SRodney W. Grimes 
10849b50d902SRodney W. Grimes 	case preesc:		/* got the character '#' */
10859b50d902SRodney W. Grimes 	    if ((s_com != e_com) ||
10869b50d902SRodney W. Grimes 		    (s_lab != e_lab) ||
10879b50d902SRodney W. Grimes 		    (s_code != e_code))
10889b50d902SRodney W. Grimes 		dump_line();
10899de29bfbSPiotr Pawel Stefaniak 	    CHECK_SIZE_LAB(1);
10909b50d902SRodney W. Grimes 	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
10919b50d902SRodney W. Grimes 	    {
10929b50d902SRodney W. Grimes 		int         in_comment = 0;
10939b50d902SRodney W. Grimes 		int         com_start = 0;
10949b50d902SRodney W. Grimes 		char        quote = 0;
10959b50d902SRodney W. Grimes 		int         com_end = 0;
10969b50d902SRodney W. Grimes 
10979b50d902SRodney W. Grimes 		while (*buf_ptr == ' ' || *buf_ptr == '\t') {
10989b50d902SRodney W. Grimes 		    buf_ptr++;
10999b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
11009b50d902SRodney W. Grimes 			fill_buffer();
11019b50d902SRodney W. Grimes 		}
110252608c9fSDavid E. O'Brien 		while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
11039de29bfbSPiotr Pawel Stefaniak 		    CHECK_SIZE_LAB(2);
11049b50d902SRodney W. Grimes 		    *e_lab = *buf_ptr++;
11059b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
11069b50d902SRodney W. Grimes 			fill_buffer();
11079b50d902SRodney W. Grimes 		    switch (*e_lab++) {
11089b50d902SRodney W. Grimes 		    case BACKSLASH:
11099b50d902SRodney W. Grimes 			if (!in_comment) {
11109b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11119b50d902SRodney W. Grimes 			    if (buf_ptr >= buf_end)
11129b50d902SRodney W. Grimes 				fill_buffer();
11139b50d902SRodney W. Grimes 			}
11149b50d902SRodney W. Grimes 			break;
11159b50d902SRodney W. Grimes 		    case '/':
11169b50d902SRodney W. Grimes 			if (*buf_ptr == '*' && !in_comment && !quote) {
11179b50d902SRodney W. Grimes 			    in_comment = 1;
11189b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11199b50d902SRodney W. Grimes 			    com_start = e_lab - s_lab - 2;
11209b50d902SRodney W. Grimes 			}
11219b50d902SRodney W. Grimes 			break;
11229b50d902SRodney W. Grimes 		    case '"':
11239b50d902SRodney W. Grimes 			if (quote == '"')
11249b50d902SRodney W. Grimes 			    quote = 0;
11259b50d902SRodney W. Grimes 			break;
11269b50d902SRodney W. Grimes 		    case '\'':
11279b50d902SRodney W. Grimes 			if (quote == '\'')
11289b50d902SRodney W. Grimes 			    quote = 0;
11299b50d902SRodney W. Grimes 			break;
11309b50d902SRodney W. Grimes 		    case '*':
11319b50d902SRodney W. Grimes 			if (*buf_ptr == '/' && in_comment) {
11329b50d902SRodney W. Grimes 			    in_comment = 0;
11339b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11349b50d902SRodney W. Grimes 			    com_end = e_lab - s_lab;
11359b50d902SRodney W. Grimes 			}
11369b50d902SRodney W. Grimes 			break;
11379b50d902SRodney W. Grimes 		    }
11389b50d902SRodney W. Grimes 		}
11399b50d902SRodney W. Grimes 
11409b50d902SRodney W. Grimes 		while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
11419b50d902SRodney W. Grimes 		    e_lab--;
1142c917a54bSPedro F. Giffuni 		if (e_lab - s_lab == com_end && bp_save == NULL) {
1143e643b783SPedro F. Giffuni 		    /* comment on preprocessor line */
11443c51c3cfSPiotr Pawel Stefaniak 		    if (sc_end == NULL) {	/* if this is the first comment,
11453c51c3cfSPiotr Pawel Stefaniak 						 * we must set up the buffer */
11463c51c3cfSPiotr Pawel Stefaniak 			save_com = sc_buf;
11473c51c3cfSPiotr Pawel Stefaniak 			sc_end = &save_com[0];
11483c51c3cfSPiotr Pawel Stefaniak 		    }
11499b50d902SRodney W. Grimes 		    else {
11509b50d902SRodney W. Grimes 			*sc_end++ = '\n';	/* add newline between
11519b50d902SRodney W. Grimes 						 * comments */
11529b50d902SRodney W. Grimes 			*sc_end++ = ' ';
11539b50d902SRodney W. Grimes 			--line_no;
11549b50d902SRodney W. Grimes 		    }
1155bef613ffSPiotr Pawel Stefaniak 		    if (sc_end - save_com + com_end - com_start > sc_size)
1156bef613ffSPiotr Pawel Stefaniak 			errx(1, "input too long");
115703e6aeefSPiotr Pawel Stefaniak 		    memmove(sc_end, s_lab + com_start, com_end - com_start);
11589b50d902SRodney W. Grimes 		    sc_end += com_end - com_start;
11599b50d902SRodney W. Grimes 		    e_lab = s_lab + com_start;
11609b50d902SRodney W. Grimes 		    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
11619b50d902SRodney W. Grimes 			e_lab--;
11629b50d902SRodney W. Grimes 		    bp_save = buf_ptr;	/* save current input buffer */
11639b50d902SRodney W. Grimes 		    be_save = buf_end;
11649b50d902SRodney W. Grimes 		    buf_ptr = save_com;	/* fix so that subsequent calls to
11659b50d902SRodney W. Grimes 					 * lexi will take tokens out of
11669b50d902SRodney W. Grimes 					 * save_com */
11679b50d902SRodney W. Grimes 		    *sc_end++ = ' ';	/* add trailing blank, just in case */
11689b50d902SRodney W. Grimes 		    buf_end = sc_end;
1169c917a54bSPedro F. Giffuni 		    sc_end = NULL;
11709b50d902SRodney W. Grimes 		}
11719de29bfbSPiotr Pawel Stefaniak 		CHECK_SIZE_LAB(1);
11729b50d902SRodney W. Grimes 		*e_lab = '\0';	/* null terminate line */
11739b50d902SRodney W. Grimes 		ps.pcase = false;
11749b50d902SRodney W. Grimes 	    }
11759b50d902SRodney W. Grimes 
1176f7adee23SPedro F. Giffuni 	    if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
11776cf1bae2SMarcelo Araujo 		if ((size_t)ifdef_level < nitems(state_stack)) {
11789b50d902SRodney W. Grimes 		    match_state[ifdef_level].tos = -1;
11799b50d902SRodney W. Grimes 		    state_stack[ifdef_level++] = ps;
11809b50d902SRodney W. Grimes 		}
11819b50d902SRodney W. Grimes 		else
11827916863dSJens Schweikhardt 		    diag2(1, "#if stack overflow");
11839b50d902SRodney W. Grimes 	    }
1184f7adee23SPedro F. Giffuni 	    else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
11859b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
1186f7adee23SPedro F. Giffuni 		    diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
11879b50d902SRodney W. Grimes 		else {
11889b50d902SRodney W. Grimes 		    match_state[ifdef_level - 1] = ps;
11899b50d902SRodney W. Grimes 		    ps = state_stack[ifdef_level - 1];
11909b50d902SRodney W. Grimes 		}
1191f7adee23SPedro F. Giffuni 	    }
11929b50d902SRodney W. Grimes 	    else if (strncmp(s_lab, "#endif", 6) == 0) {
11939b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
11947916863dSJens Schweikhardt 		    diag2(1, "Unmatched #endif");
1195f7adee23SPedro F. Giffuni 		else
11969b50d902SRodney W. Grimes 		    ifdef_level--;
1197f7adee23SPedro F. Giffuni 	    } else {
1198f7adee23SPedro F. Giffuni 		struct directives {
1199f7adee23SPedro F. Giffuni 		    int size;
1200f7adee23SPedro F. Giffuni 		    const char *string;
1201f7adee23SPedro F. Giffuni 		}
1202f7adee23SPedro F. Giffuni 		recognized[] = {
1203f7adee23SPedro F. Giffuni 		    {7, "include"},
1204f7adee23SPedro F. Giffuni 		    {6, "define"},
1205f7adee23SPedro F. Giffuni 		    {5, "undef"},
1206f7adee23SPedro F. Giffuni 		    {4, "line"},
1207f7adee23SPedro F. Giffuni 		    {5, "error"},
1208f7adee23SPedro F. Giffuni 		    {6, "pragma"}
1209f7adee23SPedro F. Giffuni 		};
1210f7adee23SPedro F. Giffuni 		int d = nitems(recognized);
1211f7adee23SPedro F. Giffuni 		while (--d >= 0)
1212f7adee23SPedro F. Giffuni 		    if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
1213f7adee23SPedro F. Giffuni 			break;
1214f7adee23SPedro F. Giffuni 		if (d < 0) {
1215f7adee23SPedro F. Giffuni 		    diag2(1, "Unrecognized cpp directive");
1216f7adee23SPedro F. Giffuni 		    break;
1217f7adee23SPedro F. Giffuni 		}
12189b50d902SRodney W. Grimes 	    }
12197e53aaedSPiotr Pawel Stefaniak 	    if (opt.blanklines_around_conditional_compilation) {
12209b50d902SRodney W. Grimes 		postfix_blankline_requested++;
12219b50d902SRodney W. Grimes 		n_real_blanklines = 0;
12229b50d902SRodney W. Grimes 	    }
1223f7adee23SPedro F. Giffuni 	    else {
1224f7adee23SPedro F. Giffuni 		postfix_blankline_requested = 0;
1225f7adee23SPedro F. Giffuni 		prefix_blankline_requested = 0;
12269b50d902SRodney W. Grimes 	    }
12279b50d902SRodney W. Grimes 	    break;		/* subsequent processing of the newline
12289b50d902SRodney W. Grimes 				 * character will cause the line to be printed */
12299b50d902SRodney W. Grimes 
12307916863dSJens Schweikhardt 	case comment:		/* we have gotten a / followed by * this is a biggie */
12319b50d902SRodney W. Grimes 	    pr_comment();
12329b50d902SRodney W. Grimes 	    break;
12339b50d902SRodney W. Grimes 	}			/* end of big switch stmt */
12349b50d902SRodney W. Grimes 
12359b50d902SRodney W. Grimes 	*e_code = '\0';		/* make sure code section is null terminated */
12369b50d902SRodney W. Grimes 	if (type_code != comment && type_code != newline && type_code != preesc)
12379b50d902SRodney W. Grimes 	    ps.last_token = type_code;
12389b50d902SRodney W. Grimes     }				/* end of main while (1) loop */
12399b50d902SRodney W. Grimes }
12409b50d902SRodney W. Grimes 
12419b50d902SRodney W. Grimes /*
12429b50d902SRodney W. Grimes  * copy input file to backup file if in_name is /blah/blah/blah/file, then
12439b50d902SRodney W. Grimes  * backup file will be ".Bfile" then make the backup file the input and
12449b50d902SRodney W. Grimes  * original input file the output
12459b50d902SRodney W. Grimes  */
12467916863dSJens Schweikhardt static void
12477916863dSJens Schweikhardt bakcopy(void)
12489b50d902SRodney W. Grimes {
12499b50d902SRodney W. Grimes     int         n,
12509b50d902SRodney W. Grimes                 bakchn;
12519b50d902SRodney W. Grimes     char        buff[8 * 1024];
12528c7e7698SDavid Malone     const char *p;
12539b50d902SRodney W. Grimes 
12549b50d902SRodney W. Grimes     /* construct file name .Bfile */
12559b50d902SRodney W. Grimes     for (p = in_name; *p; p++);	/* skip to end of string */
12569b50d902SRodney W. Grimes     while (p > in_name && *p != '/')	/* find last '/' */
12579b50d902SRodney W. Grimes 	p--;
12589b50d902SRodney W. Grimes     if (*p == '/')
12599b50d902SRodney W. Grimes 	p++;
1260c6841b07SKyle Evans     sprintf(bakfile, "%s%s", p, simple_backup_suffix);
12619b50d902SRodney W. Grimes 
12629b50d902SRodney W. Grimes     /* copy in_name to backup file */
12639b50d902SRodney W. Grimes     bakchn = creat(bakfile, 0600);
12649b50d902SRodney W. Grimes     if (bakchn < 0)
12650c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
12660504bd65SPedro F. Giffuni     while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
12679b50d902SRodney W. Grimes 	if (write(bakchn, buff, n) != n)
12680c4d24a7SKris Kennaway 	    err(1, "%s", bakfile);
12699b50d902SRodney W. Grimes     if (n < 0)
12700c4d24a7SKris Kennaway 	err(1, "%s", in_name);
12719b50d902SRodney W. Grimes     close(bakchn);
12729b50d902SRodney W. Grimes     fclose(input);
12739b50d902SRodney W. Grimes 
12749b50d902SRodney W. Grimes     /* re-open backup file as the input file */
12759b50d902SRodney W. Grimes     input = fopen(bakfile, "r");
1276f5f8c098SKevin Lo     if (input == NULL)
12770c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
12789b50d902SRodney W. Grimes     /* now the original input file will be the output */
12799b50d902SRodney W. Grimes     output = fopen(in_name, "w");
1280f5f8c098SKevin Lo     if (output == NULL) {
12819b50d902SRodney W. Grimes 	unlink(bakfile);
12820c4d24a7SKris Kennaway 	err(1, "%s", in_name);
12839b50d902SRodney W. Grimes     }
12849b50d902SRodney W. Grimes }
128511c49893SPedro F. Giffuni 
128611c49893SPedro F. Giffuni static void
128711c49893SPedro F. Giffuni indent_declaration(int cur_dec_ind, int tabs_to_var)
128811c49893SPedro F. Giffuni {
128911c49893SPedro F. Giffuni     int pos = e_code - s_code;
129011c49893SPedro F. Giffuni     char *startpos = e_code;
129111c49893SPedro F. Giffuni 
129211c49893SPedro F. Giffuni     /*
1293fbdbd284SPiotr Pawel Stefaniak      * get the tab math right for indentations that are not multiples of tabsize
129411c49893SPedro F. Giffuni      */
12957e53aaedSPiotr Pawel Stefaniak     if ((ps.ind_level * opt.ind_size) % opt.tabsize != 0) {
12967e53aaedSPiotr Pawel Stefaniak 	pos += (ps.ind_level * opt.ind_size) % opt.tabsize;
12977e53aaedSPiotr Pawel Stefaniak 	cur_dec_ind += (ps.ind_level * opt.ind_size) % opt.tabsize;
129811c49893SPedro F. Giffuni     }
1299fbdbd284SPiotr Pawel Stefaniak     if (tabs_to_var) {
1300fbdbd284SPiotr Pawel Stefaniak 	int tpos;
1301fbdbd284SPiotr Pawel Stefaniak 
13027e53aaedSPiotr Pawel Stefaniak 	CHECK_SIZE_CODE(cur_dec_ind / opt.tabsize);
13037e53aaedSPiotr Pawel Stefaniak 	while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
130411c49893SPedro F. Giffuni 	    *e_code++ = '\t';
1305fbdbd284SPiotr Pawel Stefaniak 	    pos = tpos;
1306fbdbd284SPiotr Pawel Stefaniak 	}
130711c49893SPedro F. Giffuni     }
13089de29bfbSPiotr Pawel Stefaniak     CHECK_SIZE_CODE(cur_dec_ind - pos + 1);
130911c49893SPedro F. Giffuni     while (pos < cur_dec_ind) {
131011c49893SPedro F. Giffuni 	*e_code++ = ' ';
131111c49893SPedro F. Giffuni 	pos++;
131211c49893SPedro F. Giffuni     }
131311c49893SPedro F. Giffuni     if (e_code == startpos && ps.want_blank) {
131411c49893SPedro F. Giffuni 	*e_code++ = ' ';
131511c49893SPedro F. Giffuni 	ps.want_blank = false;
131611c49893SPedro F. Giffuni     }
131711c49893SPedro F. Giffuni }
1318