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