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