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