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