indent.c revision 1.129 1 /* $NetBSD: indent.c,v 1.129 2021/10/08 20:14:52 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.129 2021/10/08 20:14:52 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_expr_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_decl_at_top && ps.in_parameter_declaration)
908 postfix_blankline_requested = true;
909 ps.in_parameter_declaration = false;
910 ps.in_decl = false;
911 }
912
913 *decl_ind = 0;
914 parse(lbrace);
915 if (ps.want_blank)
916 *code.e++ = ' ';
917 ps.want_blank = false;
918 *code.e++ = '{';
919 ps.just_saw_decl = 0;
920 }
921
922 static void
923 process_rbrace(bool *sp_sw, int *decl_ind, const int *di_stack)
924 {
925 if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
926 * omitted in
927 * declarations */
928 parse(semicolon);
929
930 if (ps.p_l_follow != 0) { /* check for unclosed if, for, else. */
931 diag(1, "Unbalanced parens");
932 ps.p_l_follow = 0;
933 *sp_sw = false;
934 }
935
936 ps.just_saw_decl = 0;
937 ps.block_init_level--;
938
939 if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */
940 if (opt.verbose)
941 diag(0, "Line broken");
942 dump_line();
943 }
944
945 *code.e++ = '}';
946 ps.want_blank = true;
947 ps.in_stmt = ps.ind_stmt = false;
948
949 if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
950 *decl_ind = di_stack[--ps.decl_nest];
951 if (ps.decl_nest == 0 && !ps.in_parameter_declaration)
952 ps.just_saw_decl = 2;
953 ps.in_decl = true;
954 }
955
956 prefix_blankline_requested = false;
957 parse(rbrace); /* let parser know about this */
958 ps.search_brace = opt.cuddle_else
959 && ps.p_stack[ps.tos] == if_expr_stmt
960 && ps.il[ps.tos] >= ps.ind_level;
961
962 if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
963 postfix_blankline_requested = true;
964 }
965
966 static void
967 process_keyword_do(bool *force_nl, bool *last_else)
968 {
969 ps.in_stmt = false;
970
971 if (code.e != code.s) { /* make sure this starts a line */
972 if (opt.verbose)
973 diag(0, "Line broken");
974 dump_line();
975 ps.want_blank = false;
976 }
977
978 *force_nl = true; /* following stuff must go onto new line */
979 *last_else = false;
980 parse(keyword_do);
981 }
982
983 static void
984 process_keyword_else(bool *force_nl, bool *last_else)
985 {
986 ps.in_stmt = false;
987
988 if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) {
989 if (opt.verbose)
990 diag(0, "Line broken");
991 dump_line(); /* make sure this starts a line */
992 ps.want_blank = false;
993 }
994
995 *force_nl = true; /* following stuff must go onto new line */
996 *last_else = true;
997 parse(keyword_else);
998 }
999
1000 static void
1001 process_decl(int *out_decl_ind, bool *out_tabs_to_var)
1002 {
1003 parse(decl); /* let parser worry about indentation */
1004
1005 if (ps.last_token == rparen_or_rbracket && ps.tos <= 1) {
1006 if (code.s != code.e) {
1007 dump_line();
1008 ps.want_blank = false;
1009 }
1010 }
1011
1012 if (ps.in_parameter_declaration && opt.indent_parameters &&
1013 ps.decl_nest == 0) {
1014 ps.ind_level = ps.ind_level_follow = 1;
1015 ps.ind_stmt = false;
1016 }
1017
1018 ps.in_or_st = true; /* this might be a structure or initialization
1019 * declaration */
1020 ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
1021 if ( /* !ps.in_or_st && */ ps.decl_nest <= 0)
1022 ps.just_saw_decl = 2;
1023
1024 prefix_blankline_requested = false;
1025
1026 int len = (int)strlen(token.s) + 1;
1027 int ind = ps.ind_level == 0 || ps.decl_nest > 0
1028 ? opt.decl_indent /* global variable or local member */
1029 : opt.local_decl_indent; /* local variable */
1030 *out_decl_ind = ind > 0 ? ind : len;
1031 *out_tabs_to_var = opt.use_tabs ? ind > 0 : false;
1032 }
1033
1034 static void
1035 process_ident(token_type ttype, int decl_ind, bool tabs_to_var,
1036 bool *sp_sw, bool *force_nl, token_type hd_type)
1037 {
1038 if (ps.in_decl) {
1039 if (ttype == funcname) {
1040 ps.in_decl = false;
1041 if (opt.procnames_start_line && code.s != code.e) {
1042 *code.e = '\0';
1043 dump_line();
1044 } else if (ps.want_blank) {
1045 *code.e++ = ' ';
1046 }
1047 ps.want_blank = false;
1048
1049 } else if (!ps.block_init && !ps.dumped_decl_indent &&
1050 ps.paren_level == 0) { /* if we are in a declaration, we must
1051 * indent identifier */
1052 indent_declaration(decl_ind, tabs_to_var);
1053 ps.dumped_decl_indent = true;
1054 ps.want_blank = false;
1055 }
1056
1057 } else if (*sp_sw && ps.p_l_follow == 0) {
1058 *sp_sw = false;
1059 *force_nl = true;
1060 ps.last_u_d = true;
1061 ps.in_stmt = false;
1062 parse(hd_type);
1063 }
1064 }
1065
1066 static void
1067 copy_token(void)
1068 {
1069 if (ps.want_blank)
1070 buf_add_char(&code, ' ');
1071 buf_add_buf(&code, &token);
1072 }
1073
1074 static void
1075 process_string_prefix(void)
1076 {
1077 copy_token();
1078 ps.want_blank = false;
1079 }
1080
1081 static void
1082 process_period(void)
1083 {
1084 if (code.e[-1] == ',')
1085 *code.e++ = ' ';
1086 *code.e++ = '.';
1087 ps.want_blank = false;
1088 }
1089
1090 static void
1091 process_comma(int decl_ind, bool tabs_to_var, bool *force_nl)
1092 {
1093 ps.want_blank = (code.s != code.e); /* only put blank after comma if comma
1094 * does not start the line */
1095
1096 if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
1097 !ps.dumped_decl_indent && ps.paren_level == 0) {
1098 /* indent leading commas and not the actual identifiers */
1099 indent_declaration(decl_ind - 1, tabs_to_var);
1100 ps.dumped_decl_indent = true;
1101 }
1102
1103 *code.e++ = ',';
1104
1105 if (ps.p_l_follow == 0) {
1106 if (ps.block_init_level <= 0)
1107 ps.block_init = false;
1108 if (break_comma && (opt.break_after_comma ||
1109 indentation_after_range(
1110 compute_code_indent(), code.s, code.e)
1111 >= opt.max_line_length - opt.tabsize))
1112 *force_nl = true;
1113 }
1114 }
1115
1116 /* move the whole line to the 'label' buffer */
1117 static void
1118 read_preprocessing_line(void)
1119 {
1120 enum {
1121 PLAIN, STR, CHR, COMM
1122 } state;
1123
1124 buf_add_char(&lab, '#');
1125
1126 state = PLAIN;
1127 int com_start = 0, com_end = 0;
1128
1129 while (is_hspace(*inp.s))
1130 inbuf_skip();
1131
1132 while (*inp.s != '\n' || (state == COMM && !had_eof)) {
1133 buf_reserve(&lab, 2);
1134 *lab.e++ = inbuf_next();
1135 switch (lab.e[-1]) {
1136 case '\\':
1137 if (state != COMM)
1138 *lab.e++ = inbuf_next();
1139 break;
1140 case '/':
1141 if (*inp.s == '*' && state == PLAIN) {
1142 state = COMM;
1143 *lab.e++ = *inp.s++;
1144 com_start = (int)buf_len(&lab) - 2;
1145 }
1146 break;
1147 case '"':
1148 if (state == STR)
1149 state = PLAIN;
1150 else if (state == PLAIN)
1151 state = STR;
1152 break;
1153 case '\'':
1154 if (state == CHR)
1155 state = PLAIN;
1156 else if (state == PLAIN)
1157 state = CHR;
1158 break;
1159 case '*':
1160 if (*inp.s == '/' && state == COMM) {
1161 state = PLAIN;
1162 *lab.e++ = *inp.s++;
1163 com_end = (int)buf_len(&lab);
1164 }
1165 break;
1166 }
1167 }
1168
1169 while (lab.e > lab.s && is_hspace(lab.e[-1]))
1170 lab.e--;
1171 if (lab.e - lab.s == com_end && saved_inp_s == NULL) {
1172 /* comment on preprocessor line */
1173 if (sc_end == NULL) { /* if this is the first comment, we must set
1174 * up the buffer */
1175 save_com = sc_buf;
1176 sc_end = save_com;
1177 } else {
1178 *sc_end++ = '\n'; /* add newline between comments */
1179 *sc_end++ = ' ';
1180 --line_no;
1181 }
1182 if (sc_end - save_com + com_end - com_start > sc_size)
1183 errx(1, "input too long");
1184 memmove(sc_end, lab.s + com_start, (size_t)(com_end - com_start));
1185 sc_end += com_end - com_start;
1186 lab.e = lab.s + com_start;
1187 while (lab.e > lab.s && is_hspace(lab.e[-1]))
1188 lab.e--;
1189 saved_inp_s = inp.s; /* save current input buffer */
1190 saved_inp_e = inp.e;
1191 inp.s = save_com; /* fix so that subsequent calls to lexi will
1192 * take tokens out of save_com */
1193 *sc_end++ = ' '; /* add trailing blank, just in case */
1194 inp.e = sc_end;
1195 sc_end = NULL;
1196 debug_println("switched inp.s to save_com");
1197 }
1198 buf_terminate(&lab);
1199 }
1200
1201 static void
1202 process_preprocessing(void)
1203 {
1204 if (com.s != com.e || lab.s != lab.e || code.s != code.e)
1205 dump_line();
1206
1207 read_preprocessing_line();
1208
1209 ps.is_case_label = false;
1210
1211 if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */
1212 if ((size_t)ifdef_level < nitems(state_stack))
1213 state_stack[ifdef_level++] = ps;
1214 else
1215 diag(1, "#if stack overflow");
1216
1217 } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */
1218 if (ifdef_level <= 0)
1219 diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1220 else
1221 ps = state_stack[ifdef_level - 1];
1222
1223 } else if (strncmp(lab.s, "#endif", 6) == 0) {
1224 if (ifdef_level <= 0)
1225 diag(1, "Unmatched #endif");
1226 else
1227 ifdef_level--;
1228
1229 } else {
1230 if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
1231 strncmp(lab.s + 1, "error", 5) != 0 &&
1232 strncmp(lab.s + 1, "line", 4) != 0 &&
1233 strncmp(lab.s + 1, "undef", 5) != 0 &&
1234 strncmp(lab.s + 1, "define", 6) != 0 &&
1235 strncmp(lab.s + 1, "include", 7) != 0) {
1236 diag(1, "Unrecognized cpp directive");
1237 return;
1238 }
1239 }
1240
1241 if (opt.blanklines_around_conditional_compilation) {
1242 postfix_blankline_requested = true;
1243 next_blank_lines = 0;
1244 } else {
1245 postfix_blankline_requested = false;
1246 prefix_blankline_requested = false;
1247 }
1248
1249 /*
1250 * subsequent processing of the newline character will cause the line to
1251 * be printed
1252 */
1253 }
1254
1255 static void __attribute__((__noreturn__))
1256 main_loop(void)
1257 {
1258 token_type ttype;
1259 bool force_nl; /* when true, code must be broken */
1260 bool last_else = false; /* true iff last keyword was an else */
1261 int decl_ind; /* current indentation for declarations */
1262 int di_stack[20]; /* a stack of structure indentation levels */
1263 bool tabs_to_var; /* true if using tabs to indent to var name */
1264 bool sp_sw; /* when true, we are in the expression of
1265 * if(...), while(...), etc. */
1266 token_type hd_type = end_of_file; /* used to store type of stmt for if
1267 * (...), for (...), etc */
1268 int seen_quest; /* when this is positive, we have seen a '?'
1269 * without the matching ':' in a <c>?<s>:<s>
1270 * construct */
1271 bool seen_case; /* set to true when we see a 'case', so we
1272 * know what to do with the following colon */
1273
1274 sp_sw = force_nl = false;
1275 decl_ind = 0;
1276 di_stack[ps.decl_nest = 0] = 0;
1277 seen_case = false;
1278 seen_quest = 0;
1279 tabs_to_var = false;
1280
1281 for (;;) { /* this is the main loop. it will go until we
1282 * reach eof */
1283 bool comment_buffered = false;
1284
1285 ttype = lexi(&ps); /* Read the next token. The actual characters
1286 * read are stored in "token". */
1287
1288 /*
1289 * Move newlines and comments following an if (), while (), else, etc.
1290 * up to the start of the following stmt to a buffer. This allows
1291 * proper handling of both kinds of brace placement (-br, -bl) and
1292 * cuddling "else" (-ce).
1293 */
1294 search_brace(&ttype, &force_nl, &comment_buffered, &last_else);
1295
1296 if (ttype == end_of_file) {
1297 process_end_of_file();
1298 /* NOTREACHED */
1299 }
1300
1301 if (
1302 ttype != comment &&
1303 ttype != newline &&
1304 ttype != preprocessing &&
1305 ttype != form_feed) {
1306 process_comment_in_code(ttype, &force_nl);
1307
1308 } else if (ttype != comment) /* preserve force_nl through a comment */
1309 force_nl = false; /* cancel forced newline after newline, form
1310 * feed, etc */
1311
1312 buf_reserve(&code, 3); /* space for 2 characters plus '\0' */
1313
1314 switch (ttype) {
1315
1316 case form_feed:
1317 process_form_feed();
1318 break;
1319
1320 case newline:
1321 process_newline();
1322 break;
1323
1324 case lparen_or_lbracket:
1325 process_lparen_or_lbracket(decl_ind, tabs_to_var, sp_sw);
1326 break;
1327
1328 case rparen_or_rbracket:
1329 process_rparen_or_rbracket(&sp_sw, &force_nl, hd_type);
1330 break;
1331
1332 case unary_op:
1333 process_unary_op(decl_ind, tabs_to_var);
1334 break;
1335
1336 case binary_op:
1337 process_binary_op();
1338 break;
1339
1340 case postfix_op:
1341 process_postfix_op();
1342 break;
1343
1344 case question:
1345 process_question(&seen_quest);
1346 break;
1347
1348 case case_label: /* got word 'case' or 'default' */
1349 seen_case = true;
1350 goto copy_token;
1351
1352 case colon:
1353 process_colon(&seen_quest, &force_nl, &seen_case);
1354 break;
1355
1356 case semicolon:
1357 process_semicolon(&seen_case, &seen_quest, decl_ind, tabs_to_var,
1358 &sp_sw, hd_type, &force_nl);
1359 break;
1360
1361 case lbrace:
1362 process_lbrace(&force_nl, &sp_sw, hd_type, di_stack,
1363 (int)nitems(di_stack), &decl_ind);
1364 break;
1365
1366 case rbrace:
1367 process_rbrace(&sp_sw, &decl_ind, di_stack);
1368 break;
1369
1370 case switch_expr: /* got keyword "switch" */
1371 sp_sw = true;
1372 hd_type = switch_expr; /* keep this for when we have seen the
1373 * expression */
1374 goto copy_token;
1375
1376 case keyword_for_if_while:
1377 sp_sw = true; /* the interesting stuff is done after the
1378 * expression is scanned */
1379 hd_type = (*token.s == 'i' ? if_expr :
1380 (*token.s == 'w' ? while_expr : for_exprs));
1381
1382 /* remember the type of header for later use by parser */
1383 goto copy_token;
1384
1385 case keyword_do_else:
1386 if (*token.s == 'd')
1387 process_keyword_do(&force_nl, &last_else);
1388 else
1389 process_keyword_else(&force_nl, &last_else);
1390 goto copy_token;
1391
1392 case type_def:
1393 case storage_class:
1394 prefix_blankline_requested = false;
1395 goto copy_token;
1396
1397 case keyword_struct_union_enum:
1398 if (ps.p_l_follow > 0)
1399 goto copy_token;
1400 /* FALLTHROUGH */
1401 case decl: /* a declaration type (int, etc.) */
1402 process_decl(&decl_ind, &tabs_to_var);
1403 goto copy_token;
1404
1405 case funcname:
1406 case ident: /* an identifier, constant or string */
1407 process_ident(ttype, decl_ind, tabs_to_var, &sp_sw, &force_nl,
1408 hd_type);
1409 copy_token:
1410 copy_token();
1411 if (ttype != funcname)
1412 ps.want_blank = true;
1413 break;
1414
1415 case string_prefix:
1416 process_string_prefix();
1417 break;
1418
1419 case period:
1420 process_period();
1421 break;
1422
1423 case comma:
1424 process_comma(decl_ind, tabs_to_var, &force_nl);
1425 break;
1426
1427 case preprocessing: /* the initial '#' */
1428 process_preprocessing();
1429 break;
1430
1431 case comment: /* the initial '/' '*' or '//' of a comment */
1432 process_comment();
1433 break;
1434
1435 default:
1436 break;
1437 }
1438
1439 *code.e = '\0';
1440 if (ttype != comment && ttype != newline && ttype != preprocessing)
1441 ps.last_token = ttype;
1442 }
1443 }
1444
1445 int
1446 main(int argc, char **argv)
1447 {
1448 main_init_globals();
1449 main_parse_command_line(argc, argv);
1450 #if HAVE_CAPSICUM
1451 init_capsicum();
1452 #endif
1453 main_prepare_parsing();
1454 main_loop();
1455 }
1456
1457 /*
1458 * Copy the input file to the backup file, then make the backup file the input
1459 * and the original input file the output.
1460 */
1461 static void
1462 bakcopy(void)
1463 {
1464 ssize_t n;
1465 int bak_fd;
1466 char buff[8 * 1024];
1467
1468 const char *last_slash = strrchr(in_name, '/');
1469 snprintf(bakfile, sizeof(bakfile), "%s%s",
1470 last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
1471
1472 /* copy in_name to backup file */
1473 bak_fd = creat(bakfile, 0600);
1474 if (bak_fd < 0)
1475 err(1, "%s", bakfile);
1476
1477 while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
1478 if (write(bak_fd, buff, (size_t)n) != n)
1479 err(1, "%s", bakfile);
1480 if (n < 0)
1481 err(1, "%s", in_name);
1482
1483 close(bak_fd);
1484 (void)fclose(input);
1485
1486 /* re-open backup file as the input file */
1487 input = fopen(bakfile, "r");
1488 if (input == NULL)
1489 err(1, "%s", bakfile);
1490 /* now the original input file will be the output */
1491 output = fopen(in_name, "w");
1492 if (output == NULL) {
1493 unlink(bakfile);
1494 err(1, "%s", in_name);
1495 }
1496 }
1497
1498 static void
1499 indent_declaration(int cur_decl_ind, bool tabs_to_var)
1500 {
1501 int pos = (int)(code.e - code.s);
1502 char *startpos = code.e;
1503
1504 /*
1505 * get the tab math right for indentations that are not multiples of
1506 * tabsize
1507 */
1508 if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) {
1509 pos += (ps.ind_level * opt.indent_size) % opt.tabsize;
1510 cur_decl_ind += (ps.ind_level * opt.indent_size) % opt.tabsize;
1511 }
1512
1513 if (tabs_to_var) {
1514 int tpos;
1515
1516 while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_decl_ind) {
1517 buf_add_char(&code, '\t');
1518 pos = tpos;
1519 }
1520 }
1521
1522 while (pos < cur_decl_ind) {
1523 buf_add_char(&code, ' ');
1524 pos++;
1525 }
1526
1527 if (code.e == startpos && ps.want_blank) {
1528 *code.e++ = ' ';
1529 ps.want_blank = false;
1530 }
1531 }
1532
1533 #ifdef debug
1534 void
1535 debug_printf(const char *fmt, ...)
1536 {
1537 FILE *f = output == stdout ? stderr : stdout;
1538 va_list ap;
1539
1540 va_start(ap, fmt);
1541 vfprintf(f, fmt, ap);
1542 va_end(ap);
1543 }
1544
1545 void
1546 debug_println(const char *fmt, ...)
1547 {
1548 FILE *f = output == stdout ? stderr : stdout;
1549 va_list ap;
1550
1551 va_start(ap, fmt);
1552 vfprintf(f, fmt, ap);
1553 va_end(ap);
1554 fprintf(f, "\n");
1555 }
1556
1557 void
1558 debug_vis_range(const char *prefix, const char *s, const char *e,
1559 const char *suffix)
1560 {
1561 debug_printf("%s", prefix);
1562 for (const char *p = s; p < e; p++) {
1563 if (isprint((unsigned char)*p) && *p != '\\' && *p != '"')
1564 debug_printf("%c", *p);
1565 else if (*p == '\n')
1566 debug_printf("\\n");
1567 else if (*p == '\t')
1568 debug_printf("\\t");
1569 else
1570 debug_printf("\\x%02x", *p);
1571 }
1572 debug_printf("%s", suffix);
1573 }
1574 #endif
1575
1576 static void *
1577 nonnull(void *p)
1578 {
1579 if (p == NULL)
1580 err(EXIT_FAILURE, NULL);
1581 return p;
1582 }
1583
1584 void *
1585 xmalloc(size_t size)
1586 {
1587 return nonnull(malloc(size));
1588 }
1589
1590 void *
1591 xrealloc(void *p, size_t new_size)
1592 {
1593 return nonnull(realloc(p, new_size));
1594 }
1595
1596 char *
1597 xstrdup(const char *s)
1598 {
1599 return nonnull(strdup(s));
1600 }
1601