Home | History | Annotate | Line # | Download | only in indent
indent.c revision 1.4
      1  1.4      tls /*	$NetBSD: indent.c,v 1.4 1997/01/09 20:20:11 tls Exp $	*/
      2  1.4      tls 
      3  1.1      cgd /*
      4  1.1      cgd  * Copyright (c) 1985 Sun Microsystems, Inc.
      5  1.1      cgd  * Copyright (c) 1980 The Regents of the University of California.
      6  1.1      cgd  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
      7  1.1      cgd  * All rights reserved.
      8  1.1      cgd  *
      9  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     10  1.1      cgd  * modification, are permitted provided that the following conditions
     11  1.1      cgd  * are met:
     12  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     14  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     16  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     17  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     18  1.1      cgd  *    must display the following acknowledgement:
     19  1.1      cgd  *	This product includes software developed by the University of
     20  1.1      cgd  *	California, Berkeley and its contributors.
     21  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     22  1.1      cgd  *    may be used to endorse or promote products derived from this software
     23  1.1      cgd  *    without specific prior written permission.
     24  1.1      cgd  *
     25  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1      cgd  * SUCH DAMAGE.
     36  1.1      cgd  */
     37  1.1      cgd 
     38  1.1      cgd #ifndef lint
     39  1.1      cgd char copyright[] =
     40  1.1      cgd "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
     41  1.1      cgd  @(#) Copyright (c) 1980 The Regents of the University of California.\n\
     42  1.1      cgd  @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
     43  1.1      cgd  All rights reserved.\n";
     44  1.1      cgd #endif /* not lint */
     45  1.1      cgd 
     46  1.1      cgd #ifndef lint
     47  1.2  mycroft /*static char sccsid[] = "from: @(#)indent.c	5.16 (Berkeley) 2/26/91";*/
     48  1.4      tls static char rcsid[] = "$NetBSD: indent.c,v 1.4 1997/01/09 20:20:11 tls Exp $";
     49  1.1      cgd #endif /* not lint */
     50  1.1      cgd 
     51  1.1      cgd #include <sys/param.h>
     52  1.1      cgd #include <fcntl.h>
     53  1.1      cgd #include <unistd.h>
     54  1.1      cgd #include <stdio.h>
     55  1.1      cgd #include <stdlib.h>
     56  1.1      cgd #include <string.h>
     57  1.1      cgd #include "indent_globs.h"
     58  1.1      cgd #include "indent_codes.h"
     59  1.1      cgd #include <ctype.h>
     60  1.3      jtc #include <errno.h>
     61  1.1      cgd 
     62  1.1      cgd char       *in_name = "Standard Input";	/* will always point to name of input
     63  1.1      cgd 					 * file */
     64  1.1      cgd char       *out_name = "Standard Output";	/* will always point to name
     65  1.1      cgd 						 * of output file */
     66  1.1      cgd char        bakfile[MAXPATHLEN] = "";
     67  1.1      cgd 
     68  1.1      cgd main(argc, argv)
     69  1.1      cgd     int         argc;
     70  1.1      cgd     char      **argv;
     71  1.1      cgd {
     72  1.1      cgd 
     73  1.1      cgd     extern int  found_err;	/* flag set in diag() on error */
     74  1.1      cgd     int         dec_ind;	/* current indentation for declarations */
     75  1.1      cgd     int         di_stack[20];	/* a stack of structure indentation levels */
     76  1.1      cgd     int         flushed_nl;	/* used when buffering up comments to remember
     77  1.1      cgd 				 * that a newline was passed over */
     78  1.1      cgd     int         force_nl;	/* when true, code must be broken */
     79  1.1      cgd     int         hd_type;	/* used to store type of stmt for if (...),
     80  1.1      cgd 				 * for (...), etc */
     81  1.1      cgd     register int i;		/* local loop counter */
     82  1.1      cgd     int         scase;		/* set to true when we see a case, so we will
     83  1.1      cgd 				 * know what to do with the following colon */
     84  1.1      cgd     int         sp_sw;		/* when true, we are in the expressin of
     85  1.1      cgd 				 * if(...), while(...), etc. */
     86  1.1      cgd     int         squest;		/* when this is positive, we have seen a ?
     87  1.1      cgd 				 * without the matching : in a <c>?<s>:<s>
     88  1.1      cgd 				 * construct */
     89  1.1      cgd     register char *t_ptr;	/* used for copying tokens */
     90  1.1      cgd     int         type_code;	/* the type of token, returned by lexi */
     91  1.1      cgd 
     92  1.1      cgd     int         last_else = 0;	/* true iff last keyword was an else */
     93  1.1      cgd 
     94  1.1      cgd 
     95  1.1      cgd     /*-----------------------------------------------*\
     96  1.1      cgd     |		      INITIALIZATION		      |
     97  1.1      cgd     \*-----------------------------------------------*/
     98  1.1      cgd 
     99  1.1      cgd 
    100  1.1      cgd     ps.p_stack[0] = stmt;	/* this is the parser's stack */
    101  1.1      cgd     ps.last_nl = true;		/* this is true if the last thing scanned was
    102  1.1      cgd 				 * a newline */
    103  1.1      cgd     ps.last_token = semicolon;
    104  1.1      cgd     combuf = (char *) malloc(bufsize);
    105  1.1      cgd     labbuf = (char *) malloc(bufsize);
    106  1.1      cgd     codebuf = (char *) malloc(bufsize);
    107  1.1      cgd     tokenbuf = (char *) malloc(bufsize);
    108  1.1      cgd     l_com = combuf + bufsize - 5;
    109  1.1      cgd     l_lab = labbuf + bufsize - 5;
    110  1.1      cgd     l_code = codebuf + bufsize - 5;
    111  1.1      cgd     l_token = tokenbuf + bufsize - 5;
    112  1.1      cgd     combuf[0] = codebuf[0] = labbuf[0] = ' ';	/* set up code, label, and
    113  1.1      cgd 						 * comment buffers */
    114  1.1      cgd     combuf[1] = codebuf[1] = labbuf[1] = '\0';
    115  1.1      cgd     ps.else_if = 1;		/* Default else-if special processing to on */
    116  1.1      cgd     s_lab = e_lab = labbuf + 1;
    117  1.1      cgd     s_code = e_code = codebuf + 1;
    118  1.1      cgd     s_com = e_com = combuf + 1;
    119  1.1      cgd     s_token = e_token = tokenbuf + 1;
    120  1.1      cgd 
    121  1.1      cgd     in_buffer = (char *) malloc(10);
    122  1.1      cgd     in_buffer_limit = in_buffer + 8;
    123  1.1      cgd     buf_ptr = buf_end = in_buffer;
    124  1.1      cgd     line_no = 1;
    125  1.1      cgd     had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
    126  1.1      cgd     sp_sw = force_nl = false;
    127  1.1      cgd     ps.in_or_st = false;
    128  1.1      cgd     ps.bl_line = true;
    129  1.1      cgd     dec_ind = 0;
    130  1.1      cgd     di_stack[ps.dec_nest = 0] = 0;
    131  1.1      cgd     ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
    132  1.1      cgd 
    133  1.1      cgd 
    134  1.1      cgd     scase = ps.pcase = false;
    135  1.1      cgd     squest = 0;
    136  1.1      cgd     sc_end = 0;
    137  1.1      cgd     bp_save = 0;
    138  1.1      cgd     be_save = 0;
    139  1.1      cgd 
    140  1.1      cgd     output = 0;
    141  1.1      cgd 
    142  1.1      cgd 
    143  1.1      cgd 
    144  1.1      cgd     /*--------------------------------------------------*\
    145  1.1      cgd     |   		COMMAND LINE SCAN		 |
    146  1.1      cgd     \*--------------------------------------------------*/
    147  1.1      cgd 
    148  1.1      cgd #ifdef undef
    149  1.1      cgd     max_col = 78;		/* -l78 */
    150  1.1      cgd     lineup_to_parens = 1;	/* -lp */
    151  1.1      cgd     ps.ljust_decl = 0;		/* -ndj */
    152  1.1      cgd     ps.com_ind = 33;		/* -c33 */
    153  1.1      cgd     star_comment_cont = 1;	/* -sc */
    154  1.1      cgd     ps.ind_size = 8;		/* -i8 */
    155  1.1      cgd     verbose = 0;
    156  1.1      cgd     ps.decl_indent = 16;	/* -di16 */
    157  1.1      cgd     ps.indent_parameters = 1;	/* -ip */
    158  1.1      cgd     ps.decl_com_ind = 0;	/* if this is not set to some positive value
    159  1.1      cgd 				 * by an arg, we will set this equal to
    160  1.1      cgd 				 * ps.com_ind */
    161  1.1      cgd     btype_2 = 1;		/* -br */
    162  1.1      cgd     cuddle_else = 1;		/* -ce */
    163  1.1      cgd     ps.unindent_displace = 0;	/* -d0 */
    164  1.1      cgd     ps.case_indent = 0;		/* -cli0 */
    165  1.1      cgd     format_col1_comments = 1;	/* -fc1 */
    166  1.1      cgd     procnames_start_line = 1;	/* -psl */
    167  1.1      cgd     proc_calls_space = 0;	/* -npcs */
    168  1.1      cgd     comment_delimiter_on_blankline = 1;	/* -cdb */
    169  1.1      cgd     ps.leave_comma = 1;		/* -nbc */
    170  1.1      cgd #endif
    171  1.1      cgd 
    172  1.1      cgd     for (i = 1; i < argc; ++i)
    173  1.1      cgd 	if (strcmp(argv[i], "-npro") == 0)
    174  1.1      cgd 	    break;
    175  1.1      cgd     set_defaults();
    176  1.1      cgd     if (i >= argc)
    177  1.1      cgd 	set_profile();
    178  1.1      cgd 
    179  1.1      cgd     for (i = 1; i < argc; ++i) {
    180  1.1      cgd 
    181  1.1      cgd 	/*
    182  1.1      cgd 	 * look thru args (if any) for changes to defaults
    183  1.1      cgd 	 */
    184  1.1      cgd 	if (argv[i][0] != '-') {/* no flag on parameter */
    185  1.1      cgd 	    if (input == 0) {	/* we must have the input file */
    186  1.1      cgd 		in_name = argv[i];	/* remember name of input file */
    187  1.1      cgd 		input = fopen(in_name, "r");
    188  1.1      cgd 		if (input == 0)		/* check for open error */
    189  1.1      cgd 			err(in_name);
    190  1.1      cgd 		continue;
    191  1.1      cgd 	    }
    192  1.1      cgd 	    else if (output == 0) {	/* we have the output file */
    193  1.1      cgd 		out_name = argv[i];	/* remember name of output file */
    194  1.1      cgd 		if (strcmp(in_name, out_name) == 0) {	/* attempt to overwrite
    195  1.1      cgd 							 * the file */
    196  1.1      cgd 		    fprintf(stderr, "indent: input and output files must be different\n");
    197  1.1      cgd 		    exit(1);
    198  1.1      cgd 		}
    199  1.1      cgd 		output = fopen(out_name, "w");
    200  1.1      cgd 		if (output == 0)	/* check for create error */
    201  1.1      cgd 			err(out_name);
    202  1.1      cgd 		continue;
    203  1.1      cgd 	    }
    204  1.1      cgd 	    fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]);
    205  1.1      cgd 	    exit(1);
    206  1.1      cgd 	}
    207  1.1      cgd 	else
    208  1.1      cgd 	    set_option(argv[i]);
    209  1.1      cgd     }				/* end of for */
    210  1.1      cgd     if (input == 0) {
    211  1.1      cgd 	fprintf(stderr, "indent: usage: indent file [ outfile ] [ options ]\n");
    212  1.1      cgd 	exit(1);
    213  1.1      cgd     }
    214  1.1      cgd     if (output == 0)
    215  1.1      cgd 	if (troff)
    216  1.1      cgd 	    output = stdout;
    217  1.1      cgd 	else {
    218  1.1      cgd 	    out_name = in_name;
    219  1.1      cgd 	    bakcopy();
    220  1.1      cgd 	}
    221  1.1      cgd     if (ps.com_ind <= 1)
    222  1.1      cgd 	ps.com_ind = 2;		/* dont put normal comments before column 2 */
    223  1.1      cgd     if (troff) {
    224  1.1      cgd 	if (bodyf.font[0] == 0)
    225  1.1      cgd 	    parsefont(&bodyf, "R");
    226  1.1      cgd 	if (scomf.font[0] == 0)
    227  1.1      cgd 	    parsefont(&scomf, "I");
    228  1.1      cgd 	if (blkcomf.font[0] == 0)
    229  1.1      cgd 	    blkcomf = scomf, blkcomf.size += 2;
    230  1.1      cgd 	if (boxcomf.font[0] == 0)
    231  1.1      cgd 	    boxcomf = blkcomf;
    232  1.1      cgd 	if (stringf.font[0] == 0)
    233  1.1      cgd 	    parsefont(&stringf, "L");
    234  1.1      cgd 	if (keywordf.font[0] == 0)
    235  1.1      cgd 	    parsefont(&keywordf, "B");
    236  1.1      cgd 	writefdef(&bodyf, 'B');
    237  1.1      cgd 	writefdef(&scomf, 'C');
    238  1.1      cgd 	writefdef(&blkcomf, 'L');
    239  1.1      cgd 	writefdef(&boxcomf, 'X');
    240  1.1      cgd 	writefdef(&stringf, 'S');
    241  1.1      cgd 	writefdef(&keywordf, 'K');
    242  1.1      cgd     }
    243  1.1      cgd     if (block_comment_max_col <= 0)
    244  1.1      cgd 	block_comment_max_col = max_col;
    245  1.1      cgd     if (ps.decl_com_ind <= 0)	/* if not specified by user, set this */
    246  1.1      cgd 	ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
    247  1.1      cgd     if (continuation_indent == 0)
    248  1.1      cgd 	continuation_indent = ps.ind_size;
    249  1.1      cgd     fill_buffer();		/* get first batch of stuff into input buffer */
    250  1.1      cgd 
    251  1.1      cgd     parse(semicolon);
    252  1.1      cgd     {
    253  1.1      cgd 	register char *p = buf_ptr;
    254  1.1      cgd 	register    col = 1;
    255  1.1      cgd 
    256  1.1      cgd 	while (1) {
    257  1.1      cgd 	    if (*p == ' ')
    258  1.1      cgd 		col++;
    259  1.1      cgd 	    else if (*p == '\t')
    260  1.1      cgd 		col = ((col - 1) & ~7) + 9;
    261  1.1      cgd 	    else
    262  1.1      cgd 		break;
    263  1.1      cgd 	    p++;
    264  1.1      cgd 	}
    265  1.1      cgd 	if (col > ps.ind_size)
    266  1.1      cgd 	    ps.ind_level = ps.i_l_follow = col / ps.ind_size;
    267  1.1      cgd     }
    268  1.1      cgd     if (troff) {
    269  1.1      cgd 	register char *p = in_name,
    270  1.1      cgd 	           *beg = in_name;
    271  1.1      cgd 
    272  1.1      cgd 	while (*p)
    273  1.1      cgd 	    if (*p++ == '/')
    274  1.1      cgd 		beg = p;
    275  1.1      cgd 	fprintf(output, ".Fn \"%s\"\n", beg);
    276  1.1      cgd     }
    277  1.1      cgd     /*
    278  1.1      cgd      * START OF MAIN LOOP
    279  1.1      cgd      */
    280  1.1      cgd 
    281  1.1      cgd     while (1) {			/* this is the main loop.  it will go until we
    282  1.1      cgd 				 * reach eof */
    283  1.1      cgd 	int         is_procname;
    284  1.1      cgd 
    285  1.1      cgd 	type_code = lexi();	/* lexi reads one token.  The actual
    286  1.1      cgd 				 * characters read are stored in "token". lexi
    287  1.1      cgd 				 * returns a code indicating the type of token */
    288  1.1      cgd 	is_procname = ps.procname[0];
    289  1.1      cgd 
    290  1.1      cgd 	/*
    291  1.1      cgd 	 * The following code moves everything following an if (), while (),
    292  1.1      cgd 	 * else, etc. up to the start of the following stmt to a buffer. This
    293  1.1      cgd 	 * allows proper handling of both kinds of brace placement.
    294  1.1      cgd 	 */
    295  1.1      cgd 
    296  1.1      cgd 	flushed_nl = false;
    297  1.1      cgd 	while (ps.search_brace) {	/* if we scanned an if(), while(),
    298  1.1      cgd 					 * etc., we might need to copy stuff
    299  1.1      cgd 					 * into a buffer we must loop, copying
    300  1.1      cgd 					 * stuff into save_com, until we find
    301  1.1      cgd 					 * the start of the stmt which follows
    302  1.1      cgd 					 * the if, or whatever */
    303  1.1      cgd 	    switch (type_code) {
    304  1.1      cgd 	    case newline:
    305  1.1      cgd 		++line_no;
    306  1.1      cgd 		flushed_nl = true;
    307  1.1      cgd 	    case form_feed:
    308  1.1      cgd 		break;		/* form feeds and newlines found here will be
    309  1.1      cgd 				 * ignored */
    310  1.1      cgd 
    311  1.1      cgd 	    case lbrace:	/* this is a brace that starts the compound
    312  1.1      cgd 				 * stmt */
    313  1.1      cgd 		if (sc_end == 0) {	/* ignore buffering if a comment wasnt
    314  1.1      cgd 					 * stored up */
    315  1.1      cgd 		    ps.search_brace = false;
    316  1.1      cgd 		    goto check_type;
    317  1.1      cgd 		}
    318  1.1      cgd 		if (btype_2) {
    319  1.1      cgd 		    save_com[0] = '{';	/* we either want to put the brace
    320  1.1      cgd 					 * right after the if */
    321  1.1      cgd 		    goto sw_buffer;	/* go to common code to get out of
    322  1.1      cgd 					 * this loop */
    323  1.1      cgd 		}
    324  1.1      cgd 	    case comment:	/* we have a comment, so we must copy it into
    325  1.1      cgd 				 * the buffer */
    326  1.1      cgd 		if (!flushed_nl || sc_end != 0) {
    327  1.1      cgd 		    if (sc_end == 0) {	/* if this is the first comment, we
    328  1.1      cgd 					 * must set up the buffer */
    329  1.1      cgd 			save_com[0] = save_com[1] = ' ';
    330  1.1      cgd 			sc_end = &(save_com[2]);
    331  1.1      cgd 		    }
    332  1.1      cgd 		    else {
    333  1.1      cgd 			*sc_end++ = '\n';	/* add newline between
    334  1.1      cgd 						 * comments */
    335  1.1      cgd 			*sc_end++ = ' ';
    336  1.1      cgd 			--line_no;
    337  1.1      cgd 		    }
    338  1.1      cgd 		    *sc_end++ = '/';	/* copy in start of comment */
    339  1.1      cgd 		    *sc_end++ = '*';
    340  1.1      cgd 
    341  1.1      cgd 		    for (;;) {	/* loop until we get to the end of the comment */
    342  1.1      cgd 			*sc_end = *buf_ptr++;
    343  1.1      cgd 			if (buf_ptr >= buf_end)
    344  1.1      cgd 			    fill_buffer();
    345  1.1      cgd 
    346  1.1      cgd 			if (*sc_end++ == '*' && *buf_ptr == '/')
    347  1.1      cgd 			    break;	/* we are at end of comment */
    348  1.1      cgd 
    349  1.1      cgd 			if (sc_end >= &(save_com[sc_size])) {	/* check for temp buffer
    350  1.1      cgd 								 * overflow */
    351  1.1      cgd 			    diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
    352  1.1      cgd 			    fflush(output);
    353  1.1      cgd 			    exit(1);
    354  1.1      cgd 			}
    355  1.1      cgd 		    }
    356  1.1      cgd 		    *sc_end++ = '/';	/* add ending slash */
    357  1.1      cgd 		    if (++buf_ptr >= buf_end)	/* get past / in buffer */
    358  1.1      cgd 			fill_buffer();
    359  1.1      cgd 		    break;
    360  1.1      cgd 		}
    361  1.1      cgd 	    default:		/* it is the start of a normal statment */
    362  1.1      cgd 		if (flushed_nl)	/* if we flushed a newline, make sure it is
    363  1.1      cgd 				 * put back */
    364  1.1      cgd 		    force_nl = true;
    365  1.1      cgd 		if (type_code == sp_paren && *token == 'i'
    366  1.1      cgd 			&& last_else && ps.else_if
    367  1.1      cgd 			|| type_code == sp_nparen && *token == 'e'
    368  1.1      cgd 			&& e_code != s_code && e_code[-1] == '}')
    369  1.1      cgd 		    force_nl = false;
    370  1.1      cgd 
    371  1.1      cgd 		if (sc_end == 0) {	/* ignore buffering if comment wasnt
    372  1.1      cgd 					 * saved up */
    373  1.1      cgd 		    ps.search_brace = false;
    374  1.1      cgd 		    goto check_type;
    375  1.1      cgd 		}
    376  1.1      cgd 		if (force_nl) {	/* if we should insert a nl here, put it into
    377  1.1      cgd 				 * the buffer */
    378  1.1      cgd 		    force_nl = false;
    379  1.1      cgd 		    --line_no;	/* this will be re-increased when the nl is
    380  1.1      cgd 				 * read from the buffer */
    381  1.1      cgd 		    *sc_end++ = '\n';
    382  1.1      cgd 		    *sc_end++ = ' ';
    383  1.1      cgd 		    if (verbose && !flushed_nl)	/* print error msg if the line
    384  1.1      cgd 						 * was not already broken */
    385  1.1      cgd 			diag(0, "Line broken");
    386  1.1      cgd 		    flushed_nl = false;
    387  1.1      cgd 		}
    388  1.1      cgd 		for (t_ptr = token; *t_ptr; ++t_ptr)
    389  1.1      cgd 		    *sc_end++ = *t_ptr;	/* copy token into temp buffer */
    390  1.1      cgd 		ps.procname[0] = 0;
    391  1.1      cgd 
    392  1.1      cgd 	sw_buffer:
    393  1.1      cgd 		ps.search_brace = false;	/* stop looking for start of
    394  1.1      cgd 						 * stmt */
    395  1.1      cgd 		bp_save = buf_ptr;	/* save current input buffer */
    396  1.1      cgd 		be_save = buf_end;
    397  1.1      cgd 		buf_ptr = save_com;	/* fix so that subsequent calls to
    398  1.1      cgd 					 * lexi will take tokens out of
    399  1.1      cgd 					 * save_com */
    400  1.1      cgd 		*sc_end++ = ' ';/* add trailing blank, just in case */
    401  1.1      cgd 		buf_end = sc_end;
    402  1.1      cgd 		sc_end = 0;
    403  1.1      cgd 		break;
    404  1.1      cgd 	    }			/* end of switch */
    405  1.1      cgd 	    if (type_code != 0)	/* we must make this check, just in case there
    406  1.1      cgd 				 * was an unexpected EOF */
    407  1.1      cgd 		type_code = lexi();	/* read another token */
    408  1.1      cgd 	    /* if (ps.search_brace) ps.procname[0] = 0; */
    409  1.1      cgd 	    if ((is_procname = ps.procname[0]) && flushed_nl
    410  1.1      cgd 		    && !procnames_start_line && ps.in_decl
    411  1.1      cgd 		    && type_code == ident)
    412  1.1      cgd 		flushed_nl = 0;
    413  1.1      cgd 	}			/* end of while (search_brace) */
    414  1.1      cgd 	last_else = 0;
    415  1.1      cgd check_type:
    416  1.1      cgd 	if (type_code == 0) {	/* we got eof */
    417  1.1      cgd 	    if (s_lab != e_lab || s_code != e_code
    418  1.1      cgd 		    || s_com != e_com)	/* must dump end of line */
    419  1.1      cgd 		dump_line();
    420  1.1      cgd 	    if (ps.tos > 1)	/* check for balanced braces */
    421  1.1      cgd 		diag(1, "Stuff missing from end of file.");
    422  1.1      cgd 
    423  1.1      cgd 	    if (verbose) {
    424  1.1      cgd 		printf("There were %d output lines and %d comments\n",
    425  1.1      cgd 		       ps.out_lines, ps.out_coms);
    426  1.1      cgd 		printf("(Lines with comments)/(Lines with code): %6.3f\n",
    427  1.1      cgd 		       (1.0 * ps.com_lines) / code_lines);
    428  1.1      cgd 	    }
    429  1.1      cgd 	    fflush(output);
    430  1.1      cgd 	    exit(found_err);
    431  1.1      cgd 	}
    432  1.1      cgd 	if (
    433  1.1      cgd 		(type_code != comment) &&
    434  1.1      cgd 		(type_code != newline) &&
    435  1.1      cgd 		(type_code != preesc) &&
    436  1.1      cgd 		(type_code != form_feed)) {
    437  1.1      cgd 	    if (force_nl &&
    438  1.1      cgd 		    (type_code != semicolon) &&
    439  1.1      cgd 		    (type_code != lbrace || !btype_2)) {
    440  1.1      cgd 		/* we should force a broken line here */
    441  1.1      cgd 		if (verbose && !flushed_nl)
    442  1.1      cgd 		    diag(0, "Line broken");
    443  1.1      cgd 		flushed_nl = false;
    444  1.1      cgd 		dump_line();
    445  1.1      cgd 		ps.want_blank = false;	/* dont insert blank at line start */
    446  1.1      cgd 		force_nl = false;
    447  1.1      cgd 	    }
    448  1.1      cgd 	    ps.in_stmt = true;	/* turn on flag which causes an extra level of
    449  1.1      cgd 				 * indentation. this is turned off by a ; or
    450  1.1      cgd 				 * '}' */
    451  1.1      cgd 	    if (s_com != e_com) {	/* the turkey has embedded a comment
    452  1.1      cgd 					 * in a line. fix it */
    453  1.1      cgd 		*e_code++ = ' ';
    454  1.1      cgd 		for (t_ptr = s_com; *t_ptr; ++t_ptr) {
    455  1.1      cgd 		    CHECK_SIZE_CODE;
    456  1.1      cgd 		    *e_code++ = *t_ptr;
    457  1.1      cgd 		}
    458  1.1      cgd 		*e_code++ = ' ';
    459  1.1      cgd 		*e_code = '\0';	/* null terminate code sect */
    460  1.1      cgd 		ps.want_blank = false;
    461  1.1      cgd 		e_com = s_com;
    462  1.1      cgd 	    }
    463  1.1      cgd 	}
    464  1.1      cgd 	else if (type_code != comment)	/* preserve force_nl thru a comment */
    465  1.1      cgd 	    force_nl = false;	/* cancel forced newline after newline, form
    466  1.1      cgd 				 * feed, etc */
    467  1.1      cgd 
    468  1.1      cgd 
    469  1.1      cgd 
    470  1.1      cgd 	/*-----------------------------------------------------*\
    471  1.1      cgd 	|	   do switch on type of token scanned		|
    472  1.1      cgd 	\*-----------------------------------------------------*/
    473  1.1      cgd 	CHECK_SIZE_CODE;
    474  1.1      cgd 	switch (type_code) {	/* now, decide what to do with the token */
    475  1.1      cgd 
    476  1.1      cgd 	case form_feed:	/* found a form feed in line */
    477  1.1      cgd 	    ps.use_ff = true;	/* a form feed is treated much like a newline */
    478  1.1      cgd 	    dump_line();
    479  1.1      cgd 	    ps.want_blank = false;
    480  1.1      cgd 	    break;
    481  1.1      cgd 
    482  1.1      cgd 	case newline:
    483  1.1      cgd 	    if (ps.last_token != comma || ps.p_l_follow > 0
    484  1.1      cgd 		    || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
    485  1.1      cgd 		dump_line();
    486  1.1      cgd 		ps.want_blank = false;
    487  1.1      cgd 	    }
    488  1.1      cgd 	    ++line_no;		/* keep track of input line number */
    489  1.1      cgd 	    break;
    490  1.1      cgd 
    491  1.1      cgd 	case lparen:		/* got a '(' or '[' */
    492  1.1      cgd 	    ++ps.p_l_follow;	/* count parens to make Healy happy */
    493  1.1      cgd 	    if (ps.want_blank && *token != '[' &&
    494  1.1      cgd 		    (ps.last_token != ident || proc_calls_space
    495  1.1      cgd 	      || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
    496  1.1      cgd 		*e_code++ = ' ';
    497  1.1      cgd 	    if (ps.in_decl && !ps.block_init)
    498  1.1      cgd 		if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
    499  1.1      cgd 		    ps.dumped_decl_indent = 1;
    500  1.1      cgd 		    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
    501  1.1      cgd 		    e_code += strlen(e_code);
    502  1.1      cgd 		}
    503  1.1      cgd 		else {
    504  1.1      cgd 		    while ((e_code - s_code) < dec_ind) {
    505  1.1      cgd 			CHECK_SIZE_CODE;
    506  1.1      cgd 			*e_code++ = ' ';
    507  1.1      cgd 		    }
    508  1.1      cgd 		    *e_code++ = token[0];
    509  1.1      cgd 		}
    510  1.1      cgd 	    else
    511  1.1      cgd 		*e_code++ = token[0];
    512  1.1      cgd 	    ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
    513  1.1      cgd 	    if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
    514  1.1      cgd 		    && ps.paren_indents[0] < 2 * ps.ind_size)
    515  1.1      cgd 		ps.paren_indents[0] = 2 * ps.ind_size;
    516  1.1      cgd 	    ps.want_blank = false;
    517  1.1      cgd 	    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
    518  1.1      cgd 		/*
    519  1.1      cgd 		 * this is a kluge to make sure that declarations will be
    520  1.1      cgd 		 * aligned right if proc decl has an explicit type on it, i.e.
    521  1.1      cgd 		 * "int a(x) {..."
    522  1.1      cgd 		 */
    523  1.1      cgd 		parse(semicolon);	/* I said this was a kluge... */
    524  1.1      cgd 		ps.in_or_st = false;	/* turn off flag for structure decl or
    525  1.1      cgd 					 * initialization */
    526  1.1      cgd 	    }
    527  1.1      cgd 	    if (ps.sizeof_keyword)
    528  1.1      cgd 		ps.sizeof_mask |= 1 << ps.p_l_follow;
    529  1.1      cgd 	    break;
    530  1.1      cgd 
    531  1.1      cgd 	case rparen:		/* got a ')' or ']' */
    532  1.1      cgd 	    rparen_count--;
    533  1.1      cgd 	    if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
    534  1.1      cgd 		ps.last_u_d = true;
    535  1.1      cgd 		ps.cast_mask &= (1 << ps.p_l_follow) - 1;
    536  1.1      cgd 	    }
    537  1.1      cgd 	    ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
    538  1.1      cgd 	    if (--ps.p_l_follow < 0) {
    539  1.1      cgd 		ps.p_l_follow = 0;
    540  1.1      cgd 		diag(0, "Extra %c", *token);
    541  1.1      cgd 	    }
    542  1.1      cgd 	    if (e_code == s_code)	/* if the paren starts the line */
    543  1.1      cgd 		ps.paren_level = ps.p_l_follow;	/* then indent it */
    544  1.1      cgd 
    545  1.1      cgd 	    *e_code++ = token[0];
    546  1.1      cgd 	    ps.want_blank = true;
    547  1.1      cgd 
    548  1.1      cgd 	    if (sp_sw && (ps.p_l_follow == 0)) {	/* check for end of if
    549  1.1      cgd 							 * (...), or some such */
    550  1.1      cgd 		sp_sw = false;
    551  1.1      cgd 		force_nl = true;/* must force newline after if */
    552  1.1      cgd 		ps.last_u_d = true;	/* inform lexi that a following
    553  1.1      cgd 					 * operator is unary */
    554  1.1      cgd 		ps.in_stmt = false;	/* dont use stmt continuation
    555  1.1      cgd 					 * indentation */
    556  1.1      cgd 
    557  1.1      cgd 		parse(hd_type);	/* let parser worry about if, or whatever */
    558  1.1      cgd 	    }
    559  1.1      cgd 	    ps.search_brace = btype_2;	/* this should insure that constructs
    560  1.1      cgd 					 * such as main(){...} and int[]{...}
    561  1.1      cgd 					 * have their braces put in the right
    562  1.1      cgd 					 * place */
    563  1.1      cgd 	    break;
    564  1.1      cgd 
    565  1.1      cgd 	case unary_op:		/* this could be any unary operation */
    566  1.1      cgd 	    if (ps.want_blank)
    567  1.1      cgd 		*e_code++ = ' ';
    568  1.1      cgd 
    569  1.1      cgd 	    if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
    570  1.1      cgd 		sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
    571  1.1      cgd 		ps.dumped_decl_indent = 1;
    572  1.1      cgd 		e_code += strlen(e_code);
    573  1.1      cgd 	    }
    574  1.1      cgd 	    else {
    575  1.1      cgd 		char       *res = token;
    576  1.1      cgd 
    577  1.1      cgd 		if (ps.in_decl && !ps.block_init) {	/* if this is a unary op
    578  1.1      cgd 							 * in a declaration, we
    579  1.1      cgd 							 * should indent this
    580  1.1      cgd 							 * token */
    581  1.1      cgd 		    for (i = 0; token[i]; ++i);	/* find length of token */
    582  1.1      cgd 		    while ((e_code - s_code) < (dec_ind - i)) {
    583  1.1      cgd 			CHECK_SIZE_CODE;
    584  1.1      cgd 			*e_code++ = ' ';	/* pad it */
    585  1.1      cgd 		    }
    586  1.1      cgd 		}
    587  1.1      cgd 		if (troff && token[0] == '-' && token[1] == '>')
    588  1.1      cgd 		    res = "\\(->";
    589  1.1      cgd 		for (t_ptr = res; *t_ptr; ++t_ptr) {
    590  1.1      cgd 		    CHECK_SIZE_CODE;
    591  1.1      cgd 		    *e_code++ = *t_ptr;
    592  1.1      cgd 		}
    593  1.1      cgd 	    }
    594  1.1      cgd 	    ps.want_blank = false;
    595  1.1      cgd 	    break;
    596  1.1      cgd 
    597  1.1      cgd 	case binary_op:	/* any binary operation */
    598  1.1      cgd 	    if (ps.want_blank)
    599  1.1      cgd 		*e_code++ = ' ';
    600  1.1      cgd 	    {
    601  1.1      cgd 		char       *res = token;
    602  1.1      cgd 
    603  1.1      cgd 		if (troff)
    604  1.1      cgd 		    switch (token[0]) {
    605  1.1      cgd 		    case '<':
    606  1.1      cgd 			if (token[1] == '=')
    607  1.1      cgd 			    res = "\\(<=";
    608  1.1      cgd 			break;
    609  1.1      cgd 		    case '>':
    610  1.1      cgd 			if (token[1] == '=')
    611  1.1      cgd 			    res = "\\(>=";
    612  1.1      cgd 			break;
    613  1.1      cgd 		    case '!':
    614  1.1      cgd 			if (token[1] == '=')
    615  1.1      cgd 			    res = "\\(!=";
    616  1.1      cgd 			break;
    617  1.1      cgd 		    case '|':
    618  1.1      cgd 			if (token[1] == '|')
    619  1.1      cgd 			    res = "\\(br\\(br";
    620  1.1      cgd 			else if (token[1] == 0)
    621  1.1      cgd 			    res = "\\(br";
    622  1.1      cgd 			break;
    623  1.1      cgd 		    }
    624  1.1      cgd 		for (t_ptr = res; *t_ptr; ++t_ptr) {
    625  1.1      cgd 		    CHECK_SIZE_CODE;
    626  1.1      cgd 		    *e_code++ = *t_ptr;	/* move the operator */
    627  1.1      cgd 		}
    628  1.1      cgd 	    }
    629  1.1      cgd 	    ps.want_blank = true;
    630  1.1      cgd 	    break;
    631  1.1      cgd 
    632  1.1      cgd 	case postop:		/* got a trailing ++ or -- */
    633  1.1      cgd 	    *e_code++ = token[0];
    634  1.1      cgd 	    *e_code++ = token[1];
    635  1.1      cgd 	    ps.want_blank = true;
    636  1.1      cgd 	    break;
    637  1.1      cgd 
    638  1.1      cgd 	case question:		/* got a ? */
    639  1.1      cgd 	    squest++;		/* this will be used when a later colon
    640  1.1      cgd 				 * appears so we can distinguish the
    641  1.1      cgd 				 * <c>?<n>:<n> construct */
    642  1.1      cgd 	    if (ps.want_blank)
    643  1.1      cgd 		*e_code++ = ' ';
    644  1.1      cgd 	    *e_code++ = '?';
    645  1.1      cgd 	    ps.want_blank = true;
    646  1.1      cgd 	    break;
    647  1.1      cgd 
    648  1.1      cgd 	case casestmt:		/* got word 'case' or 'default' */
    649  1.1      cgd 	    scase = true;	/* so we can process the later colon properly */
    650  1.1      cgd 	    goto copy_id;
    651  1.1      cgd 
    652  1.1      cgd 	case colon:		/* got a ':' */
    653  1.1      cgd 	    if (squest > 0) {	/* it is part of the <c>?<n>: <n> construct */
    654  1.1      cgd 		--squest;
    655  1.1      cgd 		if (ps.want_blank)
    656  1.1      cgd 		    *e_code++ = ' ';
    657  1.1      cgd 		*e_code++ = ':';
    658  1.1      cgd 		ps.want_blank = true;
    659  1.1      cgd 		break;
    660  1.1      cgd 	    }
    661  1.1      cgd 	    if (ps.in_decl) {
    662  1.1      cgd 		*e_code++ = ':';
    663  1.1      cgd 		ps.want_blank = false;
    664  1.1      cgd 		break;
    665  1.1      cgd 	    }
    666  1.1      cgd 	    ps.in_stmt = false;	/* seeing a label does not imply we are in a
    667  1.1      cgd 				 * stmt */
    668  1.1      cgd 	    for (t_ptr = s_code; *t_ptr; ++t_ptr)
    669  1.1      cgd 		*e_lab++ = *t_ptr;	/* turn everything so far into a label */
    670  1.1      cgd 	    e_code = s_code;
    671  1.1      cgd 	    *e_lab++ = ':';
    672  1.1      cgd 	    *e_lab++ = ' ';
    673  1.1      cgd 	    *e_lab = '\0';
    674  1.1      cgd 
    675  1.1      cgd 	    force_nl = ps.pcase = scase;	/* ps.pcase will be used by
    676  1.1      cgd 						 * dump_line to decide how to
    677  1.1      cgd 						 * indent the label. force_nl
    678  1.1      cgd 						 * will force a case n: to be
    679  1.1      cgd 						 * on a line by itself */
    680  1.1      cgd 	    scase = false;
    681  1.1      cgd 	    ps.want_blank = false;
    682  1.1      cgd 	    break;
    683  1.1      cgd 
    684  1.1      cgd 	case semicolon:	/* got a ';' */
    685  1.1      cgd 	    ps.in_or_st = false;/* we are not in an initialization or
    686  1.1      cgd 				 * structure declaration */
    687  1.1      cgd 	    scase = false;	/* these will only need resetting in a error */
    688  1.1      cgd 	    squest = 0;
    689  1.1      cgd 	    if (ps.last_token == rparen && rparen_count == 0)
    690  1.1      cgd 		ps.in_parameter_declaration = 0;
    691  1.1      cgd 	    ps.cast_mask = 0;
    692  1.1      cgd 	    ps.sizeof_mask = 0;
    693  1.1      cgd 	    ps.block_init = 0;
    694  1.1      cgd 	    ps.block_init_level = 0;
    695  1.1      cgd 	    ps.just_saw_decl--;
    696  1.1      cgd 
    697  1.1      cgd 	    if (ps.in_decl && s_code == e_code && !ps.block_init)
    698  1.1      cgd 		while ((e_code - s_code) < (dec_ind - 1)) {
    699  1.1      cgd 		    CHECK_SIZE_CODE;
    700  1.1      cgd 		    *e_code++ = ' ';
    701  1.1      cgd 		}
    702  1.1      cgd 
    703  1.1      cgd 	    ps.in_decl = (ps.dec_nest > 0);	/* if we were in a first level
    704  1.1      cgd 						 * structure declaration, we
    705  1.1      cgd 						 * arent any more */
    706  1.1      cgd 
    707  1.1      cgd 	    if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
    708  1.1      cgd 
    709  1.1      cgd 		/*
    710  1.1      cgd 		 * This should be true iff there were unbalanced parens in the
    711  1.1      cgd 		 * stmt.  It is a bit complicated, because the semicolon might
    712  1.1      cgd 		 * be in a for stmt
    713  1.1      cgd 		 */
    714  1.1      cgd 		diag(1, "Unbalanced parens");
    715  1.1      cgd 		ps.p_l_follow = 0;
    716  1.1      cgd 		if (sp_sw) {	/* this is a check for a if, while, etc. with
    717  1.1      cgd 				 * unbalanced parens */
    718  1.1      cgd 		    sp_sw = false;
    719  1.1      cgd 		    parse(hd_type);	/* dont lose the if, or whatever */
    720  1.1      cgd 		}
    721  1.1      cgd 	    }
    722  1.1      cgd 	    *e_code++ = ';';
    723  1.1      cgd 	    ps.want_blank = true;
    724  1.1      cgd 	    ps.in_stmt = (ps.p_l_follow > 0);	/* we are no longer in the
    725  1.1      cgd 						 * middle of a stmt */
    726  1.1      cgd 
    727  1.1      cgd 	    if (!sp_sw) {	/* if not if for (;;) */
    728  1.1      cgd 		parse(semicolon);	/* let parser know about end of stmt */
    729  1.1      cgd 		force_nl = true;/* force newline after a end of stmt */
    730  1.1      cgd 	    }
    731  1.1      cgd 	    break;
    732  1.1      cgd 
    733  1.1      cgd 	case lbrace:		/* got a '{' */
    734  1.1      cgd 	    ps.in_stmt = false;	/* dont indent the {} */
    735  1.1      cgd 	    if (!ps.block_init)
    736  1.1      cgd 		force_nl = true;/* force other stuff on same line as '{' onto
    737  1.1      cgd 				 * new line */
    738  1.1      cgd 	    else if (ps.block_init_level <= 0)
    739  1.1      cgd 		ps.block_init_level = 1;
    740  1.1      cgd 	    else
    741  1.1      cgd 		ps.block_init_level++;
    742  1.1      cgd 
    743  1.1      cgd 	    if (s_code != e_code && !ps.block_init) {
    744  1.1      cgd 		if (!btype_2) {
    745  1.1      cgd 		    dump_line();
    746  1.1      cgd 		    ps.want_blank = false;
    747  1.1      cgd 		}
    748  1.1      cgd 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
    749  1.1      cgd 		    ps.i_l_follow = 0;
    750  1.1      cgd 		    dump_line();
    751  1.1      cgd 		    ps.want_blank = false;
    752  1.1      cgd 		}
    753  1.1      cgd 	    }
    754  1.1      cgd 	    if (ps.in_parameter_declaration)
    755  1.1      cgd 		prefix_blankline_requested = 0;
    756  1.1      cgd 
    757  1.1      cgd 	    if (ps.p_l_follow > 0) {	/* check for preceeding unbalanced
    758  1.1      cgd 					 * parens */
    759  1.1      cgd 		diag(1, "Unbalanced parens");
    760  1.1      cgd 		ps.p_l_follow = 0;
    761  1.1      cgd 		if (sp_sw) {	/* check for unclosed if, for, etc. */
    762  1.1      cgd 		    sp_sw = false;
    763  1.1      cgd 		    parse(hd_type);
    764  1.1      cgd 		    ps.ind_level = ps.i_l_follow;
    765  1.1      cgd 		}
    766  1.1      cgd 	    }
    767  1.1      cgd 	    if (s_code == e_code)
    768  1.1      cgd 		ps.ind_stmt = false;	/* dont put extra indentation on line
    769  1.1      cgd 					 * with '{' */
    770  1.1      cgd 	    if (ps.in_decl && ps.in_or_st) {	/* this is either a structure
    771  1.1      cgd 						 * declaration or an init */
    772  1.1      cgd 		di_stack[ps.dec_nest++] = dec_ind;
    773  1.1      cgd 		/* ?		dec_ind = 0; */
    774  1.1      cgd 	    }
    775  1.1      cgd 	    else {
    776  1.1      cgd 		ps.decl_on_line = false;	/* we cant be in the middle of
    777  1.1      cgd 						 * a declaration, so dont do
    778  1.1      cgd 						 * special indentation of
    779  1.1      cgd 						 * comments */
    780  1.1      cgd 		if (blanklines_after_declarations_at_proctop
    781  1.1      cgd 			&& ps.in_parameter_declaration)
    782  1.1      cgd 		    postfix_blankline_requested = 1;
    783  1.1      cgd 		ps.in_parameter_declaration = 0;
    784  1.1      cgd 	    }
    785  1.1      cgd 	    dec_ind = 0;
    786  1.1      cgd 	    parse(lbrace);	/* let parser know about this */
    787  1.1      cgd 	    if (ps.want_blank)	/* put a blank before '{' if '{' is not at
    788  1.1      cgd 				 * start of line */
    789  1.1      cgd 		*e_code++ = ' ';
    790  1.1      cgd 	    ps.want_blank = false;
    791  1.1      cgd 	    *e_code++ = '{';
    792  1.1      cgd 	    ps.just_saw_decl = 0;
    793  1.1      cgd 	    break;
    794  1.1      cgd 
    795  1.1      cgd 	case rbrace:		/* got a '}' */
    796  1.1      cgd 	    if (ps.p_stack[ps.tos] == decl && !ps.block_init)	/* semicolons can be
    797  1.1      cgd 								 * omitted in
    798  1.1      cgd 								 * declarations */
    799  1.1      cgd 		parse(semicolon);
    800  1.1      cgd 	    if (ps.p_l_follow) {/* check for unclosed if, for, else. */
    801  1.1      cgd 		diag(1, "Unbalanced parens");
    802  1.1      cgd 		ps.p_l_follow = 0;
    803  1.1      cgd 		sp_sw = false;
    804  1.1      cgd 	    }
    805  1.1      cgd 	    ps.just_saw_decl = 0;
    806  1.1      cgd 	    ps.block_init_level--;
    807  1.1      cgd 	    if (s_code != e_code && !ps.block_init) {	/* '}' must be first on
    808  1.1      cgd 							 * line */
    809  1.1      cgd 		if (verbose)
    810  1.1      cgd 		    diag(0, "Line broken");
    811  1.1      cgd 		dump_line();
    812  1.1      cgd 	    }
    813  1.1      cgd 	    *e_code++ = '}';
    814  1.1      cgd 	    ps.want_blank = true;
    815  1.1      cgd 	    ps.in_stmt = ps.ind_stmt = false;
    816  1.1      cgd 	    if (ps.dec_nest > 0) {	/* we are in multi-level structure
    817  1.1      cgd 					 * declaration */
    818  1.1      cgd 		dec_ind = di_stack[--ps.dec_nest];
    819  1.1      cgd 		if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
    820  1.1      cgd 		    ps.just_saw_decl = 2;
    821  1.1      cgd 		ps.in_decl = true;
    822  1.1      cgd 	    }
    823  1.1      cgd 	    prefix_blankline_requested = 0;
    824  1.1      cgd 	    parse(rbrace);	/* let parser know about this */
    825  1.1      cgd 	    ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
    826  1.1      cgd 		&& ps.il[ps.tos] >= ps.ind_level;
    827  1.1      cgd 	    if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
    828  1.1      cgd 		postfix_blankline_requested = 1;
    829  1.1      cgd 	    break;
    830  1.1      cgd 
    831  1.1      cgd 	case swstmt:		/* got keyword "switch" */
    832  1.1      cgd 	    sp_sw = true;
    833  1.1      cgd 	    hd_type = swstmt;	/* keep this for when we have seen the
    834  1.1      cgd 				 * expression */
    835  1.1      cgd 	    goto copy_id;	/* go move the token into buffer */
    836  1.1      cgd 
    837  1.1      cgd 	case sp_paren:		/* token is if, while, for */
    838  1.1      cgd 	    sp_sw = true;	/* the interesting stuff is done after the
    839  1.1      cgd 				 * expression is scanned */
    840  1.1      cgd 	    hd_type = (*token == 'i' ? ifstmt :
    841  1.1      cgd 		       (*token == 'w' ? whilestmt : forstmt));
    842  1.1      cgd 
    843  1.1      cgd 	    /*
    844  1.1      cgd 	     * remember the type of header for later use by parser
    845  1.1      cgd 	     */
    846  1.1      cgd 	    goto copy_id;	/* copy the token into line */
    847  1.1      cgd 
    848  1.1      cgd 	case sp_nparen:	/* got else, do */
    849  1.1      cgd 	    ps.in_stmt = false;
    850  1.1      cgd 	    if (*token == 'e') {
    851  1.1      cgd 		if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
    852  1.1      cgd 		    if (verbose)
    853  1.1      cgd 			diag(0, "Line broken");
    854  1.1      cgd 		    dump_line();/* make sure this starts a line */
    855  1.1      cgd 		    ps.want_blank = false;
    856  1.1      cgd 		}
    857  1.1      cgd 		force_nl = true;/* also, following stuff must go onto new line */
    858  1.1      cgd 		last_else = 1;
    859  1.1      cgd 		parse(elselit);
    860  1.1      cgd 	    }
    861  1.1      cgd 	    else {
    862  1.1      cgd 		if (e_code != s_code) {	/* make sure this starts a line */
    863  1.1      cgd 		    if (verbose)
    864  1.1      cgd 			diag(0, "Line broken");
    865  1.1      cgd 		    dump_line();
    866  1.1      cgd 		    ps.want_blank = false;
    867  1.1      cgd 		}
    868  1.1      cgd 		force_nl = true;/* also, following stuff must go onto new line */
    869  1.1      cgd 		last_else = 0;
    870  1.1      cgd 		parse(dolit);
    871  1.1      cgd 	    }
    872  1.1      cgd 	    goto copy_id;	/* move the token into line */
    873  1.1      cgd 
    874  1.1      cgd 	case decl:		/* we have a declaration type (int, register,
    875  1.1      cgd 				 * etc.) */
    876  1.1      cgd 	    parse(decl);	/* let parser worry about indentation */
    877  1.1      cgd 	    if (ps.last_token == rparen && ps.tos <= 1) {
    878  1.1      cgd 		ps.in_parameter_declaration = 1;
    879  1.1      cgd 		if (s_code != e_code) {
    880  1.1      cgd 		    dump_line();
    881  1.1      cgd 		    ps.want_blank = 0;
    882  1.1      cgd 		}
    883  1.1      cgd 	    }
    884  1.1      cgd 	    if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
    885  1.1      cgd 		ps.ind_level = ps.i_l_follow = 1;
    886  1.1      cgd 		ps.ind_stmt = 0;
    887  1.1      cgd 	    }
    888  1.1      cgd 	    ps.in_or_st = true;	/* this might be a structure or initialization
    889  1.1      cgd 				 * declaration */
    890  1.1      cgd 	    ps.in_decl = ps.decl_on_line = true;
    891  1.1      cgd 	    if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
    892  1.1      cgd 		ps.just_saw_decl = 2;
    893  1.1      cgd 	    prefix_blankline_requested = 0;
    894  1.1      cgd 	    for (i = 0; token[i++];);	/* get length of token */
    895  1.1      cgd 
    896  1.1      cgd 	    /*
    897  1.1      cgd 	     * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
    898  1.1      cgd 	     * : i);
    899  1.1      cgd 	     */
    900  1.1      cgd 	    dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
    901  1.1      cgd 	    goto copy_id;
    902  1.1      cgd 
    903  1.1      cgd 	case ident:		/* got an identifier or constant */
    904  1.1      cgd 	    if (ps.in_decl) {	/* if we are in a declaration, we must indent
    905  1.1      cgd 				 * identifier */
    906  1.1      cgd 		if (ps.want_blank)
    907  1.1      cgd 		    *e_code++ = ' ';
    908  1.1      cgd 		ps.want_blank = false;
    909  1.1      cgd 		if (is_procname == 0 || !procnames_start_line) {
    910  1.1      cgd 		    if (!ps.block_init)
    911  1.1      cgd 			if (troff && !ps.dumped_decl_indent) {
    912  1.1      cgd 			    sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
    913  1.1      cgd 			    ps.dumped_decl_indent = 1;
    914  1.1      cgd 			    e_code += strlen(e_code);
    915  1.1      cgd 			}
    916  1.1      cgd 			else
    917  1.1      cgd 			    while ((e_code - s_code) < dec_ind) {
    918  1.1      cgd 				CHECK_SIZE_CODE;
    919  1.1      cgd 				*e_code++ = ' ';
    920  1.1      cgd 			    }
    921  1.1      cgd 		}
    922  1.1      cgd 		else {
    923  1.1      cgd 		    if (dec_ind && s_code != e_code)
    924  1.1      cgd 			dump_line();
    925  1.1      cgd 		    dec_ind = 0;
    926  1.1      cgd 		    ps.want_blank = false;
    927  1.1      cgd 		}
    928  1.1      cgd 	    }
    929  1.1      cgd 	    else if (sp_sw && ps.p_l_follow == 0) {
    930  1.1      cgd 		sp_sw = false;
    931  1.1      cgd 		force_nl = true;
    932  1.1      cgd 		ps.last_u_d = true;
    933  1.1      cgd 		ps.in_stmt = false;
    934  1.1      cgd 		parse(hd_type);
    935  1.1      cgd 	    }
    936  1.1      cgd     copy_id:
    937  1.1      cgd 	    if (ps.want_blank)
    938  1.1      cgd 		*e_code++ = ' ';
    939  1.1      cgd 	    if (troff && ps.its_a_keyword) {
    940  1.1      cgd 		e_code = chfont(&bodyf, &keywordf, e_code);
    941  1.1      cgd 		for (t_ptr = token; *t_ptr; ++t_ptr) {
    942  1.1      cgd 		    CHECK_SIZE_CODE;
    943  1.1      cgd 		    *e_code++ = keywordf.allcaps && islower(*t_ptr)
    944  1.1      cgd 			? toupper(*t_ptr) : *t_ptr;
    945  1.1      cgd 		}
    946  1.1      cgd 		e_code = chfont(&keywordf, &bodyf, e_code);
    947  1.1      cgd 	    }
    948  1.1      cgd 	    else
    949  1.1      cgd 		for (t_ptr = token; *t_ptr; ++t_ptr) {
    950  1.1      cgd 		    CHECK_SIZE_CODE;
    951  1.1      cgd 		    *e_code++ = *t_ptr;
    952  1.1      cgd 		}
    953  1.1      cgd 	    ps.want_blank = true;
    954  1.1      cgd 	    break;
    955  1.1      cgd 
    956  1.1      cgd 	case period:		/* treat a period kind of like a binary
    957  1.1      cgd 				 * operation */
    958  1.1      cgd 	    *e_code++ = '.';	/* move the period into line */
    959  1.1      cgd 	    ps.want_blank = false;	/* dont put a blank after a period */
    960  1.1      cgd 	    break;
    961  1.1      cgd 
    962  1.1      cgd 	case comma:
    963  1.1      cgd 	    ps.want_blank = (s_code != e_code);	/* only put blank after comma
    964  1.1      cgd 						 * if comma does not start the
    965  1.1      cgd 						 * line */
    966  1.1      cgd 	    if (ps.in_decl && is_procname == 0 && !ps.block_init)
    967  1.1      cgd 		while ((e_code - s_code) < (dec_ind - 1)) {
    968  1.1      cgd 		    CHECK_SIZE_CODE;
    969  1.1      cgd 		    *e_code++ = ' ';
    970  1.1      cgd 		}
    971  1.1      cgd 
    972  1.1      cgd 	    *e_code++ = ',';
    973  1.1      cgd 	    if (ps.p_l_follow == 0) {
    974  1.1      cgd 		if (ps.block_init_level <= 0)
    975  1.1      cgd 		    ps.block_init = 0;
    976  1.1      cgd 		if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
    977  1.1      cgd 		    force_nl = true;
    978  1.1      cgd 	    }
    979  1.1      cgd 	    break;
    980  1.1      cgd 
    981  1.1      cgd 	case preesc:		/* got the character '#' */
    982  1.1      cgd 	    if ((s_com != e_com) ||
    983  1.1      cgd 		    (s_lab != e_lab) ||
    984  1.1      cgd 		    (s_code != e_code))
    985  1.1      cgd 		dump_line();
    986  1.1      cgd 	    *e_lab++ = '#';	/* move whole line to 'label' buffer */
    987  1.1      cgd 	    {
    988  1.1      cgd 		int         in_comment = 0;
    989  1.1      cgd 		int         com_start = 0;
    990  1.1      cgd 		char        quote = 0;
    991  1.1      cgd 		int         com_end = 0;
    992  1.1      cgd 
    993  1.1      cgd 		while (*buf_ptr == ' ' || *buf_ptr == '\t') {
    994  1.1      cgd 		    buf_ptr++;
    995  1.1      cgd 		    if (buf_ptr >= buf_end)
    996  1.1      cgd 			fill_buffer();
    997  1.1      cgd 		}
    998  1.1      cgd 		while (*buf_ptr != '\n' || in_comment) {
    999  1.1      cgd 		    CHECK_SIZE_LAB;
   1000  1.1      cgd 		    *e_lab = *buf_ptr++;
   1001  1.1      cgd 		    if (buf_ptr >= buf_end)
   1002  1.1      cgd 			fill_buffer();
   1003  1.1      cgd 		    switch (*e_lab++) {
   1004  1.1      cgd 		    case BACKSLASH:
   1005  1.1      cgd 			if (troff)
   1006  1.1      cgd 			    *e_lab++ = BACKSLASH;
   1007  1.1      cgd 			if (!in_comment) {
   1008  1.1      cgd 			    *e_lab++ = *buf_ptr++;
   1009  1.1      cgd 			    if (buf_ptr >= buf_end)
   1010  1.1      cgd 				fill_buffer();
   1011  1.1      cgd 			}
   1012  1.1      cgd 			break;
   1013  1.1      cgd 		    case '/':
   1014  1.1      cgd 			if (*buf_ptr == '*' && !in_comment && !quote) {
   1015  1.1      cgd 			    in_comment = 1;
   1016  1.1      cgd 			    *e_lab++ = *buf_ptr++;
   1017  1.1      cgd 			    com_start = e_lab - s_lab - 2;
   1018  1.1      cgd 			}
   1019  1.1      cgd 			break;
   1020  1.1      cgd 		    case '"':
   1021  1.1      cgd 			if (quote == '"')
   1022  1.1      cgd 			    quote = 0;
   1023  1.1      cgd 			break;
   1024  1.1      cgd 		    case '\'':
   1025  1.1      cgd 			if (quote == '\'')
   1026  1.1      cgd 			    quote = 0;
   1027  1.1      cgd 			break;
   1028  1.1      cgd 		    case '*':
   1029  1.1      cgd 			if (*buf_ptr == '/' && in_comment) {
   1030  1.1      cgd 			    in_comment = 0;
   1031  1.1      cgd 			    *e_lab++ = *buf_ptr++;
   1032  1.1      cgd 			    com_end = e_lab - s_lab;
   1033  1.1      cgd 			}
   1034  1.1      cgd 			break;
   1035  1.1      cgd 		    }
   1036  1.1      cgd 		}
   1037  1.1      cgd 
   1038  1.1      cgd 		while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
   1039  1.1      cgd 		    e_lab--;
   1040  1.1      cgd 		if (e_lab - s_lab == com_end && bp_save == 0) {	/* comment on
   1041  1.1      cgd 								 * preprocessor line */
   1042  1.1      cgd 		    if (sc_end == 0)	/* if this is the first comment, we
   1043  1.1      cgd 					 * must set up the buffer */
   1044  1.1      cgd 			sc_end = &(save_com[0]);
   1045  1.1      cgd 		    else {
   1046  1.1      cgd 			*sc_end++ = '\n';	/* add newline between
   1047  1.1      cgd 						 * comments */
   1048  1.1      cgd 			*sc_end++ = ' ';
   1049  1.1      cgd 			--line_no;
   1050  1.1      cgd 		    }
   1051  1.1      cgd 		    bcopy(s_lab + com_start, sc_end, com_end - com_start);
   1052  1.1      cgd 		    sc_end += com_end - com_start;
   1053  1.1      cgd 		    if (sc_end >= &save_com[sc_size])
   1054  1.1      cgd 			abort();
   1055  1.1      cgd 		    e_lab = s_lab + com_start;
   1056  1.1      cgd 		    while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
   1057  1.1      cgd 			e_lab--;
   1058  1.1      cgd 		    bp_save = buf_ptr;	/* save current input buffer */
   1059  1.1      cgd 		    be_save = buf_end;
   1060  1.1      cgd 		    buf_ptr = save_com;	/* fix so that subsequent calls to
   1061  1.1      cgd 					 * lexi will take tokens out of
   1062  1.1      cgd 					 * save_com */
   1063  1.1      cgd 		    *sc_end++ = ' ';	/* add trailing blank, just in case */
   1064  1.1      cgd 		    buf_end = sc_end;
   1065  1.1      cgd 		    sc_end = 0;
   1066  1.1      cgd 		}
   1067  1.1      cgd 		*e_lab = '\0';	/* null terminate line */
   1068  1.1      cgd 		ps.pcase = false;
   1069  1.1      cgd 	    }
   1070  1.1      cgd 
   1071  1.1      cgd 	    if (strncmp(s_lab, "#if", 3) == 0) {
   1072  1.1      cgd 		if (blanklines_around_conditional_compilation) {
   1073  1.1      cgd 		    register    c;
   1074  1.1      cgd 		    prefix_blankline_requested++;
   1075  1.1      cgd 		    while ((c = getc(input)) == '\n');
   1076  1.1      cgd 		    ungetc(c, input);
   1077  1.1      cgd 		}
   1078  1.1      cgd 		if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
   1079  1.1      cgd 		    match_state[ifdef_level].tos = -1;
   1080  1.1      cgd 		    state_stack[ifdef_level++] = ps;
   1081  1.1      cgd 		}
   1082  1.1      cgd 		else
   1083  1.1      cgd 		    diag(1, "#if stack overflow");
   1084  1.1      cgd 	    }
   1085  1.1      cgd 	    else if (strncmp(s_lab, "#else", 5) == 0)
   1086  1.1      cgd 		if (ifdef_level <= 0)
   1087  1.1      cgd 		    diag(1, "Unmatched #else");
   1088  1.1      cgd 		else {
   1089  1.1      cgd 		    match_state[ifdef_level - 1] = ps;
   1090  1.1      cgd 		    ps = state_stack[ifdef_level - 1];
   1091  1.1      cgd 		}
   1092  1.1      cgd 	    else if (strncmp(s_lab, "#endif", 6) == 0) {
   1093  1.1      cgd 		if (ifdef_level <= 0)
   1094  1.1      cgd 		    diag(1, "Unmatched #endif");
   1095  1.1      cgd 		else {
   1096  1.1      cgd 		    ifdef_level--;
   1097  1.1      cgd 
   1098  1.1      cgd #ifdef undef
   1099  1.1      cgd 		    /*
   1100  1.1      cgd 		     * This match needs to be more intelligent before the
   1101  1.1      cgd 		     * message is useful
   1102  1.1      cgd 		     */
   1103  1.1      cgd 		    if (match_state[ifdef_level].tos >= 0
   1104  1.1      cgd 			  && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
   1105  1.1      cgd 			diag(0, "Syntactically inconsistant #ifdef alternatives.");
   1106  1.1      cgd #endif
   1107  1.1      cgd 		}
   1108  1.1      cgd 		if (blanklines_around_conditional_compilation) {
   1109  1.1      cgd 		    postfix_blankline_requested++;
   1110  1.1      cgd 		    n_real_blanklines = 0;
   1111  1.1      cgd 		}
   1112  1.1      cgd 	    }
   1113  1.1      cgd 	    break;		/* subsequent processing of the newline
   1114  1.1      cgd 				 * character will cause the line to be printed */
   1115  1.1      cgd 
   1116  1.1      cgd 	case comment:		/* we have gotten a /*  this is a biggie */
   1117  1.1      cgd 	    if (flushed_nl) {	/* we should force a broken line here */
   1118  1.1      cgd 		flushed_nl = false;
   1119  1.1      cgd 		dump_line();
   1120  1.1      cgd 		ps.want_blank = false;	/* dont insert blank at line start */
   1121  1.1      cgd 		force_nl = false;
   1122  1.1      cgd 	    }
   1123  1.1      cgd 	    pr_comment();
   1124  1.1      cgd 	    break;
   1125  1.1      cgd 	}			/* end of big switch stmt */
   1126  1.1      cgd 
   1127  1.1      cgd 	*e_code = '\0';		/* make sure code section is null terminated */
   1128  1.1      cgd 	if (type_code != comment && type_code != newline && type_code != preesc)
   1129  1.1      cgd 	    ps.last_token = type_code;
   1130  1.1      cgd     }				/* end of main while (1) loop */
   1131  1.1      cgd }
   1132  1.1      cgd 
   1133  1.1      cgd /*
   1134  1.1      cgd  * copy input file to backup file if in_name is /blah/blah/blah/file, then
   1135  1.1      cgd  * backup file will be ".Bfile" then make the backup file the input and
   1136  1.1      cgd  * original input file the output
   1137  1.1      cgd  */
   1138  1.1      cgd bakcopy()
   1139  1.1      cgd {
   1140  1.1      cgd     int         n,
   1141  1.1      cgd                 bakchn;
   1142  1.1      cgd     char        buff[8 * 1024];
   1143  1.1      cgd     register char *p;
   1144  1.1      cgd 
   1145  1.1      cgd     /* construct file name .Bfile */
   1146  1.1      cgd     for (p = in_name; *p; p++);	/* skip to end of string */
   1147  1.1      cgd     while (p > in_name && *p != '/')	/* find last '/' */
   1148  1.1      cgd 	p--;
   1149  1.1      cgd     if (*p == '/')
   1150  1.1      cgd 	p++;
   1151  1.1      cgd     sprintf(bakfile, "%s.BAK", p);
   1152  1.1      cgd 
   1153  1.1      cgd     /* copy in_name to backup file */
   1154  1.1      cgd     bakchn = creat(bakfile, 0600);
   1155  1.1      cgd     if (bakchn < 0)
   1156  1.1      cgd 	err(bakfile);
   1157  1.1      cgd     while (n = read(fileno(input), buff, sizeof buff))
   1158  1.1      cgd 	if (write(bakchn, buff, n) != n)
   1159  1.1      cgd 	    err(bakfile);
   1160  1.1      cgd     if (n < 0)
   1161  1.1      cgd 	err(in_name);
   1162  1.1      cgd     close(bakchn);
   1163  1.1      cgd     fclose(input);
   1164  1.1      cgd 
   1165  1.1      cgd     /* re-open backup file as the input file */
   1166  1.1      cgd     input = fopen(bakfile, "r");
   1167  1.1      cgd     if (input == 0)
   1168  1.1      cgd 	err(bakfile);
   1169  1.1      cgd     /* now the original input file will be the output */
   1170  1.1      cgd     output = fopen(in_name, "w");
   1171  1.1      cgd     if (output == 0) {
   1172  1.1      cgd 	unlink(bakfile);
   1173  1.1      cgd 	err(in_name);
   1174  1.1      cgd     }
   1175  1.1      cgd }
   1176  1.1      cgd 
   1177  1.1      cgd err(msg)
   1178  1.1      cgd 	char *msg;
   1179  1.1      cgd {
   1180  1.1      cgd 	(void)fprintf(stderr, "indent: %s: %s\n", msg, strerror(errno));
   1181  1.1      cgd 	exit(1);
   1182  1.1      cgd }
   1183