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