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