Home | History | Annotate | Line # | Download | only in indent
indent.c revision 1.359
      1 /*	$NetBSD: indent.c,v 1.359 2023/06/14 08:36:51 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 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: indent.c,v 1.359 2023/06/14 08:36:51 rillig Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <err.h>
     45 #include <stdarg.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #include "indent.h"
     51 
     52 struct options opt = {
     53 	.brace_same_line = true,
     54 	.comment_delimiter_on_blank_line = true,
     55 	.cuddle_else = true,
     56 	.comment_column = 33,
     57 	.decl_indent = 16,
     58 	.else_if_in_same_line = true,
     59 	.function_brace_split = true,
     60 	.format_col1_comments = true,
     61 	.format_block_comments = true,
     62 	.indent_parameters = true,
     63 	.indent_size = 8,
     64 	.local_decl_indent = -1,
     65 	.lineup_to_parens = true,
     66 	.procnames_start_line = true,
     67 	.star_comment_cont = true,
     68 	.tabsize = 8,
     69 	.max_line_length = 78,
     70 	.use_tabs = true,
     71 };
     72 
     73 struct parser_state ps;
     74 
     75 struct buffer token;
     76 
     77 struct buffer lab;
     78 struct buffer code;
     79 struct buffer com;
     80 
     81 bool found_err;
     82 bool had_eof;
     83 int line_no = 1;
     84 
     85 static int ifdef_level;
     86 static struct parser_state state_stack[5];
     87 
     88 FILE *input;
     89 FILE *output;
     90 
     91 static const char *in_name = "Standard Input";
     92 static char backup_name[PATH_MAX];
     93 static const char *backup_suffix = ".BAK";
     94 
     95 
     96 void *
     97 nonnull(void *p)
     98 {
     99 	if (p == NULL)
    100 		err(EXIT_FAILURE, NULL);
    101 	return p;
    102 }
    103 
    104 static void
    105 buf_expand(struct buffer *buf, size_t add_size)
    106 {
    107 	buf->cap = buf->cap + add_size + 400;
    108 	buf->s = nonnull(realloc(buf->s, buf->cap));
    109 }
    110 
    111 #ifdef debug
    112 void
    113 buf_terminate(struct buffer *buf)
    114 {
    115 	if (buf->len == buf->cap)
    116 		buf_expand(buf, 1);
    117 	buf->s[buf->len] = '\0';
    118 }
    119 #endif
    120 
    121 void
    122 buf_add_char(struct buffer *buf, char ch)
    123 {
    124 	if (buf->len == buf->cap)
    125 		buf_expand(buf, 1);
    126 	buf->s[buf->len++] = ch;
    127 	buf_terminate(buf);
    128 }
    129 
    130 void
    131 buf_add_chars(struct buffer *buf, const char *s, size_t len)
    132 {
    133 	if (len == 0)
    134 		return;
    135 	if (len > buf->cap - buf->len)
    136 		buf_expand(buf, len);
    137 	memcpy(buf->s + buf->len, s, len);
    138 	buf->len += len;
    139 	buf_terminate(buf);
    140 }
    141 
    142 static void
    143 buf_add_buf(struct buffer *buf, const struct buffer *add)
    144 {
    145 	buf_add_chars(buf, add->s, add->len);
    146 }
    147 
    148 void
    149 diag(int level, const char *msg, ...)
    150 {
    151 	va_list ap;
    152 
    153 	if (level != 0)
    154 		found_err = true;
    155 
    156 	va_start(ap, msg);
    157 	fprintf(stderr, "%s: %s:%d: ",
    158 	    level == 0 ? "warning" : "error", in_name, line_no);
    159 	vfprintf(stderr, msg, ap);
    160 	fprintf(stderr, "\n");
    161 	va_end(ap);
    162 }
    163 
    164 /*
    165  * Compute the indentation from starting at 'ind' and adding the text starting
    166  * at 's'.
    167  */
    168 int
    169 ind_add(int ind, const char *s, size_t len)
    170 {
    171 	for (const char *p = s; len > 0; p++, len--) {
    172 		if (*p == '\n')
    173 			ind = 0;
    174 		else if (*p == '\t')
    175 			ind = next_tab(ind);
    176 		else if (*p == '\b')
    177 			--ind;
    178 		else
    179 			++ind;
    180 	}
    181 	return ind;
    182 }
    183 
    184 static void
    185 init_globals(void)
    186 {
    187 	ps.psyms.sym[0] = psym_stmt;
    188 	ps.prev_lsym = lsym_semicolon;
    189 	ps.lbrace_kind = psym_lbrace_block;
    190 
    191 	const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
    192 	if (suffix != NULL)
    193 		backup_suffix = suffix;
    194 }
    195 
    196 static void
    197 load_profiles(int argc, char **argv)
    198 {
    199 	const char *profile_name = NULL;
    200 
    201 	for (int i = 1; i < argc; ++i) {
    202 		const char *arg = argv[i];
    203 
    204 		if (strcmp(arg, "-npro") == 0)
    205 			return;
    206 		if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
    207 			profile_name = arg + 2;
    208 	}
    209 
    210 	load_profile_files(profile_name);
    211 }
    212 
    213 /*
    214  * Copy the input file to the backup file, then make the backup file the input
    215  * and the original input file the output.
    216  */
    217 static void
    218 copy_to_bak_file(void)
    219 {
    220 	size_t n;
    221 	char buff[BUFSIZ];
    222 
    223 	const char *last_slash = strrchr(in_name, '/');
    224 	const char *base = last_slash != NULL ? last_slash + 1 : in_name;
    225 	snprintf(backup_name, sizeof(backup_name), "%s%s", base, backup_suffix);
    226 
    227 	/* copy the input file to the backup file */
    228 	FILE *bak = fopen(backup_name, "w");
    229 	if (bak == NULL)
    230 		err(1, "%s", backup_name);
    231 
    232 	while ((n = fread(buff, 1, sizeof(buff), input)) > 0)
    233 		if (fwrite(buff, 1, n, bak) != n)
    234 			err(1, "%s", backup_name);
    235 	if (fclose(input) != 0)
    236 		err(1, "%s", in_name);
    237 	if (fclose(bak) != 0)
    238 		err(1, "%s", backup_name);
    239 
    240 	/* re-open the backup file as the input file */
    241 	input = fopen(backup_name, "r");
    242 	if (input == NULL)
    243 		err(1, "%s", backup_name);
    244 	/* now the original input file will be the output */
    245 	output = fopen(in_name, "w");
    246 	if (output == NULL) {
    247 		remove(backup_name);
    248 		err(1, "%s", in_name);
    249 	}
    250 }
    251 
    252 static void
    253 parse_command_line(int argc, char **argv)
    254 {
    255 	for (int i = 1; i < argc; ++i) {
    256 		const char *arg = argv[i];
    257 
    258 		if (arg[0] == '-') {
    259 			set_option(arg, "Command line");
    260 
    261 		} else if (input == NULL) {
    262 			in_name = arg;
    263 			if ((input = fopen(in_name, "r")) == NULL)
    264 				err(1, "%s", in_name);
    265 
    266 		} else if (output == NULL) {
    267 			if (strcmp(arg, in_name) == 0)
    268 				errx(1, "input and output files "
    269 				    "must be different");
    270 			if ((output = fopen(arg, "w")) == NULL)
    271 				err(1, "%s", arg);
    272 
    273 		} else
    274 			errx(1, "too many arguments: %s", arg);
    275 	}
    276 
    277 	if (input == NULL) {
    278 		input = stdin;
    279 		output = stdout;
    280 	} else if (output == NULL)
    281 		copy_to_bak_file();
    282 
    283 	if (opt.comment_column <= 1)
    284 		opt.comment_column = 2;	/* don't put normal comments in column
    285 					 * 1, see opt.format_col1_comments */
    286 	if (opt.block_comment_max_line_length <= 0)
    287 		opt.block_comment_max_line_length = opt.max_line_length;
    288 	if (opt.local_decl_indent < 0)
    289 		opt.local_decl_indent = opt.decl_indent;
    290 	if (opt.decl_comment_column <= 0)
    291 		opt.decl_comment_column = opt.left_justify_decl
    292 		    ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
    293 		    : opt.comment_column;
    294 	if (opt.continuation_indent == 0)
    295 		opt.continuation_indent = opt.indent_size;
    296 }
    297 
    298 static void
    299 set_initial_indentation(void)
    300 {
    301 	inp_read_line();
    302 
    303 	int ind = 0;
    304 	for (const char *p = inp_p;; p++) {
    305 		if (*p == ' ')
    306 			ind++;
    307 		else if (*p == '\t')
    308 			ind = next_tab(ind);
    309 		else
    310 			break;
    311 	}
    312 
    313 	ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
    314 }
    315 
    316 static bool
    317 should_break_line(lexer_symbol lsym)
    318 {
    319 	if (lsym == lsym_semicolon)
    320 		return false;
    321 	if (ps.prev_lsym == lsym_lbrace || ps.prev_lsym == lsym_semicolon)
    322 		return true;
    323 	if (lsym == lsym_lbrace && opt.brace_same_line)
    324 		return false;
    325 	return true;
    326 }
    327 
    328 static void
    329 move_com_to_code(lexer_symbol lsym)
    330 {
    331 	if (ps.want_blank)
    332 		buf_add_char(&code, ' ');
    333 	buf_add_buf(&code, &com);
    334 	buf_clear(&com);
    335 	ps.want_blank = lsym != lsym_rparen && lsym != lsym_rbracket;
    336 }
    337 
    338 static void
    339 update_ps_lbrace_kind(lexer_symbol lsym)
    340 {
    341 	if (lsym == lsym_tag) {
    342 		ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
    343 		    token.s[0] == 'u' ? psym_lbrace_union :
    344 		    psym_lbrace_enum;
    345 	} else if (lsym != lsym_type_outside_parentheses
    346 	    && lsym != lsym_word
    347 	    && lsym != lsym_lbrace)
    348 		ps.lbrace_kind = psym_lbrace_block;
    349 }
    350 
    351 static void
    352 indent_declarator(int decl_ind, bool tabs_to_var)
    353 {
    354 	int base = ps.ind_level * opt.indent_size;
    355 	int ind = base + (int)code.len;
    356 	int target = base + decl_ind;
    357 	size_t orig_code_len = code.len;
    358 
    359 	if (tabs_to_var)
    360 		for (int next; (next = next_tab(ind)) <= target; ind = next)
    361 			buf_add_char(&code, '\t');
    362 
    363 	for (; ind < target; ind++)
    364 		buf_add_char(&code, ' ');
    365 
    366 	if (code.len == orig_code_len && ps.want_blank) {
    367 		buf_add_char(&code, ' ');
    368 		ps.want_blank = false;
    369 	}
    370 	ps.decl_indent_done = true;
    371 }
    372 
    373 static bool
    374 is_function_pointer_declaration(void)
    375 {
    376 	return ps.in_decl
    377 	    && !ps.in_init
    378 	    && !ps.decl_indent_done
    379 	    && !ps.line_has_func_def
    380 	    && ps.ind_paren_level == 0;
    381 }
    382 
    383 static int
    384 process_eof(void)
    385 {
    386 	finish_output();
    387 
    388 	if (ps.psyms.top > 1)	/* check for balanced braces */
    389 		diag(1, "Stuff missing from end of file");
    390 
    391 	return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
    392 }
    393 
    394 /* move the whole line to the 'label' buffer */
    395 static void
    396 read_preprocessing_line(void)
    397 {
    398 	enum {
    399 		PLAIN, STR, CHR, COMM
    400 	} state = PLAIN;
    401 
    402 	buf_add_char(&lab, '#');
    403 
    404 	while (inp_p[0] != '\n' || (state == COMM && !had_eof)) {
    405 		buf_add_char(&lab, inp_next());
    406 		switch (lab.s[lab.len - 1]) {
    407 		case '\\':
    408 			if (state != COMM)
    409 				buf_add_char(&lab, inp_next());
    410 			break;
    411 		case '/':
    412 			if (inp_p[0] == '*' && state == PLAIN) {
    413 				state = COMM;
    414 				buf_add_char(&lab, *inp_p++);
    415 			}
    416 			break;
    417 		case '"':
    418 			if (state == STR)
    419 				state = PLAIN;
    420 			else if (state == PLAIN)
    421 				state = STR;
    422 			break;
    423 		case '\'':
    424 			if (state == CHR)
    425 				state = PLAIN;
    426 			else if (state == PLAIN)
    427 				state = CHR;
    428 			break;
    429 		case '*':
    430 			if (inp_p[0] == '/' && state == COMM) {
    431 				state = PLAIN;
    432 				buf_add_char(&lab, *inp_p++);
    433 			}
    434 			break;
    435 		}
    436 	}
    437 
    438 	while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
    439 		lab.len--;
    440 	buf_terminate(&lab);
    441 }
    442 
    443 static void
    444 process_preprocessing(void)
    445 {
    446 	if (lab.len > 0 || code.len > 0 || com.len > 0)
    447 		output_line();
    448 
    449 	read_preprocessing_line();
    450 
    451 	const char *dir = lab.s + 1, *line_end = lab.s + lab.len;
    452 	while (dir < line_end && ch_isblank(*dir))
    453 		dir++;
    454 	size_t dir_len = 0;
    455 	while (dir + dir_len < line_end && ch_isalpha(dir[dir_len]))
    456 		dir_len++;
    457 
    458 	if (dir_len >= 2 && memcmp(dir, "if", 2) == 0) {
    459 		if ((size_t)ifdef_level < array_length(state_stack))
    460 			state_stack[ifdef_level++] = ps;
    461 		else
    462 			diag(1, "#if stack overflow");
    463 		out.line_kind = lk_if;
    464 
    465 	} else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
    466 		if (ifdef_level <= 0)
    467 			diag(1, dir[2] == 'i'
    468 			    ? "Unmatched #elif" : "Unmatched #else");
    469 		else
    470 			ps = state_stack[ifdef_level - 1];
    471 
    472 	} else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
    473 		if (ifdef_level <= 0)
    474 			diag(1, "Unmatched #endif");
    475 		else
    476 			ifdef_level--;
    477 		out.line_kind = lk_endif;
    478 	}
    479 }
    480 
    481 static void
    482 process_newline(void)
    483 {
    484 	if (ps.prev_lsym == lsym_comma
    485 	    && ps.nparen == 0 && !ps.in_init
    486 	    && !opt.break_after_comma && ps.break_after_comma
    487 	    && lab.len == 0	/* for preprocessing lines */
    488 	    && com.len == 0)
    489 		goto stay_in_line;
    490 	if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr
    491 	    && opt.brace_same_line) {
    492 		ps.force_nl = true;
    493 		goto stay_in_line;
    494 	}
    495 
    496 	output_line();
    497 
    498 stay_in_line:
    499 	++line_no;
    500 }
    501 
    502 static bool
    503 want_blank_before_lparen(void)
    504 {
    505 	if (!ps.want_blank)
    506 		return false;
    507 	if (opt.proc_calls_space)
    508 		return true;
    509 	if (ps.prev_lsym == lsym_rparen || ps.prev_lsym == lsym_rbracket)
    510 		return false;
    511 	if (ps.prev_lsym == lsym_offsetof)
    512 		return false;
    513 	if (ps.prev_lsym == lsym_sizeof)
    514 		return opt.blank_after_sizeof;
    515 	if (ps.prev_lsym == lsym_word || ps.prev_lsym == lsym_funcname)
    516 		return false;
    517 	return true;
    518 }
    519 
    520 static void
    521 process_lparen(void)
    522 {
    523 	if (++ps.nparen == array_length(ps.paren)) {
    524 		diag(0, "Reached internal limit of %zu unclosed parentheses",
    525 		    array_length(ps.paren));
    526 		ps.nparen--;
    527 	}
    528 
    529 	if (is_function_pointer_declaration())
    530 		indent_declarator(ps.decl_ind, ps.tabs_to_var);
    531 	else if (want_blank_before_lparen())
    532 		buf_add_char(&code, ' ');
    533 	ps.want_blank = false;
    534 	buf_add_char(&code, token.s[0]);
    535 
    536 	if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0)
    537 		ps.extra_expr_indent = eei_maybe;
    538 
    539 	if (ps.in_var_decl && ps.psyms.top <= 2 && !ps.in_init) {
    540 		parse(psym_stmt);	/* prepare for function definition */
    541 		ps.in_var_decl = false;
    542 	}
    543 
    544 	int indent = ind_add(0, code.s, code.len);
    545 
    546 	enum paren_level_cast cast = cast_unknown;
    547 	if (ps.prev_lsym == lsym_offsetof
    548 	    || ps.prev_lsym == lsym_sizeof
    549 	    || ps.prev_lsym == lsym_for
    550 	    || ps.prev_lsym == lsym_if
    551 	    || ps.prev_lsym == lsym_switch
    552 	    || ps.prev_lsym == lsym_while
    553 	    || ps.line_has_func_def)
    554 		cast = cast_no;
    555 
    556 	ps.paren[ps.nparen - 1].indent = indent;
    557 	ps.paren[ps.nparen - 1].cast = cast;
    558 	debug_println("paren_indents[%d] is now %s%d",
    559 	    ps.nparen - 1, paren_level_cast_name[cast], indent);
    560 }
    561 
    562 static void
    563 process_rparen(void)
    564 {
    565 	if (ps.nparen == 0) {
    566 		diag(0, "Extra '%c'", *token.s);
    567 		goto unbalanced;
    568 	}
    569 
    570 	enum paren_level_cast cast = ps.paren[--ps.nparen].cast;
    571 	if (ps.in_func_def_params || (ps.line_has_decl && !ps.in_init))
    572 		cast = cast_no;
    573 
    574 	ps.prev_paren_was_cast = cast == cast_maybe;
    575 	if (cast == cast_maybe) {
    576 		ps.next_unary = true;
    577 		ps.want_blank = opt.space_after_cast;
    578 	} else
    579 		ps.want_blank = true;
    580 
    581 	if (code.len == 0)
    582 		ps.ind_paren_level = ps.nparen;
    583 
    584 unbalanced:
    585 	buf_add_char(&code, token.s[0]);
    586 
    587 	if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
    588 		if (ps.extra_expr_indent == eei_maybe)
    589 			ps.extra_expr_indent = eei_last;
    590 		ps.force_nl = true;
    591 		ps.next_unary = true;
    592 		ps.in_stmt_or_decl = false;
    593 		parse(ps.spaced_expr_psym);
    594 		ps.spaced_expr_psym = psym_0;
    595 		ps.want_blank = true;
    596 		out.line_kind = lk_stmt_head;
    597 	}
    598 }
    599 
    600 static void
    601 process_lbracket(void)
    602 {
    603 	if (++ps.nparen == array_length(ps.paren)) {
    604 		diag(0, "Reached internal limit of %zu unclosed parentheses",
    605 		    array_length(ps.paren));
    606 		ps.nparen--;
    607 	}
    608 
    609 	if (code.len > 0
    610 	    && (ps.prev_lsym == lsym_comma || ps.prev_lsym == lsym_binary_op))
    611 		buf_add_char(&code, ' ');
    612 	ps.want_blank = false;
    613 	buf_add_char(&code, token.s[0]);
    614 
    615 	int indent = ind_add(0, code.s, code.len);
    616 
    617 	ps.paren[ps.nparen - 1].indent = indent;
    618 	ps.paren[ps.nparen - 1].cast = cast_no;
    619 	debug_println("paren_indents[%d] is now %d", ps.nparen - 1, indent);
    620 }
    621 
    622 static void
    623 process_rbracket(void)
    624 {
    625 	if (ps.nparen == 0) {
    626 		diag(0, "Extra '%c'", *token.s);
    627 		goto unbalanced;
    628 	}
    629 	--ps.nparen;
    630 
    631 	ps.want_blank = true;
    632 	if (code.len == 0)
    633 		ps.ind_paren_level = ps.nparen;
    634 
    635 unbalanced:
    636 	buf_add_char(&code, token.s[0]);
    637 }
    638 
    639 static void
    640 process_lbrace(void)
    641 {
    642 	if (ps.prev_lsym == lsym_rparen && ps.prev_paren_was_cast) {
    643 		ps.in_var_decl = true;	// XXX: not really
    644 		ps.in_init = true;
    645 	}
    646 
    647 	if (out.line_kind == lk_stmt_head)
    648 		out.line_kind = lk_other;
    649 
    650 	ps.in_stmt_or_decl = false;	/* don't indent the {} */
    651 
    652 	if (ps.in_init)
    653 		ps.init_level++;
    654 	else
    655 		ps.force_nl = true;
    656 
    657 	if (code.len > 0 && !ps.in_init) {
    658 		if (!opt.brace_same_line ||
    659 		    (code.len > 0 && code.s[code.len - 1] == '}'))
    660 			output_line();
    661 		else if (ps.in_func_def_params && !ps.in_var_decl) {
    662 			ps.ind_level_follow = 0;
    663 			if (opt.function_brace_split)
    664 				output_line();
    665 			else
    666 				ps.want_blank = true;
    667 		}
    668 	}
    669 
    670 	if (ps.nparen > 0 && ps.init_level == 0) {
    671 		diag(1, "Unbalanced parentheses");
    672 		ps.nparen = 0;
    673 		if (ps.spaced_expr_psym != psym_0) {
    674 			parse(ps.spaced_expr_psym);
    675 			ps.spaced_expr_psym = psym_0;
    676 			ps.ind_level = ps.ind_level_follow;
    677 		}
    678 	}
    679 
    680 	if (code.len == 0)
    681 		ps.in_stmt_cont = false;	/* don't indent the '{' itself
    682 						 */
    683 	if (ps.in_decl && ps.in_var_decl) {
    684 		ps.di_stack[ps.decl_level] = ps.decl_ind;
    685 		if (++ps.decl_level == (int)array_length(ps.di_stack)) {
    686 			diag(0, "Reached internal limit of %zu struct levels",
    687 			    array_length(ps.di_stack));
    688 			ps.decl_level--;
    689 		}
    690 	} else {
    691 		ps.line_has_decl = false;	/* we can't be in the middle of
    692 						 * a declaration, so don't do
    693 						 * special indentation of
    694 						 * comments */
    695 		ps.in_func_def_params = false;
    696 		ps.in_decl = false;
    697 	}
    698 
    699 	ps.decl_ind = 0;
    700 	parse(ps.lbrace_kind);
    701 	if (ps.want_blank)
    702 		buf_add_char(&code, ' ');
    703 	ps.want_blank = false;
    704 	buf_add_char(&code, '{');
    705 	ps.declaration = decl_no;
    706 }
    707 
    708 static void
    709 process_rbrace(void)
    710 {
    711 	if (ps.nparen > 0 && ps.init_level == 0) {
    712 		diag(1, "Unbalanced parentheses");
    713 		ps.nparen = 0;
    714 		ps.spaced_expr_psym = psym_0;
    715 	}
    716 
    717 	ps.declaration = decl_no;
    718 	if (ps.init_level > 0)
    719 		ps.init_level--;
    720 
    721 	if (code.len > 0 && !ps.in_init)
    722 		output_line();
    723 
    724 	buf_add_char(&code, '}');
    725 	ps.want_blank = true;
    726 	ps.in_stmt_or_decl = false;	// XXX: Initializers don't end a stmt
    727 	ps.in_stmt_cont = false;
    728 
    729 	if (ps.decl_level > 0) {	/* multi-level structure declaration */
    730 		ps.decl_ind = ps.di_stack[--ps.decl_level];
    731 		if (ps.decl_level == 0 && !ps.in_func_def_params) {
    732 			ps.declaration = decl_begin;
    733 			ps.decl_ind = ps.ind_level == 0
    734 			    ? opt.decl_indent : opt.local_decl_indent;
    735 		}
    736 		ps.in_decl = true;
    737 	}
    738 
    739 	if (ps.psyms.top == 2)
    740 		out.line_kind = lk_func_end;
    741 
    742 	parse(psym_rbrace);
    743 
    744 	if (!ps.in_var_decl
    745 	    && ps.psyms.sym[ps.psyms.top] != psym_do_stmt
    746 	    && ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
    747 		ps.force_nl = true;
    748 }
    749 
    750 static void
    751 process_period(void)
    752 {
    753 	if (code.len > 0 && code.s[code.len - 1] == ',')
    754 		buf_add_char(&code, ' ');
    755 	buf_add_char(&code, '.');
    756 	ps.want_blank = false;
    757 }
    758 
    759 static void
    760 process_unary_op(void)
    761 {
    762 	if (is_function_pointer_declaration()) {
    763 		int ind = ps.decl_ind - (int)token.len;
    764 		indent_declarator(ind, ps.tabs_to_var);
    765 		ps.want_blank = false;
    766 	} else if ((token.s[0] == '+' || token.s[0] == '-')
    767 	    && code.len > 0 && code.s[code.len - 1] == token.s[0])
    768 		ps.want_blank = true;
    769 
    770 	if (ps.want_blank)
    771 		buf_add_char(&code, ' ');
    772 	buf_add_buf(&code, &token);
    773 	ps.want_blank = false;
    774 }
    775 
    776 static void
    777 process_postfix_op(void)
    778 {
    779 	buf_add_buf(&code, &token);
    780 	ps.want_blank = true;
    781 }
    782 
    783 static void
    784 process_comma(void)
    785 {
    786 	ps.want_blank = code.len > 0;	/* only put blank after comma if comma
    787 					 * does not start the line */
    788 
    789 	if (ps.in_decl && !ps.line_has_func_def && !ps.in_init &&
    790 	    !ps.decl_indent_done && ps.ind_paren_level == 0) {
    791 		/* indent leading commas and not the actual identifiers */
    792 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
    793 	}
    794 
    795 	buf_add_char(&code, ',');
    796 
    797 	if (ps.nparen == 0) {
    798 		if (ps.init_level == 0)
    799 			ps.in_init = false;
    800 		int typical_varname_length = 8;
    801 		if (ps.break_after_comma && (opt.break_after_comma ||
    802 			ind_add(compute_code_indent(), code.s, code.len)
    803 			>= opt.max_line_length - typical_varname_length))
    804 			ps.force_nl = true;
    805 	}
    806 }
    807 
    808 static void
    809 process_label_colon(void)
    810 {
    811 	buf_add_buf(&lab, &code);
    812 	buf_add_char(&lab, ':');
    813 	buf_clear(&code);
    814 
    815 	if (ps.seen_case)
    816 		out.line_kind = lk_case_or_default;
    817 	ps.in_stmt_or_decl = false;
    818 	ps.force_nl = ps.seen_case;
    819 	ps.seen_case = false;
    820 	ps.want_blank = false;
    821 }
    822 
    823 static void
    824 process_other_colon(void)
    825 {
    826 	buf_add_char(&code, ':');
    827 	ps.want_blank = ps.decl_level == 0;
    828 }
    829 
    830 static void
    831 process_semicolon(void)
    832 {
    833 	if (out.line_kind == lk_stmt_head)
    834 		out.line_kind = lk_other;
    835 	if (ps.decl_level == 0)
    836 		ps.in_var_decl = false;
    837 	ps.seen_case = false;	/* only needs to be reset on error */
    838 	ps.quest_level = 0;	/* only needs to be reset on error */
    839 	if (ps.prev_lsym == lsym_rparen)
    840 		ps.in_func_def_params = false;
    841 	ps.in_init = false;
    842 	ps.init_level = 0;
    843 	ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
    844 
    845 	if (ps.in_decl && code.len == 0 && !ps.in_init &&
    846 	    !ps.decl_indent_done && ps.ind_paren_level == 0) {
    847 		/* indent stray semicolons in declarations */
    848 		indent_declarator(ps.decl_ind - 1, ps.tabs_to_var);
    849 	}
    850 
    851 	ps.in_decl = ps.decl_level > 0;	/* if we were in a first level
    852 					 * structure declaration before, we
    853 					 * aren't anymore */
    854 
    855 	if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) {
    856 		/* There were unbalanced parentheses in the statement. It is a
    857 		 * bit complicated, because the semicolon might be in a for
    858 		 * statement. */
    859 		diag(1, "Unbalanced parentheses");
    860 		ps.nparen = 0;
    861 		if (ps.spaced_expr_psym != psym_0) {
    862 			parse(ps.spaced_expr_psym);
    863 			ps.spaced_expr_psym = psym_0;
    864 		}
    865 	}
    866 	buf_add_char(&code, ';');
    867 	ps.want_blank = true;
    868 	ps.in_stmt_or_decl = ps.nparen > 0;
    869 	ps.decl_ind = 0;
    870 
    871 	if (ps.spaced_expr_psym == psym_0) {
    872 		parse(psym_stmt);
    873 		ps.force_nl = true;
    874 	}
    875 }
    876 
    877 static void
    878 process_type_outside_parentheses(void)
    879 {
    880 	parse(psym_decl);	/* let the parser worry about indentation */
    881 
    882 	if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1 && code.len > 0)
    883 		output_line();
    884 
    885 	if (ps.in_func_def_params && opt.indent_parameters &&
    886 	    ps.decl_level == 0) {
    887 		ps.ind_level = ps.ind_level_follow = 1;
    888 		ps.in_stmt_cont = false;
    889 	}
    890 
    891 	ps.in_var_decl = /* maybe */ true;
    892 	ps.in_decl = ps.line_has_decl = ps.prev_lsym != lsym_typedef;
    893 	if (ps.decl_level <= 0)
    894 		ps.declaration = decl_begin;
    895 
    896 	int len = (int)token.len + 1;
    897 	int ind = ps.ind_level > 0 && ps.decl_level == 0
    898 	    ? opt.local_decl_indent	/* local variable */
    899 	    : opt.decl_indent;	/* global variable, or member */
    900 	ps.decl_ind = ind > 0 ? ind : len;
    901 	ps.tabs_to_var = opt.use_tabs && ind > 0;
    902 }
    903 
    904 static void
    905 process_word(lexer_symbol lsym)
    906 {
    907 	if (ps.in_decl) {
    908 		if (lsym == lsym_funcname) {
    909 			ps.in_decl = false;
    910 			if (opt.procnames_start_line && code.len > 0)
    911 				output_line();
    912 			else if (ps.want_blank)
    913 				buf_add_char(&code, ' ');
    914 			ps.want_blank = false;
    915 
    916 		} else if (!ps.in_init && !ps.decl_indent_done &&
    917 		    ps.ind_paren_level == 0) {
    918 			if (opt.decl_indent == 0
    919 			    && code.len > 0 && code.s[code.len - 1] == '}')
    920 				ps.decl_ind = ind_add(0, code.s, code.len) + 1;
    921 			indent_declarator(ps.decl_ind, ps.tabs_to_var);
    922 			ps.want_blank = false;
    923 		}
    924 
    925 	} else if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
    926 		ps.force_nl = true;
    927 		ps.in_stmt_or_decl = false;
    928 		ps.next_unary = true;
    929 		parse(ps.spaced_expr_psym);
    930 		ps.spaced_expr_psym = psym_0;
    931 	}
    932 }
    933 
    934 static void
    935 process_do(void)
    936 {
    937 	ps.in_stmt_or_decl = false;
    938 	ps.in_decl = false;
    939 
    940 	if (code.len > 0)
    941 		output_line();
    942 
    943 	ps.force_nl = true;
    944 	parse(psym_do);
    945 }
    946 
    947 static void
    948 process_else(void)
    949 {
    950 	ps.in_stmt_or_decl = false;
    951 
    952 	if (code.len > 0
    953 	    && !(opt.cuddle_else && code.s[code.len - 1] == '}'))
    954 		output_line();
    955 
    956 	ps.force_nl = true;
    957 	parse(psym_else);
    958 }
    959 
    960 static void
    961 process_lsym(lexer_symbol lsym)
    962 {
    963 	switch (lsym) {
    964 		/* INDENT OFF */
    965 	case lsym_preprocessing: process_preprocessing(); break;
    966 	case lsym_newline:	process_newline();	break;
    967 	case lsym_comment:	process_comment();	break;
    968 	case lsym_lparen:	process_lparen();	break;
    969 	case lsym_lbracket:	process_lbracket();	break;
    970 	case lsym_rparen:	process_rparen();	break;
    971 	case lsym_rbracket:	process_rbracket();	break;
    972 	case lsym_lbrace:	process_lbrace();	break;
    973 	case lsym_rbrace:	process_rbrace();	break;
    974 	case lsym_period:	process_period();	break;
    975 	case lsym_unary_op:	process_unary_op();	break;
    976 	case lsym_postfix_op:	process_postfix_op();	break;
    977 	case lsym_binary_op:				goto copy_token;
    978 	case lsym_question:	ps.quest_level++;	goto copy_token;
    979 	case lsym_question_colon:			goto copy_token;
    980 	case lsym_label_colon:	process_label_colon();	break;
    981 	case lsym_other_colon:	process_other_colon();	break;
    982 	case lsym_comma:	process_comma();	break;
    983 	case lsym_semicolon:	process_semicolon();	break;
    984 	case lsym_typedef:				goto copy_token;
    985 	case lsym_modifier:				goto copy_token;
    986 	case lsym_case:		ps.seen_case = true;	goto copy_token;
    987 	case lsym_default:	ps.seen_case = true;	goto copy_token;
    988 	case lsym_do:		process_do();		goto copy_token;
    989 	case lsym_else:		process_else();		goto copy_token;
    990 	case lsym_for:		ps.spaced_expr_psym = psym_for_exprs; goto copy_token;
    991 	case lsym_if:		ps.spaced_expr_psym = psym_if_expr; goto copy_token;
    992 	case lsym_switch:	ps.spaced_expr_psym = psym_switch_expr; goto copy_token;
    993 	case lsym_while:	ps.spaced_expr_psym = psym_while_expr; goto copy_token;
    994 		/* INDENT ON */
    995 
    996 	case lsym_tag:
    997 		if (ps.nparen > 0)
    998 			goto copy_token;
    999 		/* FALLTHROUGH */
   1000 	case lsym_type_outside_parentheses:
   1001 		process_type_outside_parentheses();
   1002 		goto copy_token;
   1003 
   1004 	case lsym_type_in_parentheses:
   1005 	case lsym_sizeof:
   1006 	case lsym_offsetof:
   1007 	case lsym_word:
   1008 	case lsym_funcname:
   1009 	case lsym_return:
   1010 		process_word(lsym);
   1011 copy_token:
   1012 		if (ps.want_blank)
   1013 			buf_add_char(&code, ' ');
   1014 		buf_add_buf(&code, &token);
   1015 		if (lsym != lsym_funcname)
   1016 			ps.want_blank = true;
   1017 		break;
   1018 
   1019 	default:
   1020 		break;
   1021 	}
   1022 }
   1023 
   1024 static int
   1025 indent(void)
   1026 {
   1027 	debug_parser_state();
   1028 
   1029 	for (;;) {		/* loop until we reach eof */
   1030 		lexer_symbol lsym = lexi();
   1031 
   1032 		debug_blank_line();
   1033 		debug_printf("line %d: %s", line_no, lsym_name[lsym]);
   1034 		debug_print_buf("token", &token);
   1035 		debug_buffers();
   1036 		debug_blank_line();
   1037 
   1038 		if (lsym == lsym_eof)
   1039 			return process_eof();
   1040 
   1041 		if (lsym == lsym_if && ps.prev_lsym == lsym_else
   1042 		    && opt.else_if_in_same_line)
   1043 			ps.force_nl = false;
   1044 
   1045 		if (lsym == lsym_preprocessing || lsym == lsym_newline)
   1046 			ps.force_nl = false;
   1047 		else if (lsym == lsym_comment) {
   1048 			/* no special processing */
   1049 		} else {
   1050 			if (ps.force_nl && should_break_line(lsym)) {
   1051 				ps.force_nl = false;
   1052 				output_line();
   1053 			}
   1054 			ps.in_stmt_or_decl = true;
   1055 			if (com.len > 0)
   1056 				move_com_to_code(lsym);
   1057 			update_ps_lbrace_kind(lsym);
   1058 		}
   1059 
   1060 		process_lsym(lsym);
   1061 
   1062 		if (lsym != lsym_preprocessing
   1063 		    && lsym != lsym_newline
   1064 		    && lsym != lsym_comment)
   1065 			ps.prev_lsym = lsym;
   1066 
   1067 		debug_parser_state();
   1068 	}
   1069 }
   1070 
   1071 int
   1072 main(int argc, char **argv)
   1073 {
   1074 	init_globals();
   1075 	load_profiles(argc, argv);
   1076 	parse_command_line(argc, argv);
   1077 	set_initial_indentation();
   1078 	return indent();
   1079 }
   1080