xref: /freebsd/usr.bin/indent/indent.c (revision 63c3f226969b89226e223bca86761b285d302ed6)
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 
389b50d902SRodney W. Grimes #ifndef lint
39958d7c9fSPhilippe Charnier static const char copyright[] =
409b50d902SRodney W. Grimes "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
419b50d902SRodney W. Grimes @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
429b50d902SRodney W. Grimes @(#) Copyright (c) 1980, 1993\n\
439b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
46958d7c9fSPhilippe Charnier #if 0
47d0054952SPhilippe Charnier #ifndef lint
489b50d902SRodney W. Grimes static char sccsid[] = "@(#)indent.c	5.17 (Berkeley) 6/7/93";
499b50d902SRodney W. Grimes #endif /* not lint */
50d0054952SPhilippe Charnier #endif
51d0054952SPhilippe Charnier 
52e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
53e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #include <sys/param.h>
56d36899d1SConrad Meyer #include <sys/capsicum.h>
57958d7c9fSPhilippe Charnier #include <err.h>
5810cc720cSConrad Meyer #include <errno.h>
599b50d902SRodney W. Grimes #include <fcntl.h>
609b50d902SRodney W. Grimes #include <unistd.h>
619b50d902SRodney W. Grimes #include <stdio.h>
629b50d902SRodney W. Grimes #include <stdlib.h>
639b50d902SRodney W. Grimes #include <string.h>
647916863dSJens Schweikhardt #include <ctype.h>
659b50d902SRodney W. Grimes #include "indent_globs.h"
669b50d902SRodney W. Grimes #include "indent_codes.h"
677916863dSJens Schweikhardt #include "indent.h"
687916863dSJens Schweikhardt 
697916863dSJens Schweikhardt static void bakcopy(void);
7011c49893SPedro F. Giffuni static void indent_declaration(int, int);
719b50d902SRodney W. Grimes 
728c7e7698SDavid Malone const char *in_name = "Standard Input";	/* will always point to name of input
739b50d902SRodney W. Grimes 					 * file */
748c7e7698SDavid Malone const char *out_name = "Standard Output";	/* will always point to name
759b50d902SRodney W. Grimes 						 * of output file */
76c6841b07SKyle Evans const char *simple_backup_suffix = ".BAK";	/* Suffix to use for backup
77c6841b07SKyle Evans 						 * files */
789b50d902SRodney W. Grimes char        bakfile[MAXPATHLEN] = "";
799b50d902SRodney W. Grimes 
807916863dSJens Schweikhardt int
817916863dSJens Schweikhardt main(int argc, char **argv)
829b50d902SRodney W. Grimes {
8310cc720cSConrad Meyer     cap_rights_t rights;
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes     int         dec_ind;	/* current indentation for declarations */
869b50d902SRodney W. Grimes     int         di_stack[20];	/* a stack of structure indentation levels */
879b50d902SRodney W. Grimes     int         force_nl;	/* when true, code must be broken */
887916863dSJens Schweikhardt     int         hd_type = 0;	/* used to store type of stmt for if (...),
899b50d902SRodney W. Grimes 				 * for (...), etc */
9090af6a72SJuli Mallett     int		i;		/* local loop counter */
919b50d902SRodney W. Grimes     int         scase;		/* set to true when we see a case, so we will
929b50d902SRodney W. Grimes 				 * know what to do with the following colon */
93dc51023cSPhilippe Charnier     int         sp_sw;		/* when true, we are in the expression of
949b50d902SRodney W. Grimes 				 * if(...), while(...), etc. */
959b50d902SRodney W. Grimes     int         squest;		/* when this is positive, we have seen a ?
969b50d902SRodney W. Grimes 				 * without the matching : in a <c>?<s>:<s>
979b50d902SRodney W. Grimes 				 * construct */
988c7e7698SDavid Malone     const char *t_ptr;		/* used for copying tokens */
99e3625e9cSJens Schweikhardt     int		tabs_to_var;	/* true if using tabs to indent to var name */
1009b50d902SRodney W. Grimes     int         type_code;	/* the type of token, returned by lexi */
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes     int         last_else = 0;	/* true iff last keyword was an else */
10386adac04SPiotr Pawel Stefaniak     const char *profile_name = NULL;
104c6841b07SKyle Evans     const char *envval = NULL;
105*63c3f226SPiotr Pawel Stefaniak     struct parser_state transient_state; /* a copy for lookup */
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes     /*-----------------------------------------------*\
1089b50d902SRodney W. Grimes     |		      INITIALIZATION		      |
1099b50d902SRodney W. Grimes     \*-----------------------------------------------*/
1109b50d902SRodney W. Grimes 
111d0054952SPhilippe Charnier     found_err = 0;
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes     ps.p_stack[0] = stmt;	/* this is the parser's stack */
1149b50d902SRodney W. Grimes     ps.last_nl = true;		/* this is true if the last thing scanned was
1159b50d902SRodney W. Grimes 				 * a newline */
1169b50d902SRodney W. Grimes     ps.last_token = semicolon;
1179b50d902SRodney W. Grimes     combuf = (char *) malloc(bufsize);
118d0054952SPhilippe Charnier     if (combuf == NULL)
119d0054952SPhilippe Charnier 	err(1, NULL);
1209b50d902SRodney W. Grimes     labbuf = (char *) malloc(bufsize);
121d0054952SPhilippe Charnier     if (labbuf == NULL)
122d0054952SPhilippe Charnier 	err(1, NULL);
1239b50d902SRodney W. Grimes     codebuf = (char *) malloc(bufsize);
124d0054952SPhilippe Charnier     if (codebuf == NULL)
125d0054952SPhilippe Charnier 	err(1, NULL);
1269b50d902SRodney W. Grimes     tokenbuf = (char *) malloc(bufsize);
127d0054952SPhilippe Charnier     if (tokenbuf == NULL)
128d0054952SPhilippe Charnier 	err(1, NULL);
129a6bcfda4SPedro F. Giffuni     alloc_typenames();
1309b50d902SRodney W. Grimes     l_com = combuf + bufsize - 5;
1319b50d902SRodney W. Grimes     l_lab = labbuf + bufsize - 5;
1329b50d902SRodney W. Grimes     l_code = codebuf + bufsize - 5;
1339b50d902SRodney W. Grimes     l_token = tokenbuf + bufsize - 5;
1349b50d902SRodney W. Grimes     combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
1359b50d902SRodney W. Grimes 						 * comment buffers */
1369b50d902SRodney W. Grimes     combuf[1] = codebuf[1] = labbuf[1] = '\0';
1379b50d902SRodney W. Grimes     ps.else_if = 1;		/* Default else-if special processing to on */
1389b50d902SRodney W. Grimes     s_lab = e_lab = labbuf + 1;
1399b50d902SRodney W. Grimes     s_code = e_code = codebuf + 1;
1409b50d902SRodney W. Grimes     s_com = e_com = combuf + 1;
1419b50d902SRodney W. Grimes     s_token = e_token = tokenbuf + 1;
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes     in_buffer = (char *) malloc(10);
144d0054952SPhilippe Charnier     if (in_buffer == NULL)
145d0054952SPhilippe Charnier 	err(1, NULL);
1469b50d902SRodney W. Grimes     in_buffer_limit = in_buffer + 8;
1479b50d902SRodney W. Grimes     buf_ptr = buf_end = in_buffer;
1489b50d902SRodney W. Grimes     line_no = 1;
1499b50d902SRodney W. Grimes     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
1509b50d902SRodney W. Grimes     sp_sw = force_nl = false;
1519b50d902SRodney W. Grimes     ps.in_or_st = false;
1529b50d902SRodney W. Grimes     ps.bl_line = true;
1539b50d902SRodney W. Grimes     dec_ind = 0;
1549b50d902SRodney W. Grimes     di_stack[ps.dec_nest = 0] = 0;
1559b50d902SRodney W. Grimes     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes     scase = ps.pcase = false;
1589b50d902SRodney W. Grimes     squest = 0;
159c917a54bSPedro F. Giffuni     sc_end = NULL;
160c917a54bSPedro F. Giffuni     bp_save = NULL;
161c917a54bSPedro F. Giffuni     be_save = NULL;
1629b50d902SRodney W. Grimes 
163c917a54bSPedro F. Giffuni     output = NULL;
164488633c7SPhilippe Charnier     tabs_to_var = 0;
1659b50d902SRodney W. Grimes 
166c6841b07SKyle Evans     envval = getenv("SIMPLE_BACKUP_SUFFIX");
167c6841b07SKyle Evans     if (envval)
168c6841b07SKyle Evans         simple_backup_suffix = envval;
169c6841b07SKyle Evans 
1709b50d902SRodney W. Grimes     /*--------------------------------------------------*\
1719b50d902SRodney W. Grimes     |   		COMMAND LINE SCAN		 |
1729b50d902SRodney W. Grimes     \*--------------------------------------------------*/
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes #ifdef undef
1759b50d902SRodney W. Grimes     max_col = 78;		/* -l78 */
1769b50d902SRodney W. Grimes     lineup_to_parens = 1;	/* -lp */
1779b50d902SRodney W. Grimes     ps.ljust_decl = 0;		/* -ndj */
1789b50d902SRodney W. Grimes     ps.com_ind = 33;		/* -c33 */
1799b50d902SRodney W. Grimes     star_comment_cont = 1;	/* -sc */
1809b50d902SRodney W. Grimes     ps.ind_size = 8;		/* -i8 */
1819b50d902SRodney W. Grimes     verbose = 0;
1829b50d902SRodney W. Grimes     ps.decl_indent = 16;	/* -di16 */
18388ce0e7fSBruce Evans     ps.local_decl_indent = -1;	/* if this is not set to some nonnegative value
18488ce0e7fSBruce Evans 				 * by an arg, we will set this equal to
18588ce0e7fSBruce Evans 				 * ps.decl_ind */
1869b50d902SRodney W. Grimes     ps.indent_parameters = 1;	/* -ip */
1879b50d902SRodney W. Grimes     ps.decl_com_ind = 0;	/* if this is not set to some positive value
1889b50d902SRodney W. Grimes 				 * by an arg, we will set this equal to
1899b50d902SRodney W. Grimes 				 * ps.com_ind */
1909b50d902SRodney W. Grimes     btype_2 = 1;		/* -br */
1919b50d902SRodney W. Grimes     cuddle_else = 1;		/* -ce */
1929b50d902SRodney W. Grimes     ps.unindent_displace = 0;	/* -d0 */
1939b50d902SRodney W. Grimes     ps.case_indent = 0;		/* -cli0 */
194a5e1cac0SDavid E. O'Brien     format_block_comments = 1;	/* -fcb */
1959b50d902SRodney W. Grimes     format_col1_comments = 1;	/* -fc1 */
1969b50d902SRodney W. Grimes     procnames_start_line = 1;	/* -psl */
1979b50d902SRodney W. Grimes     proc_calls_space = 0;	/* -npcs */
1989b50d902SRodney W. Grimes     comment_delimiter_on_blankline = 1;	/* -cdb */
1999b50d902SRodney W. Grimes     ps.leave_comma = 1;		/* -nbc */
2009b50d902SRodney W. Grimes #endif
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i)
2039b50d902SRodney W. Grimes 	if (strcmp(argv[i], "-npro") == 0)
2049b50d902SRodney W. Grimes 	    break;
20586adac04SPiotr Pawel Stefaniak 	else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
20686adac04SPiotr Pawel Stefaniak 	    profile_name = argv[i];	/* non-empty -P (set profile) */
2079b50d902SRodney W. Grimes     set_defaults();
2089b50d902SRodney W. Grimes     if (i >= argc)
20986adac04SPiotr Pawel Stefaniak 	set_profile(profile_name);
2109b50d902SRodney W. Grimes 
2119b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i) {
2129b50d902SRodney W. Grimes 
2139b50d902SRodney W. Grimes 	/*
2149b50d902SRodney W. Grimes 	 * look thru args (if any) for changes to defaults
2159b50d902SRodney W. Grimes 	 */
2169b50d902SRodney W. Grimes 	if (argv[i][0] != '-') {/* no flag on parameter */
217f5f8c098SKevin Lo 	    if (input == NULL) {	/* we must have the input file */
2189b50d902SRodney W. Grimes 		in_name = argv[i];	/* remember name of input file */
2199b50d902SRodney W. Grimes 		input = fopen(in_name, "r");
220f5f8c098SKevin Lo 		if (input == NULL)	/* check for open error */
2210c4d24a7SKris Kennaway 			err(1, "%s", in_name);
2229b50d902SRodney W. Grimes 		continue;
2239b50d902SRodney W. Grimes 	    }
224f5f8c098SKevin Lo 	    else if (output == NULL) {	/* we have the output file */
2259b50d902SRodney W. Grimes 		out_name = argv[i];	/* remember name of output file */
2269b50d902SRodney W. Grimes 		if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
2279b50d902SRodney W. Grimes 							 * the file */
228958d7c9fSPhilippe Charnier 		    errx(1, "input and output files must be different");
2299b50d902SRodney W. Grimes 		}
2309b50d902SRodney W. Grimes 		output = fopen(out_name, "w");
231f5f8c098SKevin Lo 		if (output == NULL)	/* check for create error */
2320c4d24a7SKris Kennaway 			err(1, "%s", out_name);
2339b50d902SRodney W. Grimes 		continue;
2349b50d902SRodney W. Grimes 	    }
235958d7c9fSPhilippe Charnier 	    errx(1, "unknown parameter: %s", argv[i]);
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 	else
2389b50d902SRodney W. Grimes 	    set_option(argv[i]);
2399b50d902SRodney W. Grimes     }				/* end of for */
240f5f8c098SKevin Lo     if (input == NULL)
24118251d71SPeter Hawkins 	input = stdin;
242f5f8c098SKevin Lo     if (output == NULL) {
24318251d71SPeter Hawkins 	if (troff || input == stdin)
2449b50d902SRodney W. Grimes 	    output = stdout;
2459b50d902SRodney W. Grimes 	else {
2469b50d902SRodney W. Grimes 	    out_name = in_name;
2479b50d902SRodney W. Grimes 	    bakcopy();
2489b50d902SRodney W. Grimes 	}
2499ef5c48bSBill Fumerola     }
25010cc720cSConrad Meyer 
25110cc720cSConrad Meyer     /* Restrict input/output descriptors and enter Capsicum sandbox. */
25210cc720cSConrad Meyer     cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
25310cc720cSConrad Meyer     if (cap_rights_limit(fileno(output), &rights) < 0 && errno != ENOSYS)
25410cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
25510cc720cSConrad Meyer     cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
25610cc720cSConrad Meyer     if (cap_rights_limit(fileno(input), &rights) < 0 && errno != ENOSYS)
25710cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
25810cc720cSConrad Meyer     if (cap_enter() < 0 && errno != ENOSYS)
25910cc720cSConrad Meyer 	err(EXIT_FAILURE, "unable to enter capability mode");
26010cc720cSConrad Meyer 
2619b50d902SRodney W. Grimes     if (ps.com_ind <= 1)
2629b50d902SRodney W. Grimes 	ps.com_ind = 2;		/* dont put normal comments before column 2 */
2639b50d902SRodney W. Grimes     if (troff) {
2649b50d902SRodney W. Grimes 	if (bodyf.font[0] == 0)
2659b50d902SRodney W. Grimes 	    parsefont(&bodyf, "R");
2669b50d902SRodney W. Grimes 	if (scomf.font[0] == 0)
2679b50d902SRodney W. Grimes 	    parsefont(&scomf, "I");
2689b50d902SRodney W. Grimes 	if (blkcomf.font[0] == 0)
2699b50d902SRodney W. Grimes 	    blkcomf = scomf, blkcomf.size += 2;
2709b50d902SRodney W. Grimes 	if (boxcomf.font[0] == 0)
2719b50d902SRodney W. Grimes 	    boxcomf = blkcomf;
2729b50d902SRodney W. Grimes 	if (stringf.font[0] == 0)
2739b50d902SRodney W. Grimes 	    parsefont(&stringf, "L");
2749b50d902SRodney W. Grimes 	if (keywordf.font[0] == 0)
2759b50d902SRodney W. Grimes 	    parsefont(&keywordf, "B");
2769b50d902SRodney W. Grimes 	writefdef(&bodyf, 'B');
2779b50d902SRodney W. Grimes 	writefdef(&scomf, 'C');
2789b50d902SRodney W. Grimes 	writefdef(&blkcomf, 'L');
2799b50d902SRodney W. Grimes 	writefdef(&boxcomf, 'X');
2809b50d902SRodney W. Grimes 	writefdef(&stringf, 'S');
2819b50d902SRodney W. Grimes 	writefdef(&keywordf, 'K');
2829b50d902SRodney W. Grimes     }
2839b50d902SRodney W. Grimes     if (block_comment_max_col <= 0)
2849b50d902SRodney W. Grimes 	block_comment_max_col = max_col;
28588ce0e7fSBruce Evans     if (ps.local_decl_indent < 0)	/* if not specified by user, set this */
28688ce0e7fSBruce Evans 	ps.local_decl_indent = ps.decl_indent;
2879b50d902SRodney W. Grimes     if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
2889b50d902SRodney W. Grimes 	ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
2899b50d902SRodney W. Grimes     if (continuation_indent == 0)
2909b50d902SRodney W. Grimes 	continuation_indent = ps.ind_size;
2919b50d902SRodney W. Grimes     fill_buffer();		/* get first batch of stuff into input buffer */
2929b50d902SRodney W. Grimes 
2939b50d902SRodney W. Grimes     parse(semicolon);
2949b50d902SRodney W. Grimes     {
29590af6a72SJuli Mallett 	char *p = buf_ptr;
29690af6a72SJuli Mallett 	int col = 1;
2979b50d902SRodney W. Grimes 
2989b50d902SRodney W. Grimes 	while (1) {
2999b50d902SRodney W. Grimes 	    if (*p == ' ')
3009b50d902SRodney W. Grimes 		col++;
3019b50d902SRodney W. Grimes 	    else if (*p == '\t')
302fbdbd284SPiotr Pawel Stefaniak 		col = tabsize * (1 + (col - 1) / tabsize) + 1;
3039b50d902SRodney W. Grimes 	    else
3049b50d902SRodney W. Grimes 		break;
3059b50d902SRodney W. Grimes 	    p++;
3069b50d902SRodney W. Grimes 	}
3079b50d902SRodney W. Grimes 	if (col > ps.ind_size)
3089b50d902SRodney W. Grimes 	    ps.ind_level = ps.i_l_follow = col / ps.ind_size;
3099b50d902SRodney W. Grimes     }
3109b50d902SRodney W. Grimes     if (troff) {
3118c7e7698SDavid Malone 	const char *p = in_name,
3129b50d902SRodney W. Grimes 	           *beg = in_name;
3139b50d902SRodney W. Grimes 
3149b50d902SRodney W. Grimes 	while (*p)
3159b50d902SRodney W. Grimes 	    if (*p++ == '/')
3169b50d902SRodney W. Grimes 		beg = p;
3179b50d902SRodney W. Grimes 	fprintf(output, ".Fn \"%s\"\n", beg);
3189b50d902SRodney W. Grimes     }
3199b50d902SRodney W. Grimes     /*
3209b50d902SRodney W. Grimes      * START OF MAIN LOOP
3219b50d902SRodney W. Grimes      */
3229b50d902SRodney W. Grimes 
3239b50d902SRodney W. Grimes     while (1) {			/* this is the main loop.  it will go until we
3249b50d902SRodney W. Grimes 				 * reach eof */
3259b50d902SRodney W. Grimes 	int         is_procname;
326ec5ac89eSPiotr Pawel Stefaniak 	int comment_buffered = false;
3279b50d902SRodney W. Grimes 
328*63c3f226SPiotr Pawel Stefaniak 	type_code = lexi(&ps);	/* lexi reads one token.  The actual
3299b50d902SRodney W. Grimes 				 * characters read are stored in "token". lexi
3309b50d902SRodney W. Grimes 				 * returns a code indicating the type of token */
3319b50d902SRodney W. Grimes 	is_procname = ps.procname[0];
3329b50d902SRodney W. Grimes 
3339b50d902SRodney W. Grimes 	/*
334ec5ac89eSPiotr Pawel Stefaniak 	 * The following code moves newlines and comments following an if (),
335ec5ac89eSPiotr Pawel Stefaniak 	 * while (), else, etc. up to the start of the following stmt to
336ec5ac89eSPiotr Pawel Stefaniak 	 * a buffer. This allows proper handling of both kinds of brace
337ec5ac89eSPiotr Pawel Stefaniak 	 * placement (-br, -bl) and cuddling "else" (-ce).
3389b50d902SRodney W. Grimes 	 */
3399b50d902SRodney W. Grimes 
340ec5ac89eSPiotr Pawel Stefaniak 	while (ps.search_brace) {
3419b50d902SRodney W. Grimes 	    switch (type_code) {
3429b50d902SRodney W. Grimes 	    case newline:
343ec5ac89eSPiotr Pawel Stefaniak 		if (sc_end == NULL) {
3449b50d902SRodney W. Grimes 		    save_com[0] = save_com[1] = ' ';
345ec5ac89eSPiotr Pawel Stefaniak 		    sc_end = &save_com[2];
3469b50d902SRodney W. Grimes 		}
347ec5ac89eSPiotr Pawel Stefaniak 		*sc_end++ = '\n';
348ec5ac89eSPiotr Pawel Stefaniak 		/*
349ec5ac89eSPiotr Pawel Stefaniak 		 * We may have inherited a force_nl == true from the previous
350ec5ac89eSPiotr Pawel Stefaniak 		 * token (like a semicolon). But once we know that a newline
351ec5ac89eSPiotr Pawel Stefaniak 		 * has been scanned in this loop, force_nl should be false.
352ec5ac89eSPiotr Pawel Stefaniak 		 *
353ec5ac89eSPiotr Pawel Stefaniak 		 * However, the force_nl == true must be preserved if newline
354ec5ac89eSPiotr Pawel Stefaniak 		 * is never scanned in this loop, so this assignment cannot be
355ec5ac89eSPiotr Pawel Stefaniak 		 * done earlier.
356ec5ac89eSPiotr Pawel Stefaniak 		 */
357ec5ac89eSPiotr Pawel Stefaniak 		force_nl = false;
358ec5ac89eSPiotr Pawel Stefaniak 	    case form_feed:
359ec5ac89eSPiotr Pawel Stefaniak 		break;
360ec5ac89eSPiotr Pawel Stefaniak 	    case comment:
361ec5ac89eSPiotr Pawel Stefaniak 		if (sc_end == NULL) {
362ec5ac89eSPiotr Pawel Stefaniak 		    save_com[0] = save_com[1] = ' ';
363ec5ac89eSPiotr Pawel Stefaniak 		    sc_end = &save_com[2];
3649b50d902SRodney W. Grimes 		}
365ec5ac89eSPiotr Pawel Stefaniak 		comment_buffered = true;
3669b50d902SRodney W. Grimes 		*sc_end++ = '/';	/* copy in start of comment */
3679b50d902SRodney W. Grimes 		*sc_end++ = '*';
3689b50d902SRodney W. Grimes 		for (;;) {	/* loop until we get to the end of the comment */
3699b50d902SRodney W. Grimes 		    *sc_end = *buf_ptr++;
3709b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
3719b50d902SRodney W. Grimes 			fill_buffer();
3729b50d902SRodney W. Grimes 		    if (*sc_end++ == '*' && *buf_ptr == '/')
3739b50d902SRodney W. Grimes 			break;	/* we are at end of comment */
374ec5ac89eSPiotr Pawel Stefaniak 		    if (sc_end >= &save_com[sc_size]) {	/* check for temp buffer
3759b50d902SRodney W. Grimes 							 * overflow */
376d0054952SPhilippe Charnier 			diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
3779b50d902SRodney W. Grimes 			fflush(output);
3789b50d902SRodney W. Grimes 			exit(1);
3799b50d902SRodney W. Grimes 		    }
3809b50d902SRodney W. Grimes 		}
3819b50d902SRodney W. Grimes 		*sc_end++ = '/';	/* add ending slash */
3829b50d902SRodney W. Grimes 		if (++buf_ptr >= buf_end)	/* get past / in buffer */
3839b50d902SRodney W. Grimes 		    fill_buffer();
3849b50d902SRodney W. Grimes 		break;
385ec5ac89eSPiotr Pawel Stefaniak 	    case lbrace:
386ec5ac89eSPiotr Pawel Stefaniak 		/*
387ec5ac89eSPiotr Pawel Stefaniak 		 * Put KNF-style lbraces before the buffered up tokens and
388ec5ac89eSPiotr Pawel Stefaniak 		 * jump out of this loop in order to avoid copying the token
389ec5ac89eSPiotr Pawel Stefaniak 		 * again under the default case of the switch below.
390ec5ac89eSPiotr Pawel Stefaniak 		 */
391ec5ac89eSPiotr Pawel Stefaniak 		if (sc_end != NULL && btype_2) {
392ec5ac89eSPiotr Pawel Stefaniak 		    save_com[0] = '{';
393ec5ac89eSPiotr Pawel Stefaniak 		    /*
394ec5ac89eSPiotr Pawel Stefaniak 		     * Originally the lbrace may have been alone on its own
395ec5ac89eSPiotr Pawel Stefaniak 		     * line, but it will be moved into "the else's line", so
396ec5ac89eSPiotr Pawel Stefaniak 		     * if there was a newline resulting from the "{" before,
397ec5ac89eSPiotr Pawel Stefaniak 		     * it must be scanned now and ignored.
398ec5ac89eSPiotr Pawel Stefaniak 		     */
399ec5ac89eSPiotr Pawel Stefaniak 		    while (isspace((int)*buf_ptr)) {
400ec5ac89eSPiotr Pawel Stefaniak 			if (++buf_ptr >= buf_end)
401ec5ac89eSPiotr Pawel Stefaniak 			    fill_buffer();
402ec5ac89eSPiotr Pawel Stefaniak 			if (*buf_ptr == '\n')
403ec5ac89eSPiotr Pawel Stefaniak 			    break;
4049b50d902SRodney W. Grimes 		    }
405ec5ac89eSPiotr Pawel Stefaniak 		    goto sw_buffer;
406ec5ac89eSPiotr Pawel Stefaniak 		}
407ec5ac89eSPiotr Pawel Stefaniak 		/* FALLTHROUGH */
408dc51023cSPhilippe Charnier 	    default:		/* it is the start of a normal statement */
409ec5ac89eSPiotr Pawel Stefaniak 		{
410ec5ac89eSPiotr Pawel Stefaniak 		    int remove_newlines;
4119b50d902SRodney W. Grimes 
412ec5ac89eSPiotr Pawel Stefaniak 		    remove_newlines =
413ec5ac89eSPiotr Pawel Stefaniak 			/* "} else" */
414ec5ac89eSPiotr Pawel Stefaniak 			(type_code == sp_nparen && *token == 'e' &&
415ec5ac89eSPiotr Pawel Stefaniak 			    e_code != s_code && e_code[-1] == '}')
416ec5ac89eSPiotr Pawel Stefaniak 			/* "else if" */
417ec5ac89eSPiotr Pawel Stefaniak 			|| (type_code == sp_paren && *token == 'i' &&
418ec5ac89eSPiotr Pawel Stefaniak 			    last_else && ps.else_if);
419ec5ac89eSPiotr Pawel Stefaniak 		    if (remove_newlines)
420ec5ac89eSPiotr Pawel Stefaniak 			force_nl = false;
421ec5ac89eSPiotr Pawel Stefaniak 		    if (sc_end == NULL) {	/* ignore buffering if
422ec5ac89eSPiotr Pawel Stefaniak 						 * comment wasn't saved up */
4239b50d902SRodney W. Grimes 			ps.search_brace = false;
4249b50d902SRodney W. Grimes 			goto check_type;
4259b50d902SRodney W. Grimes 		    }
426ec5ac89eSPiotr Pawel Stefaniak 		    while (sc_end > save_com && isblank((int)sc_end[-1])) {
427ec5ac89eSPiotr Pawel Stefaniak 			sc_end--;
428ec5ac89eSPiotr Pawel Stefaniak 		    }
429ec5ac89eSPiotr Pawel Stefaniak 		    if (swallow_optional_blanklines ||
430ec5ac89eSPiotr Pawel Stefaniak 			(!comment_buffered && remove_newlines)) {
431ec5ac89eSPiotr Pawel Stefaniak 			force_nl = !remove_newlines;
432ec5ac89eSPiotr Pawel Stefaniak 			while (sc_end > save_com && sc_end[-1] == '\n') {
433ec5ac89eSPiotr Pawel Stefaniak 			    sc_end--;
434ec5ac89eSPiotr Pawel Stefaniak 			}
435ec5ac89eSPiotr Pawel Stefaniak 		    }
436ec5ac89eSPiotr Pawel Stefaniak 		    if (force_nl) {	/* if we should insert a nl here, put
437ec5ac89eSPiotr Pawel Stefaniak 					 * it into the buffer */
4389b50d902SRodney W. Grimes 			force_nl = false;
439ec5ac89eSPiotr Pawel Stefaniak 			--line_no;	/* this will be re-increased when the
440ec5ac89eSPiotr Pawel Stefaniak 					 * newline is read from the buffer */
4419b50d902SRodney W. Grimes 			*sc_end++ = '\n';
4429b50d902SRodney W. Grimes 			*sc_end++ = ' ';
443ec5ac89eSPiotr Pawel Stefaniak 			if (verbose)	/* print error msg if the line was
444ec5ac89eSPiotr Pawel Stefaniak 					 * not already broken */
4457916863dSJens Schweikhardt 			    diag2(0, "Line broken");
4469b50d902SRodney W. Grimes 		    }
4479b50d902SRodney W. Grimes 		    for (t_ptr = token; *t_ptr; ++t_ptr)
448ec5ac89eSPiotr Pawel Stefaniak 			*sc_end++ = *t_ptr;
4499b50d902SRodney W. Grimes 
4509b50d902SRodney W. Grimes 	    sw_buffer:
4519b50d902SRodney W. Grimes 		    ps.search_brace = false;	/* stop looking for start of
4529b50d902SRodney W. Grimes 						 * stmt */
4539b50d902SRodney W. Grimes 		    bp_save = buf_ptr;	/* save current input buffer */
4549b50d902SRodney W. Grimes 		    be_save = buf_end;
4559b50d902SRodney W. Grimes 		    buf_ptr = save_com;	/* fix so that subsequent calls to
4569b50d902SRodney W. Grimes 					 * lexi will take tokens out of
4579b50d902SRodney W. Grimes 					 * save_com */
4589b50d902SRodney W. Grimes 		    *sc_end++ = ' ';/* add trailing blank, just in case */
4599b50d902SRodney W. Grimes 		    buf_end = sc_end;
460c917a54bSPedro F. Giffuni 		    sc_end = NULL;
4619b50d902SRodney W. Grimes 		    break;
462ec5ac89eSPiotr Pawel Stefaniak 		}
4639b50d902SRodney W. Grimes 	    }			/* end of switch */
464*63c3f226SPiotr Pawel Stefaniak 	    /*
465*63c3f226SPiotr Pawel Stefaniak 	     * We must make this check, just in case there was an unexpected
466*63c3f226SPiotr Pawel Stefaniak 	     * EOF.
467*63c3f226SPiotr Pawel Stefaniak 	     */
468*63c3f226SPiotr Pawel Stefaniak 	    if (type_code != 0) {
469*63c3f226SPiotr Pawel Stefaniak 		/*
470*63c3f226SPiotr Pawel Stefaniak 		 * The only intended purpose of calling lexi() below is to
471*63c3f226SPiotr Pawel Stefaniak 		 * categorize the next token in order to decide whether to
472*63c3f226SPiotr Pawel Stefaniak 		 * continue buffering forthcoming tokens. Once the buffering
473*63c3f226SPiotr Pawel Stefaniak 		 * is over, lexi() will be called again elsewhere on all of
474*63c3f226SPiotr Pawel Stefaniak 		 * the tokens - this time for normal processing.
475*63c3f226SPiotr Pawel Stefaniak 		 *
476*63c3f226SPiotr Pawel Stefaniak 		 * Calling it for this purpose is a bug, because lexi() also
477*63c3f226SPiotr Pawel Stefaniak 		 * changes the parser state and discards leading whitespace,
478*63c3f226SPiotr Pawel Stefaniak 		 * which is needed mostly for comment-related considerations.
479*63c3f226SPiotr Pawel Stefaniak 		 *
480*63c3f226SPiotr Pawel Stefaniak 		 * Work around the former problem by giving lexi() a copy of
481*63c3f226SPiotr Pawel Stefaniak 		 * the current parser state and discard it if the call turned
482*63c3f226SPiotr Pawel Stefaniak 		 * out to be just a look ahead.
483*63c3f226SPiotr Pawel Stefaniak 		 *
484*63c3f226SPiotr Pawel Stefaniak 		 * Work around the latter problem by copying all whitespace
485*63c3f226SPiotr Pawel Stefaniak 		 * characters into the buffer so that the later lexi() call
486*63c3f226SPiotr Pawel Stefaniak 		 * will read them.
487*63c3f226SPiotr Pawel Stefaniak 		 */
488*63c3f226SPiotr Pawel Stefaniak 		if (sc_end != NULL) {
489*63c3f226SPiotr Pawel Stefaniak 		    while (*buf_ptr == ' ' || *buf_ptr == '\t') {
490*63c3f226SPiotr Pawel Stefaniak 			*sc_end++ = *buf_ptr++;
491*63c3f226SPiotr Pawel Stefaniak 			if (sc_end >= &save_com[sc_size]) {
492*63c3f226SPiotr Pawel Stefaniak 			    abort();
493*63c3f226SPiotr Pawel Stefaniak 			}
494*63c3f226SPiotr Pawel Stefaniak 		    }
495*63c3f226SPiotr Pawel Stefaniak 		    if (buf_ptr >= buf_end) {
496*63c3f226SPiotr Pawel Stefaniak 			fill_buffer();
497*63c3f226SPiotr Pawel Stefaniak 		    }
498*63c3f226SPiotr Pawel Stefaniak 		}
499*63c3f226SPiotr Pawel Stefaniak 		transient_state = ps;
500*63c3f226SPiotr Pawel Stefaniak 		type_code = lexi(&transient_state);	/* read another token */
501*63c3f226SPiotr Pawel Stefaniak 		if (type_code != newline && type_code != form_feed &&
502*63c3f226SPiotr Pawel Stefaniak 		    type_code != comment && !transient_state.search_brace) {
503*63c3f226SPiotr Pawel Stefaniak 		    ps = transient_state;
504*63c3f226SPiotr Pawel Stefaniak 		}
505*63c3f226SPiotr Pawel Stefaniak 	    }
5069b50d902SRodney W. Grimes 	}			/* end of while (search_brace) */
5079b50d902SRodney W. Grimes 	last_else = 0;
5089b50d902SRodney W. Grimes check_type:
5099b50d902SRodney W. Grimes 	if (type_code == 0) {	/* we got eof */
5109b50d902SRodney W. Grimes 	    if (s_lab != e_lab || s_code != e_code
5119b50d902SRodney W. Grimes 		    || s_com != e_com)	/* must dump end of line */
5129b50d902SRodney W. Grimes 		dump_line();
5139b50d902SRodney W. Grimes 	    if (ps.tos > 1)	/* check for balanced braces */
514d0054952SPhilippe Charnier 		diag2(1, "Stuff missing from end of file");
5159b50d902SRodney W. Grimes 
5169b50d902SRodney W. Grimes 	    if (verbose) {
5179b50d902SRodney W. Grimes 		printf("There were %d output lines and %d comments\n",
5189b50d902SRodney W. Grimes 		       ps.out_lines, ps.out_coms);
5199b50d902SRodney W. Grimes 		printf("(Lines with comments)/(Lines with code): %6.3f\n",
5209b50d902SRodney W. Grimes 		       (1.0 * ps.com_lines) / code_lines);
5219b50d902SRodney W. Grimes 	    }
5229b50d902SRodney W. Grimes 	    fflush(output);
5239b50d902SRodney W. Grimes 	    exit(found_err);
5249b50d902SRodney W. Grimes 	}
5259b50d902SRodney W. Grimes 	if (
5269b50d902SRodney W. Grimes 		(type_code != comment) &&
5279b50d902SRodney W. Grimes 		(type_code != newline) &&
5289b50d902SRodney W. Grimes 		(type_code != preesc) &&
5299b50d902SRodney W. Grimes 		(type_code != form_feed)) {
5309b50d902SRodney W. Grimes 	    if (force_nl &&
5319b50d902SRodney W. Grimes 		    (type_code != semicolon) &&
5329b50d902SRodney W. Grimes 		    (type_code != lbrace || !btype_2)) {
5339b50d902SRodney W. Grimes 		/* we should force a broken line here */
534ec5ac89eSPiotr Pawel Stefaniak 		if (verbose)
5357916863dSJens Schweikhardt 		    diag2(0, "Line broken");
5369b50d902SRodney W. Grimes 		dump_line();
5379b50d902SRodney W. Grimes 		ps.want_blank = false;	/* dont insert blank at line start */
5389b50d902SRodney W. Grimes 		force_nl = false;
5399b50d902SRodney W. Grimes 	    }
5409b50d902SRodney W. Grimes 	    ps.in_stmt = true;	/* turn on flag which causes an extra level of
5419b50d902SRodney W. Grimes 				 * indentation. this is turned off by a ; or
5429b50d902SRodney W. Grimes 				 * '}' */
5439b50d902SRodney W. Grimes 	    if (s_com != e_com) {	/* the turkey has embedded a comment
5449b50d902SRodney W. Grimes 					 * in a line. fix it */
5459b50d902SRodney W. Grimes 		*e_code++ = ' ';
5469b50d902SRodney W. Grimes 		for (t_ptr = s_com; *t_ptr; ++t_ptr) {
5479b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
5489b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
5499b50d902SRodney W. Grimes 		}
5509b50d902SRodney W. Grimes 		*e_code++ = ' ';
5519b50d902SRodney W. Grimes 		*e_code = '\0';	/* null terminate code sect */
5529b50d902SRodney W. Grimes 		ps.want_blank = false;
5539b50d902SRodney W. Grimes 		e_com = s_com;
5549b50d902SRodney W. Grimes 	    }
5559b50d902SRodney W. Grimes 	}
5569b50d902SRodney W. Grimes 	else if (type_code != comment)	/* preserve force_nl thru a comment */
5579b50d902SRodney W. Grimes 	    force_nl = false;	/* cancel forced newline after newline, form
5589b50d902SRodney W. Grimes 				 * feed, etc */
5599b50d902SRodney W. Grimes 
5609b50d902SRodney W. Grimes 
5619b50d902SRodney W. Grimes 
5629b50d902SRodney W. Grimes 	/*-----------------------------------------------------*\
5639b50d902SRodney W. Grimes 	|	   do switch on type of token scanned		|
5649b50d902SRodney W. Grimes 	\*-----------------------------------------------------*/
5659b50d902SRodney W. Grimes 	CHECK_SIZE_CODE;
5669b50d902SRodney W. Grimes 	switch (type_code) {	/* now, decide what to do with the token */
5679b50d902SRodney W. Grimes 
5689b50d902SRodney W. Grimes 	case form_feed:	/* found a form feed in line */
5699b50d902SRodney W. Grimes 	    ps.use_ff = true;	/* a form feed is treated much like a newline */
5709b50d902SRodney W. Grimes 	    dump_line();
5719b50d902SRodney W. Grimes 	    ps.want_blank = false;
5729b50d902SRodney W. Grimes 	    break;
5739b50d902SRodney W. Grimes 
5749b50d902SRodney W. Grimes 	case newline:
5759b50d902SRodney W. Grimes 	    if (ps.last_token != comma || ps.p_l_follow > 0
5769b50d902SRodney W. Grimes 		    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
5779b50d902SRodney W. Grimes 		dump_line();
5789b50d902SRodney W. Grimes 		ps.want_blank = false;
5799b50d902SRodney W. Grimes 	    }
5809b50d902SRodney W. Grimes 	    ++line_no;		/* keep track of input line number */
5819b50d902SRodney W. Grimes 	    break;
5829b50d902SRodney W. Grimes 
5839b50d902SRodney W. Grimes 	case lparen:		/* got a '(' or '[' */
58493567e87SPiotr Pawel Stefaniak 	    /* count parens to make Healy happy */
58593567e87SPiotr Pawel Stefaniak 	    if (++ps.p_l_follow == nitems(ps.paren_indents)) {
58693567e87SPiotr Pawel Stefaniak 		diag3(0, "Reached internal limit of %d unclosed parens",
58793567e87SPiotr Pawel Stefaniak 		    nitems(ps.paren_indents));
58893567e87SPiotr Pawel Stefaniak 		ps.p_l_follow--;
58993567e87SPiotr Pawel Stefaniak 	    }
59011c49893SPedro F. Giffuni 	    if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
5919d4264fbSPiotr Pawel Stefaniak 		!is_procname && ps.paren_level == 0) {
59211c49893SPedro F. Giffuni 		/* function pointer declarations */
59311c49893SPedro F. Giffuni 		if (troff) {
5949b50d902SRodney W. Grimes 		    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
5959b50d902SRodney W. Grimes 		    e_code += strlen(e_code);
5969b50d902SRodney W. Grimes 		}
5979b50d902SRodney W. Grimes 		else {
59811c49893SPedro F. Giffuni 		    indent_declaration(dec_ind, tabs_to_var);
5999b50d902SRodney W. Grimes 		}
60011c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
6019b50d902SRodney W. Grimes 	    }
602b06c2eb7SPiotr Pawel Stefaniak 	    else if (ps.want_blank && *token != '[' &&
603b06c2eb7SPiotr Pawel Stefaniak 		    ((ps.last_token != ident && ps.last_token != funcname) ||
6041d018043SPiotr Pawel Stefaniak 		    proc_calls_space ||
605b06c2eb7SPiotr Pawel Stefaniak 		    /* offsetof (1) is never allowed a space; sizeof (2) gets
606b06c2eb7SPiotr Pawel Stefaniak 		     * one iff -bs; all other keywords (>2) always get a space
607b06c2eb7SPiotr Pawel Stefaniak 		     * before lparen */
608b06c2eb7SPiotr Pawel Stefaniak 			ps.keyword + Bill_Shannon > 2))
609b06c2eb7SPiotr Pawel Stefaniak 		*e_code++ = ' ';
610b06c2eb7SPiotr Pawel Stefaniak 	    ps.want_blank = false;
61111c49893SPedro F. Giffuni 	    if (!troff)
6129b50d902SRodney W. Grimes 		*e_code++ = token[0];
6139d4264fbSPiotr Pawel Stefaniak 	    ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
6149b50d902SRodney W. Grimes 	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
6159b50d902SRodney W. Grimes 		    && ps.paren_indents[0] < 2 * ps.ind_size)
6169b50d902SRodney W. Grimes 		ps.paren_indents[0] = 2 * ps.ind_size;
6179b50d902SRodney W. Grimes 	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
6189b50d902SRodney W. Grimes 		/*
6199b50d902SRodney W. Grimes 		 * this is a kluge to make sure that declarations will be
6209b50d902SRodney W. Grimes 		 * aligned right if proc decl has an explicit type on it, i.e.
6219b50d902SRodney W. Grimes 		 * "int a(x) {..."
6229b50d902SRodney W. Grimes 		 */
6239b50d902SRodney W. Grimes 		parse(semicolon);	/* I said this was a kluge... */
6249b50d902SRodney W. Grimes 		ps.in_or_st = false;	/* turn off flag for structure decl or
6259b50d902SRodney W. Grimes 					 * initialization */
6269b50d902SRodney W. Grimes 	    }
627b4939677SPedro F. Giffuni 	    /* parenthesized type following sizeof or offsetof is not a cast */
628b4939677SPedro F. Giffuni 	    if (ps.keyword == 1 || ps.keyword == 2)
629b4939677SPedro F. Giffuni 		ps.not_cast_mask |= 1 << ps.p_l_follow;
6309b50d902SRodney W. Grimes 	    break;
6319b50d902SRodney W. Grimes 
6329b50d902SRodney W. Grimes 	case rparen:		/* got a ')' or ']' */
633b4939677SPedro F. Giffuni 	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
6349b50d902SRodney W. Grimes 		ps.last_u_d = true;
6359b50d902SRodney W. Grimes 		ps.cast_mask &= (1 << ps.p_l_follow) - 1;
636771aff0aSPedro F. Giffuni 		ps.want_blank = space_after_cast;
637a5e1cac0SDavid E. O'Brien 	    } else
638a5e1cac0SDavid E. O'Brien 		ps.want_blank = true;
639b4939677SPedro F. Giffuni 	    ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
6409b50d902SRodney W. Grimes 	    if (--ps.p_l_follow < 0) {
6419b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
6427916863dSJens Schweikhardt 		diag3(0, "Extra %c", *token);
6439b50d902SRodney W. Grimes 	    }
6449b50d902SRodney W. Grimes 	    if (e_code == s_code)	/* if the paren starts the line */
6459b50d902SRodney W. Grimes 		ps.paren_level = ps.p_l_follow;	/* then indent it */
6469b50d902SRodney W. Grimes 
6479b50d902SRodney W. Grimes 	    *e_code++ = token[0];
6489b50d902SRodney W. Grimes 
6499b50d902SRodney W. Grimes 	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
6509b50d902SRodney W. Grimes 							 * (...), or some such */
6519b50d902SRodney W. Grimes 		sp_sw = false;
6529b50d902SRodney W. Grimes 		force_nl = true;/* must force newline after if */
6539b50d902SRodney W. Grimes 		ps.last_u_d = true;	/* inform lexi that a following
6549b50d902SRodney W. Grimes 					 * operator is unary */
6559b50d902SRodney W. Grimes 		ps.in_stmt = false;	/* dont use stmt continuation
6569b50d902SRodney W. Grimes 					 * indentation */
6579b50d902SRodney W. Grimes 
6589b50d902SRodney W. Grimes 		parse(hd_type);	/* let parser worry about if, or whatever */
6599b50d902SRodney W. Grimes 	    }
6609b50d902SRodney W. Grimes 	    ps.search_brace = btype_2;	/* this should insure that constructs
6619b50d902SRodney W. Grimes 					 * such as main(){...} and int[]{...}
6629b50d902SRodney W. Grimes 					 * have their braces put in the right
6639b50d902SRodney W. Grimes 					 * place */
6649b50d902SRodney W. Grimes 	    break;
6659b50d902SRodney W. Grimes 
6669b50d902SRodney W. Grimes 	case unary_op:		/* this could be any unary operation */
66711c49893SPedro F. Giffuni 	    if (!ps.dumped_decl_indent && ps.in_decl && !is_procname &&
6689d4264fbSPiotr Pawel Stefaniak 		!ps.block_init && ps.paren_level == 0) {
66911c49893SPedro F. Giffuni 		/* pointer declarations */
67011c49893SPedro F. Giffuni 		if (troff) {
6719b50d902SRodney W. Grimes 		    if (ps.want_blank)
6729b50d902SRodney W. Grimes 			*e_code++ = ' ';
67311c49893SPedro F. Giffuni 		    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7,
67411c49893SPedro F. Giffuni 			token);
6759b50d902SRodney W. Grimes 		    e_code += strlen(e_code);
6769b50d902SRodney W. Grimes 		}
6779b50d902SRodney W. Grimes 		else {
67811c49893SPedro F. Giffuni 			/* if this is a unary op in a declaration, we should
67911c49893SPedro F. Giffuni 			 * indent this token */
68011c49893SPedro F. Giffuni 			for (i = 0; token[i]; ++i)
68111c49893SPedro F. Giffuni 			    /* find length of token */;
68211c49893SPedro F. Giffuni 			indent_declaration(dec_ind - i, tabs_to_var);
68311c49893SPedro F. Giffuni 		}
68411c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
68511c49893SPedro F. Giffuni 	    }
68611c49893SPedro F. Giffuni 	    else if (ps.want_blank)
68711c49893SPedro F. Giffuni 		*e_code++ = ' ';
68811c49893SPedro F. Giffuni 	    {
6898c7e7698SDavid Malone 		const char *res = token;
6909b50d902SRodney W. Grimes 
6919b50d902SRodney W. Grimes 		if (troff && token[0] == '-' && token[1] == '>')
6929b50d902SRodney W. Grimes 		    res = "\\(->";
6939b50d902SRodney W. Grimes 		for (t_ptr = res; *t_ptr; ++t_ptr) {
6949b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
6959b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
6969b50d902SRodney W. Grimes 		}
6979b50d902SRodney W. Grimes 	    }
6989b50d902SRodney W. Grimes 	    ps.want_blank = false;
6999b50d902SRodney W. Grimes 	    break;
7009b50d902SRodney W. Grimes 
7019b50d902SRodney W. Grimes 	case binary_op:	/* any binary operation */
7029b50d902SRodney W. Grimes 	    if (ps.want_blank)
7039b50d902SRodney W. Grimes 		*e_code++ = ' ';
7049b50d902SRodney W. Grimes 	    {
7058c7e7698SDavid Malone 		const char *res = token;
7069b50d902SRodney W. Grimes 
7079b50d902SRodney W. Grimes 		if (troff)
7089b50d902SRodney W. Grimes 		    switch (token[0]) {
7099b50d902SRodney W. Grimes 		    case '<':
7109b50d902SRodney W. Grimes 			if (token[1] == '=')
7119b50d902SRodney W. Grimes 			    res = "\\(<=";
7129b50d902SRodney W. Grimes 			break;
7139b50d902SRodney W. Grimes 		    case '>':
7149b50d902SRodney W. Grimes 			if (token[1] == '=')
7159b50d902SRodney W. Grimes 			    res = "\\(>=";
7169b50d902SRodney W. Grimes 			break;
7179b50d902SRodney W. Grimes 		    case '!':
7189b50d902SRodney W. Grimes 			if (token[1] == '=')
7199b50d902SRodney W. Grimes 			    res = "\\(!=";
7209b50d902SRodney W. Grimes 			break;
7219b50d902SRodney W. Grimes 		    case '|':
7229b50d902SRodney W. Grimes 			if (token[1] == '|')
7239b50d902SRodney W. Grimes 			    res = "\\(br\\(br";
7249b50d902SRodney W. Grimes 			else if (token[1] == 0)
7259b50d902SRodney W. Grimes 			    res = "\\(br";
7269b50d902SRodney W. Grimes 			break;
7279b50d902SRodney W. Grimes 		    }
7289b50d902SRodney W. Grimes 		for (t_ptr = res; *t_ptr; ++t_ptr) {
7299b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
7309b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;	/* move the operator */
7319b50d902SRodney W. Grimes 		}
7329b50d902SRodney W. Grimes 	    }
7339b50d902SRodney W. Grimes 	    ps.want_blank = true;
7349b50d902SRodney W. Grimes 	    break;
7359b50d902SRodney W. Grimes 
7369b50d902SRodney W. Grimes 	case postop:		/* got a trailing ++ or -- */
7379b50d902SRodney W. Grimes 	    *e_code++ = token[0];
7389b50d902SRodney W. Grimes 	    *e_code++ = token[1];
7399b50d902SRodney W. Grimes 	    ps.want_blank = true;
7409b50d902SRodney W. Grimes 	    break;
7419b50d902SRodney W. Grimes 
7429b50d902SRodney W. Grimes 	case question:		/* got a ? */
7439b50d902SRodney W. Grimes 	    squest++;		/* this will be used when a later colon
7449b50d902SRodney W. Grimes 				 * appears so we can distinguish the
7459b50d902SRodney W. Grimes 				 * <c>?<n>:<n> construct */
7469b50d902SRodney W. Grimes 	    if (ps.want_blank)
7479b50d902SRodney W. Grimes 		*e_code++ = ' ';
7489b50d902SRodney W. Grimes 	    *e_code++ = '?';
7499b50d902SRodney W. Grimes 	    ps.want_blank = true;
7509b50d902SRodney W. Grimes 	    break;
7519b50d902SRodney W. Grimes 
7529b50d902SRodney W. Grimes 	case casestmt:		/* got word 'case' or 'default' */
7539b50d902SRodney W. Grimes 	    scase = true;	/* so we can process the later colon properly */
7549b50d902SRodney W. Grimes 	    goto copy_id;
7559b50d902SRodney W. Grimes 
7569b50d902SRodney W. Grimes 	case colon:		/* got a ':' */
7579b50d902SRodney W. Grimes 	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
7589b50d902SRodney W. Grimes 		--squest;
7599b50d902SRodney W. Grimes 		if (ps.want_blank)
7609b50d902SRodney W. Grimes 		    *e_code++ = ' ';
7619b50d902SRodney W. Grimes 		*e_code++ = ':';
7629b50d902SRodney W. Grimes 		ps.want_blank = true;
7639b50d902SRodney W. Grimes 		break;
7649b50d902SRodney W. Grimes 	    }
7656953f51aSAndriy Gapon 	    if (ps.in_or_st) {
7669b50d902SRodney W. Grimes 		*e_code++ = ':';
7679b50d902SRodney W. Grimes 		ps.want_blank = false;
7689b50d902SRodney W. Grimes 		break;
7699b50d902SRodney W. Grimes 	    }
7709b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* seeing a label does not imply we are in a
7719b50d902SRodney W. Grimes 				 * stmt */
7729b50d902SRodney W. Grimes 	    for (t_ptr = s_code; *t_ptr; ++t_ptr)
7739b50d902SRodney W. Grimes 		*e_lab++ = *t_ptr;	/* turn everything so far into a label */
7749b50d902SRodney W. Grimes 	    e_code = s_code;
7759b50d902SRodney W. Grimes 	    *e_lab++ = ':';
7769b50d902SRodney W. Grimes 	    *e_lab++ = ' ';
7779b50d902SRodney W. Grimes 	    *e_lab = '\0';
7789b50d902SRodney W. Grimes 
7799b50d902SRodney W. Grimes 	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by
7809b50d902SRodney W. Grimes 						 * dump_line to decide how to
7819b50d902SRodney W. Grimes 						 * indent the label. force_nl
7829b50d902SRodney W. Grimes 						 * will force a case n: to be
7839b50d902SRodney W. Grimes 						 * on a line by itself */
7849b50d902SRodney W. Grimes 	    scase = false;
7859b50d902SRodney W. Grimes 	    ps.want_blank = false;
7869b50d902SRodney W. Grimes 	    break;
7879b50d902SRodney W. Grimes 
7889b50d902SRodney W. Grimes 	case semicolon:	/* got a ';' */
7899b4009b4SPedro F. Giffuni 	    if (ps.dec_nest == 0)
7909b4009b4SPedro F. Giffuni 		ps.in_or_st = false;/* we are not in an initialization or
7919b4009b4SPedro F. Giffuni 				     * structure declaration */
7929d5abbddSJens Schweikhardt 	    scase = false;	/* these will only need resetting in an error */
7939b50d902SRodney W. Grimes 	    squest = 0;
794a3abcad0SPiotr Pawel Stefaniak 	    if (ps.last_token == rparen)
7959b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
7969b50d902SRodney W. Grimes 	    ps.cast_mask = 0;
797b4939677SPedro F. Giffuni 	    ps.not_cast_mask = 0;
7989b50d902SRodney W. Grimes 	    ps.block_init = 0;
7999b50d902SRodney W. Grimes 	    ps.block_init_level = 0;
8009b50d902SRodney W. Grimes 	    ps.just_saw_decl--;
8019b50d902SRodney W. Grimes 
80211c49893SPedro F. Giffuni 	    if (ps.in_decl && s_code == e_code && !ps.block_init &&
8039d4264fbSPiotr Pawel Stefaniak 		!ps.dumped_decl_indent && ps.paren_level == 0) {
80411c49893SPedro F. Giffuni 		/* indent stray semicolons in declarations */
80511c49893SPedro F. Giffuni 		indent_declaration(dec_ind - 1, tabs_to_var);
80611c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
8079b50d902SRodney W. Grimes 	    }
8089b50d902SRodney W. Grimes 
8099b50d902SRodney W. Grimes 	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
8109b50d902SRodney W. Grimes 						 * structure declaration, we
8119b50d902SRodney W. Grimes 						 * arent any more */
8129b50d902SRodney W. Grimes 
8139b50d902SRodney W. Grimes 	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
8149b50d902SRodney W. Grimes 
8159b50d902SRodney W. Grimes 		/*
8169b50d902SRodney W. Grimes 		 * This should be true iff there were unbalanced parens in the
8179b50d902SRodney W. Grimes 		 * stmt.  It is a bit complicated, because the semicolon might
8189b50d902SRodney W. Grimes 		 * be in a for stmt
8199b50d902SRodney W. Grimes 		 */
8207916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
8219b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
8229d5abbddSJens Schweikhardt 		if (sp_sw) {	/* this is a check for an if, while, etc. with
8239b50d902SRodney W. Grimes 				 * unbalanced parens */
8249b50d902SRodney W. Grimes 		    sp_sw = false;
8259b50d902SRodney W. Grimes 		    parse(hd_type);	/* dont lose the if, or whatever */
8269b50d902SRodney W. Grimes 		}
8279b50d902SRodney W. Grimes 	    }
8289b50d902SRodney W. Grimes 	    *e_code++ = ';';
8299b50d902SRodney W. Grimes 	    ps.want_blank = true;
8309b50d902SRodney W. Grimes 	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
8319b50d902SRodney W. Grimes 						 * middle of a stmt */
8329b50d902SRodney W. Grimes 
8339b50d902SRodney W. Grimes 	    if (!sp_sw) {	/* if not if for (;;) */
8349b50d902SRodney W. Grimes 		parse(semicolon);	/* let parser know about end of stmt */
8359d5abbddSJens Schweikhardt 		force_nl = true;/* force newline after an end of stmt */
8369b50d902SRodney W. Grimes 	    }
8379b50d902SRodney W. Grimes 	    break;
8389b50d902SRodney W. Grimes 
8399b50d902SRodney W. Grimes 	case lbrace:		/* got a '{' */
8409b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* dont indent the {} */
8419b50d902SRodney W. Grimes 	    if (!ps.block_init)
8429b50d902SRodney W. Grimes 		force_nl = true;/* force other stuff on same line as '{' onto
8439b50d902SRodney W. Grimes 				 * new line */
8449b50d902SRodney W. Grimes 	    else if (ps.block_init_level <= 0)
8459b50d902SRodney W. Grimes 		ps.block_init_level = 1;
8469b50d902SRodney W. Grimes 	    else
8479b50d902SRodney W. Grimes 		ps.block_init_level++;
8489b50d902SRodney W. Grimes 
8499b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {
8509b50d902SRodney W. Grimes 		if (!btype_2) {
8519b50d902SRodney W. Grimes 		    dump_line();
8529b50d902SRodney W. Grimes 		    ps.want_blank = false;
8539b50d902SRodney W. Grimes 		}
8549b50d902SRodney W. Grimes 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
8559b50d902SRodney W. Grimes 		    ps.i_l_follow = 0;
856e3625e9cSJens Schweikhardt 		    if (function_brace_split) {	/* dump the line prior to the
857e3625e9cSJens Schweikhardt 						 * brace ... */
8589b50d902SRodney W. Grimes 			dump_line();
8599b50d902SRodney W. Grimes 			ps.want_blank = false;
860e3625e9cSJens Schweikhardt 		    } else	/* add a space between the decl and brace */
861e3625e9cSJens Schweikhardt 			ps.want_blank = true;
8629b50d902SRodney W. Grimes 		}
8639b50d902SRodney W. Grimes 	    }
8649b50d902SRodney W. Grimes 	    if (ps.in_parameter_declaration)
8659b50d902SRodney W. Grimes 		prefix_blankline_requested = 0;
8669b50d902SRodney W. Grimes 
867d7d97eb0SJeroen Ruigrok van der Werven 	    if (ps.p_l_follow > 0) {	/* check for preceding unbalanced
8689b50d902SRodney W. Grimes 					 * parens */
8697916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
8709b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
8719b50d902SRodney W. Grimes 		if (sp_sw) {	/* check for unclosed if, for, etc. */
8729b50d902SRodney W. Grimes 		    sp_sw = false;
8739b50d902SRodney W. Grimes 		    parse(hd_type);
8749b50d902SRodney W. Grimes 		    ps.ind_level = ps.i_l_follow;
8759b50d902SRodney W. Grimes 		}
8769b50d902SRodney W. Grimes 	    }
8779b50d902SRodney W. Grimes 	    if (s_code == e_code)
8789b50d902SRodney W. Grimes 		ps.ind_stmt = false;	/* dont put extra indentation on line
8799b50d902SRodney W. Grimes 					 * with '{' */
8809b50d902SRodney W. Grimes 	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
8819b50d902SRodney W. Grimes 						 * declaration or an init */
8829b50d902SRodney W. Grimes 		di_stack[ps.dec_nest++] = dec_ind;
8839b50d902SRodney W. Grimes 		/* ?		dec_ind = 0; */
8849b50d902SRodney W. Grimes 	    }
8859b50d902SRodney W. Grimes 	    else {
886487ac9acSUlrich Spörlein 		ps.decl_on_line = false;	/* we can't be in the middle of
887487ac9acSUlrich Spörlein 						 * a declaration, so don't do
8889b50d902SRodney W. Grimes 						 * special indentation of
8899b50d902SRodney W. Grimes 						 * comments */
8909b50d902SRodney W. Grimes 		if (blanklines_after_declarations_at_proctop
8919b50d902SRodney W. Grimes 			&& ps.in_parameter_declaration)
8929b50d902SRodney W. Grimes 		    postfix_blankline_requested = 1;
8939b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
894a3abcad0SPiotr Pawel Stefaniak 		ps.in_decl = false;
8959b50d902SRodney W. Grimes 	    }
8969b50d902SRodney W. Grimes 	    dec_ind = 0;
8979b50d902SRodney W. Grimes 	    parse(lbrace);	/* let parser know about this */
8989b50d902SRodney W. Grimes 	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at
8999b50d902SRodney W. Grimes 				 * start of line */
9009b50d902SRodney W. Grimes 		*e_code++ = ' ';
9019b50d902SRodney W. Grimes 	    ps.want_blank = false;
9029b50d902SRodney W. Grimes 	    *e_code++ = '{';
9039b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
9049b50d902SRodney W. Grimes 	    break;
9059b50d902SRodney W. Grimes 
9069b50d902SRodney W. Grimes 	case rbrace:		/* got a '}' */
9079b50d902SRodney W. Grimes 	    if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
9089b50d902SRodney W. Grimes 								 * omitted in
9099b50d902SRodney W. Grimes 								 * declarations */
9109b50d902SRodney W. Grimes 		parse(semicolon);
9119b50d902SRodney W. Grimes 	    if (ps.p_l_follow) {/* check for unclosed if, for, else. */
9127916863dSJens Schweikhardt 		diag2(1, "Unbalanced parens");
9139b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
9149b50d902SRodney W. Grimes 		sp_sw = false;
9159b50d902SRodney W. Grimes 	    }
9169b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
9179b50d902SRodney W. Grimes 	    ps.block_init_level--;
9189b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
9199b50d902SRodney W. Grimes 							 * line */
9209b50d902SRodney W. Grimes 		if (verbose)
9217916863dSJens Schweikhardt 		    diag2(0, "Line broken");
9229b50d902SRodney W. Grimes 		dump_line();
9239b50d902SRodney W. Grimes 	    }
9249b50d902SRodney W. Grimes 	    *e_code++ = '}';
9259b50d902SRodney W. Grimes 	    ps.want_blank = true;
9269b50d902SRodney W. Grimes 	    ps.in_stmt = ps.ind_stmt = false;
9279b50d902SRodney W. Grimes 	    if (ps.dec_nest > 0) {	/* we are in multi-level structure
9289b50d902SRodney W. Grimes 					 * declaration */
9299b50d902SRodney W. Grimes 		dec_ind = di_stack[--ps.dec_nest];
9309b50d902SRodney W. Grimes 		if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
9319b50d902SRodney W. Grimes 		    ps.just_saw_decl = 2;
9329b50d902SRodney W. Grimes 		ps.in_decl = true;
9339b50d902SRodney W. Grimes 	    }
9349b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
9359b50d902SRodney W. Grimes 	    parse(rbrace);	/* let parser know about this */
9369b50d902SRodney W. Grimes 	    ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
9379b50d902SRodney W. Grimes 		&& ps.il[ps.tos] >= ps.ind_level;
9389b50d902SRodney W. Grimes 	    if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
9399b50d902SRodney W. Grimes 		postfix_blankline_requested = 1;
9409b50d902SRodney W. Grimes 	    break;
9419b50d902SRodney W. Grimes 
9429b50d902SRodney W. Grimes 	case swstmt:		/* got keyword "switch" */
9439b50d902SRodney W. Grimes 	    sp_sw = true;
9449b50d902SRodney W. Grimes 	    hd_type = swstmt;	/* keep this for when we have seen the
9459b50d902SRodney W. Grimes 				 * expression */
9469b50d902SRodney W. Grimes 	    goto copy_id;	/* go move the token into buffer */
9479b50d902SRodney W. Grimes 
9489b50d902SRodney W. Grimes 	case sp_paren:		/* token is if, while, for */
9499b50d902SRodney W. Grimes 	    sp_sw = true;	/* the interesting stuff is done after the
9509b50d902SRodney W. Grimes 				 * expression is scanned */
9519b50d902SRodney W. Grimes 	    hd_type = (*token == 'i' ? ifstmt :
9529b50d902SRodney W. Grimes 		       (*token == 'w' ? whilestmt : forstmt));
9539b50d902SRodney W. Grimes 
9549b50d902SRodney W. Grimes 	    /*
9559b50d902SRodney W. Grimes 	     * remember the type of header for later use by parser
9569b50d902SRodney W. Grimes 	     */
9579b50d902SRodney W. Grimes 	    goto copy_id;	/* copy the token into line */
9589b50d902SRodney W. Grimes 
9599b50d902SRodney W. Grimes 	case sp_nparen:	/* got else, do */
9609b50d902SRodney W. Grimes 	    ps.in_stmt = false;
9619b50d902SRodney W. Grimes 	    if (*token == 'e') {
9629b50d902SRodney W. Grimes 		if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
9639b50d902SRodney W. Grimes 		    if (verbose)
9647916863dSJens Schweikhardt 			diag2(0, "Line broken");
9659b50d902SRodney W. Grimes 		    dump_line();/* make sure this starts a line */
9669b50d902SRodney W. Grimes 		    ps.want_blank = false;
9679b50d902SRodney W. Grimes 		}
9689b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
9699b50d902SRodney W. Grimes 		last_else = 1;
9709b50d902SRodney W. Grimes 		parse(elselit);
9719b50d902SRodney W. Grimes 	    }
9729b50d902SRodney W. Grimes 	    else {
9739b50d902SRodney W. Grimes 		if (e_code != s_code) {	/* make sure this starts a line */
9749b50d902SRodney W. Grimes 		    if (verbose)
9757916863dSJens Schweikhardt 			diag2(0, "Line broken");
9769b50d902SRodney W. Grimes 		    dump_line();
9779b50d902SRodney W. Grimes 		    ps.want_blank = false;
9789b50d902SRodney W. Grimes 		}
9799b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
9809b50d902SRodney W. Grimes 		last_else = 0;
9819b50d902SRodney W. Grimes 		parse(dolit);
9829b50d902SRodney W. Grimes 	    }
9839b50d902SRodney W. Grimes 	    goto copy_id;	/* move the token into line */
9849b50d902SRodney W. Grimes 
9853bbaa755SPiotr Pawel Stefaniak 	case type_def:
986f171328eSPedro F. Giffuni 	case storage:
987f171328eSPedro F. Giffuni 	    prefix_blankline_requested = 0;
988f171328eSPedro F. Giffuni 	    goto copy_id;
989f171328eSPedro F. Giffuni 
990f171328eSPedro F. Giffuni 	case decl:		/* we have a declaration type (int, etc.) */
9919b50d902SRodney W. Grimes 	    parse(decl);	/* let parser worry about indentation */
9929b50d902SRodney W. Grimes 	    if (ps.last_token == rparen && ps.tos <= 1) {
9939b50d902SRodney W. Grimes 		if (s_code != e_code) {
9949b50d902SRodney W. Grimes 		    dump_line();
9959b50d902SRodney W. Grimes 		    ps.want_blank = 0;
9969b50d902SRodney W. Grimes 		}
9979b50d902SRodney W. Grimes 	    }
9989b50d902SRodney W. Grimes 	    if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
9999b50d902SRodney W. Grimes 		ps.ind_level = ps.i_l_follow = 1;
10009b50d902SRodney W. Grimes 		ps.ind_stmt = 0;
10019b50d902SRodney W. Grimes 	    }
10029b50d902SRodney W. Grimes 	    ps.in_or_st = true;	/* this might be a structure or initialization
10039b50d902SRodney W. Grimes 				 * declaration */
10043bbaa755SPiotr Pawel Stefaniak 	    ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
10059b50d902SRodney W. Grimes 	    if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
10069b50d902SRodney W. Grimes 		ps.just_saw_decl = 2;
10079b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
10089b50d902SRodney W. Grimes 	    for (i = 0; token[i++];);	/* get length of token */
10099b50d902SRodney W. Grimes 
101088ce0e7fSBruce Evans 	    if (ps.ind_level == 0 || ps.dec_nest > 0) {
101188ce0e7fSBruce Evans 		/* global variable or struct member in local variable */
1012eebee5a7SBruce Evans 		dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
1013e3625e9cSJens Schweikhardt 		tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
101488ce0e7fSBruce Evans 	    } else {
101588ce0e7fSBruce Evans 		/* local variable */
101688ce0e7fSBruce Evans 		dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
1017e3625e9cSJens Schweikhardt 		tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
101888ce0e7fSBruce Evans 	    }
10199b50d902SRodney W. Grimes 	    goto copy_id;
10209b50d902SRodney W. Grimes 
1021a3abcad0SPiotr Pawel Stefaniak 	case funcname:
10229b50d902SRodney W. Grimes 	case ident:		/* got an identifier or constant */
10239b50d902SRodney W. Grimes 	    if (ps.in_decl) {	/* if we are in a declaration, we must indent
10249b50d902SRodney W. Grimes 				 * identifier */
1025a3abcad0SPiotr Pawel Stefaniak 		if (type_code != funcname || !procnames_start_line) {
10269d4264fbSPiotr Pawel Stefaniak 		    if (!ps.block_init && !ps.dumped_decl_indent && ps.paren_level == 0) {
102711c49893SPedro F. Giffuni 			if (troff) {
1028e024a090SBruce Evans 			    if (ps.want_blank)
1029e024a090SBruce Evans 				*e_code++ = ' ';
10309b50d902SRodney W. Grimes 			    sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
10319b50d902SRodney W. Grimes 			    e_code += strlen(e_code);
103211c49893SPedro F. Giffuni 			} else
103311c49893SPedro F. Giffuni 			    indent_declaration(dec_ind, tabs_to_var);
103411c49893SPedro F. Giffuni 			ps.dumped_decl_indent = true;
1035e024a090SBruce Evans 			ps.want_blank = false;
10369b50d902SRodney W. Grimes 		    }
10377916863dSJens Schweikhardt 		} else {
1038a3abcad0SPiotr Pawel Stefaniak 		    if (ps.want_blank && !(procnames_start_line &&
1039a3abcad0SPiotr Pawel Stefaniak 			type_code == funcname))
1040e024a090SBruce Evans 			*e_code++ = ' ';
1041e024a090SBruce Evans 		    ps.want_blank = false;
104219fe172aSPedro F. Giffuni 		    if (dec_ind && s_code != e_code) {
104319fe172aSPedro F. Giffuni 			*e_code = '\0';
10449b50d902SRodney W. Grimes 			dump_line();
104519fe172aSPedro F. Giffuni 		    }
10469b50d902SRodney W. Grimes 		    dec_ind = 0;
10479b50d902SRodney W. Grimes 		}
10489b50d902SRodney W. Grimes 	    }
10499b50d902SRodney W. Grimes 	    else if (sp_sw && ps.p_l_follow == 0) {
10509b50d902SRodney W. Grimes 		sp_sw = false;
10519b50d902SRodney W. Grimes 		force_nl = true;
10529b50d902SRodney W. Grimes 		ps.last_u_d = true;
10539b50d902SRodney W. Grimes 		ps.in_stmt = false;
10549b50d902SRodney W. Grimes 		parse(hd_type);
10559b50d902SRodney W. Grimes 	    }
10569b50d902SRodney W. Grimes     copy_id:
10579b50d902SRodney W. Grimes 	    if (ps.want_blank)
10589b50d902SRodney W. Grimes 		*e_code++ = ' ';
1059b4939677SPedro F. Giffuni 	    if (troff && ps.keyword) {
10609b50d902SRodney W. Grimes 		e_code = chfont(&bodyf, &keywordf, e_code);
10619b50d902SRodney W. Grimes 		for (t_ptr = token; *t_ptr; ++t_ptr) {
10629b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
10639b50d902SRodney W. Grimes 		    *e_code++ = keywordf.allcaps && islower(*t_ptr)
10649b50d902SRodney W. Grimes 			? toupper(*t_ptr) : *t_ptr;
10659b50d902SRodney W. Grimes 		}
10669b50d902SRodney W. Grimes 		e_code = chfont(&keywordf, &bodyf, e_code);
10679b50d902SRodney W. Grimes 	    }
10689b50d902SRodney W. Grimes 	    else
10699b50d902SRodney W. Grimes 		for (t_ptr = token; *t_ptr; ++t_ptr) {
10709b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
10719b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
10729b50d902SRodney W. Grimes 		}
1073a3abcad0SPiotr Pawel Stefaniak 	    if (type_code != funcname)
10749b50d902SRodney W. Grimes 		ps.want_blank = true;
10759b50d902SRodney W. Grimes 	    break;
10769b50d902SRodney W. Grimes 
1077350fcdd5SPedro F. Giffuni 	case strpfx:
1078350fcdd5SPedro F. Giffuni 	    if (ps.want_blank)
1079350fcdd5SPedro F. Giffuni 		*e_code++ = ' ';
1080350fcdd5SPedro F. Giffuni 	    for (t_ptr = token; *t_ptr; ++t_ptr) {
1081350fcdd5SPedro F. Giffuni 		CHECK_SIZE_CODE;
1082350fcdd5SPedro F. Giffuni 		*e_code++ = *t_ptr;
1083350fcdd5SPedro F. Giffuni 	    }
1084350fcdd5SPedro F. Giffuni 	    ps.want_blank = false;
1085350fcdd5SPedro F. Giffuni 	    break;
1086350fcdd5SPedro F. Giffuni 
10879b50d902SRodney W. Grimes 	case period:		/* treat a period kind of like a binary
10889b50d902SRodney W. Grimes 				 * operation */
10899b50d902SRodney W. Grimes 	    *e_code++ = '.';	/* move the period into line */
10909b50d902SRodney W. Grimes 	    ps.want_blank = false;	/* dont put a blank after a period */
10919b50d902SRodney W. Grimes 	    break;
10929b50d902SRodney W. Grimes 
10939b50d902SRodney W. Grimes 	case comma:
10949b50d902SRodney W. Grimes 	    ps.want_blank = (s_code != e_code);	/* only put blank after comma
10959b50d902SRodney W. Grimes 						 * if comma does not start the
10969b50d902SRodney W. Grimes 						 * line */
109711c49893SPedro F. Giffuni 	    if (ps.in_decl && is_procname == 0 && !ps.block_init &&
10989d4264fbSPiotr Pawel Stefaniak 		!ps.dumped_decl_indent && ps.paren_level == 0) {
109911c49893SPedro F. Giffuni 		/* indent leading commas and not the actual identifiers */
110011c49893SPedro F. Giffuni 		indent_declaration(dec_ind - 1, tabs_to_var);
110111c49893SPedro F. Giffuni 		ps.dumped_decl_indent = true;
11029b50d902SRodney W. Grimes 	    }
11039b50d902SRodney W. Grimes 	    *e_code++ = ',';
11049b50d902SRodney W. Grimes 	    if (ps.p_l_follow == 0) {
11059b50d902SRodney W. Grimes 		if (ps.block_init_level <= 0)
11069b50d902SRodney W. Grimes 		    ps.block_init = 0;
11075f35ea69SPiotr Pawel Stefaniak 		if (break_comma && (!ps.leave_comma ||
11085f35ea69SPiotr Pawel Stefaniak 		    count_spaces_until(compute_code_target(), s_code, e_code) >
11095f35ea69SPiotr Pawel Stefaniak 		    max_col - tabsize))
11109b50d902SRodney W. Grimes 		    force_nl = true;
11119b50d902SRodney W. Grimes 	    }
11129b50d902SRodney W. Grimes 	    break;
11139b50d902SRodney W. Grimes 
11149b50d902SRodney W. Grimes 	case preesc:		/* got the character '#' */
11159b50d902SRodney W. Grimes 	    if ((s_com != e_com) ||
11169b50d902SRodney W. Grimes 		    (s_lab != e_lab) ||
11179b50d902SRodney W. Grimes 		    (s_code != e_code))
11189b50d902SRodney W. Grimes 		dump_line();
11199b50d902SRodney W. Grimes 	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
11209b50d902SRodney W. Grimes 	    {
11219b50d902SRodney W. Grimes 		int         in_comment = 0;
11229b50d902SRodney W. Grimes 		int         com_start = 0;
11239b50d902SRodney W. Grimes 		char        quote = 0;
11249b50d902SRodney W. Grimes 		int         com_end = 0;
11259b50d902SRodney W. Grimes 
11269b50d902SRodney W. Grimes 		while (*buf_ptr == ' ' || *buf_ptr == '\t') {
11279b50d902SRodney W. Grimes 		    buf_ptr++;
11289b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
11299b50d902SRodney W. Grimes 			fill_buffer();
11309b50d902SRodney W. Grimes 		}
113152608c9fSDavid E. O'Brien 		while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
11329b50d902SRodney W. Grimes 		    CHECK_SIZE_LAB;
11339b50d902SRodney W. Grimes 		    *e_lab = *buf_ptr++;
11349b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
11359b50d902SRodney W. Grimes 			fill_buffer();
11369b50d902SRodney W. Grimes 		    switch (*e_lab++) {
11379b50d902SRodney W. Grimes 		    case BACKSLASH:
11389b50d902SRodney W. Grimes 			if (troff)
11399b50d902SRodney W. Grimes 			    *e_lab++ = BACKSLASH;
11409b50d902SRodney W. Grimes 			if (!in_comment) {
11419b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11429b50d902SRodney W. Grimes 			    if (buf_ptr >= buf_end)
11439b50d902SRodney W. Grimes 				fill_buffer();
11449b50d902SRodney W. Grimes 			}
11459b50d902SRodney W. Grimes 			break;
11469b50d902SRodney W. Grimes 		    case '/':
11479b50d902SRodney W. Grimes 			if (*buf_ptr == '*' && !in_comment && !quote) {
11489b50d902SRodney W. Grimes 			    in_comment = 1;
11499b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11509b50d902SRodney W. Grimes 			    com_start = e_lab - s_lab - 2;
11519b50d902SRodney W. Grimes 			}
11529b50d902SRodney W. Grimes 			break;
11539b50d902SRodney W. Grimes 		    case '"':
11549b50d902SRodney W. Grimes 			if (quote == '"')
11559b50d902SRodney W. Grimes 			    quote = 0;
11569b50d902SRodney W. Grimes 			break;
11579b50d902SRodney W. Grimes 		    case '\'':
11589b50d902SRodney W. Grimes 			if (quote == '\'')
11599b50d902SRodney W. Grimes 			    quote = 0;
11609b50d902SRodney W. Grimes 			break;
11619b50d902SRodney W. Grimes 		    case '*':
11629b50d902SRodney W. Grimes 			if (*buf_ptr == '/' && in_comment) {
11639b50d902SRodney W. Grimes 			    in_comment = 0;
11649b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
11659b50d902SRodney W. Grimes 			    com_end = e_lab - s_lab;
11669b50d902SRodney W. Grimes 			}
11679b50d902SRodney W. Grimes 			break;
11689b50d902SRodney W. Grimes 		    }
11699b50d902SRodney W. Grimes 		}
11709b50d902SRodney W. Grimes 
11719b50d902SRodney W. Grimes 		while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
11729b50d902SRodney W. Grimes 		    e_lab--;
1173c917a54bSPedro F. Giffuni 		if (e_lab - s_lab == com_end && bp_save == NULL) {
1174e643b783SPedro F. Giffuni 		    /* comment on preprocessor line */
1175c917a54bSPedro F. Giffuni 		    if (sc_end == NULL)	/* if this is the first comment, we
11769b50d902SRodney W. Grimes 					 * must set up the buffer */
11779b50d902SRodney W. Grimes 			sc_end = &(save_com[0]);
11789b50d902SRodney W. Grimes 		    else {
11799b50d902SRodney W. Grimes 			*sc_end++ = '\n';	/* add newline between
11809b50d902SRodney W. Grimes 						 * comments */
11819b50d902SRodney W. Grimes 			*sc_end++ = ' ';
11829b50d902SRodney W. Grimes 			--line_no;
11839b50d902SRodney W. Grimes 		    }
11849b50d902SRodney W. Grimes 		    bcopy(s_lab + com_start, sc_end, com_end - com_start);
11859b50d902SRodney W. Grimes 		    sc_end += com_end - com_start;
11869b50d902SRodney W. Grimes 		    if (sc_end >= &save_com[sc_size])
11879b50d902SRodney W. Grimes 			abort();
11889b50d902SRodney W. Grimes 		    e_lab = s_lab + com_start;
11899b50d902SRodney W. Grimes 		    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
11909b50d902SRodney W. Grimes 			e_lab--;
11919b50d902SRodney W. Grimes 		    bp_save = buf_ptr;	/* save current input buffer */
11929b50d902SRodney W. Grimes 		    be_save = buf_end;
11939b50d902SRodney W. Grimes 		    buf_ptr = save_com;	/* fix so that subsequent calls to
11949b50d902SRodney W. Grimes 					 * lexi will take tokens out of
11959b50d902SRodney W. Grimes 					 * save_com */
11969b50d902SRodney W. Grimes 		    *sc_end++ = ' ';	/* add trailing blank, just in case */
11979b50d902SRodney W. Grimes 		    buf_end = sc_end;
1198c917a54bSPedro F. Giffuni 		    sc_end = NULL;
11999b50d902SRodney W. Grimes 		}
12009b50d902SRodney W. Grimes 		*e_lab = '\0';	/* null terminate line */
12019b50d902SRodney W. Grimes 		ps.pcase = false;
12029b50d902SRodney W. Grimes 	    }
12039b50d902SRodney W. Grimes 
1204f7adee23SPedro F. Giffuni 	    if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
12056cf1bae2SMarcelo Araujo 		if ((size_t)ifdef_level < nitems(state_stack)) {
12069b50d902SRodney W. Grimes 		    match_state[ifdef_level].tos = -1;
12079b50d902SRodney W. Grimes 		    state_stack[ifdef_level++] = ps;
12089b50d902SRodney W. Grimes 		}
12099b50d902SRodney W. Grimes 		else
12107916863dSJens Schweikhardt 		    diag2(1, "#if stack overflow");
12119b50d902SRodney W. Grimes 	    }
1212f7adee23SPedro F. Giffuni 	    else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
12139b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
1214f7adee23SPedro F. Giffuni 		    diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
12159b50d902SRodney W. Grimes 		else {
12169b50d902SRodney W. Grimes 		    match_state[ifdef_level - 1] = ps;
12179b50d902SRodney W. Grimes 		    ps = state_stack[ifdef_level - 1];
12189b50d902SRodney W. Grimes 		}
1219f7adee23SPedro F. Giffuni 	    }
12209b50d902SRodney W. Grimes 	    else if (strncmp(s_lab, "#endif", 6) == 0) {
12219b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
12227916863dSJens Schweikhardt 		    diag2(1, "Unmatched #endif");
1223f7adee23SPedro F. Giffuni 		else
12249b50d902SRodney W. Grimes 		    ifdef_level--;
1225f7adee23SPedro F. Giffuni 	    } else {
1226f7adee23SPedro F. Giffuni 		struct directives {
1227f7adee23SPedro F. Giffuni 		    int size;
1228f7adee23SPedro F. Giffuni 		    const char *string;
1229f7adee23SPedro F. Giffuni 		}
1230f7adee23SPedro F. Giffuni 		recognized[] = {
1231f7adee23SPedro F. Giffuni 		    {7, "include"},
1232f7adee23SPedro F. Giffuni 		    {6, "define"},
1233f7adee23SPedro F. Giffuni 		    {5, "undef"},
1234f7adee23SPedro F. Giffuni 		    {4, "line"},
1235f7adee23SPedro F. Giffuni 		    {5, "error"},
1236f7adee23SPedro F. Giffuni 		    {6, "pragma"}
1237f7adee23SPedro F. Giffuni 		};
1238f7adee23SPedro F. Giffuni 		int d = nitems(recognized);
1239f7adee23SPedro F. Giffuni 		while (--d >= 0)
1240f7adee23SPedro F. Giffuni 		    if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
1241f7adee23SPedro F. Giffuni 			break;
1242f7adee23SPedro F. Giffuni 		if (d < 0) {
1243f7adee23SPedro F. Giffuni 		    diag2(1, "Unrecognized cpp directive");
1244f7adee23SPedro F. Giffuni 		    break;
1245f7adee23SPedro F. Giffuni 		}
12469b50d902SRodney W. Grimes 	    }
12479b50d902SRodney W. Grimes 	    if (blanklines_around_conditional_compilation) {
12489b50d902SRodney W. Grimes 		postfix_blankline_requested++;
12499b50d902SRodney W. Grimes 		n_real_blanklines = 0;
12509b50d902SRodney W. Grimes 	    }
1251f7adee23SPedro F. Giffuni 	    else {
1252f7adee23SPedro F. Giffuni 		postfix_blankline_requested = 0;
1253f7adee23SPedro F. Giffuni 		prefix_blankline_requested = 0;
12549b50d902SRodney W. Grimes 	    }
12559b50d902SRodney W. Grimes 	    break;		/* subsequent processing of the newline
12569b50d902SRodney W. Grimes 				 * character will cause the line to be printed */
12579b50d902SRodney W. Grimes 
12587916863dSJens Schweikhardt 	case comment:		/* we have gotten a / followed by * this is a biggie */
12599b50d902SRodney W. Grimes 	    pr_comment();
12609b50d902SRodney W. Grimes 	    break;
12619b50d902SRodney W. Grimes 	}			/* end of big switch stmt */
12629b50d902SRodney W. Grimes 
12639b50d902SRodney W. Grimes 	*e_code = '\0';		/* make sure code section is null terminated */
12649b50d902SRodney W. Grimes 	if (type_code != comment && type_code != newline && type_code != preesc)
12659b50d902SRodney W. Grimes 	    ps.last_token = type_code;
12669b50d902SRodney W. Grimes     }				/* end of main while (1) loop */
12679b50d902SRodney W. Grimes }
12689b50d902SRodney W. Grimes 
12699b50d902SRodney W. Grimes /*
12709b50d902SRodney W. Grimes  * copy input file to backup file if in_name is /blah/blah/blah/file, then
12719b50d902SRodney W. Grimes  * backup file will be ".Bfile" then make the backup file the input and
12729b50d902SRodney W. Grimes  * original input file the output
12739b50d902SRodney W. Grimes  */
12747916863dSJens Schweikhardt static void
12757916863dSJens Schweikhardt bakcopy(void)
12769b50d902SRodney W. Grimes {
12779b50d902SRodney W. Grimes     int         n,
12789b50d902SRodney W. Grimes                 bakchn;
12799b50d902SRodney W. Grimes     char        buff[8 * 1024];
12808c7e7698SDavid Malone     const char *p;
12819b50d902SRodney W. Grimes 
12829b50d902SRodney W. Grimes     /* construct file name .Bfile */
12839b50d902SRodney W. Grimes     for (p = in_name; *p; p++);	/* skip to end of string */
12849b50d902SRodney W. Grimes     while (p > in_name && *p != '/')	/* find last '/' */
12859b50d902SRodney W. Grimes 	p--;
12869b50d902SRodney W. Grimes     if (*p == '/')
12879b50d902SRodney W. Grimes 	p++;
1288c6841b07SKyle Evans     sprintf(bakfile, "%s%s", p, simple_backup_suffix);
12899b50d902SRodney W. Grimes 
12909b50d902SRodney W. Grimes     /* copy in_name to backup file */
12919b50d902SRodney W. Grimes     bakchn = creat(bakfile, 0600);
12929b50d902SRodney W. Grimes     if (bakchn < 0)
12930c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
12940504bd65SPedro F. Giffuni     while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
12959b50d902SRodney W. Grimes 	if (write(bakchn, buff, n) != n)
12960c4d24a7SKris Kennaway 	    err(1, "%s", bakfile);
12979b50d902SRodney W. Grimes     if (n < 0)
12980c4d24a7SKris Kennaway 	err(1, "%s", in_name);
12999b50d902SRodney W. Grimes     close(bakchn);
13009b50d902SRodney W. Grimes     fclose(input);
13019b50d902SRodney W. Grimes 
13029b50d902SRodney W. Grimes     /* re-open backup file as the input file */
13039b50d902SRodney W. Grimes     input = fopen(bakfile, "r");
1304f5f8c098SKevin Lo     if (input == NULL)
13050c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
13069b50d902SRodney W. Grimes     /* now the original input file will be the output */
13079b50d902SRodney W. Grimes     output = fopen(in_name, "w");
1308f5f8c098SKevin Lo     if (output == NULL) {
13099b50d902SRodney W. Grimes 	unlink(bakfile);
13100c4d24a7SKris Kennaway 	err(1, "%s", in_name);
13119b50d902SRodney W. Grimes     }
13129b50d902SRodney W. Grimes }
131311c49893SPedro F. Giffuni 
131411c49893SPedro F. Giffuni static void
131511c49893SPedro F. Giffuni indent_declaration(int cur_dec_ind, int tabs_to_var)
131611c49893SPedro F. Giffuni {
131711c49893SPedro F. Giffuni     int pos = e_code - s_code;
131811c49893SPedro F. Giffuni     char *startpos = e_code;
131911c49893SPedro F. Giffuni 
132011c49893SPedro F. Giffuni     /*
1321fbdbd284SPiotr Pawel Stefaniak      * get the tab math right for indentations that are not multiples of tabsize
132211c49893SPedro F. Giffuni      */
1323fbdbd284SPiotr Pawel Stefaniak     if ((ps.ind_level * ps.ind_size) % tabsize != 0) {
1324fbdbd284SPiotr Pawel Stefaniak 	pos += (ps.ind_level * ps.ind_size) % tabsize;
1325fbdbd284SPiotr Pawel Stefaniak 	cur_dec_ind += (ps.ind_level * ps.ind_size) % tabsize;
132611c49893SPedro F. Giffuni     }
1327fbdbd284SPiotr Pawel Stefaniak     if (tabs_to_var) {
1328fbdbd284SPiotr Pawel Stefaniak 	int tpos;
1329fbdbd284SPiotr Pawel Stefaniak 
1330fbdbd284SPiotr Pawel Stefaniak 	while ((tpos = tabsize * (1 + pos / tabsize)) <= cur_dec_ind) {
133111c49893SPedro F. Giffuni 	    CHECK_SIZE_CODE;
133211c49893SPedro F. Giffuni 	    *e_code++ = '\t';
1333fbdbd284SPiotr Pawel Stefaniak 	    pos = tpos;
1334fbdbd284SPiotr Pawel Stefaniak 	}
133511c49893SPedro F. Giffuni     }
133611c49893SPedro F. Giffuni     while (pos < cur_dec_ind) {
133711c49893SPedro F. Giffuni 	CHECK_SIZE_CODE;
133811c49893SPedro F. Giffuni 	*e_code++ = ' ';
133911c49893SPedro F. Giffuni 	pos++;
134011c49893SPedro F. Giffuni     }
134111c49893SPedro F. Giffuni     if (e_code == startpos && ps.want_blank) {
134211c49893SPedro F. Giffuni 	*e_code++ = ' ';
134311c49893SPedro F. Giffuni 	ps.want_blank = false;
134411c49893SPedro F. Giffuni     }
134511c49893SPedro F. Giffuni }
1346