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