xref: /minix3/usr.bin/indent/indent.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: indent.c,v 1.19 2014/09/04 04:06:07 mrg Exp $	*/
297c7d358SVivek Prakash 
397c7d358SVivek Prakash /*
497c7d358SVivek Prakash  * Copyright (c) 1980, 1993
597c7d358SVivek Prakash  *	The Regents of the University of California.  All rights reserved.
697c7d358SVivek Prakash  *
797c7d358SVivek Prakash  * Redistribution and use in source and binary forms, with or without
897c7d358SVivek Prakash  * modification, are permitted provided that the following conditions
997c7d358SVivek Prakash  * are met:
1097c7d358SVivek Prakash  * 1. Redistributions of source code must retain the above copyright
1197c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer.
1297c7d358SVivek Prakash  * 2. Redistributions in binary form must reproduce the above copyright
1397c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer in the
1497c7d358SVivek Prakash  *    documentation and/or other materials provided with the distribution.
1597c7d358SVivek Prakash  * 3. Neither the name of the University nor the names of its contributors
1697c7d358SVivek Prakash  *    may be used to endorse or promote products derived from this software
1797c7d358SVivek Prakash  *    without specific prior written permission.
1897c7d358SVivek Prakash  *
1997c7d358SVivek Prakash  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2097c7d358SVivek Prakash  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2197c7d358SVivek Prakash  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2297c7d358SVivek Prakash  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2397c7d358SVivek Prakash  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2497c7d358SVivek Prakash  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2597c7d358SVivek Prakash  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2697c7d358SVivek Prakash  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2797c7d358SVivek Prakash  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2897c7d358SVivek Prakash  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2997c7d358SVivek Prakash  * SUCH DAMAGE.
3097c7d358SVivek Prakash  */
3197c7d358SVivek Prakash 
3297c7d358SVivek Prakash /*
3397c7d358SVivek Prakash  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
3497c7d358SVivek Prakash  * Copyright (c) 1985 Sun Microsystems, Inc.
3597c7d358SVivek Prakash  * All rights reserved.
3697c7d358SVivek Prakash  *
3797c7d358SVivek Prakash  * Redistribution and use in source and binary forms, with or without
3897c7d358SVivek Prakash  * modification, are permitted provided that the following conditions
3997c7d358SVivek Prakash  * are met:
4097c7d358SVivek Prakash  * 1. Redistributions of source code must retain the above copyright
4197c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer.
4297c7d358SVivek Prakash  * 2. Redistributions in binary form must reproduce the above copyright
4397c7d358SVivek Prakash  *    notice, this list of conditions and the following disclaimer in the
4497c7d358SVivek Prakash  *    documentation and/or other materials provided with the distribution.
4597c7d358SVivek Prakash  * 3. All advertising materials mentioning features or use of this software
4697c7d358SVivek Prakash  *    must display the following acknowledgement:
4797c7d358SVivek Prakash  *	This product includes software developed by the University of
4897c7d358SVivek Prakash  *	California, Berkeley and its contributors.
4997c7d358SVivek Prakash  * 4. Neither the name of the University nor the names of its contributors
5097c7d358SVivek Prakash  *    may be used to endorse or promote products derived from this software
5197c7d358SVivek Prakash  *    without specific prior written permission.
5297c7d358SVivek Prakash  *
5397c7d358SVivek Prakash  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5497c7d358SVivek Prakash  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5597c7d358SVivek Prakash  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5697c7d358SVivek Prakash  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5797c7d358SVivek Prakash  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5897c7d358SVivek Prakash  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5997c7d358SVivek Prakash  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6097c7d358SVivek Prakash  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6197c7d358SVivek Prakash  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6297c7d358SVivek Prakash  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6397c7d358SVivek Prakash  * SUCH DAMAGE.
6497c7d358SVivek Prakash  */
6597c7d358SVivek Prakash 
6697c7d358SVivek Prakash #include <sys/cdefs.h>
6797c7d358SVivek Prakash #ifndef lint
6897c7d358SVivek Prakash __COPYRIGHT("@(#) Copyright (c) 1985 Sun Microsystems, Inc.\
6997c7d358SVivek Prakash   Copyright (c) 1976 Board of Trustees of the University of Illinois.\
7097c7d358SVivek Prakash   Copyright (c) 1980, 1993\
7197c7d358SVivek Prakash  The Regents of the University of California.  All rights reserved.");
7297c7d358SVivek Prakash #endif				/* not lint */
7397c7d358SVivek Prakash 
7497c7d358SVivek Prakash #ifndef lint
7597c7d358SVivek Prakash #if 0
7697c7d358SVivek Prakash static char sccsid[] = "@(#)indent.c	5.17 (Berkeley) 6/7/93";
7797c7d358SVivek Prakash #else
78*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: indent.c,v 1.19 2014/09/04 04:06:07 mrg Exp $");
7997c7d358SVivek Prakash #endif
8097c7d358SVivek Prakash #endif				/* not lint */
8197c7d358SVivek Prakash 
8297c7d358SVivek Prakash #include <sys/param.h>
8397c7d358SVivek Prakash #include <ctype.h>
8497c7d358SVivek Prakash #include <err.h>
8597c7d358SVivek Prakash #include <errno.h>
8697c7d358SVivek Prakash #include <fcntl.h>
8797c7d358SVivek Prakash #include <stdio.h>
8897c7d358SVivek Prakash #include <stdlib.h>
8997c7d358SVivek Prakash #include <string.h>
9097c7d358SVivek Prakash #include <unistd.h>
9197c7d358SVivek Prakash #include <locale.h>
9297c7d358SVivek Prakash #define EXTERN
9397c7d358SVivek Prakash #include "indent_globs.h"
9497c7d358SVivek Prakash #undef  EXTERN
9597c7d358SVivek Prakash #include "indent_codes.h"
9697c7d358SVivek Prakash 
9797c7d358SVivek Prakash const char *in_name = "Standard Input";		/* will always point to name of
9897c7d358SVivek Prakash 						 * input file */
9997c7d358SVivek Prakash const char *out_name = "Standard Output";	/* will always point to name of
10097c7d358SVivek Prakash 						 * output file */
10197c7d358SVivek Prakash char    bakfile[MAXPATHLEN] = "";
10297c7d358SVivek Prakash 
10397c7d358SVivek Prakash int main(int, char **);
10497c7d358SVivek Prakash 
10597c7d358SVivek Prakash int
main(int argc,char ** argv)10697c7d358SVivek Prakash main(int argc, char **argv)
10797c7d358SVivek Prakash {
10897c7d358SVivek Prakash 
10997c7d358SVivek Prakash 	extern int found_err;	/* flag set in diag() on error */
11097c7d358SVivek Prakash 	int     dec_ind;	/* current indentation for declarations */
11197c7d358SVivek Prakash 	int     di_stack[20];	/* a stack of structure indentation levels */
11297c7d358SVivek Prakash 	int     flushed_nl;	/* used when buffering up comments to remember
11397c7d358SVivek Prakash 				 * that a newline was passed over */
11497c7d358SVivek Prakash 	int     force_nl;	/* when true, code must be broken */
11597c7d358SVivek Prakash 	int     hd_type;	/* used to store type of stmt for if (...),
11697c7d358SVivek Prakash 				 * for (...), etc */
11797c7d358SVivek Prakash 	int     i;		/* local loop counter */
11897c7d358SVivek Prakash 	int     scase;		/* set to true when we see a case, so we will
11997c7d358SVivek Prakash 				 * know what to do with the following colon */
12097c7d358SVivek Prakash 	int     sp_sw;		/* when true, we are in the expressin of
12197c7d358SVivek Prakash 				 * if(...), while(...), etc. */
12297c7d358SVivek Prakash 	int     squest;		/* when this is positive, we have seen a ?
12397c7d358SVivek Prakash 				 * without the matching : in a <c>?<s>:<s>
12497c7d358SVivek Prakash 				 * construct */
12597c7d358SVivek Prakash 	const char *t_ptr;	/* used for copying tokens */
126*0a6a1f1dSLionel Sambuc 	int	tabs_to_var = 0; /* true if using tabs to indent to var name */
12797c7d358SVivek Prakash 	int     type_code;	/* the type of token, returned by lexi */
12897c7d358SVivek Prakash 
12997c7d358SVivek Prakash 	int     last_else = 0;	/* true iff last keyword was an else */
13097c7d358SVivek Prakash 
13197c7d358SVivek Prakash 
13297c7d358SVivek Prakash 	/*-----------------------------------------------*\
13397c7d358SVivek Prakash         |		      INITIALIZATION		      |
13497c7d358SVivek Prakash         \*-----------------------------------------------*/
13597c7d358SVivek Prakash 
13697c7d358SVivek Prakash 	if (!setlocale(LC_ALL, ""))
13797c7d358SVivek Prakash 		fprintf(stderr, "indent: can't set locale.\n");
13897c7d358SVivek Prakash 
13997c7d358SVivek Prakash 	hd_type = 0;
14097c7d358SVivek Prakash 	ps.p_stack[0] = stmt;	/* this is the parser's stack */
14197c7d358SVivek Prakash 	ps.last_nl = true;	/* this is true if the last thing scanned was
14297c7d358SVivek Prakash 				 * a newline */
14397c7d358SVivek Prakash 	ps.last_token = semicolon;
14497c7d358SVivek Prakash 	combuf = (char *) malloc(bufsize);
14597c7d358SVivek Prakash 	labbuf = (char *) malloc(bufsize);
14697c7d358SVivek Prakash 	codebuf = (char *) malloc(bufsize);
14797c7d358SVivek Prakash 	tokenbuf = (char *) malloc(bufsize);
14897c7d358SVivek Prakash 	l_com = combuf + bufsize - 5;
14997c7d358SVivek Prakash 	l_lab = labbuf + bufsize - 5;
15097c7d358SVivek Prakash 	l_code = codebuf + bufsize - 5;
15197c7d358SVivek Prakash 	l_token = tokenbuf + bufsize - 5;
15297c7d358SVivek Prakash 	combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label,
15397c7d358SVivek Prakash 							 * and comment buffers */
15497c7d358SVivek Prakash 	combuf[1] = codebuf[1] = labbuf[1] = '\0';
15597c7d358SVivek Prakash 	ps.else_if = 1;		/* Default else-if special processing to on */
15697c7d358SVivek Prakash 	s_lab = e_lab = labbuf + 1;
15797c7d358SVivek Prakash 	s_code = e_code = codebuf + 1;
15897c7d358SVivek Prakash 	s_com = e_com = combuf + 1;
15997c7d358SVivek Prakash 	s_token = e_token = tokenbuf + 1;
16097c7d358SVivek Prakash 
16197c7d358SVivek Prakash 	in_buffer = (char *) malloc(10);
16297c7d358SVivek Prakash 	in_buffer_limit = in_buffer + 8;
16397c7d358SVivek Prakash 	buf_ptr = buf_end = in_buffer;
16497c7d358SVivek Prakash 	line_no = 1;
16597c7d358SVivek Prakash 	had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
16697c7d358SVivek Prakash 	sp_sw = force_nl = false;
16797c7d358SVivek Prakash 	ps.in_or_st = false;
16897c7d358SVivek Prakash 	ps.bl_line = true;
16997c7d358SVivek Prakash 	dec_ind = 0;
17097c7d358SVivek Prakash 	di_stack[ps.dec_nest = 0] = 0;
17197c7d358SVivek Prakash 	ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
17297c7d358SVivek Prakash 
17397c7d358SVivek Prakash 
17497c7d358SVivek Prakash 	scase = ps.pcase = false;
17597c7d358SVivek Prakash 	squest = 0;
17697c7d358SVivek Prakash 	sc_end = 0;
17797c7d358SVivek Prakash 	bp_save = 0;
17897c7d358SVivek Prakash 	be_save = 0;
17997c7d358SVivek Prakash 
18097c7d358SVivek Prakash 	output = 0;
18197c7d358SVivek Prakash 
18297c7d358SVivek Prakash 
18397c7d358SVivek Prakash 
18497c7d358SVivek Prakash 	/*--------------------------------------------------*\
18597c7d358SVivek Prakash         |   		COMMAND LINE SCAN		 |
18697c7d358SVivek Prakash         \*--------------------------------------------------*/
18797c7d358SVivek Prakash 
18897c7d358SVivek Prakash #ifdef undef
18997c7d358SVivek Prakash 	max_col = 78;		/* -l78 */
19097c7d358SVivek Prakash 	lineup_to_parens = 1;	/* -lp */
19197c7d358SVivek Prakash 	ps.ljust_decl = 0;	/* -ndj */
19297c7d358SVivek Prakash 	ps.com_ind = 33;	/* -c33 */
19397c7d358SVivek Prakash 	star_comment_cont = 1;	/* -sc */
19497c7d358SVivek Prakash 	ps.ind_size = 8;	/* -i8 */
19597c7d358SVivek Prakash 	verbose = 0;
19697c7d358SVivek Prakash 	ps.decl_indent = 16;	/* -di16 */
19797c7d358SVivek Prakash 	ps.indent_parameters = 1;	/* -ip */
19897c7d358SVivek Prakash 	ps.decl_com_ind = 0;	/* if this is not set to some positive value
19997c7d358SVivek Prakash 				 * by an arg, we will set this equal to
20097c7d358SVivek Prakash 				 * ps.com_ind */
20197c7d358SVivek Prakash 	btype_2 = 1;		/* -br */
20297c7d358SVivek Prakash 	cuddle_else = 1;	/* -ce */
20397c7d358SVivek Prakash 	ps.unindent_displace = 0;	/* -d0 */
20497c7d358SVivek Prakash 	ps.case_indent = 0;	/* -cli0 */
20597c7d358SVivek Prakash 	format_col1_comments = 1;	/* -fc1 */
20697c7d358SVivek Prakash 	procnames_start_line = 1;	/* -psl */
20797c7d358SVivek Prakash 	proc_calls_space = 0;	/* -npcs */
20897c7d358SVivek Prakash 	comment_delimiter_on_blankline = 1;	/* -cdb */
20997c7d358SVivek Prakash 	ps.leave_comma = 1;	/* -nbc */
21097c7d358SVivek Prakash #endif
21197c7d358SVivek Prakash 
21297c7d358SVivek Prakash 	for (i = 1; i < argc; ++i)
21397c7d358SVivek Prakash 		if (strcmp(argv[i], "-npro") == 0)
21497c7d358SVivek Prakash 			break;
21597c7d358SVivek Prakash 	set_defaults();
21697c7d358SVivek Prakash 	if (i >= argc)
21797c7d358SVivek Prakash 		set_profile();
21897c7d358SVivek Prakash 
21997c7d358SVivek Prakash 	for (i = 1; i < argc; ++i) {
22097c7d358SVivek Prakash 
22197c7d358SVivek Prakash 		/*
22297c7d358SVivek Prakash 		 * look thru args (if any) for changes to defaults
22397c7d358SVivek Prakash 		 */
22497c7d358SVivek Prakash 		if (argv[i][0] != '-') {	/* no flag on parameter */
22597c7d358SVivek Prakash 			if (input == 0) {	/* we must have the input file */
22697c7d358SVivek Prakash 				in_name = argv[i];	/* remember name of
22797c7d358SVivek Prakash 							 * input file */
22897c7d358SVivek Prakash 				input = fopen(in_name, "r");
22997c7d358SVivek Prakash 				if (input == 0)	/* check for open error */
23097c7d358SVivek Prakash 					err(1, "%s", in_name);
23197c7d358SVivek Prakash 				continue;
23297c7d358SVivek Prakash 			} else
23397c7d358SVivek Prakash 				if (output == 0) {	/* we have the output
23497c7d358SVivek Prakash 							 * file */
23597c7d358SVivek Prakash 					out_name = argv[i];	/* remember name of
23697c7d358SVivek Prakash 								 * output file */
23797c7d358SVivek Prakash 					if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
23897c7d358SVivek Prakash 										 * the file */
23997c7d358SVivek Prakash 						fprintf(stderr, "indent: input and output files must be different\n");
24097c7d358SVivek Prakash 						exit(1);
24197c7d358SVivek Prakash 					}
24297c7d358SVivek Prakash 					output = fopen(out_name, "w");
24397c7d358SVivek Prakash 					if (output == 0)	/* check for create
24497c7d358SVivek Prakash 								 * error */
24597c7d358SVivek Prakash 						err(1, "%s", out_name);
24697c7d358SVivek Prakash 					continue;
24797c7d358SVivek Prakash 				}
24897c7d358SVivek Prakash 			fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]);
24997c7d358SVivek Prakash 			exit(1);
25097c7d358SVivek Prakash 		} else
25197c7d358SVivek Prakash 			set_option(argv[i]);
25297c7d358SVivek Prakash 	}			/* end of for */
25397c7d358SVivek Prakash 	if (input == 0) {
25497c7d358SVivek Prakash 		input = stdin;
25597c7d358SVivek Prakash 	}
25697c7d358SVivek Prakash 	if (output == 0) {
25797c7d358SVivek Prakash 		if (troff || input == stdin)
25897c7d358SVivek Prakash 			output = stdout;
25997c7d358SVivek Prakash 		else {
26097c7d358SVivek Prakash 			out_name = in_name;
26197c7d358SVivek Prakash 			bakcopy();
26297c7d358SVivek Prakash 		}
26397c7d358SVivek Prakash 	}
26497c7d358SVivek Prakash 	if (ps.com_ind <= 1)
26597c7d358SVivek Prakash 		ps.com_ind = 2;	/* dont put normal comments before column 2 */
26697c7d358SVivek Prakash 	if (troff) {
26797c7d358SVivek Prakash 		if (bodyf.font[0] == 0)
26897c7d358SVivek Prakash 			parsefont(&bodyf, "R");
26997c7d358SVivek Prakash 		if (scomf.font[0] == 0)
27097c7d358SVivek Prakash 			parsefont(&scomf, "I");
27197c7d358SVivek Prakash 		if (blkcomf.font[0] == 0)
27297c7d358SVivek Prakash 			blkcomf = scomf, blkcomf.size += 2;
27397c7d358SVivek Prakash 		if (boxcomf.font[0] == 0)
27497c7d358SVivek Prakash 			boxcomf = blkcomf;
27597c7d358SVivek Prakash 		if (stringf.font[0] == 0)
27697c7d358SVivek Prakash 			parsefont(&stringf, "L");
27797c7d358SVivek Prakash 		if (keywordf.font[0] == 0)
27897c7d358SVivek Prakash 			parsefont(&keywordf, "B");
27997c7d358SVivek Prakash 		writefdef(&bodyf, 'B');
28097c7d358SVivek Prakash 		writefdef(&scomf, 'C');
28197c7d358SVivek Prakash 		writefdef(&blkcomf, 'L');
28297c7d358SVivek Prakash 		writefdef(&boxcomf, 'X');
28397c7d358SVivek Prakash 		writefdef(&stringf, 'S');
28497c7d358SVivek Prakash 		writefdef(&keywordf, 'K');
28597c7d358SVivek Prakash 	}
28697c7d358SVivek Prakash 	if (block_comment_max_col <= 0)
28797c7d358SVivek Prakash 		block_comment_max_col = max_col;
28897c7d358SVivek Prakash 	if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
28997c7d358SVivek Prakash 		ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
29097c7d358SVivek Prakash 	if (continuation_indent == 0)
29197c7d358SVivek Prakash 		continuation_indent = ps.ind_size;
29297c7d358SVivek Prakash 	fill_buffer();		/* get first batch of stuff into input buffer */
29397c7d358SVivek Prakash 
29497c7d358SVivek Prakash 	parse(semicolon);
29597c7d358SVivek Prakash 	{
29697c7d358SVivek Prakash 		char   *p = buf_ptr;
29797c7d358SVivek Prakash 		int     col = 1;
29897c7d358SVivek Prakash 
29997c7d358SVivek Prakash 		while (1) {
30097c7d358SVivek Prakash 			if (*p == ' ')
30197c7d358SVivek Prakash 				col++;
30297c7d358SVivek Prakash 			else
30397c7d358SVivek Prakash 				if (*p == '\t')
30497c7d358SVivek Prakash 					col = ((col - 1) & ~7) + 9;
30597c7d358SVivek Prakash 				else
30697c7d358SVivek Prakash 					break;
30797c7d358SVivek Prakash 			p++;
30897c7d358SVivek Prakash 		}
30997c7d358SVivek Prakash 		if (col > ps.ind_size)
31097c7d358SVivek Prakash 			ps.ind_level = ps.i_l_follow = col / ps.ind_size;
31197c7d358SVivek Prakash 	}
31297c7d358SVivek Prakash 	if (troff) {
31397c7d358SVivek Prakash 		const char   *p = in_name, *beg = in_name;
31497c7d358SVivek Prakash 
31597c7d358SVivek Prakash 		while (*p)
31697c7d358SVivek Prakash 			if (*p++ == '/')
31797c7d358SVivek Prakash 				beg = p;
31897c7d358SVivek Prakash 		fprintf(output, ".Fn \"%s\"\n", beg);
31997c7d358SVivek Prakash 	}
32097c7d358SVivek Prakash 	/*
32197c7d358SVivek Prakash          * START OF MAIN LOOP
32297c7d358SVivek Prakash          */
32397c7d358SVivek Prakash 
32497c7d358SVivek Prakash 	while (1) {		/* this is the main loop.  it will go until we
32597c7d358SVivek Prakash 				 * reach eof */
32697c7d358SVivek Prakash 		int     is_procname;
32797c7d358SVivek Prakash 
32897c7d358SVivek Prakash 		type_code = lexi();	/* lexi reads one token.  The actual
32997c7d358SVivek Prakash 					 * characters read are stored in
33097c7d358SVivek Prakash 					 * "token". lexi returns a code
33197c7d358SVivek Prakash 					 * indicating the type of token */
33297c7d358SVivek Prakash 		is_procname = ps.procname[0];
33397c7d358SVivek Prakash 
33497c7d358SVivek Prakash 		/*
33597c7d358SVivek Prakash 		 * The following code moves everything following an if (), while (),
33697c7d358SVivek Prakash 		 * else, etc. up to the start of the following stmt to a buffer. This
33797c7d358SVivek Prakash 		 * allows proper handling of both kinds of brace placement.
33897c7d358SVivek Prakash 		 */
33997c7d358SVivek Prakash 
34097c7d358SVivek Prakash 		flushed_nl = false;
34197c7d358SVivek Prakash 		while (ps.search_brace) {	/* if we scanned an if(),
34297c7d358SVivek Prakash 						 * while(), etc., we might
34397c7d358SVivek Prakash 						 * need to copy stuff into a
34497c7d358SVivek Prakash 						 * buffer we must loop,
34597c7d358SVivek Prakash 						 * copying stuff into
34697c7d358SVivek Prakash 						 * save_com, until we find the
34797c7d358SVivek Prakash 						 * start of the stmt which
34897c7d358SVivek Prakash 						 * follows the if, or whatever */
34997c7d358SVivek Prakash 			switch (type_code) {
35097c7d358SVivek Prakash 			case newline:
35197c7d358SVivek Prakash 				++line_no;
35297c7d358SVivek Prakash 				flushed_nl = true;
35397c7d358SVivek Prakash 			case form_feed:
35497c7d358SVivek Prakash 				break;	/* form feeds and newlines found here
35597c7d358SVivek Prakash 					 * will be ignored */
35697c7d358SVivek Prakash 
35797c7d358SVivek Prakash 			case lbrace:	/* this is a brace that starts the
35897c7d358SVivek Prakash 					 * compound stmt */
35997c7d358SVivek Prakash 				if (sc_end == 0) {	/* ignore buffering if a
36097c7d358SVivek Prakash 							 * comment wasnt stored
36197c7d358SVivek Prakash 							 * up */
36297c7d358SVivek Prakash 					ps.search_brace = false;
36397c7d358SVivek Prakash 					goto check_type;
36497c7d358SVivek Prakash 				}
36597c7d358SVivek Prakash 				if (btype_2) {
36697c7d358SVivek Prakash 					save_com[0] = '{';	/* we either want to put
36797c7d358SVivek Prakash 								 * the brace right after
36897c7d358SVivek Prakash 								 * the if */
36997c7d358SVivek Prakash 					goto sw_buffer;	/* go to common code to
37097c7d358SVivek Prakash 							 * get out of this loop */
37197c7d358SVivek Prakash 				}
37297c7d358SVivek Prakash 			case comment:	/* we have a comment, so we must copy
37397c7d358SVivek Prakash 					 * it into the buffer */
37497c7d358SVivek Prakash 				if (!flushed_nl || sc_end != 0) {
37597c7d358SVivek Prakash 					if (sc_end == 0) {	/* if this is the first
37697c7d358SVivek Prakash 								 * comment, we must set
37797c7d358SVivek Prakash 								 * up the buffer */
37897c7d358SVivek Prakash 						save_com[0] = save_com[1] = ' ';
37997c7d358SVivek Prakash 						sc_end = &(save_com[2]);
38097c7d358SVivek Prakash 					} else {
38197c7d358SVivek Prakash 						*sc_end++ = '\n';	/* add newline between
38297c7d358SVivek Prakash 									 * comments */
38397c7d358SVivek Prakash 						*sc_end++ = ' ';
38497c7d358SVivek Prakash 						--line_no;
38597c7d358SVivek Prakash 					}
38697c7d358SVivek Prakash 					*sc_end++ = '/';	/* copy in start of
38797c7d358SVivek Prakash 								 * comment */
38897c7d358SVivek Prakash 					*sc_end++ = '*';
38997c7d358SVivek Prakash 
39097c7d358SVivek Prakash 					for (;;) {	/* loop until we get to
39197c7d358SVivek Prakash 							 * the end of the
39297c7d358SVivek Prakash 							 * comment */
39397c7d358SVivek Prakash 						*sc_end = *buf_ptr++;
39497c7d358SVivek Prakash 						if (buf_ptr >= buf_end)
39597c7d358SVivek Prakash 							fill_buffer();
39697c7d358SVivek Prakash 
39797c7d358SVivek Prakash 						if (*sc_end++ == '*' && *buf_ptr == '/')
39897c7d358SVivek Prakash 							break;	/* we are at end of
39997c7d358SVivek Prakash 								 * comment */
40097c7d358SVivek Prakash 
40197c7d358SVivek Prakash 						if (sc_end >= &(save_com[sc_size])) {	/* check for temp buffer
40297c7d358SVivek Prakash 											 * overflow */
40397c7d358SVivek Prakash 							diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
40497c7d358SVivek Prakash 							fflush(output);
40597c7d358SVivek Prakash 							exit(1);
40697c7d358SVivek Prakash 						}
40797c7d358SVivek Prakash 					}
40897c7d358SVivek Prakash 					*sc_end++ = '/';	/* add ending slash */
40997c7d358SVivek Prakash 					if (++buf_ptr >= buf_end)	/* get past / in buffer */
41097c7d358SVivek Prakash 						fill_buffer();
41197c7d358SVivek Prakash 					break;
41297c7d358SVivek Prakash 				}
41397c7d358SVivek Prakash 			default:	/* it is the start of a normal
41497c7d358SVivek Prakash 					 * statment */
41597c7d358SVivek Prakash 				if (flushed_nl)	/* if we flushed a newline,
41697c7d358SVivek Prakash 						 * make sure it is put back */
41797c7d358SVivek Prakash 					force_nl = true;
41897c7d358SVivek Prakash 				if ((type_code == sp_paren && *token == 'i'
41997c7d358SVivek Prakash 					&& last_else && ps.else_if) ||
42097c7d358SVivek Prakash 				    (type_code == sp_nparen && *token == 'e'
42197c7d358SVivek Prakash 					&& e_code != s_code && e_code[-1] == '}'))
42297c7d358SVivek Prakash 					force_nl = false;
42397c7d358SVivek Prakash 
42497c7d358SVivek Prakash 				if (sc_end == 0) {	/* ignore buffering if
42597c7d358SVivek Prakash 							 * comment wasnt saved
42697c7d358SVivek Prakash 							 * up */
42797c7d358SVivek Prakash 					ps.search_brace = false;
42897c7d358SVivek Prakash 					goto check_type;
42997c7d358SVivek Prakash 				}
43097c7d358SVivek Prakash 				if (force_nl) {	/* if we should insert a nl
43197c7d358SVivek Prakash 						 * here, put it into the
43297c7d358SVivek Prakash 						 * buffer */
43397c7d358SVivek Prakash 					force_nl = false;
43497c7d358SVivek Prakash 					--line_no;	/* this will be
43597c7d358SVivek Prakash 							 * re-increased when the
43697c7d358SVivek Prakash 							 * nl is read from the
43797c7d358SVivek Prakash 							 * buffer */
43897c7d358SVivek Prakash 					*sc_end++ = '\n';
43997c7d358SVivek Prakash 					*sc_end++ = ' ';
44097c7d358SVivek Prakash 					if (verbose && !flushed_nl)	/* print error msg if
44197c7d358SVivek Prakash 									 * the line was not
44297c7d358SVivek Prakash 									 * already broken */
44397c7d358SVivek Prakash 						diag(0, "Line broken");
44497c7d358SVivek Prakash 					flushed_nl = false;
44597c7d358SVivek Prakash 				}
44697c7d358SVivek Prakash 				for (t_ptr = token; *t_ptr; ++t_ptr)
44797c7d358SVivek Prakash 					*sc_end++ = *t_ptr;	/* copy token into temp
44897c7d358SVivek Prakash 								 * buffer */
44997c7d358SVivek Prakash 				ps.procname[0] = 0;
45097c7d358SVivek Prakash 
45197c7d358SVivek Prakash 		sw_buffer:
45297c7d358SVivek Prakash 				ps.search_brace = false;	/* stop looking for
45397c7d358SVivek Prakash 								 * start of stmt */
45497c7d358SVivek Prakash 				bp_save = buf_ptr;	/* save current input
45597c7d358SVivek Prakash 							 * buffer */
45697c7d358SVivek Prakash 				be_save = buf_end;
45797c7d358SVivek Prakash 				buf_ptr = save_com;	/* fix so that
45897c7d358SVivek Prakash 							 * subsequent calls to
45997c7d358SVivek Prakash 							 * lexi will take tokens
46097c7d358SVivek Prakash 							 * out of save_com */
46197c7d358SVivek Prakash 				*sc_end++ = ' ';	/* add trailing blank,
46297c7d358SVivek Prakash 							 * just in case */
46397c7d358SVivek Prakash 				buf_end = sc_end;
46497c7d358SVivek Prakash 				sc_end = 0;
46597c7d358SVivek Prakash 				break;
46697c7d358SVivek Prakash 			}	/* end of switch */
46797c7d358SVivek Prakash 			if (type_code != 0)	/* we must make this check,
46897c7d358SVivek Prakash 						 * just in case there was an
46997c7d358SVivek Prakash 						 * unexpected EOF */
47097c7d358SVivek Prakash 				type_code = lexi();	/* read another token */
47197c7d358SVivek Prakash 			/* if (ps.search_brace) ps.procname[0] = 0; */
47297c7d358SVivek Prakash 			if ((is_procname = ps.procname[0]) && flushed_nl
47397c7d358SVivek Prakash 			    && !procnames_start_line && ps.in_decl
47497c7d358SVivek Prakash 			    && type_code == ident)
47597c7d358SVivek Prakash 				flushed_nl = 0;
47697c7d358SVivek Prakash 		}		/* end of while (search_brace) */
47797c7d358SVivek Prakash 		last_else = 0;
47897c7d358SVivek Prakash check_type:
47997c7d358SVivek Prakash 		if (type_code == 0) {	/* we got eof */
48097c7d358SVivek Prakash 			if (s_lab != e_lab || s_code != e_code
48197c7d358SVivek Prakash 			    || s_com != e_com)	/* must dump end of line */
48297c7d358SVivek Prakash 				dump_line();
48397c7d358SVivek Prakash 			if (ps.tos > 1)	/* check for balanced braces */
48497c7d358SVivek Prakash 				diag(1, "Stuff missing from end of file.");
48597c7d358SVivek Prakash 
48697c7d358SVivek Prakash 			if (verbose) {
48797c7d358SVivek Prakash 				printf("There were %d output lines and %d comments\n",
48897c7d358SVivek Prakash 				    ps.out_lines, ps.out_coms);
48997c7d358SVivek Prakash 				printf("(Lines with comments)/(Lines with code): %6.3f\n",
49097c7d358SVivek Prakash 				    (1.0 * ps.com_lines) / code_lines);
49197c7d358SVivek Prakash 			}
49297c7d358SVivek Prakash 			fflush(output);
49397c7d358SVivek Prakash 			exit(found_err);
49497c7d358SVivek Prakash 		}
49597c7d358SVivek Prakash 		if (
49697c7d358SVivek Prakash 		    (type_code != comment) &&
49797c7d358SVivek Prakash 		    (type_code != newline) &&
49897c7d358SVivek Prakash 		    (type_code != preesc) &&
49997c7d358SVivek Prakash 		    (type_code != form_feed)) {
50097c7d358SVivek Prakash 			if (force_nl &&
50197c7d358SVivek Prakash 			    (type_code != semicolon) &&
50297c7d358SVivek Prakash 			    (type_code != lbrace || !btype_2)) {
50397c7d358SVivek Prakash 				/* we should force a broken line here */
50497c7d358SVivek Prakash 				if (verbose && !flushed_nl)
50597c7d358SVivek Prakash 					diag(0, "Line broken");
50697c7d358SVivek Prakash 				flushed_nl = false;
50797c7d358SVivek Prakash 				dump_line();
50897c7d358SVivek Prakash 				ps.want_blank = false;	/* dont insert blank at
50997c7d358SVivek Prakash 							 * line start */
51097c7d358SVivek Prakash 				force_nl = false;
51197c7d358SVivek Prakash 			}
51297c7d358SVivek Prakash 			ps.in_stmt = true;	/* turn on flag which causes
51397c7d358SVivek Prakash 						 * an extra level of
51497c7d358SVivek Prakash 						 * indentation. this is turned
51597c7d358SVivek Prakash 						 * off by a ; or '}' */
51697c7d358SVivek Prakash 			if (s_com != e_com) {	/* the turkey has embedded a
51797c7d358SVivek Prakash 						 * comment in a line. fix it */
51897c7d358SVivek Prakash 				*e_code++ = ' ';
51997c7d358SVivek Prakash 				for (t_ptr = s_com; *t_ptr; ++t_ptr) {
52097c7d358SVivek Prakash 					CHECK_SIZE_CODE;
52197c7d358SVivek Prakash 					*e_code++ = *t_ptr;
52297c7d358SVivek Prakash 				}
52397c7d358SVivek Prakash 				*e_code++ = ' ';
52497c7d358SVivek Prakash 				*e_code = '\0';	/* null terminate code sect */
52597c7d358SVivek Prakash 				ps.want_blank = false;
52697c7d358SVivek Prakash 				e_com = s_com;
52797c7d358SVivek Prakash 			}
52897c7d358SVivek Prakash 		} else
52997c7d358SVivek Prakash 			if (type_code != comment)	/* preserve force_nl
53097c7d358SVivek Prakash 							 * thru a comment */
53197c7d358SVivek Prakash 				force_nl = false;	/* cancel forced newline
53297c7d358SVivek Prakash 							 * after newline, form
53397c7d358SVivek Prakash 							 * feed, etc */
53497c7d358SVivek Prakash 
53597c7d358SVivek Prakash 
53697c7d358SVivek Prakash 
53797c7d358SVivek Prakash 		/*-----------------------------------------------------*\
53897c7d358SVivek Prakash 		|	   do switch on type of token scanned		|
53997c7d358SVivek Prakash 		\*-----------------------------------------------------*/
54097c7d358SVivek Prakash 		CHECK_SIZE_CODE;
54197c7d358SVivek Prakash 		switch (type_code) {	/* now, decide what to do with the
54297c7d358SVivek Prakash 					 * token */
54397c7d358SVivek Prakash 
54497c7d358SVivek Prakash 		case form_feed:/* found a form feed in line */
54597c7d358SVivek Prakash 			ps.use_ff = true;	/* a form feed is treated much
54697c7d358SVivek Prakash 						 * like a newline */
54797c7d358SVivek Prakash 			dump_line();
54897c7d358SVivek Prakash 			ps.want_blank = false;
54997c7d358SVivek Prakash 			break;
55097c7d358SVivek Prakash 
55197c7d358SVivek Prakash 		case newline:
55297c7d358SVivek Prakash 			if (ps.last_token != comma || ps.p_l_follow > 0
55397c7d358SVivek Prakash 			    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
55497c7d358SVivek Prakash 				dump_line();
55597c7d358SVivek Prakash 				ps.want_blank = false;
55697c7d358SVivek Prakash 			}
55797c7d358SVivek Prakash 			++line_no;	/* keep track of input line number */
55897c7d358SVivek Prakash 			break;
55997c7d358SVivek Prakash 
56097c7d358SVivek Prakash 		case lparen:	/* got a '(' or '[' */
56197c7d358SVivek Prakash 			++ps.p_l_follow;	/* count parens to make Healy
56297c7d358SVivek Prakash 						 * happy */
56397c7d358SVivek Prakash 			if (ps.want_blank && *token != '[' &&
56497c7d358SVivek Prakash 			    (ps.last_token != ident || proc_calls_space
56597c7d358SVivek Prakash 				|| (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
56697c7d358SVivek Prakash 				*e_code++ = ' ';
56797c7d358SVivek Prakash 			if (ps.in_decl && !ps.block_init) {
56897c7d358SVivek Prakash 				if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
56997c7d358SVivek Prakash 					ps.dumped_decl_indent = 1;
57097c7d358SVivek Prakash 					sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
57197c7d358SVivek Prakash 					e_code += strlen(e_code);
57297c7d358SVivek Prakash 				} else {
57397c7d358SVivek Prakash 					while ((e_code - s_code) < dec_ind) {
57497c7d358SVivek Prakash 						CHECK_SIZE_CODE;
57597c7d358SVivek Prakash 						*e_code++ = ' ';
57697c7d358SVivek Prakash 					}
57797c7d358SVivek Prakash 					*e_code++ = token[0];
57897c7d358SVivek Prakash 				}
57997c7d358SVivek Prakash 			} else
58097c7d358SVivek Prakash 				*e_code++ = token[0];
58197c7d358SVivek Prakash 			ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
58297c7d358SVivek Prakash 			if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
58397c7d358SVivek Prakash 			    && ps.paren_indents[0] < 2 * ps.ind_size)
58497c7d358SVivek Prakash 				ps.paren_indents[0] = 2 * ps.ind_size;
58597c7d358SVivek Prakash 			ps.want_blank = false;
58697c7d358SVivek Prakash 			if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
58797c7d358SVivek Prakash 				/*
58897c7d358SVivek Prakash 				 * this is a kluge to make sure that declarations will be
58997c7d358SVivek Prakash 				 * aligned right if proc decl has an explicit type on it, i.e.
59097c7d358SVivek Prakash 				 * "int a(x) {..."
59197c7d358SVivek Prakash 				 */
59297c7d358SVivek Prakash 				parse(semicolon);	/* I said this was a
59397c7d358SVivek Prakash 							 * kluge... */
59497c7d358SVivek Prakash 				ps.in_or_st = false;	/* turn off flag for
59597c7d358SVivek Prakash 							 * structure decl or
59697c7d358SVivek Prakash 							 * initialization */
59797c7d358SVivek Prakash 			}
59897c7d358SVivek Prakash 			if (ps.sizeof_keyword)
59997c7d358SVivek Prakash 				ps.sizeof_mask |= 1 << ps.p_l_follow;
60097c7d358SVivek Prakash 			break;
60197c7d358SVivek Prakash 
60297c7d358SVivek Prakash 		case rparen:	/* got a ')' or ']' */
60397c7d358SVivek Prakash 			rparen_count--;
60497c7d358SVivek Prakash 			if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
60597c7d358SVivek Prakash 				ps.last_u_d = true;
60697c7d358SVivek Prakash 				ps.cast_mask &= (1 << ps.p_l_follow) - 1;
60797c7d358SVivek Prakash 			}
60897c7d358SVivek Prakash 			ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
60997c7d358SVivek Prakash 			if (--ps.p_l_follow < 0) {
61097c7d358SVivek Prakash 				ps.p_l_follow = 0;
61197c7d358SVivek Prakash 				diag(0, "Extra %c", *token);
61297c7d358SVivek Prakash 			}
61397c7d358SVivek Prakash 			if (e_code == s_code)	/* if the paren starts the
61497c7d358SVivek Prakash 						 * line */
61597c7d358SVivek Prakash 				ps.paren_level = ps.p_l_follow;	/* then indent it */
61697c7d358SVivek Prakash 
61797c7d358SVivek Prakash 			*e_code++ = token[0];
61897c7d358SVivek Prakash 			ps.want_blank = true;
61997c7d358SVivek Prakash 
62097c7d358SVivek Prakash 			if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
62197c7d358SVivek Prakash 								 * (...), or some such */
62297c7d358SVivek Prakash 				sp_sw = false;
62397c7d358SVivek Prakash 				force_nl = true;	/* must force newline
62497c7d358SVivek Prakash 							 * after if */
62597c7d358SVivek Prakash 				ps.last_u_d = true;	/* inform lexi that a
62697c7d358SVivek Prakash 							 * following operator is
62797c7d358SVivek Prakash 							 * unary */
62897c7d358SVivek Prakash 				ps.in_stmt = false;	/* dont use stmt
62997c7d358SVivek Prakash 							 * continuation
63097c7d358SVivek Prakash 							 * indentation */
63197c7d358SVivek Prakash 
63297c7d358SVivek Prakash 				parse(hd_type);	/* let parser worry about if,
63397c7d358SVivek Prakash 						 * or whatever */
63497c7d358SVivek Prakash 			}
63597c7d358SVivek Prakash 			ps.search_brace = btype_2;	/* this should insure
63697c7d358SVivek Prakash 							 * that constructs such
63797c7d358SVivek Prakash 							 * as main(){...} and
63897c7d358SVivek Prakash 							 * int[]{...} have their
63997c7d358SVivek Prakash 							 * braces put in the
64097c7d358SVivek Prakash 							 * right place */
64197c7d358SVivek Prakash 			break;
64297c7d358SVivek Prakash 
64397c7d358SVivek Prakash 		case unary_op:	/* this could be any unary operation */
64497c7d358SVivek Prakash 			if (ps.want_blank)
64597c7d358SVivek Prakash 				*e_code++ = ' ';
64697c7d358SVivek Prakash 
64797c7d358SVivek Prakash 			if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
64897c7d358SVivek Prakash 				sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
64997c7d358SVivek Prakash 				ps.dumped_decl_indent = 1;
65097c7d358SVivek Prakash 				e_code += strlen(e_code);
65197c7d358SVivek Prakash 			} else {
65297c7d358SVivek Prakash 				const char *res = token;
65397c7d358SVivek Prakash 
65497c7d358SVivek Prakash 				if (ps.in_decl && !ps.block_init) {	/* if this is a unary op
65597c7d358SVivek Prakash 									 * in a declaration, we
65697c7d358SVivek Prakash 									 * should indent this
65797c7d358SVivek Prakash 									 * token */
65897c7d358SVivek Prakash 					for (i = 0; token[i]; ++i);	/* find length of token */
65997c7d358SVivek Prakash 					while ((e_code - s_code) < (dec_ind - i)) {
66097c7d358SVivek Prakash 						CHECK_SIZE_CODE;
66197c7d358SVivek Prakash 						*e_code++ = ' ';	/* pad it */
66297c7d358SVivek Prakash 					}
66397c7d358SVivek Prakash 				}
66497c7d358SVivek Prakash 				if (troff && token[0] == '-' && token[1] == '>')
66597c7d358SVivek Prakash 					res = "\\(->";
66697c7d358SVivek Prakash 				for (t_ptr = res; *t_ptr; ++t_ptr) {
66797c7d358SVivek Prakash 					CHECK_SIZE_CODE;
66897c7d358SVivek Prakash 					*e_code++ = *t_ptr;
66997c7d358SVivek Prakash 				}
67097c7d358SVivek Prakash 			}
67197c7d358SVivek Prakash 			ps.want_blank = false;
67297c7d358SVivek Prakash 			break;
67397c7d358SVivek Prakash 
67497c7d358SVivek Prakash 		case binary_op:/* any binary operation */
67597c7d358SVivek Prakash 			if (ps.want_blank)
67697c7d358SVivek Prakash 				*e_code++ = ' ';
67797c7d358SVivek Prakash 			{
67897c7d358SVivek Prakash 				const char *res = token;
67997c7d358SVivek Prakash 
68097c7d358SVivek Prakash 				if (troff)
68197c7d358SVivek Prakash 					switch (token[0]) {
68297c7d358SVivek Prakash 					case '<':
68397c7d358SVivek Prakash 						if (token[1] == '=')
68497c7d358SVivek Prakash 							res = "\\(<=";
68597c7d358SVivek Prakash 						break;
68697c7d358SVivek Prakash 					case '>':
68797c7d358SVivek Prakash 						if (token[1] == '=')
68897c7d358SVivek Prakash 							res = "\\(>=";
68997c7d358SVivek Prakash 						break;
69097c7d358SVivek Prakash 					case '!':
69197c7d358SVivek Prakash 						if (token[1] == '=')
69297c7d358SVivek Prakash 							res = "\\(!=";
69397c7d358SVivek Prakash 						break;
69497c7d358SVivek Prakash 					case '|':
69597c7d358SVivek Prakash 						if (token[1] == '|')
69697c7d358SVivek Prakash 							res = "\\(br\\(br";
69797c7d358SVivek Prakash 						else
69897c7d358SVivek Prakash 							if (token[1] == 0)
69997c7d358SVivek Prakash 								res = "\\(br";
70097c7d358SVivek Prakash 						break;
70197c7d358SVivek Prakash 					}
70297c7d358SVivek Prakash 				for (t_ptr = res; *t_ptr; ++t_ptr) {
70397c7d358SVivek Prakash 					CHECK_SIZE_CODE;
70497c7d358SVivek Prakash 					*e_code++ = *t_ptr;	/* move the operator */
70597c7d358SVivek Prakash 				}
70697c7d358SVivek Prakash 			}
70797c7d358SVivek Prakash 			ps.want_blank = true;
70897c7d358SVivek Prakash 			break;
70997c7d358SVivek Prakash 
71097c7d358SVivek Prakash 		case postop:	/* got a trailing ++ or -- */
71197c7d358SVivek Prakash 			*e_code++ = token[0];
71297c7d358SVivek Prakash 			*e_code++ = token[1];
71397c7d358SVivek Prakash 			ps.want_blank = true;
71497c7d358SVivek Prakash 			break;
71597c7d358SVivek Prakash 
71697c7d358SVivek Prakash 		case question:	/* got a ? */
71797c7d358SVivek Prakash 			squest++;	/* this will be used when a later
71897c7d358SVivek Prakash 					 * colon appears so we can distinguish
71997c7d358SVivek Prakash 					 * the <c>?<n>:<n> construct */
72097c7d358SVivek Prakash 			if (ps.want_blank)
72197c7d358SVivek Prakash 				*e_code++ = ' ';
72297c7d358SVivek Prakash 			*e_code++ = '?';
72397c7d358SVivek Prakash 			ps.want_blank = true;
72497c7d358SVivek Prakash 			break;
72597c7d358SVivek Prakash 
72697c7d358SVivek Prakash 		case casestmt:	/* got word 'case' or 'default' */
72797c7d358SVivek Prakash 			scase = true;	/* so we can process the later colon
72897c7d358SVivek Prakash 					 * properly */
72997c7d358SVivek Prakash 			goto copy_id;
73097c7d358SVivek Prakash 
73197c7d358SVivek Prakash 		case colon:	/* got a ':' */
73297c7d358SVivek Prakash 			if (squest > 0) {	/* it is part of the <c>?<n>:
73397c7d358SVivek Prakash 						 * <n> construct */
73497c7d358SVivek Prakash 				--squest;
73597c7d358SVivek Prakash 				if (ps.want_blank)
73697c7d358SVivek Prakash 					*e_code++ = ' ';
73797c7d358SVivek Prakash 				*e_code++ = ':';
73897c7d358SVivek Prakash 				ps.want_blank = true;
73997c7d358SVivek Prakash 				break;
74097c7d358SVivek Prakash 			}
74197c7d358SVivek Prakash 			if (ps.in_or_st) {
74297c7d358SVivek Prakash 				*e_code++ = ':';
74397c7d358SVivek Prakash 				ps.want_blank = false;
74497c7d358SVivek Prakash 				break;
74597c7d358SVivek Prakash 			}
74697c7d358SVivek Prakash 			ps.in_stmt = false;	/* seeing a label does not
74797c7d358SVivek Prakash 						 * imply we are in a stmt */
74897c7d358SVivek Prakash 			for (t_ptr = s_code; *t_ptr; ++t_ptr)
74997c7d358SVivek Prakash 				*e_lab++ = *t_ptr;	/* turn everything so
75097c7d358SVivek Prakash 							 * far into a label */
75197c7d358SVivek Prakash 			e_code = s_code;
75297c7d358SVivek Prakash 			*e_lab++ = ':';
75397c7d358SVivek Prakash 			*e_lab++ = ' ';
75497c7d358SVivek Prakash 			*e_lab = '\0';
75597c7d358SVivek Prakash 
75697c7d358SVivek Prakash 			force_nl = ps.pcase = scase;	/* ps.pcase will be used
75797c7d358SVivek Prakash 							 * by dump_line to
75897c7d358SVivek Prakash 							 * decide how to indent
75997c7d358SVivek Prakash 							 * the label. force_nl
76097c7d358SVivek Prakash 							 * will force a case n:
76197c7d358SVivek Prakash 							 * to be on a line by
76297c7d358SVivek Prakash 							 * itself */
76397c7d358SVivek Prakash 			scase = false;
76497c7d358SVivek Prakash 			ps.want_blank = false;
76597c7d358SVivek Prakash 			break;
76697c7d358SVivek Prakash 
76797c7d358SVivek Prakash 		case semicolon:/* got a ';' */
76897c7d358SVivek Prakash 			ps.in_or_st = false;	/* we are not in an
76997c7d358SVivek Prakash 						 * initialization or structure
77097c7d358SVivek Prakash 						 * declaration */
77197c7d358SVivek Prakash 			scase = false;	/* these will only need resetting in a
77297c7d358SVivek Prakash 					 * error */
77397c7d358SVivek Prakash 			squest = 0;
77497c7d358SVivek Prakash 			if (ps.last_token == rparen && rparen_count == 0)
77597c7d358SVivek Prakash 				ps.in_parameter_declaration = 0;
77697c7d358SVivek Prakash 			ps.cast_mask = 0;
77797c7d358SVivek Prakash 			ps.sizeof_mask = 0;
77897c7d358SVivek Prakash 			ps.block_init = 0;
77997c7d358SVivek Prakash 			ps.block_init_level = 0;
78097c7d358SVivek Prakash 			ps.just_saw_decl--;
78197c7d358SVivek Prakash 
78297c7d358SVivek Prakash 			if (ps.in_decl && s_code == e_code && !ps.block_init)
78397c7d358SVivek Prakash 				while ((e_code - s_code) < (dec_ind - 1)) {
78497c7d358SVivek Prakash 					CHECK_SIZE_CODE;
78597c7d358SVivek Prakash 					*e_code++ = ' ';
78697c7d358SVivek Prakash 				}
78797c7d358SVivek Prakash 
78897c7d358SVivek Prakash 			ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first
78997c7d358SVivek Prakash 							 * level structure
79097c7d358SVivek Prakash 							 * declaration, we arent
79197c7d358SVivek Prakash 							 * any more */
79297c7d358SVivek Prakash 
79397c7d358SVivek Prakash 			if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
79497c7d358SVivek Prakash 
79597c7d358SVivek Prakash 				/*
79697c7d358SVivek Prakash 				 * This should be true iff there were unbalanced parens in the
79797c7d358SVivek Prakash 				 * stmt.  It is a bit complicated, because the semicolon might
79897c7d358SVivek Prakash 				 * be in a for stmt
79997c7d358SVivek Prakash 				 */
80097c7d358SVivek Prakash 				diag(1, "Unbalanced parens");
80197c7d358SVivek Prakash 				ps.p_l_follow = 0;
80297c7d358SVivek Prakash 				if (sp_sw) {	/* this is a check for a if,
80397c7d358SVivek Prakash 						 * while, etc. with unbalanced
80497c7d358SVivek Prakash 						 * parens */
80597c7d358SVivek Prakash 					sp_sw = false;
80697c7d358SVivek Prakash 					parse(hd_type);	/* dont lose the if, or
80797c7d358SVivek Prakash 							 * whatever */
80897c7d358SVivek Prakash 				}
80997c7d358SVivek Prakash 			}
81097c7d358SVivek Prakash 			*e_code++ = ';';
81197c7d358SVivek Prakash 			ps.want_blank = true;
81297c7d358SVivek Prakash 			ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in
81397c7d358SVivek Prakash 								 * the middle of a stmt */
81497c7d358SVivek Prakash 
81597c7d358SVivek Prakash 			if (!sp_sw) {	/* if not if for (;;) */
81697c7d358SVivek Prakash 				parse(semicolon);	/* let parser know about
81797c7d358SVivek Prakash 							 * end of stmt */
81897c7d358SVivek Prakash 				force_nl = true;	/* force newline after a
81997c7d358SVivek Prakash 							 * end of stmt */
82097c7d358SVivek Prakash 			}
82197c7d358SVivek Prakash 			break;
82297c7d358SVivek Prakash 
82397c7d358SVivek Prakash 		case lbrace:	/* got a '{' */
82497c7d358SVivek Prakash 			ps.in_stmt = false;	/* dont indent the {} */
82597c7d358SVivek Prakash 			if (!ps.block_init)
82697c7d358SVivek Prakash 				force_nl = true;	/* force other stuff on
82797c7d358SVivek Prakash 							 * same line as '{' onto
82897c7d358SVivek Prakash 							 * new line */
82997c7d358SVivek Prakash 			else
83097c7d358SVivek Prakash 				if (ps.block_init_level <= 0)
83197c7d358SVivek Prakash 					ps.block_init_level = 1;
83297c7d358SVivek Prakash 				else
83397c7d358SVivek Prakash 					ps.block_init_level++;
83497c7d358SVivek Prakash 
83597c7d358SVivek Prakash 			if (s_code != e_code && !ps.block_init) {
83697c7d358SVivek Prakash 				if (!btype_2) {
83797c7d358SVivek Prakash 					dump_line();
83897c7d358SVivek Prakash 					ps.want_blank = false;
83997c7d358SVivek Prakash 				} else
84097c7d358SVivek Prakash 					if (ps.in_parameter_declaration && !ps.in_or_st) {
84197c7d358SVivek Prakash 						ps.i_l_follow = 0;
84297c7d358SVivek Prakash 						dump_line();
84397c7d358SVivek Prakash 						ps.want_blank = false;
84497c7d358SVivek Prakash 					}
84597c7d358SVivek Prakash 			}
84697c7d358SVivek Prakash 			if (ps.in_parameter_declaration)
84797c7d358SVivek Prakash 				prefix_blankline_requested = 0;
84897c7d358SVivek Prakash 
84997c7d358SVivek Prakash 			if (ps.p_l_follow > 0) {	/* check for preceding
85097c7d358SVivek Prakash 							 * unbalanced parens */
85197c7d358SVivek Prakash 				diag(1, "Unbalanced parens");
85297c7d358SVivek Prakash 				ps.p_l_follow = 0;
85397c7d358SVivek Prakash 				if (sp_sw) {	/* check for unclosed if, for,
85497c7d358SVivek Prakash 						 * etc. */
85597c7d358SVivek Prakash 					sp_sw = false;
85697c7d358SVivek Prakash 					parse(hd_type);
85797c7d358SVivek Prakash 					ps.ind_level = ps.i_l_follow;
85897c7d358SVivek Prakash 				}
85997c7d358SVivek Prakash 			}
86097c7d358SVivek Prakash 			if (s_code == e_code)
86197c7d358SVivek Prakash 				ps.ind_stmt = false;	/* dont put extra
86297c7d358SVivek Prakash 							 * indentation on line
86397c7d358SVivek Prakash 							 * with '{' */
86497c7d358SVivek Prakash 			if (ps.in_decl && ps.in_or_st) {	/* this is either a
86597c7d358SVivek Prakash 								 * structure declaration
86697c7d358SVivek Prakash 								 * or an init */
86797c7d358SVivek Prakash 				di_stack[ps.dec_nest++] = dec_ind;
86897c7d358SVivek Prakash 				/* ?		dec_ind = 0; */
86997c7d358SVivek Prakash 			} else {
87097c7d358SVivek Prakash 				ps.decl_on_line = false;	/* we cant be in the
87197c7d358SVivek Prakash 								 * middle of a
87297c7d358SVivek Prakash 								 * declaration, so dont
87397c7d358SVivek Prakash 								 * do special
87497c7d358SVivek Prakash 								 * indentation of
87597c7d358SVivek Prakash 								 * comments */
87697c7d358SVivek Prakash 				if (blanklines_after_declarations_at_proctop
87797c7d358SVivek Prakash 				    && ps.in_parameter_declaration)
87897c7d358SVivek Prakash 					postfix_blankline_requested = 1;
87997c7d358SVivek Prakash 				ps.in_parameter_declaration = 0;
88097c7d358SVivek Prakash 			}
88197c7d358SVivek Prakash 			dec_ind = 0;
88297c7d358SVivek Prakash 			parse(lbrace);	/* let parser know about this */
88397c7d358SVivek Prakash 			if (ps.want_blank)	/* put a blank before '{' if
88497c7d358SVivek Prakash 						 * '{' is not at start of line */
88597c7d358SVivek Prakash 				*e_code++ = ' ';
88697c7d358SVivek Prakash 			ps.want_blank = false;
88797c7d358SVivek Prakash 			*e_code++ = '{';
88897c7d358SVivek Prakash 			ps.just_saw_decl = 0;
88997c7d358SVivek Prakash 			break;
89097c7d358SVivek Prakash 
89197c7d358SVivek Prakash 		case rbrace:	/* got a '}' */
89297c7d358SVivek Prakash 			if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
89397c7d358SVivek Prakash 										 * omitted in
89497c7d358SVivek Prakash 										 * declarations */
89597c7d358SVivek Prakash 				parse(semicolon);
89697c7d358SVivek Prakash 			if (ps.p_l_follow) {	/* check for unclosed if, for,
89797c7d358SVivek Prakash 						 * else. */
89897c7d358SVivek Prakash 				diag(1, "Unbalanced parens");
89997c7d358SVivek Prakash 				ps.p_l_follow = 0;
90097c7d358SVivek Prakash 				sp_sw = false;
90197c7d358SVivek Prakash 			}
90297c7d358SVivek Prakash 			ps.just_saw_decl = 0;
90397c7d358SVivek Prakash 			ps.block_init_level--;
90497c7d358SVivek Prakash 			if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
90597c7d358SVivek Prakash 									 * line */
90697c7d358SVivek Prakash 				if (verbose)
90797c7d358SVivek Prakash 					diag(0, "Line broken");
90897c7d358SVivek Prakash 				dump_line();
90997c7d358SVivek Prakash 			}
91097c7d358SVivek Prakash 			*e_code++ = '}';
91197c7d358SVivek Prakash 			ps.want_blank = true;
91297c7d358SVivek Prakash 			ps.in_stmt = ps.ind_stmt = false;
91397c7d358SVivek Prakash 			if (ps.dec_nest > 0) {	/* we are in multi-level
91497c7d358SVivek Prakash 						 * structure declaration */
91597c7d358SVivek Prakash 				dec_ind = di_stack[--ps.dec_nest];
91697c7d358SVivek Prakash 				if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
91797c7d358SVivek Prakash 					ps.just_saw_decl = 2;
91897c7d358SVivek Prakash 				ps.in_decl = true;
91997c7d358SVivek Prakash 			}
92097c7d358SVivek Prakash 			prefix_blankline_requested = 0;
92197c7d358SVivek Prakash 			parse(rbrace);	/* let parser know about this */
92297c7d358SVivek Prakash 			ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
92397c7d358SVivek Prakash 			    && ps.il[ps.tos] >= ps.ind_level;
92497c7d358SVivek Prakash 			if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
92597c7d358SVivek Prakash 				postfix_blankline_requested = 1;
92697c7d358SVivek Prakash 			break;
92797c7d358SVivek Prakash 
92897c7d358SVivek Prakash 		case swstmt:	/* got keyword "switch" */
92997c7d358SVivek Prakash 			sp_sw = true;
93097c7d358SVivek Prakash 			hd_type = swstmt;	/* keep this for when we have
93197c7d358SVivek Prakash 						 * seen the expression */
93297c7d358SVivek Prakash 			goto copy_id;	/* go move the token into buffer */
93397c7d358SVivek Prakash 
93497c7d358SVivek Prakash 		case sp_paren:	/* token is if, while, for */
93597c7d358SVivek Prakash 			sp_sw = true;	/* the interesting stuff is done after
93697c7d358SVivek Prakash 					 * the expression is scanned */
93797c7d358SVivek Prakash 			hd_type = (*token == 'i' ? ifstmt :
93897c7d358SVivek Prakash 			    (*token == 'w' ? whilestmt : forstmt));
93997c7d358SVivek Prakash 
94097c7d358SVivek Prakash 			/*
94197c7d358SVivek Prakash 		         * remember the type of header for later use by parser
94297c7d358SVivek Prakash 		         */
94397c7d358SVivek Prakash 			goto copy_id;	/* copy the token into line */
94497c7d358SVivek Prakash 
94597c7d358SVivek Prakash 		case sp_nparen:/* got else, do */
94697c7d358SVivek Prakash 			ps.in_stmt = false;
94797c7d358SVivek Prakash 			if (*token == 'e') {
94897c7d358SVivek Prakash 				if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
94997c7d358SVivek Prakash 					if (verbose)
95097c7d358SVivek Prakash 						diag(0, "Line broken");
95197c7d358SVivek Prakash 					dump_line();	/* make sure this starts
95297c7d358SVivek Prakash 							 * a line */
95397c7d358SVivek Prakash 					ps.want_blank = false;
95497c7d358SVivek Prakash 				}
95597c7d358SVivek Prakash 				force_nl = true;	/* also, following stuff
95697c7d358SVivek Prakash 							 * must go onto new line */
95797c7d358SVivek Prakash 				last_else = 1;
95897c7d358SVivek Prakash 				parse(elselit);
95997c7d358SVivek Prakash 			} else {
96097c7d358SVivek Prakash 				if (e_code != s_code) {	/* make sure this starts
96197c7d358SVivek Prakash 							 * a line */
96297c7d358SVivek Prakash 					if (verbose)
96397c7d358SVivek Prakash 						diag(0, "Line broken");
96497c7d358SVivek Prakash 					dump_line();
96597c7d358SVivek Prakash 					ps.want_blank = false;
96697c7d358SVivek Prakash 				}
96797c7d358SVivek Prakash 				force_nl = true;	/* also, following stuff
96897c7d358SVivek Prakash 							 * must go onto new line */
96997c7d358SVivek Prakash 				last_else = 0;
97097c7d358SVivek Prakash 				parse(dolit);
97197c7d358SVivek Prakash 			}
97297c7d358SVivek Prakash 			goto copy_id;	/* move the token into line */
97397c7d358SVivek Prakash 
97497c7d358SVivek Prakash 		case decl:	/* we have a declaration type (int, register,
97597c7d358SVivek Prakash 				 * etc.) */
97697c7d358SVivek Prakash 			parse(decl);	/* let parser worry about indentation */
97797c7d358SVivek Prakash 			if (ps.last_token == rparen && ps.tos <= 1) {
97897c7d358SVivek Prakash 				ps.in_parameter_declaration = 1;
97997c7d358SVivek Prakash 				if (s_code != e_code) {
98097c7d358SVivek Prakash 					dump_line();
98197c7d358SVivek Prakash 					ps.want_blank = 0;
98297c7d358SVivek Prakash 				}
98397c7d358SVivek Prakash 			}
98497c7d358SVivek Prakash 			if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
98597c7d358SVivek Prakash 				ps.ind_level = ps.i_l_follow = 1;
98697c7d358SVivek Prakash 				ps.ind_stmt = 0;
98797c7d358SVivek Prakash 			}
98897c7d358SVivek Prakash 			ps.in_or_st = true;	/* this might be a structure
98997c7d358SVivek Prakash 						 * or initialization
99097c7d358SVivek Prakash 						 * declaration */
99197c7d358SVivek Prakash 			ps.in_decl = ps.decl_on_line = true;
99297c7d358SVivek Prakash 			if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
99397c7d358SVivek Prakash 				ps.just_saw_decl = 2;
99497c7d358SVivek Prakash 			prefix_blankline_requested = 0;
99597c7d358SVivek Prakash 			for (i = 0; token[i++];);	/* get length of token */
99697c7d358SVivek Prakash 
99797c7d358SVivek Prakash 			/*
99897c7d358SVivek Prakash 		         * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
99997c7d358SVivek Prakash 		         * : i);
100097c7d358SVivek Prakash 		         */
100197c7d358SVivek Prakash 			dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
1002*0a6a1f1dSLionel Sambuc 			tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
100397c7d358SVivek Prakash 			goto copy_id;
100497c7d358SVivek Prakash 
100597c7d358SVivek Prakash 		case ident:	/* got an identifier or constant */
100697c7d358SVivek Prakash 			if (ps.in_decl) {	/* if we are in a declaration,
100797c7d358SVivek Prakash 						 * we must indent identifier */
100897c7d358SVivek Prakash 				if (ps.want_blank)
100997c7d358SVivek Prakash 					*e_code++ = ' ';
101097c7d358SVivek Prakash 				ps.want_blank = false;
101197c7d358SVivek Prakash 				if (is_procname == 0 || !procnames_start_line) {
101297c7d358SVivek Prakash 					if (!ps.block_init) {
101397c7d358SVivek Prakash 						if (troff && !ps.dumped_decl_indent) {
101497c7d358SVivek Prakash 							sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
101597c7d358SVivek Prakash 							ps.dumped_decl_indent = 1;
101697c7d358SVivek Prakash 							e_code += strlen(e_code);
1017*0a6a1f1dSLionel Sambuc 							CHECK_SIZE_CODE;
1018*0a6a1f1dSLionel Sambuc 						} else {
1019*0a6a1f1dSLionel Sambuc 							int cur_dec_ind;
1020*0a6a1f1dSLionel Sambuc 							int pos, startpos;
1021*0a6a1f1dSLionel Sambuc 
1022*0a6a1f1dSLionel Sambuc 							/*
1023*0a6a1f1dSLionel Sambuc 							 * in order to get the tab math right for
1024*0a6a1f1dSLionel Sambuc 							 * indentations that are not multiples of 8 we
1025*0a6a1f1dSLionel Sambuc 							 * need to modify both startpos and dec_ind
1026*0a6a1f1dSLionel Sambuc 							 * (cur_dec_ind) here by eight minus the
1027*0a6a1f1dSLionel Sambuc 							 * remainder of the current starting column
1028*0a6a1f1dSLionel Sambuc 							 * divided by eight. This seems to be a
1029*0a6a1f1dSLionel Sambuc 							 * properly working fix
1030*0a6a1f1dSLionel Sambuc 							 */
1031*0a6a1f1dSLionel Sambuc 							startpos = e_code - s_code;
1032*0a6a1f1dSLionel Sambuc 							cur_dec_ind = dec_ind;
1033*0a6a1f1dSLionel Sambuc 							pos = startpos;
1034*0a6a1f1dSLionel Sambuc 							if ((ps.ind_level * ps.ind_size) % 8 != 0) {
1035*0a6a1f1dSLionel Sambuc 								pos += (ps.ind_level * ps.ind_size) % 8;
1036*0a6a1f1dSLionel Sambuc 								cur_dec_ind += (ps.ind_level * ps.ind_size) % 8;
1037*0a6a1f1dSLionel Sambuc 							}
1038*0a6a1f1dSLionel Sambuc 
1039*0a6a1f1dSLionel Sambuc 							if (tabs_to_var) {
1040*0a6a1f1dSLionel Sambuc 								while ((pos & ~7) + 8 <= cur_dec_ind) {
1041*0a6a1f1dSLionel Sambuc 									CHECK_SIZE_CODE;
1042*0a6a1f1dSLionel Sambuc 									*e_code++ = '\t';
1043*0a6a1f1dSLionel Sambuc 									pos = (pos & ~7) + 8;
1044*0a6a1f1dSLionel Sambuc 								}
1045*0a6a1f1dSLionel Sambuc 							}
1046*0a6a1f1dSLionel Sambuc 							while (pos < cur_dec_ind) {
104797c7d358SVivek Prakash 								CHECK_SIZE_CODE;
104897c7d358SVivek Prakash 								*e_code++ = ' ';
1049*0a6a1f1dSLionel Sambuc 								pos++;
1050*0a6a1f1dSLionel Sambuc 							}
1051*0a6a1f1dSLionel Sambuc 							if (ps.want_blank && e_code - s_code == startpos)
1052*0a6a1f1dSLionel Sambuc 								*e_code++ = ' ';
1053*0a6a1f1dSLionel Sambuc 							ps.want_blank = false;
105497c7d358SVivek Prakash 						}
105597c7d358SVivek Prakash 					}
105697c7d358SVivek Prakash 				} else {
105797c7d358SVivek Prakash 					if (dec_ind && s_code != e_code)
105897c7d358SVivek Prakash 						dump_line();
105997c7d358SVivek Prakash 					dec_ind = 0;
106097c7d358SVivek Prakash 					ps.want_blank = false;
106197c7d358SVivek Prakash 				}
106297c7d358SVivek Prakash 			} else
106397c7d358SVivek Prakash 				if (sp_sw && ps.p_l_follow == 0) {
106497c7d358SVivek Prakash 					sp_sw = false;
106597c7d358SVivek Prakash 					force_nl = true;
106697c7d358SVivek Prakash 					ps.last_u_d = true;
106797c7d358SVivek Prakash 					ps.in_stmt = false;
106897c7d358SVivek Prakash 					parse(hd_type);
106997c7d358SVivek Prakash 				}
107097c7d358SVivek Prakash 	copy_id:
107197c7d358SVivek Prakash 			if (ps.want_blank)
107297c7d358SVivek Prakash 				*e_code++ = ' ';
107397c7d358SVivek Prakash 			if (troff && ps.its_a_keyword) {
107497c7d358SVivek Prakash 				e_code = chfont(&bodyf, &keywordf, e_code);
107597c7d358SVivek Prakash 				for (t_ptr = token; *t_ptr; ++t_ptr) {
107697c7d358SVivek Prakash 					CHECK_SIZE_CODE;
107797c7d358SVivek Prakash 					*e_code++ = keywordf.allcaps
107897c7d358SVivek Prakash 					    ? toupper((unsigned char)*t_ptr)
107997c7d358SVivek Prakash 					    : *t_ptr;
108097c7d358SVivek Prakash 				}
108197c7d358SVivek Prakash 				e_code = chfont(&keywordf, &bodyf, e_code);
108297c7d358SVivek Prakash 			} else
108397c7d358SVivek Prakash 				for (t_ptr = token; *t_ptr; ++t_ptr) {
108497c7d358SVivek Prakash 					CHECK_SIZE_CODE;
108597c7d358SVivek Prakash 					*e_code++ = *t_ptr;
108697c7d358SVivek Prakash 				}
108797c7d358SVivek Prakash 			ps.want_blank = true;
108897c7d358SVivek Prakash 			break;
108997c7d358SVivek Prakash 
109097c7d358SVivek Prakash 		case period:	/* treat a period kind of like a binary
109197c7d358SVivek Prakash 				 * operation */
109297c7d358SVivek Prakash 			*e_code++ = '.';	/* move the period into line */
109397c7d358SVivek Prakash 			ps.want_blank = false;	/* dont put a blank after a
109497c7d358SVivek Prakash 						 * period */
109597c7d358SVivek Prakash 			break;
109697c7d358SVivek Prakash 
109797c7d358SVivek Prakash 		case comma:
109897c7d358SVivek Prakash 			ps.want_blank = (s_code != e_code);	/* only put blank after
109997c7d358SVivek Prakash 								 * comma if comma does
110097c7d358SVivek Prakash 								 * not start the line */
110197c7d358SVivek Prakash 			if (ps.in_decl && is_procname == 0 && !ps.block_init)
110297c7d358SVivek Prakash 				while ((e_code - s_code) < (dec_ind - 1)) {
110397c7d358SVivek Prakash 					CHECK_SIZE_CODE;
110497c7d358SVivek Prakash 					*e_code++ = ' ';
110597c7d358SVivek Prakash 				}
110697c7d358SVivek Prakash 
110797c7d358SVivek Prakash 			*e_code++ = ',';
110897c7d358SVivek Prakash 			if (ps.p_l_follow == 0) {
110997c7d358SVivek Prakash 				if (ps.block_init_level <= 0)
111097c7d358SVivek Prakash 					ps.block_init = 0;
111197c7d358SVivek Prakash 				if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
111297c7d358SVivek Prakash 					force_nl = true;
111397c7d358SVivek Prakash 			}
111497c7d358SVivek Prakash 			break;
111597c7d358SVivek Prakash 
111697c7d358SVivek Prakash 		case preesc:	/* got the character '#' */
111797c7d358SVivek Prakash 			if ((s_com != e_com) ||
111897c7d358SVivek Prakash 			    (s_lab != e_lab) ||
111997c7d358SVivek Prakash 			    (s_code != e_code))
112097c7d358SVivek Prakash 				dump_line();
112197c7d358SVivek Prakash 			*e_lab++ = '#';	/* move whole line to 'label' buffer */
112297c7d358SVivek Prakash 			{
112397c7d358SVivek Prakash 				int     in_comment = 0;
112497c7d358SVivek Prakash 				int     com_start = 0;
112597c7d358SVivek Prakash 				char    quote = 0;
112697c7d358SVivek Prakash 				int     com_end = 0;
112797c7d358SVivek Prakash 
112897c7d358SVivek Prakash 				while (*buf_ptr == ' ' || *buf_ptr == '\t') {
112997c7d358SVivek Prakash 					buf_ptr++;
113097c7d358SVivek Prakash 					if (buf_ptr >= buf_end)
113197c7d358SVivek Prakash 						fill_buffer();
113297c7d358SVivek Prakash 				}
113397c7d358SVivek Prakash 				while (*buf_ptr != '\n' || in_comment) {
113497c7d358SVivek Prakash 					CHECK_SIZE_LAB;
113597c7d358SVivek Prakash 					*e_lab = *buf_ptr++;
113697c7d358SVivek Prakash 					if (buf_ptr >= buf_end)
113797c7d358SVivek Prakash 						fill_buffer();
113897c7d358SVivek Prakash 					switch (*e_lab++) {
113997c7d358SVivek Prakash 					case BACKSLASH:
114097c7d358SVivek Prakash 						if (troff)
114197c7d358SVivek Prakash 							*e_lab++ = BACKSLASH;
114297c7d358SVivek Prakash 						if (!in_comment) {
114397c7d358SVivek Prakash 							*e_lab++ = *buf_ptr++;
114497c7d358SVivek Prakash 							if (buf_ptr >= buf_end)
114597c7d358SVivek Prakash 								fill_buffer();
114697c7d358SVivek Prakash 						}
114797c7d358SVivek Prakash 						break;
114897c7d358SVivek Prakash 					case '/':
114997c7d358SVivek Prakash 						if (*buf_ptr == '*' && !in_comment && !quote) {
115097c7d358SVivek Prakash 							in_comment = 1;
115197c7d358SVivek Prakash 							*e_lab++ = *buf_ptr++;
115297c7d358SVivek Prakash 							com_start = e_lab - s_lab - 2;
115397c7d358SVivek Prakash 						}
115497c7d358SVivek Prakash 						break;
115597c7d358SVivek Prakash 					case '"':
115697c7d358SVivek Prakash 						if (quote == '"')
115797c7d358SVivek Prakash 							quote = 0;
115897c7d358SVivek Prakash 						break;
115997c7d358SVivek Prakash 					case '\'':
116097c7d358SVivek Prakash 						if (quote == '\'')
116197c7d358SVivek Prakash 							quote = 0;
116297c7d358SVivek Prakash 						break;
116397c7d358SVivek Prakash 					case '*':
116497c7d358SVivek Prakash 						if (*buf_ptr == '/' && in_comment) {
116597c7d358SVivek Prakash 							in_comment = 0;
116697c7d358SVivek Prakash 							*e_lab++ = *buf_ptr++;
116797c7d358SVivek Prakash 							com_end = e_lab - s_lab;
116897c7d358SVivek Prakash 						}
116997c7d358SVivek Prakash 						break;
117097c7d358SVivek Prakash 					}
117197c7d358SVivek Prakash 				}
117297c7d358SVivek Prakash 
117397c7d358SVivek Prakash 				while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
117497c7d358SVivek Prakash 					e_lab--;
117597c7d358SVivek Prakash 				if (e_lab - s_lab == com_end && bp_save == 0) {	/* comment on
117697c7d358SVivek Prakash 										 * preprocessor line */
117797c7d358SVivek Prakash 					if (sc_end == 0)	/* if this is the first
117897c7d358SVivek Prakash 								 * comment, we must set
117997c7d358SVivek Prakash 								 * up the buffer */
118097c7d358SVivek Prakash 						sc_end = &(save_com[0]);
118197c7d358SVivek Prakash 					else {
118297c7d358SVivek Prakash 						*sc_end++ = '\n';	/* add newline between
118397c7d358SVivek Prakash 									 * comments */
118497c7d358SVivek Prakash 						*sc_end++ = ' ';
118597c7d358SVivek Prakash 						--line_no;
118697c7d358SVivek Prakash 					}
118797c7d358SVivek Prakash 					memmove(sc_end, s_lab + com_start, com_end - com_start);
118897c7d358SVivek Prakash 					sc_end += com_end - com_start;
118997c7d358SVivek Prakash 					if (sc_end >= &save_com[sc_size])
119097c7d358SVivek Prakash 						abort();
119197c7d358SVivek Prakash 					e_lab = s_lab + com_start;
119297c7d358SVivek Prakash 					while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
119397c7d358SVivek Prakash 						e_lab--;
119497c7d358SVivek Prakash 					bp_save = buf_ptr;	/* save current input
119597c7d358SVivek Prakash 								 * buffer */
119697c7d358SVivek Prakash 					be_save = buf_end;
119797c7d358SVivek Prakash 					buf_ptr = save_com;	/* fix so that
119897c7d358SVivek Prakash 								 * subsequent calls to
119997c7d358SVivek Prakash 								 * lexi will take tokens
120097c7d358SVivek Prakash 								 * out of save_com */
120197c7d358SVivek Prakash 					*sc_end++ = ' ';	/* add trailing blank,
120297c7d358SVivek Prakash 								 * just in case */
120397c7d358SVivek Prakash 					buf_end = sc_end;
120497c7d358SVivek Prakash 					sc_end = 0;
120597c7d358SVivek Prakash 				}
120697c7d358SVivek Prakash 				*e_lab = '\0';	/* null terminate line */
120797c7d358SVivek Prakash 				ps.pcase = false;
120897c7d358SVivek Prakash 			}
120997c7d358SVivek Prakash 
121097c7d358SVivek Prakash 			if (strncmp(s_lab, "#if", 3) == 0) {
121197c7d358SVivek Prakash 				if (blanklines_around_conditional_compilation) {
121297c7d358SVivek Prakash 					int     c;
121397c7d358SVivek Prakash 					prefix_blankline_requested++;
121497c7d358SVivek Prakash 					while ((c = getc(input)) == '\n');
121597c7d358SVivek Prakash 					ungetc(c, input);
121697c7d358SVivek Prakash 				}
121797c7d358SVivek Prakash 				if (ifdef_level < (int)(sizeof state_stack / sizeof state_stack[0])) {
121897c7d358SVivek Prakash 					match_state[ifdef_level].tos = -1;
121997c7d358SVivek Prakash 					state_stack[ifdef_level++] = ps;
122097c7d358SVivek Prakash 				} else
122197c7d358SVivek Prakash 					diag(1, "#if stack overflow");
122297c7d358SVivek Prakash 			} else
122397c7d358SVivek Prakash 				if (strncmp(s_lab, "#else", 5) == 0) {
122497c7d358SVivek Prakash 					if (ifdef_level <= 0)
122597c7d358SVivek Prakash 						diag(1, "Unmatched #else");
122697c7d358SVivek Prakash 					else {
122797c7d358SVivek Prakash 						match_state[ifdef_level - 1] = ps;
122897c7d358SVivek Prakash 						ps = state_stack[ifdef_level - 1];
122997c7d358SVivek Prakash 					}
123097c7d358SVivek Prakash 				} else
123197c7d358SVivek Prakash 					if (strncmp(s_lab, "#endif", 6) == 0) {
123297c7d358SVivek Prakash 						if (ifdef_level <= 0)
123397c7d358SVivek Prakash 							diag(1, "Unmatched #endif");
123497c7d358SVivek Prakash 						else {
123597c7d358SVivek Prakash 							ifdef_level--;
123697c7d358SVivek Prakash 
123797c7d358SVivek Prakash #ifdef undef
123897c7d358SVivek Prakash 							/*
123997c7d358SVivek Prakash 						         * This match needs to be more intelligent before the
124097c7d358SVivek Prakash 						         * message is useful
124197c7d358SVivek Prakash 						         */
124297c7d358SVivek Prakash 							if (match_state[ifdef_level].tos >= 0
124397c7d358SVivek Prakash 							    && memcmp(&ps, &match_state[ifdef_level], sizeof ps))
124497c7d358SVivek Prakash 								diag(0, "Syntactically inconsistant #ifdef alternatives.");
124597c7d358SVivek Prakash #endif
124697c7d358SVivek Prakash 						}
124797c7d358SVivek Prakash 						if (blanklines_around_conditional_compilation) {
124897c7d358SVivek Prakash 							postfix_blankline_requested++;
124997c7d358SVivek Prakash 							n_real_blanklines = 0;
125097c7d358SVivek Prakash 						}
125197c7d358SVivek Prakash 					}
125297c7d358SVivek Prakash 			break;	/* subsequent processing of the newline
125397c7d358SVivek Prakash 				 * character will cause the line to be printed */
125497c7d358SVivek Prakash 
125597c7d358SVivek Prakash 		case comment:	/* we have gotten a start comment */
125697c7d358SVivek Prakash 			/* this is a biggie */
125797c7d358SVivek Prakash 			if (flushed_nl) {	/* we should force a broken
125897c7d358SVivek Prakash 						 * line here */
125997c7d358SVivek Prakash 				flushed_nl = false;
126097c7d358SVivek Prakash 				dump_line();
126197c7d358SVivek Prakash 				ps.want_blank = false;	/* dont insert blank at
126297c7d358SVivek Prakash 							 * line start */
126397c7d358SVivek Prakash 				force_nl = false;
126497c7d358SVivek Prakash 			}
126597c7d358SVivek Prakash 			pr_comment();
126697c7d358SVivek Prakash 			break;
126797c7d358SVivek Prakash 		}		/* end of big switch stmt */
126897c7d358SVivek Prakash 
126997c7d358SVivek Prakash 		*e_code = '\0';	/* make sure code section is null terminated */
127097c7d358SVivek Prakash 		if (type_code != comment && type_code != newline && type_code != preesc)
127197c7d358SVivek Prakash 			ps.last_token = type_code;
127297c7d358SVivek Prakash 	}			/* end of main while (1) loop */
127397c7d358SVivek Prakash }
127497c7d358SVivek Prakash /*
127597c7d358SVivek Prakash  * copy input file to backup file if in_name is /blah/blah/blah/file, then
127697c7d358SVivek Prakash  * backup file will be ".Bfile" then make the backup file the input and
127797c7d358SVivek Prakash  * original input file the output
127897c7d358SVivek Prakash  */
127997c7d358SVivek Prakash void
bakcopy(void)128097c7d358SVivek Prakash bakcopy(void)
128197c7d358SVivek Prakash {
128297c7d358SVivek Prakash 	int     n, bakchn;
128397c7d358SVivek Prakash 	char    buff[8 * 1024];
128497c7d358SVivek Prakash 	const char *p;
128597c7d358SVivek Prakash 
128697c7d358SVivek Prakash 	/* construct file name .Bfile */
128797c7d358SVivek Prakash 	for (p = in_name; *p; p++);	/* skip to end of string */
128897c7d358SVivek Prakash 	while (p > in_name && *p != '/')	/* find last '/' */
128997c7d358SVivek Prakash 		p--;
129097c7d358SVivek Prakash 	if (*p == '/')
129197c7d358SVivek Prakash 		p++;
129297c7d358SVivek Prakash 	sprintf(bakfile, "%s.BAK", p);
129397c7d358SVivek Prakash 
129497c7d358SVivek Prakash 	/* copy in_name to backup file */
129597c7d358SVivek Prakash 	bakchn = creat(bakfile, 0600);
129697c7d358SVivek Prakash 	if (bakchn < 0)
129797c7d358SVivek Prakash 		err(1, "%s", bakfile);
129897c7d358SVivek Prakash 	while ((n = read(fileno(input), buff, sizeof buff)) > 0)
129997c7d358SVivek Prakash 		if (write(bakchn, buff, n) != n)
130097c7d358SVivek Prakash 			err(1, "%s", bakfile);
130197c7d358SVivek Prakash 	if (n < 0)
130297c7d358SVivek Prakash 		err(1, "%s", in_name);
130397c7d358SVivek Prakash 	close(bakchn);
130497c7d358SVivek Prakash 	fclose(input);
130597c7d358SVivek Prakash 
130697c7d358SVivek Prakash 	/* re-open backup file as the input file */
130797c7d358SVivek Prakash 	input = fopen(bakfile, "r");
130897c7d358SVivek Prakash 	if (input == 0)
130997c7d358SVivek Prakash 		err(1, "%s", bakfile);
131097c7d358SVivek Prakash 	/* now the original input file will be the output */
131197c7d358SVivek Prakash 	output = fopen(in_name, "w");
131297c7d358SVivek Prakash 	if (output == 0) {
131397c7d358SVivek Prakash 		unlink(bakfile);
131497c7d358SVivek Prakash 		err(1, "%s", in_name);
131597c7d358SVivek Prakash 	}
131697c7d358SVivek Prakash }
1317