indent.c revision 1.199 1 /* $NetBSD: indent.c,v 1.199 2021/10/31 19:20: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.199 2021/10/31 19:20: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 <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(inbuf_next());
324 if (sc_end[-1] == '*' && *inp.s == '/') {
325 sc_add_char(inbuf_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 inbuf_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(inbuf_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(&com);
511 buf_init(&lab);
512 buf_init(&code);
513
514 ps.s_sym[0] = psym_stmt_list;
515 ps.prev_token = lsym_semicolon;
516 ps.curr_newline = 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 inbuf_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 process_comment_in_code(lexer_symbol lsym, bool *force_nl)
689 {
690 if (*force_nl &&
691 lsym != lsym_semicolon &&
692 (lsym != lsym_lbrace || !opt.brace_same_line)) {
693
694 /* we should force a broken line here */
695 if (opt.verbose)
696 diag(0, "Line broken");
697 dump_line();
698 ps.want_blank = false; /* don't insert blank at line start */
699 *force_nl = false;
700 }
701
702 /* add an extra level of indentation; turned off again by a ';' or '}' */
703 ps.in_stmt = true;
704
705 if (com.s != com.e) { /* a comment embedded in a line */
706 buf_add_char(&code, ' ');
707 buf_add_buf(&code, &com);
708 buf_add_char(&code, ' ');
709 buf_terminate(&code);
710 buf_reset(&com);
711 ps.want_blank = false;
712 }
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 (ps.prev_token == lsym_rparen_or_rbracket)
743 return false;
744 if (ps.prev_token == lsym_offsetof)
745 return opt.proc_calls_space;
746 if (ps.prev_token == lsym_sizeof)
747 return opt.proc_calls_space || opt.blank_after_sizeof;
748 if (ps.prev_token != lsym_ident && ps.prev_token != lsym_funcname)
749 return true;
750 if (opt.proc_calls_space)
751 return true;
752 return ps.prev_keyword != kw_0;
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] =
776 (short)indentation_after_range(0, code.s, code.e);
777 debug_println("paren_indents[%d] is now %d",
778 ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
779
780 if (spaced_expr && ps.p_l_follow == 1 && opt.extra_expr_indent
781 && ps.paren_indents[0] < 2 * opt.indent_size) {
782 ps.paren_indents[0] = (short)(2 * opt.indent_size);
783 debug_println("paren_indents[0] is now %d", ps.paren_indents[0]);
784 }
785
786 if (ps.init_or_struct && *token.s == '(' && ps.tos <= 2) {
787 /*
788 * this is a kluge to make sure that declarations will be aligned
789 * right if proc decl has an explicit type on it, i.e. "int a(x) {..."
790 */
791 parse(psym_semicolon); /* I said this was a kluge... */
792 ps.init_or_struct = false;
793 }
794
795 /* parenthesized type following sizeof or offsetof is not a cast */
796 if (ps.prev_keyword == kw_offsetof || ps.prev_keyword == kw_sizeof)
797 ps.not_cast_mask |= 1 << ps.p_l_follow;
798 }
799
800 static void
801 process_rparen_or_rbracket(bool *spaced_expr, bool *force_nl, stmt_head hd)
802 {
803 if ((ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) != 0) {
804 ps.next_unary = true;
805 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
806 ps.want_blank = opt.space_after_cast;
807 } else
808 ps.want_blank = true;
809 ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
810
811 if (ps.p_l_follow > 0)
812 ps.p_l_follow--;
813 else
814 diag(0, "Extra '%c'", *token.s);
815
816 if (code.e == code.s) /* if the paren starts the line */
817 ps.paren_level = ps.p_l_follow; /* then indent it */
818
819 *code.e++ = token.s[0];
820
821 if (*spaced_expr && ps.p_l_follow == 0) { /* check for end of 'if
822 * (...)', or some such */
823 *spaced_expr = false;
824 *force_nl = true; /* must force newline after if */
825 ps.next_unary = true;
826 ps.in_stmt = false; /* don't use stmt continuation indentation */
827
828 parse_stmt_head(hd);
829 }
830
831 /*
832 * This should ensure that constructs such as main(){...} and int[]{...}
833 * have their braces put in the right place.
834 */
835 ps.search_stmt = opt.brace_same_line;
836 }
837
838 static void
839 process_unary_op(int decl_ind, bool tabs_to_var)
840 {
841 if (!ps.decl_indent_done && ps.in_decl && !ps.block_init &&
842 ps.procname[0] == '\0' && ps.paren_level == 0) {
843 /* pointer declarations */
844 code_add_decl_indent(decl_ind - (int)buf_len(&token), tabs_to_var);
845 ps.decl_indent_done = true;
846 } else if (ps.want_blank)
847 *code.e++ = ' ';
848
849 buf_add_buf(&code, &token);
850 ps.want_blank = false;
851 }
852
853 static void
854 process_binary_op(void)
855 {
856 if (buf_len(&code) > 0)
857 buf_add_char(&code, ' ');
858 buf_add_buf(&code, &token);
859 ps.want_blank = true;
860 }
861
862 static void
863 process_postfix_op(void)
864 {
865 *code.e++ = token.s[0];
866 *code.e++ = token.s[1];
867 ps.want_blank = true;
868 }
869
870 static void
871 process_question(int *quest_level)
872 {
873 (*quest_level)++;
874 if (ps.want_blank)
875 *code.e++ = ' ';
876 *code.e++ = '?';
877 ps.want_blank = true;
878 }
879
880 static void
881 process_colon(int *quest_level, bool *force_nl, bool *seen_case)
882 {
883 if (*quest_level > 0) { /* part of a '?:' operator */
884 --*quest_level;
885 if (ps.want_blank)
886 *code.e++ = ' ';
887 *code.e++ = ':';
888 ps.want_blank = true;
889 return;
890 }
891
892 if (ps.init_or_struct) { /* bit-field */
893 *code.e++ = ':';
894 ps.want_blank = false;
895 return;
896 }
897
898 buf_add_buf(&lab, &code); /* 'case' or 'default' or named label */
899 buf_add_char(&lab, ':');
900 buf_terminate(&lab);
901 buf_reset(&code);
902
903 ps.in_stmt = false;
904 ps.is_case_label = *seen_case;
905 *force_nl = *seen_case;
906 *seen_case = false;
907 ps.want_blank = false;
908 }
909
910 static void
911 process_semicolon(bool *seen_case, int *quest_level, int decl_ind,
912 bool tabs_to_var, bool *spaced_expr, stmt_head hd, bool *force_nl)
913 {
914 if (ps.decl_nest == 0)
915 ps.init_or_struct = false;
916 *seen_case = false; /* these will only need resetting in an error */
917 *quest_level = 0;
918 if (ps.prev_token == lsym_rparen_or_rbracket)
919 ps.in_parameter_declaration = false;
920 ps.cast_mask = 0;
921 ps.not_cast_mask = 0;
922 ps.block_init = false;
923 ps.block_init_level = 0;
924 ps.just_saw_decl--;
925
926 if (ps.in_decl && code.s == code.e && !ps.block_init &&
927 !ps.decl_indent_done && ps.paren_level == 0) {
928 /* indent stray semicolons in declarations */
929 code_add_decl_indent(decl_ind - 1, tabs_to_var);
930 ps.decl_indent_done = true;
931 }
932
933 ps.in_decl = ps.decl_nest > 0; /* if we were in a first level
934 * structure declaration, we aren't
935 * anymore */
936
937 if ((!*spaced_expr || hd != hd_for) && ps.p_l_follow > 0) {
938
939 /*
940 * There were unbalanced parentheses in the statement. It is a bit
941 * complicated, because the semicolon might be in a for statement.
942 */
943 diag(1, "Unbalanced parentheses");
944 ps.p_l_follow = 0;
945 if (*spaced_expr) { /* 'if', 'while', etc. */
946 *spaced_expr = false;
947 parse_stmt_head(hd);
948 }
949 }
950 *code.e++ = ';';
951 ps.want_blank = true;
952 ps.in_stmt = ps.p_l_follow > 0;
953
954 if (!*spaced_expr) { /* if not if for (;;) */
955 parse(psym_semicolon); /* let parser know about end of stmt */
956 *force_nl = true; /* force newline after an end of stmt */
957 }
958 }
959
960 static void
961 process_lbrace(bool *force_nl, bool *spaced_expr, stmt_head hd,
962 int *di_stack, int di_stack_cap, int *decl_ind)
963 {
964 ps.in_stmt = false; /* don't indent the {} */
965
966 if (!ps.block_init)
967 *force_nl = true; /* force other stuff on same line as '{' onto
968 * new line */
969 else if (ps.block_init_level <= 0)
970 ps.block_init_level = 1;
971 else
972 ps.block_init_level++;
973
974 if (code.s != code.e && !ps.block_init) {
975 if (!opt.brace_same_line) {
976 dump_line();
977 ps.want_blank = false;
978 } else if (ps.in_parameter_declaration && !ps.init_or_struct) {
979 ps.ind_level_follow = 0;
980 if (opt.function_brace_split) { /* dump the line prior to the
981 * brace ... */
982 dump_line();
983 ps.want_blank = false;
984 } else /* add a space between the decl and brace */
985 ps.want_blank = true;
986 }
987 }
988
989 if (ps.in_parameter_declaration)
990 blank_line_before = false;
991
992 if (ps.p_l_follow > 0) {
993 diag(1, "Unbalanced parentheses");
994 ps.p_l_follow = 0;
995 if (*spaced_expr) { /* check for unclosed 'if', 'for', etc. */
996 *spaced_expr = false;
997 parse_stmt_head(hd);
998 ps.ind_level = ps.ind_level_follow;
999 }
1000 }
1001
1002 if (code.s == code.e)
1003 ps.ind_stmt = false; /* don't indent the '{' itself */
1004 if (ps.in_decl && ps.init_or_struct) {
1005 di_stack[ps.decl_nest] = *decl_ind;
1006 if (++ps.decl_nest == di_stack_cap) {
1007 diag(0, "Reached internal limit of %d struct levels",
1008 di_stack_cap);
1009 ps.decl_nest--;
1010 }
1011 } else {
1012 ps.decl_on_line = false; /* we can't be in the middle of a
1013 * declaration, so don't do special
1014 * indentation of comments */
1015 if (opt.blanklines_after_decl_at_top && ps.in_parameter_declaration)
1016 blank_line_after = true;
1017 ps.in_parameter_declaration = false;
1018 ps.in_decl = false;
1019 }
1020
1021 *decl_ind = 0;
1022 parse(psym_lbrace);
1023 if (ps.want_blank)
1024 *code.e++ = ' ';
1025 ps.want_blank = false;
1026 *code.e++ = '{';
1027 ps.just_saw_decl = 0;
1028 }
1029
1030 static void
1031 process_rbrace(bool *spaced_expr, int *decl_ind, const int *di_stack)
1032 {
1033 if (ps.s_sym[ps.tos] == psym_decl && !ps.block_init) {
1034 /* semicolons can be omitted in declarations */
1035 parse(psym_semicolon);
1036 }
1037
1038 if (ps.p_l_follow > 0) { /* check for unclosed if, for, else. */
1039 diag(1, "Unbalanced parentheses");
1040 ps.p_l_follow = 0;
1041 *spaced_expr = false;
1042 }
1043
1044 ps.just_saw_decl = 0;
1045 ps.block_init_level--;
1046
1047 if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */
1048 if (opt.verbose)
1049 diag(0, "Line broken");
1050 dump_line();
1051 }
1052
1053 *code.e++ = '}';
1054 ps.want_blank = true;
1055 ps.in_stmt = ps.ind_stmt = false;
1056
1057 if (ps.decl_nest > 0) { /* we are in multi-level structure declaration */
1058 *decl_ind = di_stack[--ps.decl_nest];
1059 if (ps.decl_nest == 0 && !ps.in_parameter_declaration) {
1060 ps.just_saw_decl = 2;
1061 *decl_ind = ps.ind_level == 0
1062 ? opt.decl_indent : opt.local_decl_indent;
1063 }
1064 ps.in_decl = true;
1065 }
1066
1067 blank_line_before = false;
1068 parse(psym_rbrace);
1069 ps.search_stmt = opt.cuddle_else
1070 && ps.s_sym[ps.tos] == psym_if_expr_stmt
1071 && ps.s_ind_level[ps.tos] >= ps.ind_level;
1072
1073 if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
1074 blank_line_after = true;
1075 }
1076
1077 static void
1078 process_keyword_do(bool *force_nl, bool *last_else)
1079 {
1080 ps.in_stmt = false;
1081
1082 if (code.e != code.s) { /* make sure this starts a line */
1083 if (opt.verbose)
1084 diag(0, "Line broken");
1085 dump_line();
1086 ps.want_blank = false;
1087 }
1088
1089 *force_nl = true; /* following stuff must go onto new line */
1090 *last_else = false;
1091 parse(psym_do);
1092 }
1093
1094 static void
1095 process_keyword_else(bool *force_nl, bool *last_else)
1096 {
1097 ps.in_stmt = false;
1098
1099 if (code.e != code.s && (!opt.cuddle_else || code.e[-1] != '}')) {
1100 if (opt.verbose)
1101 diag(0, "Line broken");
1102 dump_line(); /* make sure this starts a line */
1103 ps.want_blank = false;
1104 }
1105
1106 *force_nl = true; /* following stuff must go onto new line */
1107 *last_else = true;
1108 parse(psym_else);
1109 }
1110
1111 static void
1112 process_type(int *decl_ind, bool *tabs_to_var)
1113 {
1114 parse(psym_decl); /* let the parser worry about indentation */
1115
1116 if (ps.prev_token == lsym_rparen_or_rbracket && ps.tos <= 1) {
1117 if (code.s != code.e) {
1118 dump_line();
1119 ps.want_blank = false;
1120 }
1121 }
1122
1123 if (ps.in_parameter_declaration && opt.indent_parameters &&
1124 ps.decl_nest == 0) {
1125 ps.ind_level = ps.ind_level_follow = 1;
1126 ps.ind_stmt = false;
1127 }
1128
1129 ps.init_or_struct = /* maybe */ true;
1130 ps.in_decl = ps.decl_on_line = ps.prev_token != lsym_typedef;
1131 if (ps.decl_nest <= 0)
1132 ps.just_saw_decl = 2;
1133
1134 blank_line_before = false;
1135
1136 int len = (int)buf_len(&token) + 1;
1137 int ind = ps.ind_level == 0 || ps.decl_nest > 0
1138 ? opt.decl_indent /* global variable or local member */
1139 : opt.local_decl_indent; /* local variable */
1140 *decl_ind = ind > 0 ? ind : len;
1141 *tabs_to_var = opt.use_tabs && ind > 0;
1142 }
1143
1144 static void
1145 process_ident(lexer_symbol lsym, int decl_ind, bool tabs_to_var,
1146 bool *spaced_expr, bool *force_nl, stmt_head hd)
1147 {
1148 if (ps.in_decl) {
1149 if (lsym == lsym_funcname) {
1150 ps.in_decl = false;
1151 if (opt.procnames_start_line && code.s != code.e) {
1152 *code.e = '\0';
1153 dump_line();
1154 } else if (ps.want_blank) {
1155 *code.e++ = ' ';
1156 }
1157 ps.want_blank = false;
1158
1159 } else if (!ps.block_init && !ps.decl_indent_done &&
1160 ps.paren_level == 0) {
1161 code_add_decl_indent(decl_ind, tabs_to_var);
1162 ps.decl_indent_done = true;
1163 ps.want_blank = false;
1164 }
1165
1166 } else if (*spaced_expr && ps.p_l_follow == 0) {
1167 *spaced_expr = false;
1168 *force_nl = true;
1169 ps.next_unary = true;
1170 ps.in_stmt = false;
1171 parse_stmt_head(hd);
1172 }
1173 }
1174
1175 static void
1176 copy_token(void)
1177 {
1178 if (ps.want_blank)
1179 buf_add_char(&code, ' ');
1180 buf_add_buf(&code, &token);
1181 }
1182
1183 static void
1184 process_string_prefix(void)
1185 {
1186 copy_token();
1187 ps.want_blank = false;
1188 }
1189
1190 static void
1191 process_period(void)
1192 {
1193 if (code.e[-1] == ',')
1194 *code.e++ = ' ';
1195 *code.e++ = '.';
1196 ps.want_blank = false;
1197 }
1198
1199 static void
1200 process_comma(int decl_ind, bool tabs_to_var, bool *force_nl)
1201 {
1202 ps.want_blank = code.s != code.e; /* only put blank after comma if comma
1203 * does not start the line */
1204
1205 if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
1206 !ps.decl_indent_done && ps.paren_level == 0) {
1207 /* indent leading commas and not the actual identifiers */
1208 code_add_decl_indent(decl_ind - 1, tabs_to_var);
1209 ps.decl_indent_done = true;
1210 }
1211
1212 *code.e++ = ',';
1213
1214 if (ps.p_l_follow == 0) {
1215 if (ps.block_init_level <= 0)
1216 ps.block_init = false;
1217 int varname_len = 8; /* rough estimate for the length of a typical
1218 * variable name */
1219 if (break_comma && (opt.break_after_comma ||
1220 indentation_after_range(compute_code_indent(), code.s, code.e)
1221 >= opt.max_line_length - varname_len))
1222 *force_nl = true;
1223 }
1224 }
1225
1226 /* move the whole line to the 'label' buffer */
1227 static void
1228 read_preprocessing_line(void)
1229 {
1230 enum {
1231 PLAIN, STR, CHR, COMM
1232 } state;
1233
1234 buf_add_char(&lab, '#');
1235
1236 state = PLAIN;
1237 int com_start = 0, com_end = 0;
1238
1239 while (ch_isblank(*inp.s))
1240 inbuf_skip();
1241
1242 while (*inp.s != '\n' || (state == COMM && !had_eof)) {
1243 buf_reserve(&lab, 2);
1244 *lab.e++ = inbuf_next();
1245 switch (lab.e[-1]) {
1246 case '\\':
1247 if (state != COMM)
1248 *lab.e++ = inbuf_next();
1249 break;
1250 case '/':
1251 if (*inp.s == '*' && state == PLAIN) {
1252 state = COMM;
1253 *lab.e++ = *inp.s++;
1254 com_start = (int)buf_len(&lab) - 2;
1255 }
1256 break;
1257 case '"':
1258 if (state == STR)
1259 state = PLAIN;
1260 else if (state == PLAIN)
1261 state = STR;
1262 break;
1263 case '\'':
1264 if (state == CHR)
1265 state = PLAIN;
1266 else if (state == PLAIN)
1267 state = CHR;
1268 break;
1269 case '*':
1270 if (*inp.s == '/' && state == COMM) {
1271 state = PLAIN;
1272 *lab.e++ = *inp.s++;
1273 com_end = (int)buf_len(&lab);
1274 }
1275 break;
1276 }
1277 }
1278
1279 while (lab.e > lab.s && ch_isblank(lab.e[-1]))
1280 lab.e--;
1281 if (lab.e - lab.s == com_end && saved_inp_s == NULL) {
1282 /* comment on preprocessor line */
1283 if (sc_end == NULL) { /* if this is the first comment, we must set
1284 * up the buffer */
1285 save_com = sc_buf;
1286 sc_end = save_com;
1287 } else {
1288 sc_add_char('\n'); /* add newline between comments */
1289 sc_add_char(' ');
1290 --line_no;
1291 }
1292 sc_add_range(lab.s + com_start, lab.s + com_end);
1293 lab.e = lab.s + com_start;
1294 while (lab.e > lab.s && ch_isblank(lab.e[-1]))
1295 lab.e--;
1296 saved_inp_s = inp.s; /* save current input buffer */
1297 saved_inp_e = inp.e;
1298 inp.s = save_com; /* fix so that subsequent calls to lexi will
1299 * take tokens out of save_com */
1300 sc_add_char(' '); /* add trailing blank, just in case */
1301 debug_save_com(__func__);
1302 inp.e = sc_end;
1303 sc_end = NULL;
1304 debug_println("switched inp.s to save_com");
1305 }
1306 buf_terminate(&lab);
1307 }
1308
1309 static void
1310 process_preprocessing(void)
1311 {
1312 if (com.s != com.e || lab.s != lab.e || code.s != code.e)
1313 dump_line();
1314
1315 read_preprocessing_line();
1316
1317 ps.is_case_label = false;
1318
1319 if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */
1320 if ((size_t)ifdef_level < array_length(state_stack))
1321 state_stack[ifdef_level++] = ps;
1322 else
1323 diag(1, "#if stack overflow");
1324
1325 } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */
1326 if (ifdef_level <= 0)
1327 diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1328 else
1329 ps = state_stack[ifdef_level - 1];
1330
1331 } else if (strncmp(lab.s, "#endif", 6) == 0) {
1332 if (ifdef_level <= 0)
1333 diag(1, "Unmatched #endif");
1334 else
1335 ifdef_level--;
1336
1337 } else {
1338 if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
1339 strncmp(lab.s + 1, "error", 5) != 0 &&
1340 strncmp(lab.s + 1, "line", 4) != 0 &&
1341 strncmp(lab.s + 1, "undef", 5) != 0 &&
1342 strncmp(lab.s + 1, "define", 6) != 0 &&
1343 strncmp(lab.s + 1, "include", 7) != 0) {
1344 diag(1, "Unrecognized cpp directive");
1345 return;
1346 }
1347 }
1348
1349 if (opt.blanklines_around_conditional_compilation) {
1350 blank_line_after = true;
1351 blank_lines_to_output = 0;
1352 } else {
1353 blank_line_after = false;
1354 blank_line_before = false;
1355 }
1356
1357 /*
1358 * subsequent processing of the newline character will cause the line to
1359 * be printed
1360 */
1361 }
1362
1363 static void __attribute__((__noreturn__))
1364 main_loop(void)
1365 {
1366 bool force_nl = false; /* when true, code must be broken */
1367 bool last_else = false; /* true iff last keyword was an else */
1368 int decl_ind = 0; /* current indentation for declarations */
1369 int di_stack[20]; /* a stack of structure indentation levels */
1370 bool tabs_to_var = false; /* true if using tabs to indent to var name */
1371 bool spaced_expr = false; /* whether we are in the expression of
1372 * if(...), while(...), etc. */
1373 stmt_head hd = hd_0; /* the type of statement for 'if (...)', 'for
1374 * (...)', etc */
1375 int quest_level = 0; /* when this is positive, we have seen a '?'
1376 * without the matching ':' in a '?:'
1377 * expression */
1378 bool seen_case = false; /* set to true when we see a 'case', so we
1379 * know what to do with the following colon */
1380
1381 di_stack[ps.decl_nest = 0] = 0;
1382
1383 for (;;) { /* loop until we reach eof */
1384 lexer_symbol lsym = lexi();
1385
1386 search_stmt(&lsym, &force_nl, &last_else);
1387
1388 if (lsym == lsym_eof) {
1389 process_end_of_file();
1390 /* NOTREACHED */
1391 }
1392
1393 if (lsym == lsym_newline || lsym == lsym_form_feed ||
1394 lsym == lsym_preprocessing)
1395 force_nl = false;
1396 else if (lsym != lsym_comment)
1397 process_comment_in_code(lsym, &force_nl);
1398
1399 buf_reserve(&code, 3); /* space for 2 characters plus '\0' */
1400
1401 switch (lsym) {
1402
1403 case lsym_form_feed:
1404 process_form_feed();
1405 break;
1406
1407 case lsym_newline:
1408 process_newline();
1409 break;
1410
1411 case lsym_lparen_or_lbracket:
1412 process_lparen_or_lbracket(decl_ind, tabs_to_var, spaced_expr);
1413 break;
1414
1415 case lsym_rparen_or_rbracket:
1416 process_rparen_or_rbracket(&spaced_expr, &force_nl, hd);
1417 break;
1418
1419 case lsym_unary_op:
1420 process_unary_op(decl_ind, tabs_to_var);
1421 break;
1422
1423 case lsym_binary_op:
1424 process_binary_op();
1425 break;
1426
1427 case lsym_postfix_op:
1428 process_postfix_op();
1429 break;
1430
1431 case lsym_question:
1432 process_question(&quest_level);
1433 break;
1434
1435 case lsym_case_label:
1436 seen_case = true;
1437 goto copy_token;
1438
1439 case lsym_colon:
1440 process_colon(&quest_level, &force_nl, &seen_case);
1441 break;
1442
1443 case lsym_semicolon:
1444 process_semicolon(&seen_case, &quest_level, decl_ind, tabs_to_var,
1445 &spaced_expr, hd, &force_nl);
1446 break;
1447
1448 case lsym_lbrace:
1449 process_lbrace(&force_nl, &spaced_expr, hd, di_stack,
1450 (int)array_length(di_stack), &decl_ind);
1451 break;
1452
1453 case lsym_rbrace:
1454 process_rbrace(&spaced_expr, &decl_ind, di_stack);
1455 break;
1456
1457 case lsym_switch:
1458 spaced_expr = true; /* the interesting stuff is done after the
1459 * expressions are scanned */
1460 hd = hd_switch; /* remember the type of header for later use
1461 * by the parser */
1462 goto copy_token;
1463
1464 case lsym_for:
1465 spaced_expr = true;
1466 hd = hd_for;
1467 goto copy_token;
1468
1469 case lsym_if:
1470 spaced_expr = true;
1471 hd = hd_if;
1472 goto copy_token;
1473
1474 case lsym_while:
1475 spaced_expr = true;
1476 hd = hd_while;
1477 goto copy_token;
1478
1479 case lsym_do:
1480 process_keyword_do(&force_nl, &last_else);
1481 goto copy_token;
1482
1483 case lsym_else:
1484 process_keyword_else(&force_nl, &last_else);
1485 goto copy_token;
1486
1487 case lsym_typedef:
1488 case lsym_storage_class:
1489 blank_line_before = false;
1490 goto copy_token;
1491
1492 case lsym_tag:
1493 if (ps.p_l_follow > 0)
1494 goto copy_token;
1495 /* FALLTHROUGH */
1496 case lsym_type_at_paren_level_0:
1497 process_type(&decl_ind, &tabs_to_var);
1498 goto copy_token;
1499
1500 case lsym_offsetof:
1501 case lsym_sizeof:
1502 case lsym_ident:
1503 case lsym_funcname:
1504 process_ident(lsym, decl_ind, tabs_to_var, &spaced_expr,
1505 &force_nl, hd);
1506 copy_token:
1507 copy_token();
1508 if (lsym != lsym_funcname)
1509 ps.want_blank = true;
1510 break;
1511
1512 case lsym_string_prefix:
1513 process_string_prefix();
1514 break;
1515
1516 case lsym_period:
1517 process_period();
1518 break;
1519
1520 case lsym_comma:
1521 process_comma(decl_ind, tabs_to_var, &force_nl);
1522 break;
1523
1524 case lsym_preprocessing:
1525 process_preprocessing();
1526 break;
1527
1528 case lsym_comment:
1529 process_comment();
1530 break;
1531
1532 default:
1533 break;
1534 }
1535
1536 *code.e = '\0';
1537 if (lsym != lsym_comment && lsym != lsym_newline &&
1538 lsym != lsym_preprocessing)
1539 ps.prev_token = lsym;
1540 }
1541 }
1542
1543 int
1544 main(int argc, char **argv)
1545 {
1546 main_init_globals();
1547 main_load_profiles(argc, argv);
1548 main_parse_command_line(argc, argv);
1549 #if HAVE_CAPSICUM
1550 init_capsicum();
1551 #endif
1552 main_prepare_parsing();
1553 main_loop();
1554 }
1555
1556 #ifdef debug
1557 void
1558 debug_printf(const char *fmt, ...)
1559 {
1560 FILE *f = output == stdout ? stderr : stdout;
1561 va_list ap;
1562
1563 va_start(ap, fmt);
1564 vfprintf(f, fmt, ap);
1565 va_end(ap);
1566 }
1567
1568 void
1569 debug_println(const char *fmt, ...)
1570 {
1571 FILE *f = output == stdout ? stderr : stdout;
1572 va_list ap;
1573
1574 va_start(ap, fmt);
1575 vfprintf(f, fmt, ap);
1576 va_end(ap);
1577 fprintf(f, "\n");
1578 }
1579
1580 void
1581 debug_vis_range(const char *prefix, const char *s, const char *e,
1582 const char *suffix)
1583 {
1584 debug_printf("%s", prefix);
1585 for (const char *p = s; p < e; p++) {
1586 if (*p == '\\' || *p == '"')
1587 debug_printf("\\%c", *p);
1588 else if (isprint((unsigned char)*p))
1589 debug_printf("%c", *p);
1590 else if (*p == '\n')
1591 debug_printf("\\n");
1592 else if (*p == '\t')
1593 debug_printf("\\t");
1594 else
1595 debug_printf("\\x%02x", (unsigned char)*p);
1596 }
1597 debug_printf("%s", suffix);
1598 }
1599 #endif
1600
1601 static void *
1602 nonnull(void *p)
1603 {
1604 if (p == NULL)
1605 err(EXIT_FAILURE, NULL);
1606 return p;
1607 }
1608
1609 void *
1610 xmalloc(size_t size)
1611 {
1612 return nonnull(malloc(size));
1613 }
1614
1615 void *
1616 xrealloc(void *p, size_t new_size)
1617 {
1618 return nonnull(realloc(p, new_size));
1619 }
1620
1621 char *
1622 xstrdup(const char *s)
1623 {
1624 return nonnull(strdup(s));
1625 }
1626