xref: /freebsd/usr.bin/indent/indent.c (revision a5e1cac0f50bdfadd53b412fe740ccdc4ce460e3)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
39b50d902SRodney W. Grimes  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
159b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
169b50d902SRodney W. Grimes  *    must display the following acknowledgement:
179b50d902SRodney W. Grimes  *	This product includes software developed by the University of
189b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
199b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
209b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
219b50d902SRodney W. Grimes  *    without specific prior written permission.
229b50d902SRodney W. Grimes  *
239b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
249b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
259b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
269b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
279b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
289b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
299b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
309b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
319b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
329b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
339b50d902SRodney W. Grimes  * SUCH DAMAGE.
349b50d902SRodney W. Grimes  */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37958d7c9fSPhilippe Charnier static const char copyright[] =
389b50d902SRodney W. Grimes "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
399b50d902SRodney W. Grimes @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
409b50d902SRodney W. Grimes @(#) Copyright (c) 1980, 1993\n\
419b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #ifndef lint
45958d7c9fSPhilippe Charnier #if 0
469b50d902SRodney W. Grimes static char sccsid[] = "@(#)indent.c	5.17 (Berkeley) 6/7/93";
47958d7c9fSPhilippe Charnier #endif
48958d7c9fSPhilippe Charnier static const char rcsid[] =
49c3aac50fSPeter Wemm   "$FreeBSD$";
509b50d902SRodney W. Grimes #endif /* not lint */
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes #include <sys/param.h>
53958d7c9fSPhilippe Charnier #include <err.h>
549b50d902SRodney W. Grimes #include <fcntl.h>
559b50d902SRodney W. Grimes #include <unistd.h>
569b50d902SRodney W. Grimes #include <stdio.h>
579b50d902SRodney W. Grimes #include <stdlib.h>
589b50d902SRodney W. Grimes #include <string.h>
599b50d902SRodney W. Grimes #include "indent_globs.h"
609b50d902SRodney W. Grimes #include "indent_codes.h"
619b50d902SRodney W. Grimes #include <ctype.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes char       *in_name = "Standard Input";	/* will always point to name of input
649b50d902SRodney W. Grimes 					 * file */
659b50d902SRodney W. Grimes char       *out_name = "Standard Output";	/* will always point to name
669b50d902SRodney W. Grimes 						 * of output file */
679b50d902SRodney W. Grimes char        bakfile[MAXPATHLEN] = "";
689b50d902SRodney W. Grimes 
69958d7c9fSPhilippe Charnier static void usage __P((void));
70958d7c9fSPhilippe Charnier 
719b50d902SRodney W. Grimes main(argc, argv)
729b50d902SRodney W. Grimes     int         argc;
739b50d902SRodney W. Grimes     char      **argv;
749b50d902SRodney W. Grimes {
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes     extern int  found_err;	/* flag set in diag() on error */
779b50d902SRodney W. Grimes     int         dec_ind;	/* current indentation for declarations */
789b50d902SRodney W. Grimes     int         di_stack[20];	/* a stack of structure indentation levels */
799b50d902SRodney W. Grimes     int         flushed_nl;	/* used when buffering up comments to remember
809b50d902SRodney W. Grimes 				 * that a newline was passed over */
819b50d902SRodney W. Grimes     int         force_nl;	/* when true, code must be broken */
829b50d902SRodney W. Grimes     int         hd_type;	/* used to store type of stmt for if (...),
839b50d902SRodney W. Grimes 				 * for (...), etc */
849b50d902SRodney W. Grimes     register int i;		/* local loop counter */
859b50d902SRodney W. Grimes     int         scase;		/* set to true when we see a case, so we will
869b50d902SRodney W. Grimes 				 * know what to do with the following colon */
879b50d902SRodney W. Grimes     int         sp_sw;		/* when true, we are in the expressin of
889b50d902SRodney W. Grimes 				 * if(...), while(...), etc. */
899b50d902SRodney W. Grimes     int         squest;		/* when this is positive, we have seen a ?
909b50d902SRodney W. Grimes 				 * without the matching : in a <c>?<s>:<s>
919b50d902SRodney W. Grimes 				 * construct */
929b50d902SRodney W. Grimes     register char *t_ptr;	/* used for copying tokens */
939b50d902SRodney W. Grimes     int         type_code;	/* the type of token, returned by lexi */
949b50d902SRodney W. Grimes 
959b50d902SRodney W. Grimes     int         last_else = 0;	/* true iff last keyword was an else */
969b50d902SRodney W. Grimes 
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes     /*-----------------------------------------------*\
999b50d902SRodney W. Grimes     |		      INITIALIZATION		      |
1009b50d902SRodney W. Grimes     \*-----------------------------------------------*/
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes     ps.p_stack[0] = stmt;	/* this is the parser's stack */
1049b50d902SRodney W. Grimes     ps.last_nl = true;		/* this is true if the last thing scanned was
1059b50d902SRodney W. Grimes 				 * a newline */
1069b50d902SRodney W. Grimes     ps.last_token = semicolon;
1079b50d902SRodney W. Grimes     combuf = (char *) malloc(bufsize);
1089b50d902SRodney W. Grimes     labbuf = (char *) malloc(bufsize);
1099b50d902SRodney W. Grimes     codebuf = (char *) malloc(bufsize);
1109b50d902SRodney W. Grimes     tokenbuf = (char *) malloc(bufsize);
1119b50d902SRodney W. Grimes     l_com = combuf + bufsize - 5;
1129b50d902SRodney W. Grimes     l_lab = labbuf + bufsize - 5;
1139b50d902SRodney W. Grimes     l_code = codebuf + bufsize - 5;
1149b50d902SRodney W. Grimes     l_token = tokenbuf + bufsize - 5;
1159b50d902SRodney W. Grimes     combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
1169b50d902SRodney W. Grimes 						 * comment buffers */
1179b50d902SRodney W. Grimes     combuf[1] = codebuf[1] = labbuf[1] = '\0';
1189b50d902SRodney W. Grimes     ps.else_if = 1;		/* Default else-if special processing to on */
1199b50d902SRodney W. Grimes     s_lab = e_lab = labbuf + 1;
1209b50d902SRodney W. Grimes     s_code = e_code = codebuf + 1;
1219b50d902SRodney W. Grimes     s_com = e_com = combuf + 1;
1229b50d902SRodney W. Grimes     s_token = e_token = tokenbuf + 1;
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes     in_buffer = (char *) malloc(10);
1259b50d902SRodney W. Grimes     in_buffer_limit = in_buffer + 8;
1269b50d902SRodney W. Grimes     buf_ptr = buf_end = in_buffer;
1279b50d902SRodney W. Grimes     line_no = 1;
1289b50d902SRodney W. Grimes     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
1299b50d902SRodney W. Grimes     sp_sw = force_nl = false;
1309b50d902SRodney W. Grimes     ps.in_or_st = false;
1319b50d902SRodney W. Grimes     ps.bl_line = true;
1329b50d902SRodney W. Grimes     dec_ind = 0;
1339b50d902SRodney W. Grimes     di_stack[ps.dec_nest = 0] = 0;
1349b50d902SRodney W. Grimes     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
1359b50d902SRodney W. Grimes 
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes     scase = ps.pcase = false;
1389b50d902SRodney W. Grimes     squest = 0;
1399b50d902SRodney W. Grimes     sc_end = 0;
1409b50d902SRodney W. Grimes     bp_save = 0;
1419b50d902SRodney W. Grimes     be_save = 0;
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes     output = 0;
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes     /*--------------------------------------------------*\
1489b50d902SRodney W. Grimes     |   		COMMAND LINE SCAN		 |
1499b50d902SRodney W. Grimes     \*--------------------------------------------------*/
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes #ifdef undef
1529b50d902SRodney W. Grimes     max_col = 78;		/* -l78 */
1539b50d902SRodney W. Grimes     lineup_to_parens = 1;	/* -lp */
1549b50d902SRodney W. Grimes     ps.ljust_decl = 0;		/* -ndj */
1559b50d902SRodney W. Grimes     ps.com_ind = 33;		/* -c33 */
1569b50d902SRodney W. Grimes     star_comment_cont = 1;	/* -sc */
1579b50d902SRodney W. Grimes     ps.ind_size = 8;		/* -i8 */
1589b50d902SRodney W. Grimes     verbose = 0;
1599b50d902SRodney W. Grimes     ps.decl_indent = 16;	/* -di16 */
1609b50d902SRodney W. Grimes     ps.indent_parameters = 1;	/* -ip */
1619b50d902SRodney W. Grimes     ps.decl_com_ind = 0;	/* if this is not set to some positive value
1629b50d902SRodney W. Grimes 				 * by an arg, we will set this equal to
1639b50d902SRodney W. Grimes 				 * ps.com_ind */
1649b50d902SRodney W. Grimes     btype_2 = 1;		/* -br */
1659b50d902SRodney W. Grimes     cuddle_else = 1;		/* -ce */
1669b50d902SRodney W. Grimes     ps.unindent_displace = 0;	/* -d0 */
1679b50d902SRodney W. Grimes     ps.case_indent = 0;		/* -cli0 */
168a5e1cac0SDavid E. O'Brien     format_block_comments = 1;	/* -fcb */
1699b50d902SRodney W. Grimes     format_col1_comments = 1;	/* -fc1 */
1709b50d902SRodney W. Grimes     procnames_start_line = 1;	/* -psl */
1719b50d902SRodney W. Grimes     proc_calls_space = 0;	/* -npcs */
1729b50d902SRodney W. Grimes     comment_delimiter_on_blankline = 1;	/* -cdb */
1739b50d902SRodney W. Grimes     ps.leave_comma = 1;		/* -nbc */
1749b50d902SRodney W. Grimes #endif
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i)
1779b50d902SRodney W. Grimes 	if (strcmp(argv[i], "-npro") == 0)
1789b50d902SRodney W. Grimes 	    break;
1799b50d902SRodney W. Grimes     set_defaults();
1809b50d902SRodney W. Grimes     if (i >= argc)
1819b50d902SRodney W. Grimes 	set_profile();
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes     for (i = 1; i < argc; ++i) {
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 	/*
1869b50d902SRodney W. Grimes 	 * look thru args (if any) for changes to defaults
1879b50d902SRodney W. Grimes 	 */
1889b50d902SRodney W. Grimes 	if (argv[i][0] != '-') {/* no flag on parameter */
1899b50d902SRodney W. Grimes 	    if (input == 0) {	/* we must have the input file */
1909b50d902SRodney W. Grimes 		in_name = argv[i];	/* remember name of input file */
1919b50d902SRodney W. Grimes 		input = fopen(in_name, "r");
1929b50d902SRodney W. Grimes 		if (input == 0)		/* check for open error */
1930c4d24a7SKris Kennaway 			err(1, "%s", in_name);
1949b50d902SRodney W. Grimes 		continue;
1959b50d902SRodney W. Grimes 	    }
1969b50d902SRodney W. Grimes 	    else if (output == 0) {	/* we have the output file */
1979b50d902SRodney W. Grimes 		out_name = argv[i];	/* remember name of output file */
1989b50d902SRodney W. Grimes 		if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
1999b50d902SRodney W. Grimes 							 * the file */
200958d7c9fSPhilippe Charnier 		    errx(1, "input and output files must be different");
2019b50d902SRodney W. Grimes 		}
2029b50d902SRodney W. Grimes 		output = fopen(out_name, "w");
2039b50d902SRodney W. Grimes 		if (output == 0)	/* check for create error */
2040c4d24a7SKris Kennaway 			err(1, "%s", out_name);
2059b50d902SRodney W. Grimes 		continue;
2069b50d902SRodney W. Grimes 	    }
207958d7c9fSPhilippe Charnier 	    errx(1, "unknown parameter: %s", argv[i]);
2089b50d902SRodney W. Grimes 	}
2099b50d902SRodney W. Grimes 	else
2109b50d902SRodney W. Grimes 	    set_option(argv[i]);
2119b50d902SRodney W. Grimes     }				/* end of for */
2129ef5c48bSBill Fumerola     if (input == 0)
21318251d71SPeter Hawkins 	input = stdin;
2149ef5c48bSBill Fumerola     if (output == 0) {
21518251d71SPeter Hawkins 	if (troff || input == stdin)
2169b50d902SRodney W. Grimes 	    output = stdout;
2179b50d902SRodney W. Grimes 	else {
2189b50d902SRodney W. Grimes 	    out_name = in_name;
2199b50d902SRodney W. Grimes 	    bakcopy();
2209b50d902SRodney W. Grimes 	}
2219ef5c48bSBill Fumerola     }
2229b50d902SRodney W. Grimes     if (ps.com_ind <= 1)
2239b50d902SRodney W. Grimes 	ps.com_ind = 2;		/* dont put normal comments before column 2 */
2249b50d902SRodney W. Grimes     if (troff) {
2259b50d902SRodney W. Grimes 	if (bodyf.font[0] == 0)
2269b50d902SRodney W. Grimes 	    parsefont(&bodyf, "R");
2279b50d902SRodney W. Grimes 	if (scomf.font[0] == 0)
2289b50d902SRodney W. Grimes 	    parsefont(&scomf, "I");
2299b50d902SRodney W. Grimes 	if (blkcomf.font[0] == 0)
2309b50d902SRodney W. Grimes 	    blkcomf = scomf, blkcomf.size += 2;
2319b50d902SRodney W. Grimes 	if (boxcomf.font[0] == 0)
2329b50d902SRodney W. Grimes 	    boxcomf = blkcomf;
2339b50d902SRodney W. Grimes 	if (stringf.font[0] == 0)
2349b50d902SRodney W. Grimes 	    parsefont(&stringf, "L");
2359b50d902SRodney W. Grimes 	if (keywordf.font[0] == 0)
2369b50d902SRodney W. Grimes 	    parsefont(&keywordf, "B");
2379b50d902SRodney W. Grimes 	writefdef(&bodyf, 'B');
2389b50d902SRodney W. Grimes 	writefdef(&scomf, 'C');
2399b50d902SRodney W. Grimes 	writefdef(&blkcomf, 'L');
2409b50d902SRodney W. Grimes 	writefdef(&boxcomf, 'X');
2419b50d902SRodney W. Grimes 	writefdef(&stringf, 'S');
2429b50d902SRodney W. Grimes 	writefdef(&keywordf, 'K');
2439b50d902SRodney W. Grimes     }
2449b50d902SRodney W. Grimes     if (block_comment_max_col <= 0)
2459b50d902SRodney W. Grimes 	block_comment_max_col = max_col;
2469b50d902SRodney W. Grimes     if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
2479b50d902SRodney W. Grimes 	ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
2489b50d902SRodney W. Grimes     if (continuation_indent == 0)
2499b50d902SRodney W. Grimes 	continuation_indent = ps.ind_size;
2509b50d902SRodney W. Grimes     fill_buffer();		/* get first batch of stuff into input buffer */
2519b50d902SRodney W. Grimes 
2529b50d902SRodney W. Grimes     parse(semicolon);
2539b50d902SRodney W. Grimes     {
2549b50d902SRodney W. Grimes 	register char *p = buf_ptr;
2559b50d902SRodney W. Grimes 	register    col = 1;
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes 	while (1) {
2589b50d902SRodney W. Grimes 	    if (*p == ' ')
2599b50d902SRodney W. Grimes 		col++;
2609b50d902SRodney W. Grimes 	    else if (*p == '\t')
2619b50d902SRodney W. Grimes 		col = ((col - 1) & ~7) + 9;
2629b50d902SRodney W. Grimes 	    else
2639b50d902SRodney W. Grimes 		break;
2649b50d902SRodney W. Grimes 	    p++;
2659b50d902SRodney W. Grimes 	}
2669b50d902SRodney W. Grimes 	if (col > ps.ind_size)
2679b50d902SRodney W. Grimes 	    ps.ind_level = ps.i_l_follow = col / ps.ind_size;
2689b50d902SRodney W. Grimes     }
2699b50d902SRodney W. Grimes     if (troff) {
2709b50d902SRodney W. Grimes 	register char *p = in_name,
2719b50d902SRodney W. Grimes 	           *beg = in_name;
2729b50d902SRodney W. Grimes 
2739b50d902SRodney W. Grimes 	while (*p)
2749b50d902SRodney W. Grimes 	    if (*p++ == '/')
2759b50d902SRodney W. Grimes 		beg = p;
2769b50d902SRodney W. Grimes 	fprintf(output, ".Fn \"%s\"\n", beg);
2779b50d902SRodney W. Grimes     }
2789b50d902SRodney W. Grimes     /*
2799b50d902SRodney W. Grimes      * START OF MAIN LOOP
2809b50d902SRodney W. Grimes      */
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes     while (1) {			/* this is the main loop.  it will go until we
2839b50d902SRodney W. Grimes 				 * reach eof */
2849b50d902SRodney W. Grimes 	int         is_procname;
2859b50d902SRodney W. Grimes 
2869b50d902SRodney W. Grimes 	type_code = lexi();	/* lexi reads one token.  The actual
2879b50d902SRodney W. Grimes 				 * characters read are stored in "token". lexi
2889b50d902SRodney W. Grimes 				 * returns a code indicating the type of token */
2899b50d902SRodney W. Grimes 	is_procname = ps.procname[0];
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	/*
2929b50d902SRodney W. Grimes 	 * The following code moves everything following an if (), while (),
2939b50d902SRodney W. Grimes 	 * else, etc. up to the start of the following stmt to a buffer. This
2949b50d902SRodney W. Grimes 	 * allows proper handling of both kinds of brace placement.
2959b50d902SRodney W. Grimes 	 */
2969b50d902SRodney W. Grimes 
2979b50d902SRodney W. Grimes 	flushed_nl = false;
2989b50d902SRodney W. Grimes 	while (ps.search_brace) {	/* if we scanned an if(), while(),
2999b50d902SRodney W. Grimes 					 * etc., we might need to copy stuff
3009b50d902SRodney W. Grimes 					 * into a buffer we must loop, copying
3019b50d902SRodney W. Grimes 					 * stuff into save_com, until we find
3029b50d902SRodney W. Grimes 					 * the start of the stmt which follows
3039b50d902SRodney W. Grimes 					 * the if, or whatever */
3049b50d902SRodney W. Grimes 	    switch (type_code) {
3059b50d902SRodney W. Grimes 	    case newline:
3069b50d902SRodney W. Grimes 		++line_no;
3079b50d902SRodney W. Grimes 		flushed_nl = true;
3089b50d902SRodney W. Grimes 	    case form_feed:
3099b50d902SRodney W. Grimes 		break;		/* form feeds and newlines found here will be
3109b50d902SRodney W. Grimes 				 * ignored */
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	    case lbrace:	/* this is a brace that starts the compound
3139b50d902SRodney W. Grimes 				 * stmt */
3149b50d902SRodney W. Grimes 		if (sc_end == 0) {	/* ignore buffering if a comment wasnt
3159b50d902SRodney W. Grimes 					 * stored up */
3169b50d902SRodney W. Grimes 		    ps.search_brace = false;
3179b50d902SRodney W. Grimes 		    goto check_type;
3189b50d902SRodney W. Grimes 		}
3199b50d902SRodney W. Grimes 		if (btype_2) {
3209b50d902SRodney W. Grimes 		    save_com[0] = '{';	/* we either want to put the brace
3219b50d902SRodney W. Grimes 					 * right after the if */
3229b50d902SRodney W. Grimes 		    goto sw_buffer;	/* go to common code to get out of
3239b50d902SRodney W. Grimes 					 * this loop */
3249b50d902SRodney W. Grimes 		}
3259b50d902SRodney W. Grimes 	    case comment:	/* we have a comment, so we must copy it into
3269b50d902SRodney W. Grimes 				 * the buffer */
3279b50d902SRodney W. Grimes 		if (!flushed_nl || sc_end != 0) {
3289b50d902SRodney W. Grimes 		    if (sc_end == 0) {	/* if this is the first comment, we
3299b50d902SRodney W. Grimes 					 * must set up the buffer */
3309b50d902SRodney W. Grimes 			save_com[0] = save_com[1] = ' ';
3319b50d902SRodney W. Grimes 			sc_end = &(save_com[2]);
3329b50d902SRodney W. Grimes 		    }
3339b50d902SRodney W. Grimes 		    else {
3349b50d902SRodney W. Grimes 			*sc_end++ = '\n';	/* add newline between
3359b50d902SRodney W. Grimes 						 * comments */
3369b50d902SRodney W. Grimes 			*sc_end++ = ' ';
3379b50d902SRodney W. Grimes 			--line_no;
3389b50d902SRodney W. Grimes 		    }
3399b50d902SRodney W. Grimes 		    *sc_end++ = '/';	/* copy in start of comment */
3409b50d902SRodney W. Grimes 		    *sc_end++ = '*';
3419b50d902SRodney W. Grimes 
3429b50d902SRodney W. Grimes 		    for (;;) {	/* loop until we get to the end of the comment */
3439b50d902SRodney W. Grimes 			*sc_end = *buf_ptr++;
3449b50d902SRodney W. Grimes 			if (buf_ptr >= buf_end)
3459b50d902SRodney W. Grimes 			    fill_buffer();
3469b50d902SRodney W. Grimes 
3479b50d902SRodney W. Grimes 			if (*sc_end++ == '*' && *buf_ptr == '/')
3489b50d902SRodney W. Grimes 			    break;	/* we are at end of comment */
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 			if (sc_end >= &(save_com[sc_size])) {	/* check for temp buffer
3519b50d902SRodney W. Grimes 								 * overflow */
3529b50d902SRodney W. Grimes 			    diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
3539b50d902SRodney W. Grimes 			    fflush(output);
3549b50d902SRodney W. Grimes 			    exit(1);
3559b50d902SRodney W. Grimes 			}
3569b50d902SRodney W. Grimes 		    }
3579b50d902SRodney W. Grimes 		    *sc_end++ = '/';	/* add ending slash */
3589b50d902SRodney W. Grimes 		    if (++buf_ptr >= buf_end)	/* get past / in buffer */
3599b50d902SRodney W. Grimes 			fill_buffer();
3609b50d902SRodney W. Grimes 		    break;
3619b50d902SRodney W. Grimes 		}
3629b50d902SRodney W. Grimes 	    default:		/* it is the start of a normal statment */
3639b50d902SRodney W. Grimes 		if (flushed_nl)	/* if we flushed a newline, make sure it is
3649b50d902SRodney W. Grimes 				 * put back */
3659b50d902SRodney W. Grimes 		    force_nl = true;
3669b50d902SRodney W. Grimes 		if (type_code == sp_paren && *token == 'i'
3679b50d902SRodney W. Grimes 			&& last_else && ps.else_if
3689b50d902SRodney W. Grimes 			|| type_code == sp_nparen && *token == 'e'
3699b50d902SRodney W. Grimes 			&& e_code != s_code && e_code[-1] == '}')
3709b50d902SRodney W. Grimes 		    force_nl = false;
3719b50d902SRodney W. Grimes 
3729b50d902SRodney W. Grimes 		if (sc_end == 0) {	/* ignore buffering if comment wasnt
3739b50d902SRodney W. Grimes 					 * saved up */
3749b50d902SRodney W. Grimes 		    ps.search_brace = false;
3759b50d902SRodney W. Grimes 		    goto check_type;
3769b50d902SRodney W. Grimes 		}
3779b50d902SRodney W. Grimes 		if (force_nl) {	/* if we should insert a nl here, put it into
3789b50d902SRodney W. Grimes 				 * the buffer */
3799b50d902SRodney W. Grimes 		    force_nl = false;
3809b50d902SRodney W. Grimes 		    --line_no;	/* this will be re-increased when the nl is
3819b50d902SRodney W. Grimes 				 * read from the buffer */
3829b50d902SRodney W. Grimes 		    *sc_end++ = '\n';
3839b50d902SRodney W. Grimes 		    *sc_end++ = ' ';
3849b50d902SRodney W. Grimes 		    if (verbose && !flushed_nl)	/* print error msg if the line
3859b50d902SRodney W. Grimes 						 * was not already broken */
3869b50d902SRodney W. Grimes 			diag(0, "Line broken");
3879b50d902SRodney W. Grimes 		    flushed_nl = false;
3889b50d902SRodney W. Grimes 		}
3899b50d902SRodney W. Grimes 		for (t_ptr = token; *t_ptr; ++t_ptr)
3909b50d902SRodney W. Grimes 		    *sc_end++ = *t_ptr;	/* copy token into temp buffer */
3919b50d902SRodney W. Grimes 		ps.procname[0] = 0;
3929b50d902SRodney W. Grimes 
3939b50d902SRodney W. Grimes 	sw_buffer:
3949b50d902SRodney W. Grimes 		ps.search_brace = false;	/* stop looking for start of
3959b50d902SRodney W. Grimes 						 * stmt */
3969b50d902SRodney W. Grimes 		bp_save = buf_ptr;	/* save current input buffer */
3979b50d902SRodney W. Grimes 		be_save = buf_end;
3989b50d902SRodney W. Grimes 		buf_ptr = save_com;	/* fix so that subsequent calls to
3999b50d902SRodney W. Grimes 					 * lexi will take tokens out of
4009b50d902SRodney W. Grimes 					 * save_com */
4019b50d902SRodney W. Grimes 		*sc_end++ = ' ';/* add trailing blank, just in case */
4029b50d902SRodney W. Grimes 		buf_end = sc_end;
4039b50d902SRodney W. Grimes 		sc_end = 0;
4049b50d902SRodney W. Grimes 		break;
4059b50d902SRodney W. Grimes 	    }			/* end of switch */
4069b50d902SRodney W. Grimes 	    if (type_code != 0)	/* we must make this check, just in case there
4079b50d902SRodney W. Grimes 				 * was an unexpected EOF */
4089b50d902SRodney W. Grimes 		type_code = lexi();	/* read another token */
4099b50d902SRodney W. Grimes 	    /* if (ps.search_brace) ps.procname[0] = 0; */
4109b50d902SRodney W. Grimes 	    if ((is_procname = ps.procname[0]) && flushed_nl
4119b50d902SRodney W. Grimes 		    && !procnames_start_line && ps.in_decl
4129b50d902SRodney W. Grimes 		    && type_code == ident)
4139b50d902SRodney W. Grimes 		flushed_nl = 0;
4149b50d902SRodney W. Grimes 	}			/* end of while (search_brace) */
4159b50d902SRodney W. Grimes 	last_else = 0;
4169b50d902SRodney W. Grimes check_type:
4179b50d902SRodney W. Grimes 	if (type_code == 0) {	/* we got eof */
4189b50d902SRodney W. Grimes 	    if (s_lab != e_lab || s_code != e_code
4199b50d902SRodney W. Grimes 		    || s_com != e_com)	/* must dump end of line */
4209b50d902SRodney W. Grimes 		dump_line();
4219b50d902SRodney W. Grimes 	    if (ps.tos > 1)	/* check for balanced braces */
4229b50d902SRodney W. Grimes 		diag(1, "Stuff missing from end of file.");
4239b50d902SRodney W. Grimes 
4249b50d902SRodney W. Grimes 	    if (verbose) {
4259b50d902SRodney W. Grimes 		printf("There were %d output lines and %d comments\n",
4269b50d902SRodney W. Grimes 		       ps.out_lines, ps.out_coms);
4279b50d902SRodney W. Grimes 		printf("(Lines with comments)/(Lines with code): %6.3f\n",
4289b50d902SRodney W. Grimes 		       (1.0 * ps.com_lines) / code_lines);
4299b50d902SRodney W. Grimes 	    }
4309b50d902SRodney W. Grimes 	    fflush(output);
4319b50d902SRodney W. Grimes 	    exit(found_err);
4329b50d902SRodney W. Grimes 	}
4339b50d902SRodney W. Grimes 	if (
4349b50d902SRodney W. Grimes 		(type_code != comment) &&
4359b50d902SRodney W. Grimes 		(type_code != newline) &&
4369b50d902SRodney W. Grimes 		(type_code != preesc) &&
4379b50d902SRodney W. Grimes 		(type_code != form_feed)) {
4389b50d902SRodney W. Grimes 	    if (force_nl &&
4399b50d902SRodney W. Grimes 		    (type_code != semicolon) &&
4409b50d902SRodney W. Grimes 		    (type_code != lbrace || !btype_2)) {
4419b50d902SRodney W. Grimes 		/* we should force a broken line here */
4429b50d902SRodney W. Grimes 		if (verbose && !flushed_nl)
4439b50d902SRodney W. Grimes 		    diag(0, "Line broken");
4449b50d902SRodney W. Grimes 		flushed_nl = false;
4459b50d902SRodney W. Grimes 		dump_line();
4469b50d902SRodney W. Grimes 		ps.want_blank = false;	/* dont insert blank at line start */
4479b50d902SRodney W. Grimes 		force_nl = false;
4489b50d902SRodney W. Grimes 	    }
4499b50d902SRodney W. Grimes 	    ps.in_stmt = true;	/* turn on flag which causes an extra level of
4509b50d902SRodney W. Grimes 				 * indentation. this is turned off by a ; or
4519b50d902SRodney W. Grimes 				 * '}' */
4529b50d902SRodney W. Grimes 	    if (s_com != e_com) {	/* the turkey has embedded a comment
4539b50d902SRodney W. Grimes 					 * in a line. fix it */
4549b50d902SRodney W. Grimes 		*e_code++ = ' ';
4559b50d902SRodney W. Grimes 		for (t_ptr = s_com; *t_ptr; ++t_ptr) {
4569b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
4579b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
4589b50d902SRodney W. Grimes 		}
4599b50d902SRodney W. Grimes 		*e_code++ = ' ';
4609b50d902SRodney W. Grimes 		*e_code = '\0';	/* null terminate code sect */
4619b50d902SRodney W. Grimes 		ps.want_blank = false;
4629b50d902SRodney W. Grimes 		e_com = s_com;
4639b50d902SRodney W. Grimes 	    }
4649b50d902SRodney W. Grimes 	}
4659b50d902SRodney W. Grimes 	else if (type_code != comment)	/* preserve force_nl thru a comment */
4669b50d902SRodney W. Grimes 	    force_nl = false;	/* cancel forced newline after newline, form
4679b50d902SRodney W. Grimes 				 * feed, etc */
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 
4709b50d902SRodney W. Grimes 
4719b50d902SRodney W. Grimes 	/*-----------------------------------------------------*\
4729b50d902SRodney W. Grimes 	|	   do switch on type of token scanned		|
4739b50d902SRodney W. Grimes 	\*-----------------------------------------------------*/
4749b50d902SRodney W. Grimes 	CHECK_SIZE_CODE;
4759b50d902SRodney W. Grimes 	switch (type_code) {	/* now, decide what to do with the token */
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes 	case form_feed:	/* found a form feed in line */
4789b50d902SRodney W. Grimes 	    ps.use_ff = true;	/* a form feed is treated much like a newline */
4799b50d902SRodney W. Grimes 	    dump_line();
4809b50d902SRodney W. Grimes 	    ps.want_blank = false;
4819b50d902SRodney W. Grimes 	    break;
4829b50d902SRodney W. Grimes 
4839b50d902SRodney W. Grimes 	case newline:
4849b50d902SRodney W. Grimes 	    if (ps.last_token != comma || ps.p_l_follow > 0
4859b50d902SRodney W. Grimes 		    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
4869b50d902SRodney W. Grimes 		dump_line();
4879b50d902SRodney W. Grimes 		ps.want_blank = false;
4889b50d902SRodney W. Grimes 	    }
4899b50d902SRodney W. Grimes 	    ++line_no;		/* keep track of input line number */
4909b50d902SRodney W. Grimes 	    break;
4919b50d902SRodney W. Grimes 
4929b50d902SRodney W. Grimes 	case lparen:		/* got a '(' or '[' */
4939b50d902SRodney W. Grimes 	    ++ps.p_l_follow;	/* count parens to make Healy happy */
4949b50d902SRodney W. Grimes 	    if (ps.want_blank && *token != '[' &&
4959b50d902SRodney W. Grimes 		    (ps.last_token != ident || proc_calls_space
4969b50d902SRodney W. Grimes 	      || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
4979b50d902SRodney W. Grimes 		*e_code++ = ' ';
4989b50d902SRodney W. Grimes 	    if (ps.in_decl && !ps.block_init)
4999b50d902SRodney W. Grimes 		if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
5009b50d902SRodney W. Grimes 		    ps.dumped_decl_indent = 1;
5019b50d902SRodney W. Grimes 		    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
5029b50d902SRodney W. Grimes 		    e_code += strlen(e_code);
5039b50d902SRodney W. Grimes 		}
5049b50d902SRodney W. Grimes 		else {
5059b50d902SRodney W. Grimes 		    while ((e_code - s_code) < dec_ind) {
5069b50d902SRodney W. Grimes 			CHECK_SIZE_CODE;
5079b50d902SRodney W. Grimes 			*e_code++ = ' ';
5089b50d902SRodney W. Grimes 		    }
5099b50d902SRodney W. Grimes 		    *e_code++ = token[0];
5109b50d902SRodney W. Grimes 		}
5119b50d902SRodney W. Grimes 	    else
5129b50d902SRodney W. Grimes 		*e_code++ = token[0];
5139b50d902SRodney W. Grimes 	    ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
5149b50d902SRodney W. Grimes 	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
5159b50d902SRodney W. Grimes 		    && ps.paren_indents[0] < 2 * ps.ind_size)
5169b50d902SRodney W. Grimes 		ps.paren_indents[0] = 2 * ps.ind_size;
5179b50d902SRodney W. Grimes 	    ps.want_blank = false;
5189b50d902SRodney W. Grimes 	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
5199b50d902SRodney W. Grimes 		/*
5209b50d902SRodney W. Grimes 		 * this is a kluge to make sure that declarations will be
5219b50d902SRodney W. Grimes 		 * aligned right if proc decl has an explicit type on it, i.e.
5229b50d902SRodney W. Grimes 		 * "int a(x) {..."
5239b50d902SRodney W. Grimes 		 */
5249b50d902SRodney W. Grimes 		parse(semicolon);	/* I said this was a kluge... */
5259b50d902SRodney W. Grimes 		ps.in_or_st = false;	/* turn off flag for structure decl or
5269b50d902SRodney W. Grimes 					 * initialization */
5279b50d902SRodney W. Grimes 	    }
5289b50d902SRodney W. Grimes 	    if (ps.sizeof_keyword)
5299b50d902SRodney W. Grimes 		ps.sizeof_mask |= 1 << ps.p_l_follow;
5309b50d902SRodney W. Grimes 	    break;
5319b50d902SRodney W. Grimes 
5329b50d902SRodney W. Grimes 	case rparen:		/* got a ')' or ']' */
5339b50d902SRodney W. Grimes 	    rparen_count--;
5349b50d902SRodney W. Grimes 	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
5359b50d902SRodney W. Grimes 		ps.last_u_d = true;
5369b50d902SRodney W. Grimes 		ps.cast_mask &= (1 << ps.p_l_follow) - 1;
537a5e1cac0SDavid E. O'Brien 		ps.want_blank = false;
538a5e1cac0SDavid E. O'Brien 	    } else
539a5e1cac0SDavid E. O'Brien 		ps.want_blank = true;
5409b50d902SRodney W. Grimes 	    ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
5419b50d902SRodney W. Grimes 	    if (--ps.p_l_follow < 0) {
5429b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
5439b50d902SRodney W. Grimes 		diag(0, "Extra %c", *token);
5449b50d902SRodney W. Grimes 	    }
5459b50d902SRodney W. Grimes 	    if (e_code == s_code)	/* if the paren starts the line */
5469b50d902SRodney W. Grimes 		ps.paren_level = ps.p_l_follow;	/* then indent it */
5479b50d902SRodney W. Grimes 
5489b50d902SRodney W. Grimes 	    *e_code++ = token[0];
5499b50d902SRodney W. Grimes 
5509b50d902SRodney W. Grimes 	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
5519b50d902SRodney W. Grimes 							 * (...), or some such */
5529b50d902SRodney W. Grimes 		sp_sw = false;
5539b50d902SRodney W. Grimes 		force_nl = true;/* must force newline after if */
5549b50d902SRodney W. Grimes 		ps.last_u_d = true;	/* inform lexi that a following
5559b50d902SRodney W. Grimes 					 * operator is unary */
5569b50d902SRodney W. Grimes 		ps.in_stmt = false;	/* dont use stmt continuation
5579b50d902SRodney W. Grimes 					 * indentation */
5589b50d902SRodney W. Grimes 
5599b50d902SRodney W. Grimes 		parse(hd_type);	/* let parser worry about if, or whatever */
5609b50d902SRodney W. Grimes 	    }
5619b50d902SRodney W. Grimes 	    ps.search_brace = btype_2;	/* this should insure that constructs
5629b50d902SRodney W. Grimes 					 * such as main(){...} and int[]{...}
5639b50d902SRodney W. Grimes 					 * have their braces put in the right
5649b50d902SRodney W. Grimes 					 * place */
5659b50d902SRodney W. Grimes 	    break;
5669b50d902SRodney W. Grimes 
5679b50d902SRodney W. Grimes 	case unary_op:		/* this could be any unary operation */
5689b50d902SRodney W. Grimes 	    if (ps.want_blank)
5699b50d902SRodney W. Grimes 		*e_code++ = ' ';
5709b50d902SRodney W. Grimes 
5719b50d902SRodney W. Grimes 	    if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
5729b50d902SRodney W. Grimes 		sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
5739b50d902SRodney W. Grimes 		ps.dumped_decl_indent = 1;
5749b50d902SRodney W. Grimes 		e_code += strlen(e_code);
5759b50d902SRodney W. Grimes 	    }
5769b50d902SRodney W. Grimes 	    else {
5779b50d902SRodney W. Grimes 		char       *res = token;
5789b50d902SRodney W. Grimes 
5799b50d902SRodney W. Grimes 		if (ps.in_decl && !ps.block_init) {	/* if this is a unary op
5809b50d902SRodney W. Grimes 							 * in a declaration, we
5819b50d902SRodney W. Grimes 							 * should indent this
5829b50d902SRodney W. Grimes 							 * token */
5839b50d902SRodney W. Grimes 		    for (i = 0; token[i]; ++i);	/* find length of token */
5849b50d902SRodney W. Grimes 		    while ((e_code - s_code) < (dec_ind - i)) {
5859b50d902SRodney W. Grimes 			CHECK_SIZE_CODE;
5869b50d902SRodney W. Grimes 			*e_code++ = ' ';	/* pad it */
5879b50d902SRodney W. Grimes 		    }
5889b50d902SRodney W. Grimes 		}
5899b50d902SRodney W. Grimes 		if (troff && token[0] == '-' && token[1] == '>')
5909b50d902SRodney W. Grimes 		    res = "\\(->";
5919b50d902SRodney W. Grimes 		for (t_ptr = res; *t_ptr; ++t_ptr) {
5929b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
5939b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
5949b50d902SRodney W. Grimes 		}
5959b50d902SRodney W. Grimes 	    }
5969b50d902SRodney W. Grimes 	    ps.want_blank = false;
5979b50d902SRodney W. Grimes 	    break;
5989b50d902SRodney W. Grimes 
5999b50d902SRodney W. Grimes 	case binary_op:	/* any binary operation */
6009b50d902SRodney W. Grimes 	    if (ps.want_blank)
6019b50d902SRodney W. Grimes 		*e_code++ = ' ';
6029b50d902SRodney W. Grimes 	    {
6039b50d902SRodney W. Grimes 		char       *res = token;
6049b50d902SRodney W. Grimes 
6059b50d902SRodney W. Grimes 		if (troff)
6069b50d902SRodney W. Grimes 		    switch (token[0]) {
6079b50d902SRodney W. Grimes 		    case '<':
6089b50d902SRodney W. Grimes 			if (token[1] == '=')
6099b50d902SRodney W. Grimes 			    res = "\\(<=";
6109b50d902SRodney W. Grimes 			break;
6119b50d902SRodney W. Grimes 		    case '>':
6129b50d902SRodney W. Grimes 			if (token[1] == '=')
6139b50d902SRodney W. Grimes 			    res = "\\(>=";
6149b50d902SRodney W. Grimes 			break;
6159b50d902SRodney W. Grimes 		    case '!':
6169b50d902SRodney W. Grimes 			if (token[1] == '=')
6179b50d902SRodney W. Grimes 			    res = "\\(!=";
6189b50d902SRodney W. Grimes 			break;
6199b50d902SRodney W. Grimes 		    case '|':
6209b50d902SRodney W. Grimes 			if (token[1] == '|')
6219b50d902SRodney W. Grimes 			    res = "\\(br\\(br";
6229b50d902SRodney W. Grimes 			else if (token[1] == 0)
6239b50d902SRodney W. Grimes 			    res = "\\(br";
6249b50d902SRodney W. Grimes 			break;
6259b50d902SRodney W. Grimes 		    }
6269b50d902SRodney W. Grimes 		for (t_ptr = res; *t_ptr; ++t_ptr) {
6279b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
6289b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;	/* move the operator */
6299b50d902SRodney W. Grimes 		}
6309b50d902SRodney W. Grimes 	    }
6319b50d902SRodney W. Grimes 	    ps.want_blank = true;
6329b50d902SRodney W. Grimes 	    break;
6339b50d902SRodney W. Grimes 
6349b50d902SRodney W. Grimes 	case postop:		/* got a trailing ++ or -- */
6359b50d902SRodney W. Grimes 	    *e_code++ = token[0];
6369b50d902SRodney W. Grimes 	    *e_code++ = token[1];
6379b50d902SRodney W. Grimes 	    ps.want_blank = true;
6389b50d902SRodney W. Grimes 	    break;
6399b50d902SRodney W. Grimes 
6409b50d902SRodney W. Grimes 	case question:		/* got a ? */
6419b50d902SRodney W. Grimes 	    squest++;		/* this will be used when a later colon
6429b50d902SRodney W. Grimes 				 * appears so we can distinguish the
6439b50d902SRodney W. Grimes 				 * <c>?<n>:<n> construct */
6449b50d902SRodney W. Grimes 	    if (ps.want_blank)
6459b50d902SRodney W. Grimes 		*e_code++ = ' ';
6469b50d902SRodney W. Grimes 	    *e_code++ = '?';
6479b50d902SRodney W. Grimes 	    ps.want_blank = true;
6489b50d902SRodney W. Grimes 	    break;
6499b50d902SRodney W. Grimes 
6509b50d902SRodney W. Grimes 	case casestmt:		/* got word 'case' or 'default' */
6519b50d902SRodney W. Grimes 	    scase = true;	/* so we can process the later colon properly */
6529b50d902SRodney W. Grimes 	    goto copy_id;
6539b50d902SRodney W. Grimes 
6549b50d902SRodney W. Grimes 	case colon:		/* got a ':' */
6559b50d902SRodney W. Grimes 	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
6569b50d902SRodney W. Grimes 		--squest;
6579b50d902SRodney W. Grimes 		if (ps.want_blank)
6589b50d902SRodney W. Grimes 		    *e_code++ = ' ';
6599b50d902SRodney W. Grimes 		*e_code++ = ':';
6609b50d902SRodney W. Grimes 		ps.want_blank = true;
6619b50d902SRodney W. Grimes 		break;
6629b50d902SRodney W. Grimes 	    }
6639b50d902SRodney W. Grimes 	    if (ps.in_decl) {
6649b50d902SRodney W. Grimes 		*e_code++ = ':';
6659b50d902SRodney W. Grimes 		ps.want_blank = false;
6669b50d902SRodney W. Grimes 		break;
6679b50d902SRodney W. Grimes 	    }
6689b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* seeing a label does not imply we are in a
6699b50d902SRodney W. Grimes 				 * stmt */
6709b50d902SRodney W. Grimes 	    for (t_ptr = s_code; *t_ptr; ++t_ptr)
6719b50d902SRodney W. Grimes 		*e_lab++ = *t_ptr;	/* turn everything so far into a label */
6729b50d902SRodney W. Grimes 	    e_code = s_code;
6739b50d902SRodney W. Grimes 	    *e_lab++ = ':';
6749b50d902SRodney W. Grimes 	    *e_lab++ = ' ';
6759b50d902SRodney W. Grimes 	    *e_lab = '\0';
6769b50d902SRodney W. Grimes 
6779b50d902SRodney W. Grimes 	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by
6789b50d902SRodney W. Grimes 						 * dump_line to decide how to
6799b50d902SRodney W. Grimes 						 * indent the label. force_nl
6809b50d902SRodney W. Grimes 						 * will force a case n: to be
6819b50d902SRodney W. Grimes 						 * on a line by itself */
6829b50d902SRodney W. Grimes 	    scase = false;
6839b50d902SRodney W. Grimes 	    ps.want_blank = false;
6849b50d902SRodney W. Grimes 	    break;
6859b50d902SRodney W. Grimes 
6869b50d902SRodney W. Grimes 	case semicolon:	/* got a ';' */
6879b50d902SRodney W. Grimes 	    ps.in_or_st = false;/* we are not in an initialization or
6889b50d902SRodney W. Grimes 				 * structure declaration */
6899b50d902SRodney W. Grimes 	    scase = false;	/* these will only need resetting in a error */
6909b50d902SRodney W. Grimes 	    squest = 0;
6919b50d902SRodney W. Grimes 	    if (ps.last_token == rparen && rparen_count == 0)
6929b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
6939b50d902SRodney W. Grimes 	    ps.cast_mask = 0;
6949b50d902SRodney W. Grimes 	    ps.sizeof_mask = 0;
6959b50d902SRodney W. Grimes 	    ps.block_init = 0;
6969b50d902SRodney W. Grimes 	    ps.block_init_level = 0;
6979b50d902SRodney W. Grimes 	    ps.just_saw_decl--;
6989b50d902SRodney W. Grimes 
6999b50d902SRodney W. Grimes 	    if (ps.in_decl && s_code == e_code && !ps.block_init)
7009b50d902SRodney W. Grimes 		while ((e_code - s_code) < (dec_ind - 1)) {
7019b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
7029b50d902SRodney W. Grimes 		    *e_code++ = ' ';
7039b50d902SRodney W. Grimes 		}
7049b50d902SRodney W. Grimes 
7059b50d902SRodney W. Grimes 	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
7069b50d902SRodney W. Grimes 						 * structure declaration, we
7079b50d902SRodney W. Grimes 						 * arent any more */
7089b50d902SRodney W. Grimes 
7099b50d902SRodney W. Grimes 	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
7109b50d902SRodney W. Grimes 
7119b50d902SRodney W. Grimes 		/*
7129b50d902SRodney W. Grimes 		 * This should be true iff there were unbalanced parens in the
7139b50d902SRodney W. Grimes 		 * stmt.  It is a bit complicated, because the semicolon might
7149b50d902SRodney W. Grimes 		 * be in a for stmt
7159b50d902SRodney W. Grimes 		 */
7169b50d902SRodney W. Grimes 		diag(1, "Unbalanced parens");
7179b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
7189b50d902SRodney W. Grimes 		if (sp_sw) {	/* this is a check for a if, while, etc. with
7199b50d902SRodney W. Grimes 				 * unbalanced parens */
7209b50d902SRodney W. Grimes 		    sp_sw = false;
7219b50d902SRodney W. Grimes 		    parse(hd_type);	/* dont lose the if, or whatever */
7229b50d902SRodney W. Grimes 		}
7239b50d902SRodney W. Grimes 	    }
7249b50d902SRodney W. Grimes 	    *e_code++ = ';';
7259b50d902SRodney W. Grimes 	    ps.want_blank = true;
7269b50d902SRodney W. Grimes 	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
7279b50d902SRodney W. Grimes 						 * middle of a stmt */
7289b50d902SRodney W. Grimes 
7299b50d902SRodney W. Grimes 	    if (!sp_sw) {	/* if not if for (;;) */
7309b50d902SRodney W. Grimes 		parse(semicolon);	/* let parser know about end of stmt */
7319b50d902SRodney W. Grimes 		force_nl = true;/* force newline after a end of stmt */
7329b50d902SRodney W. Grimes 	    }
7339b50d902SRodney W. Grimes 	    break;
7349b50d902SRodney W. Grimes 
7359b50d902SRodney W. Grimes 	case lbrace:		/* got a '{' */
7369b50d902SRodney W. Grimes 	    ps.in_stmt = false;	/* dont indent the {} */
7379b50d902SRodney W. Grimes 	    if (!ps.block_init)
7389b50d902SRodney W. Grimes 		force_nl = true;/* force other stuff on same line as '{' onto
7399b50d902SRodney W. Grimes 				 * new line */
7409b50d902SRodney W. Grimes 	    else if (ps.block_init_level <= 0)
7419b50d902SRodney W. Grimes 		ps.block_init_level = 1;
7429b50d902SRodney W. Grimes 	    else
7439b50d902SRodney W. Grimes 		ps.block_init_level++;
7449b50d902SRodney W. Grimes 
7459b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {
7469b50d902SRodney W. Grimes 		if (!btype_2) {
7479b50d902SRodney W. Grimes 		    dump_line();
7489b50d902SRodney W. Grimes 		    ps.want_blank = false;
7499b50d902SRodney W. Grimes 		}
7509b50d902SRodney W. Grimes 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
7519b50d902SRodney W. Grimes 		    ps.i_l_follow = 0;
7529b50d902SRodney W. Grimes 		    dump_line();
7539b50d902SRodney W. Grimes 		    ps.want_blank = false;
7549b50d902SRodney W. Grimes 		}
7559b50d902SRodney W. Grimes 	    }
7569b50d902SRodney W. Grimes 	    if (ps.in_parameter_declaration)
7579b50d902SRodney W. Grimes 		prefix_blankline_requested = 0;
7589b50d902SRodney W. Grimes 
7599b50d902SRodney W. Grimes 	    if (ps.p_l_follow > 0) {	/* check for preceeding unbalanced
7609b50d902SRodney W. Grimes 					 * parens */
7619b50d902SRodney W. Grimes 		diag(1, "Unbalanced parens");
7629b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
7639b50d902SRodney W. Grimes 		if (sp_sw) {	/* check for unclosed if, for, etc. */
7649b50d902SRodney W. Grimes 		    sp_sw = false;
7659b50d902SRodney W. Grimes 		    parse(hd_type);
7669b50d902SRodney W. Grimes 		    ps.ind_level = ps.i_l_follow;
7679b50d902SRodney W. Grimes 		}
7689b50d902SRodney W. Grimes 	    }
7699b50d902SRodney W. Grimes 	    if (s_code == e_code)
7709b50d902SRodney W. Grimes 		ps.ind_stmt = false;	/* dont put extra indentation on line
7719b50d902SRodney W. Grimes 					 * with '{' */
7729b50d902SRodney W. Grimes 	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
7739b50d902SRodney W. Grimes 						 * declaration or an init */
7749b50d902SRodney W. Grimes 		di_stack[ps.dec_nest++] = dec_ind;
7759b50d902SRodney W. Grimes 		/* ?		dec_ind = 0; */
7769b50d902SRodney W. Grimes 	    }
7779b50d902SRodney W. Grimes 	    else {
7789b50d902SRodney W. Grimes 		ps.decl_on_line = false;	/* we cant be in the middle of
7799b50d902SRodney W. Grimes 						 * a declaration, so dont do
7809b50d902SRodney W. Grimes 						 * special indentation of
7819b50d902SRodney W. Grimes 						 * comments */
7829b50d902SRodney W. Grimes 		if (blanklines_after_declarations_at_proctop
7839b50d902SRodney W. Grimes 			&& ps.in_parameter_declaration)
7849b50d902SRodney W. Grimes 		    postfix_blankline_requested = 1;
7859b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 0;
7869b50d902SRodney W. Grimes 	    }
7879b50d902SRodney W. Grimes 	    dec_ind = 0;
7889b50d902SRodney W. Grimes 	    parse(lbrace);	/* let parser know about this */
7899b50d902SRodney W. Grimes 	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at
7909b50d902SRodney W. Grimes 				 * start of line */
7919b50d902SRodney W. Grimes 		*e_code++ = ' ';
7929b50d902SRodney W. Grimes 	    ps.want_blank = false;
7939b50d902SRodney W. Grimes 	    *e_code++ = '{';
7949b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
7959b50d902SRodney W. Grimes 	    break;
7969b50d902SRodney W. Grimes 
7979b50d902SRodney W. Grimes 	case rbrace:		/* got a '}' */
7989b50d902SRodney W. Grimes 	    if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
7999b50d902SRodney W. Grimes 								 * omitted in
8009b50d902SRodney W. Grimes 								 * declarations */
8019b50d902SRodney W. Grimes 		parse(semicolon);
8029b50d902SRodney W. Grimes 	    if (ps.p_l_follow) {/* check for unclosed if, for, else. */
8039b50d902SRodney W. Grimes 		diag(1, "Unbalanced parens");
8049b50d902SRodney W. Grimes 		ps.p_l_follow = 0;
8059b50d902SRodney W. Grimes 		sp_sw = false;
8069b50d902SRodney W. Grimes 	    }
8079b50d902SRodney W. Grimes 	    ps.just_saw_decl = 0;
8089b50d902SRodney W. Grimes 	    ps.block_init_level--;
8099b50d902SRodney W. Grimes 	    if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
8109b50d902SRodney W. Grimes 							 * line */
8119b50d902SRodney W. Grimes 		if (verbose)
8129b50d902SRodney W. Grimes 		    diag(0, "Line broken");
8139b50d902SRodney W. Grimes 		dump_line();
8149b50d902SRodney W. Grimes 	    }
8159b50d902SRodney W. Grimes 	    *e_code++ = '}';
8169b50d902SRodney W. Grimes 	    ps.want_blank = true;
8179b50d902SRodney W. Grimes 	    ps.in_stmt = ps.ind_stmt = false;
8189b50d902SRodney W. Grimes 	    if (ps.dec_nest > 0) {	/* we are in multi-level structure
8199b50d902SRodney W. Grimes 					 * declaration */
8209b50d902SRodney W. Grimes 		dec_ind = di_stack[--ps.dec_nest];
8219b50d902SRodney W. Grimes 		if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
8229b50d902SRodney W. Grimes 		    ps.just_saw_decl = 2;
8239b50d902SRodney W. Grimes 		ps.in_decl = true;
8249b50d902SRodney W. Grimes 	    }
8259b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
8269b50d902SRodney W. Grimes 	    parse(rbrace);	/* let parser know about this */
8279b50d902SRodney W. Grimes 	    ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
8289b50d902SRodney W. Grimes 		&& ps.il[ps.tos] >= ps.ind_level;
8299b50d902SRodney W. Grimes 	    if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
8309b50d902SRodney W. Grimes 		postfix_blankline_requested = 1;
8319b50d902SRodney W. Grimes 	    break;
8329b50d902SRodney W. Grimes 
8339b50d902SRodney W. Grimes 	case swstmt:		/* got keyword "switch" */
8349b50d902SRodney W. Grimes 	    sp_sw = true;
8359b50d902SRodney W. Grimes 	    hd_type = swstmt;	/* keep this for when we have seen the
8369b50d902SRodney W. Grimes 				 * expression */
8379b50d902SRodney W. Grimes 	    goto copy_id;	/* go move the token into buffer */
8389b50d902SRodney W. Grimes 
8399b50d902SRodney W. Grimes 	case sp_paren:		/* token is if, while, for */
8409b50d902SRodney W. Grimes 	    sp_sw = true;	/* the interesting stuff is done after the
8419b50d902SRodney W. Grimes 				 * expression is scanned */
8429b50d902SRodney W. Grimes 	    hd_type = (*token == 'i' ? ifstmt :
8439b50d902SRodney W. Grimes 		       (*token == 'w' ? whilestmt : forstmt));
8449b50d902SRodney W. Grimes 
8459b50d902SRodney W. Grimes 	    /*
8469b50d902SRodney W. Grimes 	     * remember the type of header for later use by parser
8479b50d902SRodney W. Grimes 	     */
8489b50d902SRodney W. Grimes 	    goto copy_id;	/* copy the token into line */
8499b50d902SRodney W. Grimes 
8509b50d902SRodney W. Grimes 	case sp_nparen:	/* got else, do */
8519b50d902SRodney W. Grimes 	    ps.in_stmt = false;
8529b50d902SRodney W. Grimes 	    if (*token == 'e') {
8539b50d902SRodney W. Grimes 		if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
8549b50d902SRodney W. Grimes 		    if (verbose)
8559b50d902SRodney W. Grimes 			diag(0, "Line broken");
8569b50d902SRodney W. Grimes 		    dump_line();/* make sure this starts a line */
8579b50d902SRodney W. Grimes 		    ps.want_blank = false;
8589b50d902SRodney W. Grimes 		}
8599b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
8609b50d902SRodney W. Grimes 		last_else = 1;
8619b50d902SRodney W. Grimes 		parse(elselit);
8629b50d902SRodney W. Grimes 	    }
8639b50d902SRodney W. Grimes 	    else {
8649b50d902SRodney W. Grimes 		if (e_code != s_code) {	/* make sure this starts a line */
8659b50d902SRodney W. Grimes 		    if (verbose)
8669b50d902SRodney W. Grimes 			diag(0, "Line broken");
8679b50d902SRodney W. Grimes 		    dump_line();
8689b50d902SRodney W. Grimes 		    ps.want_blank = false;
8699b50d902SRodney W. Grimes 		}
8709b50d902SRodney W. Grimes 		force_nl = true;/* also, following stuff must go onto new line */
8719b50d902SRodney W. Grimes 		last_else = 0;
8729b50d902SRodney W. Grimes 		parse(dolit);
8739b50d902SRodney W. Grimes 	    }
8749b50d902SRodney W. Grimes 	    goto copy_id;	/* move the token into line */
8759b50d902SRodney W. Grimes 
8769b50d902SRodney W. Grimes 	case decl:		/* we have a declaration type (int, register,
8779b50d902SRodney W. Grimes 				 * etc.) */
8789b50d902SRodney W. Grimes 	    parse(decl);	/* let parser worry about indentation */
8799b50d902SRodney W. Grimes 	    if (ps.last_token == rparen && ps.tos <= 1) {
8809b50d902SRodney W. Grimes 		ps.in_parameter_declaration = 1;
8819b50d902SRodney W. Grimes 		if (s_code != e_code) {
8829b50d902SRodney W. Grimes 		    dump_line();
8839b50d902SRodney W. Grimes 		    ps.want_blank = 0;
8849b50d902SRodney W. Grimes 		}
8859b50d902SRodney W. Grimes 	    }
8869b50d902SRodney W. Grimes 	    if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
8879b50d902SRodney W. Grimes 		ps.ind_level = ps.i_l_follow = 1;
8889b50d902SRodney W. Grimes 		ps.ind_stmt = 0;
8899b50d902SRodney W. Grimes 	    }
8909b50d902SRodney W. Grimes 	    ps.in_or_st = true;	/* this might be a structure or initialization
8919b50d902SRodney W. Grimes 				 * declaration */
8929b50d902SRodney W. Grimes 	    ps.in_decl = ps.decl_on_line = true;
8939b50d902SRodney W. Grimes 	    if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
8949b50d902SRodney W. Grimes 		ps.just_saw_decl = 2;
8959b50d902SRodney W. Grimes 	    prefix_blankline_requested = 0;
8969b50d902SRodney W. Grimes 	    for (i = 0; token[i++];);	/* get length of token */
8979b50d902SRodney W. Grimes 
8989b50d902SRodney W. Grimes 	    /*
8999b50d902SRodney W. Grimes 	     * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
9009b50d902SRodney W. Grimes 	     * : i);
9019b50d902SRodney W. Grimes 	     */
9029b50d902SRodney W. Grimes 	    dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
9039b50d902SRodney W. Grimes 	    goto copy_id;
9049b50d902SRodney W. Grimes 
9059b50d902SRodney W. Grimes 	case ident:		/* got an identifier or constant */
9069b50d902SRodney W. Grimes 	    if (ps.in_decl) {	/* if we are in a declaration, we must indent
9079b50d902SRodney W. Grimes 				 * identifier */
9089b50d902SRodney W. Grimes 		if (ps.want_blank)
9099b50d902SRodney W. Grimes 		    *e_code++ = ' ';
9109b50d902SRodney W. Grimes 		ps.want_blank = false;
9119b50d902SRodney W. Grimes 		if (is_procname == 0 || !procnames_start_line) {
9129b50d902SRodney W. Grimes 		    if (!ps.block_init)
9139b50d902SRodney W. Grimes 			if (troff && !ps.dumped_decl_indent) {
9149b50d902SRodney W. Grimes 			    sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
9159b50d902SRodney W. Grimes 			    ps.dumped_decl_indent = 1;
9169b50d902SRodney W. Grimes 			    e_code += strlen(e_code);
9179b50d902SRodney W. Grimes 			}
9189b50d902SRodney W. Grimes 			else
9199b50d902SRodney W. Grimes 			    while ((e_code - s_code) < dec_ind) {
9209b50d902SRodney W. Grimes 				CHECK_SIZE_CODE;
9219b50d902SRodney W. Grimes 				*e_code++ = ' ';
9229b50d902SRodney W. Grimes 			    }
9239b50d902SRodney W. Grimes 		}
9249b50d902SRodney W. Grimes 		else {
9259b50d902SRodney W. Grimes 		    if (dec_ind && s_code != e_code)
9269b50d902SRodney W. Grimes 			dump_line();
9279b50d902SRodney W. Grimes 		    dec_ind = 0;
9289b50d902SRodney W. Grimes 		    ps.want_blank = false;
9299b50d902SRodney W. Grimes 		}
9309b50d902SRodney W. Grimes 	    }
9319b50d902SRodney W. Grimes 	    else if (sp_sw && ps.p_l_follow == 0) {
9329b50d902SRodney W. Grimes 		sp_sw = false;
9339b50d902SRodney W. Grimes 		force_nl = true;
9349b50d902SRodney W. Grimes 		ps.last_u_d = true;
9359b50d902SRodney W. Grimes 		ps.in_stmt = false;
9369b50d902SRodney W. Grimes 		parse(hd_type);
9379b50d902SRodney W. Grimes 	    }
9389b50d902SRodney W. Grimes     copy_id:
9399b50d902SRodney W. Grimes 	    if (ps.want_blank)
9409b50d902SRodney W. Grimes 		*e_code++ = ' ';
9419b50d902SRodney W. Grimes 	    if (troff && ps.its_a_keyword) {
9429b50d902SRodney W. Grimes 		e_code = chfont(&bodyf, &keywordf, e_code);
9439b50d902SRodney W. Grimes 		for (t_ptr = token; *t_ptr; ++t_ptr) {
9449b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
9459b50d902SRodney W. Grimes 		    *e_code++ = keywordf.allcaps && islower(*t_ptr)
9469b50d902SRodney W. Grimes 			? toupper(*t_ptr) : *t_ptr;
9479b50d902SRodney W. Grimes 		}
9489b50d902SRodney W. Grimes 		e_code = chfont(&keywordf, &bodyf, e_code);
9499b50d902SRodney W. Grimes 	    }
9509b50d902SRodney W. Grimes 	    else
9519b50d902SRodney W. Grimes 		for (t_ptr = token; *t_ptr; ++t_ptr) {
9529b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
9539b50d902SRodney W. Grimes 		    *e_code++ = *t_ptr;
9549b50d902SRodney W. Grimes 		}
9559b50d902SRodney W. Grimes 	    ps.want_blank = true;
9569b50d902SRodney W. Grimes 	    break;
9579b50d902SRodney W. Grimes 
9589b50d902SRodney W. Grimes 	case period:		/* treat a period kind of like a binary
9599b50d902SRodney W. Grimes 				 * operation */
9609b50d902SRodney W. Grimes 	    *e_code++ = '.';	/* move the period into line */
9619b50d902SRodney W. Grimes 	    ps.want_blank = false;	/* dont put a blank after a period */
9629b50d902SRodney W. Grimes 	    break;
9639b50d902SRodney W. Grimes 
9649b50d902SRodney W. Grimes 	case comma:
9659b50d902SRodney W. Grimes 	    ps.want_blank = (s_code != e_code);	/* only put blank after comma
9669b50d902SRodney W. Grimes 						 * if comma does not start the
9679b50d902SRodney W. Grimes 						 * line */
9689b50d902SRodney W. Grimes 	    if (ps.in_decl && is_procname == 0 && !ps.block_init)
9699b50d902SRodney W. Grimes 		while ((e_code - s_code) < (dec_ind - 1)) {
9709b50d902SRodney W. Grimes 		    CHECK_SIZE_CODE;
9719b50d902SRodney W. Grimes 		    *e_code++ = ' ';
9729b50d902SRodney W. Grimes 		}
9739b50d902SRodney W. Grimes 
9749b50d902SRodney W. Grimes 	    *e_code++ = ',';
9759b50d902SRodney W. Grimes 	    if (ps.p_l_follow == 0) {
9769b50d902SRodney W. Grimes 		if (ps.block_init_level <= 0)
9779b50d902SRodney W. Grimes 		    ps.block_init = 0;
9789b50d902SRodney W. Grimes 		if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
9799b50d902SRodney W. Grimes 		    force_nl = true;
9809b50d902SRodney W. Grimes 	    }
9819b50d902SRodney W. Grimes 	    break;
9829b50d902SRodney W. Grimes 
9839b50d902SRodney W. Grimes 	case preesc:		/* got the character '#' */
9849b50d902SRodney W. Grimes 	    if ((s_com != e_com) ||
9859b50d902SRodney W. Grimes 		    (s_lab != e_lab) ||
9869b50d902SRodney W. Grimes 		    (s_code != e_code))
9879b50d902SRodney W. Grimes 		dump_line();
9889b50d902SRodney W. Grimes 	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
9899b50d902SRodney W. Grimes 	    {
9909b50d902SRodney W. Grimes 		int         in_comment = 0;
9919b50d902SRodney W. Grimes 		int         com_start = 0;
9929b50d902SRodney W. Grimes 		char        quote = 0;
9939b50d902SRodney W. Grimes 		int         com_end = 0;
9949b50d902SRodney W. Grimes 
9959b50d902SRodney W. Grimes 		while (*buf_ptr == ' ' || *buf_ptr == '\t') {
9969b50d902SRodney W. Grimes 		    buf_ptr++;
9979b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
9989b50d902SRodney W. Grimes 			fill_buffer();
9999b50d902SRodney W. Grimes 		}
10009b50d902SRodney W. Grimes 		while (*buf_ptr != '\n' || in_comment) {
10019b50d902SRodney W. Grimes 		    CHECK_SIZE_LAB;
10029b50d902SRodney W. Grimes 		    *e_lab = *buf_ptr++;
10039b50d902SRodney W. Grimes 		    if (buf_ptr >= buf_end)
10049b50d902SRodney W. Grimes 			fill_buffer();
10059b50d902SRodney W. Grimes 		    switch (*e_lab++) {
10069b50d902SRodney W. Grimes 		    case BACKSLASH:
10079b50d902SRodney W. Grimes 			if (troff)
10089b50d902SRodney W. Grimes 			    *e_lab++ = BACKSLASH;
10099b50d902SRodney W. Grimes 			if (!in_comment) {
10109b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
10119b50d902SRodney W. Grimes 			    if (buf_ptr >= buf_end)
10129b50d902SRodney W. Grimes 				fill_buffer();
10139b50d902SRodney W. Grimes 			}
10149b50d902SRodney W. Grimes 			break;
10159b50d902SRodney W. Grimes 		    case '/':
10169b50d902SRodney W. Grimes 			if (*buf_ptr == '*' && !in_comment && !quote) {
10179b50d902SRodney W. Grimes 			    in_comment = 1;
10189b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
10199b50d902SRodney W. Grimes 			    com_start = e_lab - s_lab - 2;
10209b50d902SRodney W. Grimes 			}
10219b50d902SRodney W. Grimes 			break;
10229b50d902SRodney W. Grimes 		    case '"':
10239b50d902SRodney W. Grimes 			if (quote == '"')
10249b50d902SRodney W. Grimes 			    quote = 0;
10259b50d902SRodney W. Grimes 			break;
10269b50d902SRodney W. Grimes 		    case '\'':
10279b50d902SRodney W. Grimes 			if (quote == '\'')
10289b50d902SRodney W. Grimes 			    quote = 0;
10299b50d902SRodney W. Grimes 			break;
10309b50d902SRodney W. Grimes 		    case '*':
10319b50d902SRodney W. Grimes 			if (*buf_ptr == '/' && in_comment) {
10329b50d902SRodney W. Grimes 			    in_comment = 0;
10339b50d902SRodney W. Grimes 			    *e_lab++ = *buf_ptr++;
10349b50d902SRodney W. Grimes 			    com_end = e_lab - s_lab;
10359b50d902SRodney W. Grimes 			}
10369b50d902SRodney W. Grimes 			break;
10379b50d902SRodney W. Grimes 		    }
10389b50d902SRodney W. Grimes 		}
10399b50d902SRodney W. Grimes 
10409b50d902SRodney W. Grimes 		while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
10419b50d902SRodney W. Grimes 		    e_lab--;
10429b50d902SRodney W. Grimes 		if (e_lab - s_lab == com_end && bp_save == 0) {	/* comment on
10439b50d902SRodney W. Grimes 								 * preprocessor line */
10449b50d902SRodney W. Grimes 		    if (sc_end == 0)	/* if this is the first comment, we
10459b50d902SRodney W. Grimes 					 * must set up the buffer */
10469b50d902SRodney W. Grimes 			sc_end = &(save_com[0]);
10479b50d902SRodney W. Grimes 		    else {
10489b50d902SRodney W. Grimes 			*sc_end++ = '\n';	/* add newline between
10499b50d902SRodney W. Grimes 						 * comments */
10509b50d902SRodney W. Grimes 			*sc_end++ = ' ';
10519b50d902SRodney W. Grimes 			--line_no;
10529b50d902SRodney W. Grimes 		    }
10539b50d902SRodney W. Grimes 		    bcopy(s_lab + com_start, sc_end, com_end - com_start);
10549b50d902SRodney W. Grimes 		    sc_end += com_end - com_start;
10559b50d902SRodney W. Grimes 		    if (sc_end >= &save_com[sc_size])
10569b50d902SRodney W. Grimes 			abort();
10579b50d902SRodney W. Grimes 		    e_lab = s_lab + com_start;
10589b50d902SRodney W. Grimes 		    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
10599b50d902SRodney W. Grimes 			e_lab--;
10609b50d902SRodney W. Grimes 		    bp_save = buf_ptr;	/* save current input buffer */
10619b50d902SRodney W. Grimes 		    be_save = buf_end;
10629b50d902SRodney W. Grimes 		    buf_ptr = save_com;	/* fix so that subsequent calls to
10639b50d902SRodney W. Grimes 					 * lexi will take tokens out of
10649b50d902SRodney W. Grimes 					 * save_com */
10659b50d902SRodney W. Grimes 		    *sc_end++ = ' ';	/* add trailing blank, just in case */
10669b50d902SRodney W. Grimes 		    buf_end = sc_end;
10679b50d902SRodney W. Grimes 		    sc_end = 0;
10689b50d902SRodney W. Grimes 		}
10699b50d902SRodney W. Grimes 		*e_lab = '\0';	/* null terminate line */
10709b50d902SRodney W. Grimes 		ps.pcase = false;
10719b50d902SRodney W. Grimes 	    }
10729b50d902SRodney W. Grimes 
10739b50d902SRodney W. Grimes 	    if (strncmp(s_lab, "#if", 3) == 0) {
10749b50d902SRodney W. Grimes 		if (blanklines_around_conditional_compilation) {
10759b50d902SRodney W. Grimes 		    register    c;
10769b50d902SRodney W. Grimes 		    prefix_blankline_requested++;
10779b50d902SRodney W. Grimes 		    while ((c = getc(input)) == '\n');
10789b50d902SRodney W. Grimes 		    ungetc(c, input);
10799b50d902SRodney W. Grimes 		}
10809b50d902SRodney W. Grimes 		if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
10819b50d902SRodney W. Grimes 		    match_state[ifdef_level].tos = -1;
10829b50d902SRodney W. Grimes 		    state_stack[ifdef_level++] = ps;
10839b50d902SRodney W. Grimes 		}
10849b50d902SRodney W. Grimes 		else
10859b50d902SRodney W. Grimes 		    diag(1, "#if stack overflow");
10869b50d902SRodney W. Grimes 	    }
10879b50d902SRodney W. Grimes 	    else if (strncmp(s_lab, "#else", 5) == 0)
10889b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
10899b50d902SRodney W. Grimes 		    diag(1, "Unmatched #else");
10909b50d902SRodney W. Grimes 		else {
10919b50d902SRodney W. Grimes 		    match_state[ifdef_level - 1] = ps;
10929b50d902SRodney W. Grimes 		    ps = state_stack[ifdef_level - 1];
10939b50d902SRodney W. Grimes 		}
10949b50d902SRodney W. Grimes 	    else if (strncmp(s_lab, "#endif", 6) == 0) {
10959b50d902SRodney W. Grimes 		if (ifdef_level <= 0)
10969b50d902SRodney W. Grimes 		    diag(1, "Unmatched #endif");
10979b50d902SRodney W. Grimes 		else {
10989b50d902SRodney W. Grimes 		    ifdef_level--;
10999b50d902SRodney W. Grimes 
11009b50d902SRodney W. Grimes #ifdef undef
11019b50d902SRodney W. Grimes 		    /*
11029b50d902SRodney W. Grimes 		     * This match needs to be more intelligent before the
11039b50d902SRodney W. Grimes 		     * message is useful
11049b50d902SRodney W. Grimes 		     */
11059b50d902SRodney W. Grimes 		    if (match_state[ifdef_level].tos >= 0
11069b50d902SRodney W. Grimes 			  && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
11079b50d902SRodney W. Grimes 			diag(0, "Syntactically inconsistant #ifdef alternatives.");
11089b50d902SRodney W. Grimes #endif
11099b50d902SRodney W. Grimes 		}
11109b50d902SRodney W. Grimes 		if (blanklines_around_conditional_compilation) {
11119b50d902SRodney W. Grimes 		    postfix_blankline_requested++;
11129b50d902SRodney W. Grimes 		    n_real_blanklines = 0;
11139b50d902SRodney W. Grimes 		}
11149b50d902SRodney W. Grimes 	    }
11159b50d902SRodney W. Grimes 	    break;		/* subsequent processing of the newline
11169b50d902SRodney W. Grimes 				 * character will cause the line to be printed */
11179b50d902SRodney W. Grimes 
11189b50d902SRodney W. Grimes 	case comment:		/* we have gotten a /*  this is a biggie */
11199b50d902SRodney W. Grimes 	    if (flushed_nl) {	/* we should force a broken line here */
11209b50d902SRodney W. Grimes 		flushed_nl = false;
11219b50d902SRodney W. Grimes 		dump_line();
11229b50d902SRodney W. Grimes 		ps.want_blank = false;	/* dont insert blank at line start */
11239b50d902SRodney W. Grimes 		force_nl = false;
11249b50d902SRodney W. Grimes 	    }
11259b50d902SRodney W. Grimes 	    pr_comment();
11269b50d902SRodney W. Grimes 	    break;
11279b50d902SRodney W. Grimes 	}			/* end of big switch stmt */
11289b50d902SRodney W. Grimes 
11299b50d902SRodney W. Grimes 	*e_code = '\0';		/* make sure code section is null terminated */
11309b50d902SRodney W. Grimes 	if (type_code != comment && type_code != newline && type_code != preesc)
11319b50d902SRodney W. Grimes 	    ps.last_token = type_code;
11329b50d902SRodney W. Grimes     }				/* end of main while (1) loop */
11339b50d902SRodney W. Grimes }
11349b50d902SRodney W. Grimes 
1135958d7c9fSPhilippe Charnier static void
1136958d7c9fSPhilippe Charnier usage()
1137958d7c9fSPhilippe Charnier {
113818251d71SPeter Hawkins 	fprintf(stderr, "usage: indent [ file [ outfile ] ] [ options ]\n");
1139958d7c9fSPhilippe Charnier 	exit(1);
1140958d7c9fSPhilippe Charnier }
1141958d7c9fSPhilippe Charnier 
11429b50d902SRodney W. Grimes /*
11439b50d902SRodney W. Grimes  * copy input file to backup file if in_name is /blah/blah/blah/file, then
11449b50d902SRodney W. Grimes  * backup file will be ".Bfile" then make the backup file the input and
11459b50d902SRodney W. Grimes  * original input file the output
11469b50d902SRodney W. Grimes  */
11479b50d902SRodney W. Grimes bakcopy()
11489b50d902SRodney W. Grimes {
11499b50d902SRodney W. Grimes     int         n,
11509b50d902SRodney W. Grimes                 bakchn;
11519b50d902SRodney W. Grimes     char        buff[8 * 1024];
11529b50d902SRodney W. Grimes     register char *p;
11539b50d902SRodney W. Grimes 
11549b50d902SRodney W. Grimes     /* construct file name .Bfile */
11559b50d902SRodney W. Grimes     for (p = in_name; *p; p++);	/* skip to end of string */
11569b50d902SRodney W. Grimes     while (p > in_name && *p != '/')	/* find last '/' */
11579b50d902SRodney W. Grimes 	p--;
11589b50d902SRodney W. Grimes     if (*p == '/')
11599b50d902SRodney W. Grimes 	p++;
11609b50d902SRodney W. Grimes     sprintf(bakfile, "%s.BAK", p);
11619b50d902SRodney W. Grimes 
11629b50d902SRodney W. Grimes     /* copy in_name to backup file */
11639b50d902SRodney W. Grimes     bakchn = creat(bakfile, 0600);
11649b50d902SRodney W. Grimes     if (bakchn < 0)
11650c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
11669b50d902SRodney W. Grimes     while (n = read(fileno(input), buff, sizeof buff))
11679b50d902SRodney W. Grimes 	if (write(bakchn, buff, n) != n)
11680c4d24a7SKris Kennaway 	    err(1, "%s", bakfile);
11699b50d902SRodney W. Grimes     if (n < 0)
11700c4d24a7SKris Kennaway 	err(1, "%s", in_name);
11719b50d902SRodney W. Grimes     close(bakchn);
11729b50d902SRodney W. Grimes     fclose(input);
11739b50d902SRodney W. Grimes 
11749b50d902SRodney W. Grimes     /* re-open backup file as the input file */
11759b50d902SRodney W. Grimes     input = fopen(bakfile, "r");
11769b50d902SRodney W. Grimes     if (input == 0)
11770c4d24a7SKris Kennaway 	err(1, "%s", bakfile);
11789b50d902SRodney W. Grimes     /* now the original input file will be the output */
11799b50d902SRodney W. Grimes     output = fopen(in_name, "w");
11809b50d902SRodney W. Grimes     if (output == 0) {
11819b50d902SRodney W. Grimes 	unlink(bakfile);
11820c4d24a7SKris Kennaway 	err(1, "%s", in_name);
11839b50d902SRodney W. Grimes     }
11849b50d902SRodney W. Grimes }
1185