indent.c revision 1.127 1 /* $NetBSD: indent.c,v 1.127 2021/10/08 16:47:42 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: indent.c,v 1.127 2021/10/08 16:47:42 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
49 #endif
50
51 #include <sys/param.h>
52 #if HAVE_CAPSICUM
53 #include <sys/capsicum.h>
54 #include <capsicum_helpers.h>
55 #endif
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 #include "indent.h"
66
67 struct options opt = {
68 .brace_same_line = true,
69 .comment_delimiter_on_blankline = true,
70 .cuddle_else = true,
71 .comment_column = 33,
72 .decl_indent = 16,
73 .else_if = true,
74 .function_brace_split = true,
75 .format_col1_comments = true,
76 .format_block_comments = true,
77 .indent_parameters = true,
78 .indent_size = 8,
79 .local_decl_indent = -1,
80 .lineup_to_parens = true,
81 .procnames_start_line = true,
82 .star_comment_cont = true,
83 .tabsize = 8,
84 .max_line_length = 78,
85 .use_tabs = true,
86 };
87
88 struct parser_state ps;
89
90 struct buffer lab;
91 struct buffer code;
92 struct buffer com;
93 struct buffer token;
94
95 struct buffer inp;
96
97 char sc_buf[sc_size];
98 char *save_com;
99 static char *sc_end; /* pointer into save_com buffer */
100
101 char *saved_inp_s;
102 char *saved_inp_e;
103
104 bool found_err;
105 int next_blank_lines;
106 bool prefix_blankline_requested;
107 bool postfix_blankline_requested;
108 bool break_comma;
109 float case_ind;
110 bool had_eof;
111 int line_no;
112 bool inhibit_formatting;
113
114 static int ifdef_level;
115 static struct parser_state state_stack[5];
116
117 FILE *input;
118 FILE *output;
119
120 static void bakcopy(void);
121 static void indent_declaration(int, bool);
122
123 static const char *in_name = "Standard Input";
124 static const char *out_name = "Standard Output";
125 static const char *backup_suffix = ".BAK";
126 static char bakfile[MAXPATHLEN] = "";
127
128 #if HAVE_CAPSICUM
129 static void
130 init_capsicum(void)
131 {
132 cap_rights_t rights;
133
134 /* Restrict input/output descriptors and enter Capsicum sandbox. */
135 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
136 if (caph_rights_limit(fileno(output), &rights) < 0)
137 err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
138 cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
139 if (caph_rights_limit(fileno(input), &rights) < 0)
140 err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
141 if (caph_enter() < 0)
142 err(EXIT_FAILURE, "unable to enter capability mode");
143 }
144 #endif
145
146 static void
147 search_brace_newline(bool *force_nl)
148 {
149 if (sc_end == NULL) {
150 save_com = sc_buf;
151 save_com[0] = save_com[1] = ' ';
152 sc_end = &save_com[2];
153 }
154 *sc_end++ = '\n';
155
156 /*
157 * We may have inherited a force_nl == true from the previous token (like
158 * a semicolon). But once we know that a newline has been scanned in this
159 * loop, force_nl should be false.
160 *
161 * However, the force_nl == true must be preserved if newline is never
162 * scanned in this loop, so this assignment cannot be done earlier.
163 */
164 *force_nl = false;
165 }
166
167 static void
168 search_brace_comment(bool *comment_buffered)
169 {
170 if (sc_end == NULL) {
171 /*
172 * Copy everything from the start of the line, because
173 * process_comment() will use that to calculate original indentation
174 * of a boxed comment.
175 */
176 memcpy(sc_buf, inp.buf, (size_t)(inp.s - inp.buf) - 4);
177 save_com = sc_buf + (inp.s - inp.buf - 4);
178 save_com[0] = save_com[1] = ' ';
179 sc_end = &save_com[2];
180 }
181
182 *comment_buffered = true;
183 *sc_end++ = '/'; /* copy in start of comment */
184 *sc_end++ = '*';
185
186 for (;;) { /* loop until the end of the comment */
187 *sc_end++ = inbuf_next();
188 if (sc_end[-1] == '*' && *inp.s == '/')
189 break; /* we are at end of comment */
190 if (sc_end >= &save_com[sc_size]) { /* check for temp buffer
191 * overflow */
192 diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
193 fflush(output);
194 exit(1);
195 }
196 }
197
198 *sc_end++ = '/'; /* add ending slash */
199 inbuf_skip(); /* get past / in buffer */
200 }
201
202 static bool
203 search_brace_lbrace(void)
204 {
205 /*
206 * Put KNF-style lbraces before the buffered up tokens and jump out of
207 * this loop in order to avoid copying the token again.
208 */
209 if (sc_end != NULL && opt.brace_same_line) {
210 save_com[0] = '{';
211 /*
212 * Originally the lbrace may have been alone on its own line, but it
213 * will be moved into "the else's line", so if there was a newline
214 * resulting from the "{" before, it must be scanned now and ignored.
215 */
216 while (isspace((unsigned char)*inp.s)) {
217 inbuf_skip();
218 if (*inp.s == '\n')
219 break;
220 }
221 return true;
222 }
223 return false;
224 }
225
226 static bool
227 search_brace_other(token_type ttype, bool *force_nl,
228 bool comment_buffered, bool last_else)
229 {
230 bool remove_newlines;
231
232 remove_newlines =
233 /* "} else" */
234 (ttype == keyword_do_else && *token.s == 'e' &&
235 code.e != code.s && code.e[-1] == '}')
236 /* "else if" */
237 || (ttype == keyword_for_if_while &&
238 *token.s == 'i' && last_else && opt.else_if);
239 if (remove_newlines)
240 *force_nl = false;
241
242 if (sc_end == NULL) { /* ignore buffering if comment wasn't saved
243 * up */
244 ps.search_brace = false;
245 return false;
246 }
247
248 while (sc_end > save_com && isblank((unsigned char)sc_end[-1]))
249 sc_end--;
250
251 if (opt.swallow_optional_blanklines ||
252 (!comment_buffered && remove_newlines)) {
253 *force_nl = !remove_newlines;
254 while (sc_end > save_com && sc_end[-1] == '\n') {
255 sc_end--;
256 }
257 }
258
259 if (*force_nl) { /* if we should insert a nl here, put it into
260 * the buffer */
261 *force_nl = false;
262 --line_no; /* this will be re-increased when the newline
263 * is read from the buffer */
264 *sc_end++ = '\n';
265 *sc_end++ = ' ';
266 if (opt.verbose) /* warn if the line was not already broken */
267 diag(0, "Line broken");
268 }
269
270 for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
271 *sc_end++ = *t_ptr;
272 return true;
273 }
274
275 static void
276 switch_buffer(void)
277 {
278 ps.search_brace = false; /* stop looking for start of stmt */
279 saved_inp_s = inp.s; /* save current input buffer */
280 saved_inp_e = inp.e;
281 inp.s = save_com; /* fix so that subsequent calls to lexi will
282 * take tokens out of save_com */
283 *sc_end++ = ' '; /* add trailing blank, just in case */
284 inp.e = sc_end;
285 sc_end = NULL;
286 debug_println("switched inp.s to save_com");
287 }
288
289 static void
290 search_brace_lookahead(token_type *ttype)
291 {
292 if (*ttype == end_of_file)
293 return;
294
295 /*
296 * The only intended purpose of calling lexi() below is to categorize
297 * the next token in order to decide whether to continue buffering
298 * forthcoming tokens. Once the buffering is over, lexi() will be
299 * called again elsewhere on all of the tokens - this time for normal
300 * processing.
301 *
302 * Calling it for this purpose is a bug, because lexi() also changes
303 * the parser state and discards leading whitespace, which is needed
304 * mostly for comment-related considerations.
305 *
306 * Work around the former problem by giving lexi() a copy of the
307 * current parser state and discard it if the call turned out to be
308 * just a lookahead.
309 *
310 * Work around the latter problem by copying all whitespace characters
311 * into the buffer so that the later lexi() call will read them.
312 */
313 if (sc_end != NULL) {
314 while (is_hspace(*inp.s)) {
315 *sc_end++ = *inp.s++;
316 if (sc_end >= &save_com[sc_size])
317 errx(1, "input too long");
318 }
319 if (inp.s >= inp.e)
320 fill_buffer();
321 }
322
323 struct parser_state transient_state;
324 transient_state = ps;
325 *ttype = lexi(&transient_state); /* read another token */
326 if (*ttype != newline && *ttype != form_feed &&
327 *ttype != comment && !transient_state.search_brace) {
328 ps = transient_state;
329 }
330 }
331
332 static void
333 search_brace(token_type *ttype, bool *force_nl,
334 bool *comment_buffered, bool *last_else)
335 {
336 while (ps.search_brace) {
337 switch (*ttype) {
338 case newline:
339 search_brace_newline(force_nl);
340 break;
341 case form_feed:
342 break;
343 case comment:
344 search_brace_comment(comment_buffered);
345 break;
346 case lbrace:
347 if (search_brace_lbrace())
348 goto switch_buffer;
349 /* FALLTHROUGH */
350 default: /* it is the start of a normal statement */
351 if (!search_brace_other(*ttype, force_nl,
352 *comment_buffered, *last_else))
353 return;
354 switch_buffer:
355 switch_buffer();
356 }
357 search_brace_lookahead(ttype);
358 }
359
360 *last_else = false;
361 }
362
363 static void
364 buf_init(struct buffer *buf)
365 {
366 size_t size = 200;
367 buf->buf = xmalloc(size);
368 buf->buf[0] = ' '; /* allow accessing buf->e[-1] */
369 buf->buf[1] = '\0';
370 buf->s = buf->buf + 1;
371 buf->e = buf->s;
372 buf->l = buf->buf + size - 5; /* safety margin */
373 }
374
375 static size_t
376 buf_len(const struct buffer *buf)
377 {
378 return (size_t)(buf->e - buf->s);
379 }
380
381 void
382 buf_expand(struct buffer *buf, size_t desired_size)
383 {
384 size_t nsize = (size_t)(buf->l - buf->s) + 400 + desired_size;
385 size_t len = buf_len(buf);
386 buf->buf = xrealloc(buf->buf, nsize);
387 buf->e = buf->buf + len + 1;
388 buf->l = buf->buf + nsize - 5;
389 buf->s = buf->buf + 1;
390 }
391
392 static void
393 buf_reserve(struct buffer *buf, size_t n)
394 {
395 if (buf->e + n >= buf->l)
396 buf_expand(buf, n);
397 }
398
399 static void
400 buf_add_char(struct buffer *buf, char ch)
401 {
402 buf_reserve(buf, 1);
403 *buf->e++ = ch;
404 }
405
406 static void
407 buf_add_buf(struct buffer *buf, const struct buffer *add)
408 {
409 size_t len = buf_len(add);
410 buf_reserve(buf, len);
411 memcpy(buf->e, add->s, len);
412 buf->e += len;
413 }
414
415 static void
416 buf_terminate(struct buffer *buf)
417 {
418 buf_reserve(buf, 1);
419 *buf->e = '\0';
420 }
421
422 static void
423 buf_reset(struct buffer *buf)
424 {
425 buf->e = buf->s;
426 }
427
428 static void
429 main_init_globals(void)
430 {
431 found_err = false;
432
433 ps.p_stack[0] = stmt;
434 ps.last_nl = true;
435 ps.last_token = semicolon;
436 buf_init(&com);
437 buf_init(&lab);
438 buf_init(&code);
439 buf_init(&token);
440
441 opt.else_if = true; /* XXX: redundant? */
442
443 inp.buf = xmalloc(10);
444 inp.l = inp.buf + 8;
445 inp.s = inp.e = inp.buf;
446 line_no = 1;
447 had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
448
449 ps.in_or_st = false;
450 ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
451 ps.is_case_label = false;
452
453 sc_end = NULL;
454 saved_inp_s = NULL;
455 saved_inp_e = NULL;
456
457 output = NULL;
458
459 const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
460 if (suffix != NULL)
461 backup_suffix = suffix;
462 }
463
464 static void
465 main_parse_command_line(int argc, char **argv)
466 {
467 int i;
468 const char *profile_name = NULL;
469
470 for (i = 1; i < argc; ++i)
471 if (strcmp(argv[i], "-npro") == 0)
472 break;
473 else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
474 profile_name = argv[i] + 2; /* non-empty -P (set profile) */
475 if (i >= argc)
476 load_profiles(profile_name);
477
478 for (i = 1; i < argc; ++i) {
479 if (argv[i][0] == '-') {
480 set_option(argv[i], "Command line");
481
482 } else if (input == NULL) {
483 in_name = argv[i];
484 input = fopen(in_name, "r");
485 if (input == NULL)
486 err(1, "%s", in_name);
487
488 } else if (output == NULL) {
489 out_name = argv[i];
490 if (strcmp(in_name, out_name) == 0)
491 errx(1, "input and output files must be different");
492 output = fopen(out_name, "w");
493 if (output == NULL)
494 err(1, "%s", out_name);
495
496 } else
497 errx(1, "unknown parameter: %s", argv[i]);
498 }
499
500 if (input == NULL)
501 input = stdin;
502 if (output == NULL) {
503 if (input == stdin)
504 output = stdout;
505 else {
506 out_name = in_name;
507 bakcopy();
508 }
509 }
510
511 if (opt.comment_column <= 1)
512 opt.comment_column = 2; /* don't put normal comments before column 2 */
513 if (opt.block_comment_max_line_length <= 0)
514 opt.block_comment_max_line_length = opt.max_line_length;
515 if (opt.local_decl_indent < 0) /* if not specified by user, set this */
516 opt.local_decl_indent = opt.decl_indent;
517 if (opt.decl_comment_column <= 0) /* if not specified by user, set this */
518 opt.decl_comment_column = opt.ljust_decl
519 ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
520 : opt.comment_column;
521 if (opt.continuation_indent == 0)
522 opt.continuation_indent = opt.indent_size;
523 if (!(1 <= opt.tabsize && opt.tabsize <= 80))
524 errx(EXIT_FAILURE, "invalid tabsize %d", opt.tabsize);
525 if (!(1 <= opt.indent_size && opt.indent_size <= 80))
526 errx(EXIT_FAILURE, "invalid indentation %d", opt.indent_size);
527 }
528
529 static void
530 main_prepare_parsing(void)
531 {
532 fill_buffer(); /* get first batch of stuff into input buffer */
533
534 parse(semicolon);
535
536 char *p = inp.s;
537 int ind = 0;
538
539 for (;;) {
540 if (*p == ' ')
541 ind++;
542 else if (*p == '\t')
543 ind = opt.tabsize * (1 + ind / opt.tabsize);
544 else
545 break;
546 p++;
547 }
548 if (ind >= opt.indent_size)
549 ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
550 }
551
552 static void __attribute__((__noreturn__))
553 process_end_of_file(void)
554 {
555 if (lab.s != lab.e || code.s != code.e || com.s != com.e)
556 dump_line();
557
558 if (ps.tos > 1) /* check for balanced braces */
559 diag(1, "Stuff missing from end of file");
560
561 if (opt.verbose) {
562 printf("There were %d output lines and %d comments\n",
563 ps.stats.lines, ps.stats.comments);
564 printf("(Lines with comments)/(Lines with code): %6.3f\n",
565 (1.0 * ps.stats.comment_lines) / ps.stats.code_lines);
566 }
567
568 fflush(output);
569 exit(found_err ? EXIT_FAILURE : EXIT_SUCCESS);
570 }
571
572 static void
573 process_comment_in_code(token_type ttype, bool *force_nl)
574 {
575 if (*force_nl &&
576 ttype != semicolon &&
577 (ttype != lbrace || !opt.brace_same_line)) {
578
579 /* we should force a broken line here */
580 if (opt.verbose)
581 diag(0, "Line broken");
582 dump_line();
583 ps.want_blank = false; /* don't insert blank at line start */
584 *force_nl = false;
585 }
586
587 /* add an extra level of indentation; turned off again by a ';' or '}' */
588 ps.in_stmt = true;
589
590 if (com.s != com.e) { /* a comment embedded in a line */
591 buf_add_char(&code, ' ');
592 buf_add_buf(&code, &com);
593 buf_add_char(&code, ' ');
594 buf_terminate(&code);
595 buf_reset(&com);
596 ps.want_blank = false;
597 }
598 }
599
600 static void
601 process_form_feed(void)
602 {
603 ps.use_ff = true;
604 dump_line();
605 ps.want_blank = false;
606 }
607
608 static void
609 process_newline(void)
610 {
611 if (ps.last_token != comma || ps.p_l_follow > 0 || opt.break_after_comma
612 || ps.block_init || !break_comma || com.s != com.e) {
613 dump_line();
614 ps.want_blank = false;
615 }
616 ++line_no;
617 }
618
619 static bool
620 want_blank_before_lparen(void)
621 {
622 if (!ps.want_blank)
623 return false;
624 if (ps.last_token == rparen_or_rbracket)
625 return false;
626 if (ps.last_token != ident && ps.last_token != funcname)
627 return true;
628 if (opt.proc_calls_space)
629 return true;
630 if (ps.keyword == kw_sizeof)
631 return opt.blank_after_sizeof;
632 return ps.keyword != kw_0 && ps.keyword != kw_offsetof;
633 }
634
635 static void
636 process_lparen_or_lbracket(int decl_ind, bool tabs_to_var, bool sp_sw)
637 {
638 if (++ps.p_l_follow == nitems(ps.paren_indents)) {
639 diag(0, "Reached internal limit of %zu unclosed parens",
640 nitems(ps.paren_indents));
641 ps.p_l_follow--;
642 }
643
644 if (token.s[0] == '(' && ps.in_decl
645 && !ps.block_init && !ps.dumped_decl_indent &&
646 ps.procname[0] == '\0' && ps.paren_level == 0) {
647 /* function pointer declarations */
648 indent_declaration(decl_ind, tabs_to_var);
649 ps.dumped_decl_indent = true;
650 } else if (want_blank_before_lparen())
651 *code.e++ = ' ';
652 ps.want_blank = false;
653 *code.e++ = token.s[0];
654
655 ps.paren_indents[ps.p_l_follow - 1] =
656 (short)indentation_after_range(0, code.s, code.e);
657 debug_println("paren_indent[%d] is now %d",
658 ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
659
660 if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
661 && ps.paren_indents[0] < 2 * opt.indent_size) {
662 ps.paren_indents[0] = (short)(2 * opt.indent_size);
663 debug_println("paren_indent[0] is now %d", ps.paren_indents[0]);
664 }
665
666 if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) {
667 /*
668 * this is a kluge to make sure that declarations will be aligned
669 * right if proc decl has an explicit type on it, i.e. "int a(x) {..."
670 */
671 parse(semicolon); /* I said this was a kluge... */
672 ps.in_or_st = false; /* turn off flag for structure decl or
673 * initialization */
674 }
675
676 /* parenthesized type following sizeof or offsetof is not a cast */
677 if (ps.keyword == kw_offsetof || ps.keyword == kw_sizeof)
678 ps.not_cast_mask |= 1 << ps.p_l_follow;
679 }
680
681 static void
682 process_rparen_or_rbracket(bool *sp_sw, bool *force_nl,
683 token_type hd_type)
684 {
685 if ((ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) != 0) {
686 ps.last_u_d = true;
687 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
688 ps.want_blank = opt.space_after_cast;
689 } else
690 ps.want_blank = true;
691 ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
692
693 if (--ps.p_l_follow < 0) {
694 ps.p_l_follow = 0;
695 diag(0, "Extra %c", *token.s);
696 }
697
698 if (code.e == code.s) /* if the paren starts the line */
699 ps.paren_level = ps.p_l_follow; /* then indent it */
700
701 *code.e++ = token.s[0];
702
703 if (*sp_sw && (ps.p_l_follow == 0)) { /* check for end of if (...),
704 * or some such */
705 *sp_sw = false;
706 *force_nl = true; /* must force newline after if */
707 ps.last_u_d = true; /* inform lexi that a following operator is
708 * unary */
709 ps.in_stmt = false; /* don't use stmt continuation indentation */
710
711 parse(hd_type); /* let parser worry about if, or whatever */
712 }
713
714 /*
715 * This should ensure that constructs such as main(){...} and int[]{...}
716 * have their braces put in the right place.
717 */
718 ps.search_brace = opt.brace_same_line;
719 }
720
721 static void
722 process_unary_op(int decl_ind, bool tabs_to_var)
723 {
724 if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
725 ps.procname[0] == '\0' && ps.paren_level == 0) {
726 /* pointer declarations */
727 indent_declaration(decl_ind - (int)strlen(token.s), tabs_to_var);
728 ps.dumped_decl_indent = true;
729 } else if (ps.want_blank)
730 *code.e++ = ' ';
731
732 buf_add_buf(&code, &token);
733 ps.want_blank = false;
734 }
735
736 static void
737 process_binary_op(void)
738 {
739 if (ps.want_blank)
740 buf_add_char(&code, ' ');
741 buf_add_buf(&code, &token);
742 ps.want_blank = true;
743 }
744
745 static void
746 process_postfix_op(void)
747 {
748 *code.e++ = token.s[0];
749 *code.e++ = token.s[1];
750 ps.want_blank = true;
751 }
752
753 static void
754 process_question(int *seen_quest)
755 {
756 (*seen_quest)++; /* this will be used when a later colon
757 * appears, so we can distinguish the
758 * <c>?<n>:<n> construct */
759 if (ps.want_blank)
760 *code.e++ = ' ';
761 *code.e++ = '?';
762 ps.want_blank = true;
763 }
764
765 static void
766 process_colon(int *seen_quest, bool *force_nl, bool *seen_case)
767 {
768 if (*seen_quest > 0) { /* part of a '?:' operator */
769 --*seen_quest;
770 if (ps.want_blank)
771 *code.e++ = ' ';
772 *code.e++ = ':';
773 ps.want_blank = true;
774 return;
775 }
776
777 if (ps.in_or_st) { /* bit-field */
778 *code.e++ = ':';
779 ps.want_blank = false;
780 return;
781 }
782
783 buf_add_buf(&lab, &code); /* 'case' or 'default' or named label */
784 buf_add_char(&lab, ':');
785 buf_terminate(&lab);
786 buf_reset(&code);
787
788 ps.in_stmt = false;
789 ps.is_case_label = *seen_case;
790 *force_nl = *seen_case;
791 *seen_case = false;
792 ps.want_blank = false;
793 }
794
795 static void
796 process_semicolon(bool *seen_case, int *seen_quest, int decl_ind,
797 bool tabs_to_var, bool *sp_sw,
798 token_type hd_type,
799 bool *force_nl)
800 {
801 if (ps.decl_nest == 0)
802 ps.in_or_st = false; /* we are not in an initialization or
803 * structure declaration */
804 *seen_case = false; /* these will only need resetting in an error */
805 *seen_quest = 0;
806 if (ps.last_token == rparen_or_rbracket)
807 ps.in_parameter_declaration = false;
808 ps.cast_mask = 0;
809 ps.not_cast_mask = 0;
810 ps.block_init = false;
811 ps.block_init_level = 0;
812 ps.just_saw_decl--;
813
814 if (ps.in_decl && code.s == code.e && !ps.block_init &&
815 !ps.dumped_decl_indent && ps.paren_level == 0) {
816 /* indent stray semicolons in declarations */
817 indent_declaration(decl_ind - 1, tabs_to_var);
818 ps.dumped_decl_indent = true;
819 }
820
821 ps.in_decl = (ps.decl_nest > 0); /* if we were in a first level
822 * structure declaration, we aren't
823 * anymore */
824
825 if ((!*sp_sw || hd_type != for_exprs) && ps.p_l_follow > 0) {
826
827 /*
828 * This should be true iff there were unbalanced parens in the stmt.
829 * It is a bit complicated, because the semicolon might be in a for
830 * stmt
831 */
832 diag(1, "Unbalanced parens");
833 ps.p_l_follow = 0;
834 if (*sp_sw) { /* this is a check for an if, while, etc. with
835 * unbalanced parens */
836 *sp_sw = false;
837 parse(hd_type); /* don't lose the 'if', or whatever */
838 }
839 }
840 *code.e++ = ';';
841 ps.want_blank = true;
842 ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the middle of a
843 * stmt */
844
845 if (!*sp_sw) { /* if not if for (;;) */
846 parse(semicolon); /* let parser know about end of stmt */
847 *force_nl = true; /* force newline after an end of stmt */
848 }
849 }
850
851 static void
852 process_lbrace(bool *force_nl, bool *sp_sw, token_type hd_type,
853 int *di_stack, int di_stack_cap, int *decl_ind)
854 {
855 ps.in_stmt = false; /* don't indent the {} */
856
857 if (!ps.block_init)
858 *force_nl = true; /* force other stuff on same line as '{' onto
859 * new line */
860 else if (ps.block_init_level <= 0)
861 ps.block_init_level = 1;
862 else
863 ps.block_init_level++;
864
865 if (code.s != code.e && !ps.block_init) {
866 if (!opt.brace_same_line) {
867 dump_line();
868 ps.want_blank = false;
869 } else if (ps.in_parameter_declaration && !ps.in_or_st) {
870 ps.ind_level_follow = 0;
871 if (opt.function_brace_split) { /* dump the line prior to the
872 * brace ... */
873 dump_line();
874 ps.want_blank = false;
875 } else /* add a space between the decl and brace */
876 ps.want_blank = true;
877 }
878 }
879
880 if (ps.in_parameter_declaration)
881 prefix_blankline_requested = false;
882
883 if (ps.p_l_follow > 0) { /* check for preceding unbalanced parens */
884 diag(1, "Unbalanced parens");
885 ps.p_l_follow = 0;
886 if (*sp_sw) { /* check for unclosed if, for, etc. */
887 *sp_sw = false;
888 parse(hd_type);
889 ps.ind_level = ps.ind_level_follow;
890 }
891 }
892
893 if (code.s == code.e)
894 ps.ind_stmt = false; /* don't indent the '{' itself */
895 if (ps.in_decl && ps.in_or_st) { /* this is either a structure
896 * declaration or an init */
897 di_stack[ps.decl_nest] = *decl_ind;
898 if (++ps.decl_nest == di_stack_cap) {
899 diag(0, "Reached internal limit of %d struct levels",
900 di_stack_cap);
901 ps.decl_nest--;
902 }
903 } else {
904 ps.decl_on_line = false; /* we can't be in the middle of a
905 * declaration, so don't do special
906 * indentation of comments */
907 if (opt.blanklines_after_declarations_at_proctop
908 && ps.in_parameter_declaration)
909 postfix_blankline_requested = true;
910 ps.in_parameter_declaration = false;
911 ps.in_decl = false;
912 }
913
914 *decl_ind = 0;
915 parse(lbrace);
916 if (ps.want_blank)
917 *code.e++ = ' ';
918 ps.want_blank = false;
919 *code.e++ = '{';
920 ps.just_saw_decl = 0;
921 }
922
923 static void
924 process_rbrace(bool *sp_sw, int *decl_ind, const int *di_stack)
925 {
926 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
927 * omitted in
928 * declarations */
929 parse(semicolon);
930
931 if (ps.p_l_follow != 0) { /* check for unclosed if, for, else. */
932 diag(1, "Unbalanced parens");
933 ps.p_l_follow = 0;
934 *sp_sw = false;
935 }
936
937 ps.just_saw_decl = 0;
938 ps.block_init_level--;
939
940 if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */
941 if (opt.verbose)
942 diag(0, "Line broken");
943 dump_line();
944 }
945
946 *code.e++ = '}';
947 ps.want_blank = true;
948 ps.in_stmt = ps.ind_stmt = false;
949
950 if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
951 *decl_ind = di_stack[--ps.decl_nest];
952 if (ps.decl_nest == 0 && !ps.in_parameter_declaration)
953 ps.just_saw_decl = 2;
954 ps.in_decl = true;
955 }
956
957 prefix_blankline_requested = false;
958 parse(rbrace); /* let parser know about this */
959 ps.search_brace = opt.cuddle_else
960 && ps.p_stack[ps.tos] == if_expr_stmt
961 && ps.il[ps.tos] >= ps.ind_level;
962
963 if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
964 postfix_blankline_requested = true;
965 }
966
967 static void
968 process_keyword_do(bool *force_nl, bool *last_else)
969 {
970 ps.in_stmt = false;
971
972 if (code.e != code.s) { /* make sure this starts a line */
973 if (opt.verbose)
974 diag(0, "Line broken");
975 dump_line();
976 ps.want_blank = false;
977 }
978
979 *force_nl = true; /* following stuff must go onto new line */
980 *last_else = false;
981 parse(keyword_do);
982 }
983
984 static void
985 process_keyword_else(bool *force_nl, bool *last_else)
986 {
987 ps.in_stmt = false;
988
989 if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) {
990 if (opt.verbose)
991 diag(0, "Line broken");
992 dump_line(); /* make sure this starts a line */
993 ps.want_blank = false;
994 }
995
996 *force_nl = true; /* following stuff must go onto new line */
997 *last_else = true;
998 parse(keyword_else);
999 }
1000
1001 static void
1002 process_decl(int *out_decl_ind, bool *out_tabs_to_var)
1003 {
1004 parse(decl); /* let parser worry about indentation */
1005
1006 if (ps.last_token == rparen_or_rbracket && ps.tos <= 1) {
1007 if (code.s != code.e) {
1008 dump_line();
1009 ps.want_blank = false;
1010 }
1011 }
1012
1013 if (ps.in_parameter_declaration && opt.indent_parameters &&
1014 ps.decl_nest == 0) {
1015 ps.ind_level = ps.ind_level_follow = 1;
1016 ps.ind_stmt = false;
1017 }
1018
1019 ps.in_or_st = true; /* this might be a structure or initialization
1020 * declaration */
1021 ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
1022 if ( /* !ps.in_or_st && */ ps.decl_nest <= 0)
1023 ps.just_saw_decl = 2;
1024
1025 prefix_blankline_requested = false;
1026
1027 int len = (int)strlen(token.s) + 1;
1028 int ind = ps.ind_level == 0 || ps.decl_nest > 0
1029 ? opt.decl_indent /* global variable or local member */
1030 : opt.local_decl_indent; /* local variable */
1031 *out_decl_ind = ind > 0 ? ind : len;
1032 *out_tabs_to_var = opt.use_tabs ? ind > 0 : false;
1033 }
1034
1035 static void
1036 process_ident(token_type ttype, int decl_ind, bool tabs_to_var,
1037 bool *sp_sw, bool *force_nl, token_type hd_type)
1038 {
1039 if (ps.in_decl) {
1040 if (ttype == funcname) {
1041 ps.in_decl = false;
1042 if (opt.procnames_start_line && code.s != code.e) {
1043 *code.e = '\0';
1044 dump_line();
1045 } else if (ps.want_blank) {
1046 *code.e++ = ' ';
1047 }
1048 ps.want_blank = false;
1049
1050 } else if (!ps.block_init && !ps.dumped_decl_indent &&
1051 ps.paren_level == 0) { /* if we are in a declaration, we must
1052 * indent identifier */
1053 indent_declaration(decl_ind, tabs_to_var);
1054 ps.dumped_decl_indent = true;
1055 ps.want_blank = false;
1056 }
1057
1058 } else if (*sp_sw && ps.p_l_follow == 0) {
1059 *sp_sw = false;
1060 *force_nl = true;
1061 ps.last_u_d = true;
1062 ps.in_stmt = false;
1063 parse(hd_type);
1064 }
1065 }
1066
1067 static void
1068 copy_token(void)
1069 {
1070 if (ps.want_blank)
1071 buf_add_char(&code, ' ');
1072 buf_add_buf(&code, &token);
1073 }
1074
1075 static void
1076 process_string_prefix(void)
1077 {
1078 copy_token();
1079 ps.want_blank = false;
1080 }
1081
1082 static void
1083 process_period(void)
1084 {
1085 if (code.e[-1] == ',')
1086 *code.e++ = ' ';
1087 *code.e++ = '.';
1088 ps.want_blank = false;
1089 }
1090
1091 static void
1092 process_comma(int decl_ind, bool tabs_to_var, bool *force_nl)
1093 {
1094 ps.want_blank = (code.s != code.e); /* only put blank after comma if comma
1095 * does not start the line */
1096
1097 if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
1098 !ps.dumped_decl_indent && ps.paren_level == 0) {
1099 /* indent leading commas and not the actual identifiers */
1100 indent_declaration(decl_ind - 1, tabs_to_var);
1101 ps.dumped_decl_indent = true;
1102 }
1103
1104 *code.e++ = ',';
1105
1106 if (ps.p_l_follow == 0) {
1107 if (ps.block_init_level <= 0)
1108 ps.block_init = false;
1109 if (break_comma && (opt.break_after_comma ||
1110 indentation_after_range(
1111 compute_code_indent(), code.s, code.e)
1112 >= opt.max_line_length - opt.tabsize))
1113 *force_nl = true;
1114 }
1115 }
1116
1117 /* move the whole line to the 'label' buffer */
1118 static void
1119 read_preprocessing_line(void)
1120 {
1121 buf_add_char(&lab, '#');
1122
1123 bool in_comment = false;
1124 int com_start = 0, com_end = 0;
1125 char quote = '\0';
1126
1127 while (is_hspace(*inp.s))
1128 inbuf_skip();
1129
1130 while (*inp.s != '\n' || (in_comment && !had_eof)) {
1131 buf_reserve(&lab, 2);
1132 *lab.e++ = inbuf_next();
1133 switch (lab.e[-1]) {
1134 case '\\':
1135 if (!in_comment)
1136 *lab.e++ = inbuf_next();
1137 break;
1138 case '/':
1139 if (*inp.s == '*' && !in_comment && quote == '\0') {
1140 in_comment = true;
1141 *lab.e++ = *inp.s++;
1142 com_start = (int)buf_len(&lab) - 2;
1143 }
1144 break;
1145 case '"':
1146 if (quote == '"')
1147 quote = '\0';
1148 else if (quote == '\0')
1149 quote = '"';
1150 break;
1151 case '\'':
1152 if (quote == '\'')
1153 quote = '\0';
1154 else if (quote == '\0')
1155 quote = '\'';
1156 break;
1157 case '*':
1158 if (*inp.s == '/' && in_comment) {
1159 in_comment = false;
1160 *lab.e++ = *inp.s++;
1161 com_end = (int)buf_len(&lab);
1162 }
1163 break;
1164 }
1165 }
1166
1167 while (lab.e > lab.s && is_hspace(lab.e[-1]))
1168 lab.e--;
1169 if (lab.e - lab.s == com_end && saved_inp_s == NULL) {
1170 /* comment on preprocessor line */
1171 if (sc_end == NULL) { /* if this is the first comment, we must set
1172 * up the buffer */
1173 save_com = sc_buf;
1174 sc_end = save_com;
1175 } else {
1176 *sc_end++ = '\n'; /* add newline between comments */
1177 *sc_end++ = ' ';
1178 --line_no;
1179 }
1180 if (sc_end - save_com + com_end - com_start > sc_size)
1181 errx(1, "input too long");
1182 memmove(sc_end, lab.s + com_start, (size_t)(com_end - com_start));
1183 sc_end += com_end - com_start;
1184 lab.e = lab.s + com_start;
1185 while (lab.e > lab.s && is_hspace(lab.e[-1]))
1186 lab.e--;
1187 saved_inp_s = inp.s; /* save current input buffer */
1188 saved_inp_e = inp.e;
1189 inp.s = save_com; /* fix so that subsequent calls to lexi will
1190 * take tokens out of save_com */
1191 *sc_end++ = ' '; /* add trailing blank, just in case */
1192 inp.e = sc_end;
1193 sc_end = NULL;
1194 debug_println("switched inp.s to save_com");
1195 }
1196 buf_terminate(&lab);
1197 }
1198
1199 static void
1200 process_preprocessing(void)
1201 {
1202 if (com.s != com.e || lab.s != lab.e || code.s != code.e)
1203 dump_line();
1204
1205 read_preprocessing_line();
1206
1207 ps.is_case_label = false;
1208
1209 if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */
1210 if ((size_t)ifdef_level < nitems(state_stack))
1211 state_stack[ifdef_level++] = ps;
1212 else
1213 diag(1, "#if stack overflow");
1214
1215 } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */
1216 if (ifdef_level <= 0)
1217 diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1218 else
1219 ps = state_stack[ifdef_level - 1];
1220
1221 } else if (strncmp(lab.s, "#endif", 6) == 0) {
1222 if (ifdef_level <= 0)
1223 diag(1, "Unmatched #endif");
1224 else
1225 ifdef_level--;
1226
1227 } else {
1228 if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
1229 strncmp(lab.s + 1, "error", 5) != 0 &&
1230 strncmp(lab.s + 1, "line", 4) != 0 &&
1231 strncmp(lab.s + 1, "undef", 5) != 0 &&
1232 strncmp(lab.s + 1, "define", 6) != 0 &&
1233 strncmp(lab.s + 1, "include", 7) != 0) {
1234 diag(1, "Unrecognized cpp directive");
1235 return;
1236 }
1237 }
1238
1239 if (opt.blanklines_around_conditional_compilation) {
1240 postfix_blankline_requested = true;
1241 next_blank_lines = 0;
1242 } else {
1243 postfix_blankline_requested = false;
1244 prefix_blankline_requested = false;
1245 }
1246
1247 /*
1248 * subsequent processing of the newline character will cause the line to
1249 * be printed
1250 */
1251 }
1252
1253 static void __attribute__((__noreturn__))
1254 main_loop(void)
1255 {
1256 token_type ttype;
1257 bool force_nl; /* when true, code must be broken */
1258 bool last_else = false; /* true iff last keyword was an else */
1259 int decl_ind; /* current indentation for declarations */
1260 int di_stack[20]; /* a stack of structure indentation levels */
1261 bool tabs_to_var; /* true if using tabs to indent to var name */
1262 bool sp_sw; /* when true, we are in the expression of
1263 * if(...), while(...), etc. */
1264 token_type hd_type = end_of_file; /* used to store type of stmt for if
1265 * (...), for (...), etc */
1266 int seen_quest; /* when this is positive, we have seen a '?'
1267 * without the matching ':' in a <c>?<s>:<s>
1268 * construct */
1269 bool seen_case; /* set to true when we see a 'case', so we
1270 * know what to do with the following colon */
1271
1272 sp_sw = force_nl = false;
1273 decl_ind = 0;
1274 di_stack[ps.decl_nest = 0] = 0;
1275 seen_case = false;
1276 seen_quest = 0;
1277 tabs_to_var = false;
1278
1279 for (;;) { /* this is the main loop. it will go until we
1280 * reach eof */
1281 bool comment_buffered = false;
1282
1283 ttype = lexi(&ps); /* Read the next token. The actual characters
1284 * read are stored in "token". */
1285
1286 /*
1287 * Move newlines and comments following an if (), while (), else, etc.
1288 * up to the start of the following stmt to a buffer. This allows
1289 * proper handling of both kinds of brace placement (-br, -bl) and
1290 * cuddling "else" (-ce).
1291 */
1292 search_brace(&ttype, &force_nl, &comment_buffered, &last_else);
1293
1294 if (ttype == end_of_file) {
1295 process_end_of_file();
1296 /* NOTREACHED */
1297 }
1298
1299 if (
1300 ttype != comment &&
1301 ttype != newline &&
1302 ttype != preprocessing &&
1303 ttype != form_feed) {
1304 process_comment_in_code(ttype, &force_nl);
1305
1306 } else if (ttype != comment) /* preserve force_nl through a comment */
1307 force_nl = false; /* cancel forced newline after newline, form
1308 * feed, etc */
1309
1310 buf_reserve(&code, 3); /* space for 2 characters plus '\0' */
1311
1312 switch (ttype) {
1313
1314 case form_feed:
1315 process_form_feed();
1316 break;
1317
1318 case newline:
1319 process_newline();
1320 break;
1321
1322 case lparen_or_lbracket:
1323 process_lparen_or_lbracket(decl_ind, tabs_to_var, sp_sw);
1324 break;
1325
1326 case rparen_or_rbracket:
1327 process_rparen_or_rbracket(&sp_sw, &force_nl, hd_type);
1328 break;
1329
1330 case unary_op:
1331 process_unary_op(decl_ind, tabs_to_var);
1332 break;
1333
1334 case binary_op:
1335 process_binary_op();
1336 break;
1337
1338 case postfix_op:
1339 process_postfix_op();
1340 break;
1341
1342 case question:
1343 process_question(&seen_quest);
1344 break;
1345
1346 case case_label: /* got word 'case' or 'default' */
1347 seen_case = true;
1348 goto copy_token;
1349
1350 case colon:
1351 process_colon(&seen_quest, &force_nl, &seen_case);
1352 break;
1353
1354 case semicolon:
1355 process_semicolon(&seen_case, &seen_quest, decl_ind, tabs_to_var,
1356 &sp_sw, hd_type, &force_nl);
1357 break;
1358
1359 case lbrace:
1360 process_lbrace(&force_nl, &sp_sw, hd_type, di_stack,
1361 (int)nitems(di_stack), &decl_ind);
1362 break;
1363
1364 case rbrace:
1365 process_rbrace(&sp_sw, &decl_ind, di_stack);
1366 break;
1367
1368 case switch_expr: /* got keyword "switch" */
1369 sp_sw = true;
1370 hd_type = switch_expr; /* keep this for when we have seen the
1371 * expression */
1372 goto copy_token;
1373
1374 case keyword_for_if_while:
1375 sp_sw = true; /* the interesting stuff is done after the
1376 * expression is scanned */
1377 hd_type = (*token.s == 'i' ? if_expr :
1378 (*token.s == 'w' ? while_expr : for_exprs));
1379
1380 /* remember the type of header for later use by parser */
1381 goto copy_token;
1382
1383 case keyword_do_else:
1384 if (*token.s == 'd')
1385 process_keyword_do(&force_nl, &last_else);
1386 else
1387 process_keyword_else(&force_nl, &last_else);
1388 goto copy_token;
1389
1390 case type_def:
1391 case storage_class:
1392 prefix_blankline_requested = false;
1393 goto copy_token;
1394
1395 case keyword_struct_union_enum:
1396 if (ps.p_l_follow > 0)
1397 goto copy_token;
1398 /* FALLTHROUGH */
1399 case decl: /* a declaration type (int, etc.) */
1400 process_decl(&decl_ind, &tabs_to_var);
1401 goto copy_token;
1402
1403 case funcname:
1404 case ident: /* an identifier, constant or string */
1405 process_ident(ttype, decl_ind, tabs_to_var, &sp_sw, &force_nl,
1406 hd_type);
1407 copy_token:
1408 copy_token();
1409 if (ttype != funcname)
1410 ps.want_blank = true;
1411 break;
1412
1413 case string_prefix:
1414 process_string_prefix();
1415 break;
1416
1417 case period:
1418 process_period();
1419 break;
1420
1421 case comma:
1422 process_comma(decl_ind, tabs_to_var, &force_nl);
1423 break;
1424
1425 case preprocessing: /* the initial '#' */
1426 process_preprocessing();
1427 break;
1428
1429 case comment: /* the initial '/' '*' or '//' of a comment */
1430 process_comment();
1431 break;
1432
1433 default:
1434 break;
1435 }
1436
1437 *code.e = '\0';
1438 if (ttype != comment && ttype != newline && ttype != preprocessing)
1439 ps.last_token = ttype;
1440 }
1441 }
1442
1443 int
1444 main(int argc, char **argv)
1445 {
1446 main_init_globals();
1447 main_parse_command_line(argc, argv);
1448 #if HAVE_CAPSICUM
1449 init_capsicum();
1450 #endif
1451 main_prepare_parsing();
1452 main_loop();
1453 }
1454
1455 /*
1456 * Copy the input file to the backup file, then make the backup file the input
1457 * and the original input file the output.
1458 */
1459 static void
1460 bakcopy(void)
1461 {
1462 ssize_t n;
1463 int bak_fd;
1464 char buff[8 * 1024];
1465
1466 const char *last_slash = strrchr(in_name, '/');
1467 snprintf(bakfile, sizeof(bakfile), "%s%s",
1468 last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
1469
1470 /* copy in_name to backup file */
1471 bak_fd = creat(bakfile, 0600);
1472 if (bak_fd < 0)
1473 err(1, "%s", bakfile);
1474
1475 while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
1476 if (write(bak_fd, buff, (size_t)n) != n)
1477 err(1, "%s", bakfile);
1478 if (n < 0)
1479 err(1, "%s", in_name);
1480
1481 close(bak_fd);
1482 (void)fclose(input);
1483
1484 /* re-open backup file as the input file */
1485 input = fopen(bakfile, "r");
1486 if (input == NULL)
1487 err(1, "%s", bakfile);
1488 /* now the original input file will be the output */
1489 output = fopen(in_name, "w");
1490 if (output == NULL) {
1491 unlink(bakfile);
1492 err(1, "%s", in_name);
1493 }
1494 }
1495
1496 static void
1497 indent_declaration(int cur_decl_ind, bool tabs_to_var)
1498 {
1499 int pos = (int)(code.e - code.s);
1500 char *startpos = code.e;
1501
1502 /*
1503 * get the tab math right for indentations that are not multiples of
1504 * tabsize
1505 */
1506 if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
1507 pos += (ps.ind_level * opt.indent_size) % opt.tabsize;
1508 cur_decl_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
1509 }
1510
1511 if (tabs_to_var) {
1512 int tpos;
1513
1514 while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_decl_ind) {
1515 buf_add_char(&code, '\t');
1516 pos = tpos;
1517 }
1518 }
1519
1520 while (pos < cur_decl_ind) {
1521 buf_add_char(&code, ' ');
1522 pos++;
1523 }
1524
1525 if (code.e == startpos && ps.want_blank) {
1526 *code.e++ = ' ';
1527 ps.want_blank = false;
1528 }
1529 }
1530
1531 #ifdef debug
1532 void
1533 debug_printf(const char *fmt, ...)
1534 {
1535 FILE *f = output == stdout ? stderr : stdout;
1536 va_list ap;
1537
1538 va_start(ap, fmt);
1539 vfprintf(f, fmt, ap);
1540 va_end(ap);
1541 }
1542
1543 void
1544 debug_println(const char *fmt, ...)
1545 {
1546 FILE *f = output == stdout ? stderr : stdout;
1547 va_list ap;
1548
1549 va_start(ap, fmt);
1550 vfprintf(f, fmt, ap);
1551 va_end(ap);
1552 fprintf(f, "\n");
1553 }
1554
1555 void
1556 debug_vis_range(const char *prefix, const char *s, const char *e,
1557 const char *suffix)
1558 {
1559 debug_printf("%s", prefix);
1560 for (const char *p = s; p < e; p++) {
1561 if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
1562 debug_printf("%c", *p);
1563 else if (*p == '\n')
1564 debug_printf("\\n");
1565 else if (*p == '\t')
1566 debug_printf("\\t");
1567 else
1568 debug_printf("\\x%02x", *p);
1569 }
1570 debug_printf("%s", suffix);
1571 }
1572 #endif
1573
1574 static void *
1575 nonnull(void *p)
1576 {
1577 if (p == NULL)
1578 err(EXIT_FAILURE, NULL);
1579 return p;
1580 }
1581
1582 void *
1583 xmalloc(size_t size)
1584 {
1585 return nonnull(malloc(size));
1586 }
1587
1588 void *
1589 xrealloc(void *p, size_t new_size)
1590 {
1591 return nonnull(realloc(p, new_size));
1592 }
1593
1594 char *
1595 xstrdup(const char *s)
1596 {
1597 return nonnull(strdup(s));
1598 }
1599