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